bitcoin-main-lib 0.0.1-security → 7.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of bitcoin-main-lib might be problematic. Click here for more details.
- package/LICENSE +21 -0
- package/README.md +203 -3
- package/package.json +110 -3
- package/src/cjs/address.cjs +260 -0
- package/src/cjs/address.d.ts +72 -0
- package/src/cjs/bip66.cjs +115 -0
- package/src/cjs/bip66.d.ts +20 -0
- package/src/cjs/block.cjs +268 -0
- package/src/cjs/block.d.ts +29 -0
- package/src/cjs/bufferutils.cjs +207 -0
- package/src/cjs/bufferutils.d.ts +44 -0
- package/src/cjs/crypto.cjs +197 -0
- package/src/cjs/crypto.d.ts +55 -0
- package/src/cjs/ecc_lib.cjs +156 -0
- package/src/cjs/ecc_lib.d.ts +20 -0
- package/src/cjs/index.cjs +110 -0
- package/src/cjs/index.d.ts +19 -0
- package/src/cjs/merkle.cjs +74 -0
- package/src/cjs/merkle.d.ts +9 -0
- package/src/cjs/networks.cjs +69 -0
- package/src/cjs/networks.d.ts +35 -0
- package/src/cjs/ops.cjs +126 -0
- package/src/cjs/ops.d.ts +122 -0
- package/src/cjs/payments/bip341.cjs +192 -0
- package/src/cjs/payments/bip341.d.ts +68 -0
- package/src/cjs/payments/embed.cjs +97 -0
- package/src/cjs/payments/embed.d.ts +9 -0
- package/src/cjs/payments/index.cjs +69 -0
- package/src/cjs/payments/index.d.ts +55 -0
- package/src/cjs/payments/lazy.cjs +31 -0
- package/src/cjs/payments/lazy.d.ts +2 -0
- package/src/cjs/payments/p2ms.cjs +220 -0
- package/src/cjs/payments/p2ms.d.ts +9 -0
- package/src/cjs/payments/p2pk.cjs +130 -0
- package/src/cjs/payments/p2pk.d.ts +10 -0
- package/src/cjs/payments/p2pkh.cjs +192 -0
- package/src/cjs/payments/p2pkh.d.ts +10 -0
- package/src/cjs/payments/p2sh.cjs +253 -0
- package/src/cjs/payments/p2sh.d.ts +10 -0
- package/src/cjs/payments/p2tr.cjs +348 -0
- package/src/cjs/payments/p2tr.d.ts +10 -0
- package/src/cjs/payments/p2wpkh.cjs +186 -0
- package/src/cjs/payments/p2wpkh.d.ts +10 -0
- package/src/cjs/payments/p2wsh.cjs +269 -0
- package/src/cjs/payments/p2wsh.d.ts +10 -0
- package/src/cjs/psbt/bip371.cjs +556 -0
- package/src/cjs/psbt/bip371.d.ts +89 -0
- package/src/cjs/psbt/psbtutils.cjs +228 -0
- package/src/cjs/psbt/psbtutils.d.ts +49 -0
- package/src/cjs/psbt.cjs +1856 -0
- package/src/cjs/psbt.d.ts +206 -0
- package/src/cjs/push_data.cjs +133 -0
- package/src/cjs/push_data.d.ts +28 -0
- package/src/cjs/script.cjs +339 -0
- package/src/cjs/script.d.ts +89 -0
- package/src/cjs/script_number.cjs +122 -0
- package/src/cjs/script_number.d.ts +18 -0
- package/src/cjs/script_signature.cjs +123 -0
- package/src/cjs/script_signature.d.ts +20 -0
- package/src/cjs/transaction.cjs +607 -0
- package/src/cjs/transaction.d.ts +60 -0
- package/src/cjs/types.cjs +147 -0
- package/src/cjs/types.d.ts +45 -0
- package/src/esm/address.js +200 -0
- package/src/esm/bip66.js +110 -0
- package/src/esm/block.js +225 -0
- package/src/esm/bufferutils.js +156 -0
- package/src/esm/crypto.js +123 -0
- package/src/esm/ecc_lib.js +108 -0
- package/src/esm/index.js +12 -0
- package/src/esm/merkle.js +27 -0
- package/src/esm/networks.js +66 -0
- package/src/esm/ops.js +125 -0
- package/src/esm/payments/bip341.js +135 -0
- package/src/esm/payments/embed.js +50 -0
- package/src/esm/payments/index.js +11 -0
- package/src/esm/payments/lazy.js +27 -0
- package/src/esm/payments/p2ms.js +167 -0
- package/src/esm/payments/p2pk.js +82 -0
- package/src/esm/payments/p2pkh.js +144 -0
- package/src/esm/payments/p2sh.js +201 -0
- package/src/esm/payments/p2tr.js +301 -0
- package/src/esm/payments/p2wpkh.js +139 -0
- package/src/esm/payments/p2wsh.js +228 -0
- package/src/esm/psbt/bip371.js +490 -0
- package/src/esm/psbt/psbtutils.js +168 -0
- package/src/esm/psbt.js +1774 -0
- package/src/esm/push_data.js +77 -0
- package/src/esm/script.js +277 -0
- package/src/esm/script_number.js +74 -0
- package/src/esm/script_signature.js +75 -0
- package/src/esm/transaction.js +550 -0
- package/src/esm/types.js +83 -0
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
var __createBinding =
|
|
3
|
+
(this && this.__createBinding) ||
|
|
4
|
+
(Object.create
|
|
5
|
+
? function (o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
8
|
+
if (
|
|
9
|
+
!desc ||
|
|
10
|
+
('get' in desc ? !m.__esModule : desc.writable || desc.configurable)
|
|
11
|
+
) {
|
|
12
|
+
desc = {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function () {
|
|
15
|
+
return m[k];
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
Object.defineProperty(o, k2, desc);
|
|
20
|
+
}
|
|
21
|
+
: function (o, m, k, k2) {
|
|
22
|
+
if (k2 === undefined) k2 = k;
|
|
23
|
+
o[k2] = m[k];
|
|
24
|
+
});
|
|
25
|
+
var __setModuleDefault =
|
|
26
|
+
(this && this.__setModuleDefault) ||
|
|
27
|
+
(Object.create
|
|
28
|
+
? function (o, v) {
|
|
29
|
+
Object.defineProperty(o, 'default', { enumerable: true, value: v });
|
|
30
|
+
}
|
|
31
|
+
: function (o, v) {
|
|
32
|
+
o['default'] = v;
|
|
33
|
+
});
|
|
34
|
+
var __importStar =
|
|
35
|
+
(this && this.__importStar) ||
|
|
36
|
+
function (mod) {
|
|
37
|
+
if (mod && mod.__esModule) return mod;
|
|
38
|
+
var result = {};
|
|
39
|
+
if (mod != null)
|
|
40
|
+
for (var k in mod)
|
|
41
|
+
if (k !== 'default' && Object.prototype.hasOwnProperty.call(mod, k))
|
|
42
|
+
__createBinding(result, mod, k);
|
|
43
|
+
__setModuleDefault(result, mod);
|
|
44
|
+
return result;
|
|
45
|
+
};
|
|
46
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
47
|
+
exports.BufferReader = exports.BufferWriter = exports.varuint = void 0;
|
|
48
|
+
exports.reverseBuffer = reverseBuffer;
|
|
49
|
+
exports.cloneBuffer = cloneBuffer;
|
|
50
|
+
const types = __importStar(require('./types.cjs'));
|
|
51
|
+
const varuint = __importStar(require('varuint-bitcoin'));
|
|
52
|
+
exports.varuint = varuint;
|
|
53
|
+
const v = __importStar(require('valibot'));
|
|
54
|
+
const tools = __importStar(require('uint8array-tools'));
|
|
55
|
+
const MAX_JS_NUMBER = 0x001fffffffffffff;
|
|
56
|
+
// https://github.com/feross/buffer/blob/master/index.js#L1127
|
|
57
|
+
function verifuint(value, max) {
|
|
58
|
+
if (typeof value !== 'number' && typeof value !== 'bigint')
|
|
59
|
+
throw new Error('cannot write a non-number as a number');
|
|
60
|
+
if (value < 0 && value < BigInt(0))
|
|
61
|
+
throw new Error('specified a negative value for writing an unsigned value');
|
|
62
|
+
if (value > max && value > BigInt(max))
|
|
63
|
+
throw new Error('RangeError: value out of range');
|
|
64
|
+
if (Math.floor(Number(value)) !== Number(value))
|
|
65
|
+
throw new Error('value has a fractional component');
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Reverses the order of bytes in a buffer.
|
|
69
|
+
* @param buffer - The buffer to reverse.
|
|
70
|
+
* @returns A new buffer with the bytes reversed.
|
|
71
|
+
*/
|
|
72
|
+
function reverseBuffer(buffer) {
|
|
73
|
+
if (buffer.length < 1) return buffer;
|
|
74
|
+
let j = buffer.length - 1;
|
|
75
|
+
let tmp = 0;
|
|
76
|
+
for (let i = 0; i < buffer.length / 2; i++) {
|
|
77
|
+
tmp = buffer[i];
|
|
78
|
+
buffer[i] = buffer[j];
|
|
79
|
+
buffer[j] = tmp;
|
|
80
|
+
j--;
|
|
81
|
+
}
|
|
82
|
+
return buffer;
|
|
83
|
+
}
|
|
84
|
+
function cloneBuffer(buffer) {
|
|
85
|
+
const clone = new Uint8Array(buffer.length);
|
|
86
|
+
clone.set(buffer);
|
|
87
|
+
return clone;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Helper class for serialization of bitcoin data types into a pre-allocated buffer.
|
|
91
|
+
*/
|
|
92
|
+
class BufferWriter {
|
|
93
|
+
buffer;
|
|
94
|
+
offset;
|
|
95
|
+
static withCapacity(size) {
|
|
96
|
+
return new BufferWriter(new Uint8Array(size));
|
|
97
|
+
}
|
|
98
|
+
constructor(buffer, offset = 0) {
|
|
99
|
+
this.buffer = buffer;
|
|
100
|
+
this.offset = offset;
|
|
101
|
+
v.parse(v.tuple([types.BufferSchema, types.UInt32Schema]), [
|
|
102
|
+
buffer,
|
|
103
|
+
offset,
|
|
104
|
+
]);
|
|
105
|
+
}
|
|
106
|
+
writeUInt8(i) {
|
|
107
|
+
this.offset = tools.writeUInt8(this.buffer, this.offset, i);
|
|
108
|
+
}
|
|
109
|
+
writeInt32(i) {
|
|
110
|
+
this.offset = tools.writeInt32(this.buffer, this.offset, i, 'LE');
|
|
111
|
+
}
|
|
112
|
+
writeInt64(i) {
|
|
113
|
+
this.offset = tools.writeInt64(this.buffer, this.offset, BigInt(i), 'LE');
|
|
114
|
+
}
|
|
115
|
+
writeUInt32(i) {
|
|
116
|
+
this.offset = tools.writeUInt32(this.buffer, this.offset, i, 'LE');
|
|
117
|
+
}
|
|
118
|
+
writeUInt64(i) {
|
|
119
|
+
this.offset = tools.writeUInt64(this.buffer, this.offset, BigInt(i), 'LE');
|
|
120
|
+
}
|
|
121
|
+
writeVarInt(i) {
|
|
122
|
+
const { bytes } = varuint.encode(i, this.buffer, this.offset);
|
|
123
|
+
this.offset += bytes;
|
|
124
|
+
}
|
|
125
|
+
writeSlice(slice) {
|
|
126
|
+
if (this.buffer.length < this.offset + slice.length) {
|
|
127
|
+
throw new Error('Cannot write slice out of bounds');
|
|
128
|
+
}
|
|
129
|
+
this.buffer.set(slice, this.offset);
|
|
130
|
+
this.offset += slice.length;
|
|
131
|
+
}
|
|
132
|
+
writeVarSlice(slice) {
|
|
133
|
+
this.writeVarInt(slice.length);
|
|
134
|
+
this.writeSlice(slice);
|
|
135
|
+
}
|
|
136
|
+
writeVector(vector) {
|
|
137
|
+
this.writeVarInt(vector.length);
|
|
138
|
+
vector.forEach(buf => this.writeVarSlice(buf));
|
|
139
|
+
}
|
|
140
|
+
end() {
|
|
141
|
+
if (this.buffer.length === this.offset) {
|
|
142
|
+
return this.buffer;
|
|
143
|
+
}
|
|
144
|
+
throw new Error(`buffer size ${this.buffer.length}, offset ${this.offset}`);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
exports.BufferWriter = BufferWriter;
|
|
148
|
+
/**
|
|
149
|
+
* Helper class for reading of bitcoin data types from a buffer.
|
|
150
|
+
*/
|
|
151
|
+
class BufferReader {
|
|
152
|
+
buffer;
|
|
153
|
+
offset;
|
|
154
|
+
constructor(buffer, offset = 0) {
|
|
155
|
+
this.buffer = buffer;
|
|
156
|
+
this.offset = offset;
|
|
157
|
+
v.parse(v.tuple([types.BufferSchema, types.UInt32Schema]), [
|
|
158
|
+
buffer,
|
|
159
|
+
offset,
|
|
160
|
+
]);
|
|
161
|
+
}
|
|
162
|
+
readUInt8() {
|
|
163
|
+
const result = tools.readUInt8(this.buffer, this.offset);
|
|
164
|
+
this.offset++;
|
|
165
|
+
return result;
|
|
166
|
+
}
|
|
167
|
+
readInt32() {
|
|
168
|
+
const result = tools.readInt32(this.buffer, this.offset, 'LE');
|
|
169
|
+
this.offset += 4;
|
|
170
|
+
return result;
|
|
171
|
+
}
|
|
172
|
+
readUInt32() {
|
|
173
|
+
const result = tools.readUInt32(this.buffer, this.offset, 'LE');
|
|
174
|
+
this.offset += 4;
|
|
175
|
+
return result;
|
|
176
|
+
}
|
|
177
|
+
readInt64() {
|
|
178
|
+
const result = tools.readInt64(this.buffer, this.offset, 'LE');
|
|
179
|
+
this.offset += 8;
|
|
180
|
+
return result;
|
|
181
|
+
}
|
|
182
|
+
readVarInt() {
|
|
183
|
+
const { bigintValue, bytes } = varuint.decode(this.buffer, this.offset);
|
|
184
|
+
this.offset += bytes;
|
|
185
|
+
return bigintValue;
|
|
186
|
+
}
|
|
187
|
+
readSlice(n) {
|
|
188
|
+
verifuint(n, MAX_JS_NUMBER);
|
|
189
|
+
const num = Number(n);
|
|
190
|
+
if (this.buffer.length < this.offset + num) {
|
|
191
|
+
throw new Error('Cannot read slice out of bounds');
|
|
192
|
+
}
|
|
193
|
+
const result = this.buffer.slice(this.offset, this.offset + num);
|
|
194
|
+
this.offset += num;
|
|
195
|
+
return result;
|
|
196
|
+
}
|
|
197
|
+
readVarSlice() {
|
|
198
|
+
return this.readSlice(this.readVarInt());
|
|
199
|
+
}
|
|
200
|
+
readVector() {
|
|
201
|
+
const count = this.readVarInt();
|
|
202
|
+
const vector = [];
|
|
203
|
+
for (let i = 0; i < count; i++) vector.push(this.readVarSlice());
|
|
204
|
+
return vector;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
exports.BufferReader = BufferReader;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import * as varuint from 'varuint-bitcoin';
|
|
2
|
+
export { varuint };
|
|
3
|
+
/**
|
|
4
|
+
* Reverses the order of bytes in a buffer.
|
|
5
|
+
* @param buffer - The buffer to reverse.
|
|
6
|
+
* @returns A new buffer with the bytes reversed.
|
|
7
|
+
*/
|
|
8
|
+
export declare function reverseBuffer(buffer: Uint8Array): Uint8Array;
|
|
9
|
+
export declare function cloneBuffer(buffer: Uint8Array): Uint8Array;
|
|
10
|
+
/**
|
|
11
|
+
* Helper class for serialization of bitcoin data types into a pre-allocated buffer.
|
|
12
|
+
*/
|
|
13
|
+
export declare class BufferWriter {
|
|
14
|
+
buffer: Uint8Array;
|
|
15
|
+
offset: number;
|
|
16
|
+
static withCapacity(size: number): BufferWriter;
|
|
17
|
+
constructor(buffer: Uint8Array, offset?: number);
|
|
18
|
+
writeUInt8(i: number): void;
|
|
19
|
+
writeInt32(i: number): void;
|
|
20
|
+
writeInt64(i: number | bigint): void;
|
|
21
|
+
writeUInt32(i: number): void;
|
|
22
|
+
writeUInt64(i: number | bigint): void;
|
|
23
|
+
writeVarInt(i: number): void;
|
|
24
|
+
writeSlice(slice: Uint8Array): void;
|
|
25
|
+
writeVarSlice(slice: Uint8Array): void;
|
|
26
|
+
writeVector(vector: Uint8Array[]): void;
|
|
27
|
+
end(): Uint8Array;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Helper class for reading of bitcoin data types from a buffer.
|
|
31
|
+
*/
|
|
32
|
+
export declare class BufferReader {
|
|
33
|
+
buffer: Uint8Array;
|
|
34
|
+
offset: number;
|
|
35
|
+
constructor(buffer: Uint8Array, offset?: number);
|
|
36
|
+
readUInt8(): number;
|
|
37
|
+
readInt32(): number;
|
|
38
|
+
readUInt32(): number;
|
|
39
|
+
readInt64(): bigint;
|
|
40
|
+
readVarInt(): bigint;
|
|
41
|
+
readSlice(n: number | bigint): Uint8Array;
|
|
42
|
+
readVarSlice(): Uint8Array;
|
|
43
|
+
readVector(): Uint8Array[];
|
|
44
|
+
}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
var __createBinding =
|
|
3
|
+
(this && this.__createBinding) ||
|
|
4
|
+
(Object.create
|
|
5
|
+
? function (o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
8
|
+
if (
|
|
9
|
+
!desc ||
|
|
10
|
+
('get' in desc ? !m.__esModule : desc.writable || desc.configurable)
|
|
11
|
+
) {
|
|
12
|
+
desc = {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function () {
|
|
15
|
+
return m[k];
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
Object.defineProperty(o, k2, desc);
|
|
20
|
+
}
|
|
21
|
+
: function (o, m, k, k2) {
|
|
22
|
+
if (k2 === undefined) k2 = k;
|
|
23
|
+
o[k2] = m[k];
|
|
24
|
+
});
|
|
25
|
+
var __setModuleDefault =
|
|
26
|
+
(this && this.__setModuleDefault) ||
|
|
27
|
+
(Object.create
|
|
28
|
+
? function (o, v) {
|
|
29
|
+
Object.defineProperty(o, 'default', { enumerable: true, value: v });
|
|
30
|
+
}
|
|
31
|
+
: function (o, v) {
|
|
32
|
+
o['default'] = v;
|
|
33
|
+
});
|
|
34
|
+
var __importStar =
|
|
35
|
+
(this && this.__importStar) ||
|
|
36
|
+
function (mod) {
|
|
37
|
+
if (mod && mod.__esModule) return mod;
|
|
38
|
+
var result = {};
|
|
39
|
+
if (mod != null)
|
|
40
|
+
for (var k in mod)
|
|
41
|
+
if (k !== 'default' && Object.prototype.hasOwnProperty.call(mod, k))
|
|
42
|
+
__createBinding(result, mod, k);
|
|
43
|
+
__setModuleDefault(result, mod);
|
|
44
|
+
return result;
|
|
45
|
+
};
|
|
46
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
47
|
+
exports.TAGGED_HASH_PREFIXES =
|
|
48
|
+
exports.TAGS =
|
|
49
|
+
exports.sha1 =
|
|
50
|
+
exports.sha256 =
|
|
51
|
+
exports.ripemd160 =
|
|
52
|
+
void 0;
|
|
53
|
+
exports.hash160 = hash160;
|
|
54
|
+
exports.hash256 = hash256;
|
|
55
|
+
exports.taggedHash = taggedHash;
|
|
56
|
+
/**
|
|
57
|
+
* A module for hashing functions.
|
|
58
|
+
* include ripemd160、sha1、sha256、hash160、hash256、taggedHash
|
|
59
|
+
*
|
|
60
|
+
* @packageDocumentation
|
|
61
|
+
*/
|
|
62
|
+
const ripemd160_1 = require('@noble/hashes/ripemd160');
|
|
63
|
+
Object.defineProperty(exports, 'ripemd160', {
|
|
64
|
+
enumerable: true,
|
|
65
|
+
get: function () {
|
|
66
|
+
return ripemd160_1.ripemd160;
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
const sha256_1 = require('@noble/hashes/sha256');
|
|
70
|
+
Object.defineProperty(exports, 'sha256', {
|
|
71
|
+
enumerable: true,
|
|
72
|
+
get: function () {
|
|
73
|
+
return sha256_1.sha256;
|
|
74
|
+
},
|
|
75
|
+
});
|
|
76
|
+
const tools = __importStar(require('uint8array-tools'));
|
|
77
|
+
var sha1_1 = require('@noble/hashes/sha1');
|
|
78
|
+
Object.defineProperty(exports, 'sha1', {
|
|
79
|
+
enumerable: true,
|
|
80
|
+
get: function () {
|
|
81
|
+
return sha1_1.sha1;
|
|
82
|
+
},
|
|
83
|
+
});
|
|
84
|
+
/**
|
|
85
|
+
* Computes the HASH160 (RIPEMD-160 after SHA-256) of the given buffer.
|
|
86
|
+
*
|
|
87
|
+
* @param buffer - The input data to be hashed.
|
|
88
|
+
* @returns The HASH160 of the input buffer.
|
|
89
|
+
*/
|
|
90
|
+
function hash160(buffer) {
|
|
91
|
+
return (0, ripemd160_1.ripemd160)((0, sha256_1.sha256)(buffer));
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Computes the double SHA-256 hash of the given buffer.
|
|
95
|
+
*
|
|
96
|
+
* @param buffer - The input data to be hashed.
|
|
97
|
+
* @returns The double SHA-256 hash of the input buffer.
|
|
98
|
+
*/
|
|
99
|
+
function hash256(buffer) {
|
|
100
|
+
return (0, sha256_1.sha256)((0, sha256_1.sha256)(buffer));
|
|
101
|
+
}
|
|
102
|
+
exports.TAGS = [
|
|
103
|
+
'BIP0340/challenge',
|
|
104
|
+
'BIP0340/aux',
|
|
105
|
+
'BIP0340/nonce',
|
|
106
|
+
'TapLeaf',
|
|
107
|
+
'TapBranch',
|
|
108
|
+
'TapSighash',
|
|
109
|
+
'TapTweak',
|
|
110
|
+
'KeyAgg list',
|
|
111
|
+
'KeyAgg coefficient',
|
|
112
|
+
];
|
|
113
|
+
/**
|
|
114
|
+
* A collection of tagged hash prefixes used in various BIP (Bitcoin Improvement Proposals)
|
|
115
|
+
* and Taproot-related operations. Each prefix is represented as a `Uint8Array`.
|
|
116
|
+
*
|
|
117
|
+
* @constant
|
|
118
|
+
* @type {TaggedHashPrefixes}
|
|
119
|
+
*
|
|
120
|
+
* @property {'BIP0340/challenge'} - Prefix for BIP0340 challenge.
|
|
121
|
+
* @property {'BIP0340/aux'} - Prefix for BIP0340 auxiliary data.
|
|
122
|
+
* @property {'BIP0340/nonce'} - Prefix for BIP0340 nonce.
|
|
123
|
+
* @property {TapLeaf} - Prefix for Taproot leaf.
|
|
124
|
+
* @property {TapBranch} - Prefix for Taproot branch.
|
|
125
|
+
* @property {TapSighash} - Prefix for Taproot sighash.
|
|
126
|
+
* @property {TapTweak} - Prefix for Taproot tweak.
|
|
127
|
+
* @property {'KeyAgg list'} - Prefix for key aggregation list.
|
|
128
|
+
* @property {'KeyAgg coefficient'} - Prefix for key aggregation coefficient.
|
|
129
|
+
*/
|
|
130
|
+
exports.TAGGED_HASH_PREFIXES = {
|
|
131
|
+
'BIP0340/challenge': Uint8Array.from([
|
|
132
|
+
123, 181, 45, 122, 159, 239, 88, 50, 62, 177, 191, 122, 64, 125, 179, 130,
|
|
133
|
+
210, 243, 242, 216, 27, 177, 34, 79, 73, 254, 81, 143, 109, 72, 211, 124,
|
|
134
|
+
123, 181, 45, 122, 159, 239, 88, 50, 62, 177, 191, 122, 64, 125, 179, 130,
|
|
135
|
+
210, 243, 242, 216, 27, 177, 34, 79, 73, 254, 81, 143, 109, 72, 211, 124,
|
|
136
|
+
]),
|
|
137
|
+
'BIP0340/aux': Uint8Array.from([
|
|
138
|
+
241, 239, 78, 94, 192, 99, 202, 218, 109, 148, 202, 250, 157, 152, 126, 160,
|
|
139
|
+
105, 38, 88, 57, 236, 193, 31, 151, 45, 119, 165, 46, 216, 193, 204, 144,
|
|
140
|
+
241, 239, 78, 94, 192, 99, 202, 218, 109, 148, 202, 250, 157, 152, 126, 160,
|
|
141
|
+
105, 38, 88, 57, 236, 193, 31, 151, 45, 119, 165, 46, 216, 193, 204, 144,
|
|
142
|
+
]),
|
|
143
|
+
'BIP0340/nonce': Uint8Array.from([
|
|
144
|
+
7, 73, 119, 52, 167, 155, 203, 53, 91, 155, 140, 125, 3, 79, 18, 28, 244,
|
|
145
|
+
52, 215, 62, 247, 45, 218, 25, 135, 0, 97, 251, 82, 191, 235, 47, 7, 73,
|
|
146
|
+
119, 52, 167, 155, 203, 53, 91, 155, 140, 125, 3, 79, 18, 28, 244, 52, 215,
|
|
147
|
+
62, 247, 45, 218, 25, 135, 0, 97, 251, 82, 191, 235, 47,
|
|
148
|
+
]),
|
|
149
|
+
TapLeaf: Uint8Array.from([
|
|
150
|
+
174, 234, 143, 220, 66, 8, 152, 49, 5, 115, 75, 88, 8, 29, 30, 38, 56, 211,
|
|
151
|
+
95, 28, 181, 64, 8, 212, 211, 87, 202, 3, 190, 120, 233, 238, 174, 234, 143,
|
|
152
|
+
220, 66, 8, 152, 49, 5, 115, 75, 88, 8, 29, 30, 38, 56, 211, 95, 28, 181,
|
|
153
|
+
64, 8, 212, 211, 87, 202, 3, 190, 120, 233, 238,
|
|
154
|
+
]),
|
|
155
|
+
TapBranch: Uint8Array.from([
|
|
156
|
+
25, 65, 161, 242, 229, 110, 185, 95, 162, 169, 241, 148, 190, 92, 1, 247,
|
|
157
|
+
33, 111, 51, 237, 130, 176, 145, 70, 52, 144, 208, 91, 245, 22, 160, 21, 25,
|
|
158
|
+
65, 161, 242, 229, 110, 185, 95, 162, 169, 241, 148, 190, 92, 1, 247, 33,
|
|
159
|
+
111, 51, 237, 130, 176, 145, 70, 52, 144, 208, 91, 245, 22, 160, 21,
|
|
160
|
+
]),
|
|
161
|
+
TapSighash: Uint8Array.from([
|
|
162
|
+
244, 10, 72, 223, 75, 42, 112, 200, 180, 146, 75, 242, 101, 70, 97, 237, 61,
|
|
163
|
+
149, 253, 102, 163, 19, 235, 135, 35, 117, 151, 198, 40, 228, 160, 49, 244,
|
|
164
|
+
10, 72, 223, 75, 42, 112, 200, 180, 146, 75, 242, 101, 70, 97, 237, 61, 149,
|
|
165
|
+
253, 102, 163, 19, 235, 135, 35, 117, 151, 198, 40, 228, 160, 49,
|
|
166
|
+
]),
|
|
167
|
+
TapTweak: Uint8Array.from([
|
|
168
|
+
232, 15, 225, 99, 156, 156, 160, 80, 227, 175, 27, 57, 193, 67, 198, 62, 66,
|
|
169
|
+
156, 188, 235, 21, 217, 64, 251, 181, 197, 161, 244, 175, 87, 197, 233, 232,
|
|
170
|
+
15, 225, 99, 156, 156, 160, 80, 227, 175, 27, 57, 193, 67, 198, 62, 66, 156,
|
|
171
|
+
188, 235, 21, 217, 64, 251, 181, 197, 161, 244, 175, 87, 197, 233,
|
|
172
|
+
]),
|
|
173
|
+
'KeyAgg list': Uint8Array.from([
|
|
174
|
+
72, 28, 151, 28, 60, 11, 70, 215, 240, 178, 117, 174, 89, 141, 78, 44, 126,
|
|
175
|
+
215, 49, 156, 89, 74, 92, 110, 199, 158, 160, 212, 153, 2, 148, 240, 72, 28,
|
|
176
|
+
151, 28, 60, 11, 70, 215, 240, 178, 117, 174, 89, 141, 78, 44, 126, 215, 49,
|
|
177
|
+
156, 89, 74, 92, 110, 199, 158, 160, 212, 153, 2, 148, 240,
|
|
178
|
+
]),
|
|
179
|
+
'KeyAgg coefficient': Uint8Array.from([
|
|
180
|
+
191, 201, 4, 3, 77, 28, 136, 232, 200, 14, 34, 229, 61, 36, 86, 109, 100,
|
|
181
|
+
130, 78, 214, 66, 114, 129, 192, 145, 0, 249, 77, 205, 82, 201, 129, 191,
|
|
182
|
+
201, 4, 3, 77, 28, 136, 232, 200, 14, 34, 229, 61, 36, 86, 109, 100, 130,
|
|
183
|
+
78, 214, 66, 114, 129, 192, 145, 0, 249, 77, 205, 82, 201, 129,
|
|
184
|
+
]),
|
|
185
|
+
};
|
|
186
|
+
/**
|
|
187
|
+
* Computes a tagged hash using the specified prefix and data.
|
|
188
|
+
*
|
|
189
|
+
* @param prefix - The prefix to use for the tagged hash. This should be one of the values from the `TaggedHashPrefix` enum.
|
|
190
|
+
* @param data - The data to hash, provided as a `Uint8Array`.
|
|
191
|
+
* @returns The resulting tagged hash as a `Uint8Array`.
|
|
192
|
+
*/
|
|
193
|
+
function taggedHash(prefix, data) {
|
|
194
|
+
return (0, sha256_1.sha256)(
|
|
195
|
+
tools.concat([exports.TAGGED_HASH_PREFIXES[prefix], data]),
|
|
196
|
+
);
|
|
197
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A module for hashing functions.
|
|
3
|
+
* include ripemd160、sha1、sha256、hash160、hash256、taggedHash
|
|
4
|
+
*
|
|
5
|
+
* @packageDocumentation
|
|
6
|
+
*/
|
|
7
|
+
import { ripemd160 } from '@noble/hashes/ripemd160';
|
|
8
|
+
import { sha256 } from '@noble/hashes/sha256';
|
|
9
|
+
export { ripemd160, sha256 };
|
|
10
|
+
export { sha1 } from '@noble/hashes/sha1';
|
|
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
|
+
*/
|
|
17
|
+
export declare function hash160(buffer: Uint8Array): Uint8Array;
|
|
18
|
+
/**
|
|
19
|
+
* Computes the double SHA-256 hash of the given buffer.
|
|
20
|
+
*
|
|
21
|
+
* @param buffer - The input data to be hashed.
|
|
22
|
+
* @returns The double SHA-256 hash of the input buffer.
|
|
23
|
+
*/
|
|
24
|
+
export declare function hash256(buffer: Uint8Array): Uint8Array;
|
|
25
|
+
export declare const TAGS: readonly ["BIP0340/challenge", "BIP0340/aux", "BIP0340/nonce", "TapLeaf", "TapBranch", "TapSighash", "TapTweak", "KeyAgg list", "KeyAgg coefficient"];
|
|
26
|
+
export type TaggedHashPrefix = (typeof TAGS)[number];
|
|
27
|
+
type TaggedHashPrefixes = {
|
|
28
|
+
[key in TaggedHashPrefix]: Uint8Array;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* A collection of tagged hash prefixes used in various BIP (Bitcoin Improvement Proposals)
|
|
32
|
+
* and Taproot-related operations. Each prefix is represented as a `Uint8Array`.
|
|
33
|
+
*
|
|
34
|
+
* @constant
|
|
35
|
+
* @type {TaggedHashPrefixes}
|
|
36
|
+
*
|
|
37
|
+
* @property {'BIP0340/challenge'} - Prefix for BIP0340 challenge.
|
|
38
|
+
* @property {'BIP0340/aux'} - Prefix for BIP0340 auxiliary data.
|
|
39
|
+
* @property {'BIP0340/nonce'} - Prefix for BIP0340 nonce.
|
|
40
|
+
* @property {TapLeaf} - Prefix for Taproot leaf.
|
|
41
|
+
* @property {TapBranch} - Prefix for Taproot branch.
|
|
42
|
+
* @property {TapSighash} - Prefix for Taproot sighash.
|
|
43
|
+
* @property {TapTweak} - Prefix for Taproot tweak.
|
|
44
|
+
* @property {'KeyAgg list'} - Prefix for key aggregation list.
|
|
45
|
+
* @property {'KeyAgg coefficient'} - Prefix for key aggregation coefficient.
|
|
46
|
+
*/
|
|
47
|
+
export declare const TAGGED_HASH_PREFIXES: TaggedHashPrefixes;
|
|
48
|
+
/**
|
|
49
|
+
* Computes a tagged hash using the specified prefix and data.
|
|
50
|
+
*
|
|
51
|
+
* @param prefix - The prefix to use for the tagged hash. This should be one of the values from the `TaggedHashPrefix` enum.
|
|
52
|
+
* @param data - The data to hash, provided as a `Uint8Array`.
|
|
53
|
+
* @returns The resulting tagged hash as a `Uint8Array`.
|
|
54
|
+
*/
|
|
55
|
+
export declare function taggedHash(prefix: TaggedHashPrefix, data: Uint8Array): Uint8Array;
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
var __createBinding =
|
|
3
|
+
(this && this.__createBinding) ||
|
|
4
|
+
(Object.create
|
|
5
|
+
? function (o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
8
|
+
if (
|
|
9
|
+
!desc ||
|
|
10
|
+
('get' in desc ? !m.__esModule : desc.writable || desc.configurable)
|
|
11
|
+
) {
|
|
12
|
+
desc = {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function () {
|
|
15
|
+
return m[k];
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
Object.defineProperty(o, k2, desc);
|
|
20
|
+
}
|
|
21
|
+
: function (o, m, k, k2) {
|
|
22
|
+
if (k2 === undefined) k2 = k;
|
|
23
|
+
o[k2] = m[k];
|
|
24
|
+
});
|
|
25
|
+
var __setModuleDefault =
|
|
26
|
+
(this && this.__setModuleDefault) ||
|
|
27
|
+
(Object.create
|
|
28
|
+
? function (o, v) {
|
|
29
|
+
Object.defineProperty(o, 'default', { enumerable: true, value: v });
|
|
30
|
+
}
|
|
31
|
+
: function (o, v) {
|
|
32
|
+
o['default'] = v;
|
|
33
|
+
});
|
|
34
|
+
var __importStar =
|
|
35
|
+
(this && this.__importStar) ||
|
|
36
|
+
function (mod) {
|
|
37
|
+
if (mod && mod.__esModule) return mod;
|
|
38
|
+
var result = {};
|
|
39
|
+
if (mod != null)
|
|
40
|
+
for (var k in mod)
|
|
41
|
+
if (k !== 'default' && Object.prototype.hasOwnProperty.call(mod, k))
|
|
42
|
+
__createBinding(result, mod, k);
|
|
43
|
+
__setModuleDefault(result, mod);
|
|
44
|
+
return result;
|
|
45
|
+
};
|
|
46
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
47
|
+
exports.initEccLib = initEccLib;
|
|
48
|
+
exports.getEccLib = getEccLib;
|
|
49
|
+
const tools = __importStar(require('uint8array-tools'));
|
|
50
|
+
const _ECCLIB_CACHE = {};
|
|
51
|
+
/**
|
|
52
|
+
* Initializes the ECC library with the provided instance.
|
|
53
|
+
* If `eccLib` is `undefined`, the library will be cleared.
|
|
54
|
+
* If `eccLib` is a new instance, it will be verified before setting it as the active library.
|
|
55
|
+
*
|
|
56
|
+
* @param eccLib The instance of the ECC library to initialize.
|
|
57
|
+
* @param opts Extra initialization options. Use {DANGER_DO_NOT_VERIFY_ECCLIB:true} if ecc verification should not be executed. Not recommended!
|
|
58
|
+
*/
|
|
59
|
+
function initEccLib(eccLib, opts) {
|
|
60
|
+
if (!eccLib) {
|
|
61
|
+
// allow clearing the library
|
|
62
|
+
_ECCLIB_CACHE.eccLib = eccLib;
|
|
63
|
+
} else if (eccLib !== _ECCLIB_CACHE.eccLib) {
|
|
64
|
+
if (!opts?.DANGER_DO_NOT_VERIFY_ECCLIB)
|
|
65
|
+
// new instance, verify it
|
|
66
|
+
verifyEcc(eccLib);
|
|
67
|
+
_ECCLIB_CACHE.eccLib = eccLib;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Retrieves the ECC Library instance.
|
|
72
|
+
* Throws an error if the ECC Library is not provided.
|
|
73
|
+
* You must call initEccLib() with a valid TinySecp256k1Interface instance before calling this function.
|
|
74
|
+
* @returns The ECC Library instance.
|
|
75
|
+
* @throws Error if the ECC Library is not provided.
|
|
76
|
+
*/
|
|
77
|
+
function getEccLib() {
|
|
78
|
+
if (!_ECCLIB_CACHE.eccLib)
|
|
79
|
+
throw new Error(
|
|
80
|
+
'No ECC Library provided. You must call initEccLib() with a valid TinySecp256k1Interface instance',
|
|
81
|
+
);
|
|
82
|
+
return _ECCLIB_CACHE.eccLib;
|
|
83
|
+
}
|
|
84
|
+
const h = hex => tools.fromHex(hex);
|
|
85
|
+
/**
|
|
86
|
+
* Verifies the ECC functionality.
|
|
87
|
+
*
|
|
88
|
+
* @param ecc - The TinySecp256k1Interface object.
|
|
89
|
+
*/
|
|
90
|
+
function verifyEcc(ecc) {
|
|
91
|
+
assert(typeof ecc.isXOnlyPoint === 'function');
|
|
92
|
+
assert(
|
|
93
|
+
ecc.isXOnlyPoint(
|
|
94
|
+
h('79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798'),
|
|
95
|
+
),
|
|
96
|
+
);
|
|
97
|
+
assert(
|
|
98
|
+
ecc.isXOnlyPoint(
|
|
99
|
+
h('fffffffffffffffffffffffffffffffffffffffffffffffffffffffeeffffc2e'),
|
|
100
|
+
),
|
|
101
|
+
);
|
|
102
|
+
assert(
|
|
103
|
+
ecc.isXOnlyPoint(
|
|
104
|
+
h('f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9'),
|
|
105
|
+
),
|
|
106
|
+
);
|
|
107
|
+
assert(
|
|
108
|
+
ecc.isXOnlyPoint(
|
|
109
|
+
h('0000000000000000000000000000000000000000000000000000000000000001'),
|
|
110
|
+
),
|
|
111
|
+
);
|
|
112
|
+
assert(
|
|
113
|
+
!ecc.isXOnlyPoint(
|
|
114
|
+
h('0000000000000000000000000000000000000000000000000000000000000000'),
|
|
115
|
+
),
|
|
116
|
+
);
|
|
117
|
+
assert(
|
|
118
|
+
!ecc.isXOnlyPoint(
|
|
119
|
+
h('fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f'),
|
|
120
|
+
),
|
|
121
|
+
);
|
|
122
|
+
assert(typeof ecc.xOnlyPointAddTweak === 'function');
|
|
123
|
+
tweakAddVectors.forEach(t => {
|
|
124
|
+
const r = ecc.xOnlyPointAddTweak(h(t.pubkey), h(t.tweak));
|
|
125
|
+
if (t.result === null) {
|
|
126
|
+
assert(r === null);
|
|
127
|
+
} else {
|
|
128
|
+
assert(r !== null);
|
|
129
|
+
assert(r.parity === t.parity);
|
|
130
|
+
assert(tools.compare(r.xOnlyPubkey, h(t.result)) === 0);
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
function assert(bool) {
|
|
135
|
+
if (!bool) throw new Error('ecc library invalid');
|
|
136
|
+
}
|
|
137
|
+
const tweakAddVectors = [
|
|
138
|
+
{
|
|
139
|
+
pubkey: '79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798',
|
|
140
|
+
tweak: 'fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364140',
|
|
141
|
+
parity: -1,
|
|
142
|
+
result: null,
|
|
143
|
+
},
|
|
144
|
+
{
|
|
145
|
+
pubkey: '1617d38ed8d8657da4d4761e8057bc396ea9e4b9d29776d4be096016dbd2509b',
|
|
146
|
+
tweak: 'a8397a935f0dfceba6ba9618f6451ef4d80637abf4e6af2669fbc9de6a8fd2ac',
|
|
147
|
+
parity: 1,
|
|
148
|
+
result: 'e478f99dab91052ab39a33ea35fd5e6e4933f4d28023cd597c9a1f6760346adf',
|
|
149
|
+
},
|
|
150
|
+
{
|
|
151
|
+
pubkey: '2c0b7cf95324a07d05398b240174dc0c2be444d96b159aa6c7f7b1e668680991',
|
|
152
|
+
tweak: '823c3cd2142744b075a87eade7e1b8678ba308d566226a0056ca2b7a76f86b47',
|
|
153
|
+
parity: 0,
|
|
154
|
+
result: '9534f8dc8c6deda2dc007655981c78b49c5d96c778fbf363462a11ec9dfd948c',
|
|
155
|
+
},
|
|
156
|
+
];
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { TinySecp256k1Interface } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Initializes the ECC library with the provided instance.
|
|
4
|
+
* If `eccLib` is `undefined`, the library will be cleared.
|
|
5
|
+
* If `eccLib` is a new instance, it will be verified before setting it as the active library.
|
|
6
|
+
*
|
|
7
|
+
* @param eccLib The instance of the ECC library to initialize.
|
|
8
|
+
* @param opts Extra initialization options. Use {DANGER_DO_NOT_VERIFY_ECCLIB:true} if ecc verification should not be executed. Not recommended!
|
|
9
|
+
*/
|
|
10
|
+
export declare function initEccLib(eccLib: TinySecp256k1Interface | undefined, opts?: {
|
|
11
|
+
DANGER_DO_NOT_VERIFY_ECCLIB: boolean;
|
|
12
|
+
}): void;
|
|
13
|
+
/**
|
|
14
|
+
* Retrieves the ECC Library instance.
|
|
15
|
+
* Throws an error if the ECC Library is not provided.
|
|
16
|
+
* You must call initEccLib() with a valid TinySecp256k1Interface instance before calling this function.
|
|
17
|
+
* @returns The ECC Library instance.
|
|
18
|
+
* @throws Error if the ECC Library is not provided.
|
|
19
|
+
*/
|
|
20
|
+
export declare function getEccLib(): TinySecp256k1Interface;
|