From 28c6128c267ab89036ef4aab400cf18a31a8598f Mon Sep 17 00:00:00 2001 From: viktor Date: Wed, 22 Nov 2017 18:13:56 +0300 Subject: [PATCH] Refactoring --- assets/javascripts/application.js | 790 +++++++++++++++++- .../javascripts/application/addValidator.js | 69 +- .../application/blockchainHelper.js | 4 +- .../application/checkInitialKey.js | 21 +- .../application/checkNetworkVersion.js | 31 +- assets/javascripts/application/createKeys.js | 31 +- assets/javascripts/application/getConfig.js | 18 +- assets/javascripts/application/getWeb3.js | 5 +- assets/javascripts/application/index.js | 9 +- assets/javascripts/config.json | 1 + package.json | 2 +- 11 files changed, 879 insertions(+), 102 deletions(-) mode change 100644 => 100755 assets/javascripts/application.js diff --git a/assets/javascripts/application.js b/assets/javascripts/application.js old mode 100644 new mode 100755 index c6df4e9..0cc6d26 --- a/assets/javascripts/application.js +++ b/assets/javascripts/application.js @@ -1,8 +1,782 @@ -function generateAddress(t){var e={keyBytes:32,ivBytes:16};keythereum.create(e);keythereum.create(e,function(e){var n={},r=generatePassword();keythereum.dump(r,e.privateKey,e.salt,e.iv,n,function(e){console.log(e),console.log(JSON.stringify(e)),t(e,r)})})}function generatePassword(){for(var t="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",e="",n=0,r=t.length;n<8;++n)e+=t.charAt(Math.floor(Math.random()*r));return e}function addValidator(t,e,n,r,o){console.log("***Add validator function***"),attachToContract(t,r,n,function(t,n){if(console.log("attach to oracles contract"),t)return console.log(t),o();console.log(e),console.log(n),n.addValidator.sendTransaction(e.miningKey,e.zip,e.licenseID,e.licenseExpiredAt,e.fullName,e.streetName,e.state,function(t,e){if(t)return void o(e,t);o(e)})})}function showAlert(t,e){t?"REQUEST_REJECTED"!=t.type&&swal({title:"Error",text:e,type:"error"}):swal({title:"Error",text:e,type:"error"})}function SHA3Encrypt(t,e,n){n(t.sha3(e))}function estimateGas(t,e,n,r,o,i){var a;a=o?{from:e,value:o,data:null,to:n}:{from:e,data:r,to:n},t.eth.estimateGas(a,function(t,e){t&&console.log(t),console.log(e),i(e)})}function sendTx(t,e,n,r,o,i,a,s){var c;c=o?{from:e,value:o,to:n,gas:i,gasPrice:a}:{from:e,data:r,to:n,gas:i,gasPrice:a},t.eth.sendTransaction(c,function(t,e){s(e,t)})}function call(t,e,n,r,o){var i;i=e?{from:e,data:r,to:n}:{data:r,to:n},t.eth.call(i,function(t,e){o(e)})}function getBalance(t,e){web3.eth.getBalance(t,function(t,n){if(t)return console.log(t),void $(".loading-container").hide();e(n)})}function getGasPrice(t){web3.eth.getGasPrice(function(e,n){if(e)return console.log(e),void $(".loading-container").hide();console.log(n);var r=n.c[0];t(r)})}function attachToContract(t,e,n,r){if(t.isConnected()){t.eth.defaultAccount=t.eth.accounts[0],console.log("web3.eth.defaultAccount:"+t.eth.defaultAccount);var o=t.eth.contract(e),i=o.at(n);r&&r(null,i)}else r&&r({code:200,title:"Error",message:"check RPC availability"})}function checkInitialKey(t,e,n,r,o){SHA3Encrypt(t,e,function(e){var i=e.substring(0,10),a=i+toUnifiedLengthLeft(n);console.log(a),console.log("0x"+n),console.log(r),call(t,"0x"+n,r,a,function(t){console.log(t),o(parseInt(t,16))})})}function checkNetworkVersion(t,e){var n="You aren't connected to Oracles network. Please, switch on Oracles plugin and choose Oracles network. Check Oracles network wiki for more info.";t.version.getNetwork(function(t,r){switch(t&&console.log(t),console.log("netId: "+r),r){case"1":console.log("This is mainnet"),swal("Warning",n,"warning"),e(!1);break;case"2":console.log("This is the deprecated Morden test network."),swal("Warning",n,"warning"),e(!1);break;case"3":console.log("This is the ropsten test network."),swal("Warning",n,"warning"),e(!1);break;case"12648430":console.log("This is Oracles from Metamask"),e(!0);break;default:console.log("This is an unknown network."),swal("Warning",n,"warning"),e(!1)}})}function toUnifiedLengthLeft(t){for(var e="",n=0;n<64-t.length;n++)e+="0";return e+=t}function countRows(t){return t.length%64>0?parseInt(t.length/64)+1:parseInt(t.length/64)}function toUnifiedLengthRight(t){var e="";e+=t;for(var n=countRows(t),r=0;r<64*n-t.length;r++)e+="0";return e}function toUTF8Array(t){for(var e=[],n=0;n>6,128|63&r):r<55296||r>=57344?e.push(224|r>>12,128|r>>6&63,128|63&r):(n++,r=65536+((1023&r)<<10|1023&t.charCodeAt(n)),e.push(240|r>>18,128|r>>12&63,128|r>>6&63,128|63&r))}return e}function toHexString(t){return t.map(function(t){return("0"+(255&t).toString(16)).slice(-2)}).join("")}function bytesCount(t){return encodeURI(t).split(/%..|./).length-1}function createKeys(t,e,n,r,o){console.log("***Create keys function***"),attachToContract(t,r,n,function(t,n){if(console.log("attach to oracles contract"),t)return console.log(t),o();console.log(e),n.createKeys.sendTransaction("0x"+e.miningKey.miningKeyObject.address,"0x"+e.payoutKey.payoutKeyObject.address,"0x"+e.votingKey.votingKeyObject.address,function(t,e){if(t)return void o(e,t);o(e)})})}function download(t,e){var n=document.createElement("a");n.setAttribute("href","data:application/json;charset=utf-8,"+encodeURIComponent(e)),n.setAttribute("download",t),n.style.display="none",document.body.appendChild(n),n.click(),document.body.removeChild(n)}function getAccounts(t){web3.eth.getAccounts(function(e,n){if(e)return $(".loading-container").hide(),void showAlert(e,e.message);t(n)})}function getConfig(t){$.getJSON("./assets/javascripts/config.json",function(e){var n=e.Ethereum[e.environment].contractAddress,r=e.Ethereum[e.environment].abi;t(n,r)})}function getWeb3(t){if(void 0===window.web3){console.error("Please use a web3 browser");swal("Warning","You aren't connected to Oracles Network. Please, switch on Oracles plugin and refresh the page. Check Oracles network wiki for more info.","warning"),t(e,!1)}else{var e=new Web3(window.web3.currentProvider);e.eth.defaultAccount=window.web3.eth.defaultAccount,checkNetworkVersion(e,function(n){t(e,n)})}}function startDapp(t,e){$(function(){function n(t,e,n,o){if(1==e.length){checkInitialKey(t,"checkInitialKey(address)",e[0].substr(2),n,function(t){(t=!!+t)||swal("Warning","Current key isn't valid initial key. Please, choose your initial key in MetaMask and reload the page. Check Oracles network wiki for more info.","warning")})}else 0==e.length&&swal("Warning","You haven't chosen any account in MetaMask. Please, choose your initial key in MetaMask and reload the page. Check Oracles network wiki for more info.","warning");$(".create-keys-button").click(function(){$("#initialKeySource").click()}),$("#initialKeySource").change({contractAddress:n,abi:o},r)}function r(t){o(this,t.data.contractAddress,t.data.abi,function(e){i(m,function(n){s(t.data.contractAddress,t.data.abi,n,e,function(t,e){c(t,e,n)})})})}function o(e,n,o,i){var s=$(e).prop("files")[0];$(e).remove();$("").change({contractAddress:n,abi:o},r).appendTo($(".create-keys"));var c=new FileReader;c.readAsText(s,"UTF-8"),c.onload=function(e){try{a=JSON.parse(e.target.result)}catch(t){return swal("Error","Invalid key file","error")}var r=JSON.parse(e.target.result),o=r.address;if(!o)return swal("Error","No address in key file","error");checkInitialKey(t,"checkInitialKey(address)",o,n,function(t){if(!(t=!!+t))return swal("Error","Initial key is already activated or isn't valid","error");$(".loading-container").show(),setTimeout(function(){i(o)},500)})},c.onerror=function(t){swal("Error","Error in reading file","error")}}function i(t,e){var n=0;for(var r in t)n++;var o=0;generateAddress(function(r,i){o++,t.miningKey={},r.name="miningKey",t.miningKey.miningKeyObject=r,t.miningKey.password=i,o==n&&e(t)}),generateAddress(function(r,i){o++,t.payoutKey={},r.name="payoutKey",t.payoutKey.payoutKeyObject=r,t.payoutKey.password=i,o==n&&e(t)}),generateAddress(function(r,i){o++,t.votingKey={},r.name="votingKey",t.votingKey.votingKeyObject=r,t.votingKey.password=i,o==n&&e(t)})}function s(e,n,r,o,i){$(".content").hide(),$(".waiting-container").show(),$(".waiting-container").empty(),$(".waiting-container").append("

Adding notary's data to Oracles contract...

");var a={miningKey:"0x"+r.miningKey.miningKeyObject.address,fullName:$("#full-name").val(),streetName:$("#address").val(),state:$("#state").val(),zip:$("#zip").val(),licenseID:$("#license-id").val(),licenseExpiredAt:new Date($("#license-expiration").val()).getTime()/1e3};addValidator(t,a,e,n,function(a,s){if(s)return d(),void("REQUEST_REJECTED"!=s.type&&swal("Error","Error in addresses addition to contract","error"));$(".content").hide(),$(".waiting-container").show(),$(".waiting-container").empty(),$(".waiting-container").append("

Adding production keys to Oracles contract...

"),createKeys(t,r,e,n,function(t,e){if(e)return d(),console.log(e.message),void("REQUEST_REJECTED"!=e.type&&swal("Error","Error in addresses addition to contract","error"));i(e,o)})})}function c(t,e,n){if($(".content").hide(),$(".waiting-container").show(),$(".waiting-container").empty(),$(".waiting-container").append("

Transfering ether from initial key to payout key...

"),t)return d(),void swal("Error",t.message,"error");var r="0x"+n.payoutKey.payoutKeyObject.address;getBalance(e,function(t){getGasPrice(function(n){u(e,r,t,function(o){l(o,n,t,function(t){f(o,n,e,r,t)})})})})}function u(e,n,r,o){estimateGas(t,e,n,null,parseInt(r/2),function(t,e){if(e)return console.log(e),void d();o(t)})}function l(t,e,n,r){var o=n-20*t*e;console.log("ammountToSend: "+o),r(o)}function f(e,n,r,o,i){t.eth.sendTransaction({gas:e,gasPrice:n,from:r,to:o,value:i},function(t,e){if(t)return console.log(t),void d();d(),swal("Success","Keys are created","success"),$(".content").empty(),p()})}function p(){$(".content").load("./keys.html",function(){$("#miningKey").text("0x"+m.miningKey.miningKeyObject.address),$("#payoutKey").text("0x"+m.payoutKey.payoutKeyObject.address),$("#votingKey").text("0x"+m.votingKey.votingKeyObject.address),$("#miningKeyPass").text(m.miningKey.password),$("#payoutKeyPass").text(m.payoutKey.password),$("#votingKeyPass").text(m.votingKey.password),$("#copyMiningPass").attr("data-clipboard-text",m.miningKey.password),$("#copyPayoutPass").attr("data-clipboard-text",m.payoutKey.password),$("#copyVotingPass").attr("data-clipboard-text",m.votingKey.password),h("copyMiningPass","Mining key password copied"),h("copyPayoutPass","Payout key password copied"),h("copyVotingPass","Voting key password copied"),$("#copyMiningKey").attr("data-clipboard-text","0x"+m.miningKey.miningKeyObject.address),$("#copyPayoutKey").attr("data-clipboard-text","0x"+m.payoutKey.payoutKeyObject.address),$("#copyVotingKey").attr("data-clipboard-text","0x"+m.votingKey.votingKeyObject.address),h("copyMiningKey","Mining key copied"),h("copyPayoutKey","Payout key copied"),h("copyVotingKey","Voting key copied"),$("#miningKeyDownload").click(function(){download("mining_key_"+m.miningKey.miningKeyObject.address,JSON.stringify(m.miningKey.miningKeyObject))}),$("#payoutKeyDownload").click(function(){download("payout_key_"+m.payoutKey.payoutKeyObject.address,JSON.stringify(m.payoutKey.payoutKeyObject))}),$("#votingKeyDownload").click(function(){download("voting_key_"+m.votingKey.votingKeyObject.address,JSON.stringify(m.votingKey.votingKeyObject))})})}function d(){$(".loading-container").hide(),$(".waiting-container").hide(),$(".content").show()}function h(t,e){var n=document.getElementById(t);new Clipboard(n).on("success",function(t){toastr.success(e)})}if($(".loading-container").hide(),e){var m={miningKey:{},payoutKey:{},votingKey:{}};getAccounts(function(e){getConfig(function(r,o){n(t,e,r,o)})})}})}!function(t,e){"object"==typeof module&&"object"==typeof module.exports?module.exports=t.document?e(t,!0):function(t){if(!t.document)throw new Error("jQuery requires a window with a document");return e(t)}:e(t)}("undefined"!=typeof window?window:this,function(t,e){function n(t){var e=!!t&&"length"in t&&t.length,n=it.type(t);return"function"!==n&&!it.isWindow(t)&&("array"===n||0===e||"number"==typeof e&&e>0&&e-1 in t)}function r(t,e,n){if(it.isFunction(e))return it.grep(t,function(t,r){return!!e.call(t,r,t)!==n});if(e.nodeType)return it.grep(t,function(t){return t===e!==n});if("string"==typeof e){if(mt.test(e))return it.filter(e,t,n);e=it.filter(e,t)}return it.grep(t,function(t){return Z.call(e,t)>-1!==n})}function o(t,e){for(;(t=t[e])&&1!==t.nodeType;);return t}function i(t){var e={};return it.each(t.match(wt)||[],function(t,n){e[n]=!0}),e}function a(){X.removeEventListener("DOMContentLoaded",a),t.removeEventListener("load",a),it.ready()}function s(){this.expando=it.expando+s.uid++}function c(t,e,n){var r;if(void 0===n&&1===t.nodeType)if(r="data-"+e.replace(At,"-$&").toLowerCase(),"string"==typeof(n=t.getAttribute(r))){try{n="true"===n||"false"!==n&&("null"===n?null:+n+""===n?+n:St.test(n)?it.parseJSON(n):n)}catch(t){}Tt.set(t,e,n)}else n=void 0;return n}function u(t,e,n,r){var o,i=1,a=20,s=r?function(){return r.cur()}:function(){return it.css(t,e,"")},c=s(),u=n&&n[3]||(it.cssNumber[e]?"":"px"),l=(it.cssNumber[e]||"px"!==u&&+c)&&Bt.exec(it.css(t,e));if(l&&l[3]!==u){u=u||l[3],n=n||[],l=+c||1;do{i=i||".5",l/=i,it.style(t,e,l+u)}while(i!==(i=s()/c)&&1!==i&&--a)}return n&&(l=+l||+c||0,o=n[1]?l+(n[1]+1)*n[2]:+n[2],r&&(r.unit=u,r.start=l,r.end=o)),o}function l(t,e){var n=void 0!==t.getElementsByTagName?t.getElementsByTagName(e||"*"):void 0!==t.querySelectorAll?t.querySelectorAll(e||"*"):[];return void 0===e||e&&it.nodeName(t,e)?it.merge([t],n):n}function f(t,e){for(var n=0,r=t.length;r>n;n++)Ct.set(t[n],"globalEval",!e||Ct.get(e[n],"globalEval"))}function p(t,e,n,r,o){for(var i,a,s,c,u,p,d=e.createDocumentFragment(),h=[],m=0,y=t.length;y>m;m++)if((i=t[m])||0===i)if("object"===it.type(i))it.merge(h,i.nodeType?[i]:i);else if(It.test(i)){for(a=a||d.appendChild(e.createElement("div")),s=(Ft.exec(i)||["",""])[1].toLowerCase(),c=Pt[s]||Pt._default,a.innerHTML=c[1]+it.htmlPrefilter(i)+c[2],p=c[0];p--;)a=a.lastChild;it.merge(h,a.childNodes),a=d.firstChild,a.textContent=""}else h.push(e.createTextNode(i));for(d.textContent="",m=0;i=h[m++];)if(r&&it.inArray(i,r)>-1)o&&o.push(i);else if(u=it.contains(i.ownerDocument,i),a=l(d.appendChild(i),"script"),u&&f(a),n)for(p=0;i=a[p++];)jt.test(i.type||"")&&n.push(i);return d}function d(){return!0}function h(){return!1}function m(){try{return X.activeElement}catch(t){}}function y(t,e,n,r,o,i){var a,s;if("object"==typeof e){"string"!=typeof n&&(r=r||n,n=void 0);for(s in e)y(t,s,n,r,e[s],i);return t}if(null==r&&null==o?(o=n,r=n=void 0):null==o&&("string"==typeof n?(o=r,r=void 0):(o=r,r=n,n=void 0)),!1===o)o=h;else if(!o)return t;return 1===i&&(a=o,o=function(t){return it().off(t),a.apply(this,arguments)},o.guid=a.guid||(a.guid=it.guid++)),t.each(function(){it.event.add(this,e,o,r,n)})}function g(t,e){return it.nodeName(t,"table")&&it.nodeName(11!==e.nodeType?e:e.firstChild,"tr")?t.getElementsByTagName("tbody")[0]||t.appendChild(t.ownerDocument.createElement("tbody")):t}function v(t){return t.type=(null!==t.getAttribute("type"))+"/"+t.type,t}function b(t){var e=$t.exec(t.type);return e?t.type=e[1]:t.removeAttribute("type"),t}function w(t,e){var n,r,o,i,a,s,c,u;if(1===e.nodeType){if(Ct.hasData(t)&&(i=Ct.access(t),a=Ct.set(e,i),u=i.events)){delete a.handle,a.events={};for(o in u)for(n=0,r=u[o].length;r>n;n++)it.event.add(e,o,u[o][n])}Tt.hasData(t)&&(s=Tt.access(t),c=it.extend({},s),Tt.set(e,c))}}function x(t,e){var n=e.nodeName.toLowerCase();"input"===n&&Dt.test(t.type)?e.checked=t.checked:"input"!==n&&"textarea"!==n||(e.defaultValue=t.defaultValue)}function k(t,e,n,r){e=Y.apply([],e);var o,i,a,s,c,u,f=0,d=t.length,h=d-1,m=e[0],y=it.isFunction(m);if(y||d>1&&"string"==typeof m&&!rt.checkClone&&zt.test(m))return t.each(function(o){var i=t.eq(o);y&&(e[0]=m.call(this,o,i.html())),k(i,e,n,r)});if(d&&(o=p(e,t[0].ownerDocument,!1,t,r),i=o.firstChild,1===o.childNodes.length&&(o=i),i||r)){for(a=it.map(l(o,"script"),v),s=a.length;d>f;f++)c=o,f!==h&&(c=it.clone(c,!0,!0),s&&it.merge(a,l(c,"script"))),n.call(t[f],c,f);if(s)for(u=a[a.length-1].ownerDocument,it.map(a,b),f=0;s>f;f++)c=a[f],jt.test(c.type||"")&&!Ct.access(c,"globalEval")&&it.contains(u,c)&&(c.src?it._evalUrl&&it._evalUrl(c.src):it.globalEval(c.textContent.replace(Wt,"")))}return t}function _(t,e,n){for(var r,o=e?it.filter(e,t):t,i=0;null!=(r=o[i]);i++)n||1!==r.nodeType||it.cleanData(l(r)),r.parentNode&&(n&&it.contains(r.ownerDocument,r)&&f(l(r,"script")),r.parentNode.removeChild(r));return t}function C(t,e){var n=it(e.createElement(t)).appendTo(e.body),r=it.css(n[0],"display");return n.detach(),r}function T(t){var e=X,n=Ut[t];return n||(n=C(t,e),"none"!==n&&n||(Kt=(Kt||it("