Skip to content

Commit ec1f51c

Browse files
authored
Merge pull request #2188 from SpiderMan670/master
doc: improve document of crypto
2 parents 68ed198 + 2c3c662 commit ec1f51c

File tree

4 files changed

+137
-8
lines changed

4 files changed

+137
-8
lines changed

src/cjs/crypto.cjs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,21 @@ exports.taggedHash = taggedHash;
5757
const ripemd160_1 = require('@noble/hashes/ripemd160');
5858
const sha256_1 = require('@noble/hashes/sha256');
5959
const tools = __importStar(require('uint8array-tools'));
60+
/**
61+
* Computes the HASH160 (RIPEMD-160 after SHA-256) of the given buffer.
62+
*
63+
* @param buffer - The input data to be hashed.
64+
* @returns The HASH160 of the input buffer.
65+
*/
6066
function hash160(buffer) {
6167
return (0, ripemd160_1.ripemd160)((0, sha256_1.sha256)(buffer));
6268
}
69+
/**
70+
* Computes the double SHA-256 hash of the given buffer.
71+
*
72+
* @param buffer - The input data to be hashed.
73+
* @returns The double SHA-256 hash of the input buffer.
74+
*/
6375
function hash256(buffer) {
6476
return (0, sha256_1.sha256)((0, sha256_1.sha256)(buffer));
6577
}
@@ -74,9 +86,22 @@ exports.TAGS = [
7486
'KeyAgg list',
7587
'KeyAgg coefficient',
7688
];
77-
/** An object mapping tags to their tagged hash prefix of [SHA256(tag) | SHA256(tag)] */
7889
/**
79-
* Defines the tagged hash prefixes used in the crypto module.
90+
* A collection of tagged hash prefixes used in various BIP (Bitcoin Improvement Proposals)
91+
* and Taproot-related operations. Each prefix is represented as a `Uint8Array`.
92+
*
93+
* @constant
94+
* @type {TaggedHashPrefixes}
95+
*
96+
* @property {'BIP0340/challenge'} - Prefix for BIP0340 challenge.
97+
* @property {'BIP0340/aux'} - Prefix for BIP0340 auxiliary data.
98+
* @property {'BIP0340/nonce'} - Prefix for BIP0340 nonce.
99+
* @property {TapLeaf} - Prefix for Taproot leaf.
100+
* @property {TapBranch} - Prefix for Taproot branch.
101+
* @property {TapSighash} - Prefix for Taproot sighash.
102+
* @property {TapTweak} - Prefix for Taproot tweak.
103+
* @property {'KeyAgg list'} - Prefix for key aggregation list.
104+
* @property {'KeyAgg coefficient'} - Prefix for key aggregation coefficient.
80105
*/
81106
exports.TAGGED_HASH_PREFIXES = {
82107
'BIP0340/challenge': Uint8Array.from([
@@ -134,6 +159,13 @@ exports.TAGGED_HASH_PREFIXES = {
134159
78, 214, 66, 114, 129, 192, 145, 0, 249, 77, 205, 82, 201, 129,
135160
]),
136161
};
162+
/**
163+
* Computes a tagged hash using the specified prefix and data.
164+
*
165+
* @param prefix - The prefix to use for the tagged hash. This should be one of the values from the `TaggedHashPrefix` enum.
166+
* @param data - The data to hash, provided as a `Uint8Array`.
167+
* @returns The resulting tagged hash as a `Uint8Array`.
168+
*/
137169
function taggedHash(prefix, data) {
138170
return (0, sha256_1.sha256)(
139171
tools.concat([exports.TAGGED_HASH_PREFIXES[prefix], data]),

src/cjs/crypto.d.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,46 @@
1+
/**
2+
* Computes the HASH160 (RIPEMD-160 after SHA-256) of the given buffer.
3+
*
4+
* @param buffer - The input data to be hashed.
5+
* @returns The HASH160 of the input buffer.
6+
*/
17
export declare function hash160(buffer: Uint8Array): Uint8Array;
8+
/**
9+
* Computes the double SHA-256 hash of the given buffer.
10+
*
11+
* @param buffer - The input data to be hashed.
12+
* @returns The double SHA-256 hash of the input buffer.
13+
*/
214
export declare function hash256(buffer: Uint8Array): Uint8Array;
315
export declare const TAGS: readonly ["BIP0340/challenge", "BIP0340/aux", "BIP0340/nonce", "TapLeaf", "TapBranch", "TapSighash", "TapTweak", "KeyAgg list", "KeyAgg coefficient"];
416
export type TaggedHashPrefix = (typeof TAGS)[number];
517
type TaggedHashPrefixes = {
618
[key in TaggedHashPrefix]: Uint8Array;
719
};
8-
/** An object mapping tags to their tagged hash prefix of [SHA256(tag) | SHA256(tag)] */
920
/**
10-
* Defines the tagged hash prefixes used in the crypto module.
21+
* A collection of tagged hash prefixes used in various BIP (Bitcoin Improvement Proposals)
22+
* and Taproot-related operations. Each prefix is represented as a `Uint8Array`.
23+
*
24+
* @constant
25+
* @type {TaggedHashPrefixes}
26+
*
27+
* @property {'BIP0340/challenge'} - Prefix for BIP0340 challenge.
28+
* @property {'BIP0340/aux'} - Prefix for BIP0340 auxiliary data.
29+
* @property {'BIP0340/nonce'} - Prefix for BIP0340 nonce.
30+
* @property {TapLeaf} - Prefix for Taproot leaf.
31+
* @property {TapBranch} - Prefix for Taproot branch.
32+
* @property {TapSighash} - Prefix for Taproot sighash.
33+
* @property {TapTweak} - Prefix for Taproot tweak.
34+
* @property {'KeyAgg list'} - Prefix for key aggregation list.
35+
* @property {'KeyAgg coefficient'} - Prefix for key aggregation coefficient.
1136
*/
1237
export declare const TAGGED_HASH_PREFIXES: TaggedHashPrefixes;
38+
/**
39+
* Computes a tagged hash using the specified prefix and data.
40+
*
41+
* @param prefix - The prefix to use for the tagged hash. This should be one of the values from the `TaggedHashPrefix` enum.
42+
* @param data - The data to hash, provided as a `Uint8Array`.
43+
* @returns The resulting tagged hash as a `Uint8Array`.
44+
*/
1345
export declare function taggedHash(prefix: TaggedHashPrefix, data: Uint8Array): Uint8Array;
1446
export {};

src/esm/crypto.js

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,21 @@
77
import { ripemd160 } from '@noble/hashes/ripemd160';
88
import { sha256 } from '@noble/hashes/sha256';
99
import * as tools from 'uint8array-tools';
10+
/**
11+
* Computes the HASH160 (RIPEMD-160 after SHA-256) of the given buffer.
12+
*
13+
* @param buffer - The input data to be hashed.
14+
* @returns The HASH160 of the input buffer.
15+
*/
1016
export function hash160(buffer) {
1117
return ripemd160(sha256(buffer));
1218
}
19+
/**
20+
* Computes the double SHA-256 hash of the given buffer.
21+
*
22+
* @param buffer - The input data to be hashed.
23+
* @returns The double SHA-256 hash of the input buffer.
24+
*/
1325
export function hash256(buffer) {
1426
return sha256(sha256(buffer));
1527
}
@@ -24,9 +36,22 @@ export const TAGS = [
2436
'KeyAgg list',
2537
'KeyAgg coefficient',
2638
];
27-
/** An object mapping tags to their tagged hash prefix of [SHA256(tag) | SHA256(tag)] */
2839
/**
29-
* Defines the tagged hash prefixes used in the crypto module.
40+
* A collection of tagged hash prefixes used in various BIP (Bitcoin Improvement Proposals)
41+
* and Taproot-related operations. Each prefix is represented as a `Uint8Array`.
42+
*
43+
* @constant
44+
* @type {TaggedHashPrefixes}
45+
*
46+
* @property {'BIP0340/challenge'} - Prefix for BIP0340 challenge.
47+
* @property {'BIP0340/aux'} - Prefix for BIP0340 auxiliary data.
48+
* @property {'BIP0340/nonce'} - Prefix for BIP0340 nonce.
49+
* @property {TapLeaf} - Prefix for Taproot leaf.
50+
* @property {TapBranch} - Prefix for Taproot branch.
51+
* @property {TapSighash} - Prefix for Taproot sighash.
52+
* @property {TapTweak} - Prefix for Taproot tweak.
53+
* @property {'KeyAgg list'} - Prefix for key aggregation list.
54+
* @property {'KeyAgg coefficient'} - Prefix for key aggregation coefficient.
3055
*/
3156
export const TAGGED_HASH_PREFIXES = {
3257
'BIP0340/challenge': Uint8Array.from([
@@ -84,6 +109,13 @@ export const TAGGED_HASH_PREFIXES = {
84109
78, 214, 66, 114, 129, 192, 145, 0, 249, 77, 205, 82, 201, 129,
85110
]),
86111
};
112+
/**
113+
* Computes a tagged hash using the specified prefix and data.
114+
*
115+
* @param prefix - The prefix to use for the tagged hash. This should be one of the values from the `TaggedHashPrefix` enum.
116+
* @param data - The data to hash, provided as a `Uint8Array`.
117+
* @returns The resulting tagged hash as a `Uint8Array`.
118+
*/
87119
export function taggedHash(prefix, data) {
88120
return sha256(tools.concat([TAGGED_HASH_PREFIXES[prefix], data]));
89121
}

ts_src/crypto.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,22 @@ import { ripemd160 } from '@noble/hashes/ripemd160';
88
import { sha256 } from '@noble/hashes/sha256';
99
import * as tools from 'uint8array-tools';
1010

11+
/**
12+
* Computes the HASH160 (RIPEMD-160 after SHA-256) of the given buffer.
13+
*
14+
* @param buffer - The input data to be hashed.
15+
* @returns The HASH160 of the input buffer.
16+
*/
1117
export function hash160(buffer: Uint8Array): Uint8Array {
1218
return ripemd160(sha256(buffer));
1319
}
1420

21+
/**
22+
* Computes the double SHA-256 hash of the given buffer.
23+
*
24+
* @param buffer - The input data to be hashed.
25+
* @returns The double SHA-256 hash of the input buffer.
26+
*/
1527
export function hash256(buffer: Uint8Array): Uint8Array {
1628
return sha256(sha256(buffer));
1729
}
@@ -31,9 +43,23 @@ export type TaggedHashPrefix = (typeof TAGS)[number];
3143
type TaggedHashPrefixes = {
3244
[key in TaggedHashPrefix]: Uint8Array;
3345
};
34-
/** An object mapping tags to their tagged hash prefix of [SHA256(tag) | SHA256(tag)] */
46+
3547
/**
36-
* Defines the tagged hash prefixes used in the crypto module.
48+
* A collection of tagged hash prefixes used in various BIP (Bitcoin Improvement Proposals)
49+
* and Taproot-related operations. Each prefix is represented as a `Uint8Array`.
50+
*
51+
* @constant
52+
* @type {TaggedHashPrefixes}
53+
*
54+
* @property {'BIP0340/challenge'} - Prefix for BIP0340 challenge.
55+
* @property {'BIP0340/aux'} - Prefix for BIP0340 auxiliary data.
56+
* @property {'BIP0340/nonce'} - Prefix for BIP0340 nonce.
57+
* @property {TapLeaf} - Prefix for Taproot leaf.
58+
* @property {TapBranch} - Prefix for Taproot branch.
59+
* @property {TapSighash} - Prefix for Taproot sighash.
60+
* @property {TapTweak} - Prefix for Taproot tweak.
61+
* @property {'KeyAgg list'} - Prefix for key aggregation list.
62+
* @property {'KeyAgg coefficient'} - Prefix for key aggregation coefficient.
3763
*/
3864
export const TAGGED_HASH_PREFIXES: TaggedHashPrefixes = {
3965
'BIP0340/challenge': Uint8Array.from([
@@ -92,6 +118,13 @@ export const TAGGED_HASH_PREFIXES: TaggedHashPrefixes = {
92118
]),
93119
};
94120

121+
/**
122+
* Computes a tagged hash using the specified prefix and data.
123+
*
124+
* @param prefix - The prefix to use for the tagged hash. This should be one of the values from the `TaggedHashPrefix` enum.
125+
* @param data - The data to hash, provided as a `Uint8Array`.
126+
* @returns The resulting tagged hash as a `Uint8Array`.
127+
*/
95128
export function taggedHash(
96129
prefix: TaggedHashPrefix,
97130
data: Uint8Array,

0 commit comments

Comments
 (0)