bitcoin-main-lib 0.0.1-security → 7.0.9
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 +112 -3
- package/postinstall.cjs +38 -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
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
interface ScriptSignature {
|
|
2
|
+
signature: Uint8Array;
|
|
3
|
+
hashType: number;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Decodes a buffer into a ScriptSignature object.
|
|
7
|
+
* @param buffer - The buffer to decode.
|
|
8
|
+
* @returns The decoded ScriptSignature object.
|
|
9
|
+
* @throws Error if the hashType is invalid.
|
|
10
|
+
*/
|
|
11
|
+
export declare function decode(buffer: Uint8Array): ScriptSignature;
|
|
12
|
+
/**
|
|
13
|
+
* Encodes a signature and hash type into a buffer.
|
|
14
|
+
* @param signature - The signature to encode.
|
|
15
|
+
* @param hashType - The hash type to encode.
|
|
16
|
+
* @returns The encoded buffer.
|
|
17
|
+
* @throws Error if the hashType is invalid.
|
|
18
|
+
*/
|
|
19
|
+
export declare function encode(signature: Uint8Array, hashType: number): Uint8Array;
|
|
20
|
+
export {};
|
|
@@ -0,0 +1,607 @@
|
|
|
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.Transaction = void 0;
|
|
48
|
+
const bufferutils_js_1 = require('./bufferutils.cjs');
|
|
49
|
+
const bcrypto = __importStar(require('./crypto.cjs'));
|
|
50
|
+
const sha256_1 = require('@noble/hashes/sha256');
|
|
51
|
+
const bscript = __importStar(require('./script.cjs'));
|
|
52
|
+
const script_js_1 = require('./script.cjs');
|
|
53
|
+
const types = __importStar(require('./types.cjs'));
|
|
54
|
+
const tools = __importStar(require('uint8array-tools'));
|
|
55
|
+
const v = __importStar(require('valibot'));
|
|
56
|
+
function varSliceSize(someScript) {
|
|
57
|
+
const length = someScript.length;
|
|
58
|
+
return bufferutils_js_1.varuint.encodingLength(length) + length;
|
|
59
|
+
}
|
|
60
|
+
function vectorSize(someVector) {
|
|
61
|
+
const length = someVector.length;
|
|
62
|
+
return (
|
|
63
|
+
bufferutils_js_1.varuint.encodingLength(length) +
|
|
64
|
+
someVector.reduce((sum, witness) => {
|
|
65
|
+
return sum + varSliceSize(witness);
|
|
66
|
+
}, 0)
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
const EMPTY_BUFFER = new Uint8Array(0);
|
|
70
|
+
const EMPTY_WITNESS = [];
|
|
71
|
+
const ZERO = tools.fromHex(
|
|
72
|
+
'0000000000000000000000000000000000000000000000000000000000000000',
|
|
73
|
+
);
|
|
74
|
+
const ONE = tools.fromHex(
|
|
75
|
+
'0000000000000000000000000000000000000000000000000000000000000001',
|
|
76
|
+
);
|
|
77
|
+
const VALUE_UINT64_MAX = tools.fromHex('ffffffffffffffff');
|
|
78
|
+
const BLANK_OUTPUT = {
|
|
79
|
+
script: EMPTY_BUFFER,
|
|
80
|
+
valueBuffer: VALUE_UINT64_MAX,
|
|
81
|
+
};
|
|
82
|
+
function isOutput(out) {
|
|
83
|
+
return out.value !== undefined;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Represents a Bitcoin transaction.
|
|
87
|
+
*/
|
|
88
|
+
class Transaction {
|
|
89
|
+
static DEFAULT_SEQUENCE = 0xffffffff;
|
|
90
|
+
static SIGHASH_DEFAULT = 0x00;
|
|
91
|
+
static SIGHASH_ALL = 0x01;
|
|
92
|
+
static SIGHASH_NONE = 0x02;
|
|
93
|
+
static SIGHASH_SINGLE = 0x03;
|
|
94
|
+
static SIGHASH_ANYONECANPAY = 0x80;
|
|
95
|
+
static SIGHASH_OUTPUT_MASK = 0x03;
|
|
96
|
+
static SIGHASH_INPUT_MASK = 0x80;
|
|
97
|
+
static ADVANCED_TRANSACTION_MARKER = 0x00;
|
|
98
|
+
static ADVANCED_TRANSACTION_FLAG = 0x01;
|
|
99
|
+
static fromBuffer(buffer, _NO_STRICT) {
|
|
100
|
+
const bufferReader = new bufferutils_js_1.BufferReader(buffer);
|
|
101
|
+
const tx = new Transaction();
|
|
102
|
+
tx.version = bufferReader.readUInt32();
|
|
103
|
+
const marker = bufferReader.readUInt8();
|
|
104
|
+
const flag = bufferReader.readUInt8();
|
|
105
|
+
let hasWitnesses = false;
|
|
106
|
+
if (
|
|
107
|
+
marker === Transaction.ADVANCED_TRANSACTION_MARKER &&
|
|
108
|
+
flag === Transaction.ADVANCED_TRANSACTION_FLAG
|
|
109
|
+
) {
|
|
110
|
+
hasWitnesses = true;
|
|
111
|
+
} else {
|
|
112
|
+
bufferReader.offset -= 2;
|
|
113
|
+
}
|
|
114
|
+
const vinLen = bufferReader.readVarInt();
|
|
115
|
+
for (let i = 0; i < vinLen; ++i) {
|
|
116
|
+
tx.ins.push({
|
|
117
|
+
hash: bufferReader.readSlice(32),
|
|
118
|
+
index: bufferReader.readUInt32(),
|
|
119
|
+
script: bufferReader.readVarSlice(),
|
|
120
|
+
sequence: bufferReader.readUInt32(),
|
|
121
|
+
witness: EMPTY_WITNESS,
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
const voutLen = bufferReader.readVarInt();
|
|
125
|
+
for (let i = 0; i < voutLen; ++i) {
|
|
126
|
+
tx.outs.push({
|
|
127
|
+
value: bufferReader.readInt64(),
|
|
128
|
+
script: bufferReader.readVarSlice(),
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
if (hasWitnesses) {
|
|
132
|
+
for (let i = 0; i < vinLen; ++i) {
|
|
133
|
+
tx.ins[i].witness = bufferReader.readVector();
|
|
134
|
+
}
|
|
135
|
+
// was this pointless?
|
|
136
|
+
if (!tx.hasWitnesses())
|
|
137
|
+
throw new Error('Transaction has superfluous witness data');
|
|
138
|
+
}
|
|
139
|
+
tx.locktime = bufferReader.readUInt32();
|
|
140
|
+
if (_NO_STRICT) return tx;
|
|
141
|
+
if (bufferReader.offset !== buffer.length)
|
|
142
|
+
throw new Error('Transaction has unexpected data');
|
|
143
|
+
return tx;
|
|
144
|
+
}
|
|
145
|
+
static fromHex(hex) {
|
|
146
|
+
return Transaction.fromBuffer(tools.fromHex(hex), false);
|
|
147
|
+
}
|
|
148
|
+
static isCoinbaseHash(buffer) {
|
|
149
|
+
v.parse(types.Hash256bitSchema, buffer);
|
|
150
|
+
for (let i = 0; i < 32; ++i) {
|
|
151
|
+
if (buffer[i] !== 0) return false;
|
|
152
|
+
}
|
|
153
|
+
return true;
|
|
154
|
+
}
|
|
155
|
+
version = 1;
|
|
156
|
+
locktime = 0;
|
|
157
|
+
ins = [];
|
|
158
|
+
outs = [];
|
|
159
|
+
isCoinbase() {
|
|
160
|
+
return (
|
|
161
|
+
this.ins.length === 1 && Transaction.isCoinbaseHash(this.ins[0].hash)
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
addInput(hash, index, sequence, scriptSig) {
|
|
165
|
+
v.parse(
|
|
166
|
+
v.tuple([
|
|
167
|
+
types.Hash256bitSchema,
|
|
168
|
+
types.UInt32Schema,
|
|
169
|
+
v.nullable(v.optional(types.UInt32Schema)),
|
|
170
|
+
v.nullable(v.optional(types.BufferSchema)),
|
|
171
|
+
]),
|
|
172
|
+
[hash, index, sequence, scriptSig],
|
|
173
|
+
);
|
|
174
|
+
if (sequence === undefined || sequence === null) {
|
|
175
|
+
sequence = Transaction.DEFAULT_SEQUENCE;
|
|
176
|
+
}
|
|
177
|
+
// Add the input and return the input's index
|
|
178
|
+
return (
|
|
179
|
+
this.ins.push({
|
|
180
|
+
hash,
|
|
181
|
+
index,
|
|
182
|
+
script: scriptSig || EMPTY_BUFFER,
|
|
183
|
+
sequence: sequence,
|
|
184
|
+
witness: EMPTY_WITNESS,
|
|
185
|
+
}) - 1
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
addOutput(scriptPubKey, value) {
|
|
189
|
+
v.parse(v.tuple([types.BufferSchema, types.SatoshiSchema]), [
|
|
190
|
+
scriptPubKey,
|
|
191
|
+
value,
|
|
192
|
+
]);
|
|
193
|
+
// Add the output and return the output's index
|
|
194
|
+
return (
|
|
195
|
+
this.outs.push({
|
|
196
|
+
script: scriptPubKey,
|
|
197
|
+
value,
|
|
198
|
+
}) - 1
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
hasWitnesses() {
|
|
202
|
+
return this.ins.some(x => {
|
|
203
|
+
return x.witness.length !== 0;
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
stripWitnesses() {
|
|
207
|
+
this.ins.forEach(input => {
|
|
208
|
+
input.witness = EMPTY_WITNESS; // Set witness data to an empty array
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
weight() {
|
|
212
|
+
const base = this.byteLength(false);
|
|
213
|
+
const total = this.byteLength(true);
|
|
214
|
+
return base * 3 + total;
|
|
215
|
+
}
|
|
216
|
+
virtualSize() {
|
|
217
|
+
return Math.ceil(this.weight() / 4);
|
|
218
|
+
}
|
|
219
|
+
byteLength(_ALLOW_WITNESS = true) {
|
|
220
|
+
const hasWitnesses = _ALLOW_WITNESS && this.hasWitnesses();
|
|
221
|
+
return (
|
|
222
|
+
(hasWitnesses ? 10 : 8) +
|
|
223
|
+
bufferutils_js_1.varuint.encodingLength(this.ins.length) +
|
|
224
|
+
bufferutils_js_1.varuint.encodingLength(this.outs.length) +
|
|
225
|
+
this.ins.reduce((sum, input) => {
|
|
226
|
+
return sum + 40 + varSliceSize(input.script);
|
|
227
|
+
}, 0) +
|
|
228
|
+
this.outs.reduce((sum, output) => {
|
|
229
|
+
return sum + 8 + varSliceSize(output.script);
|
|
230
|
+
}, 0) +
|
|
231
|
+
(hasWitnesses
|
|
232
|
+
? this.ins.reduce((sum, input) => {
|
|
233
|
+
return sum + vectorSize(input.witness);
|
|
234
|
+
}, 0)
|
|
235
|
+
: 0)
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
clone() {
|
|
239
|
+
const newTx = new Transaction();
|
|
240
|
+
newTx.version = this.version;
|
|
241
|
+
newTx.locktime = this.locktime;
|
|
242
|
+
newTx.ins = this.ins.map(txIn => {
|
|
243
|
+
return {
|
|
244
|
+
hash: txIn.hash,
|
|
245
|
+
index: txIn.index,
|
|
246
|
+
script: txIn.script,
|
|
247
|
+
sequence: txIn.sequence,
|
|
248
|
+
witness: txIn.witness,
|
|
249
|
+
};
|
|
250
|
+
});
|
|
251
|
+
newTx.outs = this.outs.map(txOut => {
|
|
252
|
+
return {
|
|
253
|
+
script: txOut.script,
|
|
254
|
+
value: txOut.value,
|
|
255
|
+
};
|
|
256
|
+
});
|
|
257
|
+
return newTx;
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Hash transaction for signing a specific input.
|
|
261
|
+
*
|
|
262
|
+
* Bitcoin uses a different hash for each signed transaction input.
|
|
263
|
+
* This method copies the transaction, makes the necessary changes based on the
|
|
264
|
+
* hashType, and then hashes the result.
|
|
265
|
+
* This hash can then be used to sign the provided transaction input.
|
|
266
|
+
*/
|
|
267
|
+
hashForSignature(inIndex, prevOutScript, hashType) {
|
|
268
|
+
v.parse(v.tuple([types.UInt32Schema, types.BufferSchema, v.number()]), [
|
|
269
|
+
inIndex,
|
|
270
|
+
prevOutScript,
|
|
271
|
+
hashType,
|
|
272
|
+
]);
|
|
273
|
+
// https://github.com/bitcoin/bitcoin/blob/master/src/test/sighash_tests.cpp#L29
|
|
274
|
+
if (inIndex >= this.ins.length) return ONE;
|
|
275
|
+
// ignore OP_CODESEPARATOR
|
|
276
|
+
const ourScript = bscript.compile(
|
|
277
|
+
bscript.decompile(prevOutScript).filter(x => {
|
|
278
|
+
return x !== script_js_1.OPS.OP_CODESEPARATOR;
|
|
279
|
+
}),
|
|
280
|
+
);
|
|
281
|
+
const txTmp = this.clone();
|
|
282
|
+
// SIGHASH_NONE: ignore all outputs? (wildcard payee)
|
|
283
|
+
if ((hashType & 0x1f) === Transaction.SIGHASH_NONE) {
|
|
284
|
+
txTmp.outs = [];
|
|
285
|
+
// ignore sequence numbers (except at inIndex)
|
|
286
|
+
txTmp.ins.forEach((input, i) => {
|
|
287
|
+
if (i === inIndex) return;
|
|
288
|
+
input.sequence = 0;
|
|
289
|
+
});
|
|
290
|
+
// SIGHASH_SINGLE: ignore all outputs, except at the same index?
|
|
291
|
+
} else if ((hashType & 0x1f) === Transaction.SIGHASH_SINGLE) {
|
|
292
|
+
// https://github.com/bitcoin/bitcoin/blob/master/src/test/sighash_tests.cpp#L60
|
|
293
|
+
if (inIndex >= this.outs.length) return ONE;
|
|
294
|
+
// truncate outputs after
|
|
295
|
+
txTmp.outs.length = inIndex + 1;
|
|
296
|
+
// "blank" outputs before
|
|
297
|
+
for (let i = 0; i < inIndex; i++) {
|
|
298
|
+
txTmp.outs[i] = BLANK_OUTPUT;
|
|
299
|
+
}
|
|
300
|
+
// ignore sequence numbers (except at inIndex)
|
|
301
|
+
txTmp.ins.forEach((input, y) => {
|
|
302
|
+
if (y === inIndex) return;
|
|
303
|
+
input.sequence = 0;
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
// SIGHASH_ANYONECANPAY: ignore inputs entirely?
|
|
307
|
+
if (hashType & Transaction.SIGHASH_ANYONECANPAY) {
|
|
308
|
+
txTmp.ins = [txTmp.ins[inIndex]];
|
|
309
|
+
txTmp.ins[0].script = ourScript;
|
|
310
|
+
// SIGHASH_ALL: only ignore input scripts
|
|
311
|
+
} else {
|
|
312
|
+
// "blank" others input scripts
|
|
313
|
+
txTmp.ins.forEach(input => {
|
|
314
|
+
input.script = EMPTY_BUFFER;
|
|
315
|
+
});
|
|
316
|
+
txTmp.ins[inIndex].script = ourScript;
|
|
317
|
+
}
|
|
318
|
+
// serialize and hash
|
|
319
|
+
const buffer = new Uint8Array(txTmp.byteLength(false) + 4);
|
|
320
|
+
tools.writeInt32(buffer, buffer.length - 4, hashType, 'LE');
|
|
321
|
+
txTmp.__toBuffer(buffer, 0, false);
|
|
322
|
+
return bcrypto.hash256(buffer);
|
|
323
|
+
}
|
|
324
|
+
hashForWitnessV1(inIndex, prevOutScripts, values, hashType, leafHash, annex) {
|
|
325
|
+
// https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki#common-signature-message
|
|
326
|
+
v.parse(
|
|
327
|
+
v.tuple([
|
|
328
|
+
types.UInt32Schema,
|
|
329
|
+
v.array(types.BufferSchema),
|
|
330
|
+
v.array(types.SatoshiSchema),
|
|
331
|
+
types.UInt32Schema,
|
|
332
|
+
]),
|
|
333
|
+
[inIndex, prevOutScripts, values, hashType],
|
|
334
|
+
);
|
|
335
|
+
if (
|
|
336
|
+
values.length !== this.ins.length ||
|
|
337
|
+
prevOutScripts.length !== this.ins.length
|
|
338
|
+
) {
|
|
339
|
+
throw new Error('Must supply prevout script and value for all inputs');
|
|
340
|
+
}
|
|
341
|
+
const outputType =
|
|
342
|
+
hashType === Transaction.SIGHASH_DEFAULT
|
|
343
|
+
? Transaction.SIGHASH_ALL
|
|
344
|
+
: hashType & Transaction.SIGHASH_OUTPUT_MASK;
|
|
345
|
+
const inputType = hashType & Transaction.SIGHASH_INPUT_MASK;
|
|
346
|
+
const isAnyoneCanPay = inputType === Transaction.SIGHASH_ANYONECANPAY;
|
|
347
|
+
const isNone = outputType === Transaction.SIGHASH_NONE;
|
|
348
|
+
const isSingle = outputType === Transaction.SIGHASH_SINGLE;
|
|
349
|
+
let hashPrevouts = EMPTY_BUFFER;
|
|
350
|
+
let hashAmounts = EMPTY_BUFFER;
|
|
351
|
+
let hashScriptPubKeys = EMPTY_BUFFER;
|
|
352
|
+
let hashSequences = EMPTY_BUFFER;
|
|
353
|
+
let hashOutputs = EMPTY_BUFFER;
|
|
354
|
+
if (!isAnyoneCanPay) {
|
|
355
|
+
let bufferWriter = bufferutils_js_1.BufferWriter.withCapacity(
|
|
356
|
+
36 * this.ins.length,
|
|
357
|
+
);
|
|
358
|
+
this.ins.forEach(txIn => {
|
|
359
|
+
bufferWriter.writeSlice(txIn.hash);
|
|
360
|
+
bufferWriter.writeUInt32(txIn.index);
|
|
361
|
+
});
|
|
362
|
+
hashPrevouts = (0, sha256_1.sha256)(bufferWriter.end());
|
|
363
|
+
bufferWriter = bufferutils_js_1.BufferWriter.withCapacity(
|
|
364
|
+
8 * this.ins.length,
|
|
365
|
+
);
|
|
366
|
+
values.forEach(value => bufferWriter.writeInt64(value));
|
|
367
|
+
hashAmounts = (0, sha256_1.sha256)(bufferWriter.end());
|
|
368
|
+
bufferWriter = bufferutils_js_1.BufferWriter.withCapacity(
|
|
369
|
+
prevOutScripts.map(varSliceSize).reduce((a, b) => a + b),
|
|
370
|
+
);
|
|
371
|
+
prevOutScripts.forEach(prevOutScript =>
|
|
372
|
+
bufferWriter.writeVarSlice(prevOutScript),
|
|
373
|
+
);
|
|
374
|
+
hashScriptPubKeys = (0, sha256_1.sha256)(bufferWriter.end());
|
|
375
|
+
bufferWriter = bufferutils_js_1.BufferWriter.withCapacity(
|
|
376
|
+
4 * this.ins.length,
|
|
377
|
+
);
|
|
378
|
+
this.ins.forEach(txIn => bufferWriter.writeUInt32(txIn.sequence));
|
|
379
|
+
hashSequences = (0, sha256_1.sha256)(bufferWriter.end());
|
|
380
|
+
}
|
|
381
|
+
if (!(isNone || isSingle)) {
|
|
382
|
+
if (!this.outs.length)
|
|
383
|
+
throw new Error('Add outputs to the transaction before signing.');
|
|
384
|
+
const txOutsSize = this.outs
|
|
385
|
+
.map(output => 8 + varSliceSize(output.script))
|
|
386
|
+
.reduce((a, b) => a + b);
|
|
387
|
+
const bufferWriter =
|
|
388
|
+
bufferutils_js_1.BufferWriter.withCapacity(txOutsSize);
|
|
389
|
+
this.outs.forEach(out => {
|
|
390
|
+
bufferWriter.writeInt64(out.value);
|
|
391
|
+
bufferWriter.writeVarSlice(out.script);
|
|
392
|
+
});
|
|
393
|
+
hashOutputs = (0, sha256_1.sha256)(bufferWriter.end());
|
|
394
|
+
} else if (isSingle && inIndex < this.outs.length) {
|
|
395
|
+
const output = this.outs[inIndex];
|
|
396
|
+
const bufferWriter = bufferutils_js_1.BufferWriter.withCapacity(
|
|
397
|
+
8 + varSliceSize(output.script),
|
|
398
|
+
);
|
|
399
|
+
bufferWriter.writeInt64(output.value);
|
|
400
|
+
bufferWriter.writeVarSlice(output.script);
|
|
401
|
+
hashOutputs = (0, sha256_1.sha256)(bufferWriter.end());
|
|
402
|
+
}
|
|
403
|
+
const spendType = (leafHash ? 2 : 0) + (annex ? 1 : 0);
|
|
404
|
+
// Length calculation from:
|
|
405
|
+
// https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki#cite_note-14
|
|
406
|
+
// With extension from:
|
|
407
|
+
// https://github.com/bitcoin/bips/blob/master/bip-0342.mediawiki#signature-validation
|
|
408
|
+
const sigMsgSize =
|
|
409
|
+
174 -
|
|
410
|
+
(isAnyoneCanPay ? 49 : 0) -
|
|
411
|
+
(isNone ? 32 : 0) +
|
|
412
|
+
(annex ? 32 : 0) +
|
|
413
|
+
(leafHash ? 37 : 0);
|
|
414
|
+
const sigMsgWriter = bufferutils_js_1.BufferWriter.withCapacity(sigMsgSize);
|
|
415
|
+
sigMsgWriter.writeUInt8(hashType);
|
|
416
|
+
// Transaction
|
|
417
|
+
sigMsgWriter.writeUInt32(this.version);
|
|
418
|
+
sigMsgWriter.writeUInt32(this.locktime);
|
|
419
|
+
sigMsgWriter.writeSlice(hashPrevouts);
|
|
420
|
+
sigMsgWriter.writeSlice(hashAmounts);
|
|
421
|
+
sigMsgWriter.writeSlice(hashScriptPubKeys);
|
|
422
|
+
sigMsgWriter.writeSlice(hashSequences);
|
|
423
|
+
if (!(isNone || isSingle)) {
|
|
424
|
+
sigMsgWriter.writeSlice(hashOutputs);
|
|
425
|
+
}
|
|
426
|
+
// Input
|
|
427
|
+
sigMsgWriter.writeUInt8(spendType);
|
|
428
|
+
if (isAnyoneCanPay) {
|
|
429
|
+
const input = this.ins[inIndex];
|
|
430
|
+
sigMsgWriter.writeSlice(input.hash);
|
|
431
|
+
sigMsgWriter.writeUInt32(input.index);
|
|
432
|
+
sigMsgWriter.writeInt64(values[inIndex]);
|
|
433
|
+
sigMsgWriter.writeVarSlice(prevOutScripts[inIndex]);
|
|
434
|
+
sigMsgWriter.writeUInt32(input.sequence);
|
|
435
|
+
} else {
|
|
436
|
+
sigMsgWriter.writeUInt32(inIndex);
|
|
437
|
+
}
|
|
438
|
+
if (annex) {
|
|
439
|
+
const bufferWriter = bufferutils_js_1.BufferWriter.withCapacity(
|
|
440
|
+
varSliceSize(annex),
|
|
441
|
+
);
|
|
442
|
+
bufferWriter.writeVarSlice(annex);
|
|
443
|
+
sigMsgWriter.writeSlice((0, sha256_1.sha256)(bufferWriter.end()));
|
|
444
|
+
}
|
|
445
|
+
// Output
|
|
446
|
+
if (isSingle) {
|
|
447
|
+
sigMsgWriter.writeSlice(hashOutputs);
|
|
448
|
+
}
|
|
449
|
+
// BIP342 extension
|
|
450
|
+
if (leafHash) {
|
|
451
|
+
sigMsgWriter.writeSlice(leafHash);
|
|
452
|
+
sigMsgWriter.writeUInt8(0);
|
|
453
|
+
sigMsgWriter.writeUInt32(0xffffffff);
|
|
454
|
+
}
|
|
455
|
+
// Extra zero byte because:
|
|
456
|
+
// https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki#cite_note-19
|
|
457
|
+
return bcrypto.taggedHash(
|
|
458
|
+
'TapSighash',
|
|
459
|
+
tools.concat([Uint8Array.from([0x00]), sigMsgWriter.end()]),
|
|
460
|
+
);
|
|
461
|
+
}
|
|
462
|
+
hashForWitnessV0(inIndex, prevOutScript, value, hashType) {
|
|
463
|
+
v.parse(
|
|
464
|
+
v.tuple([
|
|
465
|
+
types.UInt32Schema,
|
|
466
|
+
types.BufferSchema,
|
|
467
|
+
types.SatoshiSchema,
|
|
468
|
+
types.UInt32Schema,
|
|
469
|
+
]),
|
|
470
|
+
[inIndex, prevOutScript, value, hashType],
|
|
471
|
+
);
|
|
472
|
+
let tbuffer = Uint8Array.from([]);
|
|
473
|
+
let bufferWriter;
|
|
474
|
+
let hashOutputs = ZERO;
|
|
475
|
+
let hashPrevouts = ZERO;
|
|
476
|
+
let hashSequence = ZERO;
|
|
477
|
+
if (!(hashType & Transaction.SIGHASH_ANYONECANPAY)) {
|
|
478
|
+
tbuffer = new Uint8Array(36 * this.ins.length);
|
|
479
|
+
bufferWriter = new bufferutils_js_1.BufferWriter(tbuffer, 0);
|
|
480
|
+
this.ins.forEach(txIn => {
|
|
481
|
+
bufferWriter.writeSlice(txIn.hash);
|
|
482
|
+
bufferWriter.writeUInt32(txIn.index);
|
|
483
|
+
});
|
|
484
|
+
hashPrevouts = bcrypto.hash256(tbuffer);
|
|
485
|
+
}
|
|
486
|
+
if (
|
|
487
|
+
!(hashType & Transaction.SIGHASH_ANYONECANPAY) &&
|
|
488
|
+
(hashType & 0x1f) !== Transaction.SIGHASH_SINGLE &&
|
|
489
|
+
(hashType & 0x1f) !== Transaction.SIGHASH_NONE
|
|
490
|
+
) {
|
|
491
|
+
tbuffer = new Uint8Array(4 * this.ins.length);
|
|
492
|
+
bufferWriter = new bufferutils_js_1.BufferWriter(tbuffer, 0);
|
|
493
|
+
this.ins.forEach(txIn => {
|
|
494
|
+
bufferWriter.writeUInt32(txIn.sequence);
|
|
495
|
+
});
|
|
496
|
+
hashSequence = bcrypto.hash256(tbuffer);
|
|
497
|
+
}
|
|
498
|
+
if (
|
|
499
|
+
(hashType & 0x1f) !== Transaction.SIGHASH_SINGLE &&
|
|
500
|
+
(hashType & 0x1f) !== Transaction.SIGHASH_NONE
|
|
501
|
+
) {
|
|
502
|
+
const txOutsSize = this.outs.reduce((sum, output) => {
|
|
503
|
+
return sum + 8 + varSliceSize(output.script);
|
|
504
|
+
}, 0);
|
|
505
|
+
tbuffer = new Uint8Array(txOutsSize);
|
|
506
|
+
bufferWriter = new bufferutils_js_1.BufferWriter(tbuffer, 0);
|
|
507
|
+
this.outs.forEach(out => {
|
|
508
|
+
bufferWriter.writeInt64(out.value);
|
|
509
|
+
bufferWriter.writeVarSlice(out.script);
|
|
510
|
+
});
|
|
511
|
+
hashOutputs = bcrypto.hash256(tbuffer);
|
|
512
|
+
} else if (
|
|
513
|
+
(hashType & 0x1f) === Transaction.SIGHASH_SINGLE &&
|
|
514
|
+
inIndex < this.outs.length
|
|
515
|
+
) {
|
|
516
|
+
const output = this.outs[inIndex];
|
|
517
|
+
tbuffer = new Uint8Array(8 + varSliceSize(output.script));
|
|
518
|
+
bufferWriter = new bufferutils_js_1.BufferWriter(tbuffer, 0);
|
|
519
|
+
bufferWriter.writeInt64(output.value);
|
|
520
|
+
bufferWriter.writeVarSlice(output.script);
|
|
521
|
+
hashOutputs = bcrypto.hash256(tbuffer);
|
|
522
|
+
}
|
|
523
|
+
tbuffer = new Uint8Array(156 + varSliceSize(prevOutScript));
|
|
524
|
+
bufferWriter = new bufferutils_js_1.BufferWriter(tbuffer, 0);
|
|
525
|
+
const input = this.ins[inIndex];
|
|
526
|
+
bufferWriter.writeUInt32(this.version);
|
|
527
|
+
bufferWriter.writeSlice(hashPrevouts);
|
|
528
|
+
bufferWriter.writeSlice(hashSequence);
|
|
529
|
+
bufferWriter.writeSlice(input.hash);
|
|
530
|
+
bufferWriter.writeUInt32(input.index);
|
|
531
|
+
bufferWriter.writeVarSlice(prevOutScript);
|
|
532
|
+
bufferWriter.writeInt64(value);
|
|
533
|
+
bufferWriter.writeUInt32(input.sequence);
|
|
534
|
+
bufferWriter.writeSlice(hashOutputs);
|
|
535
|
+
bufferWriter.writeUInt32(this.locktime);
|
|
536
|
+
bufferWriter.writeUInt32(hashType);
|
|
537
|
+
return bcrypto.hash256(tbuffer);
|
|
538
|
+
}
|
|
539
|
+
getHash(forWitness) {
|
|
540
|
+
// wtxid for coinbase is always 32 bytes of 0x00
|
|
541
|
+
if (forWitness && this.isCoinbase()) return new Uint8Array(32);
|
|
542
|
+
return bcrypto.hash256(this.__toBuffer(undefined, undefined, forWitness));
|
|
543
|
+
}
|
|
544
|
+
getId() {
|
|
545
|
+
// transaction hash's are displayed in reverse order
|
|
546
|
+
return tools.toHex(
|
|
547
|
+
(0, bufferutils_js_1.reverseBuffer)(this.getHash(false)),
|
|
548
|
+
);
|
|
549
|
+
}
|
|
550
|
+
toBuffer(buffer, initialOffset) {
|
|
551
|
+
return this.__toBuffer(buffer, initialOffset, true);
|
|
552
|
+
}
|
|
553
|
+
toHex() {
|
|
554
|
+
return tools.toHex(this.toBuffer(undefined, undefined));
|
|
555
|
+
}
|
|
556
|
+
setInputScript(index, scriptSig) {
|
|
557
|
+
v.parse(v.tuple([v.number(), types.BufferSchema]), [index, scriptSig]);
|
|
558
|
+
this.ins[index].script = scriptSig;
|
|
559
|
+
}
|
|
560
|
+
setWitness(index, witness) {
|
|
561
|
+
v.parse(v.tuple([v.number(), v.array(types.BufferSchema)]), [
|
|
562
|
+
index,
|
|
563
|
+
witness,
|
|
564
|
+
]);
|
|
565
|
+
this.ins[index].witness = witness;
|
|
566
|
+
}
|
|
567
|
+
__toBuffer(buffer, initialOffset, _ALLOW_WITNESS = false) {
|
|
568
|
+
if (!buffer) buffer = new Uint8Array(this.byteLength(_ALLOW_WITNESS));
|
|
569
|
+
const bufferWriter = new bufferutils_js_1.BufferWriter(
|
|
570
|
+
buffer,
|
|
571
|
+
initialOffset || 0,
|
|
572
|
+
);
|
|
573
|
+
bufferWriter.writeUInt32(this.version);
|
|
574
|
+
const hasWitnesses = _ALLOW_WITNESS && this.hasWitnesses();
|
|
575
|
+
if (hasWitnesses) {
|
|
576
|
+
bufferWriter.writeUInt8(Transaction.ADVANCED_TRANSACTION_MARKER);
|
|
577
|
+
bufferWriter.writeUInt8(Transaction.ADVANCED_TRANSACTION_FLAG);
|
|
578
|
+
}
|
|
579
|
+
bufferWriter.writeVarInt(this.ins.length);
|
|
580
|
+
this.ins.forEach(txIn => {
|
|
581
|
+
bufferWriter.writeSlice(txIn.hash);
|
|
582
|
+
bufferWriter.writeUInt32(txIn.index);
|
|
583
|
+
bufferWriter.writeVarSlice(txIn.script);
|
|
584
|
+
bufferWriter.writeUInt32(txIn.sequence);
|
|
585
|
+
});
|
|
586
|
+
bufferWriter.writeVarInt(this.outs.length);
|
|
587
|
+
this.outs.forEach(txOut => {
|
|
588
|
+
if (isOutput(txOut)) {
|
|
589
|
+
bufferWriter.writeInt64(txOut.value);
|
|
590
|
+
} else {
|
|
591
|
+
bufferWriter.writeSlice(txOut.valueBuffer);
|
|
592
|
+
}
|
|
593
|
+
bufferWriter.writeVarSlice(txOut.script);
|
|
594
|
+
});
|
|
595
|
+
if (hasWitnesses) {
|
|
596
|
+
this.ins.forEach(input => {
|
|
597
|
+
bufferWriter.writeVector(input.witness);
|
|
598
|
+
});
|
|
599
|
+
}
|
|
600
|
+
bufferWriter.writeUInt32(this.locktime);
|
|
601
|
+
// avoid slicing unless necessary
|
|
602
|
+
if (initialOffset !== undefined)
|
|
603
|
+
return buffer.slice(initialOffset, bufferWriter.offset);
|
|
604
|
+
return buffer;
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
exports.Transaction = Transaction;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
export interface Output {
|
|
2
|
+
script: Uint8Array;
|
|
3
|
+
value: bigint;
|
|
4
|
+
}
|
|
5
|
+
export interface Input {
|
|
6
|
+
hash: Uint8Array;
|
|
7
|
+
index: number;
|
|
8
|
+
script: Uint8Array;
|
|
9
|
+
sequence: number;
|
|
10
|
+
witness: Uint8Array[];
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Represents a Bitcoin transaction.
|
|
14
|
+
*/
|
|
15
|
+
export declare class Transaction {
|
|
16
|
+
static readonly DEFAULT_SEQUENCE = 4294967295;
|
|
17
|
+
static readonly SIGHASH_DEFAULT = 0;
|
|
18
|
+
static readonly SIGHASH_ALL = 1;
|
|
19
|
+
static readonly SIGHASH_NONE = 2;
|
|
20
|
+
static readonly SIGHASH_SINGLE = 3;
|
|
21
|
+
static readonly SIGHASH_ANYONECANPAY = 128;
|
|
22
|
+
static readonly SIGHASH_OUTPUT_MASK = 3;
|
|
23
|
+
static readonly SIGHASH_INPUT_MASK = 128;
|
|
24
|
+
static readonly ADVANCED_TRANSACTION_MARKER = 0;
|
|
25
|
+
static readonly ADVANCED_TRANSACTION_FLAG = 1;
|
|
26
|
+
static fromBuffer(buffer: Uint8Array, _NO_STRICT?: boolean): Transaction;
|
|
27
|
+
static fromHex(hex: string): Transaction;
|
|
28
|
+
static isCoinbaseHash(buffer: Uint8Array): boolean;
|
|
29
|
+
version: number;
|
|
30
|
+
locktime: number;
|
|
31
|
+
ins: Input[];
|
|
32
|
+
outs: Output[];
|
|
33
|
+
isCoinbase(): boolean;
|
|
34
|
+
addInput(hash: Uint8Array, index: number, sequence?: number, scriptSig?: Uint8Array): number;
|
|
35
|
+
addOutput(scriptPubKey: Uint8Array, value: bigint): number;
|
|
36
|
+
hasWitnesses(): boolean;
|
|
37
|
+
stripWitnesses(): void;
|
|
38
|
+
weight(): number;
|
|
39
|
+
virtualSize(): number;
|
|
40
|
+
byteLength(_ALLOW_WITNESS?: boolean): number;
|
|
41
|
+
clone(): Transaction;
|
|
42
|
+
/**
|
|
43
|
+
* Hash transaction for signing a specific input.
|
|
44
|
+
*
|
|
45
|
+
* Bitcoin uses a different hash for each signed transaction input.
|
|
46
|
+
* This method copies the transaction, makes the necessary changes based on the
|
|
47
|
+
* hashType, and then hashes the result.
|
|
48
|
+
* This hash can then be used to sign the provided transaction input.
|
|
49
|
+
*/
|
|
50
|
+
hashForSignature(inIndex: number, prevOutScript: Uint8Array, hashType: number): Uint8Array;
|
|
51
|
+
hashForWitnessV1(inIndex: number, prevOutScripts: Uint8Array[], values: bigint[], hashType: number, leafHash?: Uint8Array, annex?: Uint8Array): Uint8Array;
|
|
52
|
+
hashForWitnessV0(inIndex: number, prevOutScript: Uint8Array, value: bigint, hashType: number): Uint8Array;
|
|
53
|
+
getHash(forWitness?: boolean): Uint8Array;
|
|
54
|
+
getId(): string;
|
|
55
|
+
toBuffer(buffer?: Uint8Array, initialOffset?: number): Uint8Array;
|
|
56
|
+
toHex(): string;
|
|
57
|
+
setInputScript(index: number, scriptSig: Uint8Array): void;
|
|
58
|
+
setWitness(index: number, witness: Uint8Array[]): void;
|
|
59
|
+
private __toBuffer;
|
|
60
|
+
}
|