forked from Chaine-de-Blocs/v0.blocs.fr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen.js
64 lines (46 loc) · 1.93 KB
/
gen.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
var window = self;
importScripts('./bower_components/kjur-jsrsasign/jsrsasign-all-min.js', './lib/Base58/Base58.js');
function toBytes(value) {
var arr = new Uint8Array(value.length / 2);
for (var i = 0; i < value.length; i += 2) {
arr[i / 2] = parseInt(value.substring(i, i + 2), 16);
}
return arr;
}
do {
var ec = new KJUR.crypto.ECDSA({'curve': 'secp256k1'});
// Generates two randoms points (x,y) with corresponding public key and private key
var keypair = ec.generateKeyPairHex();
var privkey = keypair.ecprvhex;
var pubkey = keypair.ecpubhex;
/**********************************************/
/* PUBLIC ADDRESS */
/**********************************************/
var pubSha = KJUR.crypto.Util.hashHex(pubkey, 'sha256');
// Adds leading 0x00 on ripemd160 of hashed public key for signaling on main network
var ripemd = '0' + KJUR.crypto.Util.hashHex(pubSha, 'ripemd160');
var pubRipemd = (0x00).toString(16) + ripemd;
// Double hash with sha256
var pubShaRipemd = KJUR.crypto.Util.hashHex(pubRipemd, 'sha256');
var pubShaShaRipemd = KJUR.crypto.Util.hashHex(pubShaRipemd, 'sha256');
// 4 leading bytes as checksum
var checksum = pubShaShaRipemd.slice(0, 8);
// Final readable public address
var fullAddress = pubRipemd.concat(checksum);
/**********************************************/
/* PRIVATE KEY (WIP) */
/**********************************************/
// Wallet Import Format
// Adds leading 0x80 for main network
privkey = (0x80).toString(16) + privkey;
// Double hash of private key from ECDSA
var shaShaPrivkey = KJUR.crypto.Util.hashHex(KJUR.crypto.Util.hashHex(privkey, 'sha256'), 'sha256');
// 4 leading bytes as checksum
var privChecksum = shaShaPrivkey.slice(0, 8);
// Final readable WIP
privkey = privkey.concat(privChecksum);
postMessage({
pub: Base58.encode(toBytes(fullAddress)),
priv: Base58.encode(toBytes(privkey))
});
} while(true);