Skip to content

Commit 3dc81f7

Browse files
committed
📄 docs: add other docs and build src
1 parent 94039ae commit 3dc81f7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+805
-225
lines changed

src/address.d.ts

Lines changed: 1 addition & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* bitcoin address decode and encode tools, include base58、bech32 and output script
44
*
5-
* networks support bitcoin、litecoin、bitcoin testnet、litecoin testnet、bitcoin regtest、litecoin regtest and so on
5+
* networks support bitcoin、bitcoin testnet and bitcoin regtest
66
*
77
* addresses support P2PKH、P2SH、P2WPKH、P2WSH、P2TR and so on
88
*
@@ -27,137 +27,25 @@ export interface Bech32Result {
2727
}
2828
/**
2929
* decode address with base58 specification, return address version and address hash if valid
30-
* @case
31-
* ```ts
32-
* // You can test it here and find more case in test/address.spec.ts
33-
* const result = address.fromBase58Check('1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH')
34-
* console.log(JSON.stringify(result))
35-
* // => {"version":0,"hash":{"type":"Buffer","data":[117,30,118,232,25,145,150,212,84,148,28,69,209,179,163,35,241,67,59,214]}}
36-
* ```
3730
*/
3831
export declare function fromBase58Check(address: string): Base58CheckResult;
3932
/**
4033
* decode address with bech32 specification, return address version、address prefix and address data if valid
41-
* @example
42-
* ```ts
43-
* // valid case
44-
* fromBech32('BC1QW508D6QEJXTDG4Y5R3ZARVARY0C5XW7KV8F3T4')
45-
* // => {version: 0, prefix: 'bc', data: <Buffer 75 1e 76 e8 19 91 96 d4 54 94 1c 45 d1 b3 a3 23 f1 43 3b d6>}
46-
*
47-
* // invalid case
48-
* fromBase58Check('bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t5')
49-
* // => Invalid checksum
50-
*
51-
* // invalid case
52-
* fromBase58Check('tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sL5k7')
53-
* // => Mixed-case string
54-
*
55-
* // invalid case
56-
* fromBase58Check('tb1pw508d6qejxtdg4y5r3zarquvzkan')
57-
* // => Excess padding
58-
*
59-
* // invalid case
60-
* fromBase58Check('bc1zw508d6qejxtdg4y5r3zarvaryvq37eag7')
61-
* // => Excess padding
62-
*
63-
* // invalid case
64-
* fromBase58Check('bc1zw508d6qejxtdg4y5r3zarvaryvq37eag7')
65-
* // => Non-zero padding
66-
*
67-
* // invalid case
68-
* fromBase58Check('tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3pjxtptv')
69-
* // => uses wrong encoding
70-
*
71-
* // invalid case
72-
* fromBase58Check('bc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vqh2y7hd')
73-
* // => uses wrong encoding
74-
* ```
7534
*/
7635
export declare function fromBech32(address: string): Bech32Result;
7736
/**
7837
* encode address hash to base58 address with version
79-
*
80-
* @example
81-
* ```ts
82-
* // valid case
83-
* toBase58Check('751e76e8199196d454941c45d1b3a323f1433bd6', 0)
84-
* // => 1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH
85-
* ```
8638
*/
8739
export declare function toBase58Check(hash: Buffer, version: number): string;
8840
/**
8941
* encode address hash to bech32 address with version and prefix
90-
*
91-
* @example
92-
* ```ts
93-
* // valid case
94-
* toBech32('000000c4a5cad46221b2a187905e5266362b99d5e91c6ce24d165dab93e86433', 0, 'tb)
95-
* // => tb1qqqqqp399et2xygdj5xreqhjjvcmzhxw4aywxecjdzew6hylgvsesrxh6hy
96-
* ```
9742
*/
9843
export declare function toBech32(data: Buffer, version: number, prefix: string): string;
9944
/**
10045
* decode address from output script with network, return address if matched
101-
* @example
102-
* ```ts
103-
* // valid case
104-
* fromOutputScript('OP_DUP OP_HASH160 751e76e8199196d454941c45d1b3a323f1433bd6 OP_EQUALVERIFY OP_CHECKSIG', 'bicoin)
105-
* // => 1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH
106-
*
107-
* // invalid case
108-
* fromOutputScript('031f1e68f82112b373f0fe980b3a89d212d2b5c01fb51eb25acb8b4c4b4299ce95 OP_CHECKSIG', undefined)
109-
* // => has no matching Address
110-
*
111-
* // invalid case
112-
* fromOutputScript('OP_TRUE 032487c2a32f7c8d57d2a93906a6457afd00697925b0e6e145d89af6d3bca33016 02308673d16987eaa010e540901cc6fe3695e758c19f46ce604e174dac315e685a OP_2 OP_CHECKMULTISIG', undefined)
113-
* // => has no matching Address
114-
*
115-
* // invalid case
116-
* fromOutputScript('OP_RETURN 06deadbeef03f895a2ad89fb6d696497af486cb7c644a27aa568c7a18dd06113401115185474', undefined)
117-
* // => has no matching Address
118-
*
119-
* // invalid case
120-
* fromOutputScript('OP_0 75', undefined)
121-
* // => has no matching Address
122-
*
123-
* // invalid case
124-
* fromOutputScript('OP_0 751e76e8199196d454941c45d1b3a323f1433bd6751e76e8199196d454941c45d1b3a323f1433bd675', undefined)
125-
* // => has no matching Address
126-
*
127-
* // invalid case
128-
* fromOutputScript('OP_1 75', undefined)
129-
* // => has no matching Address
130-
*
131-
* // invalid case
132-
* fromOutputScript('OP_1 751e76e8199196d454941c45d1b3a323f1433bd6751e76e8199196d454941c45d1b3a323f1433bd675', undefined)
133-
* // => has no matching Address
134-
*
135-
* // invalid case
136-
* fromOutputScript('OP_1 fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f', undefined)
137-
* // => has no matching Address
138-
*
139-
* ```
14046
*/
14147
export declare function fromOutputScript(output: Buffer, network?: Network): string;
14248
/**
14349
* encodes address to output script with network, return output script if address matched
144-
* @example
145-
* ```ts
146-
* // valid case
147-
* toOutputScript('1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH', 'bicoin)
148-
* // => OP_DUP OP_HASH160 751e76e8199196d454941c45d1b3a323f1433bd6 OP_EQUALVERIFY OP_CHECKSIG
149-
*
150-
* // invalid case
151-
* toOutputScript('24kPZCmVgzfkpGdXExy56234MRHrsqQxNWE', undefined)
152-
* // => has no matching Script
153-
*
154-
* // invalid case
155-
* toOutputScript('BC1SW50QGDZ25J', { "bech32": "foo" })
156-
* // => has an invalid prefix
157-
*
158-
* // invalid case
159-
* toOutputScript('bc1rw5uspcuh', undefined)
160-
* // => has no matching Script
161-
* ```
16250
*/
16351
export declare function toOutputScript(address: string, network?: Network): Buffer;

