@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/src/wallet.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
+
import { mkdir, writeFile } from 'fs/promises';
|
|
1
2
|
import 'source-map-support/register';
|
|
3
|
+
import os from 'os';
|
|
4
|
+
import path from 'path';
|
|
2
5
|
import Mnemonic from '@bitpay-labs/bitcore-mnemonic';
|
|
3
6
|
import {
|
|
4
7
|
BitcoreLib,
|
|
@@ -18,6 +21,7 @@ import * as Bcrypt from 'bcrypt';
|
|
|
18
21
|
import { Client } from './client';
|
|
19
22
|
import { Encryption } from './encryption';
|
|
20
23
|
import { Storage } from './storage';
|
|
24
|
+
import { TextFile } from './storage/textFile';
|
|
21
25
|
import { ParseApiStream } from './stream-util';
|
|
22
26
|
import { StorageType } from './types/storage';
|
|
23
27
|
import { BumpTxFeeType, IWallet, KeyImport } from './types/wallet';
|
|
@@ -36,9 +40,26 @@ const chainLibs = {
|
|
|
36
40
|
XRP: xrpl,
|
|
37
41
|
SOL: { SolKit, SolanaProgram }
|
|
38
42
|
};
|
|
43
|
+
const CURRENT_WALLET_VERSION = 2;
|
|
44
|
+
|
|
45
|
+
const baseUrls = {
|
|
46
|
+
BTC: 'https://api.bitcore.io/api',
|
|
47
|
+
BCH: 'https://api.bitcore.io/api',
|
|
48
|
+
DOGE: 'https://api.bitcore.io/api',
|
|
49
|
+
LTC: 'https://api.bitcore.io/api',
|
|
50
|
+
ETH: 'https://api-eth.bitcore.io/api',
|
|
51
|
+
MATIC: 'https://api-matic.bitcore.io/api',
|
|
52
|
+
ARB: 'https://api-eth.bitcore.io/api',
|
|
53
|
+
OP: 'https://api-eth.bitcore.io/api',
|
|
54
|
+
BASE: 'https://api-eth.bitcore.io/api',
|
|
55
|
+
ARC: 'https://api-eth.bitcore.io/api',
|
|
56
|
+
XRP: 'https://api-xrp.bitcore.io/api',
|
|
57
|
+
SOL: 'https://api-sol.bitcore.io/api'
|
|
58
|
+
};
|
|
39
59
|
|
|
40
60
|
export interface IWalletExt extends IWallet {
|
|
41
61
|
storage?: Storage;
|
|
62
|
+
version?: 0 | 2; // Wallet versioning used for backwards compatibility
|
|
42
63
|
}
|
|
43
64
|
|
|
44
65
|
export class Wallet {
|
|
@@ -49,7 +70,7 @@ export class Wallet {
|
|
|
49
70
|
client: Client;
|
|
50
71
|
storage: Storage;
|
|
51
72
|
storageType: string;
|
|
52
|
-
unlocked?: { encryptionKey:
|
|
73
|
+
unlocked?: { encryptionKey: Buffer; masterKey: { xprivkey: Buffer; privateKey: Buffer } };
|
|
53
74
|
password: string;
|
|
54
75
|
encryptionKey: string;
|
|
55
76
|
authPubKey: string;
|
|
@@ -64,13 +85,14 @@ export class Wallet {
|
|
|
64
85
|
lite: boolean;
|
|
65
86
|
addressType: string;
|
|
66
87
|
addressZero: string;
|
|
88
|
+
version?: number;
|
|
67
89
|
|
|
68
90
|
static XrpAccountFlags = xrpl.AccountSetTfFlags;
|
|
69
91
|
|
|
70
92
|
constructor(params: Wallet | IWalletExt) {
|
|
71
93
|
Object.assign(this, params);
|
|
72
94
|
if (!this.baseUrl) {
|
|
73
|
-
this.baseUrl = 'https://api.bitcore.io/api';
|
|
95
|
+
this.baseUrl = baseUrls[this.chain.toUpperCase()] || 'https://api.bitcore.io/api';
|
|
74
96
|
}
|
|
75
97
|
this.client = new Client({
|
|
76
98
|
apiUrl: this.getApiUrl(),
|
|
@@ -120,7 +142,8 @@ export class Wallet {
|
|
|
120
142
|
storageType: this.storageType,
|
|
121
143
|
lite,
|
|
122
144
|
addressType: this.addressType,
|
|
123
|
-
addressZero: this.addressZero
|
|
145
|
+
addressZero: this.addressZero,
|
|
146
|
+
version: this.version
|
|
124
147
|
};
|
|
125
148
|
}
|
|
126
149
|
|
|
@@ -157,11 +180,7 @@ export class Wallet {
|
|
|
157
180
|
const keyType = Constants.ALGO_TO_KEY_TYPE[algo];
|
|
158
181
|
hdPrivKey = mnemonic.toHDPrivateKey('', network).derive(Deriver.pathFor(chain, network), keyType);
|
|
159
182
|
}
|
|
160
|
-
const privKeyObj = hdPrivKey.
|
|
161
|
-
|
|
162
|
-
// Generate authentication keys
|
|
163
|
-
const authKey = new PrivateKey();
|
|
164
|
-
const authPubKey = authKey.toPublicKey().toString();
|
|
183
|
+
const privKeyObj = hdPrivKey.toObjectWithBufferPrivateKey();
|
|
165
184
|
|
|
166
185
|
// Generate public keys
|
|
167
186
|
// bip44 compatible pubKey
|
|
@@ -169,8 +188,18 @@ export class Wallet {
|
|
|
169
188
|
|
|
170
189
|
// Generate and encrypt the encryption key and private key
|
|
171
190
|
const walletEncryptionKey = Encryption.generateEncryptionKey();
|
|
172
|
-
const encryptionKey = Encryption.encryptEncryptionKey(walletEncryptionKey, password);
|
|
173
|
-
|
|
191
|
+
const encryptionKey = Encryption.encryptEncryptionKey(walletEncryptionKey, password); // stored, password-wrapped
|
|
192
|
+
|
|
193
|
+
// Encrypt privKeyObj.privateKey & privKeyObj.xprivkey
|
|
194
|
+
const xprivBuffer = BitcoreLib.encoding.Base58Check.decode(privKeyObj.xprivkey);
|
|
195
|
+
privKeyObj.xprivkey = Encryption.encryptBuffer(xprivBuffer, pubKey, walletEncryptionKey).toString('hex');
|
|
196
|
+
privKeyObj.privateKey = Encryption.encryptBuffer(privKeyObj.privateKey, pubKey, walletEncryptionKey).toString('hex');
|
|
197
|
+
|
|
198
|
+
// Generate authentication keys
|
|
199
|
+
const authKey = new PrivateKey();
|
|
200
|
+
const authPubKey = authKey.toPublicKey().toString();
|
|
201
|
+
|
|
202
|
+
const masterKeyWithEncryptedPrivateKeys = JSON.stringify(privKeyObj);
|
|
174
203
|
|
|
175
204
|
storageType = storageType ? storageType : 'Level';
|
|
176
205
|
storage =
|
|
@@ -182,13 +211,11 @@ export class Wallet {
|
|
|
182
211
|
storageType
|
|
183
212
|
});
|
|
184
213
|
|
|
185
|
-
|
|
186
|
-
try {
|
|
187
|
-
alreadyExists = await this.loadWallet({ storage, name, storageType });
|
|
188
|
-
} catch { /* ignore */ }
|
|
214
|
+
const alreadyExists = await this.loadWallet({ storage, name, storageType }).catch(() => {/** no op */});
|
|
189
215
|
if (alreadyExists) {
|
|
190
216
|
throw new Error('Wallet already exists');
|
|
191
217
|
}
|
|
218
|
+
|
|
192
219
|
const wallet = new Wallet({
|
|
193
220
|
name,
|
|
194
221
|
chain,
|
|
@@ -198,7 +225,7 @@ export class Wallet {
|
|
|
198
225
|
encryptionKey,
|
|
199
226
|
authKey,
|
|
200
227
|
authPubKey,
|
|
201
|
-
masterKey:
|
|
228
|
+
masterKey: masterKeyWithEncryptedPrivateKeys,
|
|
202
229
|
password,
|
|
203
230
|
xPubKey: hdPrivKey.xpubkey,
|
|
204
231
|
pubKey,
|
|
@@ -207,7 +234,8 @@ export class Wallet {
|
|
|
207
234
|
storageType,
|
|
208
235
|
lite,
|
|
209
236
|
addressType,
|
|
210
|
-
addressZero: null
|
|
237
|
+
addressZero: null,
|
|
238
|
+
version: CURRENT_WALLET_VERSION,
|
|
211
239
|
} as IWalletExt);
|
|
212
240
|
|
|
213
241
|
// save wallet to storage and then bitcore-node
|
|
@@ -219,9 +247,11 @@ export class Wallet {
|
|
|
219
247
|
});
|
|
220
248
|
|
|
221
249
|
if (!xpriv) {
|
|
222
|
-
|
|
250
|
+
process.stdout.write(Buffer.from(mnemonic.phrase));
|
|
251
|
+
process.stdout.write(os.EOL);
|
|
223
252
|
} else {
|
|
224
|
-
|
|
253
|
+
process.stdout.write(hdPrivKey.toBuffer());
|
|
254
|
+
process.stdout.write(os.EOL);
|
|
225
255
|
}
|
|
226
256
|
|
|
227
257
|
await loadedWallet.register().catch(e => {
|
|
@@ -251,11 +281,12 @@ export class Wallet {
|
|
|
251
281
|
let { storage } = params;
|
|
252
282
|
storage = storage || new Storage({ errorIfExists: false, createIfMissing: false, path, storageType });
|
|
253
283
|
const loadedWallet = await storage.loadWallet({ name });
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
} else {
|
|
284
|
+
|
|
285
|
+
if (!loadedWallet) {
|
|
257
286
|
throw new Error('No wallet could be found');
|
|
258
287
|
}
|
|
288
|
+
|
|
289
|
+
return new Wallet(Object.assign(loadedWallet, { storage }));
|
|
259
290
|
}
|
|
260
291
|
|
|
261
292
|
/**
|
|
@@ -280,7 +311,24 @@ export class Wallet {
|
|
|
280
311
|
}
|
|
281
312
|
|
|
282
313
|
lock() {
|
|
283
|
-
this.unlocked
|
|
314
|
+
if (!this.unlocked) {
|
|
315
|
+
return this;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
if (Buffer.isBuffer(this.unlocked.masterKey.xprivkey)) {
|
|
319
|
+
this.unlocked.masterKey.xprivkey.fill(0);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
if (Buffer.isBuffer(this.unlocked.masterKey.privateKey)) {
|
|
323
|
+
this.unlocked.masterKey.privateKey.fill(0);
|
|
324
|
+
}
|
|
325
|
+
this.unlocked.masterKey = null;
|
|
326
|
+
|
|
327
|
+
if (Buffer.isBuffer(this.unlocked.encryptionKey)) {
|
|
328
|
+
this.unlocked.encryptionKey.fill(0);
|
|
329
|
+
}
|
|
330
|
+
this.unlocked.encryptionKey = null;
|
|
331
|
+
this.unlocked = null;
|
|
284
332
|
return this;
|
|
285
333
|
}
|
|
286
334
|
|
|
@@ -289,12 +337,15 @@ export class Wallet {
|
|
|
289
337
|
if (!validPass) {
|
|
290
338
|
throw new Error('Incorrect Password');
|
|
291
339
|
}
|
|
292
|
-
const encryptionKey =
|
|
340
|
+
const encryptionKey = Encryption.decryptEncryptionKey(this.encryptionKey, password, true);
|
|
341
|
+
if (this.version != CURRENT_WALLET_VERSION) {
|
|
342
|
+
await this.migrateWallet(encryptionKey);
|
|
343
|
+
}
|
|
293
344
|
let masterKey;
|
|
294
345
|
if (!this.lite) {
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
masterKey =
|
|
346
|
+
masterKey = JSON.parse(this.masterKey);
|
|
347
|
+
masterKey.xprivkey = Encryption.decryptToBuffer(masterKey.xprivkey, this.pubKey, encryptionKey);
|
|
348
|
+
masterKey.privateKey = Encryption.decryptToBuffer(masterKey.privateKey, this.pubKey, encryptionKey);
|
|
298
349
|
}
|
|
299
350
|
this.unlocked = {
|
|
300
351
|
encryptionKey,
|
|
@@ -303,6 +354,142 @@ export class Wallet {
|
|
|
303
354
|
return this;
|
|
304
355
|
}
|
|
305
356
|
|
|
357
|
+
async migrateWallet(encryptionKey: Buffer): Promise<Wallet> {
|
|
358
|
+
const preMigrationVersion = this.version ?? 1;
|
|
359
|
+
/**
|
|
360
|
+
* 0: Checks
|
|
361
|
+
*/
|
|
362
|
+
if (preMigrationVersion == CURRENT_WALLET_VERSION) {
|
|
363
|
+
console.warn('Wallet migration unnecessarily called - wallet is current version');
|
|
364
|
+
return this;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
if (preMigrationVersion > CURRENT_WALLET_VERSION) {
|
|
368
|
+
console.warn(`Wallet version ${preMigrationVersion} greater than expected current wallet version ${CURRENT_WALLET_VERSION}`);
|
|
369
|
+
return this;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
console.log(`Migrating wallet from version ${preMigrationVersion} to version ${CURRENT_WALLET_VERSION}`);
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
* 1: Wallet to .bak
|
|
376
|
+
*/
|
|
377
|
+
const rawWallet = await this.storage.loadWallet({ name: this.name, raw: true });
|
|
378
|
+
if (!rawWallet) {
|
|
379
|
+
throw new Error('Migration failed - wallet not found');
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
const backupDir = path.join(
|
|
383
|
+
os.homedir(),
|
|
384
|
+
'.bitcore',
|
|
385
|
+
'bitcoreWallet',
|
|
386
|
+
'backup'
|
|
387
|
+
);
|
|
388
|
+
await mkdir(backupDir, { recursive: true });
|
|
389
|
+
|
|
390
|
+
const walletFilePath = path.join(backupDir, `${this.name}.v${preMigrationVersion}.bak`);
|
|
391
|
+
|
|
392
|
+
await writeFile(walletFilePath, rawWallet, 'utf8')
|
|
393
|
+
.then(() => console.log(`Pre-migration wallet backup written to ${walletFilePath}`))
|
|
394
|
+
.catch(err => {
|
|
395
|
+
console.error('Wallet backup failed, aborting migration', err.message);
|
|
396
|
+
throw new Error('Migration failure: failed to write wallet backup file. Aborting.');
|
|
397
|
+
});
|
|
398
|
+
|
|
399
|
+
/**
|
|
400
|
+
* Retrieve stored keys for backup and for migration
|
|
401
|
+
*/
|
|
402
|
+
const addresses = await this.getAddresses();
|
|
403
|
+
const storedKeys = await this.storage.getStoredKeys({
|
|
404
|
+
addresses,
|
|
405
|
+
name: this.name,
|
|
406
|
+
});
|
|
407
|
+
|
|
408
|
+
// Back up keys (enc)
|
|
409
|
+
const backupKeysStr = JSON.stringify(storedKeys);
|
|
410
|
+
const keysFilePath = path.join(backupDir, `${this.name}_keys.v${preMigrationVersion}.bak`);
|
|
411
|
+
await writeFile(keysFilePath, backupKeysStr, 'utf8')
|
|
412
|
+
.then(() => console.log(`Pre-migration keys backup written to ${keysFilePath}`))
|
|
413
|
+
.catch(err => {
|
|
414
|
+
console.error('Keys backup failed, aborting migration', err.message);
|
|
415
|
+
throw new Error('Migration failure: failed to write keys backup file. Aborting.');
|
|
416
|
+
});
|
|
417
|
+
|
|
418
|
+
/**
|
|
419
|
+
* 2. Convert
|
|
420
|
+
*/
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* 2a. Convert masterKey and encryptionKey
|
|
424
|
+
*/
|
|
425
|
+
const masterKeyStr = Encryption.decryptPrivateKey(this.masterKey, this.pubKey, encryptionKey);
|
|
426
|
+
const masterKey = JSON.parse(masterKeyStr);
|
|
427
|
+
if (!(masterKey.xprivkey && masterKey.privateKey)) {
|
|
428
|
+
throw new Error('Migration failure: masterKey is not formatted as expected');
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
const xprivBuffer = BitcoreLib.encoding.Base58Check.decode(masterKey.xprivkey);
|
|
432
|
+
const enc_xprivkeyBuffer = Encryption.encryptBuffer(xprivBuffer, this.pubKey, encryptionKey);
|
|
433
|
+
xprivBuffer.fill(0);
|
|
434
|
+
|
|
435
|
+
masterKey.xprivkey = enc_xprivkeyBuffer.toString('hex');
|
|
436
|
+
enc_xprivkeyBuffer.fill(0);
|
|
437
|
+
|
|
438
|
+
const privateKeyBuffer = Buffer.from(masterKey.privateKey, 'hex');
|
|
439
|
+
const enc_privateKeyBuffer = Encryption.encryptBuffer(privateKeyBuffer, this.pubKey, encryptionKey);
|
|
440
|
+
privateKeyBuffer.fill(0);
|
|
441
|
+
|
|
442
|
+
masterKey.privateKey = enc_privateKeyBuffer.toString('hex');
|
|
443
|
+
enc_privateKeyBuffer.fill(0);
|
|
444
|
+
|
|
445
|
+
// String with encrypted hex-encoded xprivkey and privateKey strings
|
|
446
|
+
this.masterKey = JSON.stringify(masterKey);
|
|
447
|
+
|
|
448
|
+
/**
|
|
449
|
+
* 2b. Convert signing keys
|
|
450
|
+
*/
|
|
451
|
+
const newKeys = [];
|
|
452
|
+
for (const key of storedKeys) {
|
|
453
|
+
const { encKey, pubKey } = key;
|
|
454
|
+
const decryptedKey = Encryption.decryptPrivateKey(encKey, pubKey, encryptionKey);
|
|
455
|
+
const decryptedKeyJSON = JSON.parse(decryptedKey);
|
|
456
|
+
|
|
457
|
+
// Convert private key to buffer format (uniform across all chains)
|
|
458
|
+
const privKeyBuffer = Deriver.privateKeyToBuffer(this.chain, decryptedKeyJSON.privKey);
|
|
459
|
+
const encryptedPrivateKeyBuffer = Encryption.encryptBuffer(privKeyBuffer, pubKey, encryptionKey);
|
|
460
|
+
privKeyBuffer.fill(0); // Zero out the plaintext buffer
|
|
461
|
+
|
|
462
|
+
decryptedKeyJSON.privKey = encryptedPrivateKeyBuffer.toString('hex');
|
|
463
|
+
newKeys.push(decryptedKeyJSON);
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
/**
|
|
467
|
+
* 3. Overwrite
|
|
468
|
+
*/
|
|
469
|
+
// 3a. Overwrite keys
|
|
470
|
+
const overwriteKeys = this.storage.storageType instanceof TextFile
|
|
471
|
+
? this.storage.storageType.migrate({ version: preMigrationVersion, name: this.name, keys: newKeys })
|
|
472
|
+
: this.storage.addKeysSafe({ name: this.name, keys: newKeys });
|
|
473
|
+
await overwriteKeys.catch(err => {
|
|
474
|
+
console.error('Migration failure: updated keys not successfully stored', err);
|
|
475
|
+
throw new Error('Migration failure: keys not successfully stored. Use backups to restore prior wallet and keys.');
|
|
476
|
+
});
|
|
477
|
+
|
|
478
|
+
// 3b. Overwrite wallet
|
|
479
|
+
this.version = CURRENT_WALLET_VERSION;
|
|
480
|
+
const storedEncryptedPassword = this.password; // Wallet.toObject() rehashes password - save and replace
|
|
481
|
+
const walletObj = this.toObject(false);
|
|
482
|
+
walletObj.password = storedEncryptedPassword;
|
|
483
|
+
await this.storage.saveWallet({ wallet: walletObj })
|
|
484
|
+
.catch(err => {
|
|
485
|
+
console.error('Migration failure: wallet not successfully saved', err);
|
|
486
|
+
throw new Error('Migration failure: wallet not successfully saved. Use backups to restore prior wallet and keys');
|
|
487
|
+
});
|
|
488
|
+
|
|
489
|
+
console.log('Migration succeeded');
|
|
490
|
+
return this;
|
|
491
|
+
}
|
|
492
|
+
|
|
306
493
|
async register(params: { baseUrl?: string } = {}) {
|
|
307
494
|
const { baseUrl } = params;
|
|
308
495
|
if (baseUrl) {
|
|
@@ -408,7 +595,6 @@ export class Wallet {
|
|
|
408
595
|
// If tokenName was given, find the token by name (e.g. USDC_m)
|
|
409
596
|
let tokenObj = tokenName && this.tokens.find(tok => tok.name === tokenName);
|
|
410
597
|
// If not found by name AND token was given, find the token by symbol (e.g. USDC)
|
|
411
|
-
// NOTE: we don't want to
|
|
412
598
|
tokenObj = tokenObj || (token && this.tokens.find(tok => tok.symbol === token && [token, undefined].includes(tok.name)));
|
|
413
599
|
if (!tokenObj) {
|
|
414
600
|
throw new Error(`${tokenName || token} not found on wallet ${this.name}`);
|
|
@@ -596,26 +782,43 @@ export class Wallet {
|
|
|
596
782
|
}
|
|
597
783
|
|
|
598
784
|
async importKeys(params: { keys: KeyImport[]; rederiveAddys?: boolean }) {
|
|
599
|
-
const { encryptionKey } = this.unlocked;
|
|
600
785
|
const { rederiveAddys } = params;
|
|
601
786
|
let { keys } = params;
|
|
602
|
-
|
|
787
|
+
// Avoid mutating caller-owned references (we'll encrypt privKeys below)
|
|
788
|
+
keys = keys.map(k => ({ ...k }));
|
|
789
|
+
|
|
790
|
+
const keysToSave: KeyImport[] = [];
|
|
791
|
+
for (const key of keys) {
|
|
792
|
+
if (typeof key.privKey !== 'string') {
|
|
793
|
+
continue;
|
|
794
|
+
}
|
|
795
|
+
|
|
796
|
+
let privKeyBuffer: Buffer | undefined;
|
|
797
|
+
try {
|
|
798
|
+
privKeyBuffer = Deriver.privateKeyToBuffer(this.chain, key.privKey);
|
|
799
|
+
if (typeof key.pubKey !== 'string') {
|
|
800
|
+
key.pubKey = Deriver.getPublicKey(this.chain, this.network, privKeyBuffer);
|
|
801
|
+
}
|
|
802
|
+
if (!key.pubKey) {
|
|
803
|
+
throw new Error(`pubKey is undefined for ${this.name}. Keys not added to storage`);
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
if (rederiveAddys) {
|
|
807
|
+
key.address = Deriver.getAddress(this.chain, this.network, key.pubKey, this.addressType);
|
|
808
|
+
}
|
|
603
809
|
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
address: key.pubKey ? Deriver.getAddress(this.chain, this.network, key.pubKey, this.addressType) : key.address
|
|
612
|
-
}) as KeyImport);
|
|
810
|
+
key.privKey = Encryption.encryptBuffer(privKeyBuffer, key.pubKey, this.unlocked.encryptionKey).toString('hex');
|
|
811
|
+
} finally {
|
|
812
|
+
if (Buffer.isBuffer(privKeyBuffer)) {
|
|
813
|
+
privKeyBuffer.fill(0);
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
keysToSave.push(key);
|
|
613
817
|
}
|
|
614
818
|
|
|
615
819
|
if (keysToSave.length) {
|
|
616
|
-
await this.storage.
|
|
820
|
+
await this.storage.addKeysSafe({
|
|
617
821
|
keys: keysToSave,
|
|
618
|
-
encryptionKey,
|
|
619
822
|
name: this.name
|
|
620
823
|
});
|
|
621
824
|
}
|
|
@@ -642,24 +845,27 @@ export class Wallet {
|
|
|
642
845
|
}
|
|
643
846
|
let addresses = [];
|
|
644
847
|
let decryptedKeys;
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
addresses
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
848
|
+
let decryptPrivateKeys = true;
|
|
849
|
+
if (!signingKeys) {
|
|
850
|
+
if (!keys) {
|
|
851
|
+
for (const utxo of utxos) {
|
|
852
|
+
addresses.push(utxo.address);
|
|
853
|
+
}
|
|
854
|
+
addresses = addresses.length > 0 ? addresses : await this.getAddresses();
|
|
855
|
+
decryptedKeys = await this.storage.getStoredKeys({
|
|
856
|
+
addresses,
|
|
857
|
+
name: this.name,
|
|
858
|
+
});
|
|
859
|
+
} else {
|
|
860
|
+
addresses.push(keys[0]);
|
|
861
|
+
for (const element of utxos) {
|
|
862
|
+
const keyToDecrypt = keys.find(key => key.address === element.address);
|
|
863
|
+
addresses.push(keyToDecrypt);
|
|
864
|
+
}
|
|
865
|
+
const decryptedParams = Encryption.bitcoinCoreDecrypt(addresses, passphrase);
|
|
866
|
+
decryptedKeys = [...decryptedParams.jsonlDecrypted];
|
|
867
|
+
decryptPrivateKeys = false;
|
|
660
868
|
}
|
|
661
|
-
const decryptedParams = Encryption.bitcoinCoreDecrypt(addresses, passphrase);
|
|
662
|
-
decryptedKeys = [...decryptedParams.jsonlDecrypted];
|
|
663
869
|
}
|
|
664
870
|
if (this.isUtxoChain()) {
|
|
665
871
|
// If changeAddressIdx == null, then save the change key at the current addressIndex (just in case)
|
|
@@ -667,12 +873,37 @@ export class Wallet {
|
|
|
667
873
|
await this.importKeys({ keys: [changeKey] });
|
|
668
874
|
}
|
|
669
875
|
|
|
876
|
+
// Shallow copy to avoid mutation if signingKeys are passed in
|
|
877
|
+
const keysForSigning = [...(signingKeys || decryptedKeys)];
|
|
878
|
+
|
|
879
|
+
if (decryptPrivateKeys) {
|
|
880
|
+
for (const key of keysForSigning) {
|
|
881
|
+
let privKeyBuf: Buffer | undefined;
|
|
882
|
+
try {
|
|
883
|
+
privKeyBuf = Encryption.decryptToBuffer(key.privKey, key.pubKey, this.unlocked.encryptionKey);
|
|
884
|
+
|
|
885
|
+
// Convert buffer to chain-specific native format (e.g., WIF for BTC, hex for ETH, base58 for SOL)
|
|
886
|
+
const nativePrivKey = Deriver.bufferToPrivateKey_TEMP(this.chain, this.network, privKeyBuf);
|
|
887
|
+
|
|
888
|
+
key.privKey = nativePrivKey;
|
|
889
|
+
} catch (e) {
|
|
890
|
+
console.error('Failed to decrypt/convert private key:', e);
|
|
891
|
+
continue;
|
|
892
|
+
} finally {
|
|
893
|
+
// Zero out the buffer immediately after use
|
|
894
|
+
if (Buffer.isBuffer(privKeyBuf)) {
|
|
895
|
+
privKeyBuf.fill(0);
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
|
|
670
901
|
const payload = {
|
|
671
902
|
chain: this.chain,
|
|
672
903
|
network: this.network,
|
|
673
904
|
tx,
|
|
674
|
-
keys:
|
|
675
|
-
key:
|
|
905
|
+
keys: keysForSigning,
|
|
906
|
+
key: keysForSigning[0],
|
|
676
907
|
utxos
|
|
677
908
|
};
|
|
678
909
|
return Transactions.sign({ ...payload });
|
|
@@ -741,7 +972,7 @@ export class Wallet {
|
|
|
741
972
|
}
|
|
742
973
|
|
|
743
974
|
async derivePrivateKey(isChange, addressIndex = this.addressIndex) {
|
|
744
|
-
|
|
975
|
+
return Deriver.derivePrivateKey(
|
|
745
976
|
this.chain,
|
|
746
977
|
this.network,
|
|
747
978
|
this.unlocked.masterKey,
|
|
@@ -749,7 +980,6 @@ export class Wallet {
|
|
|
749
980
|
isChange,
|
|
750
981
|
this.addressType
|
|
751
982
|
);
|
|
752
|
-
return keyToImport;
|
|
753
983
|
}
|
|
754
984
|
|
|
755
985
|
async nextAddressPair(withChangeAddress?: boolean) {
|
|
@@ -827,7 +1057,7 @@ export class Wallet {
|
|
|
827
1057
|
let existingTx;
|
|
828
1058
|
if (rawTx) {
|
|
829
1059
|
if (lib.ethers) {
|
|
830
|
-
existingTx = lib.ethers.
|
|
1060
|
+
existingTx = lib.ethers.Transaction.from(rawTx);
|
|
831
1061
|
} else {
|
|
832
1062
|
const tx = new lib.Transaction(rawTx);
|
|
833
1063
|
txid = tx.id;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
export const ethMigrationTestWalletFixture = {
|
|
2
|
+
exportedAt: '2026-03-11T19:39:04.994Z',
|
|
3
|
+
name: 'ETH_migration_test_wallet',
|
|
4
|
+
storageType: 'Level',
|
|
5
|
+
password: 'password',
|
|
6
|
+
wallet: {
|
|
7
|
+
name: 'ETH_migration_test_wallet',
|
|
8
|
+
chain: 'ETH',
|
|
9
|
+
network: 'regtest',
|
|
10
|
+
baseUrl: 'http://localhost:3000/api',
|
|
11
|
+
encryptionKey: '7eab496f0253b5395844356faa629b48ff063fb4186c51e8e448ee7db0eeb9b24afd041bad14c7153dd35a2755b5438f',
|
|
12
|
+
authKey: {
|
|
13
|
+
bn: '1a0202578666668324a496bd06b9bbf1c197ec5a6b2f0c3ccd6470abbc5328a4',
|
|
14
|
+
compressed: true,
|
|
15
|
+
network: 'livenet'
|
|
16
|
+
},
|
|
17
|
+
authPubKey: '03a7107731216187960c8b7436e3cdc5d4d1b7004e21d85907a402838b5702a118',
|
|
18
|
+
masterKey: 'e8daacd89b584a43c5e14b9baba7b94c358d4e271fa6eaa3fd140bebc7507a2f80e88a460042a785adf07660b5126fb05016416fa1544c5f20f1cb9a9bb25c4cca198b9162075e902143b5dfebfcb0e88c305fc08ab0f672f5cdfaeb0ee501a4ba5a1d22f586f69ab4625a719b1b9a4ce5f217574417892fc8748b08db5a4b76938e470e768054e3758071e6de291c66d372c30613a02d0c2a63647bbfa4a305f094138d728e78b6f7f05017bc0c0c8bd390f9837e56fa463f6edeb4761fe645c53c482cae062af7fe4a01c5045fee2fbcc93b56e4362d9f0b9aaa4bf4352e3abd430cd967747fc68c2515aa088a6b5f34751a2cfaa34ef02eca74d5d7aaca2d5ed109348d2e85a22ae08a665543649279b405dac3e8879e0e1f5a784e6af1aaa5b1253bca30cbb2197346e9ce13ff358963cce0f11f46c798dc25ebfda9d920e374c785ae8983b30f661ba9b26806def02553d85533225b394847269861c310dc8c19914348219ff2d59b8d9e7ed22ee79c7552353b22eebe7e6a568e04371071932c1f5331009455b0e7944bcfa5d9e685a44f637aa53d9eafc7d3b62eafdee70543732d5d646f60108fd2c0bdef36',
|
|
19
|
+
password: '$2b$10$EgUUeV0zld1TOQvjMOmmPuyWQxF9hwpCBe1G4UiaAW1XYNY5/wMDm',
|
|
20
|
+
xPubKey: 'tpubDDT9XiD9hNQpHjpCizEvPq2TqDwZYwtKKiKHJuC5AAV2vbfooRVg2Mi7WogY8ygCTve5NgAjDXWEo3JF6sPayayYM2kNL3sQMeb6bDbv1Lk',
|
|
21
|
+
pubKey: '0332f26acf0584acb762eb99e638190e6ea1ddbae6bb59f79d8611d2412a50791d',
|
|
22
|
+
tokens: [],
|
|
23
|
+
storageType: 'Level',
|
|
24
|
+
addressType: 'pubkeyhash',
|
|
25
|
+
addressZero: '0xAf4a2A1c3cc0cbE9FF17B04dD91692F7686696Af',
|
|
26
|
+
client: {
|
|
27
|
+
apiUrl: 'http://localhost:3000/api/ETH/regtest',
|
|
28
|
+
authKey: {
|
|
29
|
+
bn: '1a0202578666668324a496bd06b9bbf1c197ec5a6b2f0c3ccd6470abbc5328a4',
|
|
30
|
+
compressed: true,
|
|
31
|
+
network: 'livenet'
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
addressIndex: 2,
|
|
35
|
+
lite: false
|
|
36
|
+
},
|
|
37
|
+
rawWallet:
|
|
38
|
+
'{"name":"ETH_migration_test_wallet","chain":"ETH","network":"regtest","baseUrl":"http://localhost:3000/api","encryptionKey":"7eab496f0253b5395844356faa629b48ff063fb4186c51e8e448ee7db0eeb9b24afd041bad14c7153dd35a2755b5438f","authKey":{"bn":"1a0202578666668324a496bd06b9bbf1c197ec5a6b2f0c3ccd6470abbc5328a4","compressed":true,"network":"livenet"},"authPubKey":"03a7107731216187960c8b7436e3cdc5d4d1b7004e21d85907a402838b5702a118","masterKey":"e8daacd89b584a43c5e14b9baba7b94c358d4e271fa6eaa3fd140bebc7507a2f80e88a460042a785adf07660b5126fb05016416fa1544c5f20f1cb9a9bb25c4cca198b9162075e902143b5dfebfcb0e88c305fc08ab0f672f5cdfaeb0ee501a4ba5a1d22f586f69ab4625a719b1b9a4ce5f217574417892fc8748b08db5a4b76938e470e768054e3758071e6de291c66d372c30613a02d0c2a63647bbfa4a305f094138d728e78b6f7f05017bc0c0c8bd390f9837e56fa463f6edeb4761fe645c53c482cae062af7fe4a01c5045fee2fbcc93b56e4362d9f0b9aaa4bf4352e3abd430cd967747fc68c2515aa088a6b5f34751a2cfaa34ef02eca74d5d7aaca2d5ed109348d2e85a22ae08a665543649279b405dac3e8879e0e1f5a784e6af1aaa5b1253bca30cbb2197346e9ce13ff358963cce0f11f46c798dc25ebfda9d920e374c785ae8983b30f661ba9b26806def02553d85533225b394847269861c310dc8c19914348219ff2d59b8d9e7ed22ee79c7552353b22eebe7e6a568e04371071932c1f5331009455b0e7944bcfa5d9e685a44f637aa53d9eafc7d3b62eafdee70543732d5d646f60108fd2c0bdef36","password":"$2b$10$EgUUeV0zld1TOQvjMOmmPuyWQxF9hwpCBe1G4UiaAW1XYNY5/wMDm","xPubKey":"tpubDDT9XiD9hNQpHjpCizEvPq2TqDwZYwtKKiKHJuC5AAV2vbfooRVg2Mi7WogY8ygCTve5NgAjDXWEo3JF6sPayayYM2kNL3sQMeb6bDbv1Lk","pubKey":"0332f26acf0584acb762eb99e638190e6ea1ddbae6bb59f79d8611d2412a50791d","tokens":[],"storageType":"Level","addressType":"pubkeyhash","addressZero":"0xAf4a2A1c3cc0cbE9FF17B04dD91692F7686696Af","client":{"apiUrl":"http://localhost:3000/api/ETH/regtest","authKey":{"bn":"1a0202578666668324a496bd06b9bbf1c197ec5a6b2f0c3ccd6470abbc5328a4","compressed":true,"network":"livenet"}},"addressIndex":2,"lite":false}',
|
|
39
|
+
localAddresses: [
|
|
40
|
+
{
|
|
41
|
+
address: '0x021e018Ae71D1A9136cf15B3aF4be9E023EE8A70',
|
|
42
|
+
pubKey: '02ed66479c577e81ba397fa4498934172a8f6502b836a0d7ba340032a940cfcfd1',
|
|
43
|
+
path: 'm/0/1'
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
address: '0xAf4a2A1c3cc0cbE9FF17B04dD91692F7686696Af',
|
|
47
|
+
pubKey: '030cd384611d36e243a4cdf9cca229dbc62bef0dfb11abe4a928cf8576b4bc79f7',
|
|
48
|
+
path: 'm/0/0'
|
|
49
|
+
}
|
|
50
|
+
],
|
|
51
|
+
addresses: ['0x021e018Ae71D1A9136cf15B3aF4be9E023EE8A70', '0xAf4a2A1c3cc0cbE9FF17B04dD91692F7686696Af'],
|
|
52
|
+
storedKeys: [
|
|
53
|
+
{
|
|
54
|
+
encKey:
|
|
55
|
+
'788e8e1d3f1b263307f2e28c76e8a842f38e162db17d02e136b333228044e0f9b16b10ce6d19c035e727826419b2707728647feaf92aeb63890941bcec2c7d1e46e655e5ad4c76e0f1dd7935e7a0b1f77a5fa28a00719b635d4744790e1c5d63857d57e83fc66a7073659e302e710b625cedfc9f35b9b78dc8e032e6f70db55f672699d388ce957a59266bfcdb29e4b78267854698c730d295686a513a66773a9395d4edf6e9acd4815e58125479adb4cf271114add96df038de5e754b786c4db0e22bb5411e46f870c7f699d0d9ee10644c0bbe29b003641945390d10b6322a2859c8dfff3312daeedc87fbbf64cdcd',
|
|
56
|
+
pubKey: '02ed66479c577e81ba397fa4498934172a8f6502b836a0d7ba340032a940cfcfd1',
|
|
57
|
+
path: 'm/0/1'
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
encKey:
|
|
61
|
+
'c416e891b133305bd5294cd079be851bf75f5a4c2a0b04e3e09f0ce91f0b2175a61e9e451f48af3202314886006bc9d378741c82987189abe8f01f187df34bdde9519dac08d8d327b6ccd825b70eda155f316d581bb339e1d8b2311ae8f5666de1716ce8a3d1400550d070b641d684bf04906c2b9c573af64c02baf7ccb83f7997a9295e075d314708c06106a8544bab5a7d6326a00b85bf4b961e7492adfcecd84d41ffe2e95c2b4475a500a2d27ef6368ce8998974cc46e953b93a52f25b629d24ae61c719e1c5cdb6a6848bcf4d0e8770d1df2a47e12a113409e158719aded23ccafc42a8f95b63029b735f76d3b6',
|
|
62
|
+
pubKey: '030cd384611d36e243a4cdf9cca229dbc62bef0dfb11abe4a928cf8576b4bc79f7',
|
|
63
|
+
path: 'm/0/0'
|
|
64
|
+
}
|
|
65
|
+
]
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
export default ethMigrationTestWalletFixture;
|