Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions src/cjs/crypto.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,21 @@ exports.taggedHash = taggedHash;
const ripemd160_1 = require('@noble/hashes/ripemd160');
const sha256_1 = require('@noble/hashes/sha256');
const tools = __importStar(require('uint8array-tools'));
/**
* Computes the HASH160 (RIPEMD-160 after SHA-256) of the given buffer.
*
* @param buffer - The input data to be hashed.
* @returns The HASH160 of the input buffer.
*/
function hash160(buffer) {
return (0, ripemd160_1.ripemd160)((0, sha256_1.sha256)(buffer));
}
/**
* Computes the double SHA-256 hash of the given buffer.
*
* @param buffer - The input data to be hashed.
* @returns The double SHA-256 hash of the input buffer.
*/
function hash256(buffer) {
return (0, sha256_1.sha256)((0, sha256_1.sha256)(buffer));
}
Expand All @@ -74,9 +86,22 @@ exports.TAGS = [
'KeyAgg list',
'KeyAgg coefficient',
];
/** An object mapping tags to their tagged hash prefix of [SHA256(tag) | SHA256(tag)] */
/**
* Defines the tagged hash prefixes used in the crypto module.
* A collection of tagged hash prefixes used in various BIP (Bitcoin Improvement Proposals)
* and Taproot-related operations. Each prefix is represented as a `Uint8Array`.
*
* @constant
* @type {TaggedHashPrefixes}
*
* @property {'BIP0340/challenge'} - Prefix for BIP0340 challenge.
* @property {'BIP0340/aux'} - Prefix for BIP0340 auxiliary data.
* @property {'BIP0340/nonce'} - Prefix for BIP0340 nonce.
* @property {TapLeaf} - Prefix for Taproot leaf.
* @property {TapBranch} - Prefix for Taproot branch.
* @property {TapSighash} - Prefix for Taproot sighash.
* @property {TapTweak} - Prefix for Taproot tweak.
* @property {'KeyAgg list'} - Prefix for key aggregation list.
* @property {'KeyAgg coefficient'} - Prefix for key aggregation coefficient.
*/
exports.TAGGED_HASH_PREFIXES = {
'BIP0340/challenge': Uint8Array.from([
Expand Down Expand Up @@ -134,6 +159,13 @@ exports.TAGGED_HASH_PREFIXES = {
78, 214, 66, 114, 129, 192, 145, 0, 249, 77, 205, 82, 201, 129,
]),
};
/**
* Computes a tagged hash using the specified prefix and data.
*
* @param prefix - The prefix to use for the tagged hash. This should be one of the values from the `TaggedHashPrefix` enum.
* @param data - The data to hash, provided as a `Uint8Array`.
* @returns The resulting tagged hash as a `Uint8Array`.
*/
function taggedHash(prefix, data) {
return (0, sha256_1.sha256)(
tools.concat([exports.TAGGED_HASH_PREFIXES[prefix], data]),
Expand Down
36 changes: 34 additions & 2 deletions src/cjs/crypto.d.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,46 @@
/**
* Computes the HASH160 (RIPEMD-160 after SHA-256) of the given buffer.
*
* @param buffer - The input data to be hashed.
* @returns The HASH160 of the input buffer.
*/
export declare function hash160(buffer: Uint8Array): Uint8Array;
/**
* Computes the double SHA-256 hash of the given buffer.
*
* @param buffer - The input data to be hashed.
* @returns The double SHA-256 hash of the input buffer.
*/
export declare function hash256(buffer: Uint8Array): Uint8Array;
export declare const TAGS: readonly ["BIP0340/challenge", "BIP0340/aux", "BIP0340/nonce", "TapLeaf", "TapBranch", "TapSighash", "TapTweak", "KeyAgg list", "KeyAgg coefficient"];
export type TaggedHashPrefix = (typeof TAGS)[number];
type TaggedHashPrefixes = {
[key in TaggedHashPrefix]: Uint8Array;
};
/** An object mapping tags to their tagged hash prefix of [SHA256(tag) | SHA256(tag)] */
/**
* Defines the tagged hash prefixes used in the crypto module.
* A collection of tagged hash prefixes used in various BIP (Bitcoin Improvement Proposals)
* and Taproot-related operations. Each prefix is represented as a `Uint8Array`.
*
* @constant
* @type {TaggedHashPrefixes}
*
* @property {'BIP0340/challenge'} - Prefix for BIP0340 challenge.
* @property {'BIP0340/aux'} - Prefix for BIP0340 auxiliary data.
* @property {'BIP0340/nonce'} - Prefix for BIP0340 nonce.
* @property {TapLeaf} - Prefix for Taproot leaf.
* @property {TapBranch} - Prefix for Taproot branch.
* @property {TapSighash} - Prefix for Taproot sighash.
* @property {TapTweak} - Prefix for Taproot tweak.
* @property {'KeyAgg list'} - Prefix for key aggregation list.
* @property {'KeyAgg coefficient'} - Prefix for key aggregation coefficient.
*/
export declare const TAGGED_HASH_PREFIXES: TaggedHashPrefixes;
/**
* Computes a tagged hash using the specified prefix and data.
*
* @param prefix - The prefix to use for the tagged hash. This should be one of the values from the `TaggedHashPrefix` enum.
* @param data - The data to hash, provided as a `Uint8Array`.
* @returns The resulting tagged hash as a `Uint8Array`.
*/
export declare function taggedHash(prefix: TaggedHashPrefix, data: Uint8Array): Uint8Array;
export {};
36 changes: 34 additions & 2 deletions src/esm/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,21 @@
import { ripemd160 } from '@noble/hashes/ripemd160';
import { sha256 } from '@noble/hashes/sha256';
import * as tools from 'uint8array-tools';
/**
* Computes the HASH160 (RIPEMD-160 after SHA-256) of the given buffer.
*
* @param buffer - The input data to be hashed.
* @returns The HASH160 of the input buffer.
*/
export function hash160(buffer) {
return ripemd160(sha256(buffer));
}
/**
* Computes the double SHA-256 hash of the given buffer.
*
* @param buffer - The input data to be hashed.
* @returns The double SHA-256 hash of the input buffer.
*/
export function hash256(buffer) {
return sha256(sha256(buffer));
}
Expand All @@ -24,9 +36,22 @@ export const TAGS = [
'KeyAgg list',
'KeyAgg coefficient',
];
/** An object mapping tags to their tagged hash prefix of [SHA256(tag) | SHA256(tag)] */
/**
* Defines the tagged hash prefixes used in the crypto module.
* A collection of tagged hash prefixes used in various BIP (Bitcoin Improvement Proposals)
* and Taproot-related operations. Each prefix is represented as a `Uint8Array`.
*
* @constant
* @type {TaggedHashPrefixes}
*
* @property {'BIP0340/challenge'} - Prefix for BIP0340 challenge.
* @property {'BIP0340/aux'} - Prefix for BIP0340 auxiliary data.
* @property {'BIP0340/nonce'} - Prefix for BIP0340 nonce.
* @property {TapLeaf} - Prefix for Taproot leaf.
* @property {TapBranch} - Prefix for Taproot branch.
* @property {TapSighash} - Prefix for Taproot sighash.
* @property {TapTweak} - Prefix for Taproot tweak.
* @property {'KeyAgg list'} - Prefix for key aggregation list.
* @property {'KeyAgg coefficient'} - Prefix for key aggregation coefficient.
*/
export const TAGGED_HASH_PREFIXES = {
'BIP0340/challenge': Uint8Array.from([
Expand Down Expand Up @@ -84,6 +109,13 @@ export const TAGGED_HASH_PREFIXES = {
78, 214, 66, 114, 129, 192, 145, 0, 249, 77, 205, 82, 201, 129,
]),
};
/**
* Computes a tagged hash using the specified prefix and data.
*
* @param prefix - The prefix to use for the tagged hash. This should be one of the values from the `TaggedHashPrefix` enum.
* @param data - The data to hash, provided as a `Uint8Array`.
* @returns The resulting tagged hash as a `Uint8Array`.
*/
export function taggedHash(prefix, data) {
return sha256(tools.concat([TAGGED_HASH_PREFIXES[prefix], data]));
}
37 changes: 35 additions & 2 deletions ts_src/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,22 @@ import { ripemd160 } from '@noble/hashes/ripemd160';
import { sha256 } from '@noble/hashes/sha256';
import * as tools from 'uint8array-tools';

/**
* Computes the HASH160 (RIPEMD-160 after SHA-256) of the given buffer.
*
* @param buffer - The input data to be hashed.
* @returns The HASH160 of the input buffer.
*/
export function hash160(buffer: Uint8Array): Uint8Array {
return ripemd160(sha256(buffer));
}

/**
* Computes the double SHA-256 hash of the given buffer.
*
* @param buffer - The input data to be hashed.
* @returns The double SHA-256 hash of the input buffer.
*/
export function hash256(buffer: Uint8Array): Uint8Array {
return sha256(sha256(buffer));
}
Expand All @@ -31,9 +43,23 @@ export type TaggedHashPrefix = (typeof TAGS)[number];
type TaggedHashPrefixes = {
[key in TaggedHashPrefix]: Uint8Array;
};
/** An object mapping tags to their tagged hash prefix of [SHA256(tag) | SHA256(tag)] */

/**
* Defines the tagged hash prefixes used in the crypto module.
* A collection of tagged hash prefixes used in various BIP (Bitcoin Improvement Proposals)
* and Taproot-related operations. Each prefix is represented as a `Uint8Array`.
*
* @constant
* @type {TaggedHashPrefixes}
*
* @property {'BIP0340/challenge'} - Prefix for BIP0340 challenge.
* @property {'BIP0340/aux'} - Prefix for BIP0340 auxiliary data.
* @property {'BIP0340/nonce'} - Prefix for BIP0340 nonce.
* @property {TapLeaf} - Prefix for Taproot leaf.
* @property {TapBranch} - Prefix for Taproot branch.
* @property {TapSighash} - Prefix for Taproot sighash.
* @property {TapTweak} - Prefix for Taproot tweak.
* @property {'KeyAgg list'} - Prefix for key aggregation list.
* @property {'KeyAgg coefficient'} - Prefix for key aggregation coefficient.
*/
export const TAGGED_HASH_PREFIXES: TaggedHashPrefixes = {
'BIP0340/challenge': Uint8Array.from([
Expand Down Expand Up @@ -92,6 +118,13 @@ export const TAGGED_HASH_PREFIXES: TaggedHashPrefixes = {
]),
};

/**
* Computes a tagged hash using the specified prefix and data.
*
* @param prefix - The prefix to use for the tagged hash. This should be one of the values from the `TaggedHashPrefix` enum.
* @param data - The data to hash, provided as a `Uint8Array`.
* @returns The resulting tagged hash as a `Uint8Array`.
*/
export function taggedHash(
prefix: TaggedHashPrefix,
data: Uint8Array,
Expand Down
Loading