Skip to content

Commit 3cdf612

Browse files
committed
Merge branch 'master' into feat/hybrid
2 parents 579538f + f06bbce commit 3cdf612

File tree

13 files changed

+310
-39
lines changed

13 files changed

+310
-39
lines changed

.github/workflows/main_ci.yml

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
strategy:
2222
fail-fast: false
2323
matrix:
24-
node-version: [14, 'lts/*']
24+
node-version: [18, 'lts/*']
2525
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
2626
steps:
2727
- uses: actions/checkout@ec3a7ce113134d7a93b817d10a8272cb61118579 # v2
@@ -38,7 +38,7 @@ jobs:
3838
strategy:
3939
fail-fast: false
4040
matrix:
41-
node-version: [14, 'lts/*']
41+
node-version: [18, 'lts/*']
4242
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
4343
services:
4444
regtest:
@@ -61,17 +61,6 @@ jobs:
6161
#####################
6262
# Jobs without matrix
6363
#####################
64-
audit:
65-
runs-on: ubuntu-latest
66-
steps:
67-
- uses: actions/checkout@ec3a7ce113134d7a93b817d10a8272cb61118579 # v2
68-
- uses: actions/setup-node@eeb10cff27034e7acf239c5d29f62154018672fd # v3
69-
with:
70-
node-version: 'lts/*'
71-
registry-url: https://registry.npmjs.org/
72-
cache: 'npm'
73-
- run: npm ci
74-
- run: npm run audit
7564
coverage:
7665
runs-on: ubuntu-latest
7766
steps:

src/cjs/address.cjs

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ const FUTURE_SEGWIT_VERSION_WARNING =
7373
'End users MUST be warned carefully in the GUI and asked if they wish to proceed ' +
7474
'with caution. Wallets should verify the segwit version from the output of fromBech32, ' +
7575
'then decide when it is safe to use which version of segwit.';
76+
/**
77+
* Converts an output buffer to a future segwit address.
78+
* @param output - The output buffer.
79+
* @param network - The network object.
80+
* @returns The future segwit address.
81+
* @throws {TypeError} If the program length or version is invalid for segwit address.
82+
*/
7683
function _toFutureSegwitAddress(output, network) {
7784
const data = output.slice(2);
7885
if (
@@ -92,7 +99,11 @@ function _toFutureSegwitAddress(output, network) {
9299
return toBech32(data, version, network.bech32);
93100
}
94101
/**
95-
* decode address with base58 specification, return address version and address hash if valid
102+
* Decodes a base58check encoded Bitcoin address and returns the version and hash.
103+
*
104+
* @param address - The base58check encoded Bitcoin address to decode.
105+
* @returns An object containing the version and hash of the decoded address.
106+
* @throws {TypeError} If the address is too short or too long.
96107
*/
97108
function fromBase58Check(address) {
98109
const payload = bs58check_1.default.decode(address);
@@ -104,7 +115,10 @@ function fromBase58Check(address) {
104115
return { version, hash };
105116
}
106117
/**
107-
* decode address with bech32 specification, return address version、address prefix and address data if valid
118+
* Converts a Bech32 or Bech32m encoded address to its corresponding data representation.
119+
* @param address - The Bech32 or Bech32m encoded address.
120+
* @returns An object containing the version, prefix, and data of the address.
121+
* @throws {TypeError} If the address uses the wrong encoding.
108122
*/
109123
function fromBech32(address) {
110124
let result;
@@ -128,7 +142,10 @@ function fromBech32(address) {
128142
};
129143
}
130144
/**
131-
* encode address hash to base58 address with version
145+
* Converts a hash to a Base58Check-encoded string.
146+
* @param hash - The hash to be encoded.
147+
* @param version - The version byte to be prepended to the encoded string.
148+
* @returns The Base58Check-encoded string.
132149
*/
133150
function toBase58Check(hash, version) {
134151
v.parse(v.tuple([types_js_1.Hash160bitSchema, types_js_1.UInt8Schema]), [
@@ -141,7 +158,11 @@ function toBase58Check(hash, version) {
141158
return bs58check_1.default.encode(payload);
142159
}
143160
/**
144-
* encode address hash to bech32 address with version and prefix
161+
* Converts a buffer to a Bech32 or Bech32m encoded string.
162+
* @param data - The buffer to be encoded.
163+
* @param version - The version number to be used in the encoding.
164+
* @param prefix - The prefix string to be used in the encoding.
165+
* @returns The Bech32 or Bech32m encoded string.
145166
*/
146167
function toBech32(data, version, prefix) {
147168
const words = bech32_1.bech32.toWords(data);
@@ -151,7 +172,11 @@ function toBech32(data, version, prefix) {
151172
: bech32_1.bech32m.encode(prefix, words);
152173
}
153174
/**
154-
* decode address from output script with network, return address if matched
175+
* Converts an output script to a Bitcoin address.
176+
* @param output - The output script as a Buffer.
177+
* @param network - The Bitcoin network (optional).
178+
* @returns The Bitcoin address corresponding to the output script.
179+
* @throws If the output script has no matching address.
155180
*/
156181
function fromOutputScript(output, network) {
157182
// TODO: Network
@@ -177,7 +202,11 @@ function fromOutputScript(output, network) {
177202
throw new Error(bscript.toASM(output) + ' has no matching Address');
178203
}
179204
/**
180-
* encodes address to output script with network, return output script if address matched
205+
* Converts a Bitcoin address to its corresponding output script.
206+
* @param address - The Bitcoin address to convert.
207+
* @param network - The Bitcoin network to use. Defaults to the Bitcoin network.
208+
* @returns The corresponding output script as a Buffer.
209+
* @throws If the address has an invalid prefix or no matching script.
181210
*/
182211
function toOutputScript(address, network) {
183212
network = network || networks.bitcoin;

src/cjs/address.d.ts

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

src/cjs/bip66.cjs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ Object.defineProperty(exports, '__esModule', { value: true });
66
exports.check = check;
77
exports.decode = decode;
88
exports.encode = encode;
9+
/**
10+
* Checks if the given buffer is a valid BIP66-encoded signature.
11+
*
12+
* @param buffer - The buffer to check.
13+
* @returns A boolean indicating whether the buffer is a valid BIP66-encoded signature.
14+
*/
915
function check(buffer) {
1016
if (buffer.length < 8) return false;
1117
if (buffer.length > 72) return false;
@@ -26,6 +32,14 @@ function check(buffer) {
2632
return false;
2733
return true;
2834
}
35+
/**
36+
* Decodes a DER-encoded signature buffer and returns the R and S values.
37+
* @param buffer - The DER-encoded signature buffer.
38+
* @returns An object containing the R and S values.
39+
* @throws {Error} If the DER sequence length is too short, too long, or invalid.
40+
* @throws {Error} If the R or S length is zero or invalid.
41+
* @throws {Error} If the R or S value is negative or excessively padded.
42+
*/
2943
function decode(buffer) {
3044
if (buffer.length < 8) throw new Error('DER sequence length is too short');
3145
if (buffer.length > 72) throw new Error('DER sequence length is too long');

src/cjs/bip66.d.ts

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

src/cjs/payments/bip341.cjs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,19 +119,39 @@ function findScriptPath(node, hash) {
119119
}
120120
return undefined;
121121
}
122+
/**
123+
* Calculates the tapleaf hash for a given Tapleaf object.
124+
* @param leaf - The Tapleaf object to calculate the hash for.
125+
* @returns The tapleaf hash as a Buffer.
126+
*/
122127
function tapleafHash(leaf) {
123128
const version = leaf.version || exports.LEAF_VERSION_TAPSCRIPT;
124129
return bcrypto.taggedHash(
125130
'TapLeaf',
126131
tools.concat([Uint8Array.from([version]), serializeScript(leaf.output)]),
127132
);
128133
}
134+
/**
135+
* Computes the taproot tweak hash for a given public key and optional hash.
136+
* If a hash is provided, the public key and hash are concatenated before computing the hash.
137+
* If no hash is provided, only the public key is used to compute the hash.
138+
*
139+
* @param pubKey - The public key buffer.
140+
* @param h - The optional hash buffer.
141+
* @returns The taproot tweak hash.
142+
*/
129143
function tapTweakHash(pubKey, h) {
130144
return bcrypto.taggedHash(
131145
'TapTweak',
132146
tools.concat(h ? [pubKey, h] : [pubKey]),
133147
);
134148
}
149+
/**
150+
* Tweak a public key with a given tweak hash.
151+
* @param pubKey - The public key to be tweaked.
152+
* @param h - The tweak hash.
153+
* @returns The tweaked public key or null if the input is invalid.
154+
*/
135155
function tweakKey(pubKey, h) {
136156
if (!(pubKey instanceof Uint8Array)) return null;
137157
if (pubKey.length !== 32) return null;
@@ -147,9 +167,22 @@ function tweakKey(pubKey, h) {
147167
x: Uint8Array.from(res.xOnlyPubkey),
148168
};
149169
}
170+
/**
171+
* Computes the TapBranch hash by concatenating two buffers and applying the 'TapBranch' tagged hash algorithm.
172+
*
173+
* @param a - The first buffer.
174+
* @param b - The second buffer.
175+
* @returns The TapBranch hash of the concatenated buffers.
176+
*/
150177
function tapBranchHash(a, b) {
151178
return bcrypto.taggedHash('TapBranch', tools.concat([a, b]));
152179
}
180+
/**
181+
* Serializes a script by encoding its length as a varint and concatenating it with the script.
182+
*
183+
* @param s - The script to be serialized.
184+
* @returns The serialized script as a Buffer.
185+
*/
153186
function serializeScript(s) {
154187
/* global BigInt */
155188
const varintLen = bufferutils_js_1.varuint.encodingLength(s.length);

src/cjs/payments/bip341.d.ts

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

0 commit comments

Comments
 (0)