-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.js
152 lines (129 loc) · 4.05 KB
/
main.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
var EC = require('elliptic').ec;
var EdDSA = require('elliptic').eddsa;
var pbkdf2 = require('pbkdf2');
var scrypt = require('scrypt-js');
var bs58 = require('bs58');
var shajs = require('sha.js')
var Ripemd160 = require('ripemd160');
var keccak = require('keccak');
var BN = require('bn.js');
var s256 = new EC('secp256k1');
var ed25519 = new EdDSA('ed25519');
function sha256(s) {
return shajs('sha256').update(s).digest();
}
function ripemd160(s) {
return (new Ripemd160()).update(s).digest();
}
function keccak256(s) {
return keccak('keccak256').update(s).digest();
}
function keccak256(s) {
return keccak('keccak256').update(s).digest();
}
function reduce32(s) {
return (new BN(s, 'le').mod(new BN('1000000000000000000000000000000014def9dea2f79cd65812631a5cf5d3ed', 16))).toArrayLike(Buffer, 'le', 32);
}
function b58checkencode(version, buffer) {
buffer = Buffer.concat([Buffer.alloc(1, version), buffer])
var hash = sha256(sha256(buffer));
buffer = Buffer.concat([buffer, hash.slice(0, 4)]);
var encoded = bs58.encode(buffer);
return encoded;
}
function keyToBitcoinish(key, version) {
var pubkey = Buffer.from(s256.keyFromPrivate(key).getPublic(false, 'hex'), 'hex');
return {
private: b58checkencode(version + 0x80, key),
public: b58checkencode(version, ripemd160(sha256(pubkey))),
};
}
function keyToBitcoin(key) {
return keyToBitcoinish(key, 0);
}
function keyToLitecoin(key) {
return keyToBitcoinish(key, 48);
}
function keyToEthereum(key) {
var pubkey = Buffer.from(s256.keyFromPrivate(key).getPublic(false, 'hex'), 'hex');
return {
private: key.toString('hex'),
public: '0x' + keccak256(pubkey.slice(1)).slice(12).toString('hex')
};
}
function keyToMonero(seed) {
var private_spend = reduce32(seed);
var private_view = reduce32(keccak256(private_spend));
// Hack
var kp = ed25519.keyFromSecret()
kp._privBytes = Array.from(private_spend);
var public_spend = Buffer.from(kp.pubBytes());
var kp = ed25519.keyFromSecret()
kp._privBytes = Array.from(private_view);
var public_view = Buffer.from(kp.pubBytes());
var address_buf = Buffer.concat([Buffer.alloc(1, 0x12), public_spend, public_view])
address_buf = Buffer.concat([address_buf, keccak256(address_buf).slice(0,4)]);
var address = ''
for (var i = 0; i < 8; i++) {
address += bs58.encode(address_buf.slice(i*8, i*8+8));
}
address += bs58.encode(address_buf.slice(64, 69));
return {
private_spend: private_spend.toString('hex'),
private_view: private_view.toString('hex'),
public_spend: public_spend.toString('hex'),
public_view: public_view.toString('hex'),
public: address
}
}
function warpwallet(password, salt, power, hashSuffix, callback) {
var password_buffer = Buffer.from(password, 'utf-8');
var salt_buffer = Buffer.from(salt, 'utf-8');
var x1 = Buffer.alloc(1, hashSuffix);
var x2 = Buffer.alloc(1, hashSuffix + 1);
scrypt(Buffer.concat([password_buffer, x1]), Buffer.concat([salt_buffer, x1]), Math.pow(2, power), 8, 1, 32, function(error, progress, key1) {
if(key1) {
pbkdf2.pbkdf2(Buffer.concat([password_buffer, x2]), Buffer.concat([salt_buffer, x2]), Math.pow(2, 16), 32, 'sha256', function(err, key2) {
for (var i = 0; i < 32; i++) {
key2[i] = key2[i] ^ key1[i];
}
//console.log(key2.toString('hex'));
callback(1, key2);
});
}
callback(progress, null);
});
}
var currencies = {
bitcoin: {
fn: keyToBitcoin,
hashSuffix: 1,
},
litecoin: {
fn: keyToLitecoin,
hashSuffix: 2,
},
monero: {
fn: keyToMonero,
hashSuffix: 3,
},
ethereum: {
fn: keyToEthereum,
hashSuffix: 4
}
}
function generateWallet(passphrase, salt, currency, callback) {
warpwallet(passphrase, salt, 18, currencies[currency].hashSuffix, function(progress, result) {
if(result) {
var wallet = currencies[currency].fn(result);
callback(1, wallet)
} else {
callback(progress, null);
}
});
}
module.exports = {
generateWallet: generateWallet
};
//warpwallet('hello', 'a@b.c', 10);
//warpwallet('hello', 'a@b.c', 18);