@alexbosworth/blockchain 4.0.0 → 4.1.1

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 CHANGED
@@ -1,5 +1,9 @@
1
1
  # Versions
2
2
 
3
+ ## 4.1.1
4
+
5
+ - `p2msScript`: Add method to get pay to multi-sig script for keys + required
6
+
3
7
  ## 4.0.0
4
8
 
5
9
  - `sizeOfTransaction`: Add method to get transaction weight and virtual size
package/README.md CHANGED
@@ -13,6 +13,7 @@ Utility methods for working with Blockchain data
13
13
  - [idForTransactionComponents](#idfortransactioncomponents)
14
14
  - [noLocktimeIdForTransaction](#nolocktimeidfortransaction)
15
15
  - [numberAsCompactInt](#numberascompactint)
16
+ - [p2msScript](#p2msscript)
16
17
  - [p2pkhOutputScript](#p2pkhoutputscript)
17
18
  - [p2shOutputScript](#p2shoutputscript)
18
19
  - [p2trOutputScript](#p2troutputscript)
@@ -206,6 +207,38 @@ Convert a number to compact size integer serialization
206
207
  encoded: <Serialized Compact Integer Buffer Object>
207
208
  }
208
209
 
210
+ ### p2msScript
211
+
212
+ Get a Pay To Multisig Script and the script hash to pay to the script
213
+
214
+ Use `hash` with `p2wshOutputScript` for a P2WSH output script
215
+
216
+ {
217
+ keys: [<Public Key Buffer Object>]
218
+ required: <Signatures Required Count Number>
219
+ }
220
+
221
+ @throws
222
+ <Error>
223
+
224
+ @returns
225
+ {
226
+ hash: <Witness Script SHA256 Hash Buffer Object>
227
+ script: <Multisig Script Buffer Object>
228
+ }
229
+
230
+ Example:
231
+
232
+ ```node
233
+ const {p2msScript, p2wshOutputScript} = require('@alexbosworth/blockchain');
234
+
235
+ // Derive the 2-of-3 multisig script and its witness script hash
236
+ const {hash} = p2msScript({keys: publicKeys, required: 2});
237
+
238
+ // Get the P2WSH output script paying to the multisig script
239
+ const {script} = p2wshOutputScript({hash});
240
+ ```
241
+
209
242
  ### p2pkhOutputScript
210
243
 
211
244
  Get a Pay To Witness Public Key Hash Output Script
package/index.js CHANGED
@@ -7,6 +7,7 @@ const {idForTransaction} = require('./transactions');
7
7
  const {idForTransactionComponents} = require('./hashes');
8
8
  const {noLocktimeIdForTransaction} = require('./transactions');
9
9
  const {numberAsCompactInt} = require('./numbers');
10
+ const {p2msScript} = require('./script');
10
11
  const {p2pkhOutputScript} = require('./script');
11
12
  const {p2shOutputScript} = require('./script');
12
13
  const {p2trOutputScript} = require('./script');
@@ -29,6 +30,7 @@ module.exports = {
29
30
  idForTransactionComponents,
30
31
  noLocktimeIdForTransaction,
31
32
  numberAsCompactInt,
33
+ p2msScript,
32
34
  p2pkhOutputScript,
33
35
  p2shOutputScript,
34
36
  p2trOutputScript,
package/package.json CHANGED
@@ -19,5 +19,5 @@
19
19
  "scripts": {
20
20
  "test": "node --experimental-test-coverage --test test/addresses/*.js test/hashes/*.js test/numbers/*.js test/script/*.js test/transactions/*.js"
21
21
  },
22
- "version": "4.0.0"
22
+ "version": "4.1.1"
23
23
  }
package/script/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ const p2msScript = require('./p2ms_script');
1
2
  const p2pkhOutputScript = require('./p2pkh_output_script');
2
3
  const p2shOutputScript = require('./p2sh_output_script');
3
4
  const p2trOutputScript = require('./p2tr_output_script');
@@ -7,6 +8,7 @@ const scriptAsScriptElements = require('./script_as_script_elements');
7
8
  const scriptElementsAsScript = require('./script_elements_as_script');
8
9
 
9
10
  module.exports = {
11
+ p2msScript,
10
12
  p2pkhOutputScript,
11
13
  p2shOutputScript,
12
14
  p2trOutputScript,
@@ -0,0 +1,63 @@
1
+ const {createHash} = require('node:crypto');
2
+
3
+ const scriptElementsAsOutput = require('./script_elements_as_output');
4
+
5
+ const {isArray} = Array;
6
+ const {isBuffer} = Buffer;
7
+ const {isInteger} = Number;
8
+ const maxKeysCount = 20;
9
+ const numberAsElement = num => num <= 16 ? 0x50 + num : Buffer.from([num]);
10
+ const OP_CHECKMULTISIG = 0xae;
11
+ const publicKeyLength = 33;
12
+ const sha256 = data => createHash('sha256').update(data).digest();
13
+
14
+ /** Get a Pay To Multisig Script and the script hash to pay to the script
15
+
16
+ Use `hash` with `p2wshOutputScript` for a P2WSH output script
17
+
18
+ {
19
+ keys: [<Public Key Buffer Object>]
20
+ required: <Signatures Required Count Number>
21
+ }
22
+
23
+ @throws
24
+ <Error>
25
+
26
+ @returns
27
+ {
28
+ hash: <Witness Script SHA256 Hash Buffer Object>
29
+ script: <Multisig Script Buffer Object>
30
+ }
31
+ */
32
+ module.exports = ({keys, required}) => {
33
+ if (!isArray(keys) || !keys.length) {
34
+ throw new Error('ExpectedArrayOfPublicKeysToDeriveP2msScript');
35
+ }
36
+
37
+ if (keys.length > maxKeysCount) {
38
+ throw new Error('ExpectedFewerPublicKeysToDeriveP2msScript');
39
+ }
40
+
41
+ if (!!keys.find(key => !isBuffer(key) || key.length !== publicKeyLength)) {
42
+ throw new Error('ExpectedCompressedPublicKeysToDeriveP2msScript');
43
+ }
44
+
45
+ if (!required || !isInteger(required)) {
46
+ throw new Error('ExpectedRequiredKeysCountToDeriveP2msScript');
47
+ }
48
+
49
+ if (required > keys.length) {
50
+ throw new Error('UnexpectedCountOfRequiredKeysToDeriveP2msScript');
51
+ }
52
+
53
+ const {output} = scriptElementsAsOutput({
54
+ elements: [
55
+ numberAsElement(required),
56
+ ...keys,
57
+ numberAsElement(keys.length),
58
+ OP_CHECKMULTISIG,
59
+ ],
60
+ });
61
+
62
+ return {hash: sha256(output), script: output};
63
+ };
@@ -0,0 +1,101 @@
1
+ const {deepStrictEqual} = require('node:assert').strict;
2
+ const {throws} = require('node:assert').strict;
3
+ const test = require('node:test');
4
+
5
+ const {p2msScript} = require('./../../');
6
+
7
+ const bufferFromHex = hex => Buffer.from(hex, 'hex');
8
+
9
+ // Public keys from the BIP 143 P2SH-P2WSH 6-of-6 multisig example
10
+ const bip143MultisigKeys = [
11
+ '0307b8ae49ac90a048e9b53357a2354b3334e9c8bee813ecb98e99a7e07e8c3ba3',
12
+ '03b28f0c28bfab54554ae8c658ac5c3e0ce6e79ad336331f78c428dd43eea8449b',
13
+ '034b8113d703413d57761b8b9781957b8c0ac1dfe69f492580ca4195f50376ba4a',
14
+ '033400f6afecb833092a9a21cfdf1ed1376e58c5d1f47de74683123987e967a8f4',
15
+ '03a6d48b1131e94ba04d9737d61acdaa1322008af9602b3b14862c07a1789aac16',
16
+ '02d8b661b0b3302ee2f162b09e07a55ad5dfbe673a9f01d9f0c19617681024306b',
17
+ ];
18
+
19
+ // Twenty derived placeholder public keys to test past the op number range
20
+ const twentyKeys = bip143MultisigKeys
21
+ .concat(bip143MultisigKeys)
22
+ .concat(bip143MultisigKeys)
23
+ .slice(0, 18)
24
+ .concat(['0307b8ae49ac90a048e9b53357a2354b3334e9c8bee813ecb98e99a7e07e8c3ba3'])
25
+ .concat(['03b28f0c28bfab54554ae8c658ac5c3e0ce6e79ad336331f78c428dd43eea8449b']);
26
+
27
+ const tests = [
28
+ {
29
+ args: {},
30
+ description: 'An array of public keys is required',
31
+ error: 'ExpectedArrayOfPublicKeysToDeriveP2msScript',
32
+ },
33
+ {
34
+ args: {keys: []},
35
+ description: 'A non empty array of public keys is required',
36
+ error: 'ExpectedArrayOfPublicKeysToDeriveP2msScript',
37
+ },
38
+ {
39
+ args: {keys: twentyKeys.concat(twentyKeys).map(bufferFromHex)},
40
+ description: 'A maximum of twenty public keys is required',
41
+ error: 'ExpectedFewerPublicKeysToDeriveP2msScript',
42
+ },
43
+ {
44
+ args: {keys: [bufferFromHex('00')]},
45
+ description: 'Compressed public keys are required',
46
+ error: 'ExpectedCompressedPublicKeysToDeriveP2msScript',
47
+ },
48
+ {
49
+ args: {keys: bip143MultisigKeys.map(bufferFromHex)},
50
+ description: 'A required keys count is required',
51
+ error: 'ExpectedRequiredKeysCountToDeriveP2msScript',
52
+ },
53
+ {
54
+ args: {keys: bip143MultisigKeys.map(bufferFromHex), required: 1.5},
55
+ description: 'An integer required keys count is required',
56
+ error: 'ExpectedRequiredKeysCountToDeriveP2msScript',
57
+ },
58
+ {
59
+ args: {keys: bip143MultisigKeys.map(bufferFromHex), required: 7},
60
+ description: 'A required count within the keys count is required',
61
+ error: 'UnexpectedCountOfRequiredKeysToDeriveP2msScript',
62
+ },
63
+ {
64
+ args: {keys: bip143MultisigKeys.map(bufferFromHex), required: 6},
65
+ description: 'A multisig script is derived for the BIP 143 example keys',
66
+ expected: {
67
+ hash: bufferFromHex('a16b5755f7f6f96dbd65f5f0d6ab9418b89af4b1f14a1bb8a09062c35f0dcb54'),
68
+ script: bufferFromHex('56210307b8ae49ac90a048e9b53357a2354b3334e9c8bee813ecb98e99a7e07e8c3ba32103b28f0c28bfab54554ae8c658ac5c3e0ce6e79ad336331f78c428dd43eea8449b21034b8113d703413d57761b8b9781957b8c0ac1dfe69f492580ca4195f50376ba4a21033400f6afecb833092a9a21cfdf1ed1376e58c5d1f47de74683123987e967a8f42103a6d48b1131e94ba04d9737d61acdaa1322008af9602b3b14862c07a1789aac162102d8b661b0b3302ee2f162b09e07a55ad5dfbe673a9f01d9f0c19617681024306b56ae'),
69
+ },
70
+ },
71
+ {
72
+ args: {keys: bip143MultisigKeys.slice(0, 3).map(bufferFromHex), required: 2},
73
+ description: 'A two of three multisig script is derived',
74
+ expected: {
75
+ hash: bufferFromHex('04facdf99ba64a979191911d7643e52af145ac7b5eb3168c16bec40702f6b9d2'),
76
+ script: bufferFromHex('52210307b8ae49ac90a048e9b53357a2354b3334e9c8bee813ecb98e99a7e07e8c3ba32103b28f0c28bfab54554ae8c658ac5c3e0ce6e79ad336331f78c428dd43eea8449b21034b8113d703413d57761b8b9781957b8c0ac1dfe69f492580ca4195f50376ba4a53ae'),
77
+ },
78
+ },
79
+ {
80
+ args: {keys: twentyKeys.map(bufferFromHex), required: 17},
81
+ description: 'Counts past the op number range are encoded as data pushes',
82
+ expected: {
83
+ hash: bufferFromHex('01312333beba0c1b3a3814d64296528cbeb9a344356d606baabd4782204a3c6c'),
84
+ script: bufferFromHex('0111210307b8ae49ac90a048e9b53357a2354b3334e9c8bee813ecb98e99a7e07e8c3ba32103b28f0c28bfab54554ae8c658ac5c3e0ce6e79ad336331f78c428dd43eea8449b21034b8113d703413d57761b8b9781957b8c0ac1dfe69f492580ca4195f50376ba4a21033400f6afecb833092a9a21cfdf1ed1376e58c5d1f47de74683123987e967a8f42103a6d48b1131e94ba04d9737d61acdaa1322008af9602b3b14862c07a1789aac162102d8b661b0b3302ee2f162b09e07a55ad5dfbe673a9f01d9f0c19617681024306b210307b8ae49ac90a048e9b53357a2354b3334e9c8bee813ecb98e99a7e07e8c3ba32103b28f0c28bfab54554ae8c658ac5c3e0ce6e79ad336331f78c428dd43eea8449b21034b8113d703413d57761b8b9781957b8c0ac1dfe69f492580ca4195f50376ba4a21033400f6afecb833092a9a21cfdf1ed1376e58c5d1f47de74683123987e967a8f42103a6d48b1131e94ba04d9737d61acdaa1322008af9602b3b14862c07a1789aac162102d8b661b0b3302ee2f162b09e07a55ad5dfbe673a9f01d9f0c19617681024306b210307b8ae49ac90a048e9b53357a2354b3334e9c8bee813ecb98e99a7e07e8c3ba32103b28f0c28bfab54554ae8c658ac5c3e0ce6e79ad336331f78c428dd43eea8449b21034b8113d703413d57761b8b9781957b8c0ac1dfe69f492580ca4195f50376ba4a21033400f6afecb833092a9a21cfdf1ed1376e58c5d1f47de74683123987e967a8f42103a6d48b1131e94ba04d9737d61acdaa1322008af9602b3b14862c07a1789aac162102d8b661b0b3302ee2f162b09e07a55ad5dfbe673a9f01d9f0c19617681024306b210307b8ae49ac90a048e9b53357a2354b3334e9c8bee813ecb98e99a7e07e8c3ba32103b28f0c28bfab54554ae8c658ac5c3e0ce6e79ad336331f78c428dd43eea8449b0114ae'),
85
+ },
86
+ },
87
+ ];
88
+
89
+ tests.forEach(({args, description, error, expected}) => {
90
+ return test(description, (t, end) => {
91
+ if (!!error) {
92
+ throws(() => p2msScript(args), new Error(error), 'Got err');
93
+ } else {
94
+ const res = p2msScript(args);
95
+
96
+ deepStrictEqual(res, expected, 'Got expected result');
97
+ }
98
+
99
+ return end();
100
+ });
101
+ });