@btc-vision/bitcoin 6.3.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.
Files changed (69) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +201 -0
  3. package/package.json +95 -0
  4. package/src/address.d.ts +42 -0
  5. package/src/address.js +191 -0
  6. package/src/bip66.d.ts +7 -0
  7. package/src/bip66.js +103 -0
  8. package/src/block.d.ts +30 -0
  9. package/src/block.js +224 -0
  10. package/src/bufferutils.d.ts +54 -0
  11. package/src/bufferutils.js +182 -0
  12. package/src/crypto.d.ts +18 -0
  13. package/src/crypto.js +128 -0
  14. package/src/ecc_lib.d.ts +17 -0
  15. package/src/ecc_lib.js +122 -0
  16. package/src/hooks/AdvancedSignatureManager.d.ts +44 -0
  17. package/src/hooks/AdvancedSignatureManager.js +88 -0
  18. package/src/hooks/HookedSigner.d.ts +4 -0
  19. package/src/hooks/HookedSigner.js +90 -0
  20. package/src/hooks/SignatureManager.d.ts +35 -0
  21. package/src/hooks/SignatureManager.js +72 -0
  22. package/src/index.d.ts +42 -0
  23. package/src/index.js +87 -0
  24. package/src/merkle.d.ts +10 -0
  25. package/src/merkle.js +30 -0
  26. package/src/networks.d.ts +29 -0
  27. package/src/networks.js +71 -0
  28. package/src/ops.d.ts +126 -0
  29. package/src/ops.js +131 -0
  30. package/src/payments/bip341.d.ts +49 -0
  31. package/src/payments/bip341.js +124 -0
  32. package/src/payments/embed.d.ts +9 -0
  33. package/src/payments/embed.js +54 -0
  34. package/src/payments/index.d.ts +48 -0
  35. package/src/payments/index.js +69 -0
  36. package/src/payments/lazy.d.ts +2 -0
  37. package/src/payments/lazy.js +32 -0
  38. package/src/payments/p2ms.d.ts +9 -0
  39. package/src/payments/p2ms.js +158 -0
  40. package/src/payments/p2pk.d.ts +10 -0
  41. package/src/payments/p2pk.js +82 -0
  42. package/src/payments/p2pkh.d.ts +10 -0
  43. package/src/payments/p2pkh.js +143 -0
  44. package/src/payments/p2sh.d.ts +10 -0
  45. package/src/payments/p2sh.js +204 -0
  46. package/src/payments/p2tr.d.ts +10 -0
  47. package/src/payments/p2tr.js +315 -0
  48. package/src/payments/p2wpkh.d.ts +10 -0
  49. package/src/payments/p2wpkh.js +146 -0
  50. package/src/payments/p2wsh.d.ts +10 -0
  51. package/src/payments/p2wsh.js +226 -0
  52. package/src/psbt/bip371.d.ts +42 -0
  53. package/src/psbt/bip371.js +424 -0
  54. package/src/psbt/psbtutils.d.ts +64 -0
  55. package/src/psbt/psbtutils.js +191 -0
  56. package/src/psbt.d.ts +235 -0
  57. package/src/psbt.js +1825 -0
  58. package/src/push_data.d.ts +29 -0
  59. package/src/push_data.js +83 -0
  60. package/src/script.d.ts +42 -0
  61. package/src/script.js +231 -0
  62. package/src/script_number.d.ts +19 -0
  63. package/src/script_number.js +78 -0
  64. package/src/script_signature.d.ts +21 -0
  65. package/src/script_signature.js +79 -0
  66. package/src/transaction.d.ts +60 -0
  67. package/src/transaction.js +571 -0
  68. package/src/types.d.ts +54 -0
  69. package/src/types.js +106 -0
