|
| 1 | +import * as assert from 'assert'; |
| 2 | + |
| 3 | +import { InvalidAddressDerivationPropertyError } from '@bitgo/sdk-core'; |
| 4 | + |
| 5 | +import { assertFixedScriptWalletAddress, generateAddress } from '../../src'; |
| 6 | + |
| 7 | +const keychains = [ |
| 8 | + { |
| 9 | + pub: 'xpub661MyMwAqRbcGiQhVk1J7cD1YodF9tc5Y1B8vpTjjB1pcB1J1m1QX8fMtYP2sYqFmW6J2ra69tNoARKjvTGo9cGUrbPbJdjwrSzGGzPzWWS', |
| 10 | + }, |
| 11 | + { |
| 12 | + pub: 'xpub661MyMwAqRbcFzLXuganogQvd7MrefQQqCcJP2ZDumnCdQecf5cw1P1nD5qBz8SNS1yCLSC9VqpNUWnQU3V6qmnPt2r21oXhicQFzPA6Lby', |
| 13 | + }, |
| 14 | + { |
| 15 | + pub: 'xpub661MyMwAqRbcFHpwWrzPB61U2CgBmdD21WNVM1JKUn9rEExkoGE4yafUVFbPSd78vdX8tWcEUQWaALFkU9fUbUM4Cc49DKEJSCYGRnbzCym', |
| 16 | + }, |
| 17 | +]; |
| 18 | + |
| 19 | +describe('fixedScript address index edge cases', function () { |
| 20 | + const chain = 20; |
| 21 | + |
| 22 | + for (const invalidIndex of [-1, 1.5]) { |
| 23 | + it(`rejects invalid derivation index ${invalidIndex}`, function () { |
| 24 | + assert.throws( |
| 25 | + () => |
| 26 | + generateAddress('btc', { |
| 27 | + keychains, |
| 28 | + chain, |
| 29 | + index: invalidIndex, |
| 30 | + }), |
| 31 | + InvalidAddressDerivationPropertyError |
| 32 | + ); |
| 33 | + }); |
| 34 | + |
| 35 | + it(`rejects index ${invalidIndex} during address validation`, function () { |
| 36 | + const address0 = generateAddress('btc', { |
| 37 | + keychains, |
| 38 | + chain, |
| 39 | + index: 0, |
| 40 | + }); |
| 41 | + |
| 42 | + assert.throws( |
| 43 | + () => |
| 44 | + assertFixedScriptWalletAddress('btc', { |
| 45 | + chain, |
| 46 | + index: invalidIndex, |
| 47 | + keychains, |
| 48 | + format: 'base58', |
| 49 | + address: address0, |
| 50 | + }), |
| 51 | + InvalidAddressDerivationPropertyError |
| 52 | + ); |
| 53 | + }); |
| 54 | + } |
| 55 | + |
| 56 | + it('still accepts index 0', function () { |
| 57 | + const address0 = generateAddress('btc', { |
| 58 | + keychains, |
| 59 | + chain, |
| 60 | + index: 0, |
| 61 | + }); |
| 62 | + |
| 63 | + assert.doesNotThrow(() => |
| 64 | + assertFixedScriptWalletAddress('btc', { |
| 65 | + chain, |
| 66 | + index: 0, |
| 67 | + keychains, |
| 68 | + format: 'base58', |
| 69 | + address: address0, |
| 70 | + }) |
| 71 | + ); |
| 72 | + }); |
| 73 | +}); |
0 commit comments