Refactoring

This commit is contained in:
viktor 2017-11-22 18:13:56 +03:00
parent 4bf3a99810
commit 28c6128c26
11 changed files with 879 additions and 102 deletions

790
assets/javascripts/application.js Normal file → Executable file

File diff suppressed because one or more lines are too long

View File

@ -29,48 +29,47 @@ function addValidator(web3, validatorViewObj, contractAddr, abi, cb) {
/*var funcParamsNumber = 7;
var standardLength = 32;
SHA3Encrypt(web3, func, function(funcEncode) {
var funcEncodePart = funcEncode.substring(0,10);
if (validatorViewObj.miningKey.indexOf("0x") > -1) {
validatorViewObj.miningKey = validatorViewObj.miningKey.substr(2);
}
validatorViewObj.miningKey = validatorViewObj.miningKey.toLowerCase();
let funcEncode = SHA3Encrypt(web3, func)
var funcEncodePart = funcEncode.substring(0,10);
if (validatorViewObj.miningKey.indexOf("0x") > -1) {
validatorViewObj.miningKey = validatorViewObj.miningKey.substr(2);
}
validatorViewObj.miningKey = validatorViewObj.miningKey.toLowerCase();
var fullNameHex = "0x" + toUnifiedLengthRight(toHexString(toUTF8Array(validatorViewObj.fullName)));
var streetNameHex = "0x" + toUnifiedLengthRight(toHexString(toUTF8Array(validatorViewObj.streetName)));
var stateHex = "0x" + toUnifiedLengthRight(toHexString(toUTF8Array(validatorViewObj.state)));
var fullNameHex = "0x" + toUnifiedLengthRight(toHexString(toUTF8Array(validatorViewObj.fullName)));
var streetNameHex = "0x" + toUnifiedLengthRight(toHexString(toUTF8Array(validatorViewObj.streetName)));
var stateHex = "0x" + toUnifiedLengthRight(toHexString(toUTF8Array(validatorViewObj.state)));
var parameterLocation1 = standardLength*funcParamsNumber;
var parameterLocation2 = parameterLocation1 + standardLength*(countRows(fullNameHex));
var parameterLocation3 = parameterLocation2 + standardLength*(countRows(streetNameHex));
var parameterLocation1 = standardLength*funcParamsNumber;
var parameterLocation2 = parameterLocation1 + standardLength*(countRows(fullNameHex));
var parameterLocation3 = parameterLocation2 + standardLength*(countRows(streetNameHex));
var data = funcEncodePart
+ toUnifiedLengthLeft(validatorViewObj.miningKey)
+ toUnifiedLengthLeft(validatorViewObj.zip.toString(16))
+ toUnifiedLengthLeft(validatorViewObj.licenseID.toString(16))
+ toUnifiedLengthLeft(validatorViewObj.licenseExpiredAt.toString(16))
+ toUnifiedLengthLeft(parameterLocation1.toString(16))
+ toUnifiedLengthLeft(parameterLocation2.toString(16))
+ toUnifiedLengthLeft(parameterLocation3.toString(16))
+ toUnifiedLengthLeft(bytesCount(validatorViewObj.fullName).toString(16)) + fullNameHex.substring(2)
+ toUnifiedLengthLeft(bytesCount(validatorViewObj.streetName).toString(16)) + streetNameHex.substring(2)
+ toUnifiedLengthLeft(bytesCount(validatorViewObj.state).toString(16)) + stateHex.substring(2);
var data = funcEncodePart
+ toUnifiedLengthLeft(validatorViewObj.miningKey)
+ toUnifiedLengthLeft(validatorViewObj.zip.toString(16))
+ toUnifiedLengthLeft(validatorViewObj.licenseID.toString(16))
+ toUnifiedLengthLeft(validatorViewObj.licenseExpiredAt.toString(16))
+ toUnifiedLengthLeft(parameterLocation1.toString(16))
+ toUnifiedLengthLeft(parameterLocation2.toString(16))
+ toUnifiedLengthLeft(parameterLocation3.toString(16))
+ toUnifiedLengthLeft(bytesCount(validatorViewObj.fullName).toString(16)) + fullNameHex.substring(2)
+ toUnifiedLengthLeft(bytesCount(validatorViewObj.streetName).toString(16)) + streetNameHex.substring(2)
+ toUnifiedLengthLeft(bytesCount(validatorViewObj.state).toString(16)) + stateHex.substring(2);
getGasPrice(function(gasPrice) {
console.log(gasPrice);
estimateGas(web3, address, contractAddr, data, null, function(estimatedGas, err) {
getGasPrice(function(gasPrice) {
console.log(gasPrice);
estimateGas(web3, address, contractAddr, data, null, function(estimatedGas, err) {
if (err) {
cb(null, err);
return;
}
estimatedGas += 100000;
sendTx(web3, address, contractAddr, data, null, estimatedGas, gasPrice, function(txHash, err) {
if (err) {
cb(null, err);
cb(txHash, err);
return;
}
estimatedGas += 100000;
sendTx(web3, address, contractAddr, data, null, estimatedGas, gasPrice, function(txHash, err) {
if (err) {
cb(txHash, err);
return;
}
cb(txHash);
});
cb(txHash);
});
});
});*/

View File

@ -1,6 +1,6 @@
function SHA3Encrypt(web3, str, cb) {
function SHA3Encrypt(web3, str) {
var strEncode = web3.sha3(str);
cb(strEncode);
return strEncode;
}
function estimateGas(web3, from, to, data, val, cb) {

View File

@ -2,19 +2,18 @@ function checkInitialKey(web3, func, initialKey, contractAddr, cb) {
var funcParamsNumber = 1;
var standardLength = 32;
SHA3Encrypt(web3, func, function(funcEncode) {
var funcEncodePart = funcEncode.substring(0,10);
let funcEncode = SHA3Encrypt(web3, func)
var funcEncodePart = funcEncode.substring(0,10);
var data = funcEncodePart
+ toUnifiedLengthLeft(initialKey);
var data = funcEncodePart
+ toUnifiedLengthLeft(initialKey);
console.log(data);
console.log("0x" + initialKey);
console.log(contractAddr);
console.log(data);
console.log("0x" + initialKey);
console.log(contractAddr);
call(web3, "0x" + initialKey, contractAddr, data, function(respHex) {
console.log(respHex);
cb(parseInt(respHex, 16));
});
call(web3, "0x" + initialKey, contractAddr, data, function(respHex) {
console.log(respHex);
cb(parseInt(respHex, 16));
});
}

View File

@ -1,34 +1,35 @@
//check current network page is connected to. Alerts, if not Oracles network
function checkNetworkVersion(web3, cb) {
async function checkNetworkVersion(web3, cb) {
var msgNotOracles = "You aren't connected to Oracles network. Please, switch on Oracles plugin and choose Oracles network. Check Oracles network <a href='https://github.com/oraclesorg/oracles-wiki' target='blank'>wiki</a> for more info.";
web3.version.getNetwork(function(err, netId) {
if (err)
console.log(err);
console.log("netId: " + netId);
switch (netId) {
case "1": {
let config = await getConfig()
web3.version.getNetwork(function(err, connectedNetworkID) {
if (err) console.log(err);
console.log("connectedNetworkID: " + connectedNetworkID);
connectedNetworkID = parseInt(connectedNetworkID);
switch (connectedNetworkID) {
case 1: {
console.log('This is mainnet');
swal("Warning", msgNotOracles, "warning");
cb(false);
return false;
} break;
case "2": {
case 2: {
console.log('This is the deprecated Morden test network.');
swal("Warning", msgNotOracles, "warning");
cb(false);
return false;
} break;
case "3": {
case 3: {
console.log('This is the ropsten test network.');
swal("Warning", msgNotOracles, "warning");
cb(false);
return false;
} break;
case "12648430": {
case config.networkID: {
console.log('This is Oracles from Metamask');
cb(true);
return true;
} break;
default: {
console.log('This is an unknown network.');
swal("Warning", msgNotOracles, "warning");
cb(false);
return false;
} break;
}
})

View File

@ -27,24 +27,23 @@ function createKeys(web3, keys, contractAddr, abi, cb) {
var parameterLocation = standardLength*funcParamsNumber;
SHA3Encrypt(web3, func, function(funcEncode) {
var funcEncodePart = funcEncode.substring(0,10);
let funcEncode = SHA3Encrypt(web3, func)
var funcEncodePart = funcEncode.substring(0,10);
var data = funcEncodePart
+ toUnifiedLengthLeft(keys.miningKey.miningKeyObject.address)
+ toUnifiedLengthLeft(keys.payoutKey.payoutKeyObject.address)
+ toUnifiedLengthLeft(keys.votingKey.votingKeyObject.address);
var data = funcEncodePart
+ toUnifiedLengthLeft(keys.miningKey.miningKeyObject.address)
+ toUnifiedLengthLeft(keys.payoutKey.payoutKeyObject.address)
+ toUnifiedLengthLeft(keys.votingKey.votingKeyObject.address);
getGasPrice(function(gasPrice) {
estimateGas(web3, address, contractAddr, data, null, function(estimatedGas) {
estimatedGas += 100000;
sendTx(web3, address, contractAddr, data, null, estimatedGas, gasPrice, function(txHash, err) {
if (err) {
cb(txHash, err);
return;
}
cb(txHash);
});
getGasPrice(function(gasPrice) {
estimateGas(web3, address, contractAddr, data, null, function(estimatedGas) {
estimatedGas += 100000;
sendTx(web3, address, contractAddr, data, null, estimatedGas, gasPrice, function(txHash, err) {
if (err) {
cb(txHash, err);
return;
}
cb(txHash);
});
});
});*/

View File

@ -1,8 +1,14 @@
//gets config file with address of Oracles contract
function getConfig(cb) {
$.getJSON("./assets/javascripts/config.json", function(config) {
var contractAddress = config.Ethereum[config.environment].contractAddress;
var abi = config.Ethereum[config.environment].abi;
cb(contractAddress, abi);
});
async function getConfig(cb) {
let config = await $.getJSON("./assets/javascripts/config.json")
let contractAddress = config.Ethereum[config.environment].contractAddress
let abi = config.Ethereum[config.environment].abi
let networkID = config.networkID
let configJSON = {
contractAddress,
networkID,
abi
}
if (cb) cb(configJSON)
return configJSON;
}

View File

@ -15,8 +15,7 @@ function getWeb3(callback) {
// new instance
myWeb3.eth.defaultAccount = window.web3.eth.defaultAccount;
checkNetworkVersion(myWeb3, function(isOraclesNetwork) {
callback(myWeb3, isOraclesNetwork);
});
let isOraclesNetwork = checkNetworkVersion(myWeb3)
callback(myWeb3, isOraclesNetwork);
}
}

View File

@ -13,10 +13,9 @@ function startDapp(web3, isOraclesNetwork) {
"votingKey": {}
};
getAccounts(function(accounts) {
getConfig(function(contractAddress, abi) {
getConfigCallBack(web3, accounts, contractAddress, abi);
});
getAccounts(async function(accounts) {
let config = await getConfig()
getConfigCallBack(web3, accounts, config.contractAddress, config.abi)
});
//getting of config callback
@ -308,4 +307,4 @@ function startDapp(web3, isOraclesNetwork) {
window.addEventListener('load', function() {
getWeb3(startDapp);
});
});

View File

@ -1,5 +1,6 @@
{
"environment": "live",
"networkID": 12648430,
"Ethereum": {
"live": {
"contractAddress": "0xf472e0e43570b9afaab67089615080cf7c20018d",

View File

@ -22,7 +22,7 @@
"sass": "gulp sass",
"coffee": "gulp javascript",
"watch": "gulp watch",
"start": "http-server -a localhost -p 8000"
"start": "gulp javascript && http-server -a localhost -p 8000"
},
"repository": {
"type": "git",