@@ -0,0 +1,29 @@
1
+ /// <reference types="node" />
2
+ /**
3
+ * Calculates the encoding length of a number used for push data in Bitcoin transactions.
4
+ * @param i The number to calculate the encoding length for.
5
+ * @returns The encoding length of the number.
6
+ */
7
+ export declare function encodingLength(i: number): number;
8
+ /**
9
+ * Encodes a number into a buffer using a variable-length encoding scheme.
10
+ * The encoded buffer is written starting at the specified offset.
11
+ * Returns the size of the encoded buffer.
12
+ *
13
+ * @param buffer - The buffer to write the encoded data into.
14
+ * @param num - The number to encode.
15
+ * @param offset - The offset at which to start writing the encoded buffer.
16
+ * @returns The size of the encoded buffer.
17
+ */
18
+ export declare function encode(buffer: Buffer, num: number, offset: number): number;
19
+ /**
20
+ * Decodes a buffer and returns information about the opcode, number, and size.
21
+ * @param buffer - The buffer to decode.
22
+ * @param offset - The offset within the buffer to start decoding.
23
+ * @returns An object containing the opcode, number, and size, or null if decoding fails.
24
+ */
25
+ export declare function decode(buffer: Buffer, offset: number): {
26
+ opcode: number;
27
+ number: number;
28
+ size: number;
29
+ } | null;
@@ -0,0 +1,83 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, '__esModule', { value: true });
3
+ exports.decode = exports.encode = exports.encodingLength = void 0;
4
+ const ops_1 = require('./ops');
5
+ /**
6
+ * Calculates the encoding length of a number used for push data in Bitcoin transactions.
7
+ * @param i The number to calculate the encoding length for.
8
+ * @returns The encoding length of the number.
9
+ */
10
+ function encodingLength(i) {
11
+ return i < ops_1.OPS.OP_PUSHDATA1 ? 1 : i <= 0xff ? 2 : i <= 0xffff ? 3 : 5;
12
+ }
13
+ exports.encodingLength = encodingLength;
14
+ /**
15
+ * Encodes a number into a buffer using a variable-length encoding scheme.
16
+ * The encoded buffer is written starting at the specified offset.
17
+ * Returns the size of the encoded buffer.
18
+ *
19
+ * @param buffer - The buffer to write the encoded data into.
20
+ * @param num - The number to encode.
21
+ * @param offset - The offset at which to start writing the encoded buffer.
22
+ * @returns The size of the encoded buffer.
23
+ */
24
+ function encode(buffer, num, offset) {
25
+ const size = encodingLength(num);
26
+ // ~6 bit
27
+ if (size === 1) {
28
+ buffer.writeUInt8(num, offset);
29
+ // 8 bit
30
+ } else if (size === 2) {
31
+ buffer.writeUInt8(ops_1.OPS.OP_PUSHDATA1, offset);
32
+ buffer.writeUInt8(num, offset + 1);
33
+ // 16 bit
34
+ } else if (size === 3) {
35
+ buffer.writeUInt8(ops_1.OPS.OP_PUSHDATA2, offset);
36
+ buffer.writeUInt16LE(num, offset + 1);
37
+ // 32 bit
38
+ } else {
39
+ buffer.writeUInt8(ops_1.OPS.OP_PUSHDATA4, offset);
40
+ buffer.writeUInt32LE(num, offset + 1);
41
+ }
42
+ return size;
43
+ }
44
+ exports.encode = encode;
45
+ /**
46
+ * Decodes a buffer and returns information about the opcode, number, and size.
47
+ * @param buffer - The buffer to decode.
48
+ * @param offset - The offset within the buffer to start decoding.
49
+ * @returns An object containing the opcode, number, and size, or null if decoding fails.
50
+ */
51
+ function decode(buffer, offset) {
52
+ const opcode = buffer.readUInt8(offset);
53
+ let num;
54
+ let size;
55
+ // ~6 bit
56
+ if (opcode < ops_1.OPS.OP_PUSHDATA1) {
57
+ num = opcode;
58
+ size = 1;
59
+ // 8 bit
60
+ } else if (opcode === ops_1.OPS.OP_PUSHDATA1) {
61
+ if (offset + 2 > buffer.length) return null;
62
+ num = buffer.readUInt8(offset + 1);
63
+ size = 2;
64
+ // 16 bit
65
+ } else if (opcode === ops_1.OPS.OP_PUSHDATA2) {
66
+ if (offset + 3 > buffer.length) return null;
67
+ num = buffer.readUInt16LE(offset + 1);
68
+ size = 3;
69
+ // 32 bit
70
+ } else {
71
+ if (offset + 5 > buffer.length) return null;
72
+ if (opcode !== ops_1.OPS.OP_PUSHDATA4)
73
+ throw new Error('Unexpected opcode');
74
+ num = buffer.readUInt32LE(offset + 1);
75
+ size = 5;
76
+ }
77
+ return {
78
+ opcode,
79
+ number: num,
80
+ size,
81
+ };
82
+ }
83
+ exports.decode = decode;
@@ -0,0 +1,42 @@
1
+ /// <reference types="node" />
2
+ import { OPS } from './ops';
3
+ import { Stack } from './payments';
4
+ import * as scriptNumber from './script_number';
5
+ import * as scriptSignature from './script_signature';
6
+ export { OPS };
7
+ export declare function isPushOnly(value: Stack): boolean;
8
+ export declare function countNonPushOnlyOPs(value: Stack): number;
9
+ /**
10
+ * Compiles an array of chunks into a Buffer.
11
+ *
12
+ * @param chunks - The array of chunks to compile.
13
+ * @returns The compiled Buffer.
14
+ * @throws Error if the compilation fails.
15
+ */
16
+ export declare function compile(chunks: Buffer | Stack): Buffer;
17
+ export declare function decompile(buffer: Buffer | Array<number | Buffer>): Array<number | Buffer> | null;
18
+ /**
19
+ * Converts the given chunks into an ASM (Assembly) string representation.
20
+ * If the chunks parameter is a Buffer, it will be decompiled into a Stack before conversion.
21
+ * @param chunks - The chunks to convert into ASM.
22
+ * @returns The ASM string representation of the chunks.
23
+ */
24
+ export declare function toASM(chunks: Buffer | Array<number | Buffer>): string;
25
+ /**
26
+ * Converts an ASM string to a Buffer.
27
+ * @param asm The ASM string to convert.
28
+ * @returns The converted Buffer.
29
+ */
30
+ export declare function fromASM(asm: string): Buffer;
31
+ /**
32
+ * Converts the given chunks into a stack of buffers.
33
+ *
34
+ * @param chunks - The chunks to convert.
35
+ * @returns The stack of buffers.
36
+ */
37
+ export declare function toStack(chunks: Buffer | Array<number | Buffer>): Buffer[];
38
+ export declare function isCanonicalPubKey(buffer: Buffer): boolean;
39
+ export declare function isDefinedHashType(hashType: number): boolean;
40
+ export declare function isCanonicalScriptSignature(buffer: Buffer): boolean;
41
+ export declare const number: typeof scriptNumber;
42
+ export declare const signature: typeof scriptSignature;
package/src/script.js ADDED
@@ -0,0 +1,231 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, '__esModule', { value: true });
3
+ exports.signature =
4
+ exports.number =
5
+ exports.isCanonicalScriptSignature =
6
+ exports.isDefinedHashType =
7
+ exports.isCanonicalPubKey =
8
+ exports.toStack =
9
+ exports.fromASM =
10
+ exports.toASM =
11
+ exports.decompile =
12
+ exports.compile =
13
+ exports.countNonPushOnlyOPs =
14
+ exports.isPushOnly =
15
+ exports.OPS =
16
+ void 0;
17
+ /**
18
+ * Script tools, including decompile, compile, toASM, fromASM, toStack, isCanonicalPubKey, isCanonicalScriptSignature
19
+ * @packageDocumentation
20
+ */
21
+ const bip66 = require('./bip66');
22
+ const ops_1 = require('./ops');
23
+ Object.defineProperty(exports, 'OPS', {
24
+ enumerable: true,
25
+ get: function () {
26
+ return ops_1.OPS;
27
+ },
28
+ });
29
+ const pushdata = require('./push_data');
30
+ const scriptNumber = require('./script_number');
31
+ const scriptSignature = require('./script_signature');
32
+ const types = require('./types');
33
+ const { typeforce } = types;
34
+ const OP_INT_BASE = ops_1.OPS.OP_RESERVED; // OP_1 - 1
35
+ function isOPInt(value) {
36
+ return (
37
+ types.Number(value) &&
38
+ (value === ops_1.OPS.OP_0 ||
39
+ (value >= ops_1.OPS.OP_1 && value <= ops_1.OPS.OP_16) ||
40
+ value === ops_1.OPS.OP_1NEGATE)
41
+ );
42
+ }
43
+ function isPushOnlyChunk(value) {
44
+ return types.Buffer(value) || isOPInt(value);
45
+ }
46
+ function isPushOnly(value) {
47
+ return types.Array(value) && value.every(isPushOnlyChunk);
48
+ }
49
+ exports.isPushOnly = isPushOnly;
50
+ function countNonPushOnlyOPs(value) {
51
+ return value.length - value.filter(isPushOnlyChunk).length;
52
+ }
53
+ exports.countNonPushOnlyOPs = countNonPushOnlyOPs;
54
+ function asMinimalOP(buffer) {
55
+ if (buffer.length === 0) return ops_1.OPS.OP_0;
56
+ if (buffer.length !== 1) return;
57
+ if (buffer[0] >= 1 && buffer[0] <= 16) return OP_INT_BASE + buffer[0];
58
+ if (buffer[0] === 0x81) return ops_1.OPS.OP_1NEGATE;
59
+ }
60
+ function chunksIsBuffer(buf) {
61
+ return Buffer.isBuffer(buf);
62
+ }
63
+ function chunksIsArray(buf) {
64
+ return types.Array(buf);
65
+ }
66
+ function singleChunkIsBuffer(buf) {
67
+ return Buffer.isBuffer(buf);
68
+ }
69
+ /**
70
+ * Compiles an array of chunks into a Buffer.
71
+ *
72
+ * @param chunks - The array of chunks to compile.
73
+ * @returns The compiled Buffer.
74
+ * @throws Error if the compilation fails.
75
+ */
76
+ function compile(chunks) {
77
+ // TODO: remove me
78
+ if (chunksIsBuffer(chunks)) return chunks;
79
+ typeforce(types.Array, chunks);
80
+ const bufferSize = chunks.reduce((accum, chunk) => {
81
+ // data chunk
82
+ if (singleChunkIsBuffer(chunk)) {
83
+ // adhere to BIP62.3, minimal push policy
84
+ if (chunk.length === 1 && asMinimalOP(chunk) !== undefined) {
85
+ return accum + 1;
86
+ }
87
+ return accum + pushdata.encodingLength(chunk.length) + chunk.length;
88
+ }
89
+ // opcode
90
+ return accum + 1;
91
+ }, 0.0);
92
+ const buffer = Buffer.allocUnsafe(bufferSize);
93
+ let offset = 0;
94
+ chunks.forEach(chunk => {
95
+ // data chunk
96
+ if (singleChunkIsBuffer(chunk)) {
97
+ // adhere to BIP62.3, minimal push policy
98
+ const opcode = asMinimalOP(chunk);
99
+ if (opcode !== undefined) {
100
+ buffer.writeUInt8(opcode, offset);
101
+ offset += 1;
102
+ return;
103
+ }
104
+ offset += pushdata.encode(buffer, chunk.length, offset);
105
+ chunk.copy(buffer, offset);
106
+ offset += chunk.length;
107
+ // opcode
108
+ } else {
109
+ buffer.writeUInt8(chunk, offset);
110
+ offset += 1;
111
+ }
112
+ });
113
+ if (offset !== buffer.length) throw new Error('Could not decode chunks');
114
+ return buffer;
115
+ }
116
+ exports.compile = compile;
117
+ function decompile(buffer) {
118
+ // TODO: remove me
119
+ if (chunksIsArray(buffer)) return buffer;
120
+ typeforce(types.Buffer, buffer);
121
+ const chunks = [];
122
+ let i = 0;
123
+ while (i < buffer.length) {
124
+ const opcode = buffer[i];
125
+ // data chunk
126
+ if (opcode > ops_1.OPS.OP_0 && opcode <= ops_1.OPS.OP_PUSHDATA4) {
127
+ const d = pushdata.decode(buffer, i);
128
+ // did reading a pushDataInt fail?
129
+ if (d === null) return null;
130
+ i += d.size;
131
+ // attempt to read too much data?
132
+ if (i + d.number > buffer.length) return null;
133
+ const data = buffer.slice(i, i + d.number);
134
+ i += d.number;
135
+ // decompile minimally
136
+ const op = asMinimalOP(data);
137
+ if (op !== undefined) {
138
+ chunks.push(op);
139
+ } else {
140
+ chunks.push(data);
141
+ }
142
+ // opcode
143
+ } else {
144
+ chunks.push(opcode);
145
+ i += 1;
146
+ }
147
+ }
148
+ return chunks;
149
+ }
150
+ exports.decompile = decompile;
151
+ /**
152
+ * Converts the given chunks into an ASM (Assembly) string representation.
153
+ * If the chunks parameter is a Buffer, it will be decompiled into a Stack before conversion.
154
+ * @param chunks - The chunks to convert into ASM.
155
+ * @returns The ASM string representation of the chunks.
156
+ */
157
+ function toASM(chunks) {
158
+ if (chunksIsBuffer(chunks)) {
159
+ chunks = decompile(chunks);
160
+ }
161
+ if (!chunks) {
162
+ throw new Error('Could not convert invalid chunks to ASM');
163
+ }
164
+ return chunks
165
+ .map(chunk => {
166
+ // data?
167
+ if (singleChunkIsBuffer(chunk)) {
168
+ const op = asMinimalOP(chunk);
169
+ if (op === undefined) return chunk.toString('hex');
170
+ chunk = op;
171
+ }
172
+ // opcode!
173
+ return ops_1.REVERSE_OPS[chunk];
174
+ })
175
+ .join(' ');
176
+ }
177
+ exports.toASM = toASM;
178
+ /**
179
+ * Converts an ASM string to a Buffer.
180
+ * @param asm The ASM string to convert.
181
+ * @returns The converted Buffer.
182
+ */
183
+ function fromASM(asm) {
184
+ typeforce(types.String, asm);
185
+ return compile(
186
+ asm.split(' ').map(chunkStr => {
187
+ // opcode?
188
+ if (ops_1.OPS[chunkStr] !== undefined) {
189
+ return ops_1.OPS[chunkStr];
190
+ }
191
+ typeforce(types.Hex, chunkStr);
192
+ // data!
193
+ return Buffer.from(chunkStr, 'hex');
194
+ }),
195
+ );
196
+ }
197
+ exports.fromASM = fromASM;
198
+ /**
199
+ * Converts the given chunks into a stack of buffers.
200
+ *
201
+ * @param chunks - The chunks to convert.
202
+ * @returns The stack of buffers.
203
+ */
204
+ function toStack(chunks) {
205
+ chunks = decompile(chunks);
206
+ typeforce(isPushOnly, chunks);
207
+ return chunks.map(op => {
208
+ if (singleChunkIsBuffer(op)) return op;
209
+ if (op === ops_1.OPS.OP_0) return Buffer.allocUnsafe(0);
210
+ return scriptNumber.encode(op - OP_INT_BASE);
211
+ });
212
+ }
213
+ exports.toStack = toStack;
214
+ function isCanonicalPubKey(buffer) {
215
+ return types.isPoint(buffer);
216
+ }
217
+ exports.isCanonicalPubKey = isCanonicalPubKey;
218
+ function isDefinedHashType(hashType) {
219
+ const hashTypeMod = hashType & ~0x80;
220
+ // return hashTypeMod > SIGHASH_ALL && hashTypeMod < SIGHASH_SINGLE
221
+ return hashTypeMod > 0x00 && hashTypeMod < 0x04;
222
+ }
223
+ exports.isDefinedHashType = isDefinedHashType;
224
+ function isCanonicalScriptSignature(buffer) {
225
+ if (!Buffer.isBuffer(buffer)) return false;
226
+ if (!isDefinedHashType(buffer[buffer.length - 1])) return false;
227
+ return bip66.check(buffer.slice(0, -1));
228
+ }
229
+ exports.isCanonicalScriptSignature = isCanonicalScriptSignature;
230
+ exports.number = scriptNumber;
231
+ exports.signature = scriptSignature;
@@ -0,0 +1,19 @@
1
+ /// <reference types="node" />
2
+ /**
3
+ * Decodes a script number from a buffer.
4
+ *
5
+ * @param buffer - The buffer containing the script number.
6
+ * @param maxLength - The maximum length of the script number. Defaults to 4.
7
+ * @param minimal - Whether the script number should be minimal. Defaults to true.
8
+ * @returns The decoded script number.
9
+ * @throws {TypeError} If the script number overflows the maximum length.
10
+ * @throws {Error} If the script number is not minimally encoded when minimal is true.
11
+ */
12
+ export declare function decode(buffer: Buffer, maxLength?: number, minimal?: boolean): number;
13
+ /**
14
+ * Encodes a number into a Buffer using a specific format.
15
+ *
16
+ * @param _number - The number to encode.
17
+ * @returns The encoded number as a Buffer.
18
+ */
19
+ export declare function encode(_number: number): Buffer;
@@ -0,0 +1,78 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, '__esModule', { value: true });
3
+ exports.encode = exports.decode = void 0;
4
+ /**
5
+ * Decodes a script number from a buffer.
6
+ *
7
+ * @param buffer - The buffer containing the script number.
8
+ * @param maxLength - The maximum length of the script number. Defaults to 4.
9
+ * @param minimal - Whether the script number should be minimal. Defaults to true.
10
+ * @returns The decoded script number.
11
+ * @throws {TypeError} If the script number overflows the maximum length.
12
+ * @throws {Error} If the script number is not minimally encoded when minimal is true.
13
+ */
14
+ function decode(buffer, maxLength, minimal) {
15
+ maxLength = maxLength || 4;
16
+ minimal = minimal === undefined ? true : minimal;
17
+ const length = buffer.length;
18
+ if (length === 0) return 0;
19
+ if (length > maxLength) throw new TypeError('Script number overflow');
20
+ if (minimal) {
21
+ if ((buffer[length - 1] & 0x7f) === 0) {
22
+ if (length <= 1 || (buffer[length - 2] & 0x80) === 0)
23
+ throw new Error('Non-minimally encoded script number');
24
+ }
25
+ }
26
+ // 40-bit
27
+ if (length === 5) {
28
+ const a = buffer.readUInt32LE(0);
29
+ const b = buffer.readUInt8(4);
30
+ if (b & 0x80) return -((b & ~0x80) * 0x100000000 + a);
31
+ return b * 0x100000000 + a;
32
+ }
33
+ // 32-bit / 24-bit / 16-bit / 8-bit
34
+ let result = 0;
35
+ for (let i = 0; i < length; ++i) {
36
+ result |= buffer[i] << (8 * i);
37
+ }
38
+ if (buffer[length - 1] & 0x80)
39
+ return -(result & ~(0x80 << (8 * (length - 1))));
40
+ return result;
41
+ }
42
+ exports.decode = decode;
43
+ function scriptNumSize(i) {
44
+ return i > 0x7fffffff
45
+ ? 5
46
+ : i > 0x7fffff
47
+ ? 4
48
+ : i > 0x7fff
49
+ ? 3
50
+ : i > 0x7f
51
+ ? 2
52
+ : i > 0x00
53
+ ? 1
54
+ : 0;
55
+ }
56
+ /**
57
+ * Encodes a number into a Buffer using a specific format.
58
+ *
59
+ * @param _number - The number to encode.
60
+ * @returns The encoded number as a Buffer.
61
+ */
62
+ function encode(_number) {
63
+ let value = Math.abs(_number);
64
+ const size = scriptNumSize(value);
65
+ const buffer = Buffer.allocUnsafe(size);
66
+ const negative = _number < 0;
67
+ for (let i = 0; i < size; ++i) {
68
+ buffer.writeUInt8(value & 0xff, i);
69
+ value >>= 8;
70
+ }
71
+ if (buffer[size - 1] & 0x80) {
72
+ buffer.writeUInt8(negative ? 0x80 : 0x00, size - 1);
73
+ } else if (negative) {
74
+ buffer[size - 1] |= 0x80;
75
+ }
76
+ return buffer;
77
+ }
78
+ exports.encode = encode;
@@ -0,0 +1,21 @@
1
+ /// <reference types="node" />
2
+ interface ScriptSignature {
3
+ signature: Buffer;
4
+ hashType: number;
5
+ }
6
+ /**
7
+ * Decodes a buffer into a ScriptSignature object.
8
+ * @param buffer - The buffer to decode.
9
+ * @returns The decoded ScriptSignature object.
10
+ * @throws Error if the hashType is invalid.
11
+ */
12
+ export declare function decode(buffer: Buffer): ScriptSignature;
13
+ /**
14
+ * Encodes a signature and hash type into a buffer.
15
+ * @param signature - The signature to encode.
16
+ * @param hashType - The hash type to encode.
17
+ * @returns The encoded buffer.
18
+ * @throws Error if the hashType is invalid.
19
+ */
20
+ export declare function encode(signature: Buffer, hashType: number): Buffer;
21
+ export {};
@@ -0,0 +1,79 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, '__esModule', { value: true });
3
+ exports.encode = exports.decode = void 0;
4
+ const bip66 = require('./bip66');
5
+ const script_1 = require('./script');
6
+ const types = require('./types');
7
+ const { typeforce } = types;
8
+ const ZERO = Buffer.alloc(1, 0);
9
+ /**
10
+ * Converts a buffer to a DER-encoded buffer.
11
+ * @param x - The buffer to be converted.
12
+ * @returns The DER-encoded buffer.
13
+ */
14
+ function toDER(x) {
15
+ let i = 0;
16
+ while (x[i] === 0) ++i;
17
+ if (i === x.length) return ZERO;
18
+ x = x.slice(i);
19
+ if (x[0] & 0x80) return Buffer.concat([ZERO, x], 1 + x.length);
20
+ return x;
21
+ }
22
+ /**
23
+ * Converts a DER-encoded signature to a buffer.
24
+ * If the first byte of the input buffer is 0x00, it is skipped.
25
+ * The resulting buffer is 32 bytes long, filled with zeros if necessary.
26
+ * @param x - The DER-encoded signature.
27
+ * @returns The converted buffer.
28
+ */
29
+ function fromDER(x) {
30
+ if (x[0] === 0x00) x = x.slice(1);
31
+ const buffer = Buffer.alloc(32, 0);
32
+ const bstart = Math.max(0, 32 - x.length);
33
+ x.copy(buffer, bstart);
34
+ return buffer;
35
+ }
36
+ // BIP62: 1 byte hashType flag (only 0x01, 0x02, 0x03, 0x81, 0x82 and 0x83 are allowed)
37
+ /**
38
+ * Decodes a buffer into a ScriptSignature object.
39
+ * @param buffer - The buffer to decode.
40
+ * @returns The decoded ScriptSignature object.
41
+ * @throws Error if the hashType is invalid.
42
+ */
43
+ function decode(buffer) {
44
+ const hashType = buffer.readUInt8(buffer.length - 1);
45
+ if (!(0, script_1.isDefinedHashType)(hashType)) {
46
+ throw new Error('Invalid hashType ' + hashType);
47
+ }
48
+ const decoded = bip66.decode(buffer.slice(0, -1));
49
+ const r = fromDER(decoded.r);
50
+ const s = fromDER(decoded.s);
51
+ const signature = Buffer.concat([r, s], 64);
52
+ return { signature, hashType };
53
+ }
54
+ exports.decode = decode;
55
+ /**
56
+ * Encodes a signature and hash type into a buffer.
57
+ * @param signature - The signature to encode.
58
+ * @param hashType - The hash type to encode.
59
+ * @returns The encoded buffer.
60
+ * @throws Error if the hashType is invalid.
61
+ */
62
+ function encode(signature, hashType) {
63
+ typeforce(
64
+ {
65
+ signature: types.BufferN(64),
66
+ hashType: types.UInt8,
67
+ },
68
+ { signature, hashType },
69
+ );
70
+ if (!(0, script_1.isDefinedHashType)(hashType)) {
71
+ throw new Error('Invalid hashType ' + hashType);
72
+ }
73
+ const hashTypeBuffer = Buffer.allocUnsafe(1);
74
+ hashTypeBuffer.writeUInt8(hashType, 0);
75
+ const r = toDER(signature.slice(0, 32));
76
+ const s = toDER(signature.slice(32, 64));
77
+ return Buffer.concat([bip66.encode(r, s), hashTypeBuffer]);
78
+ }
79
+ exports.encode = encode;
@@ -0,0 +1,60 @@
1
+ /// <reference types="node" />
2
+ export interface Output {
3
+ script: Buffer;
4
+ value: number;
5
+ }
6
+ export interface Input {
7
+ hash: Buffer;
8
+ index: number;
9
+ script: Buffer;
10
+ sequence: number;
11
+ witness: Buffer[];
12
+ }
13
+ /**
14
+ * Represents a Bitcoin transaction.
15
+ */
16
+ export declare class Transaction {
17
+ static readonly DEFAULT_SEQUENCE = 4294967295;
18
+ static readonly SIGHASH_DEFAULT = 0;
19
+ static readonly SIGHASH_ALL = 1;
20
+ static readonly SIGHASH_NONE = 2;
21
+ static readonly SIGHASH_SINGLE = 3;
22
+ static readonly SIGHASH_ANYONECANPAY = 128;
23
+ static readonly SIGHASH_OUTPUT_MASK = 3;
24
+ static readonly SIGHASH_INPUT_MASK = 128;
25
+ static readonly ADVANCED_TRANSACTION_MARKER = 0;
26
+ static readonly ADVANCED_TRANSACTION_FLAG = 1;
27
+ version: number;
28
+ locktime: number;
29
+ ins: Input[];
30
+ outs: Output[];
31
+ static fromBuffer(buffer: Buffer, _NO_STRICT?: boolean): Transaction;
32
+ static fromHex(hex: string): Transaction;
33
+ static isCoinbaseHash(buffer: Buffer): boolean;
34
+ isCoinbase(): boolean;
35
+ addInput(hash: Buffer, index: number, sequence?: number, scriptSig?: Buffer): number;
36
+ addOutput(scriptPubKey: Buffer, value: number): number;
37
+ hasWitnesses(): boolean;
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: Buffer, hashType: number): Buffer;
51
+ hashForWitnessV1(inIndex: number, prevOutScripts: Buffer[], values: number[], hashType: number, leafHash?: Buffer, annex?: Buffer): Buffer;
52
+ hashForWitnessV0(inIndex: number, prevOutScript: Buffer, value: number, hashType: number): Buffer;
53
+ getHash(forWitness?: boolean): Buffer;
54
+ getId(): string;
55
+ toBuffer(buffer?: Buffer, initialOffset?: number): Buffer;
56
+ toHex(): string;
57
+ setInputScript(index: number, scriptSig: Buffer): void;
58
+ setWitness(index: number, witness: Buffer[]): void;
59
+ private __toBuffer;
60
+ }