Skip to content

Commit 32b8a8f

Browse files
OttoAllmendingerllm-git
andcommitted
refactor(utxo-lib): rename getChainAndIndex to getScriptIdFromPath
Introduce ScriptId type alias and rename function for better semantic clarity. The new name better reflects that the function extracts script identifiers from BIP32 paths, improving code readability and type safety. Issue: BTC-1966 Co-authored-by: llm-git <[email protected]>
1 parent f364913 commit 32b8a8f

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
import { ChainCode, isChainCode } from './chains';
22

3-
export function getChainAndIndexFromPath(path: string): {
3+
export type ScriptId = {
44
chain: ChainCode;
55
index: number;
6-
} {
6+
};
7+
8+
/**
9+
* Get the chain and index from a bip32 path.
10+
*
11+
* @param path
12+
*/
13+
export function getScriptIdFromPath(path: string): ScriptId {
714
const parts = path.split('/');
815
if (parts.length <= 2) {
916
throw new Error(`invalid path "${path}"`);
@@ -19,3 +26,6 @@ export function getChainAndIndexFromPath(path: string): {
1926

2027
return { chain, index };
2128
}
29+
30+
/** @deprecated use getScriptId */
31+
export const getChainAndIndexFromPath = getScriptIdFromPath;
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
import * as assert from 'assert';
22

3-
import { getChainAndIndexFromPath } from '../../../src/bitgo/wallet/ScriptId';
3+
import { getScriptIdFromPath } from '../../../src/bitgo/wallet/ScriptId';
44

5-
describe('getChainAndIndexFromPath', function () {
5+
describe('getScriptId', function () {
66
it('should throw if path is not the right length', function () {
7-
assert.throws(() => getChainAndIndexFromPath('/'), /invalid path/);
8-
assert.throws(() => getChainAndIndexFromPath('m0000ssss'), /invalid path/);
7+
assert.throws(() => getScriptIdFromPath('/'), /invalid path/);
8+
assert.throws(() => getScriptIdFromPath('m0000ssss'), /invalid path/);
99
});
1010

1111
it('should throw if the path is not a number', function () {
1212
const invalidChain = [-1, 2, 'lol'];
1313
const invalidIndex = [-1, 'lol'];
1414
for (const chain of invalidChain) {
15-
assert.throws(() => getChainAndIndexFromPath(`m/${chain}/0`), /invalid chain/);
15+
assert.throws(() => getScriptIdFromPath(`m/${chain}/0`), /invalid chain/);
1616
}
1717
for (const index of invalidIndex) {
18-
assert.throws(() => getChainAndIndexFromPath(`m/0/${index}`), /invalid index/);
18+
assert.throws(() => getScriptIdFromPath(`m/0/${index}`), /invalid index/);
1919
}
2020
});
2121

2222
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 });
23+
assert.deepStrictEqual(getScriptIdFromPath('m/1/2'), { chain: 1, index: 2 });
24+
assert.deepStrictEqual(getScriptIdFromPath('m/1/2/3/4/5/10/20'), { chain: 10, index: 20 });
2525
});
2626
});

0 commit comments

Comments
 (0)