Skip to content

Commit

Permalink
Add Ethereum
Browse files Browse the repository at this point in the history
changed files:
	- index.html
	- brainwallet.js
	- electrum.js
	- armory.js
	- bitcoinsig.js
added files:
	- web3-eth-accounts.js

Changes:
- Add ethereum (web3-eth-accounts.js + empty file web3-eth-accounts.js.map).
	-- Add Ethereum in dropdown menu.
	-- Hex -> to Ethereum privkey. Fix key length if this hex not contains 32 bytes.
	-- Privkey -> to Ethereum address
	-- Change size to 5 - for qrcode of ethereum private key.
	-- Fix addition slash in the link to block-explorers for qr-code with address.
	-- Armory and Electrum chains supporting Ethereum now. width for chain list changed to 1000px to display ethereum list fully.
	-- Sign and verify message on client-sied - supporting Ethereum now. Add notification for not supporting methods.
	-- Verify lowerCase address, like specified here: https://www.myetherwallet.com/signmsg.html Switch ETH address to LowerCase automatically.
	-- Key_file - now can be available to download 32-byte keyfile, corresponding to Ethereum privkey.
	-- Add notification with link on MyEtherWallet in Transactions tab. Make all fields readonly when Ethereum selected.
	-- Add title tooltip when ethereum privkey entered, but ETH - not selected in dropdown menu.
	-- Add ethereum to test signature function. See commented code in bitcoinsig.js
	-- Add speed test code to test signing and verify messages (commented in bitcoinsig.js).
	
- Add regenerate address from privkey, on Transactions tab, when another coin selected in dropdown menu.
- Fix console.log error on transactions tab: Uncaught SyntaxError: Unexpected end of input, when unspent outputs not loaded.

- Add timeout for generate keys, using Armory and Electrum determininstic chains generators. Now list of chains filling slowly.

See source code.
  • Loading branch information
username1565 committed Nov 14, 2018
1 parent e355453 commit 19961e0
Show file tree
Hide file tree
Showing 6 changed files with 47,445 additions and 73 deletions.
9 changes: 8 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

