Skip to content

Commit 072ed06

Browse files
authored
Merge pull request #2142 from typeofYh/master
docs: Completion and optimization annotation
2 parents 51d55cc + 140bab4 commit 072ed06

File tree

9 files changed

+226
-18
lines changed

9 files changed

+226
-18
lines changed

src/address.d.ts

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,48 @@ export interface Bech32Result {
2626
data: Buffer;
2727
}
2828
/**
29-
* decode address with base58 specification, return address version and address hash if valid
29+
* Decodes a base58check encoded Bitcoin address and returns the version and hash.
30+
*
31+
* @param address - The base58check encoded Bitcoin address to decode.
32+
* @returns An object containing the version and hash of the decoded address.
33+
* @throws {TypeError} If the address is too short or too long.
3034
*/
3135
export declare function fromBase58Check(address: string): Base58CheckResult;
3236
/**
33-
* decode address with bech32 specification, return address version、address prefix and address data if valid
37+
* Converts a Bech32 or Bech32m encoded address to its corresponding data representation.
38+
* @param address - The Bech32 or Bech32m encoded address.
39+
* @returns An object containing the version, prefix, and data of the address.
40+
* @throws {TypeError} If the address uses the wrong encoding.
3441
*/
3542
export declare function fromBech32(address: string): Bech32Result;
3643
/**
37-
* encode address hash to base58 address with version
44+
* Converts a hash to a Base58Check-encoded string.
45+
* @param hash - The hash to be encoded.
46+
* @param version - The version byte to be prepended to the encoded string.
47+
* @returns The Base58Check-encoded string.
3848
*/
3949
export declare function toBase58Check(hash: Buffer, version: number): string;
4050
/**
41-
* encode address hash to bech32 address with version and prefix
51+
* Converts a buffer to a Bech32 or Bech32m encoded string.
52+
* @param data - The buffer to be encoded.
53+
* @param version - The version number to be used in the encoding.
54+
* @param prefix - The prefix string to be used in the encoding.
55+
* @returns The Bech32 or Bech32m encoded string.
4256
*/
4357
export declare function toBech32(data: Buffer, version: number, prefix: string): string;
4458
/**
45-
* decode address from output script with network, return address if matched
59+
* Converts an output script to a Bitcoin address.
60+
* @param output - The output script as a Buffer.
61+
* @param network - The Bitcoin network (optional).
62+
* @returns The Bitcoin address corresponding to the output script.
63+
* @throws If the output script has no matching address.
4664
*/
4765
export declare function fromOutputScript(output: Buffer, network?: Network): string;
4866
/**
49-
* encodes address to output script with network, return output script if address matched
67+
* Converts a Bitcoin address to its corresponding output script.
68+
* @param address - The Bitcoin address to convert.
69+
* @param network - The Bitcoin network to use. Defaults to the Bitcoin network.
70+
* @returns The corresponding output script as a Buffer.
71+
* @throws If the address has an invalid prefix or no matching script.
5072
*/
5173
export declare function toOutputScript(address: string, network?: Network): Buffer;

