Skip to content

chore: parallellize keychain and queryPromise #6136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions modules/bitgo/test/v2/unit/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -728,12 +728,12 @@ describe('V2 Wallet:', function () {
customSigningFunction,
});
} catch (e) {
e.message.should.not.equal(errorMessage);
e.message.should.equal(errorMessage);
}
try {
await ethWallet.sendMany({ ...sendManyParamsCorrectPassPhrase });
} catch (e) {
e.message.should.not.equal(errorMessage);
e.message.should.equal(errorMessage);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did we change this test?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After making validation and prebuildQuery parallel, it would give a consistent error message unable to decrypt keychain with the given wallet passphrase. This test explicitly checked for error message to be different

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the error message changes it might break client's code - I don't think we should merge this until we can fix this

}
nockKeychain.isDone().should.be.true();
});
Expand Down
24 changes: 19 additions & 5 deletions modules/sdk-core/src/bitgo/wallet/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2176,7 +2176,7 @@ export class Wallet implements IWallet {
}

// Doing a sanity check for password here to avoid doing further work if we know it's wrong
const keychains = await this.getKeychainsAndValidatePassphrase({
const keychainPromise = this.getKeychainsAndValidatePassphrase({
reqId: params.reqId,
walletPassphrase: params.walletPassphrase,
customSigningFunction: params.customSigningFunction,
Expand All @@ -2192,10 +2192,24 @@ export class Wallet implements IWallet {
} else {
txPrebuildQuery = params.prebuildTx ? Promise.resolve(params.prebuildTx) : this.prebuildTransaction(params);
}

// the prebuild can be overridden by providing an explicit tx
const txPrebuild = (await txPrebuildQuery) as PrebuildTransactionResult;

let keychains: Keychain[];
let txPrebuild: PrebuildTransactionResult;
try {
[keychains, txPrebuild] = (await Promise.all([keychainPromise, txPrebuildQuery])) as [
Keychain[],
PrebuildTransactionResult
];
} catch (err) {
if (err !== null || (err instanceof Error && err.message.includes('unable to decrypt keychain'))) {
const error: Error & { code?: string } = new Error(
`unable to decrypt keychain with the given wallet passphrase`
);
error.code = 'wallet_passphrase_incorrect';
throw error;
} else {
throw new Error(`Failed to process transaction: ${err.message}`);
}
}
try {
await this.baseCoin.verifyTransaction({
txParams: { ...txPrebuild.buildParams, ...params },
Expand Down