|
| 1 | +import 'should'; |
| 2 | +import { startServices, IntegServices } from './helpers/setup'; |
| 3 | +import { LOCALHOST } from './helpers/servers'; |
| 4 | +import { SigningMode } from '../../shared/types'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Deterministic test keypair derived from Buffer.alloc(64, 0x42) — a public, reproducible seed. |
| 8 | + * Not a secret. Never funded. Matches getKeychain.user.json and prebuildTx.consolidate.tbtc.json. |
| 9 | + */ |
| 10 | +const USER_XPUB = |
| 11 | + 'xpub661MyMwAqRbcEvJQx6spkkHLRgtjxmVdyDSvbDt2m9NFpbkHdcu5WJsHHHqFxNATbNHnhMWJiwckoMqF75EpcNhU9xeVM4oDS7urM3os4BH'; |
| 12 | +const USER_XPRV = |
| 13 | + 'xprv9s21ZrQH143K2SDwr5LpPcLbsf4FZJmnbzXKnqURCoqGwoR965apxWYoS2DKu2ivcMTB9uTK6XhZDEPfTeNXGf7mmACuMN6cFS5ttmrpZ3i'; |
| 14 | + |
| 15 | +const WALLET_ID = 'test-wallet-id'; |
| 16 | + |
| 17 | +const consolidateRequestBody = { |
| 18 | + pubkey: USER_XPUB, |
| 19 | + source: 'user' as const, |
| 20 | + feeRate: 1000, |
| 21 | + maxFeeRate: 2000, |
| 22 | + minValue: 1000, |
| 23 | +}; |
| 24 | + |
| 25 | +interface TransferEntry { |
| 26 | + address: string; |
| 27 | + value: number; |
| 28 | + isChange?: boolean; |
| 29 | +} |
| 30 | + |
| 31 | +interface ConsolidateResponse { |
| 32 | + txid: string; |
| 33 | + tx: string; |
| 34 | + status: string; |
| 35 | + transfer: { |
| 36 | + txid: string; |
| 37 | + status: string; |
| 38 | + entries: TransferEntry[]; |
| 39 | + }; |
| 40 | +} |
| 41 | + |
| 42 | +describe('Consolidate unspents: EXTERNAL signing', () => { |
| 43 | + let services: IntegServices; |
| 44 | + |
| 45 | + before(async () => { |
| 46 | + services = await startServices({ signingMode: SigningMode.EXTERNAL }); |
| 47 | + }); |
| 48 | + |
| 49 | + after(async () => { |
| 50 | + await services.teardown(); |
| 51 | + }); |
| 52 | + |
| 53 | + beforeEach(() => { |
| 54 | + services.keyProvider.calls.length = 0; |
| 55 | + services.bitgo.calls.length = 0; |
| 56 | + }); |
| 57 | + |
| 58 | + it('consolidates tbtc unspents via external key provider', async () => { |
| 59 | + const res = await fetch( |
| 60 | + `http://${LOCALHOST}:${services.mbePort}/api/v1/tbtc/advancedwallet/${WALLET_ID}/consolidateunspents`, |
| 61 | + { |
| 62 | + method: 'POST', |
| 63 | + headers: { 'Content-Type': 'application/json', Authorization: 'Bearer test-token' }, |
| 64 | + body: JSON.stringify(consolidateRequestBody), |
| 65 | + }, |
| 66 | + ); |
| 67 | + |
| 68 | + res.status.should.equal(200); |
| 69 | + const body = (await res.json()) as ConsolidateResponse; |
| 70 | + body.should.have.property('txid', 'test-tx-id'); |
| 71 | + body.should.have.property('tx', '01000000000101030a0000'); |
| 72 | + body.should.have.property('status', 'signed'); |
| 73 | + body.transfer.should.have.property('txid', 'test-tx-id'); |
| 74 | + body.transfer.entries.should.be.Array().and.have.length(2); |
| 75 | + |
| 76 | + /** |
| 77 | + * In external mode, AWM delegates signing to the key provider. |
| 78 | + * POST /sign must be called — not POST /key (no local key retrieval for signing). |
| 79 | + * The signablePayload for BTC is a PSBT hex (starts with PSBT magic bytes 70736274ff). |
| 80 | + */ |
| 81 | + const signCalls = services.keyProvider.calls.filter((c) => c.path === '/sign'); |
| 82 | + signCalls.should.have.length(1); |
| 83 | + const signBody = signCalls[0].body as { signablePayload: string }; |
| 84 | + signBody.signablePayload.should.startWith('70736274ff'); |
| 85 | + |
| 86 | + services.keyProvider.calls.filter((c) => c.path === '/key').should.have.length(0); |
| 87 | + |
| 88 | + /** |
| 89 | + * BitGo must receive consolidateUnspents (not tx/build) with the consolidation params, |
| 90 | + * then tx/send with type: 'consolidate'. tx/build is only used by sendMany and accelerate. |
| 91 | + */ |
| 92 | + const consolidateCalls = services.bitgo.calls.filter((c) => |
| 93 | + c.path.endsWith('/consolidateUnspents'), |
| 94 | + ); |
| 95 | + consolidateCalls.should.have.length(1); |
| 96 | + const consolidateBody = consolidateCalls[0].body as { |
| 97 | + feeRate?: number; |
| 98 | + maxFeeRate?: number; |
| 99 | + minValue?: number; |
| 100 | + txFormat?: string; |
| 101 | + }; |
| 102 | + consolidateBody.should.have.property('feeRate', 1000); |
| 103 | + consolidateBody.should.have.property('maxFeeRate', 2000); |
| 104 | + consolidateBody.should.have.property('minValue', 1000); |
| 105 | + consolidateBody.should.have.property('txFormat', 'psbt-lite'); |
| 106 | + |
| 107 | + const sendCalls = services.bitgo.calls.filter((c) => c.path.endsWith('/tx/send')); |
| 108 | + sendCalls.should.have.length(1); |
| 109 | + const sendBody = sendCalls[0].body as { type?: string }; |
| 110 | + sendBody.should.have.property('type', 'consolidate'); |
| 111 | + |
| 112 | + services.bitgo.calls.filter((c) => c.path.endsWith('/tx/build')).should.have.length(0); |
| 113 | + }); |
| 114 | +}); |
| 115 | + |
| 116 | +describe('Consolidate unspents: LOCAL signing', () => { |
| 117 | + let services: IntegServices; |
| 118 | + |
| 119 | + before(async () => { |
| 120 | + services = await startServices({ signingMode: SigningMode.LOCAL }); |
| 121 | + |
| 122 | + /** |
| 123 | + * Seed the mock key provider with a known xprv so AWM can retrieve it |
| 124 | + * via GET /key/:pub and sign the PSBT locally. The xpub must match |
| 125 | + * getKeychain.user.json and the bip32Derivation in prebuildTx.consolidate.tbtc.json. |
| 126 | + */ |
| 127 | + await fetch(`http://127.0.0.1:${services.keyProvider.port}/key`, { |
| 128 | + method: 'POST', |
| 129 | + headers: { 'Content-Type': 'application/json' }, |
| 130 | + body: JSON.stringify({ |
| 131 | + pub: USER_XPUB, |
| 132 | + prv: USER_XPRV, |
| 133 | + coin: 'tbtc', |
| 134 | + source: 'user', |
| 135 | + type: 'independent', |
| 136 | + }), |
| 137 | + }); |
| 138 | + }); |
| 139 | + |
| 140 | + after(async () => { |
| 141 | + await services.teardown(); |
| 142 | + }); |
| 143 | + |
| 144 | + beforeEach(() => { |
| 145 | + services.keyProvider.calls.length = 0; |
| 146 | + services.bitgo.calls.length = 0; |
| 147 | + }); |
| 148 | + |
| 149 | + it('consolidates tbtc unspents using locally stored xprv', async () => { |
| 150 | + const res = await fetch( |
| 151 | + `http://${LOCALHOST}:${services.mbePort}/api/v1/tbtc/advancedwallet/${WALLET_ID}/consolidateunspents`, |
| 152 | + { |
| 153 | + method: 'POST', |
| 154 | + headers: { 'Content-Type': 'application/json', Authorization: 'Bearer test-token' }, |
| 155 | + body: JSON.stringify(consolidateRequestBody), |
| 156 | + }, |
| 157 | + ); |
| 158 | + |
| 159 | + res.status.should.equal(200); |
| 160 | + const body = (await res.json()) as ConsolidateResponse; |
| 161 | + body.should.have.property('txid', 'test-tx-id'); |
| 162 | + body.should.have.property('tx', '01000000000101030a0000'); |
| 163 | + body.should.have.property('status', 'signed'); |
| 164 | + body.transfer.should.have.property('txid', 'test-tx-id'); |
| 165 | + body.transfer.entries.should.be.Array().and.have.length(2); |
| 166 | + |
| 167 | + /** |
| 168 | + * In local mode, AWM retrieves the xprv via GET /key/:pub and signs internally. |
| 169 | + * POST /sign must NOT be called — signing happens inside AWM, not in the key provider. |
| 170 | + */ |
| 171 | + services.keyProvider.calls.filter((c) => c.path === '/sign').should.have.length(0); |
| 172 | + services.keyProvider.calls.filter((c) => c.path.startsWith('/key/')).length.should.be.above(0); |
| 173 | + |
| 174 | + /** |
| 175 | + * BitGo must receive consolidateUnspents (not tx/build) with the consolidation params, |
| 176 | + * then tx/send with type: 'consolidate'. |
| 177 | + */ |
| 178 | + const consolidateCalls = services.bitgo.calls.filter((c) => |
| 179 | + c.path.endsWith('/consolidateUnspents'), |
| 180 | + ); |
| 181 | + consolidateCalls.should.have.length(1); |
| 182 | + const consolidateBody = consolidateCalls[0].body as { |
| 183 | + feeRate?: number; |
| 184 | + maxFeeRate?: number; |
| 185 | + minValue?: number; |
| 186 | + txFormat?: string; |
| 187 | + }; |
| 188 | + consolidateBody.should.have.property('feeRate', 1000); |
| 189 | + consolidateBody.should.have.property('maxFeeRate', 2000); |
| 190 | + consolidateBody.should.have.property('minValue', 1000); |
| 191 | + consolidateBody.should.have.property('txFormat', 'psbt-lite'); |
| 192 | + |
| 193 | + const sendCalls = services.bitgo.calls.filter((c) => c.path.endsWith('/tx/send')); |
| 194 | + sendCalls.should.have.length(1); |
| 195 | + const sendBody = sendCalls[0].body as { type?: string }; |
| 196 | + sendBody.should.have.property('type', 'consolidate'); |
| 197 | + |
| 198 | + services.bitgo.calls.filter((c) => c.path.endsWith('/tx/build')).should.have.length(0); |
| 199 | + }); |
| 200 | +}); |
0 commit comments