src/address.js

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ const FUTURE_SEGWIT_VERSION_WARNING =
2323
'End users MUST be warned carefully in the GUI and asked if they wish to proceed ' +
2424
'with caution. Wallets should verify the segwit version from the output of fromBech32, ' +
2525
'then decide when it is safe to use which version of segwit.';
26+
/**
27+
* Converts an output buffer to a future segwit address.
28+
* @param output - The output buffer.
29+
* @param network - The network object.
30+
* @returns The future segwit address.
31+
* @throws {TypeError} If the program length or version is invalid for segwit address.
32+
*/
2633
function _toFutureSegwitAddress(output, network) {
2734
const data = output.slice(2);
2835
if (
@@ -42,7 +49,11 @@ function _toFutureSegwitAddress(output, network) {
4249
return toBech32(data, version, network.bech32);
4350
}
4451
/**
45-
* decode address with base58 specification, return address version and address hash if valid
52+
* Decodes a base58check encoded Bitcoin address and returns the version and hash.
53+
*
54+
* @param address - The base58check encoded Bitcoin address to decode.
55+
* @returns An object containing the version and hash of the decoded address.
56+
* @throws {TypeError} If the address is too short or too long.
4657
*/
4758
function fromBase58Check(address) {
4859
const payload = Buffer.from(bs58check.decode(address));
@@ -55,7 +66,10 @@ function fromBase58Check(address) {
5566
}
5667
exports.fromBase58Check = fromBase58Check;
5768
/**
58-
* decode address with bech32 specification, return address version、address prefix and address data if valid
69+
* Converts a Bech32 or Bech32m encoded address to its corresponding data representation.
70+
* @param address - The Bech32 or Bech32m encoded address.
71+
* @returns An object containing the version, prefix, and data of the address.
72+
* @throws {TypeError} If the address uses the wrong encoding.
5973
*/
6074
function fromBech32(address) {
6175
let result;
@@ -80,7 +94,10 @@ function fromBech32(address) {
8094
}
8195
exports.fromBech32 = fromBech32;
8296
/**
83-
* encode address hash to base58 address with version
97+
* Converts a hash to a Base58Check-encoded string.
98+
* @param hash - The hash to be encoded.
99+
* @param version - The version byte to be prepended to the encoded string.
100+
* @returns The Base58Check-encoded string.
84101
*/
85102
function toBase58Check(hash, version) {
86103
(0, types_1.typeforce)(
@@ -94,7 +111,11 @@ function toBase58Check(hash, version) {
94111
}
95112
exports.toBase58Check = toBase58Check;
96113
/**
97-
* encode address hash to bech32 address with version and prefix
114+
* Converts a buffer to a Bech32 or Bech32m encoded string.
115+
* @param data - The buffer to be encoded.
116+
* @param version - The version number to be used in the encoding.
117+
* @param prefix - The prefix string to be used in the encoding.
118+
* @returns The Bech32 or Bech32m encoded string.
98119
*/
99120
function toBech32(data, version, prefix) {
100121
const words = bech32_1.bech32.toWords(data);
@@ -105,7 +126,11 @@ function toBech32(data, version, prefix) {
105126
}
106127
exports.toBech32 = toBech32;
107128
/**
108-
* decode address from output script with network, return address if matched
129+
* Converts an output script to a Bitcoin address.
130+
* @param output - The output script as a Buffer.
131+
* @param network - The Bitcoin network (optional).
132+
* @returns The Bitcoin address corresponding to the output script.
133+
* @throws If the output script has no matching address.
109134
*/
110135
function fromOutputScript(output, network) {
111136
// TODO: Network
@@ -132,7 +157,11 @@ function fromOutputScript(output, network) {
132157
}
133158
exports.fromOutputScript = fromOutputScript;
134159
/**
135-
* encodes address to output script with network, return output script if address matched
160+
* Converts a Bitcoin address to its corresponding output script.
161+
* @param address - The Bitcoin address to convert.
162+
* @param network - The Bitcoin network to use. Defaults to the Bitcoin network.
163+
* @returns The corresponding output script as a Buffer.
164+
* @throws If the address has an invalid prefix or no matching script.
136165
*/
137166
function toOutputScript(address, network) {
138167
network = network || networks.bitcoin;

src/bip66.d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
/// <reference types="node" />
2+
/**
3+
* Checks if the given buffer is a valid BIP66-encoded signature.
4+
*
5+
* @param buffer - The buffer to check.
6+
* @returns A boolean indicating whether the buffer is a valid BIP66-encoded signature.
7+
*/
28
export declare function check(buffer: Buffer): boolean;
9+
/**
10+
* Decodes a DER-encoded signature buffer and returns the R and S values.
11+
* @param buffer - The DER-encoded signature buffer.
12+
* @returns An object containing the R and S values.
13+
* @throws {Error} If the DER sequence length is too short, too long, or invalid.
14+
* @throws {Error} If the R or S length is zero or invalid.
15+
* @throws {Error} If the R or S value is negative or excessively padded.
16+
*/
317
export declare function decode(buffer: Buffer): {
418
r: Buffer;
519
s: Buffer;

src/bip66.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
// NOTE: SIGHASH byte ignored AND restricted, truncate before use
55
Object.defineProperty(exports, '__esModule', { value: true });
66
exports.encode = exports.decode = exports.check = void 0;
7+
/**
8+
* Checks if the given buffer is a valid BIP66-encoded signature.
9+
*
10+
* @param buffer - The buffer to check.
11+
* @returns A boolean indicating whether the buffer is a valid BIP66-encoded signature.
12+
*/
713
function check(buffer) {
814
if (buffer.length < 8) return false;
915
if (buffer.length > 72) return false;
@@ -25,6 +31,14 @@ function check(buffer) {
2531
return true;
2632
}
2733
exports.check = check;
34+
/**
35+
* Decodes a DER-encoded signature buffer and returns the R and S values.
36+
* @param buffer - The DER-encoded signature buffer.
37+
* @returns An object containing the R and S values.
38+
* @throws {Error} If the DER sequence length is too short, too long, or invalid.
39+
* @throws {Error} If the R or S length is zero or invalid.
40+
* @throws {Error} If the R or S value is negative or excessively padded.
41+
*/
2842
function decode(buffer) {
2943
if (buffer.length < 8) throw new Error('DER sequence length is too short');
3044
if (buffer.length > 72) throw new Error('DER sequence length is too long');

src/payments/bip341.d.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,27 @@ export declare function toHashTree(scriptTree: Taptree): HashTree;
4343
* path is found
4444
*/
4545
export declare function findScriptPath(node: HashTree, hash: Buffer): Buffer[] | undefined;
46+
/**
47+
* Calculates the tapleaf hash for a given Tapleaf object.
48+
* @param leaf - The Tapleaf object to calculate the hash for.
49+
* @returns The tapleaf hash as a Buffer.
50+
*/
4651
export declare function tapleafHash(leaf: Tapleaf): Buffer;
52+
/**
53+
* Computes the taproot tweak hash for a given public key and optional hash.
54+
* If a hash is provided, the public key and hash are concatenated before computing the hash.
55+
* If no hash is provided, only the public key is used to compute the hash.
56+
*
57+
* @param pubKey - The public key buffer.
58+
* @param h - The optional hash buffer.
59+
* @returns The taproot tweak hash.
60+
*/
4761
export declare function tapTweakHash(pubKey: Buffer, h: Buffer | undefined): Buffer;
62+
/**
63+
* Tweak a public key with a given tweak hash.
64+
* @param pubKey - The public key to be tweaked.
65+
* @param h - The tweak hash.
66+
* @returns The tweaked public key or null if the input is invalid.
67+
*/
4868
export declare function tweakKey(pubKey: Buffer, h: Buffer | undefined): TweakedPublicKey | null;
4969
export {};

src/payments/bip341.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ function findScriptPath(node, hash) {
7979
return undefined;
8080
}
8181
exports.findScriptPath = findScriptPath;
82+
/**
83+
* Calculates the tapleaf hash for a given Tapleaf object.
84+
* @param leaf - The Tapleaf object to calculate the hash for.
85+
* @returns The tapleaf hash as a Buffer.
86+
*/
8287
function tapleafHash(leaf) {
8388
const version = leaf.version || exports.LEAF_VERSION_TAPSCRIPT;
8489
return bcrypto.taggedHash(
@@ -90,13 +95,28 @@ function tapleafHash(leaf) {
9095
);
9196
}
9297
exports.tapleafHash = tapleafHash;
98+
/**
99+
* Computes the taproot tweak hash for a given public key and optional hash.
100+
* If a hash is provided, the public key and hash are concatenated before computing the hash.
101+
* If no hash is provided, only the public key is used to compute the hash.
102+
*
103+
* @param pubKey - The public key buffer.
104+
* @param h - The optional hash buffer.
105+
* @returns The taproot tweak hash.
106+
*/
93107
function tapTweakHash(pubKey, h) {
94108
return bcrypto.taggedHash(
95109
'TapTweak',
96110
buffer_1.Buffer.concat(h ? [pubKey, h] : [pubKey]),
97111
);
98112
}
99113
exports.tapTweakHash = tapTweakHash;
114+
/**
115+
* Tweak a public key with a given tweak hash.
116+
* @param pubKey - The public key to be tweaked.
117+
* @param h - The tweak hash.
118+
* @returns The tweaked public key or null if the input is invalid.
119+
*/
100120
function tweakKey(pubKey, h) {
101121
if (!buffer_1.Buffer.isBuffer(pubKey)) return null;
102122
if (pubKey.length !== 32) return null;
@@ -110,9 +130,22 @@ function tweakKey(pubKey, h) {
110130
};
111131
}
112132
exports.tweakKey = tweakKey;
133+
/**
134+
* Computes the TapBranch hash by concatenating two buffers and applying the 'TapBranch' tagged hash algorithm.
135+
*
136+
* @param a - The first buffer.
137+
* @param b - The second buffer.
138+
* @returns The TapBranch hash of the concatenated buffers.
139+
*/
113140
function tapBranchHash(a, b) {
114141
return bcrypto.taggedHash('TapBranch', buffer_1.Buffer.concat([a, b]));
115142
}
143+
/**
144+
* Serializes a script by encoding its length as a varint and concatenating it with the script.
145+
*
146+
* @param s - The script to be serialized.
147+
* @returns The serialized script as a Buffer.
148+
*/
116149
function serializeScript(s) {
117150
const varintLen = bufferutils_1.varuint.encodingLength(s.length);
118151
const buffer = buffer_1.Buffer.allocUnsafe(varintLen); // better

ts_src/address.ts

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ const FUTURE_SEGWIT_VERSION_WARNING: string =
4444
'with caution. Wallets should verify the segwit version from the output of fromBech32, ' +
4545
'then decide when it is safe to use which version of segwit.';
4646

47+
/**
48+
* Converts an output buffer to a future segwit address.
49+
* @param output - The output buffer.
50+
* @param network - The network object.
51+
* @returns The future segwit address.
52+
* @throws {TypeError} If the program length or version is invalid for segwit address.
53+
*/
4754
function _toFutureSegwitAddress(output: Buffer, network: Network): string {
4855
const data = output.slice(2);
4956

@@ -70,7 +77,11 @@ function _toFutureSegwitAddress(output: Buffer, network: Network): string {
7077
}
7178

7279
/**
73-
* decode address with base58 specification, return address version and address hash if valid
80+
* Decodes a base58check encoded Bitcoin address and returns the version and hash.
81+
*
82+
* @param address - The base58check encoded Bitcoin address to decode.
83+
* @returns An object containing the version and hash of the decoded address.
84+
* @throws {TypeError} If the address is too short or too long.
7485
*/
7586
export function fromBase58Check(address: string): Base58CheckResult {
7687
const payload = Buffer.from(bs58check.decode(address));
@@ -86,7 +97,10 @@ export function fromBase58Check(address: string): Base58CheckResult {
8697
}
8798

8899
/**
89-
* decode address with bech32 specification, return address version、address prefix and address data if valid
100+
* Converts a Bech32 or Bech32m encoded address to its corresponding data representation.
101+
* @param address - The Bech32 or Bech32m encoded address.
102+
* @returns An object containing the version, prefix, and data of the address.
103+
* @throws {TypeError} If the address uses the wrong encoding.
90104
*/
91105
export function fromBech32(address: string): Bech32Result {
92106
let result;
@@ -114,7 +128,10 @@ export function fromBech32(address: string): Bech32Result {
114128
}
115129

116130
/**
117-
* encode address hash to base58 address with version
131+
* Converts a hash to a Base58Check-encoded string.
132+
* @param hash - The hash to be encoded.
133+
* @param version - The version byte to be prepended to the encoded string.
134+
* @returns The Base58Check-encoded string.
118135
*/
119136
export function toBase58Check(hash: Buffer, version: number): string {
120137
typeforce(tuple(Hash160bit, UInt8), arguments);
@@ -127,7 +144,11 @@ export function toBase58Check(hash: Buffer, version: number): string {
127144
}
128145

129146
/**
130-
* encode address hash to bech32 address with version and prefix
147+
* Converts a buffer to a Bech32 or Bech32m encoded string.
148+
* @param data - The buffer to be encoded.
149+
* @param version - The version number to be used in the encoding.
150+
* @param prefix - The prefix string to be used in the encoding.
151+
* @returns The Bech32 or Bech32m encoded string.
131152
*/
132153
export function toBech32(
133154
data: Buffer,
@@ -143,7 +164,11 @@ export function toBech32(
143164
}
144165

145166
/**
146-
* decode address from output script with network, return address if matched
167+
* Converts an output script to a Bitcoin address.
168+
* @param output - The output script as a Buffer.
169+
* @param network - The Bitcoin network (optional).
170+
* @returns The Bitcoin address corresponding to the output script.
171+
* @throws If the output script has no matching address.
147172
*/
148173
export function fromOutputScript(output: Buffer, network?: Network): string {
149174
// TODO: Network
@@ -172,7 +197,11 @@ export function fromOutputScript(output: Buffer, network?: Network): string {
172197
}
173198

174199
/**
175-
* encodes address to output script with network, return output script if address matched
200+
* Converts a Bitcoin address to its corresponding output script.
201+
* @param address - The Bitcoin address to convert.
202+
* @param network - The Bitcoin network to use. Defaults to the Bitcoin network.
203+
* @returns The corresponding output script as a Buffer.
204+
* @throws If the address has an invalid prefix or no matching script.
176205
*/
177206
export function toOutputScript(address: string, network?: Network): Buffer {
178207
network = network || networks.bitcoin;

0 commit comments

Comments
 (0)