Skip to content
Closed
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
22 changes: 20 additions & 2 deletions modules/sdk-api/src/bitgoAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ import {
DecryptOptions,
defaultConstants,
EcdhDerivedKeypair,
EcdhKeychain,
EncryptionVersion,
EncryptOptions,
EnvironmentName,
generateRandomPassword,
getAddressP2PKH,
getSharedSecret,
GetEcdhKeychainOptions,
GetSharingKeyOptions,
GetSigningKeyApi,
GlobalCoinFactory,
Expand Down Expand Up @@ -1876,9 +1878,22 @@ export class BitGoAPI implements BitGoBase {
}

/**
* Gets the user's private ECDH keychain
* Gets the user's private ECDH keychain.
* Returns undefined only when allowMissing is true and the user has no ECDH keychain.
*/
async getECDHKeychain(ecdhKeychainPub?: string, options: { allowMissing?: boolean } = {}): Promise<any | undefined> {
async getECDHKeychain(ecdhKeychainPub?: string, options?: { allowMissing?: false }): Promise<EcdhKeychain>;
async getECDHKeychain(
ecdhKeychainPub: string | undefined,
options: { allowMissing: true }
): Promise<EcdhKeychain | undefined>;
async getECDHKeychain(
ecdhKeychainPub: string | undefined,
options: GetEcdhKeychainOptions
): Promise<EcdhKeychain | undefined>;
async getECDHKeychain(
ecdhKeychainPub?: string,
options: GetEcdhKeychainOptions = {}
): Promise<EcdhKeychain | undefined> {
if (!ecdhKeychainPub) {
const result = await this.get(this.url('/user/settings')).result();
if (!result.settings.ecdhKeychain) {
Expand Down Expand Up @@ -1906,6 +1921,9 @@ export class BitGoAPI implements BitGoBase {
throw new Error('Something went wrong with the user keychain. Please contact support@bitgo.com.');
}
const userEcdhKeychain = await this.getECDHKeychain(userSigningKey.ecdhKeychain);
if (!userEcdhKeychain.encryptedXprv) {
throw new Error('Something went wrong with the user keychain. Please contact support@bitgo.com.');
}
let xprv;
try {
xprv = await this.decrypt({
Expand Down
20 changes: 20 additions & 0 deletions modules/sdk-api/test/unit/bitgoAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1593,4 +1593,24 @@ describe('getECDHKeychain', function () {

assert.strictEqual(keychain, undefined);
});

it('rejects signing-key derivation when the ECDH keychain has no encrypted private key', async function () {
const bitgo = new BitGoAPI({ env: 'custom', customRootURI: ROOT });
sinon.stub(bitgo, 'getSigningKeyForUser').resolves({
userId: 'user-id',
userEmail: 'user@example.com',
derivedPubkey: 'derived-pubkey',
derivationPath: 'm/0',
ecdhKeychain: 'xpub',
});
sinon.stub(bitgo, 'getECDHKeychain').resolves({ xpub: 'xpub' });
const decrypt = sinon.spy(bitgo, 'decrypt');

await assert.rejects(
bitgo.getEcdhKeypairPrivate('password', 'enterprise-id'),
/Something went wrong with the user keychain/
);

assert.strictEqual(decrypt.called, false);
});
});
18 changes: 16 additions & 2 deletions modules/sdk-core/src/bitgo/bitgoBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,20 @@ import { IBaseCoin } from './baseCoin';
import { CoinConstructor } from './coinFactory';
import { EnvironmentName } from './environments';
import { EcdhDerivedKeypair, GetSigningKeyApi } from './keychain';
import type { BitGoApiV1Wallets } from './v1Wallets';
import type { BitGoApiV1Wallets, EcdhKeychain } from './v1Wallets';

export interface GetEcdhKeychainOptions {
/** Return undefined instead of throwing when the user has no ECDH keychain. */
allowMissing?: boolean;
}

export type {
BitGoApiV1AddWalletParams,
BitGoApiV1BitGo,
BitGoApiV1Callback,
BitGoApiV1CreateForwardWalletParams,
BitGoApiV1CreateWalletWithKeychainsParams,
EcdhKeychain,
BitGoApiV1GetWalletParams,
BitGoApiV1Keychains,
BitGoApiV1ListWalletsParams,
Expand Down Expand Up @@ -48,7 +54,15 @@ export interface BitGoBase {
readonly env: EnvironmentName;
fetchConstants(): Promise<any>;
get(url: string): BitGoRequest;
getECDHKeychain(ecdhKeychainPub?: string, options?: { allowMissing?: boolean }): Promise<any | undefined>;
getECDHKeychain(ecdhKeychainPub?: string, options?: { allowMissing?: false }): Promise<EcdhKeychain>;
getECDHKeychain(
ecdhKeychainPub: string | undefined,
options: { allowMissing: true }
): Promise<EcdhKeychain | undefined>;
getECDHKeychain(
ecdhKeychainPub: string | undefined,
options: GetEcdhKeychainOptions
): Promise<EcdhKeychain | undefined>;
getEcdhKeypairPrivate(password: string, entId?: string): Promise<EcdhDerivedKeypair>;
getEnv(): EnvironmentName;
getSharingKey({ email }: GetSharingKeyOptions): Promise<any>;
Expand Down
10 changes: 9 additions & 1 deletion modules/sdk-core/src/bitgo/v1Wallets.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import type { BitGoRequest, DecryptOptions, EncryptionVersion, EncryptOptions } from '../api';

/** User-level Elliptic Curve Diffie-Hellman (ECDH) sharing key returned by the legacy keychain API. */
export type EcdhKeychain = {
encryptedXprv?: string;
xprv?: string;
xpub?: string;
[key: string]: unknown;
};

/**
* Minimal BitGo client surface used by the **v1** Wallets facade
* (`modules/sdk-api/src/v1/wallets.ts`).
Expand All @@ -18,7 +26,7 @@ export type BitGoApiV1BitGo = {
url(path: string, version?: number): string;
encrypt(params: EncryptOptions): Promise<string>;
decrypt(params: DecryptOptions): Promise<string>;
getECDHKeychain(): Promise<{ encryptedXprv?: string; xprv?: string }>;
getECDHKeychain(): Promise<EcdhKeychain>;
keychains(): BitGoApiV1Keychains;
};

Expand Down
4 changes: 4 additions & 0 deletions modules/sdk-core/src/bitgo/wallet/iWallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,10 @@ export interface AcceptShareOptions {

export interface BulkAcceptShareOptions {
walletShareIds: string[];
/**
* Decrypts an existing ECDH sharing key. For a passkey account accepting only user multi-key rotation shares,
* no login password or ECDH key exists; `newWalletPassphrase` or this value encrypts the fresh wallet key.
*/
userLoginPassword: string;
newWalletPassphrase?: string;
webauthnInfo?: WebauthnKeyEncryptionInfo;
Expand Down
14 changes: 7 additions & 7 deletions modules/sdk-core/src/bitgo/wallet/wallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1322,13 +1322,13 @@ export class Wallets implements IWallets {

/**
* Bulk Accept wallet shares, adding the wallets to the user's list
* Needs a user's password to decrypt the shared key
* Decrypts conventional shared keys or creates fresh user keys for multi-key rotation shares.
*
* @param params BulkAcceptShareOptions
* @param params.walletShareId - array of the wallet shares to accept
* @param params.userPassword - user's password to decrypt the shared wallet key
* @param params.walletShareIds - wallet shares to accept
* @param params.userLoginPassword - password used to decrypt an existing ECDH sharing key
* @param params.newWalletPassphrase - new wallet passphrase for saving the shared wallet prv.
* If left blank then the user's login password is used.
* If left blank then userLoginPassword is used.
*
*@returns {Promise<BulkAcceptShareResponse>}
*/
Expand Down Expand Up @@ -1361,9 +1361,9 @@ export class Wallets implements IWallets {
const hasSharesRequiringDecryption = walletShares.some(
(walletShare) => !walletShare.userMultiKeyRotationRequired && walletShare.keychain
);
const sharingKeychain = await this.bitgo.getECDHKeychain(undefined, {
allowMissing: !hasSharesRequiringDecryption,
});
const sharingKeychain = hasSharesRequiringDecryption
? await this.bitgo.getECDHKeychain(undefined, { allowMissing: false })
: await this.bitgo.getECDHKeychain(undefined, { allowMissing: true });
let sharingKeychainPrv: string | undefined;
if (sharingKeychain) {
if (_.isUndefined(sharingKeychain.encryptedXprv)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ 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 () {
it('accepts a multi-key rotation-only batch for a passkey account without an ECDH keychain', async function () {
mockBitGo.getECDHKeychain.resolves(undefined);

await wallets.bulkAcceptShare({
Expand Down
Loading