@alexbosworth/blockchain 3.2.0 → 4.1.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 CHANGED
@@ -1,5 +1,17 @@
1
1
  # Versions
2
2
 
3
+ ## 4.1.0
4
+
5
+ - `p2msScript`: Add method to get pay to multi-sig script for keys + required
6
+
7
+ ## 4.0.0
8
+
9
+ - `sizeOfTransaction`: Add method to get transaction weight and virtual size
10
+
11
+ ### Breaking Changes
12
+
13
+ - End support for Node.js v20, require v22+
14
+
3
15
  ## 3.2.0
4
16
 
5
17
  - `decode_bech32_address.js`: Add method to decode a bech32/bech32m address
package/README.md CHANGED
@@ -4,6 +4,28 @@ Utility methods for working with Blockchain data
4
4
 
5
5
  ## Methods
6
6
 
7
+ - [compactIntAsNumber](#compactintasnumber)
8
+ - [componentsOfTransaction](#componentsoftransaction)
9
+ - [decodeBase58Address](#decodebase58address)
10
+ - [decodeBech32Address](#decodebech32address)
11
+ - [idForBlock](#idforblock)
12
+ - [idForTransaction](#idfortransaction)
13
+ - [idForTransactionComponents](#idfortransactioncomponents)
14
+ - [noLocktimeIdForTransaction](#nolocktimeidfortransaction)
15
+ - [numberAsCompactInt](#numberascompactint)
16
+ - [p2msScript](#p2msscript)
17
+ - [p2pkhOutputScript](#p2pkhoutputscript)
18
+ - [p2shOutputScript](#p2shoutputscript)
19
+ - [p2trOutputScript](#p2troutputscript)
20
+ - [p2wpkhOutputScript](#p2wpkhoutputscript)
21
+ - [p2wshOutputScript](#p2wshoutputscript)
22
+ - [previousBlockId](#previousblockid)
23
+ - [queryTransactions](#querytransactions)
24
+ - [scriptAsScriptElements](#scriptasscriptelements)
25
+ - [scriptElementsAsScript](#scriptelementsasscript)
26
+ - [sizeOfTransaction](#sizeoftransaction)
27
+ - [unsignedTxFromPsbt](#unsignedtxfrompsbt)
28
+
7
29
  ### compactIntAsNumber
8
30
 
9
31
  Convert a compact integer to a regular number
@@ -185,6 +207,38 @@ Convert a number to compact size integer serialization
185
207
  encoded: <Serialized Compact Integer Buffer Object>
186
208
  }
187
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
+
188
242
  ### p2pkhOutputScript
189
243
 
190
244
  Get a Pay To Witness Public Key Hash Output Script
@@ -335,6 +389,23 @@ Map array of script buffer elements to a fully formed script
335
389
  script: <Script Hex String>
336
390
  }
337
391
 
392
+ ### sizeOfTransaction
393
+
394
+ Get the weight and virtual size of a hex-encoded transaction
395
+
396
+ {
397
+ transaction: <Hex Encoded Transaction String>
398
+ }
399
+
400
+ @throws
401
+ <Error>
402
+
403
+ @returns
404
+ {
405
+ vsize: <Transaction Virtual Size Number>
406
+ weight: <Transaction Weight Units Number>
407
+ }
408
+
338
409
  ### unsignedTxFromPsbt
339
410
 
340
411
  Get the unsigned transaction out of a PSBT
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');
@@ -16,6 +17,7 @@ const {previousBlockId} = require('./hashes');
16
17
  const {queryTransactions} = require('./transactions');
17
18
  const {scriptAsScriptElements} = require('./script');
18
19
  const {scriptElementsAsScript} = require('./script');
20
+ const {sizeOfTransaction} = require('./transactions');
19
21
  const {unsignedTxFromPsbt} = require('./transactions');
20
22
 
21
23
  module.exports = {
@@ -28,6 +30,7 @@ module.exports = {
28
30
  idForTransactionComponents,
29
31
  noLocktimeIdForTransaction,
30
32
  numberAsCompactInt,
33
+ p2msScript,
31
34
  p2pkhOutputScript,
32
35
  p2shOutputScript,
33
36
  p2trOutputScript,
@@ -37,5 +40,6 @@ module.exports = {
37
40
  queryTransactions,
38
41
  scriptAsScriptElements,
39
42
  scriptElementsAsScript,
43
+ sizeOfTransaction,
40
44
  unsignedTxFromPsbt,
41
45
  };
@@ -40,14 +40,13 @@ module.exports = ({encoded, start}) => {
40
40
 
41
41
  case 5:
42
42
  return {bytes, number: encoded.readUInt32LE(offset + lengthMarkerOffset)};
43
+ }
43
44
 
44
- default:
45
- const number = encoded.readBigUInt64LE(offset + lengthMarkerOffset);
46
-
47
- if (!isSafeNumber(number)) {
48
- throw new Error('ExpectedSafeSizeEncodedCompactInteger');
49
- }
45
+ const number = encoded.readBigUInt64LE(offset + lengthMarkerOffset);
50
46
 
51
- return {bytes, number: Number(number)};
47
+ if (!isSafeNumber(number)) {
48
+ throw new Error('ExpectedSafeSizeEncodedCompactInteger');
52
49
  }
50
+
51
+ return {bytes, number: Number(number)};
53
52
  };
package/package.json CHANGED
@@ -8,7 +8,7 @@
8
8
  },
9
9
  "description": "Blockchain data utility methods",
10
10
  "engines": {
11
- "node": ">=20"
11
+ "node": ">=22"
12
12
  },
13
13
  "license": "MIT",
14
14
  "name": "@alexbosworth/blockchain",
@@ -17,7 +17,7 @@
17
17
  "url": "https://github.com/alexbosworth/blockchain.git"
18
18
  },
19
19
  "scripts": {
20
- "test": "npx nyc@17.1.0 node --experimental-test-coverage --test test/addresses/*.js test/hashes/*.js test/numbers/*.js test/script/*.js test/transactions/*.js"
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": "3.2.0"
22
+ "version": "4.1.0"
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
+ };
@@ -55,7 +55,6 @@ module.exports = ({offset, script}) => {
55
55
  };
56
56
 
57
57
  case OP_PUSHDATA4:
58
- default:
59
58
  // Exit early when there isn't enough data for UInt32
60
59
  if (data.length < countUInt32Bytes) {
61
60
  return {};
@@ -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
+ });
@@ -0,0 +1,15 @@
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
+ });
@@ -0,0 +1,84 @@
1
+ const {deepStrictEqual} = require('node:assert').strict;
2
+ const {throws} = require('node:assert').strict;
3
+ const test = require('node:test');
4
+
5
+ const {sizeOfTransaction} = require('./../../');
6
+
7
+ const legacyTransaction = [
8
+ '02000000',
9
+ '01',
10
+ '0000000000000000000000000000000000000000000000000000000000000000',
11
+ '00000000',
12
+ '00',
13
+ 'ffffffff',
14
+ '01',
15
+ '0100000000000000',
16
+ '01',
17
+ '51',
18
+ '00000000',
19
+ ].join('');
20
+
21
+ const witnessTransaction = [
22
+ '02000000',
23
+ '0001',
24
+ '01',
25
+ '0000000000000000000000000000000000000000000000000000000000000000',
26
+ '00000000',
27
+ '00',
28
+ 'ffffffff',
29
+ '01',
30
+ '0100000000000000',
31
+ '01',
32
+ '51',
33
+ '01',
34
+ '01',
35
+ '00',
36
+ '00000000',
37
+ ].join('');
38
+
39
+ const tests = [
40
+ {
41
+ args: {transaction: ''},
42
+ description: 'A transaction is required',
43
+ error: 'ExpectedHexEncodedTransactionToGetSize',
44
+ },
45
+ {
46
+ args: {transaction: '00f'},
47
+ description: 'An even-length transaction is required',
48
+ error: 'ExpectedHexEncodedTransactionToGetSize',
49
+ },
50
+ {
51
+ args: {transaction: 'not hex'},
52
+ description: 'A hex-encoded transaction is required',
53
+ error: 'ExpectedHexEncodedTransactionToGetSize',
54
+ },
55
+ {
56
+ args: {transaction: `${legacyTransaction}00`},
57
+ description: 'Data after the transaction is not accepted',
58
+ error: 'UnexpectedDataAfterTransactionToGetSize',
59
+ },
60
+ {
61
+ args: {transaction: legacyTransaction},
62
+ description: 'The size of a legacy transaction is returned',
63
+ expected: {vsize: 61, weight: 244},
64
+ },
65
+ {
66
+ args: {transaction: witnessTransaction},
67
+ description: 'The size of a witness transaction is returned',
68
+ expected: {vsize: 63, weight: 249},
69
+ },
70
+ ];
71
+
72
+ tests.forEach(({args, description, error, expected}) => {
73
+ return test(description, (t, end) => {
74
+ if (!!error) {
75
+ throws(() => sizeOfTransaction(args), new Error(error), 'Got error');
76
+ } else {
77
+ const res = sizeOfTransaction(args);
78
+
79
+ deepStrictEqual(res, expected, 'Got expected result');
80
+ }
81
+
82
+ return end();
83
+ });
84
+ });
@@ -3,6 +3,7 @@ const idForTransaction = require('./id_for_transaction');
3
3
  const noLocktimeIdForTransaction = require('./no_locktime_id_for_transaction');
4
4
  const parseTransaction = require('./parse_transaction');
5
5
  const queryTransactions = require('./query_transactions');
6
+ const sizeOfTransaction = require('./size_of_transaction');
6
7
  const unsignedTxFromPsbt = require('./unsigned_tx_from_psbt');
7
8
 
8
9
  module.exports = {
@@ -11,5 +12,6 @@ module.exports = {
11
12
  noLocktimeIdForTransaction,
12
13
  parseTransaction,
13
14
  queryTransactions,
15
+ sizeOfTransaction,
14
16
  unsignedTxFromPsbt,
15
17
  };
@@ -0,0 +1,58 @@
1
+ const parseTransaction = require('./parse_transaction');
2
+
3
+ const byteCountLocktime = 4;
4
+ const byteCountMarkerFlag = 2;
5
+ const {ceil} = Math;
6
+ const hexAsBuffer = hex => Buffer.from(hex, 'hex');
7
+ const isHex = n => !!n && !(n.length % 2) && /^[0-9A-F]*$/i.test(n);
8
+ const noFlagAndMarker = 0;
9
+ const witnessScaling = 4;
10
+
11
+ /** Get the weight and virtual size of a hex-encoded transaction
12
+
13
+ {
14
+ transaction: <Hex Encoded Transaction String>
15
+ }
16
+
17
+ @throws
18
+ <Error>
19
+
20
+ @returns
21
+ {
22
+ vsize: <Transaction Virtual Size Number>
23
+ weight: <Transaction Weight Units Number>
24
+ }
25
+ */
26
+ module.exports = ({transaction}) => {
27
+ if (!isHex(transaction)) {
28
+ throw new Error('ExpectedHexEncodedTransactionToGetSize');
29
+ }
30
+
31
+ const buffer = hexAsBuffer(transaction);
32
+
33
+ const {bytes, inputs} = parseTransaction({buffer});
34
+
35
+ if (bytes.length !== buffer.length) {
36
+ throw new Error('UnexpectedDataAfterTransactionToGetSize');
37
+ }
38
+
39
+ // Parse through the outputs, stopping before witness data and nLockTime
40
+ const txBeforeWitness = parseTransaction({
41
+ buffer,
42
+ is_terminating_after_outputs: true,
43
+ });
44
+
45
+ // Before the witness, a SegWit transaction includes marker and flag bytes
46
+ const hasWitness = inputs.some(input => input.witness !== undefined);
47
+
48
+ const markerFlagSize = hasWitness ? byteCountMarkerFlag : noFlagAndMarker;
49
+ const sizeBeforeWitness = txBeforeWitness.bytes.length;
50
+
51
+ // Stripped size includes nLockTime but excludes marker, flag, and witnesses
52
+ const strippedSize = sizeBeforeWitness + byteCountLocktime - markerFlagSize;
53
+
54
+ // Base bytes weigh four units and witness bytes weigh one unit
55
+ const weight = strippedSize * witnessScaling + bytes.length - strippedSize;
56
+
57
+ return {weight, vsize: ceil(weight / witnessScaling)};
58
+ };