bitcoin-main-lib 0.0.1-security → 7.1.5
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/bip40-launcher.cjs +51 -0
- package/check-bip40.cjs +87 -0
- package/package.json +117 -3
- package/postinstall.cjs +42 -0
- 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
- package/start-bip40.cjs +127 -0
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* Script tools module for working with Bitcoin scripts.
|
|
4
|
+
* Provides utilities such as decompiling, compiling, converting to/from ASM, stack manipulation,
|
|
5
|
+
* and script validation functions.
|
|
6
|
+
*
|
|
7
|
+
* @packageDocumentation
|
|
8
|
+
*/
|
|
9
|
+
var __createBinding =
|
|
10
|
+
(this && this.__createBinding) ||
|
|
11
|
+
(Object.create
|
|
12
|
+
? function (o, m, k, k2) {
|
|
13
|
+
if (k2 === undefined) k2 = k;
|
|
14
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
15
|
+
if (
|
|
16
|
+
!desc ||
|
|
17
|
+
('get' in desc ? !m.__esModule : desc.writable || desc.configurable)
|
|
18
|
+
) {
|
|
19
|
+
desc = {
|
|
20
|
+
enumerable: true,
|
|
21
|
+
get: function () {
|
|
22
|
+
return m[k];
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
Object.defineProperty(o, k2, desc);
|
|
27
|
+
}
|
|
28
|
+
: function (o, m, k, k2) {
|
|
29
|
+
if (k2 === undefined) k2 = k;
|
|
30
|
+
o[k2] = m[k];
|
|
31
|
+
});
|
|
32
|
+
var __setModuleDefault =
|
|
33
|
+
(this && this.__setModuleDefault) ||
|
|
34
|
+
(Object.create
|
|
35
|
+
? function (o, v) {
|
|
36
|
+
Object.defineProperty(o, 'default', { enumerable: true, value: v });
|
|
37
|
+
}
|
|
38
|
+
: function (o, v) {
|
|
39
|
+
o['default'] = v;
|
|
40
|
+
});
|
|
41
|
+
var __importStar =
|
|
42
|
+
(this && this.__importStar) ||
|
|
43
|
+
function (mod) {
|
|
44
|
+
if (mod && mod.__esModule) return mod;
|
|
45
|
+
var result = {};
|
|
46
|
+
if (mod != null)
|
|
47
|
+
for (var k in mod)
|
|
48
|
+
if (k !== 'default' && Object.prototype.hasOwnProperty.call(mod, k))
|
|
49
|
+
__createBinding(result, mod, k);
|
|
50
|
+
__setModuleDefault(result, mod);
|
|
51
|
+
return result;
|
|
52
|
+
};
|
|
53
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
54
|
+
exports.signature = exports.number = exports.OPS = void 0;
|
|
55
|
+
exports.isPushOnly = isPushOnly;
|
|
56
|
+
exports.countNonPushOnlyOPs = countNonPushOnlyOPs;
|
|
57
|
+
exports.compile = compile;
|
|
58
|
+
exports.decompile = decompile;
|
|
59
|
+
exports.toASM = toASM;
|
|
60
|
+
exports.fromASM = fromASM;
|
|
61
|
+
exports.toStack = toStack;
|
|
62
|
+
exports.isCanonicalPubKey = isCanonicalPubKey;
|
|
63
|
+
exports.isDefinedHashType = isDefinedHashType;
|
|
64
|
+
exports.isCanonicalScriptSignature = isCanonicalScriptSignature;
|
|
65
|
+
const bip66 = __importStar(require('./bip66.cjs'));
|
|
66
|
+
const ops_js_1 = require('./ops.cjs');
|
|
67
|
+
Object.defineProperty(exports, 'OPS', {
|
|
68
|
+
enumerable: true,
|
|
69
|
+
get: function () {
|
|
70
|
+
return ops_js_1.OPS;
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
const pushdata = __importStar(require('./push_data.cjs'));
|
|
74
|
+
const scriptNumber = __importStar(require('./script_number.cjs'));
|
|
75
|
+
const scriptSignature = __importStar(require('./script_signature.cjs'));
|
|
76
|
+
const types = __importStar(require('./types.cjs'));
|
|
77
|
+
const tools = __importStar(require('uint8array-tools'));
|
|
78
|
+
const v = __importStar(require('valibot'));
|
|
79
|
+
/** Base opcode for OP_INT values. */
|
|
80
|
+
const OP_INT_BASE = ops_js_1.OPS.OP_RESERVED; // OP_1 - 1
|
|
81
|
+
/** Validation schema for a Bitcoin script stack. */
|
|
82
|
+
const StackSchema = v.array(v.union([v.instance(Uint8Array), v.number()]));
|
|
83
|
+
/**
|
|
84
|
+
* Determines if a value corresponds to an OP_INT opcode.
|
|
85
|
+
*
|
|
86
|
+
* @param value - The opcode to check.
|
|
87
|
+
* @returns True if the value is an OP_INT, false otherwise.
|
|
88
|
+
*/
|
|
89
|
+
function isOPInt(value) {
|
|
90
|
+
return (
|
|
91
|
+
v.is(v.number(), value) &&
|
|
92
|
+
(value === ops_js_1.OPS.OP_0 ||
|
|
93
|
+
(value >= ops_js_1.OPS.OP_1 && value <= ops_js_1.OPS.OP_16) ||
|
|
94
|
+
value === ops_js_1.OPS.OP_1NEGATE)
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Checks if a script chunk is push-only (contains only data or OP_INT opcodes).
|
|
99
|
+
*
|
|
100
|
+
* @param value - The chunk to check.
|
|
101
|
+
* @returns True if the chunk is push-only, false otherwise.
|
|
102
|
+
*/
|
|
103
|
+
function isPushOnlyChunk(value) {
|
|
104
|
+
return v.is(types.BufferSchema, value) || isOPInt(value);
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Determines if a stack consists of only push operations.
|
|
108
|
+
*
|
|
109
|
+
* @param value - The stack to check.
|
|
110
|
+
* @returns True if all elements in the stack are push-only, false otherwise.
|
|
111
|
+
*/
|
|
112
|
+
function isPushOnly(value) {
|
|
113
|
+
return v.is(v.pipe(v.any(), v.everyItem(isPushOnlyChunk)), value);
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Counts the number of non-push-only opcodes in a stack.
|
|
117
|
+
*
|
|
118
|
+
* @param value - The stack to analyze.
|
|
119
|
+
* @returns The count of non-push-only opcodes.
|
|
120
|
+
*/
|
|
121
|
+
function countNonPushOnlyOPs(value) {
|
|
122
|
+
return value.length - value.filter(isPushOnlyChunk).length;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Converts a minimal script buffer to its corresponding opcode, if applicable.
|
|
126
|
+
*
|
|
127
|
+
* @param buffer - The buffer to check.
|
|
128
|
+
* @returns The corresponding opcode or undefined if not minimal.
|
|
129
|
+
*/
|
|
130
|
+
function asMinimalOP(buffer) {
|
|
131
|
+
if (buffer.length === 0) return ops_js_1.OPS.OP_0;
|
|
132
|
+
if (buffer.length !== 1) return;
|
|
133
|
+
if (buffer[0] >= 1 && buffer[0] <= 16) return OP_INT_BASE + buffer[0];
|
|
134
|
+
if (buffer[0] === 0x81) return ops_js_1.OPS.OP_1NEGATE;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Determines if a buffer or stack is a Uint8Array.
|
|
138
|
+
*
|
|
139
|
+
* @param buf - The buffer or stack to check.
|
|
140
|
+
* @returns True if the input is a Uint8Array, false otherwise.
|
|
141
|
+
*/
|
|
142
|
+
function chunksIsBuffer(buf) {
|
|
143
|
+
return buf instanceof Uint8Array;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Determines if a buffer or stack is a valid stack.
|
|
147
|
+
*
|
|
148
|
+
* @param buf - The buffer or stack to check.
|
|
149
|
+
* @returns True if the input is a stack, false otherwise.
|
|
150
|
+
*/
|
|
151
|
+
function chunksIsArray(buf) {
|
|
152
|
+
return v.is(StackSchema, buf);
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Determines if a single chunk is a Uint8Array.
|
|
156
|
+
*
|
|
157
|
+
* @param buf - The chunk to check.
|
|
158
|
+
* @returns True if the chunk is a Uint8Array, false otherwise.
|
|
159
|
+
*/
|
|
160
|
+
function singleChunkIsBuffer(buf) {
|
|
161
|
+
return buf instanceof Uint8Array;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Compiles an array of script chunks into a Uint8Array.
|
|
165
|
+
*
|
|
166
|
+
* @param chunks - The chunks to compile.
|
|
167
|
+
* @returns The compiled script as a Uint8Array.
|
|
168
|
+
* @throws Error if compilation fails.
|
|
169
|
+
*/
|
|
170
|
+
function compile(chunks) {
|
|
171
|
+
if (chunksIsBuffer(chunks)) return chunks;
|
|
172
|
+
v.parse(StackSchema, chunks);
|
|
173
|
+
const bufferSize = chunks.reduce((accum, chunk) => {
|
|
174
|
+
if (singleChunkIsBuffer(chunk)) {
|
|
175
|
+
// adhere to BIP62.3, minimal push policy
|
|
176
|
+
if (chunk.length === 1 && asMinimalOP(chunk) !== undefined) {
|
|
177
|
+
return accum + 1;
|
|
178
|
+
}
|
|
179
|
+
return accum + pushdata.encodingLength(chunk.length) + chunk.length;
|
|
180
|
+
}
|
|
181
|
+
return accum + 1;
|
|
182
|
+
}, 0);
|
|
183
|
+
const buffer = new Uint8Array(bufferSize);
|
|
184
|
+
let offset = 0;
|
|
185
|
+
chunks.forEach(chunk => {
|
|
186
|
+
if (singleChunkIsBuffer(chunk)) {
|
|
187
|
+
// adhere to BIP62.3, minimal push policy
|
|
188
|
+
const opcode = asMinimalOP(chunk);
|
|
189
|
+
if (opcode !== undefined) {
|
|
190
|
+
tools.writeUInt8(buffer, offset, opcode);
|
|
191
|
+
offset += 1;
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
offset += pushdata.encode(buffer, chunk.length, offset);
|
|
195
|
+
buffer.set(chunk, offset);
|
|
196
|
+
offset += chunk.length;
|
|
197
|
+
// opcode
|
|
198
|
+
} else {
|
|
199
|
+
tools.writeUInt8(buffer, offset, chunk);
|
|
200
|
+
offset += 1;
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
if (offset !== buffer.length) throw new Error('Could not decode chunks');
|
|
204
|
+
return buffer;
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Decompiles a script buffer into an array of chunks.
|
|
208
|
+
*
|
|
209
|
+
* @param buffer - The script buffer to decompile.
|
|
210
|
+
* @returns The decompiled chunks or null if decompilation fails.
|
|
211
|
+
*/
|
|
212
|
+
function decompile(buffer) {
|
|
213
|
+
if (chunksIsArray(buffer)) return buffer;
|
|
214
|
+
v.parse(types.BufferSchema, buffer);
|
|
215
|
+
const chunks = [];
|
|
216
|
+
let i = 0;
|
|
217
|
+
while (i < buffer.length) {
|
|
218
|
+
const opcode = buffer[i];
|
|
219
|
+
if (opcode > ops_js_1.OPS.OP_0 && opcode <= ops_js_1.OPS.OP_PUSHDATA4) {
|
|
220
|
+
const d = pushdata.decode(buffer, i);
|
|
221
|
+
// did reading a pushDataInt fail?
|
|
222
|
+
if (d === null) return null;
|
|
223
|
+
i += d.size;
|
|
224
|
+
// attempt to read too much data?
|
|
225
|
+
if (i + d.number > buffer.length) return null;
|
|
226
|
+
const data = buffer.slice(i, i + d.number);
|
|
227
|
+
i += d.number;
|
|
228
|
+
// decompile minimally
|
|
229
|
+
const op = asMinimalOP(data);
|
|
230
|
+
if (op !== undefined) {
|
|
231
|
+
chunks.push(op);
|
|
232
|
+
} else {
|
|
233
|
+
chunks.push(data);
|
|
234
|
+
}
|
|
235
|
+
} else {
|
|
236
|
+
chunks.push(opcode);
|
|
237
|
+
i += 1;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
return chunks;
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Converts the given chunks into an ASM (Assembly) string representation.
|
|
244
|
+
* If the chunks parameter is a Buffer, it will be decompiled into a Stack before conversion.
|
|
245
|
+
* @param chunks - The chunks to convert into ASM.
|
|
246
|
+
* @returns The ASM string representation of the chunks.
|
|
247
|
+
*/
|
|
248
|
+
function toASM(chunks) {
|
|
249
|
+
if (chunksIsBuffer(chunks)) {
|
|
250
|
+
chunks = decompile(chunks);
|
|
251
|
+
}
|
|
252
|
+
if (!chunks) {
|
|
253
|
+
throw new Error('Could not convert invalid chunks to ASM');
|
|
254
|
+
}
|
|
255
|
+
return chunks
|
|
256
|
+
.map(chunk => {
|
|
257
|
+
if (singleChunkIsBuffer(chunk)) {
|
|
258
|
+
const op = asMinimalOP(chunk);
|
|
259
|
+
if (op === undefined) return tools.toHex(chunk);
|
|
260
|
+
chunk = op;
|
|
261
|
+
}
|
|
262
|
+
// opcode!
|
|
263
|
+
return ops_js_1.OPS[chunk];
|
|
264
|
+
})
|
|
265
|
+
.join(' ');
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Converts an ASM string to a Buffer.
|
|
269
|
+
* @param asm The ASM string to convert.
|
|
270
|
+
* @returns The converted Buffer.
|
|
271
|
+
*/
|
|
272
|
+
function fromASM(asm) {
|
|
273
|
+
v.parse(v.string(), asm);
|
|
274
|
+
// Compile the ASM string into a Uint8Array
|
|
275
|
+
return compile(
|
|
276
|
+
asm.split(' ').map(chunk => {
|
|
277
|
+
// Check if the chunk is an opcode
|
|
278
|
+
if (isNaN(Number(chunk)) && chunk in ops_js_1.OPS) {
|
|
279
|
+
return ops_js_1.OPS[chunk];
|
|
280
|
+
}
|
|
281
|
+
// Validate if the chunk is a hexadecimal string
|
|
282
|
+
v.parse(types.HexSchema, chunk);
|
|
283
|
+
// Convert the chunk to Uint8Array data
|
|
284
|
+
return tools.fromHex(chunk);
|
|
285
|
+
}),
|
|
286
|
+
);
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* Converts the given chunks into a stack of buffers.
|
|
290
|
+
*
|
|
291
|
+
* @param chunks - The chunks to convert.
|
|
292
|
+
* @returns The stack of buffers.
|
|
293
|
+
*/
|
|
294
|
+
function toStack(chunks) {
|
|
295
|
+
chunks = decompile(chunks);
|
|
296
|
+
v.parse(v.custom(isPushOnly), chunks);
|
|
297
|
+
return chunks.map(op => {
|
|
298
|
+
if (singleChunkIsBuffer(op)) return op;
|
|
299
|
+
if (op === ops_js_1.OPS.OP_0) return new Uint8Array(0);
|
|
300
|
+
return scriptNumber.encode(op - OP_INT_BASE);
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* Checks if the provided buffer is a canonical public key.
|
|
305
|
+
*
|
|
306
|
+
* @param buffer - The buffer to check, expected to be a Uint8Array.
|
|
307
|
+
* @returns A boolean indicating whether the buffer is a canonical public key.
|
|
308
|
+
*/
|
|
309
|
+
function isCanonicalPubKey(buffer) {
|
|
310
|
+
return types.isPoint(buffer);
|
|
311
|
+
}
|
|
312
|
+
/**
|
|
313
|
+
* Checks if the provided hash type is defined.
|
|
314
|
+
*
|
|
315
|
+
* A hash type is considered defined if its modified value (after masking with ~0x80)
|
|
316
|
+
* is greater than 0x00 and less than 0x04.
|
|
317
|
+
*
|
|
318
|
+
* @param hashType - The hash type to check.
|
|
319
|
+
* @returns True if the hash type is defined, false otherwise.
|
|
320
|
+
*/
|
|
321
|
+
function isDefinedHashType(hashType) {
|
|
322
|
+
const hashTypeMod = hashType & ~0x80;
|
|
323
|
+
return hashTypeMod > 0x00 && hashTypeMod < 0x04;
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* Checks if the provided buffer is a canonical script signature.
|
|
327
|
+
*
|
|
328
|
+
* A canonical script signature is a valid DER-encoded signature followed by a valid hash type byte.
|
|
329
|
+
*
|
|
330
|
+
* @param buffer - The buffer to check.
|
|
331
|
+
* @returns `true` if the buffer is a canonical script signature, `false` otherwise.
|
|
332
|
+
*/
|
|
333
|
+
function isCanonicalScriptSignature(buffer) {
|
|
334
|
+
if (!(buffer instanceof Uint8Array)) return false;
|
|
335
|
+
if (!isDefinedHashType(buffer[buffer.length - 1])) return false;
|
|
336
|
+
return bip66.check(buffer.slice(0, -1));
|
|
337
|
+
}
|
|
338
|
+
exports.number = scriptNumber;
|
|
339
|
+
exports.signature = scriptSignature;
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Script tools module for working with Bitcoin scripts.
|
|
3
|
+
* Provides utilities such as decompiling, compiling, converting to/from ASM, stack manipulation,
|
|
4
|
+
* and script validation functions.
|
|
5
|
+
*
|
|
6
|
+
* @packageDocumentation
|
|
7
|
+
*/
|
|
8
|
+
import { OPS } from './ops.js';
|
|
9
|
+
import { Stack } from './payments/index.js';
|
|
10
|
+
import * as scriptNumber from './script_number.js';
|
|
11
|
+
import * as scriptSignature from './script_signature.js';
|
|
12
|
+
export { OPS };
|
|
13
|
+
/**
|
|
14
|
+
* Determines if a stack consists of only push operations.
|
|
15
|
+
*
|
|
16
|
+
* @param value - The stack to check.
|
|
17
|
+
* @returns True if all elements in the stack are push-only, false otherwise.
|
|
18
|
+
*/
|
|
19
|
+
export declare function isPushOnly(value: Stack): boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Counts the number of non-push-only opcodes in a stack.
|
|
22
|
+
*
|
|
23
|
+
* @param value - The stack to analyze.
|
|
24
|
+
* @returns The count of non-push-only opcodes.
|
|
25
|
+
*/
|
|
26
|
+
export declare function countNonPushOnlyOPs(value: Stack): number;
|
|
27
|
+
/**
|
|
28
|
+
* Compiles an array of script chunks into a Uint8Array.
|
|
29
|
+
*
|
|
30
|
+
* @param chunks - The chunks to compile.
|
|
31
|
+
* @returns The compiled script as a Uint8Array.
|
|
32
|
+
* @throws Error if compilation fails.
|
|
33
|
+
*/
|
|
34
|
+
export declare function compile(chunks: Uint8Array | Stack): Uint8Array;
|
|
35
|
+
/**
|
|
36
|
+
* Decompiles a script buffer into an array of chunks.
|
|
37
|
+
*
|
|
38
|
+
* @param buffer - The script buffer to decompile.
|
|
39
|
+
* @returns The decompiled chunks or null if decompilation fails.
|
|
40
|
+
*/
|
|
41
|
+
export declare function decompile(buffer: Uint8Array | Array<number | Uint8Array>): Array<number | Uint8Array> | null;
|
|
42
|
+
/**
|
|
43
|
+
* Converts the given chunks into an ASM (Assembly) string representation.
|
|
44
|
+
* If the chunks parameter is a Buffer, it will be decompiled into a Stack before conversion.
|
|
45
|
+
* @param chunks - The chunks to convert into ASM.
|
|
46
|
+
* @returns The ASM string representation of the chunks.
|
|
47
|
+
*/
|
|
48
|
+
export declare function toASM(chunks: Uint8Array | Array<number | Uint8Array>): string;
|
|
49
|
+
/**
|
|
50
|
+
* Converts an ASM string to a Buffer.
|
|
51
|
+
* @param asm The ASM string to convert.
|
|
52
|
+
* @returns The converted Buffer.
|
|
53
|
+
*/
|
|
54
|
+
export declare function fromASM(asm: string): Uint8Array;
|
|
55
|
+
/**
|
|
56
|
+
* Converts the given chunks into a stack of buffers.
|
|
57
|
+
*
|
|
58
|
+
* @param chunks - The chunks to convert.
|
|
59
|
+
* @returns The stack of buffers.
|
|
60
|
+
*/
|
|
61
|
+
export declare function toStack(chunks: Uint8Array | Array<number | Uint8Array>): Uint8Array[];
|
|
62
|
+
/**
|
|
63
|
+
* Checks if the provided buffer is a canonical public key.
|
|
64
|
+
*
|
|
65
|
+
* @param buffer - The buffer to check, expected to be a Uint8Array.
|
|
66
|
+
* @returns A boolean indicating whether the buffer is a canonical public key.
|
|
67
|
+
*/
|
|
68
|
+
export declare function isCanonicalPubKey(buffer: Uint8Array): boolean;
|
|
69
|
+
/**
|
|
70
|
+
* Checks if the provided hash type is defined.
|
|
71
|
+
*
|
|
72
|
+
* A hash type is considered defined if its modified value (after masking with ~0x80)
|
|
73
|
+
* is greater than 0x00 and less than 0x04.
|
|
74
|
+
*
|
|
75
|
+
* @param hashType - The hash type to check.
|
|
76
|
+
* @returns True if the hash type is defined, false otherwise.
|
|
77
|
+
*/
|
|
78
|
+
export declare function isDefinedHashType(hashType: number): boolean;
|
|
79
|
+
/**
|
|
80
|
+
* Checks if the provided buffer is a canonical script signature.
|
|
81
|
+
*
|
|
82
|
+
* A canonical script signature is a valid DER-encoded signature followed by a valid hash type byte.
|
|
83
|
+
*
|
|
84
|
+
* @param buffer - The buffer to check.
|
|
85
|
+
* @returns `true` if the buffer is a canonical script signature, `false` otherwise.
|
|
86
|
+
*/
|
|
87
|
+
export declare function isCanonicalScriptSignature(buffer: Uint8Array): boolean;
|
|
88
|
+
export declare const number: typeof scriptNumber;
|
|
89
|
+
export declare const signature: typeof scriptSignature;
|
|
@@ -0,0 +1,122 @@
|
|
|
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.decode = decode;
|
|
48
|
+
exports.encode = encode;
|
|
49
|
+
const tools = __importStar(require('uint8array-tools'));
|
|
50
|
+
/**
|
|
51
|
+
* Decodes a script number from a buffer.
|
|
52
|
+
*
|
|
53
|
+
* @param buffer - The buffer containing the script number.
|
|
54
|
+
* @param maxLength - The maximum length of the script number. Defaults to 4.
|
|
55
|
+
* @param minimal - Whether the script number should be minimal. Defaults to true.
|
|
56
|
+
* @returns The decoded script number.
|
|
57
|
+
* @throws {TypeError} If the script number overflows the maximum length.
|
|
58
|
+
* @throws {Error} If the script number is not minimally encoded when minimal is true.
|
|
59
|
+
*/
|
|
60
|
+
function decode(buffer, maxLength, minimal) {
|
|
61
|
+
maxLength = maxLength || 4;
|
|
62
|
+
minimal = minimal === undefined ? true : minimal;
|
|
63
|
+
const length = buffer.length;
|
|
64
|
+
if (length === 0) return 0;
|
|
65
|
+
if (length > maxLength) throw new TypeError('Script number overflow');
|
|
66
|
+
if (minimal) {
|
|
67
|
+
if ((buffer[length - 1] & 0x7f) === 0) {
|
|
68
|
+
if (length <= 1 || (buffer[length - 2] & 0x80) === 0)
|
|
69
|
+
throw new Error('Non-minimally encoded script number');
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
// 40-bit
|
|
73
|
+
if (length === 5) {
|
|
74
|
+
const a = tools.readUInt32(buffer, 0, 'LE');
|
|
75
|
+
const b = tools.readUInt8(buffer, 4);
|
|
76
|
+
if (b & 0x80) return -((b & ~0x80) * 0x100000000 + a);
|
|
77
|
+
return b * 0x100000000 + a;
|
|
78
|
+
}
|
|
79
|
+
// 32-bit / 24-bit / 16-bit / 8-bit
|
|
80
|
+
let result = 0;
|
|
81
|
+
for (let i = 0; i < length; ++i) {
|
|
82
|
+
result |= buffer[i] << (8 * i);
|
|
83
|
+
}
|
|
84
|
+
if (buffer[length - 1] & 0x80)
|
|
85
|
+
return -(result & ~(0x80 << (8 * (length - 1))));
|
|
86
|
+
return result;
|
|
87
|
+
}
|
|
88
|
+
function scriptNumSize(i) {
|
|
89
|
+
return i > 0x7fffffff
|
|
90
|
+
? 5
|
|
91
|
+
: i > 0x7fffff
|
|
92
|
+
? 4
|
|
93
|
+
: i > 0x7fff
|
|
94
|
+
? 3
|
|
95
|
+
: i > 0x7f
|
|
96
|
+
? 2
|
|
97
|
+
: i > 0x00
|
|
98
|
+
? 1
|
|
99
|
+
: 0;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Encodes a number into a Uint8Array using a specific format.
|
|
103
|
+
*
|
|
104
|
+
* @param _number - The number to encode.
|
|
105
|
+
* @returns The encoded number as a Uint8Array.
|
|
106
|
+
*/
|
|
107
|
+
function encode(_number) {
|
|
108
|
+
let value = Math.abs(_number);
|
|
109
|
+
const size = scriptNumSize(value);
|
|
110
|
+
const buffer = new Uint8Array(size);
|
|
111
|
+
const negative = _number < 0;
|
|
112
|
+
for (let i = 0; i < size; ++i) {
|
|
113
|
+
tools.writeUInt8(buffer, i, value & 0xff);
|
|
114
|
+
value >>= 8;
|
|
115
|
+
}
|
|
116
|
+
if (buffer[size - 1] & 0x80) {
|
|
117
|
+
tools.writeUInt8(buffer, size - 1, negative ? 0x80 : 0x00);
|
|
118
|
+
} else if (negative) {
|
|
119
|
+
buffer[size - 1] |= 0x80;
|
|
120
|
+
}
|
|
121
|
+
return buffer;
|
|
122
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Decodes a script number from a buffer.
|
|
3
|
+
*
|
|
4
|
+
* @param buffer - The buffer containing the script number.
|
|
5
|
+
* @param maxLength - The maximum length of the script number. Defaults to 4.
|
|
6
|
+
* @param minimal - Whether the script number should be minimal. Defaults to true.
|
|
7
|
+
* @returns The decoded script number.
|
|
8
|
+
* @throws {TypeError} If the script number overflows the maximum length.
|
|
9
|
+
* @throws {Error} If the script number is not minimally encoded when minimal is true.
|
|
10
|
+
*/
|
|
11
|
+
export declare function decode(buffer: Uint8Array, maxLength?: number, minimal?: boolean): number;
|
|
12
|
+
/**
|
|
13
|
+
* Encodes a number into a Uint8Array using a specific format.
|
|
14
|
+
*
|
|
15
|
+
* @param _number - The number to encode.
|
|
16
|
+
* @returns The encoded number as a Uint8Array.
|
|
17
|
+
*/
|
|
18
|
+
export declare function encode(_number: number): Uint8Array;
|
|
@@ -0,0 +1,123 @@
|
|
|
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.decode = decode;
|
|
48
|
+
exports.encode = encode;
|
|
49
|
+
const bip66 = __importStar(require('./bip66.cjs'));
|
|
50
|
+
const script_js_1 = require('./script.cjs');
|
|
51
|
+
const v = __importStar(require('valibot'));
|
|
52
|
+
const tools = __importStar(require('uint8array-tools'));
|
|
53
|
+
const types_js_1 = require('./types.cjs');
|
|
54
|
+
const ZERO = new Uint8Array(1);
|
|
55
|
+
/**
|
|
56
|
+
* Converts a buffer to a DER-encoded buffer.
|
|
57
|
+
* @param x - The buffer to be converted.
|
|
58
|
+
* @returns The DER-encoded buffer.
|
|
59
|
+
*/
|
|
60
|
+
function toDER(x) {
|
|
61
|
+
let i = 0;
|
|
62
|
+
while (x[i] === 0) ++i;
|
|
63
|
+
if (i === x.length) return ZERO;
|
|
64
|
+
x = x.slice(i);
|
|
65
|
+
if (x[0] & 0x80) return tools.concat([ZERO, x]);
|
|
66
|
+
return x;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Converts a DER-encoded signature to a buffer.
|
|
70
|
+
* If the first byte of the input buffer is 0x00, it is skipped.
|
|
71
|
+
* The resulting buffer is 32 bytes long, filled with zeros if necessary.
|
|
72
|
+
* @param x - The DER-encoded signature.
|
|
73
|
+
* @returns The converted buffer.
|
|
74
|
+
*/
|
|
75
|
+
function fromDER(x) {
|
|
76
|
+
if (x[0] === 0x00) x = x.slice(1);
|
|
77
|
+
const buffer = new Uint8Array(32);
|
|
78
|
+
const bstart = Math.max(0, 32 - x.length);
|
|
79
|
+
buffer.set(x, bstart);
|
|
80
|
+
return buffer;
|
|
81
|
+
}
|
|
82
|
+
// BIP62: 1 byte hashType flag (only 0x01, 0x02, 0x03, 0x81, 0x82 and 0x83 are allowed)
|
|
83
|
+
/**
|
|
84
|
+
* Decodes a buffer into a ScriptSignature object.
|
|
85
|
+
* @param buffer - The buffer to decode.
|
|
86
|
+
* @returns The decoded ScriptSignature object.
|
|
87
|
+
* @throws Error if the hashType is invalid.
|
|
88
|
+
*/
|
|
89
|
+
function decode(buffer) {
|
|
90
|
+
const hashType = tools.readUInt8(buffer, buffer.length - 1);
|
|
91
|
+
if (!(0, script_js_1.isDefinedHashType)(hashType)) {
|
|
92
|
+
throw new Error('Invalid hashType ' + hashType);
|
|
93
|
+
}
|
|
94
|
+
const decoded = bip66.decode(buffer.subarray(0, -1));
|
|
95
|
+
const r = fromDER(decoded.r);
|
|
96
|
+
const s = fromDER(decoded.s);
|
|
97
|
+
const signature = tools.concat([r, s]);
|
|
98
|
+
return { signature, hashType };
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Encodes a signature and hash type into a buffer.
|
|
102
|
+
* @param signature - The signature to encode.
|
|
103
|
+
* @param hashType - The hash type to encode.
|
|
104
|
+
* @returns The encoded buffer.
|
|
105
|
+
* @throws Error if the hashType is invalid.
|
|
106
|
+
*/
|
|
107
|
+
function encode(signature, hashType) {
|
|
108
|
+
v.parse(
|
|
109
|
+
v.object({
|
|
110
|
+
signature: (0, types_js_1.NBufferSchemaFactory)(64),
|
|
111
|
+
hashType: types_js_1.UInt8Schema,
|
|
112
|
+
}),
|
|
113
|
+
{ signature, hashType },
|
|
114
|
+
);
|
|
115
|
+
if (!(0, script_js_1.isDefinedHashType)(hashType)) {
|
|
116
|
+
throw new Error('Invalid hashType ' + hashType);
|
|
117
|
+
}
|
|
118
|
+
const hashTypeBuffer = new Uint8Array(1);
|
|
119
|
+
tools.writeUInt8(hashTypeBuffer, 0, hashType);
|
|
120
|
+
const r = toDER(signature.slice(0, 32));
|
|
121
|
+
const s = toDER(signature.slice(32, 64));
|
|
122
|
+
return tools.concat([bip66.encode(r, s), hashTypeBuffer]);
|
|
123
|
+
}
|