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