Skip to content
Merged
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
9 changes: 7 additions & 2 deletions modules/sdk-api/src/bitgoAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1878,11 +1878,16 @@ export class BitGoAPI implements BitGoBase {
/**
* Gets the user's private ECDH keychain
*/
async getECDHKeychain(ecdhKeychainPub?: string): Promise<any> {
async getECDHKeychain(ecdhKeychainPub?: string, options: { allowMissing?: boolean } = {}): Promise<any | undefined> {
if (!ecdhKeychainPub) {
const result = await this.get(this.url('/user/settings')).result();
if (!result.settings.ecdhKeychain) {
return new Error('ecdh keychain not found for user');
if (options.allowMissing) {
return undefined;
}
throw new Error(
'Your account is missing the sharing key needed for this action. Please contact support@bitgo.com so it can be set up.'
);
}
ecdhKeychainPub = result.settings.ecdhKeychain;
}
Expand Down
32 changes: 32 additions & 0 deletions modules/sdk-api/test/unit/bitgoAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1562,3 +1562,35 @@ describe('wallets() v1 facade', function () {
wallets.resendShareInvite.should.be.a.Function();
});
});

describe('getECDHKeychain', function () {
const ROOT = 'https://app.example.local';

afterEach(function () {
nock.cleanAll();
});

it('rejects when the user has no ecdhKeychain in settings', async function () {
const bitgo = new BitGoAPI({ env: 'custom', customRootURI: ROOT });
nock(ROOT)
.get('/api/v1/user/settings')
.reply(200, { settings: { ecdhKeychain: null } });

await bitgo
.getECDHKeychain()
.should.be.rejectedWith(
'Your account is missing the sharing key needed for this action. Please contact support@bitgo.com so it can be set up.'
);
});

it('returns undefined when the user has no ecdhKeychain and allowMissing is true', async function () {
const bitgo = new BitGoAPI({ env: 'custom', customRootURI: ROOT });
nock(ROOT)
.get('/api/v1/user/settings')
.reply(200, { settings: { ecdhKeychain: null } });

const keychain = await bitgo.getECDHKeychain(undefined, { allowMissing: true });

assert.strictEqual(keychain, undefined);
});
});
2 changes: 1 addition & 1 deletion modules/sdk-core/src/bitgo/bitgoBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export interface BitGoBase {
readonly env: EnvironmentName;
fetchConstants(): Promise<any>;
get(url: string): BitGoRequest;
getECDHKeychain(ecdhKeychainPub?: string): Promise<any>;
getECDHKeychain(ecdhKeychainPub?: string, options?: { allowMissing?: boolean }): Promise<any | undefined>;
getEcdhKeypairPrivate(password: string, entId?: string): Promise<EcdhDerivedKeypair>;
getEnv(): EnvironmentName;
getSharingKey({ email }: GetSharingKeyOptions): Promise<any>;
Expand Down
30 changes: 21 additions & 9 deletions modules/sdk-core/src/bitgo/wallet/wallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1358,15 +1358,23 @@ export class Wallets implements IWallets {
throw new Error('No valid wallet shares found to accept');
}

const sharingKeychain = await this.bitgo.getECDHKeychain();
if (_.isUndefined(sharingKeychain.encryptedXprv)) {
throw new Error('encryptedXprv was not found on sharing keychain');
}

sharingKeychain.prv = await this.bitgo.decrypt({
password: params.userLoginPassword,
input: sharingKeychain.encryptedXprv,
const hasSharesRequiringDecryption = walletShares.some(
(walletShare) => !walletShare.userMultiKeyRotationRequired && walletShare.keychain
);
const sharingKeychain = await this.bitgo.getECDHKeychain(undefined, {
allowMissing: !hasSharesRequiringDecryption,
});
let sharingKeychainPrv: string | undefined;
if (sharingKeychain) {
if (_.isUndefined(sharingKeychain.encryptedXprv)) {
throw new Error('encryptedXprv was not found on sharing keychain');
}

sharingKeychainPrv = await this.bitgo.decrypt({
password: params.userLoginPassword,
input: sharingKeychain.encryptedXprv,
});
}
Comment thread
claude[bot] marked this conversation as resolved.
Resolved
const newWalletPassphrase = params.newWalletPassphrase || params.userLoginPassword;
const webauthnInfo = params.webauthnInfo;

Expand Down Expand Up @@ -1409,8 +1417,12 @@ export class Wallets implements IWallets {
if (!walletShare.keychain) {
return [];
}
assert(
sharingKeychainPrv,
'Unable to accept this wallet share because your sharing key could not be loaded. Please try again, or contact support@bitgo.com if the problem continues.'
);
const secret = getSharedSecret(
bip32.fromBase58(sharingKeychain.prv).derivePath(sanitizeLegacyPath(walletShare.keychain.path)),
bip32.fromBase58(sharingKeychainPrv).derivePath(sanitizeLegacyPath(walletShare.keychain.path)),
Buffer.from(walletShare.keychain.fromPubKey, 'hex')
).toString('hex');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,70 @@ describe('Wallets - encryptionVersion threading', function () {
assert.strictEqual(call.args[1], undefined);
});

it('accepts a UMKR-only batch when the user has no ECDH keychain', async function () {
mockBitGo.getECDHKeychain.resolves(undefined);

await wallets.bulkAcceptShare({
walletShareIds: ['share-id'],
userLoginPassword: 'login-password',
});

assert.deepStrictEqual(mockBitGo.getECDHKeychain.firstCall.args, [undefined, { allowMissing: true }]);
assert.strictEqual(mockBitGo.decrypt.called, false);
assert.strictEqual(mockBitGo.put.called, true);
});

it('validates the login password against an existing ECDH keychain for a UMKR-only batch', async function () {
mockBitGo.decrypt.rejects(new Error('password error'));

await assert.rejects(
wallets.bulkAcceptShare({
walletShareIds: ['share-id'],
userLoginPassword: 'incorrect-password',
}),
{ message: 'password error' }
);

assert.deepStrictEqual(mockBitGo.getECDHKeychain.firstCall.args, [undefined, { allowMissing: true }]);
assert.deepStrictEqual(mockBitGo.decrypt.firstCall.args, [{ password: 'incorrect-password', input: 'encXprv' }]);
assert.strictEqual(mockBitGo.createEncryptionSession.called, false);
assert.strictEqual(mockKeychains.create.called, false);
assert.strictEqual(mockBitGo.put.called, false);
});

it('still requires the ECDH keychain when the batch also holds a share with a keychain', async function () {
mockBitGo.get.returns({
result: sinon.stub().resolves({
incoming: [
...walletSharesList.incoming,
{
id: 'keychain-share-id',
keychain: {
pub: 'pub',
toPubKey: 'toPubKey',
fromPubKey: 'fromPubKey',
encryptedPrv: 'prv',
path: 'm/0',
},
permissions: ['spend'],
},
],
outgoing: [],
}),
});
mockBitGo.getECDHKeychain.resolves({});

await assert.rejects(
wallets.bulkAcceptShare({
walletShareIds: ['share-id', 'keychain-share-id'],
userLoginPassword: 'login-password',
}),
{ message: 'encryptedXprv was not found on sharing keychain' }
);
assert.strictEqual(mockBitGo.getECDHKeychain.calledOnce, true);
assert.deepStrictEqual(mockBitGo.getECDHKeychain.firstCall.args, [undefined, { allowMissing: false }]);
});

it('processes shares in batches of 16 to avoid WASM memory exhaustion', async function () {
// 20 shares → 2 batches (16 + 4), verifying the batch boundary is crossed
const manyShares = Array.from({ length: 20 }, (_, i) => ({
Expand Down
Loading