diff --git a/README.md b/README.md index 8cb3a38..2b681f9 100644 --- a/README.md +++ b/README.md @@ -558,6 +558,8 @@ Create a base58check encoder/decoder with custom hash functions ### `@exodus/bytes/wif.js` +Wallet Import Format (WIF) encoding and decoding. + ```js import { fromWifString, toWifString } from '@exodus/bytes/wif.js' import { fromWifStringSync, toWifStringSync } from '@exodus/bytes/wif.js' @@ -565,11 +567,34 @@ import { fromWifStringSync, toWifStringSync } from '@exodus/bytes/wif.js' On non-Node.js, requires peer dependency [@noble/hashes](https://www.npmjs.com/package/@noble/hashes) to be installed. -#### `async fromWifString(string, version)` -#### `fromWifStringSync(string, version)` +#### `async fromWifString(string[, version])` + +Decode a WIF string to WIF data + +Returns a promise that resolves to an object with `{ version, privateKey, compressed }`. + +The optional `version` parameter validates the version byte. + +Throws if the WIF string is invalid or version doesn't match. + +#### `fromWifStringSync(string[, version])` + +Decode a WIF string to WIF data (synchronous) + +Returns an object with `{ version, privateKey, compressed }`. + +The optional `version` parameter validates the version byte. + +Throws if the WIF string is invalid or version doesn't match. + #### `async toWifString({ version, privateKey, compressed })` + +Encode WIF data to a WIF string + #### `toWifStringSync({ version, privateKey, compressed })` +Encode WIF data to a WIF string (synchronous) + ### `@exodus/bytes/array.js` TypedArray utils and conversions. diff --git a/package.json b/package.json index fd24be9..df39e9f 100644 --- a/package.json +++ b/package.json @@ -119,7 +119,8 @@ "/utf8.js", "/utf8.d.ts", "/utf8.node.js", - "/wif.js" + "/wif.js", + "/wif.d.ts" ], "main": "index.js", "module": "index.js", @@ -198,7 +199,10 @@ "node": "./utf8.node.js", "default": "./utf8.js" }, - "./wif.js": "./wif.js" + "./wif.js": { + "types": "./wif.d.ts", + "default": "./wif.js" + } }, "react-native": { "./encoding-browser.js": "./encoding-browser.native.js" diff --git a/wif.d.ts b/wif.d.ts new file mode 100644 index 0000000..6e038a1 --- /dev/null +++ b/wif.d.ts @@ -0,0 +1,76 @@ +/** + * Wallet Import Format (WIF) encoding and decoding. + * + * ```js + * import { fromWifString, toWifString } from '@exodus/bytes/wif.js' + * import { fromWifStringSync, toWifStringSync } from '@exodus/bytes/wif.js' + * ``` + * + * On non-Node.js, requires peer dependency [@noble/hashes](https://www.npmjs.com/package/@noble/hashes) to be installed. + * + * @module @exodus/bytes/wif.js + */ + +/// + +import type { Uint8ArrayBuffer } from './array.js'; + +/** + * WIF (Wallet Import Format) data structure + */ +export interface Wif { + /** Network version byte */ + version: number; + /** 32-byte private key */ + privateKey: Uint8ArrayBuffer; + /** Whether the key is compressed */ + compressed: boolean; +} + +/** + * Decode a WIF string to WIF data + * + * Returns a promise that resolves to an object with `{ version, privateKey, compressed }`. + * + * The optional `version` parameter validates the version byte. + * + * Throws if the WIF string is invalid or version doesn't match. + * + * @param string - The WIF encoded string + * @param version - Optional expected version byte to validate against + * @returns The decoded WIF data + * @throws Error if the WIF string is invalid or version doesn't match + */ +export function fromWifString(string: string, version?: number): Promise; + +/** + * Decode a WIF string to WIF data (synchronous) + * + * Returns an object with `{ version, privateKey, compressed }`. + * + * The optional `version` parameter validates the version byte. + * + * Throws if the WIF string is invalid or version doesn't match. + * + * @param string - The WIF encoded string + * @param version - Optional expected version byte to validate against + * @returns The decoded WIF data + * @throws Error if the WIF string is invalid or version doesn't match + */ +export function fromWifStringSync(string: string, version?: number): Wif; + +/** + * Encode WIF data to a WIF string + * + * @param wif - The WIF data to encode + * @returns The WIF encoded string + */ +export function toWifString(wif: Wif): Promise; + +/** + * Encode WIF data to a WIF string (synchronous) + * + * @param wif - The WIF data to encode + * @returns The WIF encoded string + */ +export function toWifStringSync(wif: Wif): string;