|
| 1 | +/** |
| 2 | + * Wallet Import Format (WIF) encoding and decoding. |
| 3 | + * |
| 4 | + * ```js |
| 5 | + * import { fromWifString, toWifString } from '@exodus/bytes/wif.js' |
| 6 | + * import { fromWifStringSync, toWifStringSync } from '@exodus/bytes/wif.js' |
| 7 | + * ``` |
| 8 | + * |
| 9 | + * On non-Node.js, requires peer dependency [@noble/hashes](https://www.npmjs.com/package/@noble/hashes) to be installed. |
| 10 | + * |
| 11 | + * @module @exodus/bytes/wif.js |
| 12 | + */ |
| 13 | + |
| 14 | +/// <reference types="node" /> |
| 15 | + |
| 16 | +import type { Uint8ArrayBuffer } from './array.js'; |
| 17 | + |
| 18 | +/** |
| 19 | + * WIF (Wallet Import Format) data structure |
| 20 | + */ |
| 21 | +export interface Wif { |
| 22 | + /** Network version byte */ |
| 23 | + version: number; |
| 24 | + /** 32-byte private key */ |
| 25 | + privateKey: Uint8ArrayBuffer; |
| 26 | + /** Whether the key is compressed */ |
| 27 | + compressed: boolean; |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * Decode a WIF string to WIF data |
| 32 | + * |
| 33 | + * Returns a promise that resolves to an object with `{ version, privateKey, compressed }`. |
| 34 | + * |
| 35 | + * The optional `version` parameter validates the version byte. |
| 36 | + * |
| 37 | + * Throws if the WIF string is invalid or version doesn't match. |
| 38 | + * |
| 39 | + * @param string - The WIF encoded string |
| 40 | + * @param version - Optional expected version byte to validate against |
| 41 | + * @returns The decoded WIF data |
| 42 | + * @throws Error if the WIF string is invalid or version doesn't match |
| 43 | + */ |
| 44 | +export function fromWifString(string: string, version?: number): Promise<Wif>; |
| 45 | + |
| 46 | +/** |
| 47 | + * Decode a WIF string to WIF data (synchronous) |
| 48 | + * |
| 49 | + * Returns an object with `{ version, privateKey, compressed }`. |
| 50 | + * |
| 51 | + * The optional `version` parameter validates the version byte. |
| 52 | + * |
| 53 | + * Throws if the WIF string is invalid or version doesn't match. |
| 54 | + * |
| 55 | + * @param string - The WIF encoded string |
| 56 | + * @param version - Optional expected version byte to validate against |
| 57 | + * @returns The decoded WIF data |
| 58 | + * @throws Error if the WIF string is invalid or version doesn't match |
| 59 | + */ |
| 60 | +export function fromWifStringSync(string: string, version?: number): Wif; |
| 61 | + |
| 62 | +/** |
| 63 | + * Encode WIF data to a WIF string |
| 64 | + * |
| 65 | + * @param wif - The WIF data to encode |
| 66 | + * @returns The WIF encoded string |
| 67 | + */ |
| 68 | +export function toWifString(wif: Wif): Promise<string>; |
| 69 | + |
| 70 | +/** |
| 71 | + * Encode WIF data to a WIF string (synchronous) |
| 72 | + * |
| 73 | + * @param wif - The WIF data to encode |
| 74 | + * @returns The WIF encoded string |
| 75 | + */ |
| 76 | +export function toWifStringSync(wif: Wif): string; |
0 commit comments