Skip to content

Commit ee00443

Browse files
committed
feat: change error type for password error in bulkshareoption
this is done so that we can differentiate on UI what error message to show based on error instance. Ticket: CSI-445 TICKET: CSI-445
1 parent b475bdd commit ee00443

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
KeychainWithEncryptedPrv,
2626
WalletWithKeychains,
2727
multisigTypes,
28+
IncorrectPasswordError,
2829
} from '@bitgo/sdk-core';
2930
import { BitGo } from '../../../src';
3031
import { afterEach } from 'mocha';
@@ -2605,6 +2606,52 @@ describe('V2 Wallets:', function () {
26052606
],
26062607
});
26072608
});
2609+
2610+
it('should throw error in processing share options when wallet password is incorrect', async () => {
2611+
const userId = '[email protected]';
2612+
const permissions = ['view', 'spend'];
2613+
const path = 'm/999999/1/1';
2614+
const walletPassphrase = 'bitgo1234';
2615+
const pub = 'Zo1ggzTUKMY5bYnDvT5mtVeZxzf2FaLTbKkmvGUhUQk';
2616+
nock(bgUrl)
2617+
.get(`/api/v2/tbtc/key/${wallet.keyIds()[0]}`)
2618+
.reply(200, {
2619+
id: wallet.keyIds()[0],
2620+
pub,
2621+
source: 'user',
2622+
encryptedPrv: bitgo.encrypt({ input: 'xprv1', password: walletPassphrase }),
2623+
coinSpecific: {},
2624+
});
2625+
const params: BulkWalletShareOptions = {
2626+
walletPassphrase: 'wrong password',
2627+
keyShareOptions: [
2628+
{
2629+
userId: userId,
2630+
permissions: permissions,
2631+
pubKey: '02705a6d33a2459feb537e7abe36aaad8c11532cdbffa3a2e4e58868467d51f532',
2632+
path: path,
2633+
},
2634+
],
2635+
};
2636+
2637+
const prv1 = Math.random().toString();
2638+
const keychainTest: OptionalKeychainEncryptedKey = {
2639+
encryptedPrv: bitgo.encrypt({ input: prv1, password: walletPassphrase }),
2640+
};
2641+
2642+
sinon.stub(wallet, 'getEncryptedUserKeychain').resolves({
2643+
encryptedPrv: keychainTest.encryptedPrv,
2644+
pub,
2645+
} as KeychainWithEncryptedPrv);
2646+
2647+
try {
2648+
await wallet.createBulkWalletShare(params);
2649+
assert.fail('Expected error not thrown');
2650+
} catch (error) {
2651+
assert(error instanceof IncorrectPasswordError);
2652+
assert.equal(error.message, 'Password shared is incorrect for this wallet');
2653+
}
2654+
});
26082655
});
26092656

26102657
describe('List Wallets:', function () {

modules/sdk-core/src/bitgo/errors.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,12 @@ export class MissingEncryptedKeychainError extends Error {
163163
}
164164
}
165165

166+
export class IncorrectPasswordError extends Error {
167+
public constructor(message?: string) {
168+
super(message || 'Incorrect password');
169+
}
170+
}
171+
166172
export class ApiResponseError<ResponseBodyType = any> extends BitGoJsError {
167173
message: string;
168174
status: number;

modules/sdk-core/src/bitgo/wallet/wallet.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ import {
1818
import { makeRandomKey } from '../bitcoin';
1919
import { BitGoBase } from '../bitgoBase';
2020
import { getSharedSecret } from '../ecdh';
21-
import { AddressGenerationError, MethodNotImplementedError, MissingEncryptedKeychainError } from '../errors';
21+
import {
22+
AddressGenerationError,
23+
IncorrectPasswordError,
24+
MethodNotImplementedError,
25+
MissingEncryptedKeychainError,
26+
} from '../errors';
2227
import * as internal from '../internal/internal';
2328
import { drawKeycard } from '../internal';
2429
import { decryptKeychainPrivateKey, Keychain, KeychainWithEncryptedPrv } from '../keychain';
@@ -1685,7 +1690,7 @@ export class Wallet implements IWallet {
16851690

16861691
const userPrv = decryptKeychainPrivateKey(this.bitgo, keychain, walletPassphrase);
16871692
if (!userPrv) {
1688-
throw new Error('Unable to decrypt user keychain');
1693+
throw new IncorrectPasswordError('Password shared is incorrect for this wallet');
16891694
}
16901695

16911696
keychain.prv = userPrv;

0 commit comments

Comments
 (0)