|
| 1 | +import 'should'; |
| 2 | +import { startServices, IntegServices } from './helpers/setup'; |
| 3 | +import { LOCALHOST } from './helpers/servers'; |
| 4 | +import { SigningMode } from '../../shared/types'; |
| 5 | +import type { GenerateWalletResponseBody } from '../../masterBitgoExpress/routers/generateWalletRoute'; |
| 6 | + |
| 7 | +describe('Generate wallet: LOCAL signing', () => { |
| 8 | + let services: IntegServices; |
| 9 | + |
| 10 | + before(async () => { |
| 11 | + services = await startServices(); |
| 12 | + }); |
| 13 | + |
| 14 | + after(async () => { |
| 15 | + await services.teardown(); |
| 16 | + }); |
| 17 | + |
| 18 | + beforeEach(() => { |
| 19 | + services.keyProvider.calls.length = 0; |
| 20 | + services.bitgo.calls.length = 0; |
| 21 | + }); |
| 22 | + |
| 23 | + it('generates a tbtc onchain wallet end-to-end', async () => { |
| 24 | + const res = await fetch( |
| 25 | + `http://${LOCALHOST}:${services.mbePort}/api/v1/tbtc/advancedwallet/generate`, |
| 26 | + { |
| 27 | + method: 'POST', |
| 28 | + headers: { 'Content-Type': 'application/json', Authorization: 'Bearer test-token' }, |
| 29 | + body: JSON.stringify({ |
| 30 | + enterprise: 'test-enterprise', |
| 31 | + label: 'test-wallet', |
| 32 | + multisigType: 'onchain', |
| 33 | + }), |
| 34 | + }, |
| 35 | + ); |
| 36 | + |
| 37 | + res.status.should.equal(200); |
| 38 | + const body = (await res.json()) as GenerateWalletResponseBody; |
| 39 | + body.should.have.property('wallet'); |
| 40 | + body.wallet.should.have.property('id', 'test-wallet-id'); |
| 41 | + |
| 42 | + /** In local mode, AWM stores keys via POST /key — POST /key/generate must NOT be called */ |
| 43 | + const keyProviderStoreCalls = services.keyProvider.calls.filter((c) => c.path === '/key'); |
| 44 | + keyProviderStoreCalls.should.have.length(2); |
| 45 | + |
| 46 | + const keyProviderGenerateCalls = services.keyProvider.calls.filter( |
| 47 | + (c) => c.path === '/key/generate', |
| 48 | + ); |
| 49 | + keyProviderGenerateCalls.should.have.length(0); |
| 50 | + |
| 51 | + /** BitGo received 3 keychain adds */ |
| 52 | + const bitgoKeyCalls = services.bitgo.calls.filter( |
| 53 | + (c) => c.method === 'POST' && c.path.endsWith('/key'), |
| 54 | + ); |
| 55 | + bitgoKeyCalls.should.have.length(3); |
| 56 | + |
| 57 | + /** and 1 wallet add */ |
| 58 | + const walletAddCalls = services.bitgo.calls.filter((c) => c.path.endsWith('/wallet/add')); |
| 59 | + walletAddCalls.should.have.length(1); |
| 60 | + }); |
| 61 | +}); |
| 62 | + |
| 63 | +describe('Generate wallet: EXTERNAL signing', () => { |
| 64 | + let services: IntegServices; |
| 65 | + |
| 66 | + before(async () => { |
| 67 | + services = await startServices({ signingMode: SigningMode.EXTERNAL }); |
| 68 | + }); |
| 69 | + |
| 70 | + after(async () => { |
| 71 | + await services.teardown(); |
| 72 | + }); |
| 73 | + |
| 74 | + beforeEach(() => { |
| 75 | + services.keyProvider.calls.length = 0; |
| 76 | + services.bitgo.calls.length = 0; |
| 77 | + }); |
| 78 | + |
| 79 | + it('generates a tbtc onchain wallet — key provider generates keys (external signing mode)', async () => { |
| 80 | + const res = await fetch( |
| 81 | + `http://${LOCALHOST}:${services.mbePort}/api/v1/tbtc/advancedwallet/generate`, |
| 82 | + { |
| 83 | + method: 'POST', |
| 84 | + headers: { 'Content-Type': 'application/json', Authorization: 'Bearer test-token' }, |
| 85 | + body: JSON.stringify({ |
| 86 | + enterprise: 'test-enterprise', |
| 87 | + label: 'test-wallet', |
| 88 | + multisigType: 'onchain', |
| 89 | + }), |
| 90 | + }, |
| 91 | + ); |
| 92 | + |
| 93 | + res.status.should.equal(200); |
| 94 | + const body = (await res.json()) as GenerateWalletResponseBody; |
| 95 | + body.should.have.property('wallet'); |
| 96 | + body.wallet.should.have.property('id', 'test-wallet-id'); |
| 97 | + |
| 98 | + /** |
| 99 | + * In external mode, AWM delegates key generation to the key provider. |
| 100 | + */ |
| 101 | + const keyProviderGenerateCalls = services.keyProvider.calls.filter( |
| 102 | + (c) => c.path === '/key/generate', |
| 103 | + ); |
| 104 | + keyProviderGenerateCalls.should.have.length(2); |
| 105 | + |
| 106 | + /** POST /key should NOT be called — AWM never generates keys locally in external mode */ |
| 107 | + const keyProviderStoreCalls = services.keyProvider.calls.filter((c) => c.path === '/key'); |
| 108 | + keyProviderStoreCalls.should.have.length(0); |
| 109 | + |
| 110 | + /** BitGo receives 3 keychain adds */ |
| 111 | + const bitgoKeyCalls = services.bitgo.calls.filter( |
| 112 | + (c) => c.method === 'POST' && c.path.endsWith('/key'), |
| 113 | + ); |
| 114 | + bitgoKeyCalls.should.have.length(3); |
| 115 | + |
| 116 | + /** and 1 wallet add */ |
| 117 | + const walletAddCalls = services.bitgo.calls.filter((c) => c.path.endsWith('/wallet/add')); |
| 118 | + walletAddCalls.should.have.length(1); |
| 119 | + }); |
| 120 | +}); |
0 commit comments