-
Notifications
You must be signed in to change notification settings - Fork 100
/
index.js
401 lines (331 loc) · 11.2 KB
/
index.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
var aes = require('browserify-aes')
var assert = require('assert')
var Buffer = require('safe-buffer').Buffer
var bs58check = require('bs58check')
var createHash = require('create-hash')
var scrypt = require('scryptsy')
var xor = require('buffer-xor/inplace')
var ecurve = require('ecurve')
var curve = ecurve.getCurveByName('secp256k1')
var BigInteger = require('bigi')
// constants
var SCRYPT_PARAMS = {
N: 16384, // specified by BIP38
r: 8,
p: 8
}
var NULL = Buffer.alloc(0)
function hash160 (buffer) {
var hash
try {
hash = createHash('rmd160')
} catch (e) {
hash = createHash('ripemd160')
}
return hash.update(
createHash('sha256').update(buffer).digest()
).digest()
}
function hash256 (buffer) {
return createHash('sha256').update(
createHash('sha256').update(buffer).digest()
).digest()
}
function getAddress (d, compressed) {
var Q = curve.G.multiply(d).getEncoded(compressed)
var hash = hash160(Q)
var payload = Buffer.allocUnsafe(21)
payload.writeUInt8(0x00, 0) // XXX TODO FIXME bitcoin only??? damn you BIP38
hash.copy(payload, 1)
return bs58check.encode(payload)
}
function prepareEncryptRaw (buffer, compressed, passphrase, scryptParams) {
if (buffer.length !== 32) throw new Error('Invalid private key length')
var d = BigInteger.fromBuffer(buffer)
var address = getAddress(d, compressed)
var secret = Buffer.from(passphrase.normalize('NFC'), 'utf8')
var salt = hash256(address).slice(0, 4)
var N = scryptParams.N
var r = scryptParams.r
var p = scryptParams.p
return {
secret,
salt,
N,
r,
p
}
}
function finishEncryptRaw (buffer, compressed, salt, scryptBuf) {
var derivedHalf1 = scryptBuf.slice(0, 32)
var derivedHalf2 = scryptBuf.slice(32, 64)
var xorBuf = xor(derivedHalf1, buffer)
var cipher = aes.createCipheriv('aes-256-ecb', derivedHalf2, NULL)
cipher.setAutoPadding(false)
cipher.end(xorBuf)
var cipherText = cipher.read()
// 0x01 | 0x42 | flagByte | salt (4) | cipherText (32)
var result = Buffer.allocUnsafe(7 + 32)
result.writeUInt8(0x01, 0)
result.writeUInt8(0x42, 1)
result.writeUInt8(compressed ? 0xe0 : 0xc0, 2)
salt.copy(result, 3)
cipherText.copy(result, 7)
return result
}
async function encryptRawAsync (buffer, compressed, passphrase, progressCallback, scryptParams, promiseInterval) {
scryptParams = scryptParams || SCRYPT_PARAMS
const {
secret,
salt,
N,
r,
p
} = prepareEncryptRaw(buffer, compressed, passphrase, scryptParams)
var scryptBuf = await scrypt.async(secret, salt, N, r, p, 64, progressCallback, promiseInterval)
return finishEncryptRaw(buffer, compressed, salt, scryptBuf)
}
function encryptRaw (buffer, compressed, passphrase, progressCallback, scryptParams) {
scryptParams = scryptParams || SCRYPT_PARAMS
const {
secret,
salt,
N,
r,
p
} = prepareEncryptRaw(buffer, compressed, passphrase, scryptParams)
var scryptBuf = scrypt(secret, salt, N, r, p, 64, progressCallback)
return finishEncryptRaw(buffer, compressed, salt, scryptBuf)
}
async function encryptAsync (buffer, compressed, passphrase, progressCallback, scryptParams, promiseInterval) {
return bs58check.encode(await encryptRawAsync(buffer, compressed, passphrase, progressCallback, scryptParams, promiseInterval))
}
function encrypt (buffer, compressed, passphrase, progressCallback, scryptParams) {
return bs58check.encode(encryptRaw(buffer, compressed, passphrase, progressCallback, scryptParams))
}
function prepareDecryptRaw (buffer, progressCallback, scryptParams) {
// 39 bytes: 2 bytes prefix, 37 bytes payload
if (buffer.length !== 39) throw new Error('Invalid BIP38 data length')
if (buffer.readUInt8(0) !== 0x01) throw new Error('Invalid BIP38 prefix')
// check if BIP38 EC multiply
var type = buffer.readUInt8(1)
if (type === 0x43) return { decryptEC: true }
if (type !== 0x42) throw new Error('Invalid BIP38 type')
var flagByte = buffer.readUInt8(2)
var compressed = flagByte === 0xe0
if (!compressed && flagByte !== 0xc0) throw new Error('Invalid BIP38 compression flag')
var N = scryptParams.N
var r = scryptParams.r
var p = scryptParams.p
var salt = buffer.slice(3, 7)
return {
salt,
compressed,
N,
r,
p
}
}
function finishDecryptRaw (buffer, salt, compressed, scryptBuf) {
var derivedHalf1 = scryptBuf.slice(0, 32)
var derivedHalf2 = scryptBuf.slice(32, 64)
var privKeyBuf = buffer.slice(7, 7 + 32)
var decipher = aes.createDecipheriv('aes-256-ecb', derivedHalf2, NULL)
decipher.setAutoPadding(false)
decipher.end(privKeyBuf)
var plainText = decipher.read()
var privateKey = xor(derivedHalf1, plainText)
// verify salt matches address
var d = BigInteger.fromBuffer(privateKey)
var address = getAddress(d, compressed)
var checksum = hash256(address).slice(0, 4)
assert.deepStrictEqual(salt, checksum)
return {
privateKey: privateKey,
compressed: compressed
}
}
async function decryptRawAsync (buffer, passphrase, progressCallback, scryptParams, promiseInterval) {
scryptParams = scryptParams || SCRYPT_PARAMS
const {
salt,
compressed,
N,
r,
p,
decryptEC
} = prepareDecryptRaw(buffer, progressCallback, scryptParams)
if (decryptEC === true) return decryptECMultAsync(buffer, passphrase, progressCallback, scryptParams, promiseInterval)
var scryptBuf = await scrypt.async(passphrase.normalize('NFC'), salt, N, r, p, 64, progressCallback, promiseInterval)
return finishDecryptRaw(buffer, salt, compressed, scryptBuf)
}
// some of the techniques borrowed from: https://github.com/pointbiz/bitaddress.org
function decryptRaw (buffer, passphrase, progressCallback, scryptParams) {
scryptParams = scryptParams || SCRYPT_PARAMS
const {
salt,
compressed,
N,
r,
p,
decryptEC
} = prepareDecryptRaw(buffer, progressCallback, scryptParams)
if (decryptEC === true) return decryptECMult(buffer, passphrase, progressCallback, scryptParams)
var scryptBuf = scrypt(passphrase.normalize('NFC'), salt, N, r, p, 64, progressCallback)
return finishDecryptRaw(buffer, salt, compressed, scryptBuf)
}
async function decryptAsync (string, passphrase, progressCallback, scryptParams, promiseInterval) {
return decryptRawAsync(bs58check.decode(string), passphrase, progressCallback, scryptParams, promiseInterval)
}
function decrypt (string, passphrase, progressCallback, scryptParams) {
return decryptRaw(bs58check.decode(string), passphrase, progressCallback, scryptParams)
}
function prepareDecryptECMult (buffer, passphrase, progressCallback, scryptParams) {
var flag = buffer.readUInt8(1)
var compressed = (flag & 0x20) !== 0
var hasLotSeq = (flag & 0x04) !== 0
assert.strictEqual((flag & 0x24), flag, 'Invalid private key.')
var addressHash = buffer.slice(2, 6)
var ownerEntropy = buffer.slice(6, 14)
var ownerSalt
// 4 bytes ownerSalt if 4 bytes lot/sequence
if (hasLotSeq) {
ownerSalt = ownerEntropy.slice(0, 4)
// else, 8 bytes ownerSalt
} else {
ownerSalt = ownerEntropy
}
var encryptedPart1 = buffer.slice(14, 22) // First 8 bytes
var encryptedPart2 = buffer.slice(22, 38) // 16 bytes
var N = scryptParams.N
var r = scryptParams.r
var p = scryptParams.p
return {
addressHash,
encryptedPart1,
encryptedPart2,
ownerEntropy,
ownerSalt,
hasLotSeq,
compressed,
N,
r,
p
}
}
function getPassIntAndPoint (preFactor, ownerEntropy, hasLotSeq) {
var passFactor
if (hasLotSeq) {
var hashTarget = Buffer.concat([preFactor, ownerEntropy])
passFactor = hash256(hashTarget)
} else {
passFactor = preFactor
}
const passInt = BigInteger.fromBuffer(passFactor)
return {
passInt,
passPoint: curve.G.multiply(passInt).getEncoded(true)
}
}
function finishDecryptECMult (seedBPass, encryptedPart1, encryptedPart2, passInt, compressed) {
var derivedHalf1 = seedBPass.slice(0, 32)
var derivedHalf2 = seedBPass.slice(32, 64)
var decipher = aes.createDecipheriv('aes-256-ecb', derivedHalf2, Buffer.alloc(0))
decipher.setAutoPadding(false)
decipher.end(encryptedPart2)
var decryptedPart2 = decipher.read()
var tmp = xor(decryptedPart2, derivedHalf1.slice(16, 32))
var seedBPart2 = tmp.slice(8, 16)
var decipher2 = aes.createDecipheriv('aes-256-ecb', derivedHalf2, Buffer.alloc(0))
decipher2.setAutoPadding(false)
decipher2.write(encryptedPart1) // first 8 bytes
decipher2.end(tmp.slice(0, 8)) // last 8 bytes
var seedBPart1 = xor(decipher2.read(), derivedHalf1.slice(0, 16))
var seedB = Buffer.concat([seedBPart1, seedBPart2], 24)
var factorB = BigInteger.fromBuffer(hash256(seedB))
// d = passFactor * factorB (mod n)
var d = passInt.multiply(factorB).mod(curve.n)
return {
privateKey: d.toBuffer(32),
compressed: compressed
}
}
async function decryptECMultAsync (buffer, passphrase, progressCallback, scryptParams, promiseInterval) {
buffer = buffer.slice(1) // FIXME: we can avoid this
passphrase = Buffer.from(passphrase.normalize('NFC'), 'utf8')
scryptParams = scryptParams || SCRYPT_PARAMS
const {
addressHash,
encryptedPart1,
encryptedPart2,
ownerEntropy,
ownerSalt,
hasLotSeq,
compressed,
N,
r,
p
} = prepareDecryptECMult(buffer, passphrase, progressCallback, scryptParams)
var preFactor = await scrypt.async(passphrase, ownerSalt, N, r, p, 32, progressCallback, promiseInterval)
const {
passInt,
passPoint
} = getPassIntAndPoint(preFactor, ownerEntropy, hasLotSeq)
var seedBPass = await scrypt.async(passPoint, Buffer.concat([addressHash, ownerEntropy]), 1024, 1, 1, 64, undefined, promiseInterval)
return finishDecryptECMult(seedBPass, encryptedPart1, encryptedPart2, passInt, compressed)
}
function decryptECMult (buffer, passphrase, progressCallback, scryptParams) {
buffer = buffer.slice(1) // FIXME: we can avoid this
passphrase = Buffer.from(passphrase.normalize('NFC'), 'utf8')
scryptParams = scryptParams || SCRYPT_PARAMS
const {
addressHash,
encryptedPart1,
encryptedPart2,
ownerEntropy,
ownerSalt,
hasLotSeq,
compressed,
N,
r,
p
} = prepareDecryptECMult(buffer, passphrase, progressCallback, scryptParams)
var preFactor = scrypt(passphrase, ownerSalt, N, r, p, 32, progressCallback)
const {
passInt,
passPoint
} = getPassIntAndPoint(preFactor, ownerEntropy, hasLotSeq)
var seedBPass = scrypt(passPoint, Buffer.concat([addressHash, ownerEntropy]), 1024, 1, 1, 64)
return finishDecryptECMult(seedBPass, encryptedPart1, encryptedPart2, passInt, compressed)
}
function verify (string) {
var decoded = bs58check.decodeUnsafe(string)
if (!decoded) return false
if (decoded.length !== 39) return false
if (decoded.readUInt8(0) !== 0x01) return false
var type = decoded.readUInt8(1)
var flag = decoded.readUInt8(2)
// encrypted WIF
if (type === 0x42) {
if (flag !== 0xc0 && flag !== 0xe0) return false
// EC mult
} else if (type === 0x43) {
if ((flag & ~0x24)) return false
} else {
return false
}
return true
}
module.exports = {
decrypt: decrypt,
decryptECMult: decryptECMult,
decryptRaw: decryptRaw,
encrypt: encrypt,
encryptRaw: encryptRaw,
decryptAsync: decryptAsync,
decryptECMultAsync: decryptECMultAsync,
decryptRawAsync: decryptRawAsync,
encryptAsync: encryptAsync,
encryptRawAsync: encryptRawAsync,
verify: verify
}