@bitpay-labs/bitcore-client 11.7.2 → 11.7.3
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/package.json +4 -4
- package/ts_build/src/client.d.ts.map +1 -1
- package/ts_build/src/client.js +7 -4
- package/ts_build/src/client.js.map +1 -1
- package/ts_build/src/encryption.d.ts +7 -2
- package/ts_build/src/encryption.d.ts.map +1 -1
- package/ts_build/src/encryption.js +59 -11
- package/ts_build/src/encryption.js.map +1 -1
- package/ts_build/src/storage/mongo.d.ts.map +1 -1
- package/ts_build/src/storage/mongo.js +1 -1
- package/ts_build/src/storage/mongo.js.map +1 -1
- package/ts_build/src/storage/textFile.d.ts +5 -0
- package/ts_build/src/storage/textFile.d.ts.map +1 -1
- package/ts_build/src/storage/textFile.js +46 -0
- package/ts_build/src/storage/textFile.js.map +1 -1
- package/ts_build/src/storage.d.ts +17 -0
- package/ts_build/src/storage.d.ts.map +1 -1
- package/ts_build/src/storage.js +66 -2
- package/ts_build/src/storage.js.map +1 -1
- package/ts_build/src/wallet.d.ts +9 -2
- package/ts_build/src/wallet.d.ts.map +1 -1
- package/ts_build/src/wallet.js +215 -59
- package/ts_build/src/wallet.js.map +1 -1
- package/ts_build/test/unit/data/ethMigrationTestWallet.fixture.d.ts +51 -0
- package/ts_build/test/unit/data/ethMigrationTestWallet.fixture.d.ts.map +1 -0
- package/ts_build/test/unit/data/ethMigrationTestWallet.fixture.js +68 -0
- package/ts_build/test/unit/data/ethMigrationTestWallet.fixture.js.map +1 -0
- package/ts_build/test/unit/wallet.test.js +517 -14
- package/ts_build/test/unit/wallet.test.js.map +1 -1
- package/LICENSE +0 -21
|
@@ -38,7 +38,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
38
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
39
|
const chai = __importStar(require("chai"));
|
|
40
40
|
const CWC = __importStar(require("@bitpay-labs/crypto-wallet-core"));
|
|
41
|
+
const fs_1 = __importDefault(require("fs"));
|
|
42
|
+
const os_1 = __importDefault(require("os"));
|
|
43
|
+
const path_1 = __importDefault(require("path"));
|
|
41
44
|
const wallet_1 = require("../../src/wallet");
|
|
45
|
+
const encryption_1 = require("../../src/encryption");
|
|
42
46
|
const api_1 = require("../../../bitcore-node/build/src/services/api");
|
|
43
47
|
const storage_1 = require("../../../bitcore-node/build/src/services/storage");
|
|
44
48
|
const crypto_1 = __importDefault(require("crypto"));
|
|
@@ -48,6 +52,7 @@ const request_1 = __importDefault(require("request"));
|
|
|
48
52
|
const sinon_1 = __importDefault(require("sinon"));
|
|
49
53
|
const supertest_1 = __importDefault(require("supertest"));
|
|
50
54
|
const utils_1 = require("../../src/utils");
|
|
55
|
+
const ethMigrationTestWallet_fixture_1 = __importDefault(require("./data/ethMigrationTestWallet.fixture"));
|
|
51
56
|
const should = chai.should();
|
|
52
57
|
const expect = chai.expect;
|
|
53
58
|
const libMap = {
|
|
@@ -63,6 +68,19 @@ describe('Wallet', function () {
|
|
|
63
68
|
let walletName;
|
|
64
69
|
let wallet;
|
|
65
70
|
let api;
|
|
71
|
+
const walletsToCleanup = [];
|
|
72
|
+
const createWalletForTest = async (params) => {
|
|
73
|
+
if (!params.name) {
|
|
74
|
+
throw new Error('Tests must provide a wallet name');
|
|
75
|
+
}
|
|
76
|
+
const createdWallet = await wallet_1.Wallet.create(params);
|
|
77
|
+
walletsToCleanup.push({
|
|
78
|
+
name: params.name,
|
|
79
|
+
storageType: params.storageType,
|
|
80
|
+
path: params.path
|
|
81
|
+
});
|
|
82
|
+
return createdWallet;
|
|
83
|
+
};
|
|
66
84
|
before(async function () {
|
|
67
85
|
this.timeout(20000);
|
|
68
86
|
await storage_1.Storage.start({
|
|
@@ -96,14 +114,19 @@ describe('Wallet', function () {
|
|
|
96
114
|
});
|
|
97
115
|
});
|
|
98
116
|
afterEach(async function () {
|
|
99
|
-
|
|
117
|
+
while (walletsToCleanup.length) {
|
|
118
|
+
const walletToCleanup = walletsToCleanup.pop();
|
|
119
|
+
if (walletToCleanup) {
|
|
120
|
+
await wallet_1.Wallet.deleteWallet(walletToCleanup);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
100
123
|
sandbox.restore();
|
|
101
124
|
});
|
|
102
125
|
for (const chain of ['BTC', 'BCH', 'LTC', 'DOGE', 'ETH', 'XRP', 'MATIC']) {
|
|
103
126
|
for (const addressType of Object.keys(wallet_1.AddressTypes[chain] || { 'pubkeyhash': 1 })) {
|
|
104
127
|
it(`should create a wallet for chain and addressType: ${chain} ${addressType}`, async function () {
|
|
105
128
|
walletName = 'BitcoreClientTest' + chain + addressType;
|
|
106
|
-
wallet = await
|
|
129
|
+
wallet = await createWalletForTest({
|
|
107
130
|
chain,
|
|
108
131
|
network: 'mainnet',
|
|
109
132
|
name: walletName,
|
|
@@ -112,7 +135,8 @@ describe('Wallet', function () {
|
|
|
112
135
|
lite: false,
|
|
113
136
|
addressType,
|
|
114
137
|
storageType,
|
|
115
|
-
baseUrl
|
|
138
|
+
baseUrl,
|
|
139
|
+
version: 0
|
|
116
140
|
});
|
|
117
141
|
expect(wallet.addressType).to.equal(wallet_1.AddressTypes[chain]?.[addressType] || 'pubkeyhash');
|
|
118
142
|
});
|
|
@@ -141,14 +165,14 @@ describe('Wallet', function () {
|
|
|
141
165
|
describe('UTXO', function () {
|
|
142
166
|
walletName = 'BitcoreClientTestBumpFee-UTXO';
|
|
143
167
|
beforeEach(async function () {
|
|
144
|
-
wallet = await
|
|
168
|
+
wallet = await createWalletForTest({
|
|
145
169
|
name: walletName,
|
|
146
170
|
chain: 'BTC',
|
|
147
171
|
network: 'testnet',
|
|
148
172
|
phrase: 'snap impact summer because must pipe weasel gorilla actor acid web whip',
|
|
149
173
|
password: 'abc123',
|
|
150
174
|
storageType,
|
|
151
|
-
baseUrl
|
|
175
|
+
baseUrl,
|
|
152
176
|
});
|
|
153
177
|
await wallet.unlock('abc123');
|
|
154
178
|
});
|
|
@@ -209,14 +233,15 @@ describe('Wallet', function () {
|
|
|
209
233
|
describe('EVM', function () {
|
|
210
234
|
walletName = 'BitcoreClientTestBumpFee-EVM';
|
|
211
235
|
beforeEach(async function () {
|
|
212
|
-
wallet = await
|
|
236
|
+
wallet = await createWalletForTest({
|
|
213
237
|
name: walletName,
|
|
214
238
|
chain: 'ETH',
|
|
215
239
|
network: 'testnet',
|
|
216
240
|
phrase: 'snap impact summer because must pipe weasel gorilla actor acid web whip',
|
|
217
241
|
password: 'abc123',
|
|
218
242
|
storageType,
|
|
219
|
-
baseUrl
|
|
243
|
+
baseUrl,
|
|
244
|
+
version: 0
|
|
220
245
|
});
|
|
221
246
|
await wallet.unlock('abc123');
|
|
222
247
|
});
|
|
@@ -272,7 +297,8 @@ describe('Wallet', function () {
|
|
|
272
297
|
password: 'abc123',
|
|
273
298
|
storageType,
|
|
274
299
|
path,
|
|
275
|
-
baseUrl
|
|
300
|
+
baseUrl,
|
|
301
|
+
version: 0
|
|
276
302
|
});
|
|
277
303
|
await wallet.unlock('abc123');
|
|
278
304
|
[address1, caddress1] = await wallet.nextAddressPair(true);
|
|
@@ -302,14 +328,15 @@ describe('Wallet', function () {
|
|
|
302
328
|
let requestStub;
|
|
303
329
|
let sleepStub;
|
|
304
330
|
beforeEach(async function () {
|
|
305
|
-
wallet = await
|
|
331
|
+
wallet = await createWalletForTest({
|
|
306
332
|
name: walletName,
|
|
307
333
|
chain: 'BTC',
|
|
308
334
|
network: 'testnet',
|
|
309
335
|
phrase: 'snap impact summer because must pipe weasel gorilla actor acid web whip',
|
|
310
336
|
password: 'abc123',
|
|
311
337
|
storageType,
|
|
312
|
-
baseUrl
|
|
338
|
+
baseUrl,
|
|
339
|
+
version: 0
|
|
313
340
|
});
|
|
314
341
|
await wallet.unlock('abc123');
|
|
315
342
|
requestStub = sandbox.stub(wallet.client, '_request').resolves();
|
|
@@ -366,17 +393,491 @@ describe('Wallet', function () {
|
|
|
366
393
|
sleepStub.callCount.should.equal(1);
|
|
367
394
|
requestStub.args.flatMap(arg => arg[0].body).should.deep.equal(keys.map(k => ({ address: k.address })));
|
|
368
395
|
});
|
|
396
|
+
it('can derive the public key if not included', async function () {
|
|
397
|
+
const keys = [];
|
|
398
|
+
for (let i = 0; i < 101; i++) {
|
|
399
|
+
const pk = crypto_1.default.randomBytes(32).toString('hex');
|
|
400
|
+
keys.push({
|
|
401
|
+
privKey: pk,
|
|
402
|
+
address: libMap.BTC.PrivateKey(pk).toAddress().toString()
|
|
403
|
+
});
|
|
404
|
+
}
|
|
405
|
+
const getPublicKeyStub = sandbox.stub(CWC.Deriver, 'getPublicKey').returns('mockedPubKey');
|
|
406
|
+
sandbox.stub(wallet.storage, 'addKeysSafe').resolves();
|
|
407
|
+
await wallet.importKeys({
|
|
408
|
+
keys,
|
|
409
|
+
rederiveAddys: false
|
|
410
|
+
});
|
|
411
|
+
getPublicKeyStub.callCount.should.be.greaterThan(0);
|
|
412
|
+
});
|
|
413
|
+
it('encrypts key.privKey only', async function () {
|
|
414
|
+
const keys = [];
|
|
415
|
+
for (let i = 0; i < 1; i++) {
|
|
416
|
+
const pk = crypto_1.default.randomBytes(32).toString('hex');
|
|
417
|
+
keys.push({
|
|
418
|
+
privKey: pk,
|
|
419
|
+
address: libMap.BTC.PrivateKey(pk).toAddress().toString()
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
const addKeysSafeStub = sandbox.stub(wallet.storage, 'addKeysSafe').resolves();
|
|
423
|
+
await wallet.importKeys({
|
|
424
|
+
keys,
|
|
425
|
+
rederiveAddys: false
|
|
426
|
+
});
|
|
427
|
+
addKeysSafeStub.calledOnce.should.equal(true);
|
|
428
|
+
const savedKeys = addKeysSafeStub.firstCall.args[0].keys;
|
|
429
|
+
for (const originalKey of keys) {
|
|
430
|
+
const matchingSavedKey = savedKeys.find(sk => {
|
|
431
|
+
return !(originalKey.pubKey && sk.pubKey === originalKey.pubKey) && sk.address === originalKey.address;
|
|
432
|
+
});
|
|
433
|
+
expect(matchingSavedKey).to.exist;
|
|
434
|
+
matchingSavedKey.privKey.should.not.equal(originalKey.privKey);
|
|
435
|
+
}
|
|
436
|
+
});
|
|
437
|
+
it('can rederive addresses', async function () {
|
|
438
|
+
const keys = [];
|
|
439
|
+
for (let i = 0; i < 1; i++) {
|
|
440
|
+
const pk = crypto_1.default.randomBytes(32).toString('hex');
|
|
441
|
+
keys.push({
|
|
442
|
+
privKey: pk,
|
|
443
|
+
address: libMap.BTC.PrivateKey(pk).toAddress().toString()
|
|
444
|
+
});
|
|
445
|
+
}
|
|
446
|
+
const getPublicKeyStub = sandbox.stub(CWC.Deriver, 'getPublicKey').returns('mockedPubKey');
|
|
447
|
+
const getAddressStub = sandbox.stub(CWC.Deriver, 'getAddress').returns('mockedAddress');
|
|
448
|
+
sandbox.stub(wallet.storage, 'addKeysSafe').resolves();
|
|
449
|
+
await wallet.importKeys({
|
|
450
|
+
keys,
|
|
451
|
+
rederiveAddys: true
|
|
452
|
+
});
|
|
453
|
+
getAddressStub.callCount.should.be.greaterThan(0);
|
|
454
|
+
});
|
|
455
|
+
});
|
|
456
|
+
describe('signTx', function () {
|
|
457
|
+
let txStub;
|
|
458
|
+
afterEach(async function () {
|
|
459
|
+
sandbox.restore();
|
|
460
|
+
});
|
|
461
|
+
describe('BTC (UTXO) decrypts ciphertext to WIF', function () {
|
|
462
|
+
walletName = 'BitcoreClientTestSignTxV2-BTC';
|
|
463
|
+
let wallet;
|
|
464
|
+
beforeEach(async function () {
|
|
465
|
+
wallet = await createWalletForTest({
|
|
466
|
+
name: walletName,
|
|
467
|
+
chain: 'BTC',
|
|
468
|
+
network: 'testnet',
|
|
469
|
+
phrase: 'snap impact summer because must pipe weasel gorilla actor acid web whip',
|
|
470
|
+
password: 'abc123',
|
|
471
|
+
storageType,
|
|
472
|
+
baseUrl
|
|
473
|
+
});
|
|
474
|
+
await wallet.unlock('abc123');
|
|
475
|
+
});
|
|
476
|
+
it('should decrypt stored ciphertext and hand WIF to Transactions.sign', async function () {
|
|
477
|
+
const pk = new CWC.BitcoreLib.PrivateKey(undefined, 'testnet');
|
|
478
|
+
const address = pk.toAddress().toString();
|
|
479
|
+
const privBuf = CWC.Deriver.privateKeyToBuffer('BTC', pk.toString());
|
|
480
|
+
const encPriv = encryption_1.Encryption.encryptBuffer(privBuf, pk.publicKey.toString(), wallet.unlocked.encryptionKey).toString('hex');
|
|
481
|
+
privBuf.fill(0);
|
|
482
|
+
sandbox.stub(wallet.storage, 'getStoredKeys').resolves([
|
|
483
|
+
{
|
|
484
|
+
address,
|
|
485
|
+
privKey: encPriv,
|
|
486
|
+
pubKey: pk.publicKey.toString()
|
|
487
|
+
}
|
|
488
|
+
]);
|
|
489
|
+
sandbox.stub(wallet, 'derivePrivateKey').resolves({
|
|
490
|
+
address: 'change',
|
|
491
|
+
privKey: pk.toString(),
|
|
492
|
+
pubKey: pk.publicKey.toString(),
|
|
493
|
+
path: 'm/1/0'
|
|
494
|
+
});
|
|
495
|
+
sandbox.stub(wallet, 'importKeys').resolves();
|
|
496
|
+
let capturedPayload;
|
|
497
|
+
txStub = sandbox.stub(CWC.Transactions, 'sign').callsFake(payload => {
|
|
498
|
+
capturedPayload = payload;
|
|
499
|
+
return 'signed';
|
|
500
|
+
});
|
|
501
|
+
const utxos = [{ address, value: 1 }];
|
|
502
|
+
await wallet.signTx({ tx: 'raw', utxos });
|
|
503
|
+
txStub.calledOnce.should.equal(true);
|
|
504
|
+
capturedPayload.keys[0].privKey.should.equal(pk.toWIF());
|
|
505
|
+
capturedPayload.key.privKey.should.equal(pk.toWIF());
|
|
506
|
+
});
|
|
507
|
+
});
|
|
508
|
+
describe('ETH (account) decrypts ciphertext to hex and skips plaintext', function () {
|
|
509
|
+
walletName = 'BitcoreClientTestSignTxV2-ETH';
|
|
510
|
+
let wallet;
|
|
511
|
+
beforeEach(async function () {
|
|
512
|
+
wallet = await createWalletForTest({
|
|
513
|
+
name: walletName,
|
|
514
|
+
chain: 'ETH',
|
|
515
|
+
network: 'testnet',
|
|
516
|
+
phrase: 'snap impact summer because must pipe weasel gorilla actor acid web whip',
|
|
517
|
+
password: 'abc123',
|
|
518
|
+
storageType,
|
|
519
|
+
baseUrl
|
|
520
|
+
});
|
|
521
|
+
await wallet.unlock('abc123');
|
|
522
|
+
});
|
|
523
|
+
it('should decrypt stored ciphertext and hand hex privKey to Transactions.sign', async function () {
|
|
524
|
+
const privHex = crypto_1.default.randomBytes(32).toString('hex');
|
|
525
|
+
const privBufForPubKey = CWC.Deriver.privateKeyToBuffer('ETH', privHex);
|
|
526
|
+
const pubKey = CWC.Deriver.getPublicKey('ETH', wallet.network, privBufForPubKey);
|
|
527
|
+
privBufForPubKey.fill(0);
|
|
528
|
+
const privBuf = CWC.Deriver.privateKeyToBuffer('ETH', privHex);
|
|
529
|
+
const encPriv = encryption_1.Encryption.encryptBuffer(privBuf, pubKey, wallet.unlocked.encryptionKey).toString('hex');
|
|
530
|
+
privBuf.fill(0);
|
|
531
|
+
let capturedPayload;
|
|
532
|
+
txStub = sandbox.stub(CWC.Transactions, 'sign').callsFake(payload => {
|
|
533
|
+
capturedPayload = payload;
|
|
534
|
+
return 'signed';
|
|
535
|
+
});
|
|
536
|
+
const signingKeys = [{ address: '0xabc', privKey: encPriv, pubKey }];
|
|
537
|
+
await wallet.signTx({ tx: 'raw', signingKeys });
|
|
538
|
+
txStub.calledOnce.should.equal(true);
|
|
539
|
+
capturedPayload.keys[0].privKey.should.equal(privHex);
|
|
540
|
+
});
|
|
541
|
+
});
|
|
542
|
+
describe('XRP (account) decrypts ciphertext to uppercase hex and skips plaintext', function () {
|
|
543
|
+
walletName = 'BitcoreClientTestSignTxV2-XRP';
|
|
544
|
+
let wallet;
|
|
545
|
+
beforeEach(async function () {
|
|
546
|
+
wallet = await createWalletForTest({
|
|
547
|
+
name: walletName,
|
|
548
|
+
chain: 'XRP',
|
|
549
|
+
network: 'testnet',
|
|
550
|
+
phrase: 'snap impact summer because must pipe weasel gorilla actor acid web whip',
|
|
551
|
+
password: 'abc123',
|
|
552
|
+
storageType,
|
|
553
|
+
baseUrl
|
|
554
|
+
});
|
|
555
|
+
await wallet.unlock('abc123');
|
|
556
|
+
});
|
|
557
|
+
it('should decrypt stored ciphertext and hand uppercase hex privKey to Transactions.sign', async function () {
|
|
558
|
+
const privHex = crypto_1.default.randomBytes(32).toString('hex').toUpperCase();
|
|
559
|
+
const privBufForPubKey = CWC.Deriver.privateKeyToBuffer('XRP', privHex);
|
|
560
|
+
const pubKey = CWC.Deriver.getPublicKey('XRP', wallet.network, privBufForPubKey);
|
|
561
|
+
privBufForPubKey.fill(0);
|
|
562
|
+
const privBuf = CWC.Deriver.privateKeyToBuffer('XRP', privHex);
|
|
563
|
+
const encPriv = encryption_1.Encryption.encryptBuffer(privBuf, pubKey, wallet.unlocked.encryptionKey).toString('hex');
|
|
564
|
+
privBuf.fill(0);
|
|
565
|
+
let capturedPayload;
|
|
566
|
+
txStub = sandbox.stub(CWC.Transactions, 'sign').callsFake(payload => {
|
|
567
|
+
capturedPayload = payload;
|
|
568
|
+
return 'signed';
|
|
569
|
+
});
|
|
570
|
+
const signingKeys = [{ address: 'rabc', privKey: encPriv, pubKey }];
|
|
571
|
+
await wallet.signTx({ tx: 'raw', signingKeys });
|
|
572
|
+
txStub.calledOnce.should.equal(true);
|
|
573
|
+
capturedPayload.keys[0].privKey.should.equal(privHex);
|
|
574
|
+
});
|
|
575
|
+
});
|
|
576
|
+
describe('SOL (account) decrypts ciphertext to base58 and skips plaintext', function () {
|
|
577
|
+
walletName = 'BitcoreClientTestSignTxV2-SOL';
|
|
578
|
+
let wallet;
|
|
579
|
+
beforeEach(async function () {
|
|
580
|
+
wallet = await createWalletForTest({
|
|
581
|
+
name: walletName,
|
|
582
|
+
chain: 'SOL',
|
|
583
|
+
network: 'devnet',
|
|
584
|
+
phrase: 'snap impact summer because must pipe weasel gorilla actor acid web whip',
|
|
585
|
+
password: 'abc123',
|
|
586
|
+
storageType,
|
|
587
|
+
baseUrl
|
|
588
|
+
});
|
|
589
|
+
await wallet.unlock('abc123');
|
|
590
|
+
});
|
|
591
|
+
it('should decrypt stored ciphertext and hand base58 privKey to Transactions.sign', async function () {
|
|
592
|
+
const privBufForPubKey = crypto_1.default.randomBytes(32);
|
|
593
|
+
const pubKey = CWC.Deriver.getPublicKey('SOL', wallet.network, privBufForPubKey);
|
|
594
|
+
const privBuf = Buffer.from(privBufForPubKey);
|
|
595
|
+
privBufForPubKey.fill(0);
|
|
596
|
+
const encPriv = encryption_1.Encryption.encryptBuffer(privBuf, pubKey, wallet.unlocked.encryptionKey).toString('hex');
|
|
597
|
+
const expectedPrivKey = CWC.Deriver.bufferToPrivateKey_TEMP('SOL', wallet.network, Buffer.from(privBuf));
|
|
598
|
+
privBuf.fill(0);
|
|
599
|
+
let capturedPayload;
|
|
600
|
+
txStub = sandbox.stub(CWC.Transactions, 'sign').callsFake(payload => {
|
|
601
|
+
capturedPayload = payload;
|
|
602
|
+
return Promise.resolve('signed');
|
|
603
|
+
});
|
|
604
|
+
const signingKeys = [{ address: pubKey, privKey: encPriv, pubKey }];
|
|
605
|
+
await wallet.signTx({ tx: 'raw', signingKeys });
|
|
606
|
+
txStub.calledOnce.should.equal(true);
|
|
607
|
+
capturedPayload.keys[0].privKey.should.equal(expectedPrivKey);
|
|
608
|
+
});
|
|
609
|
+
});
|
|
610
|
+
});
|
|
611
|
+
describe('derivePrivateKey', function () {
|
|
612
|
+
let wallet;
|
|
613
|
+
const createUnlockedWallet = async (params) => {
|
|
614
|
+
walletName = `BitcoreClientTestDerivePrivateKey-${params.chain}`;
|
|
615
|
+
wallet = await createWalletForTest({
|
|
616
|
+
name: walletName,
|
|
617
|
+
chain: params.chain,
|
|
618
|
+
network: params.network,
|
|
619
|
+
xpriv: params.xpriv,
|
|
620
|
+
password: 'abc123',
|
|
621
|
+
storageType,
|
|
622
|
+
baseUrl
|
|
623
|
+
});
|
|
624
|
+
await wallet.unlock('abc123');
|
|
625
|
+
expect(Buffer.isBuffer(wallet.unlocked?.masterKey?.xprivkey)).to.equal(true);
|
|
626
|
+
expect(Buffer.isBuffer(wallet.unlocked?.masterKey?.privateKey)).to.equal(true);
|
|
627
|
+
return wallet;
|
|
628
|
+
};
|
|
629
|
+
const walletVectors = {
|
|
630
|
+
BTC: {
|
|
631
|
+
chain: 'BTC',
|
|
632
|
+
network: 'mainnet',
|
|
633
|
+
xpriv: 'xprv9s21ZrQH143K3aKdQ6kXF1vj7R6LtkoLCiUXfM5bdbGXmhQkC1iXdnFfrxAAtaTunPUCCLwUQ3cpNixGLMbLAH1gzeCr8VZDe4gPgmKLb2X',
|
|
634
|
+
expected: {
|
|
635
|
+
address: '14FubqQhpG1dhTSgD5nRsiQRJEEcxVojRf',
|
|
636
|
+
privKey: '79cab08ffc77750721329a0033c43fd1e5c32e9e2da273c18e5e36abb05cca32',
|
|
637
|
+
pubKey: '03d69dd136b999433a9f6c8f38076831ec0d3a3cf7a555bec8bc8c6d76fc266231',
|
|
638
|
+
path: 'm/0/0'
|
|
639
|
+
}
|
|
640
|
+
},
|
|
641
|
+
ETH: {
|
|
642
|
+
chain: 'ETH',
|
|
643
|
+
network: 'mainnet',
|
|
644
|
+
xpriv: 'xprv9ypBjKErGMqCdzd44hfSdy1Vk6PGtU3si8ogZcow7rA23HTxMi9XfT99EKmiNdLMr9BAZ9S8ZKCYfN1eCmzYSmXYHje1jnYQseV1VJDDfdS',
|
|
645
|
+
expected: {
|
|
646
|
+
address: '0xb497281830dE4F19a3482AbF3D5C35c514e6fB36',
|
|
647
|
+
privKey: '62b8311c71f355c5c07f6bffe9b1ae60aa20d90e2e2ec93ec11b6014b2ae6340',
|
|
648
|
+
pubKey: '0386d153aad9395924631dbc78fa560107123a759eaa3e105958248c60cd4472ad',
|
|
649
|
+
path: 'm/0/0'
|
|
650
|
+
}
|
|
651
|
+
},
|
|
652
|
+
XRP: {
|
|
653
|
+
chain: 'XRP',
|
|
654
|
+
network: 'mainnet',
|
|
655
|
+
xpriv: 'xprvA58pn8bWSyoRGvEY97ALTHP4Dj6t47Q3PTBUEw78CF91kALMwhs7D2GutQSvpRN6ACR4RX4HbF3KmF7zDf48gR8nwG7DqLp6ezUcMiPHDtV',
|
|
656
|
+
expected: {
|
|
657
|
+
address: 'r9dmAJBfBe7JL2RRLiFWGJ8kM4CHEeTpgN',
|
|
658
|
+
privKey: 'D02C6801D8F328FF2EAD51D01F9580AF36C8D74E2BD463963AC4ADBE51AE5F2C',
|
|
659
|
+
pubKey: '03DBEEC5E9E76DA09C5B502A67136BC2D73423E8902A7C35A8CBC0C5A6AC0469E8',
|
|
660
|
+
path: 'm/0/0'
|
|
661
|
+
}
|
|
662
|
+
},
|
|
663
|
+
SOL: {
|
|
664
|
+
chain: 'SOL',
|
|
665
|
+
network: 'mainnet',
|
|
666
|
+
xpriv: 'xprv9s21ZrQH143K3aKdQ6kXF1vj7R6LtkoLCiUXfM5bdbGXmhQkC1iXdnFfrxAAtaTunPUCCLwUQ3cpNixGLMbLAH1gzeCr8VZDe4gPgmKLb2X',
|
|
667
|
+
expected: {
|
|
668
|
+
address: '7EWwMxKQa5Gru7oTcS1Wi3AaEgTfA6MU3z7MaLUT6hnD',
|
|
669
|
+
privKey: 'E4Tp4nTgMCa5dtGwqvkWoMGrJC7FKRNjcpeFFXi4nNb9',
|
|
670
|
+
pubKey: '5c9c85b20525ee81d3cc56da1f8307ec169086ae41458c5458519aced7683b66'
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
};
|
|
674
|
+
it('derives the expected BTC key material', async function () {
|
|
675
|
+
await createUnlockedWallet(walletVectors.BTC);
|
|
676
|
+
const result = await wallet.derivePrivateKey(false, 0);
|
|
677
|
+
expect(result).to.deep.equal(walletVectors.BTC.expected);
|
|
678
|
+
});
|
|
679
|
+
it('derives the expected ETH key material', async function () {
|
|
680
|
+
await createUnlockedWallet(walletVectors.ETH);
|
|
681
|
+
const result = await wallet.derivePrivateKey(false, 0);
|
|
682
|
+
expect(result).to.deep.equal(walletVectors.ETH.expected);
|
|
683
|
+
});
|
|
684
|
+
it('derives the expected XRP key material', async function () {
|
|
685
|
+
await createUnlockedWallet(walletVectors.XRP);
|
|
686
|
+
const result = await wallet.derivePrivateKey(false, 0);
|
|
687
|
+
expect(result).to.deep.equal(walletVectors.XRP.expected);
|
|
688
|
+
});
|
|
689
|
+
it('derives the expected SOL key material', async function () {
|
|
690
|
+
await createUnlockedWallet(walletVectors.SOL);
|
|
691
|
+
const result = await wallet.derivePrivateKey(false, 0);
|
|
692
|
+
expect(result).to.deep.equal(walletVectors.SOL.expected);
|
|
693
|
+
});
|
|
694
|
+
});
|
|
695
|
+
describe('unlock', function () {
|
|
696
|
+
it('performs wallet migration for previous wallet versions', async () => {
|
|
697
|
+
const fixture = ethMigrationTestWallet_fixture_1.default;
|
|
698
|
+
const wallet = new wallet_1.Wallet(fixture.wallet);
|
|
699
|
+
const tempHomeDir = fs_1.default.mkdtempSync(path_1.default.join(os_1.default.tmpdir(), 'bitcore-client-migration-unlock-'));
|
|
700
|
+
const backupDir = path_1.default.join(tempHomeDir, '.bitcore', 'bitcoreWallet', 'backup');
|
|
701
|
+
const loadWalletStub = sandbox.stub();
|
|
702
|
+
const getStoredKeysStub = sandbox.stub();
|
|
703
|
+
const addKeysSafeStub = sandbox.stub();
|
|
704
|
+
const saveWalletStub = sandbox.stub();
|
|
705
|
+
wallet.storage = {
|
|
706
|
+
loadWallet: loadWalletStub,
|
|
707
|
+
getStoredKeys: getStoredKeysStub,
|
|
708
|
+
addKeysSafe: addKeysSafeStub,
|
|
709
|
+
saveWallet: saveWalletStub
|
|
710
|
+
};
|
|
711
|
+
loadWalletStub.resolves(fixture.rawWallet);
|
|
712
|
+
getStoredKeysStub.resolves(fixture.storedKeys);
|
|
713
|
+
addKeysSafeStub.resolves();
|
|
714
|
+
saveWalletStub.resolves();
|
|
715
|
+
const homedirStub = sandbox.stub(os_1.default, 'homedir').returns(tempHomeDir);
|
|
716
|
+
sandbox.stub(wallet, 'getAddresses').resolves(fixture.addresses);
|
|
717
|
+
await wallet.unlock(fixture.password);
|
|
718
|
+
expect(addKeysSafeStub.calledOnce).to.equal(true);
|
|
719
|
+
expect(saveWalletStub.calledOnce).to.equal(true);
|
|
720
|
+
expect(wallet.version).to.equal(2);
|
|
721
|
+
expect(wallet.unlocked).to.exist;
|
|
722
|
+
expect(Buffer.isBuffer(wallet.unlocked?.encryptionKey)).to.equal(true);
|
|
723
|
+
expect(Buffer.isBuffer(wallet.unlocked?.masterKey?.privateKey)).to.equal(true);
|
|
724
|
+
expect(Buffer.isBuffer(wallet.unlocked?.masterKey?.xprivkey)).to.equal(true);
|
|
725
|
+
expect(fs_1.default.readFileSync(path_1.default.join(backupDir, `${fixture.name}.v1.bak`), 'utf8')).to.equal(fixture.rawWallet);
|
|
726
|
+
expect(fs_1.default.readFileSync(path_1.default.join(backupDir, `${fixture.name}_keys.v1.bak`), 'utf8')).to.equal(JSON.stringify(fixture.storedKeys));
|
|
727
|
+
homedirStub.restore();
|
|
728
|
+
fs_1.default.rmSync(tempHomeDir, { recursive: true, force: true });
|
|
729
|
+
});
|
|
730
|
+
});
|
|
731
|
+
describe('migrateWallet', function () {
|
|
732
|
+
let wallet;
|
|
733
|
+
let decryptedEncryptionKey;
|
|
734
|
+
let loadWalletStub;
|
|
735
|
+
let getStoredKeysStub;
|
|
736
|
+
let addKeysSafeStub;
|
|
737
|
+
let saveWalletStub;
|
|
738
|
+
let homedirStub;
|
|
739
|
+
let tempHomeDir;
|
|
740
|
+
let backupDir;
|
|
741
|
+
beforeEach(async function () {
|
|
742
|
+
wallet = new wallet_1.Wallet(ethMigrationTestWallet_fixture_1.default.wallet);
|
|
743
|
+
tempHomeDir = fs_1.default.mkdtempSync(path_1.default.join(os_1.default.tmpdir(), 'bitcore-client-migration-'));
|
|
744
|
+
backupDir = path_1.default.join(tempHomeDir, '.bitcore', 'bitcoreWallet', 'backup');
|
|
745
|
+
wallet.storage = {
|
|
746
|
+
loadWallet: async () => undefined,
|
|
747
|
+
getStoredKeys: async () => [],
|
|
748
|
+
addKeysSafe: async () => undefined,
|
|
749
|
+
saveWallet: async () => undefined
|
|
750
|
+
};
|
|
751
|
+
loadWalletStub = sandbox.stub(wallet.storage, 'loadWallet').resolves(ethMigrationTestWallet_fixture_1.default.rawWallet);
|
|
752
|
+
getStoredKeysStub = sandbox.stub(wallet.storage, 'getStoredKeys').resolves(ethMigrationTestWallet_fixture_1.default.storedKeys);
|
|
753
|
+
addKeysSafeStub = sandbox.stub(wallet.storage, 'addKeysSafe').resolves();
|
|
754
|
+
saveWalletStub = sandbox.stub(wallet.storage, 'saveWallet').resolves();
|
|
755
|
+
homedirStub = sandbox.stub(os_1.default, 'homedir').returns(tempHomeDir);
|
|
756
|
+
sandbox.stub(wallet, 'getAddresses').resolves(ethMigrationTestWallet_fixture_1.default.addresses);
|
|
757
|
+
decryptedEncryptionKey = encryption_1.Encryption.decryptEncryptionKey(ethMigrationTestWallet_fixture_1.default.wallet.encryptionKey, ethMigrationTestWallet_fixture_1.default.password, true);
|
|
758
|
+
});
|
|
759
|
+
afterEach(function () {
|
|
760
|
+
homedirStub?.restore();
|
|
761
|
+
if (tempHomeDir) {
|
|
762
|
+
fs_1.default.rmSync(tempHomeDir, { recursive: true, force: true });
|
|
763
|
+
}
|
|
764
|
+
});
|
|
765
|
+
it('should back up existing raw wallet & keys (separately) before migration', async () => {
|
|
766
|
+
await wallet.migrateWallet(decryptedEncryptionKey);
|
|
767
|
+
expect(loadWalletStub.calledOnceWithExactly({ name: ethMigrationTestWallet_fixture_1.default.name, raw: true })).to.be.true;
|
|
768
|
+
expect(getStoredKeysStub.calledOnceWithExactly({
|
|
769
|
+
addresses: ethMigrationTestWallet_fixture_1.default.addresses,
|
|
770
|
+
name: ethMigrationTestWallet_fixture_1.default.name
|
|
771
|
+
})).to.be.true;
|
|
772
|
+
expect(fs_1.default.readFileSync(path_1.default.join(backupDir, `${ethMigrationTestWallet_fixture_1.default.name}.v1.bak`), 'utf8')).to.equal(ethMigrationTestWallet_fixture_1.default.rawWallet);
|
|
773
|
+
expect(fs_1.default.readFileSync(path_1.default.join(backupDir, `${ethMigrationTestWallet_fixture_1.default.name}_keys.v1.bak`), 'utf8')).to.equal(JSON.stringify(ethMigrationTestWallet_fixture_1.default.storedKeys));
|
|
774
|
+
});
|
|
775
|
+
it('should overwrite existing keys', async () => {
|
|
776
|
+
await wallet.migrateWallet(decryptedEncryptionKey);
|
|
777
|
+
expect(addKeysSafeStub.calledOnce).to.equal(true);
|
|
778
|
+
const { name, keys } = addKeysSafeStub.firstCall.args[0];
|
|
779
|
+
expect(name).to.equal(ethMigrationTestWallet_fixture_1.default.name);
|
|
780
|
+
expect(keys).to.have.length(ethMigrationTestWallet_fixture_1.default.storedKeys.length);
|
|
781
|
+
const expectedKeys = ethMigrationTestWallet_fixture_1.default.storedKeys.map(storedKey => {
|
|
782
|
+
const decryptedKey = JSON.parse(encryption_1.Encryption.decryptPrivateKey(storedKey.encKey, storedKey.pubKey, decryptedEncryptionKey));
|
|
783
|
+
const privKeyBuffer = CWC.Deriver.privateKeyToBuffer(wallet.chain, decryptedKey.privKey);
|
|
784
|
+
const encryptedPrivKey = encryption_1.Encryption.encryptBuffer(privKeyBuffer, storedKey.pubKey, decryptedEncryptionKey).toString('hex');
|
|
785
|
+
privKeyBuffer.fill(0);
|
|
786
|
+
return {
|
|
787
|
+
...decryptedKey,
|
|
788
|
+
privKey: encryptedPrivKey
|
|
789
|
+
};
|
|
790
|
+
});
|
|
791
|
+
expect(keys).to.deep.equal(expectedKeys);
|
|
792
|
+
});
|
|
793
|
+
it('should overwrite existing wallet', async () => {
|
|
794
|
+
await wallet.migrateWallet(decryptedEncryptionKey);
|
|
795
|
+
expect(saveWalletStub.calledOnce).to.equal(true);
|
|
796
|
+
const savedWallet = saveWalletStub.firstCall.args[0].wallet;
|
|
797
|
+
expect(savedWallet.version).to.equal(2);
|
|
798
|
+
expect(savedWallet.password).to.equal(ethMigrationTestWallet_fixture_1.default.wallet.password);
|
|
799
|
+
expect(savedWallet.masterKey).to.not.equal(ethMigrationTestWallet_fixture_1.default.wallet.masterKey);
|
|
800
|
+
const migratedMasterKey = JSON.parse(savedWallet.masterKey);
|
|
801
|
+
const originalMasterKey = JSON.parse(encryption_1.Encryption.decryptPrivateKey(ethMigrationTestWallet_fixture_1.default.wallet.masterKey, ethMigrationTestWallet_fixture_1.default.wallet.pubKey, decryptedEncryptionKey));
|
|
802
|
+
const migratedXpriv = encryption_1.Encryption.decryptToBuffer(migratedMasterKey.xprivkey, ethMigrationTestWallet_fixture_1.default.wallet.pubKey, decryptedEncryptionKey);
|
|
803
|
+
const migratedPrivateKey = encryption_1.Encryption.decryptToBuffer(migratedMasterKey.privateKey, ethMigrationTestWallet_fixture_1.default.wallet.pubKey, decryptedEncryptionKey);
|
|
804
|
+
expect(wallet.version).to.equal(2);
|
|
805
|
+
expect(migratedXpriv.toString('hex')).to.equal(CWC.BitcoreLib.encoding.Base58Check.decode(originalMasterKey.xprivkey).toString('hex'));
|
|
806
|
+
expect(migratedPrivateKey.toString('hex')).to.equal(originalMasterKey.privateKey);
|
|
807
|
+
});
|
|
808
|
+
it('should throw if the raw wallet cannot be loaded', async () => {
|
|
809
|
+
loadWalletStub.resolves(undefined);
|
|
810
|
+
try {
|
|
811
|
+
await wallet.migrateWallet(decryptedEncryptionKey);
|
|
812
|
+
expect.fail('Expected migrateWallet to throw');
|
|
813
|
+
}
|
|
814
|
+
catch (err) {
|
|
815
|
+
expect(err.message).to.equal('Migration failed - wallet not found');
|
|
816
|
+
}
|
|
817
|
+
expect(getStoredKeysStub.called).to.equal(false);
|
|
818
|
+
expect(addKeysSafeStub.called).to.equal(false);
|
|
819
|
+
expect(saveWalletStub.called).to.equal(false);
|
|
820
|
+
});
|
|
821
|
+
it('should throw if the decrypted masterKey is malformed', async () => {
|
|
822
|
+
wallet.masterKey = encryption_1.Encryption.encryptPrivateKey(JSON.stringify({ invalid: true }), wallet.pubKey, decryptedEncryptionKey.toString('hex'));
|
|
823
|
+
try {
|
|
824
|
+
await wallet.migrateWallet(decryptedEncryptionKey);
|
|
825
|
+
expect.fail('Expected migrateWallet to throw');
|
|
826
|
+
}
|
|
827
|
+
catch (err) {
|
|
828
|
+
expect(err.message).to.equal('Migration failure: masterKey is not formatted as expected');
|
|
829
|
+
}
|
|
830
|
+
expect(addKeysSafeStub.called).to.equal(false);
|
|
831
|
+
expect(saveWalletStub.called).to.equal(false);
|
|
832
|
+
});
|
|
833
|
+
it('should throw if addKeysSafe fails and should not save the wallet', async () => {
|
|
834
|
+
addKeysSafeStub.rejects(new Error('write failed'));
|
|
835
|
+
try {
|
|
836
|
+
await wallet.migrateWallet(decryptedEncryptionKey);
|
|
837
|
+
expect.fail('Expected migrateWallet to throw');
|
|
838
|
+
}
|
|
839
|
+
catch (err) {
|
|
840
|
+
expect(err.message).to.equal('Migration failure: keys not successfully stored. Use backups to restore prior wallet and keys.');
|
|
841
|
+
}
|
|
842
|
+
expect(addKeysSafeStub.calledOnce).to.equal(true);
|
|
843
|
+
expect(saveWalletStub.called).to.equal(false);
|
|
844
|
+
});
|
|
845
|
+
it('should no-op when the wallet is already on the current version', async () => {
|
|
846
|
+
wallet.version = 2;
|
|
847
|
+
const warnStub = sandbox.stub(console, 'warn');
|
|
848
|
+
const migratedWallet = await wallet.migrateWallet(decryptedEncryptionKey);
|
|
849
|
+
expect(migratedWallet).to.equal(wallet);
|
|
850
|
+
expect(warnStub.calledOnceWithExactly('Wallet migration unnecessarily called - wallet is current version')).to.equal(true);
|
|
851
|
+
expect(loadWalletStub.called).to.equal(false);
|
|
852
|
+
expect(getStoredKeysStub.called).to.equal(false);
|
|
853
|
+
expect(addKeysSafeStub.called).to.equal(false);
|
|
854
|
+
expect(saveWalletStub.called).to.equal(false);
|
|
855
|
+
expect(fs_1.default.existsSync(path_1.default.join(backupDir, `${ethMigrationTestWallet_fixture_1.default.name}.v2.bak`))).to.equal(false);
|
|
856
|
+
});
|
|
857
|
+
it('should no-op when the wallet version is newer than the current version', async () => {
|
|
858
|
+
wallet.version = 3;
|
|
859
|
+
const warnStub = sandbox.stub(console, 'warn');
|
|
860
|
+
const migratedWallet = await wallet.migrateWallet(decryptedEncryptionKey);
|
|
861
|
+
expect(migratedWallet).to.equal(wallet);
|
|
862
|
+
expect(warnStub.calledOnceWithExactly('Wallet version 3 greater than expected current wallet version 2')).to.equal(true);
|
|
863
|
+
expect(loadWalletStub.called).to.equal(false);
|
|
864
|
+
expect(getStoredKeysStub.called).to.equal(false);
|
|
865
|
+
expect(addKeysSafeStub.called).to.equal(false);
|
|
866
|
+
expect(saveWalletStub.called).to.equal(false);
|
|
867
|
+
expect(fs_1.default.existsSync(path_1.default.join(backupDir, `${ethMigrationTestWallet_fixture_1.default.name}.v3.bak`))).to.equal(false);
|
|
868
|
+
});
|
|
369
869
|
});
|
|
370
870
|
describe('getBalance', function () {
|
|
371
871
|
walletName = 'BitcoreClientTestGetBalance';
|
|
372
872
|
beforeEach(async function () {
|
|
373
|
-
wallet = await
|
|
873
|
+
wallet = await createWalletForTest({
|
|
374
874
|
name: walletName,
|
|
375
875
|
chain: 'MATIC',
|
|
376
876
|
network: 'testnet',
|
|
377
877
|
phrase: 'snap impact summer because must pipe weasel gorilla actor acid web whip',
|
|
378
878
|
password: 'abc123',
|
|
379
879
|
storageType: 'Level',
|
|
880
|
+
version: 0,
|
|
380
881
|
});
|
|
381
882
|
await wallet.unlock('abc123');
|
|
382
883
|
});
|
|
@@ -415,13 +916,14 @@ describe('Wallet', function () {
|
|
|
415
916
|
describe('getTokenObj', function () {
|
|
416
917
|
walletName = 'BitcoreClientTestGetTokenObj';
|
|
417
918
|
beforeEach(async function () {
|
|
418
|
-
wallet = await
|
|
919
|
+
wallet = await createWalletForTest({
|
|
419
920
|
name: walletName,
|
|
420
921
|
chain: 'MATIC',
|
|
421
922
|
network: 'testnet',
|
|
422
923
|
phrase: 'snap impact summer because must pipe weasel gorilla actor acid web whip',
|
|
423
924
|
password: 'abc123',
|
|
424
925
|
storageType: 'Level',
|
|
926
|
+
version: 0,
|
|
425
927
|
});
|
|
426
928
|
await wallet.unlock('abc123');
|
|
427
929
|
});
|
|
@@ -485,7 +987,7 @@ describe('Wallet', function () {
|
|
|
485
987
|
name: 'DAIn'
|
|
486
988
|
};
|
|
487
989
|
beforeEach(async function () {
|
|
488
|
-
wallet = await
|
|
990
|
+
wallet = await createWalletForTest({
|
|
489
991
|
chain: 'ETH',
|
|
490
992
|
network: 'mainnet',
|
|
491
993
|
name: walletName,
|
|
@@ -493,7 +995,8 @@ describe('Wallet', function () {
|
|
|
493
995
|
password: 'abc123',
|
|
494
996
|
lite: false,
|
|
495
997
|
storageType,
|
|
496
|
-
baseUrl
|
|
998
|
+
baseUrl,
|
|
999
|
+
version: 0
|
|
497
1000
|
});
|
|
498
1001
|
wallet.tokens = [
|
|
499
1002
|
usdcLegacyObj,
|