Skip to content

Commit 0ef9228

Browse files
davidkaplanbitgollm-git
andcommitted
feat(abstract-lightning): add utility to compute BIP32 derivation index
Add a utility function that computes a BIP32 derivation index from a seed string. The function uses SHA256 to generate a deterministic index value within the valid range for BIP32 derivation paths. Issue: BTC-2202 Co-authored-by: llm-git <[email protected]>
1 parent 8a75e82 commit 0ef9228

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

modules/abstract-lightning/src/lightning/lightningUtils.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,3 +226,12 @@ export function deriveTatSharedSecret(coinName: 'lnbtc' | 'tlnbtc', userXprv: st
226226
const userAuthHdNode = utxolib.bip32.fromBase58(userXprv);
227227
return sdkcore.getSharedSecret(userAuthHdNode, publicKey);
228228
}
229+
230+
/**
231+
* Given a seed, compute a BIP32 derivation index.
232+
* 0 <= index < 4294967295 (largest 4 byte number)
233+
* @param seed
234+
*/
235+
export function computeBip32DerivationIndexFromSeed(seed: string): number {
236+
return Buffer.from(utxolib.crypto.sha256(Buffer.from(seed, 'utf8'))).readUint32BE(0);
237+
}

modules/abstract-lightning/test/unit/lightning/lightningUtils.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
deriveLightningServiceSharedSecret,
1616
deriveMiddlewareSharedSecret,
1717
deriveTatSharedSecret,
18+
computeBip32DerivationIndexFromSeed,
1819
} from '../../../src/lightning';
1920

2021
import * as sdkcore from '@bitgo/sdk-core';
@@ -125,4 +126,10 @@ describe('lightning utils', function () {
125126
const secret = deriveTatSharedSecret('tlnbtc', userXprv);
126127
assert.deepStrictEqual(secret, expectedSecret);
127128
});
129+
130+
it(`computeBip32DerivationIndexFromSeed`, function () {
131+
const seed1 = 'The Times 03/Jan/2009 Chancellor on brink of second bailout for banks.';
132+
const seed2 = 'The Times 04/Jan/2009 Chancellor on brink of second bailout for banks.';
133+
assert.notDeepStrictEqual(computeBip32DerivationIndexFromSeed(seed1), computeBip32DerivationIndexFromSeed(seed2));
134+
});
128135
});

0 commit comments

Comments
 (0)