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
413 changes: 268 additions & 145 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"@bitgo-beta/abstract-cosmos": "1.0.1-beta.1741",
"@bitgo-beta/abstract-eth": "1.0.2-beta.2004",
"@bitgo-beta/abstract-utxo": "1.1.1-beta.2007",
"@bitgo-beta/sdk-api": "1.10.1-beta.1773",
"@bitgo-beta/sdk-api": "1.10.1-beta.1816",
"@bitgo-beta/sdk-coin-ada": "2.3.14-beta.1758",
"@bitgo-beta/sdk-coin-algo": "2.8.9-beta.238",
"@bitgo-beta/sdk-coin-apt": "1.0.1-beta.1200",
Expand Down Expand Up @@ -101,7 +101,7 @@
"@bitgo-beta/sdk-coin-zec": "1.1.1-beta.1984",
"@bitgo-beta/sdk-coin-zeta": "1.0.1-beta.1675",
"@bitgo-beta/sdk-coin-zketh": "1.0.1-beta.1540",
"@bitgo-beta/sdk-core": "8.2.1-beta.1775",
"@bitgo-beta/sdk-core": "8.2.1-beta.1820",
"@bitgo-beta/sdk-lib-mpc": "8.2.0-beta.1770",
"@bitgo-beta/statics": "15.1.1-beta.1782",
"@bitgo/wasm-miniscript": "2.0.0-beta.7",
Expand All @@ -122,7 +122,7 @@
"zod": "^3.25.48"
},
"overrides": {
"@bitgo-beta/sdk-core": "8.2.1-beta.1775",
"@bitgo-beta/sdk-core": "8.2.1-beta.1820",
"@bitgo-beta/statics": "15.1.1-beta.1782",
"elliptic": "^6.6.1",
"expo": "^48.0.0",
Expand Down
48 changes: 30 additions & 18 deletions src/__tests__/api/master/generateWallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ describe('POST /api/v1/:coin/advancedwallet/generate', () => {
const ecdsaCoin = 'hteth';
const accessToken = 'test-token';

// Valid BIP32 extended public keys required by the SDK's isValidPub check
const validUserPub =
'xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8';
const validBackupPub =
'xpub661MyMwAqRbcGczjuMoRm6dXaLDEhW1u34gKenbeYqAix21mdUKJyuyu5F1rzYGVxyL6tmgBUAEPrEz92mBXjByMRiJdba9wpnN37RLLAXa';

let bitgo: BitGoAPI;

function makeConfig(overrides: Partial<MasterExpressConfig> = {}): MasterExpressConfig {
Expand Down Expand Up @@ -132,7 +138,7 @@ describe('POST /api/v1/:coin/advancedwallet/generate', () => {
source: 'user',
})
.reply(200, {
pub: 'xpub_user',
pub: validUserPub,
source: 'user',
type: 'independent',
});
Expand All @@ -143,33 +149,37 @@ describe('POST /api/v1/:coin/advancedwallet/generate', () => {
source: 'backup',
})
.reply(200, {
pub: 'xpub_backup',
pub: validBackupPub,
source: 'backup',
type: 'independent',
});

const bitgoAddUserKeyNock = nock(bitgoApiUrl)
.post(`/api/v2/${coin}/key`, {
pub: 'xpub_user',
pub: validUserPub,
keyType: 'independent',
source: 'user',
})
.matchHeader('any', () => true)
.reply(200, { id: 'user-key-id', pub: 'xpub_user' });
.reply(200, { id: 'user-key-id', pub: validUserPub, source: 'user', type: 'independent' });

const bitgoAddBackupKeyNock = nock(bitgoApiUrl)
.post(`/api/v2/${coin}/key`, {
pub: 'xpub_backup',
pub: validBackupPub,
keyType: 'independent',
source: 'backup',
})
.matchHeader('any', () => true)
.reply(200, { id: 'backup-key-id', pub: 'xpub_backup' });
.reply(200, {
id: 'backup-key-id',
pub: validBackupPub,
source: 'backup',
type: 'independent',
});

const bitgoAddBitGoKeyNock = nock(bitgoApiUrl)
.post(`/api/v2/${coin}/key`, {
source: 'bitgo',
keyType: 'independent',
enterprise: 'test_enterprise',
})
.reply(200, {
Expand Down Expand Up @@ -223,7 +233,7 @@ describe('POST /api/v1/:coin/advancedwallet/generate', () => {
source: 'user',
})
.reply(200, {
pub: 'xpub_user',
pub: validUserPub,
source: 'user',
type: 'independent',
});
Expand All @@ -233,33 +243,37 @@ describe('POST /api/v1/:coin/advancedwallet/generate', () => {
source: 'backup',
})
.reply(200, {
pub: 'xpub_backup',
pub: validBackupPub,
source: 'backup',
type: 'independent',
});

const bitgoAddUserKeyNock = nock(bitgoApiUrl)
.post(`/api/v2/${coin}/key`, {
pub: 'xpub_user',
pub: validUserPub,
keyType: 'independent',
source: 'user',
})
.matchHeader('any', () => true)
.reply(200, { id: 'user-key-id', pub: 'xpub_user' });
.reply(200, { id: 'user-key-id', pub: validUserPub, source: 'user', type: 'independent' });

const bitgoAddBackupKeyNock = nock(bitgoApiUrl)
.post(`/api/v2/${coin}/key`, {
pub: 'xpub_backup',
pub: validBackupPub,
keyType: 'independent',
source: 'backup',
})
.matchHeader('any', () => true)
.reply(200, { id: 'backup-key-id', pub: 'xpub_backup' });
.reply(200, {
id: 'backup-key-id',
pub: validBackupPub,
source: 'backup',
type: 'independent',
});

const bitgoAddBitGoKeyNock = nock(bitgoApiUrl)
.post(`/api/v2/${coin}/key`, {
source: 'bitgo',
keyType: 'independent',
enterprise: 'test_enterprise',
})
.reply(200, {
Expand All @@ -276,8 +290,6 @@ describe('POST /api/v1/:coin/advancedwallet/generate', () => {
.post(`/api/v2/${coin}/wallet/add`, {
label: 'test_wallet',
enterprise: 'test_enterprise',
multisigType: 'onchain',
coin: coin,
m: 2,
n: 3,
keys: ['user-key-id', 'backup-key-id', 'bitgo-key-id'],
Expand Down Expand Up @@ -319,8 +331,8 @@ describe('POST /api/v1/:coin/advancedwallet/generate', () => {
multisigType: 'onchain',
type: 'advanced',
});
response.body.should.have.propertyByPath('userKeychain', 'pub').eql('xpub_user');
response.body.should.have.propertyByPath('backupKeychain', 'pub').eql('xpub_backup');
response.body.should.have.propertyByPath('userKeychain', 'pub').eql(validUserPub);
response.body.should.have.propertyByPath('backupKeychain', 'pub').eql(validBackupPub);
response.body.should.have.propertyByPath('bitgoKeychain', 'pub').eql('xpub_bitgo');

userKeychainNock.done();
Expand Down
153 changes: 153 additions & 0 deletions src/__tests__/api/master/walletGenerationCallbacks.test.ts

@danielpeng1 danielpeng1 Jun 12, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I created a short test file for just wallet gen callbacks, avoids clogging up generateWallet.test.ts

Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
import 'should';
import assert from 'assert';
import nock from 'nock';
import {
createAwmBackupClient,
createAwmClient,
AdvancedWalletManagerClient,
} from '../../../masterBitgoExpress/clients/advancedWalletManagerClient';
import { createOnchainKeyGenCallback } from '../../../masterBitgoExpress/handlers/walletGenerationCallbacks';
import { AppMode, KeySource, MasterExpressConfig, TlsMode } from '../../../shared/types';
import { DEFAULT_ASYNC_MODE_CONFIG } from './testUtils';

describe('walletGenerationCallbacks', () => {
const advancedWalletManagerUrl = 'http://advancedwalletmanager.invalid';
const backupAwmUrl = 'http://backup-awm.invalid';
const coin = 'tbtc';

// Valid BIP32 extended public keys required by the SDK's isValidPub check
const validUserPub =
'xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8';
const validBackupPub =
'xpub661MyMwAqRbcGczjuMoRm6dXaLDEhW1u34gKenbeYqAix21mdUKJyuyu5F1rzYGVxyL6tmgBUAEPrEz92mBXjByMRiJdba9wpnN37RLLAXa';

let awmUserClient: AdvancedWalletManagerClient;
let awmBackupClient: AdvancedWalletManagerClient;

function makeConfig(overrides: Partial<MasterExpressConfig> = {}): MasterExpressConfig {
return {
appMode: AppMode.MASTER_EXPRESS,
port: 0,
bind: 'localhost',
timeout: 60000,
httpLoggerFile: '',
env: 'test',
disableEnvCheck: true,
authVersion: 2,
advancedWalletManagerUrl,
awmServerCaCert: 'dummy-cert',
tlsMode: TlsMode.DISABLED,
clientCertAllowSelfSigned: true,
asyncModeConfig: DEFAULT_ASYNC_MODE_CONFIG,
...overrides,
};
}

before(() => {
nock.disableNetConnect();
});

afterEach(() => {
nock.cleanAll();
});

after(() => {
nock.enableNetConnect();
});

describe('createOnchainKeyGenCallback', () => {
describe('with separate backup AWM (separate-HSM mode)', () => {
beforeEach(() => {
const config = makeConfig({ advancedWalletManagerBackupUrl: backupAwmUrl });
awmUserClient = createAwmClient(config, coin)!;
awmBackupClient = createAwmBackupClient(config, coin)!;
assert(awmUserClient);
assert(awmBackupClient);
});

it('should route user source to the primary AWM client', async () => {
const userKeychainNock = nock(advancedWalletManagerUrl)
.post(`/api/${coin}/key/independent`, {
source: KeySource.USER,
})
.reply(200, {
pub: validUserPub,
source: KeySource.USER,
type: 'independent',
});

const callback = createOnchainKeyGenCallback(awmUserClient, awmBackupClient);
const result = await callback({ source: KeySource.USER, coin });

result.pub.should.equal(validUserPub);
result.source.should.equal(KeySource.USER);
result.type.should.equal('independent');
userKeychainNock.done();
});

it('should route backup source to the backup AWM client', async () => {
const backupKeychainNock = nock(backupAwmUrl)
.post(`/api/${coin}/key/independent`, {
source: KeySource.BACKUP,
})
.reply(200, {
pub: validBackupPub,
source: KeySource.BACKUP,
type: 'independent',
});

const callback = createOnchainKeyGenCallback(awmUserClient, awmBackupClient);
const result = await callback({ source: KeySource.BACKUP, coin });

result.pub.should.equal(validBackupPub);
result.source.should.equal(KeySource.BACKUP);
result.type.should.equal('independent');
backupKeychainNock.done();
});
});

describe('without separate backup AWM (same-HSM mode)', () => {
beforeEach(() => {
const config = makeConfig();
awmUserClient = createAwmClient(config, coin)!;
awmBackupClient = createAwmBackupClient(config, coin) ?? awmUserClient;
assert(awmUserClient);
});

it('should route backup source to the primary AWM client', async () => {
const backupKeychainNock = nock(advancedWalletManagerUrl)
.post(`/api/${coin}/key/independent`, {
source: KeySource.BACKUP,
})
.reply(200, {
pub: validBackupPub,
source: KeySource.BACKUP,
type: 'independent',
});

const callback = createOnchainKeyGenCallback(awmUserClient, awmBackupClient);
const result = await callback({ source: KeySource.BACKUP, coin });

result.pub.should.equal(validBackupPub);
result.source.should.equal(KeySource.BACKUP);
result.type.should.equal('independent');
backupKeychainNock.done();
});
});

it('should throw for unexpected key sources', async () => {
const config = makeConfig({ advancedWalletManagerBackupUrl: backupAwmUrl });
awmUserClient = createAwmClient(config, coin)!;
awmBackupClient = createAwmBackupClient(config, coin)!;
assert(awmUserClient);
assert(awmBackupClient);

const callback = createOnchainKeyGenCallback(awmUserClient, awmBackupClient);

await callback({
source: KeySource.BITGO as 'user',
coin,
}).should.be.rejectedWith('Unexpected key source for onchain key generation: bitgo');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ export class KeyProviderClient extends BaseHttpClient {
);
}

const { pub, coin, source } = response.body;
return { pub, coin, source } as PostKeyResponse;
const { pub, coin, source, type } = response.body;
return { pub, coin, source, type } as PostKeyResponse;
}

async getKey(params: GetKeyParams): Promise<GetKeyResponse> {
Expand Down
Loading
Loading