Files
pezkuwi-common/packages/util-crypto/src/hd/ledger/derivePrivate.ts
T
Satoshi Qazi MuhammedandClaude Opus 5.5 bebfcee609 Bring license headers to 2026
The header rule takes the current year as the end of the range, as upstream's
does; 581 sources still ended at 2025, which failed lint. Lint never ran to
completion here, so this went unseen.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-07 19:21:45 +04:00

35 lines
1.0 KiB
TypeScript

// Copyright 2017-2026 @pezkuwi/util-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { BN_EIGHT, bnToU8a, u8aConcat, u8aToBn } from '@pezkuwi/util';
import { BN_LE_32_OPTS, BN_LE_512_OPTS, BN_LE_OPTS } from '../../bn.js';
import { hmacShaAsU8a } from '../../hmac/index.js';
// performs hard-only derivation on the xprv
export function ledgerDerivePrivate (xprv: Uint8Array, index: number): Uint8Array {
const kl = xprv.subarray(0, 32);
const kr = xprv.subarray(32, 64);
const cc = xprv.subarray(64, 96);
const data = u8aConcat([0], kl, kr, bnToU8a(index, BN_LE_32_OPTS));
const z = hmacShaAsU8a(cc, data, 512);
data[0] = 0x01;
return u8aConcat(
bnToU8a(
u8aToBn(kl, BN_LE_OPTS).iadd(
u8aToBn(z.subarray(0, 28), BN_LE_OPTS).imul(BN_EIGHT)
),
BN_LE_512_OPTS
).subarray(0, 32),
bnToU8a(
u8aToBn(kr, BN_LE_OPTS).iadd(
u8aToBn(z.subarray(32, 64), BN_LE_OPTS)
),
BN_LE_512_OPTS
).subarray(0, 32),
hmacShaAsU8a(cc, data, 512).subarray(32, 64)
);
}