Skip to content

Commit 48e7fa0

Browse files
Merge pull request #6366 from BitGo/BTC-2196.correct-derivation-index-max
fix(abstract-lightning): correct derivation index computation
2 parents 5d61163 + 6800e77 commit 48e7fa0

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,14 @@ export function deriveTatSharedSecret(coinName: 'lnbtc' | 'tlnbtc', xprv: string
228228

229229
/**
230230
* Given a seed, compute a BIP32 derivation index.
231-
* 0 <= index < 4294967295 (largest 4 byte number)
231+
* 0 <= index < 2147483648 (largest 31 bit number). This needs to be 2^31 - 1 so that the bip32 library
232+
* can derive the hardened key.
232233
* @param seed (optional) If nothing provided, we will generate one randomly
233234
*/
234235
export function computeBip32DerivationIndexFromSeed(seed?: string): number {
235-
return Buffer.from(utxolib.crypto.sha256(Buffer.from(seed ?? randomBytes(32).toString('hex'), 'utf8'))).readUint32BE(
236-
0
236+
return (
237+
(Buffer.from(utxolib.crypto.sha256(Buffer.from(seed ?? randomBytes(32).toString('hex'), 'utf8'))).readUint32BE(0) %
238+
Math.pow(2, 31)) -
239+
1
237240
);
238241
}

0 commit comments

Comments
 (0)