/sign_intent.js Secret
Last active
September 1, 2016 15:02
Star
You must be signed in to star a gist
sign_intent.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function pad(pad, str, padLeft) { | |
if (typeof str === 'undefined') | |
return pad; | |
if (padLeft) { | |
return (pad + str).slice(-pad.length); | |
} else { | |
return (str + pad).substring(0, pad.length); | |
} | |
} | |
function asciiToHex(str) { | |
var res = ""; | |
for (var i = 0; i < str.length; i++) { | |
res += str.charCodeAt(i).toString(16); | |
} | |
return res; | |
} | |
function padTo64Bytes(hexstr) { | |
return pad('0000000000000000000000000000000000000000000000000000000000000000', hexstr, true); | |
} | |
function isNumeric(n) { | |
return !isNaN(parseFloat(n)) && isFinite(n); | |
} | |
function signIntent(fromAddress, beneficiaryETC, percentageWHG) { | |
if (!isNumeric(percentageWHG)) { | |
console.log("ERROR: percentageWHG should be a number!"); | |
return; | |
} | |
if (percentageWHG < 0 || percentageWHG > 100) { | |
console.log("ERROR: percentageWHG should be a number between 0 and 100!"); | |
return; | |
} | |
var text = asciiToHex("Withdraw DAOETC to ") | |
+ beneficiaryETC.substr(2, beneficiaryETC.length) | |
+ padTo64Bytes(percentageWHG.toString(16)); | |
personal.unlockAccount(fromAddress); | |
var sig = web3.eth.sign(fromAddress, web3.sha3(text, {encoding: 'hex'})); | |
return [beneficiaryETC, percentageWHG, sig]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment