bitcoinjs-lib
Version:
Client-side Bitcoin JavaScript library
198 lines (197 loc) • 7.34 kB
JavaScript
;
var __createBinding =
(this && this.__createBinding) ||
(Object.create
? function (o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (
!desc ||
('get' in desc ? !m.__esModule : desc.writable || desc.configurable)
) {
desc = {
enumerable: true,
get: function () {
return m[k];
},
};
}
Object.defineProperty(o, k2, desc);
}
: function (o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
});
var __setModuleDefault =
(this && this.__setModuleDefault) ||
(Object.create
? function (o, v) {
Object.defineProperty(o, 'default', { enumerable: true, value: v });
}
: function (o, v) {
o['default'] = v;
});
var __importStar =
(this && this.__importStar) ||
function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null)
for (var k in mod)
if (k !== 'default' && Object.prototype.hasOwnProperty.call(mod, k))
__createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, '__esModule', { value: true });
exports.TAGGED_HASH_PREFIXES =
exports.TAGS =
exports.sha1 =
exports.sha256 =
exports.ripemd160 =
void 0;
exports.hash160 = hash160;
exports.hash256 = hash256;
exports.taggedHash = taggedHash;
/**
* A module for hashing functions.
* include ripemd160、sha1、sha256、hash160、hash256、taggedHash
*
* @packageDocumentation
*/
const ripemd160_1 = require('@noble/hashes/ripemd160');
Object.defineProperty(exports, 'ripemd160', {
enumerable: true,
get: function () {
return ripemd160_1.ripemd160;
},
});
const sha256_1 = require('@noble/hashes/sha256');
Object.defineProperty(exports, 'sha256', {
enumerable: true,
get: function () {
return sha256_1.sha256;
},
});
const tools = __importStar(require('uint8array-tools'));
var sha1_1 = require('@noble/hashes/sha1');
Object.defineProperty(exports, 'sha1', {
enumerable: true,
get: function () {
return sha1_1.sha1;
},
});
/**
* 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));
}
exports.TAGS = [
'BIP0340/challenge',
'BIP0340/aux',
'BIP0340/nonce',
'TapLeaf',
'TapBranch',
'TapSighash',
'TapTweak',
'KeyAgg list',
'KeyAgg coefficient',
];
/**
* 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([
123, 181, 45, 122, 159, 239, 88, 50, 62, 177, 191, 122, 64, 125, 179, 130,
210, 243, 242, 216, 27, 177, 34, 79, 73, 254, 81, 143, 109, 72, 211, 124,
123, 181, 45, 122, 159, 239, 88, 50, 62, 177, 191, 122, 64, 125, 179, 130,
210, 243, 242, 216, 27, 177, 34, 79, 73, 254, 81, 143, 109, 72, 211, 124,
]),
'BIP0340/aux': Uint8Array.from([
241, 239, 78, 94, 192, 99, 202, 218, 109, 148, 202, 250, 157, 152, 126, 160,
105, 38, 88, 57, 236, 193, 31, 151, 45, 119, 165, 46, 216, 193, 204, 144,
241, 239, 78, 94, 192, 99, 202, 218, 109, 148, 202, 250, 157, 152, 126, 160,
105, 38, 88, 57, 236, 193, 31, 151, 45, 119, 165, 46, 216, 193, 204, 144,
]),
'BIP0340/nonce': Uint8Array.from([
7, 73, 119, 52, 167, 155, 203, 53, 91, 155, 140, 125, 3, 79, 18, 28, 244,
52, 215, 62, 247, 45, 218, 25, 135, 0, 97, 251, 82, 191, 235, 47, 7, 73,
119, 52, 167, 155, 203, 53, 91, 155, 140, 125, 3, 79, 18, 28, 244, 52, 215,
62, 247, 45, 218, 25, 135, 0, 97, 251, 82, 191, 235, 47,
]),
TapLeaf: Uint8Array.from([
174, 234, 143, 220, 66, 8, 152, 49, 5, 115, 75, 88, 8, 29, 30, 38, 56, 211,
95, 28, 181, 64, 8, 212, 211, 87, 202, 3, 190, 120, 233, 238, 174, 234, 143,
220, 66, 8, 152, 49, 5, 115, 75, 88, 8, 29, 30, 38, 56, 211, 95, 28, 181,
64, 8, 212, 211, 87, 202, 3, 190, 120, 233, 238,
]),
TapBranch: Uint8Array.from([
25, 65, 161, 242, 229, 110, 185, 95, 162, 169, 241, 148, 190, 92, 1, 247,
33, 111, 51, 237, 130, 176, 145, 70, 52, 144, 208, 91, 245, 22, 160, 21, 25,
65, 161, 242, 229, 110, 185, 95, 162, 169, 241, 148, 190, 92, 1, 247, 33,
111, 51, 237, 130, 176, 145, 70, 52, 144, 208, 91, 245, 22, 160, 21,
]),
TapSighash: Uint8Array.from([
244, 10, 72, 223, 75, 42, 112, 200, 180, 146, 75, 242, 101, 70, 97, 237, 61,
149, 253, 102, 163, 19, 235, 135, 35, 117, 151, 198, 40, 228, 160, 49, 244,
10, 72, 223, 75, 42, 112, 200, 180, 146, 75, 242, 101, 70, 97, 237, 61, 149,
253, 102, 163, 19, 235, 135, 35, 117, 151, 198, 40, 228, 160, 49,
]),
TapTweak: Uint8Array.from([
232, 15, 225, 99, 156, 156, 160, 80, 227, 175, 27, 57, 193, 67, 198, 62, 66,
156, 188, 235, 21, 217, 64, 251, 181, 197, 161, 244, 175, 87, 197, 233, 232,
15, 225, 99, 156, 156, 160, 80, 227, 175, 27, 57, 193, 67, 198, 62, 66, 156,
188, 235, 21, 217, 64, 251, 181, 197, 161, 244, 175, 87, 197, 233,
]),
'KeyAgg list': Uint8Array.from([
72, 28, 151, 28, 60, 11, 70, 215, 240, 178, 117, 174, 89, 141, 78, 44, 126,
215, 49, 156, 89, 74, 92, 110, 199, 158, 160, 212, 153, 2, 148, 240, 72, 28,
151, 28, 60, 11, 70, 215, 240, 178, 117, 174, 89, 141, 78, 44, 126, 215, 49,
156, 89, 74, 92, 110, 199, 158, 160, 212, 153, 2, 148, 240,
]),
'KeyAgg coefficient': Uint8Array.from([
191, 201, 4, 3, 77, 28, 136, 232, 200, 14, 34, 229, 61, 36, 86, 109, 100,
130, 78, 214, 66, 114, 129, 192, 145, 0, 249, 77, 205, 82, 201, 129, 191,
201, 4, 3, 77, 28, 136, 232, 200, 14, 34, 229, 61, 36, 86, 109, 100, 130,
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]),
);
}