@bitpay-labs/bitcore-client 11.6.6 → 11.7.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/bin/wallet-derive +5 -6
- package/bin/wallet-send +3 -2
- package/package.json +22 -5
- package/src/client.ts +8 -4
- package/src/encryption.ts +64 -12
- package/src/storage/mongo.ts +5 -1
- package/src/storage/textFile.ts +74 -0
- package/src/storage.ts +82 -3
- package/src/wallet.ts +292 -62
- package/test/unit/data/ethMigrationTestWallet.fixture.ts +68 -0
- package/test/unit/wallet.test.ts +639 -15
package/test/unit/wallet.test.ts
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import * as chai from 'chai';
|
|
2
2
|
import * as CWC from '@bitpay-labs/crypto-wallet-core';
|
|
3
|
-
import
|
|
3
|
+
import fs from 'fs';
|
|
4
|
+
import os from 'os';
|
|
5
|
+
import path from 'path';
|
|
6
|
+
import { AddressTypes, IWalletExt, Wallet } from '../../src/wallet';
|
|
7
|
+
import { Encryption } from '../../src/encryption';
|
|
4
8
|
import { Api as bcnApi } from '../../../bitcore-node/build/src/services/api';
|
|
5
9
|
import { Storage as bcnStorage } from '../../../bitcore-node/build/src/services/storage';
|
|
6
10
|
import crypto from 'crypto';
|
|
@@ -12,6 +16,7 @@ import sinon from 'sinon';
|
|
|
12
16
|
import { StorageType } from '../../src/types/storage';
|
|
13
17
|
import supertest from 'supertest';
|
|
14
18
|
import { utils } from '../../src/utils';
|
|
19
|
+
import ethMigrationTestWalletFixture from './data/ethMigrationTestWallet.fixture';
|
|
15
20
|
|
|
16
21
|
|
|
17
22
|
const should = chai.should();
|
|
@@ -31,6 +36,19 @@ describe('Wallet', function() {
|
|
|
31
36
|
let walletName;
|
|
32
37
|
let wallet: Wallet;
|
|
33
38
|
let api;
|
|
39
|
+
const walletsToCleanup: Array<{ name: string; storageType?: StorageType; path?: string }> = [];
|
|
40
|
+
const createWalletForTest = async (params: Partial<IWalletExt>) => {
|
|
41
|
+
if (!params.name) {
|
|
42
|
+
throw new Error('Tests must provide a wallet name');
|
|
43
|
+
}
|
|
44
|
+
const createdWallet = await Wallet.create(params);
|
|
45
|
+
walletsToCleanup.push({
|
|
46
|
+
name: params.name,
|
|
47
|
+
storageType: params.storageType,
|
|
48
|
+
path: params.path
|
|
49
|
+
});
|
|
50
|
+
return createdWallet;
|
|
51
|
+
};
|
|
34
52
|
before(async function() {
|
|
35
53
|
this.timeout(20000);
|
|
36
54
|
await bcnStorage.start({
|
|
@@ -64,7 +82,12 @@ describe('Wallet', function() {
|
|
|
64
82
|
});
|
|
65
83
|
});
|
|
66
84
|
afterEach(async function() {
|
|
67
|
-
|
|
85
|
+
while (walletsToCleanup.length) {
|
|
86
|
+
const walletToCleanup = walletsToCleanup.pop();
|
|
87
|
+
if (walletToCleanup) {
|
|
88
|
+
await Wallet.deleteWallet(walletToCleanup);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
68
91
|
sandbox.restore();
|
|
69
92
|
});
|
|
70
93
|
for (const chain of ['BTC', 'BCH', 'LTC', 'DOGE', 'ETH', 'XRP', 'MATIC']) {
|
|
@@ -73,7 +96,7 @@ describe('Wallet', function() {
|
|
|
73
96
|
it(`should create a wallet for chain and addressType: ${chain} ${addressType}`, async function() {
|
|
74
97
|
walletName = 'BitcoreClientTest' + chain + addressType;
|
|
75
98
|
|
|
76
|
-
wallet = await
|
|
99
|
+
wallet = await createWalletForTest({
|
|
77
100
|
chain,
|
|
78
101
|
network: 'mainnet',
|
|
79
102
|
name: walletName,
|
|
@@ -82,7 +105,8 @@ describe('Wallet', function() {
|
|
|
82
105
|
lite: false,
|
|
83
106
|
addressType,
|
|
84
107
|
storageType,
|
|
85
|
-
baseUrl
|
|
108
|
+
baseUrl,
|
|
109
|
+
version: 0
|
|
86
110
|
});
|
|
87
111
|
|
|
88
112
|
expect(wallet.addressType).to.equal(AddressTypes[chain]?.[addressType] || 'pubkeyhash');
|
|
@@ -116,14 +140,15 @@ describe('Wallet', function() {
|
|
|
116
140
|
walletName = 'BitcoreClientTestBumpFee-UTXO';
|
|
117
141
|
|
|
118
142
|
beforeEach(async function() {
|
|
119
|
-
wallet = await
|
|
143
|
+
wallet = await createWalletForTest({
|
|
120
144
|
name: walletName,
|
|
121
145
|
chain: 'BTC',
|
|
122
146
|
network: 'testnet',
|
|
123
147
|
phrase: 'snap impact summer because must pipe weasel gorilla actor acid web whip',
|
|
124
148
|
password: 'abc123',
|
|
125
149
|
storageType,
|
|
126
|
-
baseUrl
|
|
150
|
+
baseUrl,
|
|
151
|
+
// version: 0
|
|
127
152
|
});
|
|
128
153
|
await wallet.unlock('abc123');
|
|
129
154
|
});
|
|
@@ -192,14 +217,15 @@ describe('Wallet', function() {
|
|
|
192
217
|
walletName = 'BitcoreClientTestBumpFee-EVM';
|
|
193
218
|
|
|
194
219
|
beforeEach(async function() {
|
|
195
|
-
wallet = await
|
|
220
|
+
wallet = await createWalletForTest({
|
|
196
221
|
name: walletName,
|
|
197
222
|
chain: 'ETH',
|
|
198
223
|
network: 'testnet',
|
|
199
224
|
phrase: 'snap impact summer because must pipe weasel gorilla actor acid web whip',
|
|
200
225
|
password: 'abc123',
|
|
201
226
|
storageType,
|
|
202
|
-
baseUrl
|
|
227
|
+
baseUrl,
|
|
228
|
+
version: 0
|
|
203
229
|
});
|
|
204
230
|
await wallet.unlock('abc123');
|
|
205
231
|
});
|
|
@@ -262,7 +288,8 @@ describe('Wallet', function() {
|
|
|
262
288
|
password: 'abc123',
|
|
263
289
|
storageType,
|
|
264
290
|
path,
|
|
265
|
-
baseUrl
|
|
291
|
+
baseUrl,
|
|
292
|
+
version: 0
|
|
266
293
|
});
|
|
267
294
|
await wallet.unlock('abc123');
|
|
268
295
|
// 3 address pairs
|
|
@@ -296,14 +323,15 @@ describe('Wallet', function() {
|
|
|
296
323
|
let sleepStub;
|
|
297
324
|
|
|
298
325
|
beforeEach(async function() {
|
|
299
|
-
wallet = await
|
|
326
|
+
wallet = await createWalletForTest({
|
|
300
327
|
name: walletName,
|
|
301
328
|
chain: 'BTC',
|
|
302
329
|
network: 'testnet',
|
|
303
330
|
phrase: 'snap impact summer because must pipe weasel gorilla actor acid web whip',
|
|
304
331
|
password: 'abc123',
|
|
305
332
|
storageType,
|
|
306
|
-
baseUrl
|
|
333
|
+
baseUrl,
|
|
334
|
+
version: 0
|
|
307
335
|
});
|
|
308
336
|
await wallet.unlock('abc123');
|
|
309
337
|
requestStub = sandbox.stub(wallet.client, '_request').resolves();
|
|
@@ -366,18 +394,612 @@ describe('Wallet', function() {
|
|
|
366
394
|
sleepStub.callCount.should.equal(1);
|
|
367
395
|
requestStub.args.flatMap(arg => arg[0].body).should.deep.equal(keys.map(k => ({ address: k.address })));
|
|
368
396
|
});
|
|
397
|
+
|
|
398
|
+
it('can derive the public key if not included', async function () {
|
|
399
|
+
const keys = [];
|
|
400
|
+
for (let i = 0; i < 101; i++) {
|
|
401
|
+
const pk = crypto.randomBytes(32).toString('hex');
|
|
402
|
+
keys.push({
|
|
403
|
+
privKey: pk,
|
|
404
|
+
address: libMap.BTC.PrivateKey(pk).toAddress().toString()
|
|
405
|
+
});
|
|
406
|
+
}
|
|
407
|
+
const getPublicKeyStub = sandbox.stub(CWC.Deriver, 'getPublicKey').returns('mockedPubKey');
|
|
408
|
+
sandbox.stub(wallet.storage, 'addKeysSafe').resolves();
|
|
409
|
+
|
|
410
|
+
await wallet.importKeys({
|
|
411
|
+
keys,
|
|
412
|
+
rederiveAddys: false
|
|
413
|
+
});
|
|
414
|
+
|
|
415
|
+
getPublicKeyStub.callCount.should.be.greaterThan(0);
|
|
416
|
+
});
|
|
417
|
+
|
|
418
|
+
it('encrypts key.privKey only', async function () {
|
|
419
|
+
const keys = [];
|
|
420
|
+
for (let i = 0; i < 1; i++) {
|
|
421
|
+
const pk = crypto.randomBytes(32).toString('hex');
|
|
422
|
+
keys.push({
|
|
423
|
+
privKey: pk,
|
|
424
|
+
address: libMap.BTC.PrivateKey(pk).toAddress().toString()
|
|
425
|
+
});
|
|
426
|
+
}
|
|
427
|
+
const addKeysSafeStub = sandbox.stub(wallet.storage, 'addKeysSafe').resolves();
|
|
428
|
+
|
|
429
|
+
await wallet.importKeys({
|
|
430
|
+
keys,
|
|
431
|
+
rederiveAddys: false
|
|
432
|
+
});
|
|
433
|
+
|
|
434
|
+
addKeysSafeStub.calledOnce.should.equal(true);
|
|
435
|
+
const savedKeys = addKeysSafeStub.firstCall.args[0].keys;
|
|
436
|
+
|
|
437
|
+
for (const originalKey of keys) {
|
|
438
|
+
const matchingSavedKey = savedKeys.find(sk => {
|
|
439
|
+
// Match on pubKey only if it was in originalKey
|
|
440
|
+
return !(originalKey.pubKey && sk.pubKey === originalKey.pubKey) && sk.address === originalKey.address;
|
|
441
|
+
});
|
|
442
|
+
expect(matchingSavedKey).to.exist;
|
|
443
|
+
matchingSavedKey.privKey.should.not.equal(originalKey.privKey);
|
|
444
|
+
}
|
|
445
|
+
});
|
|
446
|
+
|
|
447
|
+
it('can rederive addresses', async function () {
|
|
448
|
+
const keys = [];
|
|
449
|
+
for (let i = 0; i < 1; i++) {
|
|
450
|
+
const pk = crypto.randomBytes(32).toString('hex');
|
|
451
|
+
keys.push({
|
|
452
|
+
privKey: pk,
|
|
453
|
+
address: libMap.BTC.PrivateKey(pk).toAddress().toString()
|
|
454
|
+
});
|
|
455
|
+
}
|
|
456
|
+
const getPublicKeyStub = sandbox.stub(CWC.Deriver, 'getPublicKey').returns('mockedPubKey');
|
|
457
|
+
const getAddressStub = sandbox.stub(CWC.Deriver, 'getAddress').returns('mockedAddress');
|
|
458
|
+
sandbox.stub(wallet.storage, 'addKeysSafe').resolves();
|
|
459
|
+
|
|
460
|
+
await wallet.importKeys({
|
|
461
|
+
keys,
|
|
462
|
+
rederiveAddys: true
|
|
463
|
+
});
|
|
464
|
+
|
|
465
|
+
getAddressStub.callCount.should.be.greaterThan(0);
|
|
466
|
+
});
|
|
467
|
+
});
|
|
468
|
+
|
|
469
|
+
describe('signTx', function() {
|
|
470
|
+
let txStub: sinon.SinonStub;
|
|
471
|
+
afterEach(async function() {
|
|
472
|
+
sandbox.restore();
|
|
473
|
+
});
|
|
474
|
+
|
|
475
|
+
describe('BTC (UTXO) decrypts ciphertext to WIF', function() {
|
|
476
|
+
walletName = 'BitcoreClientTestSignTxV2-BTC';
|
|
477
|
+
let wallet: Wallet;
|
|
478
|
+
|
|
479
|
+
beforeEach(async function() {
|
|
480
|
+
wallet = await createWalletForTest({
|
|
481
|
+
name: walletName,
|
|
482
|
+
chain: 'BTC',
|
|
483
|
+
network: 'testnet',
|
|
484
|
+
phrase: 'snap impact summer because must pipe weasel gorilla actor acid web whip',
|
|
485
|
+
password: 'abc123',
|
|
486
|
+
storageType,
|
|
487
|
+
baseUrl
|
|
488
|
+
});
|
|
489
|
+
await wallet.unlock('abc123');
|
|
490
|
+
});
|
|
491
|
+
|
|
492
|
+
it('should decrypt stored ciphertext and hand WIF to Transactions.sign', async function() {
|
|
493
|
+
const pk = new CWC.BitcoreLib.PrivateKey(undefined, 'testnet');
|
|
494
|
+
const address = pk.toAddress().toString();
|
|
495
|
+
const privBuf = CWC.Deriver.privateKeyToBuffer('BTC', pk.toString());
|
|
496
|
+
// v2 key encryption uses the key's pubKey as the IV salt (not the wallet pubKey)
|
|
497
|
+
const encPriv = Encryption.encryptBuffer(privBuf, pk.publicKey.toString(), wallet.unlocked.encryptionKey).toString('hex');
|
|
498
|
+
privBuf.fill(0);
|
|
499
|
+
|
|
500
|
+
sandbox.stub(wallet.storage, 'getStoredKeys').resolves([
|
|
501
|
+
{
|
|
502
|
+
address,
|
|
503
|
+
privKey: encPriv,
|
|
504
|
+
pubKey: pk.publicKey.toString()
|
|
505
|
+
}
|
|
506
|
+
]);
|
|
507
|
+
sandbox.stub(wallet, 'derivePrivateKey').resolves({
|
|
508
|
+
address: 'change',
|
|
509
|
+
privKey: pk.toString(),
|
|
510
|
+
pubKey: pk.publicKey.toString(),
|
|
511
|
+
path: 'm/1/0'
|
|
512
|
+
});
|
|
513
|
+
sandbox.stub(wallet, 'importKeys').resolves();
|
|
514
|
+
|
|
515
|
+
let capturedPayload;
|
|
516
|
+
txStub = sandbox.stub(CWC.Transactions, 'sign').callsFake(payload => {
|
|
517
|
+
capturedPayload = payload;
|
|
518
|
+
return 'signed';
|
|
519
|
+
});
|
|
520
|
+
|
|
521
|
+
const utxos = [{ address, value: 1 }];
|
|
522
|
+
await wallet.signTx({ tx: 'raw', utxos });
|
|
523
|
+
|
|
524
|
+
txStub.calledOnce.should.equal(true);
|
|
525
|
+
capturedPayload.keys[0].privKey.should.equal(pk.toWIF());
|
|
526
|
+
capturedPayload.key.privKey.should.equal(pk.toWIF());
|
|
527
|
+
});
|
|
528
|
+
});
|
|
529
|
+
|
|
530
|
+
describe('ETH (account) decrypts ciphertext to hex and skips plaintext', function() {
|
|
531
|
+
walletName = 'BitcoreClientTestSignTxV2-ETH';
|
|
532
|
+
let wallet: Wallet;
|
|
533
|
+
|
|
534
|
+
beforeEach(async function() {
|
|
535
|
+
wallet = await createWalletForTest({
|
|
536
|
+
name: walletName,
|
|
537
|
+
chain: 'ETH',
|
|
538
|
+
network: 'testnet',
|
|
539
|
+
phrase: 'snap impact summer because must pipe weasel gorilla actor acid web whip',
|
|
540
|
+
password: 'abc123',
|
|
541
|
+
storageType,
|
|
542
|
+
baseUrl
|
|
543
|
+
});
|
|
544
|
+
await wallet.unlock('abc123');
|
|
545
|
+
});
|
|
546
|
+
|
|
547
|
+
it('should decrypt stored ciphertext and hand hex privKey to Transactions.sign', async function() {
|
|
548
|
+
const privHex = crypto.randomBytes(32).toString('hex');
|
|
549
|
+
const privBufForPubKey = CWC.Deriver.privateKeyToBuffer('ETH', privHex);
|
|
550
|
+
const pubKey = CWC.Deriver.getPublicKey('ETH', wallet.network, privBufForPubKey);
|
|
551
|
+
privBufForPubKey.fill(0);
|
|
552
|
+
const privBuf = CWC.Deriver.privateKeyToBuffer('ETH', privHex);
|
|
553
|
+
const encPriv = Encryption.encryptBuffer(privBuf, pubKey, wallet.unlocked.encryptionKey).toString('hex');
|
|
554
|
+
privBuf.fill(0);
|
|
555
|
+
|
|
556
|
+
let capturedPayload;
|
|
557
|
+
txStub = sandbox.stub(CWC.Transactions, 'sign').callsFake(payload => {
|
|
558
|
+
capturedPayload = payload;
|
|
559
|
+
return 'signed';
|
|
560
|
+
});
|
|
561
|
+
|
|
562
|
+
const signingKeys = [{ address: '0xabc', privKey: encPriv, pubKey }];
|
|
563
|
+
await wallet.signTx({ tx: 'raw', signingKeys });
|
|
564
|
+
|
|
565
|
+
txStub.calledOnce.should.equal(true);
|
|
566
|
+
capturedPayload.keys[0].privKey.should.equal(privHex);
|
|
567
|
+
});
|
|
568
|
+
});
|
|
569
|
+
|
|
570
|
+
describe('XRP (account) decrypts ciphertext to uppercase hex and skips plaintext', function() {
|
|
571
|
+
walletName = 'BitcoreClientTestSignTxV2-XRP';
|
|
572
|
+
let wallet: Wallet;
|
|
573
|
+
|
|
574
|
+
beforeEach(async function() {
|
|
575
|
+
wallet = await createWalletForTest({
|
|
576
|
+
name: walletName,
|
|
577
|
+
chain: 'XRP',
|
|
578
|
+
network: 'testnet',
|
|
579
|
+
phrase: 'snap impact summer because must pipe weasel gorilla actor acid web whip',
|
|
580
|
+
password: 'abc123',
|
|
581
|
+
storageType,
|
|
582
|
+
baseUrl
|
|
583
|
+
});
|
|
584
|
+
await wallet.unlock('abc123');
|
|
585
|
+
});
|
|
586
|
+
|
|
587
|
+
it('should decrypt stored ciphertext and hand uppercase hex privKey to Transactions.sign', async function() {
|
|
588
|
+
const privHex = crypto.randomBytes(32).toString('hex').toUpperCase();
|
|
589
|
+
const privBufForPubKey = CWC.Deriver.privateKeyToBuffer('XRP', privHex);
|
|
590
|
+
const pubKey = CWC.Deriver.getPublicKey('XRP', wallet.network, privBufForPubKey);
|
|
591
|
+
privBufForPubKey.fill(0);
|
|
592
|
+
const privBuf = CWC.Deriver.privateKeyToBuffer('XRP', privHex);
|
|
593
|
+
const encPriv = Encryption.encryptBuffer(privBuf, pubKey, wallet.unlocked.encryptionKey).toString('hex');
|
|
594
|
+
privBuf.fill(0);
|
|
595
|
+
|
|
596
|
+
let capturedPayload;
|
|
597
|
+
txStub = sandbox.stub(CWC.Transactions, 'sign').callsFake(payload => {
|
|
598
|
+
capturedPayload = payload;
|
|
599
|
+
return 'signed';
|
|
600
|
+
});
|
|
601
|
+
|
|
602
|
+
const signingKeys = [{ address: 'rabc', privKey: encPriv, pubKey }];
|
|
603
|
+
await wallet.signTx({ tx: 'raw', signingKeys });
|
|
604
|
+
|
|
605
|
+
txStub.calledOnce.should.equal(true);
|
|
606
|
+
capturedPayload.keys[0].privKey.should.equal(privHex);
|
|
607
|
+
});
|
|
608
|
+
});
|
|
609
|
+
|
|
610
|
+
describe('SOL (account) decrypts ciphertext to base58 and skips plaintext', function() {
|
|
611
|
+
walletName = 'BitcoreClientTestSignTxV2-SOL';
|
|
612
|
+
let wallet: Wallet;
|
|
613
|
+
|
|
614
|
+
beforeEach(async function() {
|
|
615
|
+
wallet = await createWalletForTest({
|
|
616
|
+
name: walletName,
|
|
617
|
+
chain: 'SOL',
|
|
618
|
+
network: 'devnet',
|
|
619
|
+
phrase: 'snap impact summer because must pipe weasel gorilla actor acid web whip',
|
|
620
|
+
password: 'abc123',
|
|
621
|
+
storageType,
|
|
622
|
+
baseUrl
|
|
623
|
+
});
|
|
624
|
+
await wallet.unlock('abc123');
|
|
625
|
+
});
|
|
626
|
+
|
|
627
|
+
it('should decrypt stored ciphertext and hand base58 privKey to Transactions.sign', async function() {
|
|
628
|
+
const privBufForPubKey = crypto.randomBytes(32);
|
|
629
|
+
const pubKey = CWC.Deriver.getPublicKey('SOL', wallet.network, privBufForPubKey);
|
|
630
|
+
const privBuf = Buffer.from(privBufForPubKey);
|
|
631
|
+
privBufForPubKey.fill(0);
|
|
632
|
+
const encPriv = Encryption.encryptBuffer(privBuf, pubKey, wallet.unlocked.encryptionKey).toString('hex');
|
|
633
|
+
const expectedPrivKey = CWC.Deriver.bufferToPrivateKey_TEMP('SOL', wallet.network, Buffer.from(privBuf));
|
|
634
|
+
privBuf.fill(0);
|
|
635
|
+
|
|
636
|
+
let capturedPayload;
|
|
637
|
+
txStub = sandbox.stub(CWC.Transactions, 'sign').callsFake(payload => {
|
|
638
|
+
capturedPayload = payload;
|
|
639
|
+
return Promise.resolve('signed');
|
|
640
|
+
});
|
|
641
|
+
|
|
642
|
+
const signingKeys = [{ address: pubKey, privKey: encPriv, pubKey }];
|
|
643
|
+
await wallet.signTx({ tx: 'raw', signingKeys });
|
|
644
|
+
|
|
645
|
+
txStub.calledOnce.should.equal(true);
|
|
646
|
+
capturedPayload.keys[0].privKey.should.equal(expectedPrivKey);
|
|
647
|
+
});
|
|
648
|
+
});
|
|
649
|
+
});
|
|
650
|
+
|
|
651
|
+
describe('derivePrivateKey', function () {
|
|
652
|
+
let wallet: Wallet;
|
|
653
|
+
const createUnlockedWallet = async (params: { chain: string; network: string; xpriv: string }) => {
|
|
654
|
+
walletName = `BitcoreClientTestDerivePrivateKey-${params.chain}`;
|
|
655
|
+
wallet = await createWalletForTest({
|
|
656
|
+
name: walletName,
|
|
657
|
+
chain: params.chain,
|
|
658
|
+
network: params.network,
|
|
659
|
+
xpriv: params.xpriv,
|
|
660
|
+
password: 'abc123',
|
|
661
|
+
storageType,
|
|
662
|
+
baseUrl
|
|
663
|
+
});
|
|
664
|
+
await wallet.unlock('abc123');
|
|
665
|
+
expect(Buffer.isBuffer(wallet.unlocked?.masterKey?.xprivkey)).to.equal(true);
|
|
666
|
+
expect(Buffer.isBuffer(wallet.unlocked?.masterKey?.privateKey)).to.equal(true);
|
|
667
|
+
return wallet;
|
|
668
|
+
};
|
|
669
|
+
|
|
670
|
+
const walletVectors = {
|
|
671
|
+
BTC: {
|
|
672
|
+
chain: 'BTC',
|
|
673
|
+
network: 'mainnet',
|
|
674
|
+
xpriv: 'xprv9s21ZrQH143K3aKdQ6kXF1vj7R6LtkoLCiUXfM5bdbGXmhQkC1iXdnFfrxAAtaTunPUCCLwUQ3cpNixGLMbLAH1gzeCr8VZDe4gPgmKLb2X',
|
|
675
|
+
expected: {
|
|
676
|
+
address: '14FubqQhpG1dhTSgD5nRsiQRJEEcxVojRf',
|
|
677
|
+
privKey: '79cab08ffc77750721329a0033c43fd1e5c32e9e2da273c18e5e36abb05cca32',
|
|
678
|
+
pubKey: '03d69dd136b999433a9f6c8f38076831ec0d3a3cf7a555bec8bc8c6d76fc266231',
|
|
679
|
+
path: 'm/0/0'
|
|
680
|
+
}
|
|
681
|
+
},
|
|
682
|
+
ETH: {
|
|
683
|
+
chain: 'ETH',
|
|
684
|
+
network: 'mainnet',
|
|
685
|
+
xpriv: 'xprv9ypBjKErGMqCdzd44hfSdy1Vk6PGtU3si8ogZcow7rA23HTxMi9XfT99EKmiNdLMr9BAZ9S8ZKCYfN1eCmzYSmXYHje1jnYQseV1VJDDfdS',
|
|
686
|
+
expected: {
|
|
687
|
+
address: '0xb497281830dE4F19a3482AbF3D5C35c514e6fB36',
|
|
688
|
+
privKey: '62b8311c71f355c5c07f6bffe9b1ae60aa20d90e2e2ec93ec11b6014b2ae6340',
|
|
689
|
+
pubKey: '0386d153aad9395924631dbc78fa560107123a759eaa3e105958248c60cd4472ad',
|
|
690
|
+
path: 'm/0/0'
|
|
691
|
+
}
|
|
692
|
+
},
|
|
693
|
+
XRP: {
|
|
694
|
+
chain: 'XRP',
|
|
695
|
+
network: 'mainnet',
|
|
696
|
+
xpriv: 'xprvA58pn8bWSyoRGvEY97ALTHP4Dj6t47Q3PTBUEw78CF91kALMwhs7D2GutQSvpRN6ACR4RX4HbF3KmF7zDf48gR8nwG7DqLp6ezUcMiPHDtV',
|
|
697
|
+
expected: {
|
|
698
|
+
address: 'r9dmAJBfBe7JL2RRLiFWGJ8kM4CHEeTpgN',
|
|
699
|
+
privKey: 'D02C6801D8F328FF2EAD51D01F9580AF36C8D74E2BD463963AC4ADBE51AE5F2C',
|
|
700
|
+
pubKey: '03DBEEC5E9E76DA09C5B502A67136BC2D73423E8902A7C35A8CBC0C5A6AC0469E8',
|
|
701
|
+
path: 'm/0/0'
|
|
702
|
+
}
|
|
703
|
+
},
|
|
704
|
+
SOL: {
|
|
705
|
+
chain: 'SOL',
|
|
706
|
+
network: 'mainnet',
|
|
707
|
+
xpriv: 'xprv9s21ZrQH143K3aKdQ6kXF1vj7R6LtkoLCiUXfM5bdbGXmhQkC1iXdnFfrxAAtaTunPUCCLwUQ3cpNixGLMbLAH1gzeCr8VZDe4gPgmKLb2X',
|
|
708
|
+
expected: {
|
|
709
|
+
address: '7EWwMxKQa5Gru7oTcS1Wi3AaEgTfA6MU3z7MaLUT6hnD',
|
|
710
|
+
privKey: 'E4Tp4nTgMCa5dtGwqvkWoMGrJC7FKRNjcpeFFXi4nNb9',
|
|
711
|
+
pubKey: '5c9c85b20525ee81d3cc56da1f8307ec169086ae41458c5458519aced7683b66'
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
};
|
|
715
|
+
|
|
716
|
+
it('derives the expected BTC key material', async function () {
|
|
717
|
+
await createUnlockedWallet(walletVectors.BTC);
|
|
718
|
+
const result = await wallet.derivePrivateKey(false, 0);
|
|
719
|
+
expect(result).to.deep.equal(walletVectors.BTC.expected);
|
|
720
|
+
});
|
|
721
|
+
|
|
722
|
+
it('derives the expected ETH key material', async function () {
|
|
723
|
+
await createUnlockedWallet(walletVectors.ETH);
|
|
724
|
+
const result = await wallet.derivePrivateKey(false, 0);
|
|
725
|
+
expect(result).to.deep.equal(walletVectors.ETH.expected);
|
|
726
|
+
});
|
|
727
|
+
|
|
728
|
+
it('derives the expected XRP key material', async function () {
|
|
729
|
+
await createUnlockedWallet(walletVectors.XRP);
|
|
730
|
+
const result = await wallet.derivePrivateKey(false, 0);
|
|
731
|
+
expect(result).to.deep.equal(walletVectors.XRP.expected);
|
|
732
|
+
});
|
|
733
|
+
|
|
734
|
+
it('derives the expected SOL key material', async function () {
|
|
735
|
+
await createUnlockedWallet(walletVectors.SOL);
|
|
736
|
+
const result = await wallet.derivePrivateKey(false, 0);
|
|
737
|
+
expect(result).to.deep.equal(walletVectors.SOL.expected);
|
|
738
|
+
});
|
|
739
|
+
});
|
|
740
|
+
|
|
741
|
+
describe('unlock', function () {
|
|
742
|
+
it('performs wallet migration for previous wallet versions', async () => {
|
|
743
|
+
const fixture = ethMigrationTestWalletFixture;
|
|
744
|
+
const wallet = new Wallet(fixture.wallet as any);
|
|
745
|
+
const tempHomeDir = fs.mkdtempSync(path.join(os.tmpdir(), 'bitcore-client-migration-unlock-'));
|
|
746
|
+
const backupDir = path.join(tempHomeDir, '.bitcore', 'bitcoreWallet', 'backup');
|
|
747
|
+
const loadWalletStub = sandbox.stub();
|
|
748
|
+
const getStoredKeysStub = sandbox.stub();
|
|
749
|
+
const addKeysSafeStub = sandbox.stub();
|
|
750
|
+
const saveWalletStub = sandbox.stub();
|
|
751
|
+
|
|
752
|
+
wallet.storage = {
|
|
753
|
+
loadWallet: loadWalletStub,
|
|
754
|
+
getStoredKeys: getStoredKeysStub,
|
|
755
|
+
addKeysSafe: addKeysSafeStub,
|
|
756
|
+
saveWallet: saveWalletStub
|
|
757
|
+
} as any;
|
|
758
|
+
|
|
759
|
+
loadWalletStub.resolves(fixture.rawWallet);
|
|
760
|
+
getStoredKeysStub.resolves(fixture.storedKeys);
|
|
761
|
+
addKeysSafeStub.resolves();
|
|
762
|
+
saveWalletStub.resolves();
|
|
763
|
+
|
|
764
|
+
const homedirStub = sandbox.stub(os, 'homedir').returns(tempHomeDir);
|
|
765
|
+
sandbox.stub(wallet, 'getAddresses').resolves(fixture.addresses);
|
|
766
|
+
|
|
767
|
+
await wallet.unlock(fixture.password);
|
|
768
|
+
|
|
769
|
+
// Assert wallet.storage methods are called (from migrateWallet)
|
|
770
|
+
expect(addKeysSafeStub.calledOnce).to.equal(true);
|
|
771
|
+
expect(saveWalletStub.calledOnce).to.equal(true);
|
|
772
|
+
|
|
773
|
+
expect(wallet.version).to.equal(2);
|
|
774
|
+
expect(wallet.unlocked).to.exist;
|
|
775
|
+
expect(Buffer.isBuffer(wallet.unlocked?.encryptionKey)).to.equal(true);
|
|
776
|
+
expect(Buffer.isBuffer(wallet.unlocked?.masterKey?.privateKey)).to.equal(true);
|
|
777
|
+
expect(Buffer.isBuffer(wallet.unlocked?.masterKey?.xprivkey)).to.equal(true);
|
|
778
|
+
expect(fs.readFileSync(path.join(backupDir, `${fixture.name}.v1.bak`), 'utf8')).to.equal(fixture.rawWallet);
|
|
779
|
+
expect(fs.readFileSync(path.join(backupDir, `${fixture.name}_keys.v1.bak`), 'utf8')).to.equal(JSON.stringify(fixture.storedKeys));
|
|
780
|
+
homedirStub.restore();
|
|
781
|
+
fs.rmSync(tempHomeDir, { recursive: true, force: true });
|
|
782
|
+
});
|
|
783
|
+
});
|
|
784
|
+
|
|
785
|
+
describe('migrateWallet', function () {
|
|
786
|
+
let wallet: Wallet;
|
|
787
|
+
let decryptedEncryptionKey: Buffer | undefined;
|
|
788
|
+
let loadWalletStub: sinon.SinonStub;
|
|
789
|
+
let getStoredKeysStub: sinon.SinonStub;
|
|
790
|
+
let addKeysSafeStub: sinon.SinonStub;
|
|
791
|
+
let saveWalletStub: sinon.SinonStub;
|
|
792
|
+
let homedirStub: sinon.SinonStub;
|
|
793
|
+
let tempHomeDir: string;
|
|
794
|
+
let backupDir: string;
|
|
795
|
+
|
|
796
|
+
|
|
797
|
+
beforeEach(async function () {
|
|
798
|
+
wallet = new Wallet(ethMigrationTestWalletFixture.wallet as any);
|
|
799
|
+
tempHomeDir = fs.mkdtempSync(path.join(os.tmpdir(), 'bitcore-client-migration-'));
|
|
800
|
+
backupDir = path.join(tempHomeDir, '.bitcore', 'bitcoreWallet', 'backup');
|
|
801
|
+
wallet.storage = {
|
|
802
|
+
loadWallet: async () => undefined,
|
|
803
|
+
getStoredKeys: async () => [],
|
|
804
|
+
addKeysSafe: async () => undefined,
|
|
805
|
+
saveWallet: async () => undefined
|
|
806
|
+
} as any;
|
|
807
|
+
|
|
808
|
+
loadWalletStub = sandbox.stub(wallet.storage, 'loadWallet').resolves(ethMigrationTestWalletFixture.rawWallet);
|
|
809
|
+
getStoredKeysStub = sandbox.stub(wallet.storage, 'getStoredKeys').resolves(ethMigrationTestWalletFixture.storedKeys);
|
|
810
|
+
addKeysSafeStub = sandbox.stub(wallet.storage, 'addKeysSafe').resolves();
|
|
811
|
+
saveWalletStub = sandbox.stub(wallet.storage, 'saveWallet').resolves();
|
|
812
|
+
homedirStub = sandbox.stub(os, 'homedir').returns(tempHomeDir);
|
|
813
|
+
sandbox.stub(wallet, 'getAddresses').resolves(ethMigrationTestWalletFixture.addresses);
|
|
814
|
+
|
|
815
|
+
decryptedEncryptionKey = Encryption.decryptEncryptionKey(
|
|
816
|
+
ethMigrationTestWalletFixture.wallet.encryptionKey,
|
|
817
|
+
ethMigrationTestWalletFixture.password,
|
|
818
|
+
true
|
|
819
|
+
) as Buffer;
|
|
820
|
+
});
|
|
821
|
+
|
|
822
|
+
afterEach(function () {
|
|
823
|
+
homedirStub?.restore();
|
|
824
|
+
if (tempHomeDir) {
|
|
825
|
+
fs.rmSync(tempHomeDir, { recursive: true, force: true });
|
|
826
|
+
}
|
|
827
|
+
});
|
|
828
|
+
|
|
829
|
+
it('should back up existing raw wallet & keys (separately) before migration', async () => {
|
|
830
|
+
await wallet.migrateWallet(decryptedEncryptionKey);
|
|
831
|
+
|
|
832
|
+
expect(loadWalletStub.calledOnceWithExactly({ name: ethMigrationTestWalletFixture.name, raw: true })).to.be.true;
|
|
833
|
+
expect(getStoredKeysStub.calledOnceWithExactly({
|
|
834
|
+
addresses: ethMigrationTestWalletFixture.addresses,
|
|
835
|
+
name: ethMigrationTestWalletFixture.name
|
|
836
|
+
})).to.be.true;
|
|
837
|
+
expect(fs.readFileSync(path.join(backupDir, `${ethMigrationTestWalletFixture.name}.v1.bak`), 'utf8')).to.equal(
|
|
838
|
+
ethMigrationTestWalletFixture.rawWallet
|
|
839
|
+
);
|
|
840
|
+
expect(fs.readFileSync(path.join(backupDir, `${ethMigrationTestWalletFixture.name}_keys.v1.bak`), 'utf8')).to.equal(
|
|
841
|
+
JSON.stringify(ethMigrationTestWalletFixture.storedKeys)
|
|
842
|
+
);
|
|
843
|
+
});
|
|
844
|
+
|
|
845
|
+
it('should overwrite existing keys', async () => {
|
|
846
|
+
await wallet.migrateWallet(decryptedEncryptionKey);
|
|
847
|
+
|
|
848
|
+
expect(addKeysSafeStub.calledOnce).to.equal(true);
|
|
849
|
+
const { name, keys } = addKeysSafeStub.firstCall.args[0];
|
|
850
|
+
expect(name).to.equal(ethMigrationTestWalletFixture.name);
|
|
851
|
+
expect(keys).to.have.length(ethMigrationTestWalletFixture.storedKeys.length);
|
|
852
|
+
|
|
853
|
+
const expectedKeys = ethMigrationTestWalletFixture.storedKeys.map(storedKey => {
|
|
854
|
+
// Added keys encrypt privKey, convert to hex, and store that on key.privKey
|
|
855
|
+
const decryptedKey = JSON.parse(
|
|
856
|
+
Encryption.decryptPrivateKey(storedKey.encKey, storedKey.pubKey, decryptedEncryptionKey)
|
|
857
|
+
);
|
|
858
|
+
const privKeyBuffer = CWC.Deriver.privateKeyToBuffer(wallet.chain, decryptedKey.privKey);
|
|
859
|
+
const encryptedPrivKey = Encryption.encryptBuffer(privKeyBuffer, storedKey.pubKey, decryptedEncryptionKey).toString('hex');
|
|
860
|
+
privKeyBuffer.fill(0);
|
|
861
|
+
return {
|
|
862
|
+
...decryptedKey,
|
|
863
|
+
privKey: encryptedPrivKey
|
|
864
|
+
};
|
|
865
|
+
});
|
|
866
|
+
|
|
867
|
+
// Stored keys exhibit new encryption process
|
|
868
|
+
expect(keys).to.deep.equal(expectedKeys);
|
|
869
|
+
});
|
|
870
|
+
|
|
871
|
+
it('should overwrite existing wallet', async () => {
|
|
872
|
+
await wallet.migrateWallet(decryptedEncryptionKey);
|
|
873
|
+
|
|
874
|
+
expect(saveWalletStub.calledOnce).to.equal(true);
|
|
875
|
+
const savedWallet = saveWalletStub.firstCall.args[0].wallet;
|
|
876
|
+
|
|
877
|
+
expect(savedWallet.version).to.equal(2);
|
|
878
|
+
expect(savedWallet.password).to.equal(ethMigrationTestWalletFixture.wallet.password);
|
|
879
|
+
expect(savedWallet.masterKey).to.not.equal(ethMigrationTestWalletFixture.wallet.masterKey);
|
|
880
|
+
|
|
881
|
+
const migratedMasterKey = JSON.parse(savedWallet.masterKey);
|
|
882
|
+
const originalMasterKey = JSON.parse(
|
|
883
|
+
Encryption.decryptPrivateKey(
|
|
884
|
+
ethMigrationTestWalletFixture.wallet.masterKey,
|
|
885
|
+
ethMigrationTestWalletFixture.wallet.pubKey,
|
|
886
|
+
decryptedEncryptionKey
|
|
887
|
+
)
|
|
888
|
+
);
|
|
889
|
+
|
|
890
|
+
const migratedXpriv = Encryption.decryptToBuffer(
|
|
891
|
+
migratedMasterKey.xprivkey,
|
|
892
|
+
ethMigrationTestWalletFixture.wallet.pubKey,
|
|
893
|
+
decryptedEncryptionKey
|
|
894
|
+
);
|
|
895
|
+
const migratedPrivateKey = Encryption.decryptToBuffer(
|
|
896
|
+
migratedMasterKey.privateKey,
|
|
897
|
+
ethMigrationTestWalletFixture.wallet.pubKey,
|
|
898
|
+
decryptedEncryptionKey
|
|
899
|
+
);
|
|
900
|
+
|
|
901
|
+
expect(wallet.version).to.equal(2);
|
|
902
|
+
|
|
903
|
+
// Decrypted master keys same as prior master keys
|
|
904
|
+
expect(migratedXpriv.toString('hex')).to.equal(
|
|
905
|
+
CWC.BitcoreLib.encoding.Base58Check.decode(originalMasterKey.xprivkey).toString('hex')
|
|
906
|
+
);
|
|
907
|
+
expect(migratedPrivateKey.toString('hex')).to.equal(originalMasterKey.privateKey);
|
|
908
|
+
});
|
|
909
|
+
|
|
910
|
+
it('should throw if the raw wallet cannot be loaded', async () => {
|
|
911
|
+
loadWalletStub.resolves(undefined);
|
|
912
|
+
|
|
913
|
+
try {
|
|
914
|
+
await wallet.migrateWallet(decryptedEncryptionKey);
|
|
915
|
+
expect.fail('Expected migrateWallet to throw');
|
|
916
|
+
} catch (err) {
|
|
917
|
+
expect(err.message).to.equal('Migration failed - wallet not found');
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
expect(getStoredKeysStub.called).to.equal(false);
|
|
921
|
+
expect(addKeysSafeStub.called).to.equal(false);
|
|
922
|
+
expect(saveWalletStub.called).to.equal(false);
|
|
923
|
+
});
|
|
924
|
+
|
|
925
|
+
it('should throw if the decrypted masterKey is malformed', async () => {
|
|
926
|
+
wallet.masterKey = Encryption.encryptPrivateKey(
|
|
927
|
+
JSON.stringify({ invalid: true }),
|
|
928
|
+
wallet.pubKey,
|
|
929
|
+
decryptedEncryptionKey.toString('hex')
|
|
930
|
+
);
|
|
931
|
+
|
|
932
|
+
try {
|
|
933
|
+
await wallet.migrateWallet(decryptedEncryptionKey);
|
|
934
|
+
expect.fail('Expected migrateWallet to throw');
|
|
935
|
+
} catch (err) {
|
|
936
|
+
expect(err.message).to.equal('Migration failure: masterKey is not formatted as expected');
|
|
937
|
+
}
|
|
938
|
+
|
|
939
|
+
expect(addKeysSafeStub.called).to.equal(false);
|
|
940
|
+
expect(saveWalletStub.called).to.equal(false);
|
|
941
|
+
});
|
|
942
|
+
|
|
943
|
+
it('should throw if addKeysSafe fails and should not save the wallet', async () => {
|
|
944
|
+
addKeysSafeStub.rejects(new Error('write failed'));
|
|
945
|
+
|
|
946
|
+
try {
|
|
947
|
+
await wallet.migrateWallet(decryptedEncryptionKey);
|
|
948
|
+
expect.fail('Expected migrateWallet to throw');
|
|
949
|
+
} catch (err) {
|
|
950
|
+
expect(err.message).to.equal(
|
|
951
|
+
'Migration failure: keys not successfully stored. Use backups to restore prior wallet and keys.'
|
|
952
|
+
);
|
|
953
|
+
}
|
|
954
|
+
|
|
955
|
+
expect(addKeysSafeStub.calledOnce).to.equal(true);
|
|
956
|
+
expect(saveWalletStub.called).to.equal(false);
|
|
957
|
+
});
|
|
958
|
+
|
|
959
|
+
it('should no-op when the wallet is already on the current version', async () => {
|
|
960
|
+
wallet.version = 2;
|
|
961
|
+
const warnStub = sandbox.stub(console, 'warn');
|
|
962
|
+
|
|
963
|
+
const migratedWallet = await wallet.migrateWallet(decryptedEncryptionKey);
|
|
964
|
+
|
|
965
|
+
expect(migratedWallet).to.equal(wallet);
|
|
966
|
+
expect(warnStub.calledOnceWithExactly('Wallet migration unnecessarily called - wallet is current version')).to.equal(true);
|
|
967
|
+
expect(loadWalletStub.called).to.equal(false);
|
|
968
|
+
expect(getStoredKeysStub.called).to.equal(false);
|
|
969
|
+
expect(addKeysSafeStub.called).to.equal(false);
|
|
970
|
+
expect(saveWalletStub.called).to.equal(false);
|
|
971
|
+
expect(fs.existsSync(path.join(backupDir, `${ethMigrationTestWalletFixture.name}.v2.bak`))).to.equal(false);
|
|
972
|
+
});
|
|
973
|
+
|
|
974
|
+
it('should no-op when the wallet version is newer than the current version', async () => {
|
|
975
|
+
wallet.version = 3;
|
|
976
|
+
const warnStub = sandbox.stub(console, 'warn');
|
|
977
|
+
|
|
978
|
+
const migratedWallet = await wallet.migrateWallet(decryptedEncryptionKey);
|
|
979
|
+
|
|
980
|
+
expect(migratedWallet).to.equal(wallet);
|
|
981
|
+
expect(
|
|
982
|
+
warnStub.calledOnceWithExactly('Wallet version 3 greater than expected current wallet version 2')
|
|
983
|
+
).to.equal(true);
|
|
984
|
+
expect(loadWalletStub.called).to.equal(false);
|
|
985
|
+
expect(getStoredKeysStub.called).to.equal(false);
|
|
986
|
+
expect(addKeysSafeStub.called).to.equal(false);
|
|
987
|
+
expect(saveWalletStub.called).to.equal(false);
|
|
988
|
+
expect(fs.existsSync(path.join(backupDir, `${ethMigrationTestWalletFixture.name}.v3.bak`))).to.equal(false);
|
|
989
|
+
});
|
|
369
990
|
});
|
|
370
991
|
|
|
371
992
|
describe('getBalance', function() {
|
|
372
993
|
walletName = 'BitcoreClientTestGetBalance';
|
|
373
994
|
beforeEach(async function() {
|
|
374
|
-
wallet = await
|
|
995
|
+
wallet = await createWalletForTest({
|
|
375
996
|
name: walletName,
|
|
376
997
|
chain: 'MATIC',
|
|
377
998
|
network: 'testnet',
|
|
378
999
|
phrase: 'snap impact summer because must pipe weasel gorilla actor acid web whip',
|
|
379
1000
|
password: 'abc123',
|
|
380
1001
|
storageType: 'Level',
|
|
1002
|
+
version: 0,
|
|
381
1003
|
});
|
|
382
1004
|
await wallet.unlock('abc123');
|
|
383
1005
|
});
|
|
@@ -420,13 +1042,14 @@ describe('Wallet', function() {
|
|
|
420
1042
|
describe('getTokenObj', function() {
|
|
421
1043
|
walletName = 'BitcoreClientTestGetTokenObj';
|
|
422
1044
|
beforeEach(async function() {
|
|
423
|
-
wallet = await
|
|
1045
|
+
wallet = await createWalletForTest({
|
|
424
1046
|
name: walletName,
|
|
425
1047
|
chain: 'MATIC',
|
|
426
1048
|
network: 'testnet',
|
|
427
1049
|
phrase: 'snap impact summer because must pipe weasel gorilla actor acid web whip',
|
|
428
1050
|
password: 'abc123',
|
|
429
1051
|
storageType: 'Level',
|
|
1052
|
+
version: 0,
|
|
430
1053
|
});
|
|
431
1054
|
await wallet.unlock('abc123');
|
|
432
1055
|
});
|
|
@@ -498,7 +1121,7 @@ describe('Wallet', function() {
|
|
|
498
1121
|
};
|
|
499
1122
|
|
|
500
1123
|
beforeEach(async function() {
|
|
501
|
-
wallet = await
|
|
1124
|
+
wallet = await createWalletForTest({
|
|
502
1125
|
chain: 'ETH',
|
|
503
1126
|
network: 'mainnet',
|
|
504
1127
|
name: walletName,
|
|
@@ -506,7 +1129,8 @@ describe('Wallet', function() {
|
|
|
506
1129
|
password: 'abc123',
|
|
507
1130
|
lite: false,
|
|
508
1131
|
storageType,
|
|
509
|
-
baseUrl
|
|
1132
|
+
baseUrl,
|
|
1133
|
+
version: 0
|
|
510
1134
|
});
|
|
511
1135
|
|
|
512
1136
|
wallet.tokens = [
|