src/address.js

Lines changed: 0 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,6 @@ function _toFutureSegwitAddress(output, network) {
4343
}
4444
/**
4545
* decode address with base58 specification, return address version and address hash if valid
46-
* @case
47-
* ```ts
48-
* // You can test it here and find more case in test/address.spec.ts
49-
* const result = address.fromBase58Check('1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH')
50-
* console.log(JSON.stringify(result))
51-
* // => {"version":0,"hash":{"type":"Buffer","data":[117,30,118,232,25,145,150,212,84,148,28,69,209,179,163,35,241,67,59,214]}}
52-
* ```
5346
*/
5447
function fromBase58Check(address) {
5548
const payload = Buffer.from(bs58check.decode(address));
@@ -63,40 +56,6 @@ function fromBase58Check(address) {
6356
exports.fromBase58Check = fromBase58Check;
6457
/**
6558
* decode address with bech32 specification, return address version、address prefix and address data if valid
66-
* @example
67-
* ```ts
68-
* // valid case
69-
* fromBech32('BC1QW508D6QEJXTDG4Y5R3ZARVARY0C5XW7KV8F3T4')
70-
* // => {version: 0, prefix: 'bc', data: <Buffer 75 1e 76 e8 19 91 96 d4 54 94 1c 45 d1 b3 a3 23 f1 43 3b d6>}
71-
*
72-
* // invalid case
73-
* fromBase58Check('bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t5')
74-
* // => Invalid checksum
75-
*
76-
* // invalid case
77-
* fromBase58Check('tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sL5k7')
78-
* // => Mixed-case string
79-
*
80-
* // invalid case
81-
* fromBase58Check('tb1pw508d6qejxtdg4y5r3zarquvzkan')
82-
* // => Excess padding
83-
*
84-
* // invalid case
85-
* fromBase58Check('bc1zw508d6qejxtdg4y5r3zarvaryvq37eag7')
86-
* // => Excess padding
87-
*
88-
* // invalid case
89-
* fromBase58Check('bc1zw508d6qejxtdg4y5r3zarvaryvq37eag7')
90-
* // => Non-zero padding
91-
*
92-
* // invalid case
93-
* fromBase58Check('tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3pjxtptv')
94-
* // => uses wrong encoding
95-
*
96-
* // invalid case
97-
* fromBase58Check('bc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vqh2y7hd')
98-
* // => uses wrong encoding
99-
* ```
10059
*/
10160
function fromBech32(address) {
10261
let result;
@@ -122,13 +81,6 @@ function fromBech32(address) {
12281
exports.fromBech32 = fromBech32;
12382
/**
12483
* encode address hash to base58 address with version
125-
*
126-
* @example
127-
* ```ts
128-
* // valid case
129-
* toBase58Check('751e76e8199196d454941c45d1b3a323f1433bd6', 0)
130-
* // => 1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH
131-
* ```
13284
*/
13385
function toBase58Check(hash, version) {
13486
(0, types_1.typeforce)(
@@ -143,13 +95,6 @@ function toBase58Check(hash, version) {
14395
exports.toBase58Check = toBase58Check;
14496
/**
14597
* encode address hash to bech32 address with version and prefix
146-
*
147-
* @example
148-
* ```ts
149-
* // valid case
150-
* toBech32('000000c4a5cad46221b2a187905e5266362b99d5e91c6ce24d165dab93e86433', 0, 'tb)
151-
* // => tb1qqqqqp399et2xygdj5xreqhjjvcmzhxw4aywxecjdzew6hylgvsesrxh6hy
152-
* ```
15398
*/
15499
function toBech32(data, version, prefix) {
155100
const words = bech32_1.bech32.toWords(data);
@@ -161,45 +106,6 @@ function toBech32(data, version, prefix) {
161106
exports.toBech32 = toBech32;
162107
/**
163108
* decode address from output script with network, return address if matched
164-
* @example
165-
* ```ts
166-
* // valid case
167-
* fromOutputScript('OP_DUP OP_HASH160 751e76e8199196d454941c45d1b3a323f1433bd6 OP_EQUALVERIFY OP_CHECKSIG', 'bicoin)
168-
* // => 1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH
169-
*
170-
* // invalid case
171-
* fromOutputScript('031f1e68f82112b373f0fe980b3a89d212d2b5c01fb51eb25acb8b4c4b4299ce95 OP_CHECKSIG', undefined)
172-
* // => has no matching Address
173-
*
174-
* // invalid case
175-
* fromOutputScript('OP_TRUE 032487c2a32f7c8d57d2a93906a6457afd00697925b0e6e145d89af6d3bca33016 02308673d16987eaa010e540901cc6fe3695e758c19f46ce604e174dac315e685a OP_2 OP_CHECKMULTISIG', undefined)
176-
* // => has no matching Address
177-
*
178-
* // invalid case
179-
* fromOutputScript('OP_RETURN 06deadbeef03f895a2ad89fb6d696497af486cb7c644a27aa568c7a18dd06113401115185474', undefined)
180-
* // => has no matching Address
181-
*
182-
* // invalid case
183-
* fromOutputScript('OP_0 75', undefined)
184-
* // => has no matching Address
185-
*
186-
* // invalid case
187-
* fromOutputScript('OP_0 751e76e8199196d454941c45d1b3a323f1433bd6751e76e8199196d454941c45d1b3a323f1433bd675', undefined)
188-
* // => has no matching Address
189-
*
190-
* // invalid case
191-
* fromOutputScript('OP_1 75', undefined)
192-
* // => has no matching Address
193-
*
194-
* // invalid case
195-
* fromOutputScript('OP_1 751e76e8199196d454941c45d1b3a323f1433bd6751e76e8199196d454941c45d1b3a323f1433bd675', undefined)
196-
* // => has no matching Address
197-
*
198-
* // invalid case
199-
* fromOutputScript('OP_1 fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f', undefined)
200-
* // => has no matching Address
201-
*
202-
* ```
203109
*/
204110
function fromOutputScript(output, network) {
205111
// TODO: Network
@@ -227,24 +133,6 @@ function fromOutputScript(output, network) {
227133
exports.fromOutputScript = fromOutputScript;
228134
/**
229135
* encodes address to output script with network, return output script if address matched
230-
* @example
231-
* ```ts
232-
* // valid case
233-
* toOutputScript('1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH', 'bicoin)
234-
* // => OP_DUP OP_HASH160 751e76e8199196d454941c45d1b3a323f1433bd6 OP_EQUALVERIFY OP_CHECKSIG
235-
*
236-
* // invalid case
237-
* toOutputScript('24kPZCmVgzfkpGdXExy56234MRHrsqQxNWE', undefined)
238-
* // => has no matching Script
239-
*
240-
* // invalid case
241-
* toOutputScript('BC1SW50QGDZ25J', { "bech32": "foo" })
242-
* // => has an invalid prefix
243-
*
244-
* // invalid case
245-
* toOutputScript('bc1rw5uspcuh', undefined)
246-
* // => has no matching Script
247-
* ```
248136
*/
249137
function toOutputScript(address, network) {
250138
network = network || networks.bitcoin;

src/bufferutils.d.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,20 @@
22
import * as varuint from 'varuint-bitcoin';
33
export { varuint };
44
export declare function readUInt64LE(buffer: Buffer, offset: number): number;
5+
/**
6+
* Writes a 64-bit unsigned integer in little-endian format to the specified buffer at the given offset.
7+
*
8+
* @param buffer - The buffer to write the value to.
9+
* @param value - The 64-bit unsigned integer value to write.
10+
* @param offset - The offset in the buffer where the value should be written.
11+
* @returns The new offset after writing the value.
12+
*/
513
export declare function writeUInt64LE(buffer: Buffer, value: number, offset: number): number;
14+
/**
15+
* Reverses the order of bytes in a buffer.
16+
* @param buffer - The buffer to reverse.
17+
* @returns A new buffer with the bytes reversed.
18+
*/
619
export declare function reverseBuffer(buffer: Buffer): Buffer;
720
export declare function cloneBuffer(buffer: Buffer): Buffer;
821
/**

src/bufferutils.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,26 @@ function readUInt64LE(buffer, offset) {
3030
return b + a;
3131
}
3232
exports.readUInt64LE = readUInt64LE;
33+
/**
34+
* Writes a 64-bit unsigned integer in little-endian format to the specified buffer at the given offset.
35+
*
36+
* @param buffer - The buffer to write the value to.
37+
* @param value - The 64-bit unsigned integer value to write.
38+
* @param offset - The offset in the buffer where the value should be written.
39+
* @returns The new offset after writing the value.
40+
*/
3341
function writeUInt64LE(buffer, value, offset) {
3442
verifuint(value, 0x001fffffffffffff);
3543
buffer.writeInt32LE(value & -1, offset);
3644
buffer.writeUInt32LE(Math.floor(value / 0x100000000), offset + 4);
3745
return offset + 8;
3846
}
3947
exports.writeUInt64LE = writeUInt64LE;
48+
/**
49+
* Reverses the order of bytes in a buffer.
50+
* @param buffer - The buffer to reverse.
51+
* @returns A new buffer with the bytes reversed.
52+
*/
4053
function reverseBuffer(buffer) {
4154
if (buffer.length < 1) return buffer;
4255
let j = buffer.length - 1;

src/crypto.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ type TaggedHashPrefixes = {
1010
[key in TaggedHashPrefix]: Buffer;
1111
};
1212
/** An object mapping tags to their tagged hash prefix of [SHA256(tag) | SHA256(tag)] */
13+
/**
14+
* Defines the tagged hash prefixes used in the crypto module.
15+
*/
1316
export declare const TAGGED_HASH_PREFIXES: TaggedHashPrefixes;
1417
export declare function taggedHash(prefix: TaggedHashPrefix, data: Buffer): Buffer;
1518
export {};

src/crypto.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ exports.taggedHash =
99
exports.sha1 =
1010
exports.ripemd160 =
1111
void 0;
12+
/**
13+
* A module for hashing functions.
14+
* include ripemd160、sha1、sha256、hash160、hash256、taggedHash
15+
*
16+
* @packageDocumentation
17+
*/
1218
const ripemd160_1 = require('@noble/hashes/ripemd160');
1319
const sha1_1 = require('@noble/hashes/sha1');
1420
const sha256_1 = require('@noble/hashes/sha256');
@@ -48,6 +54,9 @@ exports.TAGS = [
4854
'KeyAgg coefficient',
4955
];
5056
/** An object mapping tags to their tagged hash prefix of [SHA256(tag) | SHA256(tag)] */
57+
/**
58+
* Defines the tagged hash prefixes used in the crypto module.
59+
*/
5160
exports.TAGGED_HASH_PREFIXES = {
5261
'BIP0340/challenge': Buffer.from([
5362
123, 181, 45, 122, 159, 239, 88, 50, 62, 177, 191, 122, 64, 125, 179, 130,

src/ecc_lib.d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
11
import { TinySecp256k1Interface } from './types';
2+
/**
3+
* Initializes the ECC library with the provided instance.
4+
* If `eccLib` is `undefined`, the library will be cleared.
5+
* If `eccLib` is a new instance, it will be verified before setting it as the active library.
6+
*
7+
* @param eccLib The instance of the ECC library to initialize.
8+
*/
29
export declare function initEccLib(eccLib: TinySecp256k1Interface | undefined): void;
10+
/**
11+
* Retrieves the ECC Library instance.
12+
* Throws an error if the ECC Library is not provided.
13+
* You must call initEccLib() with a valid TinySecp256k1Interface instance before calling this function.
14+
* @returns The ECC Library instance.
15+
* @throws Error if the ECC Library is not provided.
16+
*/
317
export declare function getEccLib(): TinySecp256k1Interface;

0 commit comments

Comments
 (0)