Skip to content

Commit c8ab32e

Browse files
committed
fix(abstract-utxo): reject invalid derivation indexes
1 parent 43a4ef2 commit c8ab32e

2 files changed

Lines changed: 84 additions & 2 deletions

File tree

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,11 @@ 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

@@ -157,7 +161,12 @@ export function assertFixedScriptWalletAddress(
157161
address: string;
158162
}
159163
): void {
160-
if ((_.isUndefined(chain) && _.isUndefined(index)) || !(_.isFinite(chain) && _.isFinite(index))) {
164+
if (
165+
(_.isUndefined(chain) && _.isUndefined(index)) ||
166+
!(_.isFinite(chain) && _.isFinite(index)) ||
167+
!_.isInteger(index) ||
168+
(index as number) < 0
169+
) {
161170
throw new InvalidAddressDerivationPropertyError(
162171
`address validation failure: invalid chain (${chain}) or index (${index})`
163172
);
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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

Comments
 (0)