UNPKG

ox

Version:

Ethereum Standard Library

96 lines 2.46 kB
import { HDKey } from '@scure/bip32'; import * as Bytes from './Bytes.js'; import * as internal from './internal/hdKey.js'; /** * Creates a HD Key from an extended private key. * * @example * ```ts twoslash * import { HdKey } from 'ox' * * const hdKey = HdKey.fromExtendedKey('...') * * console.log(hdKey.privateKey) * // @log: '0x...' * ``` * * @param extendedKey - The extended private key. * @returns The HD Key. */ export function fromExtendedKey(extendedKey) { const key = HDKey.fromExtendedKey(extendedKey); return internal.fromScure(key); } /** * Creates a HD Key from a JSON object containing an extended private key (`xpriv`). * * @example * ```ts twoslash * import { HdKey } from 'ox' * * const hdKey = HdKey.fromJson({ xpriv: '...' }) * * console.log(hdKey.privateKey) * // @log: '0x...' * ``` * * @param json - The JSON object containing an extended private key (`xpriv`). * @returns The HD Key. */ export function fromJson(json) { return internal.fromScure(HDKey.fromJSON(json)); } /** * Creates a HD Key from a master seed. * * @example * ```ts twoslash * import { HdKey, Mnemonic } from 'ox' * * const seed = Mnemonic.toSeed('test test test test test test test test test test test junk') * const hdKey = HdKey.fromSeed(seed) * ``` * * @example * ### Path Derivation * * You can derive a HD Key at a specific path using `derive`. * * ```ts twoslash * import { HdKey, Mnemonic } from 'ox' * * const mnemonic = Mnemonic.toSeed('test test test test test test test test test test test junk') * const hdKey = HdKey.fromSeed(mnemonic).derive(HdKey.path()) * * console.log(hdKey.privateKey) * // @log: '0x...' * ``` * * @param seed - The master seed to create the HD Key from. * @param options - Creation options. * @returns The HD Key. */ export function fromSeed(seed, options = {}) { const { versions } = options; const key = HDKey.fromMasterSeed(Bytes.from(seed), versions); return internal.fromScure(key); } /** * Creates an Ethereum-based BIP-44 HD path. * * @example * ```ts twoslash * import { HdKey } from 'ox' * * const path = HdKey.path({ account: 1, index: 2 }) * // @log: "m/44'/60'/1'/0/2" * ``` * * @param options - Path options. * @returns The path. */ export function path(options = {}) { const { account = 0, change = 0, index = 0 } = options; return `m/44'/60'/${account}'/${change}/${index}`; } //# sourceMappingURL=HdKey.js.map