Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -558,18 +558,43 @@ 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'
```

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.
Expand Down
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"
Expand Down
76 changes: 76 additions & 0 deletions wif.d.ts
Original file line number Diff line number Diff line change
@@ -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
*/

/// <reference types="node" />

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<Wif>;

/**
* 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<string>;

/**
* 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;
Loading