Skip to content

Commit 4e665ed

Browse files
committed
feat: use decryptAsync/encryptAsync for remaining WRW flows
Ticket: WCN-172
1 parent 37cde79 commit 4e665ed

16 files changed

Lines changed: 140 additions & 53 deletions

File tree

modules/abstract-cosmos/src/cosmosCoin.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ export class CosmosCoin<CustomMessage = never> extends BaseCoin {
243243
throw new Error('Invalid key format');
244244
}
245245

246-
return await ECDSAUtils.getMpcV2RecoveryKeyShares(userKey, backupKey, walletPassphrase);
246+
return await ECDSAUtils.getMpcV2RecoveryKeyShares(userKey, backupKey, walletPassphrase, this.bitgo);
247247
}
248248

249249
/**
@@ -491,7 +491,8 @@ export class CosmosCoin<CustomMessage = never> extends BaseCoin {
491491
const { userKeyShare, backupKeyShare, commonKeyChain } = await ECDSAUtils.getMpcV2RecoveryKeyShares(
492492
userKey,
493493
backupKey,
494-
params.walletPassphrase
494+
params.walletPassphrase,
495+
this.bitgo
495496
); // baseAddress is not extracted
496497

497498
const MPC = new Ecdsa();

modules/abstract-eth/src/abstractEthLikeNewCoins.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2250,7 +2250,8 @@ export abstract class AbstractEthLikeNewCoins extends AbstractEthLikeCoin {
22502250
const { userKeyShare, backupKeyShare, commonKeyChain } = await ECDSAUtils.getMpcV2RecoveryKeyShares(
22512251
userPublicOrPrivateKeyShare,
22522252
backupPrivateOrPublicKeyShare,
2253-
params.walletPassphrase
2253+
params.walletPassphrase,
2254+
this.bitgo
22542255
);
22552256

22562257
const { gasLimit, gasPrice } = await this.getGasValues(params);

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/recovery/crossChainRecovery.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { BIP32, CoinName, fixedScriptWallet, address as wasmAddress } from '@bitgo/wasm-utxo';
2+
import { decryptAsync } from '@bitgo/sdk-api';
23
import { BitGoBase, IWallet, Keychain, Triple, Wallet } from '@bitgo/sdk-core';
3-
import { decrypt } from '@bitgo/sdk-api';
44

55
import { AbstractUtxoCoin, TransactionInfo } from '../abstractUtxoCoin';
66
import { signAndVerifyPsbt } from '../transaction/fixedScript/signTransaction';
@@ -313,7 +313,7 @@ async function getPrv(xprv?: string, passphrase?: string, wallet?: IWallet | Wal
313313
encryptedPrv = (await (wallet as WalletV1).getEncryptedUserKeychain()).encryptedXprv;
314314
}
315315

316-
return getPrv(decrypt(passphrase, encryptedPrv));
316+
return getPrv(await decryptAsync(passphrase, encryptedPrv));
317317
}
318318

319319
/**

modules/sdk-api/src/v1/wallet.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1751,7 +1751,10 @@ Wallet.prototype.createAndSignTransaction = function (params, callback) {
17511751
const safeUserKey = _.get(this.wallet, 'private.userPrivKey');
17521752
if (_.isString(safeUserKey) && _.isString(params.walletPassphrase)) {
17531753
// @ts-expect-error - no implicit this
1754-
transaction.signingKey = this.bitgo.decrypt({ password: params.walletPassphrase, input: safeUserKey });
1754+
transaction.signingKey = await this.bitgo.decryptAsync({
1755+
password: params.walletPassphrase,
1756+
input: safeUserKey,
1757+
});
17551758
} else {
17561759
throw e;
17571760
}
@@ -1809,10 +1812,13 @@ Wallet.prototype.getAndPrepareSigningKeychain = function (params, callback) {
18091812

18101813
// Caller provided a wallet passphrase
18111814
if (params.walletPassphrase) {
1812-
return self.getEncryptedUserKeychain().then(function (keychain) {
1815+
return self.getEncryptedUserKeychain().then(async function (keychain) {
18131816
// Decrypt the user key with a passphrase
18141817
try {
1815-
keychain.xprv = self.bitgo.decrypt({ password: params.walletPassphrase, input: keychain.encryptedXprv });
1818+
keychain.xprv = await self.bitgo.decryptAsync({
1819+
password: params.walletPassphrase,
1820+
input: keychain.encryptedXprv,
1821+
});
18161822
} catch (e) {
18171823
throw new Error('Unable to decrypt user keychain');
18181824
}
@@ -2295,21 +2301,24 @@ Wallet.prototype.shareWallet = function (params, callback) {
22952301
sharing = result;
22962302

22972303
if (needsKeychain) {
2298-
return self.getEncryptedUserKeychain({}).then(function (keychain) {
2304+
return self.getEncryptedUserKeychain({}).then(async function (keychain) {
22992305
// Decrypt the user key with a passphrase
23002306
if (keychain.encryptedXprv) {
23012307
if (!params.walletPassphrase) {
23022308
throw new Error('Missing walletPassphrase argument');
23032309
}
23042310
try {
2305-
keychain.xprv = self.bitgo.decrypt({ password: params.walletPassphrase, input: keychain.encryptedXprv });
2311+
keychain.xprv = await self.bitgo.decryptAsync({
2312+
password: params.walletPassphrase,
2313+
input: keychain.encryptedXprv,
2314+
});
23062315
} catch (e) {
23072316
throw new Error('Unable to decrypt user keychain');
23082317
}
23092318

23102319
const eckey = makeRandomKey();
23112320
const secret = getSharedSecret(eckey, Buffer.from(sharing.pubkey, 'hex')).toString('hex');
2312-
const newEncryptedXprv = self.bitgo.encrypt({ password: secret, input: keychain.xprv });
2321+
const newEncryptedXprv = await self.bitgo.encryptAsync({ password: secret, input: keychain.xprv });
23132322

23142323
sharedKeychain = {
23152324
xpub: keychain.xpub,
@@ -2599,10 +2608,10 @@ Wallet.prototype.recover = async function (params) {
25992608
assert(parsedUnsignedTx.outs.length === 1);
26002609
assert(_.sumBy(params.unspents, 'value') - _.sumBy(parsedUnsignedTx.outs, 'value') === Number(approximateTxFee));
26012610

2602-
const plainUserKey = this.bitgo.decrypt({ password: params.walletPassphrase, input: params.userKey });
2611+
const plainUserKey = await this.bitgo.decryptAsync({ password: params.walletPassphrase, input: params.userKey });
26032612
const halfSignedTx = await this.signTransaction({ ...unsignedTx, signingKey: plainUserKey });
26042613

2605-
const plainBackupKey = this.bitgo.decrypt({ password: params.walletPassphrase, input: params.backupKey });
2614+
const plainBackupKey = await this.bitgo.decryptAsync({ password: params.walletPassphrase, input: params.backupKey });
26062615
const fullSignedTx = await this.signTransaction({
26072616
...unsignedTx,
26082617
transactionHex: halfSignedTx.tx,
@@ -2659,7 +2668,7 @@ Wallet.prototype.sweep = async function (params) {
26592668
assert(parsedUnsignedTx.outs.length === 1);
26602669
assert(_.sumBy(params.unspents, 'value') - _.sumBy(parsedUnsignedTx.outs, 'value') === Number(approximateTxFee));
26612670

2662-
const plainUserKey = this.bitgo.decrypt({ password: params.walletPassphrase, input: params.userKey });
2671+
const plainUserKey = await this.bitgo.decryptAsync({ password: params.walletPassphrase, input: params.userKey });
26632672
const halfSignedTx = await this.signTransaction({ ...unsignedTx, signingKey: plainUserKey });
26642673

26652674
return await this.sendTransaction({

modules/sdk-coin-eos/src/eos.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
BitGoBase,
2121
checkKrsProvider,
2222
Environments,
23-
getBip32Keys,
23+
getBip32KeysAsync,
2424
getIsKrsRecovery,
2525
getIsUnsignedSweep,
2626
HalfSignedAccountTransaction as BaseHalfSignedTransaction,
@@ -905,7 +905,7 @@ export class Eos extends BaseCoin {
905905
throw new Error('Invalid destination address!');
906906
}
907907

908-
const keys = getBip32Keys(this.bitgo, params, { requireBitGoXpub: false });
908+
const keys = await getBip32KeysAsync(this.bitgo, params, { requireBitGoXpub: false });
909909

910910
const rootAddressDetails = this.getAddressDetails(params.rootAddress);
911911
const account = await this.getAccountFromNode({ address: rootAddressDetails.address });

modules/sdk-coin-icp/src/icp.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,8 @@ export class Icp extends BaseCoin {
409409
({ userKeyShare, backupKeyShare, commonKeyChain } = await ECDSAUtils.getMpcV2RecoveryKeyShares(
410410
userKey,
411411
backupKey,
412-
params.walletPassphrase
412+
params.walletPassphrase,
413+
this.bitgo
413414
));
414415
publicKey = MPC.deriveUnhardened(commonKeyChain, ROOT_PATH).slice(0, 66);
415416
} else {

modules/sdk-coin-rune/src/rune.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ export class Rune extends CosmosCoin {
167167
const { userKeyShare, backupKeyShare, commonKeyChain } = await ECDSAUtils.getMpcV2RecoveryKeyShares(
168168
userKey,
169169
backupKey,
170-
params.walletPassphrase
170+
params.walletPassphrase,
171+
this.bitgo
171172
); // baseAddress is not extracted
172173
// Step 3: Instantiate the ECDSA signer and fetch the address details
173174
const MPC = new Ecdsa();

modules/sdk-coin-stx/src/stx.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
BaseTransaction,
55
BitGoBase,
66
Environments,
7-
getBip32Keys,
7+
getBip32KeysAsync,
88
getIsUnsignedSweep,
99
KeyPair,
1010
MethodNotImplementedError,
@@ -701,7 +701,7 @@ export class Stx extends BaseCoin {
701701
}
702702
}
703703
const isUnsignedSweep = getIsUnsignedSweep(params);
704-
const keys = getBip32Keys(this.bitgo, params, { requireBitGoXpub: true });
704+
const keys = await getBip32KeysAsync(this.bitgo, params, { requireBitGoXpub: true });
705705
const rootAddressDetails = getAddressDetails(params.rootAddress);
706706
const [accountBalanceData, accountNonceData] = await Promise.all([
707707
this.getNativeStxBalanceFromNode({ address: rootAddressDetails.address }),

modules/sdk-coin-tempo/src/tempo.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,8 @@ export class Tempo extends AbstractEthLikeNewCoins {
492492
const { userKeyShare, backupKeyShare, commonKeyChain } = await ECDSAUtils.getMpcV2RecoveryKeyShares(
493493
userKey,
494494
backupKey,
495-
params.walletPassphrase
495+
params.walletPassphrase,
496+
this.bitgo
496497
);
497498

498499
const MPC = new Ecdsa();

0 commit comments

Comments
 (0)