Skip to content

Commit 11d950c

Browse files
committed
feat: migrate remaining v1 encrypt callsites to async
Ticket: WCN-284
1 parent 676e584 commit 11d950c

29 files changed

Lines changed: 596 additions & 241 deletions

File tree

modules/abstract-utxo/src/abstractUtxoCoin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -699,8 +699,8 @@ export abstract class AbstractUtxoCoin
699699
/**
700700
* @deprecated - use function verifyUserPublicKey instead
701701
*/
702-
protected verifyUserPublicKey(params: VerifyUserPublicKeyOptions): boolean {
703-
return verifyUserPublicKey(this.bitgo, params);
702+
protected async verifyUserPublicKey(params: VerifyUserPublicKeyOptions): Promise<boolean> {
703+
return await verifyUserPublicKey(this.bitgo, params);
704704
}
705705

706706
/**

modules/abstract-utxo/src/recovery/backupKeyRecovery.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
BitGoBase,
44
ErrorNoInputToRecover,
55
getKrsProvider,
6-
getBip32Keys as getBip32KeysFromSdkCore,
6+
getBip32KeysAsync as getBip32KeysFromSdkCore,
77
isTriple,
88
krsProviders,
99
Triple,
@@ -410,8 +410,8 @@ export function formatBackupKeyRecoveryResult(
410410
return txInfo;
411411
}
412412

413-
function getBip32Keys(bitgo: BitGoBase, params: RecoverParams): Triple<BIP32> {
414-
const keys = getBip32KeysFromSdkCore(bitgo, params, { requireBitGoXpub: true });
413+
async function getBip32Keys(bitgo: BitGoBase, params: RecoverParams): Promise<Triple<BIP32>> {
414+
const keys = await getBip32KeysFromSdkCore(bitgo, params, { requireBitGoXpub: true });
415415
if (!isTriple(keys)) {
416416
throw new Error(`expected key triple`);
417417
}
@@ -469,7 +469,7 @@ export async function backupKeyRecovery(
469469
}
470470

471471
// check whether key material and password authenticate the users and return parent keys of all three keys of the wallet
472-
const keys = getBip32Keys(bitgo, params);
472+
const keys = await getBip32Keys(bitgo, params);
473473
const walletKeys = fixedScriptWallet.RootWalletKeys.from({
474474
triple: keys,
475475
derivationPrefixes: [params.userKeyPath || 'm/0/0', 'm/0/0', 'm/0/0'],

modules/abstract-utxo/src/transaction/fixedScript/verifyTransaction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export async function verifyTransaction<TNumber extends bigint | number>(
7979
let userPublicKeyVerified = false;
8080
try {
8181
// verify the user public key matches the private key - this will throw if there is no match
82-
userPublicKeyVerified = verifyUserPublicKey(bitgo, {
82+
userPublicKeyVerified = await verifyUserPublicKey(bitgo, {
8383
userKeychain: keychains.user,
8484
disableNetworking,
8585
txParams,

modules/abstract-utxo/src/verifyKey.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import assert from 'assert';
77

88
import buildDebug from 'debug';
99
import { BIP32, message } from '@bitgo/wasm-utxo';
10-
import { BitGoBase, decryptKeychainPrivateKey, KeyIndices } from '@bitgo/sdk-core';
10+
import { BitGoBase, decryptKeychainPrivateKeyAsync, KeyIndices } from '@bitgo/sdk-core';
1111

1212
import { VerifyKeySignaturesOptions, VerifyUserPublicKeyOptions } from './abstractUtxoCoin';
1313
import { ParsedTransaction } from './transaction/types';
@@ -84,7 +84,7 @@ export function verifyCustomChangeKeySignatures<TNumber extends number | bigint>
8484
/**
8585
* Decrypt the wallet's user private key and verify that the claimed public key matches
8686
*/
87-
export function verifyUserPublicKey(bitgo: BitGoBase, params: VerifyUserPublicKeyOptions): boolean {
87+
export async function verifyUserPublicKey(bitgo: BitGoBase, params: VerifyUserPublicKeyOptions): Promise<boolean> {
8888
const { userKeychain, txParams, disableNetworking } = params;
8989
if (!userKeychain) {
9090
throw new Error('user keychain is required');
@@ -94,7 +94,7 @@ export function verifyUserPublicKey(bitgo: BitGoBase, params: VerifyUserPublicKe
9494

9595
let userPrv = userKeychain.prv;
9696
if (!userPrv && txParams.walletPassphrase) {
97-
userPrv = decryptKeychainPrivateKey(bitgo, userKeychain, txParams.walletPassphrase);
97+
userPrv = await decryptKeychainPrivateKeyAsync(bitgo, userKeychain, txParams.walletPassphrase);
9898
}
9999

100100
if (!userPrv) {

modules/bitgo/test/unit/bitgo.ts

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -237,41 +237,44 @@ describe('BitGo Prototype Methods', function () {
237237
'xpub661MyMwAqRbcEusRjkJ64BXgR8ddYsXbuDJfbRc3eZcZVEa2ygswDiFZQpHFsA5N211YDvi2N898h4KrcXcfsR8PLhjJaPUwCUqg1ptBBHN';
238238
const passwords = ['mickey', 'mouse', 'donald', 'duck'];
239239

240-
it('should fail to split secret with wrong m', () => {
241-
(() =>
242-
bitgo.splitSecret({
240+
it('should fail to split secret with wrong m', async () => {
241+
await bitgo
242+
.splitSecret({
243243
seed,
244244
passwords: ['abc'],
245245
m: 0,
246-
})).should.throw('m must be a positive integer greater than or equal to 2');
246+
})
247+
.should.be.rejectedWith('m must be a positive integer greater than or equal to 2');
247248
});
248249

249-
it('should fail to split secret with bad password count', () => {
250-
(() =>
251-
bitgo.splitSecret({
250+
it('should fail to split secret with bad password count', async () => {
251+
await bitgo
252+
.splitSecret({
252253
seed,
253254
passwords: ['abc'],
254255
m: 2,
255-
})).should.throw('passwords array length cannot be less than m');
256+
})
257+
.should.be.rejectedWith('passwords array length cannot be less than m');
256258
});
257259

258-
it('should split and fail to reconstitute secret with bad passwords', () => {
259-
const splitSecret = bitgo.splitSecret({ seed, passwords: passwords, m: 3 });
260+
it('should split and fail to reconstitute secret with bad passwords', async () => {
261+
const splitSecret = await bitgo.splitSecret({ seed, passwords: passwords, m: 3 });
260262
const shards = _.at(splitSecret.seedShares, [0, 2]);
261263
const subsetPasswords = _.at(passwords, [0, 3]);
262-
(() =>
263-
bitgo.reconstituteSecret({
264+
await bitgo
265+
.reconstituteSecret({
264266
shards,
265267
passwords: subsetPasswords,
266268
xpub,
267-
} as any)).should.throw(/ccm: tag doesn't match/);
269+
} as any)
270+
.should.be.rejectedWith(/ccm: tag doesn't match/);
268271
});
269272

270-
it('should split and reconstitute secret', () => {
271-
const splitSecret = bitgo.splitSecret({ seed, passwords: passwords, m: 2 });
273+
it('should split and reconstitute secret', async () => {
274+
const splitSecret = await bitgo.splitSecret({ seed, passwords: passwords, m: 2 });
272275
const shards = _.at(splitSecret.seedShares, [0, 2]);
273276
const subsetPasswords = _.at(passwords, [0, 2]);
274-
const reconstitutedSeed = bitgo.reconstituteSecret({ shards, passwords: subsetPasswords });
277+
const reconstitutedSeed = await bitgo.reconstituteSecret({ shards, passwords: subsetPasswords });
275278
reconstitutedSeed.seed.should.equal(seed);
276279
reconstitutedSeed.xpub.should.equal(
277280
'xpub661MyMwAqRbcEusRjkJ64BXgR8ddYsXbuDJfbRc3eZcZVEa2ygswDiFZQpHFsA5N211YDvi2N898h4KrcXcfsR8PLhjJaPUwCUqg1ptBBHN'
@@ -281,22 +284,22 @@ describe('BitGo Prototype Methods', function () {
281284
);
282285
});
283286

284-
it('should split and incorrectly verify secret', () => {
285-
const splitSecret = bitgo.splitSecret({ seed, passwords: passwords, m: 3 });
286-
const isValid = bitgo.verifyShards({ shards: splitSecret.seedShares, passwords, m: 2 } as any);
287+
it('should split and incorrectly verify secret', async () => {
288+
const splitSecret = await bitgo.splitSecret({ seed, passwords: passwords, m: 3 });
289+
const isValid = await bitgo.verifyShards({ shards: splitSecret.seedShares, passwords, m: 2 } as any);
287290
isValid.should.equal(false);
288291
});
289292

290-
it('should split and verify secret', () => {
291-
const splitSecret = bitgo.splitSecret({ seed, passwords: passwords, m: 2 });
292-
const isValid = bitgo.verifyShards({ shards: splitSecret.seedShares, passwords, m: 2, xpub });
293+
it('should split and verify secret', async () => {
294+
const splitSecret = await bitgo.splitSecret({ seed, passwords: passwords, m: 2 });
295+
const isValid = await bitgo.verifyShards({ shards: splitSecret.seedShares, passwords, m: 2, xpub });
293296
isValid.should.equal(true);
294297
});
295298

296-
it('should split and verify secret with many parts', () => {
299+
it('should split and verify secret with many parts', async () => {
297300
const allPws = ['0', '1', '2', '3', '4', '5', '6', '7'];
298-
const splitSecret = bitgo.splitSecret({ seed, passwords: allPws, m: 3 });
299-
const isValid = bitgo.verifyShards({ shards: splitSecret.seedShares, passwords: allPws, m: 3, xpub });
301+
const splitSecret = await bitgo.splitSecret({ seed, passwords: allPws, m: 3 });
302+
const isValid = await bitgo.verifyShards({ shards: splitSecret.seedShares, passwords: allPws, m: 3, xpub });
300303
isValid.should.equal(true);
301304
});
302305
});
@@ -436,15 +439,15 @@ describe('BitGo Prototype Methods', function () {
436439
requestHeaders.hmac.should.equal('6de77d5a5446a3e5649456c11480706a71071b15639c3c787af65bdb02ecf1ec');
437440
});
438441

439-
it('should correctly handle authentication response', () => {
442+
it('should correctly handle authentication response', async () => {
440443
const responseJson = {
441444
encryptedToken:
442445
'{"iv":"EqxVaGTLY4naAYkuBaTz0w==","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"4S4dBYcgL4s=","ct":"FgBRJljb8iSYxnAjMi4Qotr7sTKbSmWnlfHZShMSi8YeeE3kiS8bpHNUwAPhY8tgouh3UsEwrJnY+54MvqFD7yd19pG1V4CVssr8"}',
443446
derivationPath: 'm/999999/104490948/173846667',
444447
encryptedECDHXprv:
445448
'{"iv":"QKHEF2GNcwOJwy6+pwANRA==","v":1,"iter":10000,"ks":256,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"W2sVFvXDlOw=","ct":"8BTCqS25X37kLzmzQdGenhXH6znn9qEmkszAeS8kLnRdqKSiUiC7bTAVgg/Np5yrV7F7Jyiq+MTpVT76EoUT+PMJzArv0gUQKC2JPB3JuVKeAAVWBQmhWfkEwRfyv4hq4WMxwZtocwBqThvd2pJm9HE51GX4/Wo="}',
446449
};
447-
const parsedAuthenticationData = bitgo.handleTokenIssuance(responseJson, 'test@bitgo.com');
450+
const parsedAuthenticationData = await bitgo.handleTokenIssuance(responseJson, 'test@bitgo.com');
448451
parsedAuthenticationData.token.should.equal(token);
449452
parsedAuthenticationData.ecdhXprv.should.equal(
450453
'xprv9s21ZrQH143K3si1bKGp7KqgCQv39ttQ7aUwWzVdytgHd8HtDCHyEp14mxfhiT3qHTq4BaSrA7uUkG6AJTfPJBsRu63drvBqYuMZyTxepH7'

modules/bitgo/test/v2/unit/keychains.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,7 +1076,7 @@ describe('V2 Keychains', function () {
10761076
updateKeychainStub = sandbox.stub().returns({ result: sandbox.stub().resolves() });
10771077
sandbox.stub(BitGo.prototype, 'put').returns({ send: updateKeychainStub });
10781078
createKeypairStub = sandbox.stub(ofcKeychains, 'create').returns(mockNewKeypair);
1079-
encryptionStub = sandbox.stub(BitGo.prototype, 'encrypt').returns('newEncryptedPrv');
1079+
encryptionStub = sandbox.stub(BitGo.prototype, 'encryptAsync').resolves('newEncryptedPrv');
10801080
});
10811081

10821082
afterEach(function () {
@@ -1088,7 +1088,7 @@ describe('V2 Keychains', function () {
10881088

10891089
await ofcKeychains.rotateKeychain({ id: mockOfcKeychain.id, password: '1234' });
10901090
sinon.assert.called(createKeypairStub);
1091-
sinon.assert.calledWith(encryptionStub, { input: mockNewKeypair.prv, password: '1234' });
1091+
sinon.assert.calledWith(encryptionStub, { input: mockNewKeypair.prv, password: '1234', encryptionVersion: 2 });
10921092
sinon.assert.calledWith(updateKeychainStub, {
10931093
pub: mockNewKeypair.pub,
10941094
encryptedPrv: 'newEncryptedPrv',

modules/bitgo/test/v2/unit/wallets.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4273,7 +4273,7 @@ describe('V2 Wallets:', function () {
42734273

42744274
const encryptPrvForUserStub = sinon
42754275
.stub(wallet, 'encryptPrvForUser')
4276-
.callsFake((prv, pubKey, userPubKey, path) => {
4276+
.callsFake(async (prv, pubKey, userPubKey, path) => {
42774277
return {
42784278
pub: pubKey,
42794279
encryptedPrv: 'dummyEncryptedPrv',

modules/passkey-crypto/src/attachPasskeyToWallet.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export async function attachPasskeyToWallet(params: {
6666
}
6767

6868
const prfPassword = derivePassword(authResult.prfResult);
69-
const encryptedPrv = await bitgo.encryptAsync({ password: prfPassword, input: privateKey, encryptionVersion: 2 });
69+
const encryptedPrv = await bitgo.encryptAsync({ password: prfPassword, input: privateKey });
7070

7171
const updatedKeychain = await bitgo
7272
.put(bitgo.url(`/${coin}/key/${keychainId}`, 2))

0 commit comments

Comments
 (0)