@alexbosworth/blockchain 4.5.2 → 4.6.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.
- package/CHANGELOG.md +5 -1
- package/README.md +27 -0
- package/index.js +2 -0
- package/numbers/encode_push_bytes_count.js +65 -0
- package/numbers/index.js +10 -1
- package/numbers/number_as_little_endian.js +65 -0
- package/package.json +1 -1
- package/script/script_as_script_elements.js +1 -1
- package/script/script_elements_as_output.js +16 -3
- package/test/numbers/test_encode_push_bytes_count.js +82 -0
- package/test/numbers/test_number_as_little_endian.js +77 -0
- package/test/numbers/test_parse_push_bytes_count.js +67 -0
- package/test/script/test_script_elements_as_script.js +40 -0
- package/test/transactions/test_v0_hash_to_sign.js +262 -0
- package/transactions/index.js +2 -0
- package/transactions/v0_hash_to_sign.js +149 -0
- package/test/script/test_parse_push_bytes_count.js +0 -15
- /package/{script → numbers}/parse_push_bytes_count.js +0 -0
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -30,6 +30,7 @@ Utility methods for working with Blockchain data
|
|
|
30
30
|
- [sizeOfTransaction](#sizeoftransaction)
|
|
31
31
|
- [transactionFromComponents](#transactionfromcomponents)
|
|
32
32
|
- [unsignedTxFromPsbt](#unsignedtxfrompsbt)
|
|
33
|
+
- [v0HashToSign](#v0hashtosign)
|
|
33
34
|
|
|
34
35
|
### compactIntAsNumber
|
|
35
36
|
|
|
@@ -553,3 +554,29 @@ Get the unsigned transaction out of a PSBT
|
|
|
553
554
|
{
|
|
554
555
|
transaction: <Unsigned Transaction Buffer Object>
|
|
555
556
|
}
|
|
557
|
+
|
|
558
|
+
### v0HashToSign
|
|
559
|
+
|
|
560
|
+
Calculate the v0 witness transaction hash to sign
|
|
561
|
+
|
|
562
|
+
The script is the BIP 143 script code of the output being spent
|
|
563
|
+
|
|
564
|
+
For P2WPKH the script code is the P2PKH script of the public key hash
|
|
565
|
+
|
|
566
|
+
For P2WSH the script code is the witness script
|
|
567
|
+
|
|
568
|
+
{
|
|
569
|
+
script: <Signing Input Script Code Hex String>
|
|
570
|
+
[sighash]: <Signature Hash Type Number>
|
|
571
|
+
tokens: <Spending Output Tokens Number>
|
|
572
|
+
transaction: <Raw Transaction Hex String>
|
|
573
|
+
vin: <Signing Transaction Input Index Number>
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
@throws
|
|
577
|
+
<Error>
|
|
578
|
+
|
|
579
|
+
@returns
|
|
580
|
+
{
|
|
581
|
+
hash: <Hash to Sign Hex String>
|
|
582
|
+
}
|
package/index.js
CHANGED
|
@@ -24,6 +24,7 @@ const {scriptElementsAsScript} = require('./script');
|
|
|
24
24
|
const {sizeOfTransaction} = require('./transactions');
|
|
25
25
|
const {transactionFromComponents} = require('./transactions');
|
|
26
26
|
const {unsignedTxFromPsbt} = require('./transactions');
|
|
27
|
+
const {v0HashToSign} = require('./transactions');
|
|
27
28
|
|
|
28
29
|
module.exports = {
|
|
29
30
|
compactIntAsNumber,
|
|
@@ -52,4 +53,5 @@ module.exports = {
|
|
|
52
53
|
sizeOfTransaction,
|
|
53
54
|
transactionFromComponents,
|
|
54
55
|
unsignedTxFromPsbt,
|
|
56
|
+
v0HashToSign,
|
|
55
57
|
};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
const {alloc} = Buffer;
|
|
2
|
+
const byteCountUInt16 = 2;
|
|
3
|
+
const byteCountUInt32 = 4;
|
|
4
|
+
const {concat} = Buffer;
|
|
5
|
+
const {from} = Buffer;
|
|
6
|
+
const {isInteger} = Number;
|
|
7
|
+
const maxDirectPushByteLength = 75;
|
|
8
|
+
const maxUInt8 = 255;
|
|
9
|
+
const maxUInt16 = 65535;
|
|
10
|
+
const maxUInt32 = 4294967295;
|
|
11
|
+
const minimumCount = 0;
|
|
12
|
+
const OP_PUSHDATA1 = 76;
|
|
13
|
+
const OP_PUSHDATA2 = 77;
|
|
14
|
+
const OP_PUSHDATA4 = 78;
|
|
15
|
+
|
|
16
|
+
/** Encode the byte count of a script data push
|
|
17
|
+
|
|
18
|
+
A count up to 75 is a direct single byte, larger counts use OP_PUSHDATA
|
|
19
|
+
|
|
20
|
+
{
|
|
21
|
+
count: <Pushed Data Byte Count Number>
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
@throws
|
|
25
|
+
<Error>
|
|
26
|
+
|
|
27
|
+
@returns
|
|
28
|
+
{
|
|
29
|
+
encoded: <Push Byte Count Encoding Buffer Object>
|
|
30
|
+
}
|
|
31
|
+
*/
|
|
32
|
+
module.exports = ({count}) => {
|
|
33
|
+
if (!isInteger(count) || count < minimumCount) {
|
|
34
|
+
throw new Error('ExpectedPushBytesCountNumberToEncode');
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (count > maxUInt32) {
|
|
38
|
+
throw new Error('UnexpectedlyLargePushBytesCountToEncode');
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Exit early when the byte count fits in a direct single byte push
|
|
42
|
+
if (count <= maxDirectPushByteLength) {
|
|
43
|
+
return {encoded: from([count])};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Exit early when the byte count fits in an OP_PUSHDATA1 push
|
|
47
|
+
if (count <= maxUInt8) {
|
|
48
|
+
return {encoded: from([OP_PUSHDATA1, count])};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Exit early when the byte count fits in an OP_PUSHDATA2 push
|
|
52
|
+
if (count <= maxUInt16) {
|
|
53
|
+
const encodedCount = alloc(byteCountUInt16);
|
|
54
|
+
|
|
55
|
+
encodedCount.writeUInt16LE(count);
|
|
56
|
+
|
|
57
|
+
return {encoded: concat([from([OP_PUSHDATA2]), encodedCount])};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const encodedCount = alloc(byteCountUInt32);
|
|
61
|
+
|
|
62
|
+
encodedCount.writeUInt32LE(count);
|
|
63
|
+
|
|
64
|
+
return {encoded: concat([from([OP_PUSHDATA4]), encodedCount])};
|
|
65
|
+
};
|
package/numbers/index.js
CHANGED
|
@@ -1,4 +1,13 @@
|
|
|
1
1
|
const compactIntAsNumber = require('./compact_int_as_number');
|
|
2
|
+
const encodePushBytesCount = require('./encode_push_bytes_count');
|
|
2
3
|
const numberAsCompactInt = require('./number_as_compact_int');
|
|
4
|
+
const numberAsLittleEndian = require('./number_as_little_endian');
|
|
5
|
+
const parsePushBytesCount = require('./parse_push_bytes_count');
|
|
3
6
|
|
|
4
|
-
module.exports = {
|
|
7
|
+
module.exports = {
|
|
8
|
+
compactIntAsNumber,
|
|
9
|
+
encodePushBytesCount,
|
|
10
|
+
numberAsCompactInt,
|
|
11
|
+
numberAsLittleEndian,
|
|
12
|
+
parsePushBytesCount,
|
|
13
|
+
};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
const {alloc} = Buffer;
|
|
2
|
+
const asBigInt = number => BigInt(number);
|
|
3
|
+
const bitsPerByte = 8;
|
|
4
|
+
const byteCountBigInt = 8;
|
|
5
|
+
const byteCounts = [1, 2, 3, 4, 5, 6, 8];
|
|
6
|
+
const {isInteger} = Number;
|
|
7
|
+
const maxSigned = bytes => 2 ** (bytes * bitsPerByte - 1) - 1;
|
|
8
|
+
const maxUnsigned = bytes => 2 ** (bytes * bitsPerByte) - 1;
|
|
9
|
+
const minSigned = bytes => -(2 ** (bytes * bitsPerByte - 1));
|
|
10
|
+
const minUnsigned = 0;
|
|
11
|
+
const writeOffset = 0;
|
|
12
|
+
|
|
13
|
+
/** Encode a number as little endian bytes
|
|
14
|
+
|
|
15
|
+
A signed negative number is encoded as its twos complement representation
|
|
16
|
+
|
|
17
|
+
{
|
|
18
|
+
bytes: <Encoded Bytes Count Number>
|
|
19
|
+
[is_signed]: <Number Is A Signed Number Bool>
|
|
20
|
+
number: <Number to Encode Number>
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
@throws
|
|
24
|
+
<Error>
|
|
25
|
+
|
|
26
|
+
@returns
|
|
27
|
+
{
|
|
28
|
+
encoded: <Little Endian Encoded Number Buffer Object>
|
|
29
|
+
}
|
|
30
|
+
*/
|
|
31
|
+
module.exports = args => {
|
|
32
|
+
if (!byteCounts.includes(args.bytes)) {
|
|
33
|
+
throw new Error('ExpectedBytesCountToEncodeNumberAsLittleEndian');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (!isInteger(args.number)) {
|
|
37
|
+
throw new Error('ExpectedNumberToEncodeAsLittleEndianBytes');
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const isSigned = !!args.is_signed;
|
|
41
|
+
|
|
42
|
+
const max = isSigned ? maxSigned(args.bytes) : maxUnsigned(args.bytes);
|
|
43
|
+
const min = isSigned ? minSigned(args.bytes) : minUnsigned;
|
|
44
|
+
|
|
45
|
+
if (args.number < min || args.number > max) {
|
|
46
|
+
throw new Error('ExpectedNumberInByteRangeToEncodeAsLittleEndian');
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const encoded = alloc(args.bytes);
|
|
50
|
+
|
|
51
|
+
if (args.bytes === byteCountBigInt && isSigned) {
|
|
52
|
+
// Eight byte signed numbers are written as signed big int numbers
|
|
53
|
+
encoded.writeBigInt64LE(asBigInt(args.number));
|
|
54
|
+
} else if (args.bytes === byteCountBigInt) {
|
|
55
|
+
// Eight byte numbers are written as unsigned big int numbers
|
|
56
|
+
encoded.writeBigUInt64LE(asBigInt(args.number));
|
|
57
|
+
} else if (isSigned) {
|
|
58
|
+
// Signed numbers are written as twos complement integers
|
|
59
|
+
encoded.writeIntLE(args.number, writeOffset, args.bytes);
|
|
60
|
+
} else {
|
|
61
|
+
encoded.writeUIntLE(args.number, writeOffset, args.bytes);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return {encoded};
|
|
65
|
+
};
|
package/package.json
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
|
-
const {
|
|
1
|
+
const {encodePushBytesCount} = require('./../numbers');
|
|
2
2
|
|
|
3
|
+
const {alloc} = Buffer;
|
|
3
4
|
const {concat} = Buffer;
|
|
4
|
-
const encode =
|
|
5
|
-
const flatten = arr => arr.reduce((
|
|
5
|
+
const encode = count => encodePushBytesCount({count}).encoded;
|
|
6
|
+
const flatten = arr => arr.reduce((s, n) => concat([s, n]), Buffer.alloc(0));
|
|
6
7
|
const {from} = Buffer;
|
|
7
8
|
const {isArray} = Array;
|
|
8
9
|
const {isBuffer} = Buffer;
|
|
10
|
+
const {isInteger} = Number;
|
|
11
|
+
const isOpCode = code => code >= 0 && code <= 255;
|
|
9
12
|
|
|
10
13
|
/** Map array of script buffer elements to an output script
|
|
11
14
|
|
|
@@ -26,6 +29,16 @@ module.exports = ({elements}) => {
|
|
|
26
29
|
throw new Error('ExpectedArrayOfScriptElementsToEncodeScript');
|
|
27
30
|
}
|
|
28
31
|
|
|
32
|
+
// Elements are either data pushes or op code numbers
|
|
33
|
+
if (elements.some(n => !isBuffer(n) && !isInteger(n))) {
|
|
34
|
+
throw new Error('ExpectedDataOrOpCodeScriptElementsToEncodeScript');
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Op code elements must be single byte values
|
|
38
|
+
if (elements.some(n => isInteger(n) && !isOpCode(n))) {
|
|
39
|
+
throw new Error('ExpectedSingleByteOpCodeElementToFormOutputScript');
|
|
40
|
+
}
|
|
41
|
+
|
|
29
42
|
// Convert numbers to buffers and hex data to pushdata
|
|
30
43
|
const fullScript = elements.map(element => {
|
|
31
44
|
// Exit early when element is a data push
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
const {deepStrictEqual} = require('node:assert').strict;
|
|
2
|
+
const {throws} = require('node:assert').strict;
|
|
3
|
+
const test = require('node:test');
|
|
4
|
+
|
|
5
|
+
const {encodePushBytesCount} = require('./../../numbers');
|
|
6
|
+
|
|
7
|
+
const tests = [
|
|
8
|
+
{
|
|
9
|
+
args: {},
|
|
10
|
+
description: 'A count number is required',
|
|
11
|
+
error: 'ExpectedPushBytesCountNumberToEncode',
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
args: {count: -1},
|
|
15
|
+
description: 'A non-negative count number is required',
|
|
16
|
+
error: 'ExpectedPushBytesCountNumberToEncode',
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
args: {count: 1.5},
|
|
20
|
+
description: 'An integer count number is required',
|
|
21
|
+
error: 'ExpectedPushBytesCountNumberToEncode',
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
args: {count: 4294967296},
|
|
25
|
+
description: 'A count beyond OP_PUSHDATA4 size is rejected',
|
|
26
|
+
error: 'UnexpectedlyLargePushBytesCountToEncode',
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
args: {count: 0},
|
|
30
|
+
description: 'A zero count is encoded as a direct byte',
|
|
31
|
+
expected: {encoded: Buffer.from('00', 'hex')},
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
args: {count: 75},
|
|
35
|
+
description: 'The largest direct push count is encoded as a direct byte',
|
|
36
|
+
expected: {encoded: Buffer.from('4b', 'hex')},
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
args: {count: 76},
|
|
40
|
+
description: 'The smallest OP_PUSHDATA1 count is encoded',
|
|
41
|
+
expected: {encoded: Buffer.from('4c4c', 'hex')},
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
args: {count: 255},
|
|
45
|
+
description: 'The largest OP_PUSHDATA1 count is encoded',
|
|
46
|
+
expected: {encoded: Buffer.from('4cff', 'hex')},
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
args: {count: 256},
|
|
50
|
+
description: 'The smallest OP_PUSHDATA2 count is encoded',
|
|
51
|
+
expected: {encoded: Buffer.from('4d0001', 'hex')},
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
args: {count: 65535},
|
|
55
|
+
description: 'The largest OP_PUSHDATA2 count is encoded',
|
|
56
|
+
expected: {encoded: Buffer.from('4dffff', 'hex')},
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
args: {count: 65536},
|
|
60
|
+
description: 'The smallest OP_PUSHDATA4 count is encoded',
|
|
61
|
+
expected: {encoded: Buffer.from('4e00000100', 'hex')},
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
args: {count: 4294967295},
|
|
65
|
+
description: 'The largest OP_PUSHDATA4 count is encoded',
|
|
66
|
+
expected: {encoded: Buffer.from('4effffffff', 'hex')},
|
|
67
|
+
},
|
|
68
|
+
];
|
|
69
|
+
|
|
70
|
+
tests.forEach(({args, description, error, expected}) => {
|
|
71
|
+
return test(description, (t, end) => {
|
|
72
|
+
if (!!error) {
|
|
73
|
+
throws(() => encodePushBytesCount(args), new Error(error), 'Got err');
|
|
74
|
+
} else {
|
|
75
|
+
const res = encodePushBytesCount(args);
|
|
76
|
+
|
|
77
|
+
deepStrictEqual(res, expected, 'Got expected result');
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return end();
|
|
81
|
+
});
|
|
82
|
+
});
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
const {deepStrictEqual} = require('node:assert').strict;
|
|
2
|
+
const {throws} = require('node:assert').strict;
|
|
3
|
+
const test = require('node:test');
|
|
4
|
+
|
|
5
|
+
const {numberAsLittleEndian} = require('./../../numbers');
|
|
6
|
+
|
|
7
|
+
const tests = [
|
|
8
|
+
{
|
|
9
|
+
args: {bytes: 7, number: 1},
|
|
10
|
+
description: 'A supported bytes count is required',
|
|
11
|
+
error: 'ExpectedBytesCountToEncodeNumberAsLittleEndian',
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
args: {bytes: 4, number: '1'},
|
|
15
|
+
description: 'A number to encode is required',
|
|
16
|
+
error: 'ExpectedNumberToEncodeAsLittleEndianBytes',
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
args: {bytes: 1, number: 256},
|
|
20
|
+
description: 'A number within the byte range is required',
|
|
21
|
+
error: 'ExpectedNumberInByteRangeToEncodeAsLittleEndian',
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
args: {bytes: 4, number: -1},
|
|
25
|
+
description: 'A negative number requires the signed flag',
|
|
26
|
+
error: 'ExpectedNumberInByteRangeToEncodeAsLittleEndian',
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
args: {bytes: 4, is_signed: true, number: -2147483649},
|
|
30
|
+
description: 'A negative number within the byte range is required',
|
|
31
|
+
error: 'ExpectedNumberInByteRangeToEncodeAsLittleEndian',
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
args: {bytes: 4, is_signed: true, number: 2147483648},
|
|
35
|
+
description: 'A signed number within the signed range is required',
|
|
36
|
+
error: 'ExpectedNumberInByteRangeToEncodeAsLittleEndian',
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
args: {bytes: 1, number: 255},
|
|
40
|
+
description: 'A single byte number is encoded',
|
|
41
|
+
expected: {encoded: Buffer.from('ff', 'hex')},
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
args: {bytes: 4, number: 16843009},
|
|
45
|
+
description: 'A four byte number is encoded as little endian',
|
|
46
|
+
expected: {encoded: Buffer.from('01010101', 'hex')},
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
args: {bytes: 4, is_signed: true, number: -1},
|
|
50
|
+
description: 'A negative number is encoded as twos complement',
|
|
51
|
+
expected: {encoded: Buffer.from('ffffffff', 'hex')},
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
args: {bytes: 8, number: 987654321},
|
|
55
|
+
description: 'An eight byte number is encoded as little endian',
|
|
56
|
+
expected: {encoded: Buffer.from('b168de3a00000000', 'hex')},
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
args: {bytes: 8, is_signed: true, number: -1},
|
|
60
|
+
description: 'A negative eight byte number is encoded as twos complement',
|
|
61
|
+
expected: {encoded: Buffer.from('ffffffffffffffff', 'hex')},
|
|
62
|
+
},
|
|
63
|
+
];
|
|
64
|
+
|
|
65
|
+
tests.forEach(({args, description, error, expected}) => {
|
|
66
|
+
return test(description, (t, end) => {
|
|
67
|
+
if (!!error) {
|
|
68
|
+
throws(() => numberAsLittleEndian(args), new Error(error), 'Got error');
|
|
69
|
+
} else {
|
|
70
|
+
const res = numberAsLittleEndian(args);
|
|
71
|
+
|
|
72
|
+
deepStrictEqual(res, expected, 'Got expected result');
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return end();
|
|
76
|
+
});
|
|
77
|
+
});
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
const {deepStrictEqual} = require('node:assert').strict;
|
|
2
|
+
const test = require('node:test');
|
|
3
|
+
|
|
4
|
+
const {parsePushBytesCount} = require('./../../numbers');
|
|
5
|
+
|
|
6
|
+
const hexAsBuffer = hex => Buffer.from(hex, 'hex');
|
|
7
|
+
|
|
8
|
+
const tests = [
|
|
9
|
+
{
|
|
10
|
+
args: {offset: 0, script: hexAsBuffer('4f00000000')},
|
|
11
|
+
description: 'An unsupported op code is not parsed as a push',
|
|
12
|
+
expected: undefined,
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
args: {offset: 0, script: hexAsBuffer('00')},
|
|
16
|
+
description: 'A zero byte count is parsed as a direct push',
|
|
17
|
+
expected: {bytes: hexAsBuffer('00'), count: 0},
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
args: {offset: 0, script: hexAsBuffer('4b' + 'ff'.repeat(75))},
|
|
21
|
+
description: 'The largest direct push byte count is parsed',
|
|
22
|
+
expected: {bytes: hexAsBuffer('4b'), count: 75},
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
args: {offset: 0, script: hexAsBuffer('4c4c' + 'ff'.repeat(76))},
|
|
26
|
+
description: 'An OP_PUSHDATA1 byte count is parsed',
|
|
27
|
+
expected: {bytes: hexAsBuffer('4c4c'), count: 76},
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
args: {offset: 0, script: hexAsBuffer('4c')},
|
|
31
|
+
description: 'An OP_PUSHDATA1 without a length byte is not parsed',
|
|
32
|
+
expected: {},
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
args: {offset: 0, script: hexAsBuffer('4d0001' + 'ff'.repeat(256))},
|
|
36
|
+
description: 'An OP_PUSHDATA2 byte count is parsed',
|
|
37
|
+
expected: {bytes: hexAsBuffer('4d0001'), count: 256},
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
args: {offset: 0, script: hexAsBuffer('4dff')},
|
|
41
|
+
description: 'An OP_PUSHDATA2 with a short length is not parsed',
|
|
42
|
+
expected: {},
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
args: {offset: 0, script: hexAsBuffer('4e70110100')},
|
|
46
|
+
description: 'An OP_PUSHDATA4 byte count is parsed',
|
|
47
|
+
expected: {bytes: hexAsBuffer('4e70110100'), count: 70000},
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
args: {offset: 0, script: hexAsBuffer('4e112233')},
|
|
51
|
+
description: 'An OP_PUSHDATA4 with a short length is not parsed',
|
|
52
|
+
expected: {},
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
args: {offset: 2, script: hexAsBuffer('ffff4c4c')},
|
|
56
|
+
description: 'A push byte count is parsed at an offset',
|
|
57
|
+
expected: {bytes: hexAsBuffer('4c4c'), count: 76},
|
|
58
|
+
},
|
|
59
|
+
];
|
|
60
|
+
|
|
61
|
+
tests.forEach(({args, description, expected}) => {
|
|
62
|
+
return test(description, (t, end) => {
|
|
63
|
+
deepStrictEqual(parsePushBytesCount(args), expected, 'Got expected');
|
|
64
|
+
|
|
65
|
+
return end();
|
|
66
|
+
});
|
|
67
|
+
});
|
|
@@ -10,6 +10,46 @@ const tests = [
|
|
|
10
10
|
description: 'An array of elements is required',
|
|
11
11
|
error: 'ExpectedArrayOfScriptElementsToEncodeScript',
|
|
12
12
|
},
|
|
13
|
+
{
|
|
14
|
+
args: {elements: []},
|
|
15
|
+
description: 'No elements are mapped to an empty script',
|
|
16
|
+
expected: {script: ''},
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
args: {elements: [Buffer.alloc(76)]},
|
|
20
|
+
description: 'A data element beyond direct push uses OP_PUSHDATA1',
|
|
21
|
+
expected: {script: '4c4c' + '00'.repeat(76)},
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
args: {elements: [Buffer.alloc(256)]},
|
|
25
|
+
description: 'A data element beyond one byte length uses OP_PUSHDATA2',
|
|
26
|
+
expected: {script: '4d0001' + '00'.repeat(256)},
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
args: {elements: [Buffer.alloc(70000)]},
|
|
30
|
+
description: 'A data element beyond two byte length uses OP_PUSHDATA4',
|
|
31
|
+
expected: {script: '4e70110100' + '00'.repeat(70000)},
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
args: {elements: [300, Buffer.alloc(20)]},
|
|
35
|
+
description: 'Op code elements above a single byte value are rejected',
|
|
36
|
+
error: 'ExpectedSingleByteOpCodeElementToFormOutputScript',
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
args: {elements: [-1, Buffer.alloc(20)]},
|
|
40
|
+
description: 'Negative op code elements are rejected',
|
|
41
|
+
error: 'ExpectedSingleByteOpCodeElementToFormOutputScript',
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
args: {elements: ['76', Buffer.alloc(20)]},
|
|
45
|
+
description: 'Non number op code elements are rejected',
|
|
46
|
+
error: 'ExpectedDataOrOpCodeScriptElementsToEncodeScript',
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
args: {elements: [0, Buffer.alloc(75)]},
|
|
50
|
+
description: 'A maximum length direct data push is encoded',
|
|
51
|
+
expected: {script: '004b' + '00'.repeat(75)},
|
|
52
|
+
},
|
|
13
53
|
{
|
|
14
54
|
args: {elements: [118, 169, Buffer.alloc(20), 136, 172]},
|
|
15
55
|
description: 'Elements are mapped to an output script',
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
const {deepStrictEqual} = require('node:assert').strict;
|
|
2
|
+
const {throws} = require('node:assert').strict;
|
|
3
|
+
const test = require('node:test');
|
|
4
|
+
|
|
5
|
+
const {v0HashToSign} = require('./../../');
|
|
6
|
+
|
|
7
|
+
const nativeP2shP2wshTransaction = '010000000136641869ca081e70f394c6948e8af409e18b619df2ed74aa106c1ca29787b96e0100000000ffffffff0200e9a435000000001976a914389ffce9cd9ae88dcc0631e88a821ffdbe9bfe2688acc0832f05000000001976a9147480a33f950689af511e6e84c138dbbd3c3ee41588ac00000000';
|
|
8
|
+
const p2shP2wshWitnessScript = '56210307b8ae49ac90a048e9b53357a2354b3334e9c8bee813ecb98e99a7e07e8c3ba32103b28f0c28bfab54554ae8c658ac5c3e0ce6e79ad336331f78c428dd43eea8449b21034b8113d703413d57761b8b9781957b8c0ac1dfe69f492580ca4195f50376ba4a21033400f6afecb833092a9a21cfdf1ed1376e58c5d1f47de74683123987e967a8f42103a6d48b1131e94ba04d9737d61acdaa1322008af9602b3b14862c07a1789aac162102d8b661b0b3302ee2f162b09e07a55ad5dfbe673a9f01d9f0c19617681024306b56ae';
|
|
9
|
+
|
|
10
|
+
const singleAnyoneCanPayTransaction = '0100000002e9b542c5176808107ff1df906f46bb1f2583b16112b95ee5380665ba7fcfc0010000000000ffffffff80e68831516392fcd100d186b3c2c7b95c80b53c77e77c35ba03a66b429a2a1b0000000000ffffffff0280969800000000001976a914de4b231626ef508c9a74a8517e6783c0546d6b2888ac80969800000000001976a9146648a8cd4531e1ec47f35916de8e259237294d1e88ac00000000';
|
|
11
|
+
|
|
12
|
+
const tests = [
|
|
13
|
+
{
|
|
14
|
+
args: {script: undefined},
|
|
15
|
+
description: 'A script code hex string is required',
|
|
16
|
+
error: 'ExpectedScriptCodeHexStringToCalculateV0HashToSign',
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
args: {script: '00', sighash: 0x04},
|
|
20
|
+
description: 'A known signature hash type is required',
|
|
21
|
+
error: 'ExpectedValidSignatureHashTypeToCalculateV0HashToSign',
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
args: {script: '00', sighash: 0x01},
|
|
25
|
+
description: 'A spend tokens number is required',
|
|
26
|
+
error: 'ExpectedSpendTokensNumberToCalculateV0HashToSign',
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
args: {script: '00', sighash: 0x01, tokens: 1},
|
|
30
|
+
description: 'A transaction hex string is required',
|
|
31
|
+
error: 'ExpectedTransactionHexStringToCalculateV0HashToSign',
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
args: {
|
|
35
|
+
script: '00',
|
|
36
|
+
sighash: 0x01,
|
|
37
|
+
tokens: 1,
|
|
38
|
+
transaction: nativeP2shP2wshTransaction,
|
|
39
|
+
},
|
|
40
|
+
description: 'An input index number is required',
|
|
41
|
+
error: 'ExpectedInputIndexNumberToCalculateV0HashToSign',
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
args: {
|
|
45
|
+
script: '00',
|
|
46
|
+
sighash: 0x01,
|
|
47
|
+
tokens: 1,
|
|
48
|
+
transaction: nativeP2shP2wshTransaction,
|
|
49
|
+
vin: 1,
|
|
50
|
+
},
|
|
51
|
+
description: 'A matching input is required',
|
|
52
|
+
error: 'ExpectedMatchingInputToCalculateV0HashToSign',
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
args: {
|
|
56
|
+
script: '76a9141d0f172a0ecb48aee1be1f2687d2963ae33f71a188ac',
|
|
57
|
+
sighash: 0x01,
|
|
58
|
+
tokens: 600000000,
|
|
59
|
+
transaction: '0100000002fff7f7881a8099afa6940d42d1e7f6362bec38171ea3edf433541db4e4ad969f0000000000eeffffffef51e1b804cc89d182d279655c3aa89e815b1b309fe287d9b2b55d57b90ec68a0100000000ffffffff02202cb206000000001976a9148280b37df378db99f66f85c95a783a76ac7a6d5988ac9093510d000000001976a9143bde42dbee7e4dbe6a21b2d50ce2f0167faa815988ac11000000',
|
|
60
|
+
vin: 1,
|
|
61
|
+
},
|
|
62
|
+
description: 'A native P2WPKH input hash to sign is calculated',
|
|
63
|
+
expected: {
|
|
64
|
+
hash: 'c37af31116d1b27caf68aae9e3ac82f1477929014d5b917657d0eb49478cb670',
|
|
65
|
+
},
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
args: {
|
|
69
|
+
script: '76a91479091972186c449eb1ded22b78e40d009bdf008988ac',
|
|
70
|
+
tokens: 1000000000,
|
|
71
|
+
transaction: '0100000001db6b1b20aa0fd7b23880be2ecbd4a98130974cf4748fb66092ac4d3ceb1a54770100000000feffffff02b8b4eb0b000000001976a914a457b684d7f0d539a46a45bbc043f35b59d0d96388ac0008af2f000000001976a914fd270b1ee6abcaea97fea7ad0402e8bd8ad6d77c88ac92040000',
|
|
72
|
+
vin: 0,
|
|
73
|
+
},
|
|
74
|
+
description: 'A P2SH-P2WPKH input hash to sign uses the default sighash',
|
|
75
|
+
expected: {
|
|
76
|
+
hash: '64f3b0f4dd2bb3aa1ce8566d220cc74dda9df97d8490cc81d89d735c92e59fb6',
|
|
77
|
+
},
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
args: {
|
|
81
|
+
script: '21026dccc749adc2a9d0d89497ac511f760f45c47dc5ed9cf352a58ac706453880aeadab210255a9626aebf5e29c0e6538428ba0d1dcf6ca98ffdf086aa8ced5e0d0215ea465ac',
|
|
82
|
+
sighash: 0x03,
|
|
83
|
+
tokens: 4900000000,
|
|
84
|
+
transaction: '0100000002fe3dc9208094f3ffd12645477b3dc56f60ec4fa8e6f5d67c565d1c6b9216b36e0000000000ffffffff0815cf020f013ed6cf91d29f4202e8a58726b1ac6c79da47c23d1bee0a6925f80000000000ffffffff0100f2052a010000001976a914a30741f8145e5acadf23f751864167f32e0963f788ac00000000',
|
|
85
|
+
vin: 1,
|
|
86
|
+
},
|
|
87
|
+
description: 'A single hash with no matching output commits to a zero hash',
|
|
88
|
+
expected: {
|
|
89
|
+
hash: '82dde6e4f1e94d02c2b7ad03d2115d691f48d064e9d52f58194a6637e4194391',
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
{
|
|
93
|
+
args: {
|
|
94
|
+
script: '76a91479091972186c449eb1ded22b78e40d009bdf008988ac',
|
|
95
|
+
sighash: 0x01,
|
|
96
|
+
tokens: 1000000000,
|
|
97
|
+
transaction: 'ffffffff01db6b1b20aa0fd7b23880be2ecbd4a98130974cf4748fb66092ac4d3ceb1a54770100000000feffffff02b8b4eb0b000000001976a914a457b684d7f0d539a46a45bbc043f35b59d0d96388ac0008af2f000000001976a914fd270b1ee6abcaea97fea7ad0402e8bd8ad6d77c88ac92040000',
|
|
98
|
+
vin: 0,
|
|
99
|
+
},
|
|
100
|
+
description: 'A negative version number is committed to as signed bytes',
|
|
101
|
+
expected: {
|
|
102
|
+
hash: '78ce09cf80ea77a65379cfca01af97312d9d6d03d011f9d53a0db17decb4a22f',
|
|
103
|
+
},
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
args: {
|
|
107
|
+
script: '210255a9626aebf5e29c0e6538428ba0d1dcf6ca98ffdf086aa8ced5e0d0215ea465ac',
|
|
108
|
+
sighash: 0x03,
|
|
109
|
+
tokens: 4900000000,
|
|
110
|
+
transaction: '0100000002fe3dc9208094f3ffd12645477b3dc56f60ec4fa8e6f5d67c565d1c6b9216b36e0000000000ffffffff0815cf020f013ed6cf91d29f4202e8a58726b1ac6c79da47c23d1bee0a6925f80000000000ffffffff0100f2052a010000001976a914a30741f8145e5acadf23f751864167f32e0963f788ac00000000',
|
|
111
|
+
vin: 1,
|
|
112
|
+
},
|
|
113
|
+
description: 'A script code following a code separator is committed to',
|
|
114
|
+
expected: {
|
|
115
|
+
hash: 'fef7bd749cce710c5c052bd796df1af0d935e59cea63736268bcbe2d2134fc47',
|
|
116
|
+
},
|
|
117
|
+
},
|
|
118
|
+
{
|
|
119
|
+
args: {
|
|
120
|
+
script: '0063ab68210392972e2eb617b2388771abe27235fd5ac44af8e61693261550447a4c3e39da98ac',
|
|
121
|
+
sighash: 0x83,
|
|
122
|
+
tokens: 16777215,
|
|
123
|
+
transaction: singleAnyoneCanPayTransaction,
|
|
124
|
+
vin: 0,
|
|
125
|
+
},
|
|
126
|
+
description: 'A single anyone can pay hash commits to the first input',
|
|
127
|
+
expected: {
|
|
128
|
+
hash: 'e9071e75e25b8a1e298a72f0d2e9f4f95a0f5cdf86a533cda597eb402ed13b3a',
|
|
129
|
+
},
|
|
130
|
+
},
|
|
131
|
+
{
|
|
132
|
+
args: {
|
|
133
|
+
script: '68210392972e2eb617b2388771abe27235fd5ac44af8e61693261550447a4c3e39da98ac',
|
|
134
|
+
sighash: 0x83,
|
|
135
|
+
tokens: 16777215,
|
|
136
|
+
transaction: singleAnyoneCanPayTransaction,
|
|
137
|
+
vin: 1,
|
|
138
|
+
},
|
|
139
|
+
description: 'A single anyone can pay hash commits to the second input',
|
|
140
|
+
expected: {
|
|
141
|
+
hash: 'cd72f1f1a433ee9df816857fad88d8ebd97e09a75cd481583eb841c330275e54',
|
|
142
|
+
},
|
|
143
|
+
},
|
|
144
|
+
{
|
|
145
|
+
args: {
|
|
146
|
+
script: 'ad4830450220487fb382c4974de3f7d834c1b617fe15860828c7f96454490edd6d891556dcc9022100baf95feb48f845d5bfc9882eb6aeefa1bc3790e39f59eaa46ff7f15ae626c53e01',
|
|
147
|
+
sighash: 0x01,
|
|
148
|
+
tokens: 200000,
|
|
149
|
+
transaction: '010000000169c12106097dc2e0526493ef67f21269fe888ef05c7a3a5dacab38e1ac8387f14c1d000000ffffffff0101000000000000000000000000',
|
|
150
|
+
vin: 0,
|
|
151
|
+
},
|
|
152
|
+
description: 'A signature in a script code is not found and deleted',
|
|
153
|
+
expected: {
|
|
154
|
+
hash: '71c9cd9b2869b9c70b01b1f0360c148f42dee72297db312638df136f43311f23',
|
|
155
|
+
},
|
|
156
|
+
},
|
|
157
|
+
{
|
|
158
|
+
args: {
|
|
159
|
+
script: '52af4830450220487fb382c4974de3f7d834c1b617fe15860828c7f96454490edd6d891556dcc9022100baf95feb48f845d5bfc9882eb6aeefa1bc3790e39f59eaa46ff7f15ae626c53e0148304502205286f726690b2e9b0207f0345711e63fa7012045b9eb0f19c2458ce1db90cf43022100e89f17f86abc5b149eba4115d4f128bcf45d77fb3ecdd34f594091340c0395960175',
|
|
160
|
+
sighash: 0x01,
|
|
161
|
+
tokens: 200000,
|
|
162
|
+
transaction: '01000000019275cb8d4a485ce95741c013f7c0d28722160008021bb469a11982d47a6628964c1d000000ffffffff010100000000000000000000000000',
|
|
163
|
+
vin: 0,
|
|
164
|
+
},
|
|
165
|
+
description: 'Signatures in a multisig script code are not deleted',
|
|
166
|
+
expected: {
|
|
167
|
+
hash: 'c1628a1e7c67f14ca0c27c06e4fdeec2e6d1a73c7a91d7c046ff83e835aebb72',
|
|
168
|
+
},
|
|
169
|
+
},
|
|
170
|
+
{
|
|
171
|
+
args: {
|
|
172
|
+
script: p2shP2wshWitnessScript,
|
|
173
|
+
sighash: 0x01,
|
|
174
|
+
tokens: 987654321,
|
|
175
|
+
transaction: nativeP2shP2wshTransaction,
|
|
176
|
+
vin: 0,
|
|
177
|
+
},
|
|
178
|
+
description: 'A P2SH-P2WSH input hash to sign is calculated for all',
|
|
179
|
+
expected: {
|
|
180
|
+
hash: '185c0be5263dce5b4bb50a047973c1b6272bfbd0103a89444597dc40b248ee7c',
|
|
181
|
+
},
|
|
182
|
+
},
|
|
183
|
+
{
|
|
184
|
+
args: {
|
|
185
|
+
script: p2shP2wshWitnessScript,
|
|
186
|
+
sighash: 0x02,
|
|
187
|
+
tokens: 987654321,
|
|
188
|
+
transaction: nativeP2shP2wshTransaction,
|
|
189
|
+
vin: 0,
|
|
190
|
+
},
|
|
191
|
+
description: 'A P2SH-P2WSH input hash to sign is calculated for none',
|
|
192
|
+
expected: {
|
|
193
|
+
hash: 'e9733bc60ea13c95c6527066bb975a2ff29a925e80aa14c213f686cbae5d2f36',
|
|
194
|
+
},
|
|
195
|
+
},
|
|
196
|
+
{
|
|
197
|
+
args: {
|
|
198
|
+
script: p2shP2wshWitnessScript,
|
|
199
|
+
sighash: 0x03,
|
|
200
|
+
tokens: 987654321,
|
|
201
|
+
transaction: nativeP2shP2wshTransaction,
|
|
202
|
+
vin: 0,
|
|
203
|
+
},
|
|
204
|
+
description: 'A P2SH-P2WSH input hash to sign is calculated for single',
|
|
205
|
+
expected: {
|
|
206
|
+
hash: '1e1f1c303dc025bd664acb72e583e933fae4cff9148bf78c157d1e8f78530aea',
|
|
207
|
+
},
|
|
208
|
+
},
|
|
209
|
+
{
|
|
210
|
+
args: {
|
|
211
|
+
script: p2shP2wshWitnessScript,
|
|
212
|
+
sighash: 0x81,
|
|
213
|
+
tokens: 987654321,
|
|
214
|
+
transaction: nativeP2shP2wshTransaction,
|
|
215
|
+
vin: 0,
|
|
216
|
+
},
|
|
217
|
+
description: 'A P2SH-P2WSH hash to sign is calculated for all one pays',
|
|
218
|
+
expected: {
|
|
219
|
+
hash: '2a67f03e63a6a422125878b40b82da593be8d4efaafe88ee528af6e5a9955c6e',
|
|
220
|
+
},
|
|
221
|
+
},
|
|
222
|
+
{
|
|
223
|
+
args: {
|
|
224
|
+
script: p2shP2wshWitnessScript,
|
|
225
|
+
sighash: 0x82,
|
|
226
|
+
tokens: 987654321,
|
|
227
|
+
transaction: nativeP2shP2wshTransaction,
|
|
228
|
+
vin: 0,
|
|
229
|
+
},
|
|
230
|
+
description: 'A P2SH-P2WSH hash to sign is calculated for none one pays',
|
|
231
|
+
expected: {
|
|
232
|
+
hash: '781ba15f3779d5542ce8ecb5c18716733a5ee42a6f51488ec96154934e2c890a',
|
|
233
|
+
},
|
|
234
|
+
},
|
|
235
|
+
{
|
|
236
|
+
args: {
|
|
237
|
+
script: p2shP2wshWitnessScript,
|
|
238
|
+
sighash: 0x83,
|
|
239
|
+
tokens: 987654321,
|
|
240
|
+
transaction: nativeP2shP2wshTransaction,
|
|
241
|
+
vin: 0,
|
|
242
|
+
},
|
|
243
|
+
description: 'A P2SH-P2WSH hash to sign is calculated for single one pays',
|
|
244
|
+
expected: {
|
|
245
|
+
hash: '511e8e52ed574121fc1b654970395502128263f62662e076dc6baf05c2e6a99b',
|
|
246
|
+
},
|
|
247
|
+
},
|
|
248
|
+
];
|
|
249
|
+
|
|
250
|
+
tests.forEach(({args, description, error, expected}) => {
|
|
251
|
+
return test(description, (t, end) => {
|
|
252
|
+
if (!!error) {
|
|
253
|
+
throws(() => v0HashToSign(args), new Error(error), 'Got error');
|
|
254
|
+
} else {
|
|
255
|
+
const res = v0HashToSign(args);
|
|
256
|
+
|
|
257
|
+
deepStrictEqual(res, expected, 'Got expected result');
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
return end();
|
|
261
|
+
});
|
|
262
|
+
});
|
package/transactions/index.js
CHANGED
|
@@ -6,6 +6,7 @@ const queryTransactions = require('./query_transactions');
|
|
|
6
6
|
const sizeOfTransaction = require('./size_of_transaction');
|
|
7
7
|
const transactionFromComponents = require('./transaction_from_components');
|
|
8
8
|
const unsignedTxFromPsbt = require('./unsigned_tx_from_psbt');
|
|
9
|
+
const v0HashToSign = require('./v0_hash_to_sign');
|
|
9
10
|
|
|
10
11
|
module.exports = {
|
|
11
12
|
componentsOfTransaction,
|
|
@@ -16,4 +17,5 @@ module.exports = {
|
|
|
16
17
|
sizeOfTransaction,
|
|
17
18
|
transactionFromComponents,
|
|
18
19
|
unsignedTxFromPsbt,
|
|
20
|
+
v0HashToSign,
|
|
19
21
|
};
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
const {createHash} = require('crypto');
|
|
2
|
+
|
|
3
|
+
const {numberAsCompactInt} = require('./../numbers');
|
|
4
|
+
const {numberAsLittleEndian} = require('./../numbers');
|
|
5
|
+
const parseTransaction = require('./parse_transaction');
|
|
6
|
+
|
|
7
|
+
const {alloc} = Buffer;
|
|
8
|
+
const anyOnePaysFlag = 0x80;
|
|
9
|
+
const bufferAsHex = buffer => buffer.toString('hex');
|
|
10
|
+
const byteCountHash = 32;
|
|
11
|
+
const {concat} = Buffer;
|
|
12
|
+
const defaultHashType = 0x01;
|
|
13
|
+
const hash256 = preimage => sha256(sha256(preimage));
|
|
14
|
+
const hashTypes = [0x01, 0x02, 0x03, 0x81, 0x82, 0x83];
|
|
15
|
+
const hexAsBuffer = hex => Buffer.from(hex, 'hex');
|
|
16
|
+
const int32 = number => littleEndian({number, bytes: 4, is_signed: true});
|
|
17
|
+
const isHex = n => !!n && !(n.length % 2) && /^[0-9A-F]*$/i.test(n);
|
|
18
|
+
const {isInteger} = Number;
|
|
19
|
+
const isString = n => typeof n === 'string';
|
|
20
|
+
const littleEndian = args => numberAsLittleEndian(args).encoded;
|
|
21
|
+
const outputTypeMask = 0x1f;
|
|
22
|
+
const sha256 = preimage => createHash('sha256').update(preimage).digest();
|
|
23
|
+
const sigHashNone = 2;
|
|
24
|
+
const sigHashSingle = 3;
|
|
25
|
+
const sizeOf = script => numberAsCompactInt({number: script.length}).encoded;
|
|
26
|
+
const uint32 = number => littleEndian({number, bytes: 4});
|
|
27
|
+
const uint64 = number => littleEndian({number, bytes: 8});
|
|
28
|
+
const zeroHash = alloc(byteCountHash);
|
|
29
|
+
|
|
30
|
+
/** Calculate the v0 witness transaction hash to sign
|
|
31
|
+
|
|
32
|
+
The script is the BIP 143 script code of the output being spent
|
|
33
|
+
|
|
34
|
+
For P2WPKH the script code is the P2PKH script of the public key hash
|
|
35
|
+
|
|
36
|
+
For P2WSH the script code is the witness script
|
|
37
|
+
|
|
38
|
+
{
|
|
39
|
+
script: <Signing Input Script Code Hex String>
|
|
40
|
+
[sighash]: <Signature Hash Type Number>
|
|
41
|
+
tokens: <Spending Output Tokens Number>
|
|
42
|
+
transaction: <Raw Transaction Hex String>
|
|
43
|
+
vin: <Signing Transaction Input Index Number>
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
@throws
|
|
47
|
+
<Error>
|
|
48
|
+
|
|
49
|
+
@returns
|
|
50
|
+
{
|
|
51
|
+
hash: <Hash to Sign Hex String>
|
|
52
|
+
}
|
|
53
|
+
*/
|
|
54
|
+
module.exports = ({script, sighash, tokens, transaction, vin}) => {
|
|
55
|
+
if (!isString(script) || !isHex(script)) {
|
|
56
|
+
throw new Error('ExpectedScriptCodeHexStringToCalculateV0HashToSign');
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (!!sighash && !hashTypes.includes(sighash)) {
|
|
60
|
+
throw new Error('ExpectedValidSignatureHashTypeToCalculateV0HashToSign');
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (!isInteger(tokens)) {
|
|
64
|
+
throw new Error('ExpectedSpendTokensNumberToCalculateV0HashToSign');
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (!isString(transaction) || !isHex(transaction)) {
|
|
68
|
+
throw new Error('ExpectedTransactionHexStringToCalculateV0HashToSign');
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (!isInteger(vin)) {
|
|
72
|
+
throw new Error('ExpectedInputIndexNumberToCalculateV0HashToSign');
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const decoded = parseTransaction({buffer: hexAsBuffer(transaction)});
|
|
76
|
+
const hashType = sighash || defaultHashType;
|
|
77
|
+
const scriptCode = hexAsBuffer(script);
|
|
78
|
+
|
|
79
|
+
// The signing input is expected to be present in the transaction
|
|
80
|
+
if (!decoded.inputs[vin]) {
|
|
81
|
+
throw new Error('ExpectedMatchingInputToCalculateV0HashToSign');
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const isAnyOnePays = !!(hashType & anyOnePaysFlag);
|
|
85
|
+
const outputType = hashType & outputTypeMask;
|
|
86
|
+
|
|
87
|
+
// Signing for all outputs is any type that isn't for none or a single one
|
|
88
|
+
const isAllOutputs = ![sigHashNone, sigHashSingle].includes(outputType);
|
|
89
|
+
|
|
90
|
+
// The hash commits to inputs as outpoints and little endian sequences
|
|
91
|
+
const spending = decoded.inputs.map(({hash, sequence, vout}) => ({
|
|
92
|
+
outpoint: concat([hash, uint32(vout)]),
|
|
93
|
+
sequence: uint32(sequence),
|
|
94
|
+
}));
|
|
95
|
+
|
|
96
|
+
const spend = spending[vin];
|
|
97
|
+
|
|
98
|
+
// Elements to hash over start with the signed transaction version number
|
|
99
|
+
const elements = [int32(decoded.version)];
|
|
100
|
+
|
|
101
|
+
// Commit to all of the outpoints being spent unless anyone can pay
|
|
102
|
+
if (!isAnyOnePays) {
|
|
103
|
+
elements.push(hash256(concat(spending.map(({outpoint}) => outpoint))));
|
|
104
|
+
} else {
|
|
105
|
+
elements.push(zeroHash);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Commit to all input sequence numbers when signing for all outputs
|
|
109
|
+
if (!isAnyOnePays && isAllOutputs) {
|
|
110
|
+
elements.push(hash256(concat(spending.map(({sequence}) => sequence))));
|
|
111
|
+
} else {
|
|
112
|
+
elements.push(zeroHash);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// Commit to the outpoint, script code, value, sequence of the signing input
|
|
116
|
+
elements.push(spend.outpoint);
|
|
117
|
+
elements.push(sizeOf(scriptCode));
|
|
118
|
+
elements.push(scriptCode);
|
|
119
|
+
elements.push(uint64(tokens));
|
|
120
|
+
elements.push(spend.sequence);
|
|
121
|
+
|
|
122
|
+
const output = decoded.outputs[vin];
|
|
123
|
+
|
|
124
|
+
if (isAllOutputs) {
|
|
125
|
+
// Commit to all of the outputs of the transaction
|
|
126
|
+
const outputs = decoded.outputs.map(({script, tokens}) => {
|
|
127
|
+
return concat([uint64(tokens), sizeOf(script), script]);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
elements.push(hash256(concat(outputs)));
|
|
131
|
+
} else if (outputType === sigHashSingle && !!output) {
|
|
132
|
+
// Commit to the matching output when signing for a single output
|
|
133
|
+
elements.push(hash256(concat([
|
|
134
|
+
uint64(output.tokens),
|
|
135
|
+
sizeOf(output.script),
|
|
136
|
+
output.script,
|
|
137
|
+
])));
|
|
138
|
+
} else {
|
|
139
|
+
// There are no outputs committed to when signing for none
|
|
140
|
+
elements.push(zeroHash);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Commit to the transaction locktime and the signature hash type
|
|
144
|
+
elements.push(uint32(decoded.locktime));
|
|
145
|
+
elements.push(uint32(hashType));
|
|
146
|
+
|
|
147
|
+
// The hash to sign is the double sha256 hash of the elements
|
|
148
|
+
return {hash: bufferAsHex(hash256(concat(elements)))};
|
|
149
|
+
};
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
const {strictEqual} = require('node:assert').strict;
|
|
2
|
-
const test = require('node:test');
|
|
3
|
-
|
|
4
|
-
const parsePushBytesCount = require('./../../script/parse_push_bytes_count');
|
|
5
|
-
|
|
6
|
-
test('An unsupported op code is not parsed as a push', (t, end) => {
|
|
7
|
-
const res = parsePushBytesCount({
|
|
8
|
-
offset: 0,
|
|
9
|
-
script: Buffer.from('4f00000000', 'hex'),
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
strictEqual(res, undefined, 'Got no push bytes count');
|
|
13
|
-
|
|
14
|
-
return end();
|
|
15
|
-
});
|
|
File without changes
|