Skip to content

Commit 38a13e2

Browse files
committed
fix(abstract-utxo): reject invalid chain and index values
ISSUE: #9401
1 parent 43a4ef2 commit 38a13e2

2 files changed

Lines changed: 121 additions & 2 deletions

File tree

modules/abstract-utxo/src/address/fixedScript.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,20 @@ export function generateAddressWithChainAndIndex(
7272
*/
7373
export function generateAddress(coinName: UtxoCoinName, params: GenerateFixedScriptAddressOptions): string {
7474
let derivationIndex = 0;
75-
if (_.isInteger(params.index) && (params.index as number) > 0) {
75+
if (!_.isUndefined(params.index)) {
76+
if (!_.isInteger(params.index) || (params.index as number) < 0) {
77+
throw new InvalidAddressDerivationPropertyError(`address validation failure: invalid index (${params.index})`);
78+
}
79+
7680
derivationIndex = params.index as number;
7781
}
7882

7983
const { keychains, chain, segwit = false, bech32 = false } = params as GenerateFixedScriptAddressOptions;
8084

85+
if (!_.isUndefined(chain) && !_.isInteger(chain)) {
86+
throw new InvalidAddressDerivationPropertyError(`address validation failure: invalid chain (${chain})`);
87+
}
88+
8189
if (_.isNumber(chain) && _.isInteger(chain) && !fixedScriptWallet.ChainCode.is(chain)) {
8290
throw new InvalidAddressDerivationPropertyError(`address validation failure: unrecognised chain code (${chain})`);
8391
}
@@ -157,7 +165,13 @@ export function assertFixedScriptWalletAddress(
157165
address: string;
158166
}
159167
): void {
160-
if ((_.isUndefined(chain) && _.isUndefined(index)) || !(_.isFinite(chain) && _.isFinite(index))) {
168+
if (
169+
(_.isUndefined(chain) && _.isUndefined(index)) ||
170+
!(_.isFinite(chain) && _.isFinite(index)) ||
171+
!_.isInteger(chain) ||
172+
!_.isInteger(index) ||
173+
(index as number) < 0
174+
) {
161175
throw new InvalidAddressDerivationPropertyError(
162176
`address validation failure: invalid chain (${chain}) or index (${index})`
163177
);
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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('rejects a non-integer derivation chain', function () {
57+
assert.throws(
58+
() =>
59+
generateAddress('btc', {
60+
keychains,
61+
chain: 1.5,
62+
index: 0,
63+
}),
64+
InvalidAddressDerivationPropertyError
65+
);
66+
});
67+
68+
it('rejects a non-integer chain during address validation', function () {
69+
const address = generateAddress('btc', {
70+
keychains,
71+
chain: 0,
72+
index: 0,
73+
});
74+
75+
assert.throws(
76+
() =>
77+
assertFixedScriptWalletAddress('btc', {
78+
chain: 1.5,
79+
index: 0,
80+
keychains,
81+
format: 'base58',
82+
address,
83+
}),
84+
InvalidAddressDerivationPropertyError
85+
);
86+
});
87+
88+
it('still accepts index 0', function () {
89+
const address0 = generateAddress('btc', {
90+
keychains,
91+
chain,
92+
index: 0,
93+
});
94+
95+
assert.doesNotThrow(() =>
96+
assertFixedScriptWalletAddress('btc', {
97+
chain,
98+
index: 0,
99+
keychains,
100+
format: 'base58',
101+
address: address0,
102+
})
103+
);
104+
});
105+
});

0 commit comments

Comments
 (0)