Skip to content

Commit f364913

Browse files
OttoAllmendingerllm-git
andcommitted
refactor(utxo-lib): move getChainAndIndexFromPath to separate file
Move chain and index parsing function to ScriptId.ts in wallet module for improved code organization. Add dedicated test file and improved validation. Issue: BTC-1966 Co-authored-by: llm-git <[email protected]>
1 parent 8c8644c commit f364913

File tree

5 files changed

+49
-43
lines changed

5 files changed

+49
-43
lines changed

modules/utxo-lib/src/bitgo/parseInput.ts

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
/* eslint no-redeclare: 0 */
22
import * as opcodes from 'bitcoin-ops';
3-
import { TxInput, script as bscript } from 'bitcoinjs-lib';
3+
import { script as bscript, TxInput } from 'bitcoinjs-lib';
44

55
import { isTriple } from './types';
66
import { isScriptType2Of3 } from './outputScripts';
7-
import { ChainCode, isChainCode } from './wallet/chains';
87

98
export function isPlaceholderSignature(v: number | Buffer): boolean {
109
if (Buffer.isBuffer(v)) {
@@ -699,20 +698,3 @@ export function parsePubScript(
699698

700699
return result;
701700
}
702-
703-
export function getChainAndIndexFromPath(path: string): { chain: ChainCode; index: number } {
704-
const parts = path.split('/');
705-
if (parts.length <= 2) {
706-
throw new Error(`invalid path "${path}"`);
707-
}
708-
const chain = Number(parts[parts.length - 2]);
709-
const index = Number(parts[parts.length - 1]);
710-
if (!isChainCode(chain)) {
711-
throw new Error(`invalid chain "${chain}"`);
712-
}
713-
if (!Number.isInteger(index) || index < 0) {
714-
throw new Error(`invalid index "${index}"`);
715-
}
716-
717-
return { chain, index };
718-
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { ChainCode, isChainCode } from './chains';
2+
3+
export function getChainAndIndexFromPath(path: string): {
4+
chain: ChainCode;
5+
index: number;
6+
} {
7+
const parts = path.split('/');
8+
if (parts.length <= 2) {
9+
throw new Error(`invalid path "${path}"`);
10+
}
11+
const chain = Number(parts[parts.length - 2]);
12+
const index = Number(parts[parts.length - 1]);
13+
if (!isChainCode(chain)) {
14+
throw new Error(`invalid chain "${chain}"`);
15+
}
16+
if (!Number.isInteger(index) || index < 0) {
17+
throw new Error(`invalid index "${index}"`);
18+
}
19+
20+
return { chain, index };
21+
}

modules/utxo-lib/src/bitgo/wallet/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ export * from './WalletScripts';
77
export * from './WalletKeys';
88
export * from './psbt/PsbtOutputs';
99
export * from './psbt/RootNodes';
10+
export * from './ScriptId';
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import * as assert from 'assert';
2+
3+
import { getChainAndIndexFromPath } from '../../../src/bitgo/wallet/ScriptId';
4+
5+
describe('getChainAndIndexFromPath', function () {
6+
it('should throw if path is not the right length', function () {
7+
assert.throws(() => getChainAndIndexFromPath('/'), /invalid path/);
8+
assert.throws(() => getChainAndIndexFromPath('m0000ssss'), /invalid path/);
9+
});
10+
11+
it('should throw if the path is not a number', function () {
12+
const invalidChain = [-1, 2, 'lol'];
13+
const invalidIndex = [-1, 'lol'];
14+
for (const chain of invalidChain) {
15+
assert.throws(() => getChainAndIndexFromPath(`m/${chain}/0`), /invalid chain/);
16+
}
17+
for (const index of invalidIndex) {
18+
assert.throws(() => getChainAndIndexFromPath(`m/0/${index}`), /invalid index/);
19+
}
20+
});
21+
22+
it('should set the chain and index correctly', function () {
23+
assert.deepStrictEqual(getChainAndIndexFromPath('m/1/2'), { chain: 1, index: 2 });
24+
assert.deepStrictEqual(getChainAndIndexFromPath('m/1/2/3/4/5/10/20'), { chain: 10, index: 20 });
25+
});
26+
});

modules/utxo-lib/test/bitgo/wallet/chains.ts

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import * as assert from 'assert';
22
import {
33
ChainCode,
44
chainCodes,
5-
getChainAndIndexFromPath,
65
getExternalChainCode,
76
getInternalChainCode,
87
isChainCode,
@@ -59,26 +58,3 @@ describe('chain codes', function () {
5958
});
6059
});
6160
});
62-
63-
describe('getChainAndIndexFromPath', function () {
64-
it('should throw if path is not the right length', function () {
65-
assert.throws(() => getChainAndIndexFromPath('/'), /invalid path/);
66-
assert.throws(() => getChainAndIndexFromPath('m0000ssss'), /invalid path/);
67-
});
68-
69-
it('should throw if the path is not a number', function () {
70-
const invalidChain = [-1, 2, 'lol'];
71-
const invalidIndex = [-1, 'lol'];
72-
for (const chain of invalidChain) {
73-
assert.throws(() => getChainAndIndexFromPath(`m/${chain}/0`), /invalid chain/);
74-
}
75-
for (const index of invalidIndex) {
76-
assert.throws(() => getChainAndIndexFromPath(`m/0/${index}`), /invalid index/);
77-
}
78-
});
79-
80-
it('should set the chain and index correctly', function () {
81-
assert.deepStrictEqual(getChainAndIndexFromPath('m/1/2'), { chain: 1, index: 2 });
82-
assert.deepStrictEqual(getChainAndIndexFromPath('m/1/2/3/4/5/10/20'), { chain: 10, index: 20 });
83-
});
84-
});

0 commit comments

Comments
 (0)