Skip to content

Commit c61d734

Browse files
committed
doc: improve document of crypto
1 parent b89a5e3 commit c61d734

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

ts_src/crypto.ts

Lines changed: 36 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,9 +118,17 @@ 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,
98131
): Uint8Array {
99132
return sha256(tools.concat([TAGGED_HASH_PREFIXES[prefix], data]));
100133
}
134+

0 commit comments

Comments
 (0)