|
1 | 1 | import * as assert from 'assert'; |
2 | 2 |
|
3 | 3 | import { InvalidAddressDerivationPropertyError, UnexpectedAddressError } from '@bitgo/sdk-core'; |
| 4 | +import { fixedScriptWallet } from '@bitgo/wasm-utxo'; |
4 | 5 |
|
5 | 6 | import { assertFixedScriptWalletAddress, generateAddress } from '../../src'; |
6 | 7 |
|
7 | 8 | import { keychainsBase58 } from './util'; |
8 | 9 |
|
9 | 10 | const keychains = keychainsBase58.map((k) => ({ pub: k.pub })); |
10 | 11 |
|
| 12 | +// A chain code that no released SDK version has ever defined — simulates a future |
| 13 | +// server-side address type unknown to an older client. |
| 14 | +const unknownChainCode = 99; |
| 15 | + |
11 | 16 | describe('assertFixedScriptWalletAddress', function () { |
12 | 17 | describe('input validation', function () { |
13 | 18 | it('throws InvalidAddressDerivationPropertyError when both chain and index are undefined', function () { |
@@ -121,6 +126,40 @@ describe('assertFixedScriptWalletAddress', function () { |
121 | 126 | ); |
122 | 127 | }); |
123 | 128 |
|
| 129 | + // Regression guard for T1-3386 / T1-3385: an unknown chain code must throw |
| 130 | + // immediately with a clear message rather than silently falling back to P2SH |
| 131 | + // and producing a confusing "expected <P2SH> but got <P2TR>" error. |
| 132 | + it('throws InvalidAddressDerivationPropertyError for an unknown chain code', function () { |
| 133 | + assert.ok(!fixedScriptWallet.ChainCode.is(unknownChainCode), 'test prerequisite: chain must be unknown'); |
| 134 | + const serverAddress = generateAddress('btc', { keychains, chain: 40 }); |
| 135 | + assert.throws( |
| 136 | + () => |
| 137 | + assertFixedScriptWalletAddress('btc', { |
| 138 | + chain: unknownChainCode, |
| 139 | + index: 0, |
| 140 | + keychains, |
| 141 | + format: 'base58', |
| 142 | + address: serverAddress, |
| 143 | + }), |
| 144 | + (err: unknown) => { |
| 145 | + assert.ok(err instanceof InvalidAddressDerivationPropertyError); |
| 146 | + assert.ok( |
| 147 | + err.message.includes(String(unknownChainCode)), |
| 148 | + `expected error to name the unrecognised chain code, got: ${err.message}` |
| 149 | + ); |
| 150 | + return true; |
| 151 | + } |
| 152 | + ); |
| 153 | + }); |
| 154 | + |
| 155 | + it('generateAddress throws InvalidAddressDerivationPropertyError for an unknown chain code', function () { |
| 156 | + assert.ok(!fixedScriptWallet.ChainCode.is(unknownChainCode), 'test prerequisite: chain must be unknown'); |
| 157 | + assert.throws( |
| 158 | + () => generateAddress('btc', { keychains, chain: unknownChainCode }), |
| 159 | + InvalidAddressDerivationPropertyError |
| 160 | + ); |
| 161 | + }); |
| 162 | + |
124 | 163 | it('succeeds for bch cashaddr (chain 0)', function () { |
125 | 164 | const address = generateAddress('bch', { keychains, chain: 0, format: 'cashaddr' }); |
126 | 165 | assert.doesNotThrow(() => |
|
0 commit comments