<script src="js/bootstrap.min.js"></script>
<script src="js/bitcoinjs-min.js"></script>
<script src="js/web3-eth-accounts.js">//generate Ethereum address from the secret exponent hex. Sign messages and verify sinature.</script>
<script src="js/qrcode.js"></script>
<script src="js/rfc1751.js"></script>
<script src="js/mnemonic.js"></script>
Expand Down Expand Up @@ -57,6 +58,12 @@
-->
<!--To using new SegWit address with bytes [0x00, 0x14] add title="SegWit address" to the tag a (see tooltips)-->
<li><a data-target="0x00" href="http://blockchain.info"><span class="pull-right">BTC</span>Bitcoin</a></li>
<li><a data-target="uncompressed, 0x00, 0x00" strMessageMagic="\x19Ethereum Signed Message:\n" href="http://etherscan.io/"><span class="pull-right">ETH</span>Ethereum</a></li>
<!--
The data will be UTF-8 HEX decoded and enveloped as follows:
"\x19Ethereum Signed Message:\n" + message.length + message
and hashed using keccak256.
-->
<li><a data-target="compressed, 0x19, 0xbf" strMessageMagic="Signatum Signed Message:\n" href="http://explorer.signatum.io"><span class="pull-right">SIGT</span>Signatum</a></li>
<li><a data-target="uncompressed,0x34" href="#generator"><span class="pull-right">611</span>SixEleven</a></li>
<li><a data-target="compressed, 0x21, 0x80" href="http://explorer.signatum.io"><span class="pull-right">EMC</span>EmerCoin</a></li>
Expand Down Expand Up @@ -321,7 +328,7 @@
<div class="form-group">
<label class="col-lg-2 control-label" for="chList">Chain</label>
<div class="col-lg-10 controls">
<textarea class="form-control" id="chList" readonly="readonly" rows="13"></textarea>
<textarea class="form-control" id="chList" readonly="readonly" rows="13" style="width:1000px"></textarea>
</div>
</div>
</fieldset>
Expand Down
14 changes: 11 additions & 3 deletions js/armory.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ var Armory = new function () {
var onSuccess;
var onUpdate;

function calcAddr(PUBLIC_KEY_VERSION, PRIVATE_KEY_VERSION, ADDRESS_URL_PREFIX, compressed) {
function calcAddr(PUBLIC_KEY_VERSION, PRIVATE_KEY_VERSION, ADDRESS_URL_PREFIX, compressed) {
var r = armory_extend_chain(pubKey, chainCode, privKey, true, PUBLIC_KEY_VERSION, PRIVATE_KEY_VERSION, ADDRESS_URL_PREFIX, compressed);
onUpdate(r);
pubKey = r[2];
Expand All @@ -176,12 +176,19 @@ var Armory = new function () {
if(counter==1){firstpriv = privKey;}

if (counter < range) {
timeout = setTimeout(calcAddr(PUBLIC_KEY_VERSION, PRIVATE_KEY_VERSION, ADDRESS_URL_PREFIX, compressed), 0);

timeout = setTimeout(
function(){
calcAddr(PUBLIC_KEY_VERSION, PRIVATE_KEY_VERSION, ADDRESS_URL_PREFIX, compressed);
},
0
);

} else {
if (onSuccess)
onSuccess();
}
}
}

this.gen = function(seed, _range, update, success, PUBLIC_KEY_VERSION, PRIVATE_KEY_VERSION, ADDRESS_URL_PREFIX, compressed){
var keys = armory_decode_keys(seed);
Expand Down Expand Up @@ -241,6 +248,7 @@ function armory_split_string(str) {

function armory_sign_message(private_key, address, message, compressed, addrtype, mode)
{
//console.log('armory_sign_message function - armory.js');
// armory needs \r\n for some reason
message = message.replace(/\n/g,'\r\n');

Expand Down
206 changes: 162 additions & 44 deletions js/bitcoinsig.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ function msg_digest(message, strMessageMagic) {
}

function verify_message(signature, message, strMessageMagic, addrtype, compressed) {
//console.log('verify_message -> ', signature, message, strMessageMagic, addrtype, compressed); //diso
if(signature===false){console.log('signature not specified! signature = ', signature); return;}

if(signature.substring(0, 2)==='0x'){ //if signature beginning from '0x' - maybe this can be ethereum signature.
//console.log('Try to check ethereum signature:');
const account = new Web3EthAccounts();
return account.recover(message, signature); //recover address from signature and hash of message
}
//else try to check bitcoin and altcoins signature...

/*
console.log('verify_message:\n',
Expand Down Expand Up @@ -108,6 +117,18 @@ function verify_message(signature, message, strMessageMagic, addrtype, compresse
}

function sign_message(private_key, message, strMessageMagic, compressed, addrtype) {
//console.log('sign_message function in bitcoinsig.js:');

if(private_key.toString().substring(0, 2)==='0x'){ //if private key is not bigInteger, and this string beginning from '0x' - then Ethereum selected
//console.log(private_key, message, strMessageMagic);

const account = new Web3EthAccounts();
var keyPair = account.privateKeyToAccount(private_key);
var signed_message = keyPair.sign(message); //strMessageMagic contains, inside web3-eth-accounts.js

return signed_message.signature;
}


//console.log("sign_message: strMessageMagic", strMessageMagic);
if (!private_key)
Expand Down Expand Up @@ -154,65 +175,81 @@ function parseBase58Check(address) {
}

//function to make tests. See console.log (F12-button)
function altcoinssig_test(k, a, s, m, strMessageMagic) {
function altcoinssig_test(k, a, s, m, strMessageMagic, no_console_log) {
//k - private key WIF
//a - address Base58Check
//s - digital signature Base58Check
//m - message text
//strMessageMagic - prefix string

//test output in console
console.log(
'TEST ',
//just escape '\n' to show. If undefined - show default parameter for bitocoin
' strMessageMagic:',
(
(typeof strMessageMagic !== 'undefined') //if not undefined
? strMessageMagic.split('\n').join('\\n') //show with escaped '\n'
: "Bitcoin Signed Message:\\n" //else, if undefined - show default with escaped '\n'
),
'\n', //LF
' key:', k, '\n',
' address:', a, '\n',
' specified_signature:', s, '\n',
' message_text:', m, '\n' //LF again
);


if(no_console_log===true){}
else{
//test output in console
console.log(
'TEST ',
//just escape '\n' to show. If undefined - show default parameter for bitocoin
' strMessageMagic:',
(
(typeof strMessageMagic !== 'undefined') //if not undefined
? strMessageMagic.split('\n').join('\\n') //show with escaped '\n'
: "Bitcoin Signed Message:\\n" //else, if undefined - show default with escaped '\n'
),
'\n', //LF
' key:', k, '\n',
' address:', a, '\n',
' specified_signature:', s, '\n',
' message_text:', m, '\n' //LF again
);
}

if(k.substring(0, 2)!=='0x'){ //if not bitcoin or altcoins private key...
//working with private key
payload = Bitcoin.Base58.decode(k); //parse private key to byte array
secret = payload.slice(1, 33); //slice first prefix byte and leave key bytes

//uncompressed private key contains [prefix_byte + 32 bytes + (0x01, if compressed)].
//byte array contains +4 bytes base58check checksum. this is CRC32 bytes of previous string.
//So payload length is 1+32+4 = 37 for uncompressed private key,
//and 1+32+1+4 = 38 for compressed priv.
compressed = payload.length == 38; //set compressed, if private key is compressed (37 bytes for uncompressed, and 38 for compressed)

var eckey = new Bitcoin.ECKey(secret); //get eckey
eckey.setCompressed(compressed); //set compressed, if compressed

//working with private key
payload = Bitcoin.Base58.decode(k); //parse private key to byte array
secret = payload.slice(1, 33); //slice first prefix byte and leave key bytes

//uncompressed private key contains [prefix_byte + 32 bytes + (0x01, if compressed)].
//byte array contains +4 bytes base58check checksum. this is CRC32 bytes of previous string.
//So payload length is 1+32+4 = 37 for uncompressed private key,
//and 1+32+1+4 = 38 for compressed priv.
compressed = payload.length == 38; //set compressed, if private key is compressed (37 bytes for uncompressed, and 38 for compressed)

var eckey = new Bitcoin.ECKey(secret); //get eckey
eckey.setCompressed(compressed); //set compressed, if compressed
addr = parseBase58Check(a); //get addr version from specified addr
//console.log('addr', addr);
}

addr = parseBase58Check(a); //get addr version from specified addr
//console.log('addr', addr);

console.log(
if(no_console_log===true){}
else{
console.log(
'1. Check specified signature:\n',
'check s =',s,'\n',
'status:', (verify_message(s, m, strMessageMagic, addr[0], compressed)===a), '\n',
'signer address:', verify_message(s, m, strMessageMagic, addr[0], compressed)
);

sig = sign_message(eckey, m, strMessageMagic, compressed);
console.log('\n2. Try to sign again:', sig, '\n');
);
}

if(k.substring(0, 2)!=='0x'){
sig = sign_message(eckey, m, strMessageMagic, compressed);
}
else{
sig = sign_message(k, m); //strMessageMagic for ethereum not specified, and contains inside web3-eth-accounts.js functions...
}

if(no_console_log===true){}
else{

console.log('\n2. Try to sign again:', sig, '\n');

console.log(
console.log(
'3. Check re-signed message:\n',
' check sig =', sig, '\n',
'status:', (verify_message(s, m, strMessageMagic, addr[0], compressed)===a), '\n',
'signer address:', verify_message(sig, m, strMessageMagic, addr[0], compressed),
'\n\n'
);
);
}
}

/**
Expand Down Expand Up @@ -408,9 +445,6 @@ function altcoinssig_test(k, a, s, m, strMessageMagic) {
altcoinssig_test(k, a, s, m, strMessageMagic); //run test
//___________________________________________________________________________________________________________________________
//and also, here can be test for many other altcoins...
//Just delete / * and * / to uncomment this.
//___________________________________________________________________________________________________________________________
//10. Vertcoin - compressed key and address
Expand All @@ -429,12 +463,96 @@ function altcoinssig_test(k, a, s, m, strMessageMagic) {
var s = 'IFUDvvSBNlBLkTztW4SWO/FbgRgPs7PVqpIAuhexJHX9qV3kjdaRPW2vDo+eZNcXkk1Lt/Qe6tdgr4nogvpmWIM=';
var strMessageMagic = "Vertcoin Signed Message:\n"; //from validation.cpp
altcoinssig_test(k, a, s, m, strMessageMagic); //run test
//___________________________________________________________________________________________________________________________
//___________________________________________________________________________________________________________________________
//11. Ethereum - private key from hex of "Secret Exponent"
//signed in brainwallet
//verified in vertcoin-qt.exe
//-----BEGIN BITCOIN SIGNED MESSAGE-----
//This is an example of a signed message.
//-----BEGIN SIGNATURE-----
//0x8c1BD965E272A529270c72f5c4B8F334e8aBD856
//0x8366dd7ad92ed39a2b2dc882bc6ff0faec2e2bc2a58007e65fe7dc632b8540184740967e497edc3dc97797cd3deb2b31857b1acb4cca57256b444f38741a18a01c
//-----END BITCOIN SIGNED MESSAGE-----
var k = '0x06c2b92f6210bcee01c9c3fde8e10b51c5e8882efecec51aef96e6aa4910fd6c';
var a = '0x8c1BD965E272A529270c72f5c4B8F334e8aBD856';
var m = 'This is an example of a signed message.';
var s = '0x8366dd7ad92ed39a2b2dc882bc6ff0faec2e2bc2a58007e65fe7dc632b8540184740967e497edc3dc97797cd3deb2b31857b1acb4cca57256b444f38741a18a01c';
var strMessageMagic = "\x19Ethereum Signed Message:\n"; //from validation.cpp
altcoinssig_test(k, a, s, m, strMessageMagic); //run test
//___________________________________________________________________________________________________________________________
//and also, here can be test for many other altcoins...
//Just delete / * and * / to uncomment this.
*/


/*
//speed test for checking signatures:
var k = 'KwSrQfTpAq6zodDmBvk8RzgqL9D35EFcDPg4NEprfzv77VyMr6Kc';
var a = 'VejdSd7zrm3XSok4CC8t94rTfZVuBwUStu';
var m = 'This is an example of a signed message.';
var s = 'IFUDvvSBNlBLkTztW4SWO/FbgRgPs7PVqpIAuhexJHX9qV3kjdaRPW2vDo+eZNcXkk1Lt/Qe6tdgr4nogvpmWIM=';
var strMessageMagic = "Vertcoin Signed Message:\n"; //from validation.cpp
var start = Date.now();
var start2 = start;
var period = 1; //period time seconds
var periods = 20; //number of periods for all test
var iterations = 0;
var min_iterations = 0;
var max_iterations = 0;
var no_console_log = true; //disable console.log notifications?
//if this false, then notifications will be show in console.log,
//and result will have lower values...
console.log("Wait "+(period*periods)+" seconds... ("+periods+" periods by "+period+" seconds)");
while(Date.now()<=start+periods*period*1000){ //up to all periods time
while(Date.now()<=start2+period*1000){ //up to period time
//run this function
altcoinssig_test(k, a, s, m, strMessageMagic, no_console_log); //run test with no_console_log===true
//end run this function
iterations++; //count iterations
}
if(min_iterations==0){min_iterations = iterations;} //set value of minimum iterations for one period
if(min_iterations>iterations){min_iterations = iterations;} //change this if not minimum
if(max_iterations<iterations){max_iterations = iterations;} //set value of maximum iterations for one period
start2=Date.now(); //update time
if(no_console_log===true){}
else{
console.log(
'period', Math.floor((start2-start)/(period*1000)),'\n',
'iterations', iterations,'\n',
'minimum iterations per',period,'seconds:', min_iterations,'\n',
'maximum iterations per',period,'seconds:', max_iterations
);
}
iterations=0; //reset iterations
}
console.log(
'result\n',
'minimum iterations per',period,'seconds:', min_iterations,'\n',
'maximum iterations per',period,'seconds:', max_iterations
);
//end speed test
*/



if (typeof require != 'undefined' && require.main === module) {
window = global; navigator = {}; Bitcoin = {};
eval(require('fs').readFileSync('./bitcoinjs-min.js')+'');
Expand Down
Loading

0 comments on commit 19961e0

Please sign in to comment.