Skip to content

Commit 4f4aad1

Browse files
CopilotChALkeR
andauthored
feat: add TypeScript types for wif.js (#35)
Co-authored-by: Nikita Skovoroda <chalkerx@gmail.com>
1 parent 8790664 commit 4f4aad1

3 files changed

Lines changed: 109 additions & 4 deletions

File tree

README.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -558,18 +558,43 @@ Create a base58check encoder/decoder with custom hash functions
558558

559559
### `@exodus/bytes/wif.js`
560560

561+
Wallet Import Format (WIF) encoding and decoding.
562+
561563
```js
562564
import { fromWifString, toWifString } from '@exodus/bytes/wif.js'
563565
import { fromWifStringSync, toWifStringSync } from '@exodus/bytes/wif.js'
564566
```
565567

566568
On non-Node.js, requires peer dependency [@noble/hashes](https://www.npmjs.com/package/@noble/hashes) to be installed.
567569

568-
#### `async fromWifString(string, version)`
569-
#### `fromWifStringSync(string, version)`
570+
#### `async fromWifString(string[, version])`
571+
572+
Decode a WIF string to WIF data
573+
574+
Returns a promise that resolves to an object with `{ version, privateKey, compressed }`.
575+
576+
The optional `version` parameter validates the version byte.
577+
578+
Throws if the WIF string is invalid or version doesn't match.
579+
580+
#### `fromWifStringSync(string[, version])`
581+
582+
Decode a WIF string to WIF data (synchronous)
583+
584+
Returns an object with `{ version, privateKey, compressed }`.
585+
586+
The optional `version` parameter validates the version byte.
587+
588+
Throws if the WIF string is invalid or version doesn't match.
589+
570590
#### `async toWifString({ version, privateKey, compressed })`
591+
592+
Encode WIF data to a WIF string
593+
571594
#### `toWifStringSync({ version, privateKey, compressed })`
572595

596+
Encode WIF data to a WIF string (synchronous)
597+
573598
### `@exodus/bytes/array.js`
574599

575600
TypedArray utils and conversions.

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@
119119
"/utf8.js",
120120
"/utf8.d.ts",
121121
"/utf8.node.js",
122-
"/wif.js"
122+
"/wif.js",
123+
"/wif.d.ts"
123124
],
124125
"main": "index.js",
125126
"module": "index.js",
@@ -198,7 +199,10 @@
198199
"node": "./utf8.node.js",
199200
"default": "./utf8.js"
200201
},
201-
"./wif.js": "./wif.js"
202+
"./wif.js": {
203+
"types": "./wif.d.ts",
204+
"default": "./wif.js"
205+
}
202206
},
203207
"react-native": {
204208
"./encoding-browser.js": "./encoding-browser.native.js"

wif.d.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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

Comments
 (0)