@opcat-labs/bip174 1.0.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 (96) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +109 -0
  3. package/package.json +66 -0
  4. package/src/cjs/lib/combiner/index.js +67 -0
  5. package/src/cjs/lib/converter/global/globalXpub.js +83 -0
  6. package/src/cjs/lib/converter/global/unsignedTx.js +10 -0
  7. package/src/cjs/lib/converter/index.js +72 -0
  8. package/src/cjs/lib/converter/input/finalScriptSig.js +36 -0
  9. package/src/cjs/lib/converter/input/finalScriptWitness.js +36 -0
  10. package/src/cjs/lib/converter/input/nonWitnessUtxo.js +35 -0
  11. package/src/cjs/lib/converter/input/opcatUtxo.js +74 -0
  12. package/src/cjs/lib/converter/input/partialSig.js +74 -0
  13. package/src/cjs/lib/converter/input/porCommitment.js +36 -0
  14. package/src/cjs/lib/converter/input/sighashType.js +38 -0
  15. package/src/cjs/lib/converter/input/tapKeySig.js +36 -0
  16. package/src/cjs/lib/converter/input/tapLeafScript.js +56 -0
  17. package/src/cjs/lib/converter/input/tapMerkleRoot.js +36 -0
  18. package/src/cjs/lib/converter/input/tapScriptSig.js +59 -0
  19. package/src/cjs/lib/converter/input/witnessUtxo.js +53 -0
  20. package/src/cjs/lib/converter/output/tapTree.js +60 -0
  21. package/src/cjs/lib/converter/shared/bip32Derivation.js +84 -0
  22. package/src/cjs/lib/converter/shared/checkPubkey.js +25 -0
  23. package/src/cjs/lib/converter/shared/redeemScript.js +41 -0
  24. package/src/cjs/lib/converter/shared/tapBip32Derivation.js +55 -0
  25. package/src/cjs/lib/converter/shared/tapInternalKey.js +41 -0
  26. package/src/cjs/lib/converter/shared/witnessScript.js +41 -0
  27. package/src/cjs/lib/converter/tools.js +45 -0
  28. package/src/cjs/lib/interfaces.js +2 -0
  29. package/src/cjs/lib/parser/fromBuffer.js +325 -0
  30. package/src/cjs/lib/parser/index.js +7 -0
  31. package/src/cjs/lib/parser/toBuffer.js +69 -0
  32. package/src/cjs/lib/psbt.js +145 -0
  33. package/src/cjs/lib/typeFields.js +64 -0
  34. package/src/cjs/lib/utils.js +141 -0
  35. package/src/esm/lib/combiner/index.d.ts +2 -0
  36. package/src/esm/lib/combiner/index.js +57 -0
  37. package/src/esm/lib/converter/global/globalXpub.d.ts +6 -0
  38. package/src/esm/lib/converter/global/globalXpub.js +70 -0
  39. package/src/esm/lib/converter/global/unsignedTx.d.ts +2 -0
  40. package/src/esm/lib/converter/global/unsignedTx.js +7 -0
  41. package/src/esm/lib/converter/index.d.ts +110 -0
  42. package/src/esm/lib/converter/index.js +61 -0
  43. package/src/esm/lib/converter/input/finalScriptSig.d.ts +6 -0
  44. package/src/esm/lib/converter/input/finalScriptSig.js +23 -0
  45. package/src/esm/lib/converter/input/finalScriptWitness.d.ts +6 -0
  46. package/src/esm/lib/converter/input/finalScriptWitness.js +23 -0
  47. package/src/esm/lib/converter/input/nonWitnessUtxo.d.ts +6 -0
  48. package/src/esm/lib/converter/input/nonWitnessUtxo.js +22 -0
  49. package/src/esm/lib/converter/input/opcatUtxo.d.ts +6 -0
  50. package/src/esm/lib/converter/input/opcatUtxo.js +61 -0
  51. package/src/esm/lib/converter/input/partialSig.d.ts +6 -0
  52. package/src/esm/lib/converter/input/partialSig.js +61 -0
  53. package/src/esm/lib/converter/input/porCommitment.d.ts +6 -0
  54. package/src/esm/lib/converter/input/porCommitment.js +23 -0
  55. package/src/esm/lib/converter/input/sighashType.d.ts +6 -0
  56. package/src/esm/lib/converter/input/sighashType.js +25 -0
  57. package/src/esm/lib/converter/input/tapKeySig.d.ts +6 -0
  58. package/src/esm/lib/converter/input/tapKeySig.js +23 -0
  59. package/src/esm/lib/converter/input/tapLeafScript.d.ts +6 -0
  60. package/src/esm/lib/converter/input/tapLeafScript.js +43 -0
  61. package/src/esm/lib/converter/input/tapMerkleRoot.d.ts +6 -0
  62. package/src/esm/lib/converter/input/tapMerkleRoot.js +23 -0
  63. package/src/esm/lib/converter/input/tapScriptSig.d.ts +6 -0
  64. package/src/esm/lib/converter/input/tapScriptSig.js +46 -0
  65. package/src/esm/lib/converter/input/witnessUtxo.d.ts +6 -0
  66. package/src/esm/lib/converter/input/witnessUtxo.js +40 -0
  67. package/src/esm/lib/converter/output/tapTree.d.ts +6 -0
  68. package/src/esm/lib/converter/output/tapTree.js +47 -0
  69. package/src/esm/lib/converter/shared/bip32Derivation.d.ts +8 -0
  70. package/src/esm/lib/converter/shared/bip32Derivation.js +74 -0
  71. package/src/esm/lib/converter/shared/checkPubkey.d.ts +2 -0
  72. package/src/esm/lib/converter/shared/checkPubkey.js +15 -0
  73. package/src/esm/lib/converter/shared/redeemScript.d.ts +8 -0
  74. package/src/esm/lib/converter/shared/redeemScript.js +31 -0
  75. package/src/esm/lib/converter/shared/tapBip32Derivation.d.ts +8 -0
  76. package/src/esm/lib/converter/shared/tapBip32Derivation.js +45 -0
  77. package/src/esm/lib/converter/shared/tapInternalKey.d.ts +8 -0
  78. package/src/esm/lib/converter/shared/tapInternalKey.js +31 -0
  79. package/src/esm/lib/converter/shared/witnessScript.d.ts +8 -0
  80. package/src/esm/lib/converter/shared/witnessScript.js +31 -0
  81. package/src/esm/lib/converter/tools.d.ts +5 -0
  82. package/src/esm/lib/converter/tools.js +33 -0
  83. package/src/esm/lib/interfaces.d.ts +120 -0
  84. package/src/esm/lib/interfaces.js +0 -0
  85. package/src/esm/lib/parser/fromBuffer.d.ts +11 -0
  86. package/src/esm/lib/parser/fromBuffer.js +313 -0
  87. package/src/esm/lib/parser/index.d.ts +8 -0
  88. package/src/esm/lib/parser/index.js +2 -0
  89. package/src/esm/lib/parser/toBuffer.d.ts +8 -0
  90. package/src/esm/lib/parser/toBuffer.js +58 -0
  91. package/src/esm/lib/psbt.d.ts +26 -0
  92. package/src/esm/lib/psbt.js +133 -0
  93. package/src/esm/lib/typeFields.d.ts +34 -0
  94. package/src/esm/lib/typeFields.js +62 -0
  95. package/src/esm/lib/utils.d.ts +13 -0
  96. package/src/esm/lib/utils.js +123 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 Jonathan Underwood and BitcoinJS team
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,109 @@
1
+ # bip174
2
+ [![Build Status](https://github.com/bitcoinjs/bip174/actions/workflows/main_ci.yml/badge.svg)](https://github.com/bitcoinjs/bip174/actions/workflows/main_ci.yml)
3
+ [![NPM](https://img.shields.io/npm/v/bip174.svg)](https://www.npmjs.org/package/bip174)
4
+
5
+ [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
6
+
7
+ A [BIP174](https://github.com/bitcoin/bips/blob/master/bip-0174.mediawiki) compatible partial Transaction encoding library.
8
+
9
+ ## Bitcoin users, use bitcoinjs-lib's Psbt class.
10
+
11
+ This library is separate as an attempt to separate Bitcoin specific logic from the encoding format.
12
+
13
+ I apologize if this library is hard to use. Removing Bitcoin specific logic from "Partially Signed BITCOIN Transaction" format was kind of hard.
14
+
15
+ ## Responsibilities Covered
16
+
17
+ 1. Creator: You can create a new psbt and add inputs and outputs, then add extra info to the inputs using each individual method.
18
+ (Note: Psbt.fromTransaction can create a PSBT from a serialized transaction, but all scriptSigs and witnessStacks must be empty,
19
+ and the transaction should not have the segwit marker and flagbyte.)
20
+ 2. Updater: You can add one of all types of the input and output information. Each function has strict type checking at runtime.
21
+ 3. Combiner: using Psbt.combine(...Psbt[]), it will do a dumb merge gathering all the new key value pairs from the subsequent PSBTs into itself. The psbt calling `combine` has highest priority.
22
+
23
+ ## Responsibilities NOT Covered
24
+
25
+ In order to keep this library as separate from Bitcoin logic as possible, This library will not implement the following responsibilities. But rather, down the road bitcoinjs-lib will adopt this class and extend it internally to allow for the following:
26
+
27
+ 1. Signer: This class has no way to sign. You may add a PartialSig object though.
28
+ 2. Extractor: This class has minimal knowledge of Bitcoin transactions, so creating a full transaction from a PSBT must be done with a bitcoin aware extended class.
29
+ 3. Input Finalizer: This class, again, has no knowledge of whether an input is finished.
30
+
31
+ ## Static methods and addInput / addOutput require an abstract Transaction object
32
+
33
+ * Static methods: You must pass a `TransactionFromBuffer` typed function. See `ts_src/lib/interfaces.ts` for info on the `Transaction` interface and the `TransactionFromBuffer` function.
34
+ * addInput/addOutput methods: The constructor takes a `Transaction` abstract interface that has an addInput/addOutput method which will be called.
35
+
36
+ ## Example
37
+ ```javascript
38
+ const { Psbt } = require('bip174')
39
+ const { PsbtTransaction , pTxFromBuffer } = require('./someImplementation')
40
+ // Psbt requires a Transaction interface to create an instance, as well as
41
+ // A function that turns a buffer into that interface. See Transaction and TransactionFromBuffer
42
+ // in ts_src/lib/interfaces.ts ...
43
+
44
+ // See tests/utils/txTools file for an example of a simple Bitcoin Transaction.
45
+ // Also see BitcoinJS-lib for an extended class that uses the Transaction class internally.
46
+ // Anyone using this library for Bitcoin specifically should use bitcoinjs-lib
47
+
48
+ // Your PsbtTransaction will have a toBuffer function to allow for serialization
49
+ const tx = pTxFromBuffer(someTransactionBuffer);
50
+ const psbt = new Psbt(tx)
51
+
52
+ // OR
53
+
54
+ // This will parse the PSBT, and use the function you pass to parse the Transaction part
55
+ // the function should throw if the scriptSig section is not empty
56
+ const psbt = Psbt.fromBuffer(somePsbtBuffer, pTxFromBuffer)
57
+
58
+ psbt.addInput({
59
+ hash: '865dce988413971fd812d0e81a3395ed916a87ea533e1a16c0f4e15df96fa7d4',
60
+ index: 3,
61
+ })
62
+ psbt.addInput({
63
+ hash: 'ff5dce988413971fd812d0e81a3395ed916a87ea533e1a16c0f4e15df96fa7d4',
64
+ index: 1,
65
+ })
66
+ psbt.addOutput({
67
+ script: Buffer.from(
68
+ 'a914e18870f2c297fbfca54c5c6f645c7745a5b66eda87',
69
+ 'hex',
70
+ ),
71
+ value: 1234567890,
72
+ })
73
+ psbt.addOutput({
74
+ script: Buffer.from(
75
+ 'a914e18870f2c297fbfca54c5c6f645c7745a5b66eda87',
76
+ 'hex',
77
+ ),
78
+ value: 987654321,
79
+ })
80
+ psbt.addRedeemScriptToInput(0, Buffer.from(
81
+ '00208c2353173743b595dfb4a07b72ba8e42e3797da74e87fe7d9d7497e3b2028903',
82
+ 'hex',
83
+ ))
84
+ psbt.addWitnessScriptToInput(0, Buffer.from(
85
+ '522103089dc10c7ac6db54f91329af617333db388cead0c231f723379d1b9903' +
86
+ '0b02dc21023add904f3d6dcf59ddb906b0dee23529b7ffb9ed50e5e861519268' +
87
+ '60221f0e7352ae',
88
+ 'hex',
89
+ ))
90
+ psbt.addBip32DerivationToInput(0, {
91
+ masterFingerprint: Buffer.from('d90c6a4f', 'hex'),
92
+ pubkey: Buffer.from(
93
+ '023add904f3d6dcf59ddb906b0dee23529b7ffb9ed50e5e86151926860221f0e73',
94
+ 'hex',
95
+ ),
96
+ path: "m/0'/0'/3'",
97
+ })
98
+ psbt.addBip32DerivationToInput(0, {
99
+ masterFingerprint: Buffer.from('d90c6a4f', 'hex'),
100
+ pubkey: Buffer.from(
101
+ '03089dc10c7ac6db54f91329af617333db388cead0c231f723379d1b99030b02dc',
102
+ 'hex',
103
+ ),
104
+ path: "m/0'/0'/2'",
105
+ })
106
+ const b64 = psbt.toBase64();
107
+ ```
108
+
109
+ ## LICENSE [MIT](LICENSE)
package/package.json ADDED
@@ -0,0 +1,66 @@
1
+ {
2
+ "name": "@opcat-labs/bip174",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "main": "src/cjs/lib/psbt.cjs",
6
+ "module": "src/esm/lib/psbt.js",
7
+ "types": "src/esm/lib/psbt.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./src/esm/lib/psbt.d.ts",
11
+ "import": "./src/esm/lib/psbt.js",
12
+ "require": "./src/cjs/lib/psbt.cjs"
13
+ }
14
+ },
15
+ "type": "module",
16
+ "files": [
17
+ "src/cjs/lib",
18
+ "src/esm/lib"
19
+ ],
20
+ "engines": {
21
+ "node": ">=18.0.0"
22
+ },
23
+ "scripts": {
24
+ "build": "npm run clean && tsc -p ./tsconfig.json && tsc -p ./tsconfig.cjs.json",
25
+ "checkHybrid": "chmod +x test.cjs && node test.cjs",
26
+ "clean": "rimraf src types",
27
+ "coverage": "c8 --check-coverage --include='src/esm/lib/**/*.js' --reporter=lcov --reporter=text --branches 90 --functions 90 --lines 90 npm run unit",
28
+ "flb": "npm run format && npm run lint && npm run build",
29
+ "format": "npm run prettier -- --write",
30
+ "formatjs": "npm run prettierjs -- --write > /dev/null 2>&1",
31
+ "format:ci": "npm run prettier -- --check && npm run prettierjs -- --check",
32
+ "gitdiff:ci": "npm run build && git diff --exit-code",
33
+ "lint": "tslint -p tsconfig.json -c tslint.json",
34
+ "postbuild": "node fixup.cjs",
35
+ "prettier": "prettier 'ts_src/**/*.ts' --ignore-path ./.prettierignore",
36
+ "prettierjs": "prettier 'src/**/*.js' --ignore-path ./.prettierignore",
37
+ "test": "npm run format:ci && npm run lint && npm run build && npm run coverage",
38
+ "unit": "tape src/esm/tests/*.js src/esm/tests/fromBIP/*.js"
39
+ },
40
+ "repository": {
41
+ "type": "git",
42
+ "url": "git+https://github.com/bitcoinjs/bip174.git"
43
+ },
44
+ "dependencies": {
45
+ "uint8array-tools": "^0.0.9",
46
+ "varuint-bitcoin": "^2.0.0"
47
+ },
48
+ "devDependencies": {
49
+ "@types/node": "12.0.8",
50
+ "@types/tape": "4.2.33",
51
+ "bitcoinjs-lib": "^6.1.6",
52
+ "c8": "^10.1.2",
53
+ "prettier": "^1.18.2",
54
+ "rimraf": "^2.6.3",
55
+ "tape": "^5.3.0",
56
+ "tslint": "5.17.0",
57
+ "typescript": "3.5.2"
58
+ },
59
+ "author": "Jonathan Underwood",
60
+ "license": "MIT",
61
+ "bugs": {
62
+ "url": "https://github.com/bitcoinjs/bip174/issues"
63
+ },
64
+ "homepage": "https://github.com/bitcoinjs/bip174#readme",
65
+ "packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
66
+ }
@@ -0,0 +1,67 @@
1
+ "use strict";
2
+ var __importStar = (this && this.__importStar) || function (mod) {
3
+ if (mod && mod.__esModule) return mod;
4
+ var result = {};
5
+ if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
6
+ result["default"] = mod;
7
+ return result;
8
+ };
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ const index_js_1 = require("../parser/index.js");
11
+ const tools = __importStar(require("uint8array-tools"));
12
+ function combine(psbts) {
13
+ const self = psbts[0];
14
+ const selfKeyVals = index_js_1.psbtToKeyVals(self);
15
+ const others = psbts.slice(1);
16
+ if (others.length === 0)
17
+ throw new Error('Combine: Nothing to combine');
18
+ const selfTx = getTx(self);
19
+ if (selfTx === undefined) {
20
+ throw new Error('Combine: Self missing transaction');
21
+ }
22
+ const selfGlobalSet = getKeySet(selfKeyVals.globalKeyVals);
23
+ const selfInputSets = selfKeyVals.inputKeyVals.map(getKeySet);
24
+ const selfOutputSets = selfKeyVals.outputKeyVals.map(getKeySet);
25
+ for (const other of others) {
26
+ const otherTx = getTx(other);
27
+ if (otherTx === undefined ||
28
+ tools.compare(otherTx.toBuffer(), selfTx.toBuffer()) !== 0) {
29
+ throw new Error('Combine: One of the Psbts does not have the same transaction.');
30
+ }
31
+ const otherKeyVals = index_js_1.psbtToKeyVals(other);
32
+ const otherGlobalSet = getKeySet(otherKeyVals.globalKeyVals);
33
+ otherGlobalSet.forEach(keyPusher(selfGlobalSet, selfKeyVals.globalKeyVals, otherKeyVals.globalKeyVals));
34
+ const otherInputSets = otherKeyVals.inputKeyVals.map(getKeySet);
35
+ otherInputSets.forEach((inputSet, idx) => inputSet.forEach(keyPusher(selfInputSets[idx], selfKeyVals.inputKeyVals[idx], otherKeyVals.inputKeyVals[idx])));
36
+ const otherOutputSets = otherKeyVals.outputKeyVals.map(getKeySet);
37
+ otherOutputSets.forEach((outputSet, idx) => outputSet.forEach(keyPusher(selfOutputSets[idx], selfKeyVals.outputKeyVals[idx], otherKeyVals.outputKeyVals[idx])));
38
+ }
39
+ return index_js_1.psbtFromKeyVals(selfTx, {
40
+ globalMapKeyVals: selfKeyVals.globalKeyVals,
41
+ inputKeyVals: selfKeyVals.inputKeyVals,
42
+ outputKeyVals: selfKeyVals.outputKeyVals,
43
+ });
44
+ }
45
+ exports.combine = combine;
46
+ function keyPusher(selfSet, selfKeyVals, otherKeyVals) {
47
+ return (key) => {
48
+ if (selfSet.has(key))
49
+ return;
50
+ const newKv = otherKeyVals.filter(kv => tools.toHex(kv.key) === key)[0];
51
+ selfKeyVals.push(newKv);
52
+ selfSet.add(key);
53
+ };
54
+ }
55
+ function getTx(psbt) {
56
+ return psbt.globalMap.unsignedTx;
57
+ }
58
+ function getKeySet(keyVals) {
59
+ const set = new Set();
60
+ keyVals.forEach(keyVal => {
61
+ const hex = tools.toHex(keyVal.key);
62
+ if (set.has(hex))
63
+ throw new Error('Combine: KeyValue Map keys should be unique');
64
+ set.add(hex);
65
+ });
66
+ return set;
67
+ }
@@ -0,0 +1,83 @@
1
+ "use strict";
2
+ var __importStar = (this && this.__importStar) || function (mod) {
3
+ if (mod && mod.__esModule) return mod;
4
+ var result = {};
5
+ if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
6
+ result["default"] = mod;
7
+ return result;
8
+ };
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ const typeFields_js_1 = require("../../typeFields.js");
11
+ const tools = __importStar(require("uint8array-tools"));
12
+ const range = (n) => [...Array(n).keys()];
13
+ function decode(keyVal) {
14
+ if (keyVal.key[0] !== typeFields_js_1.GlobalTypes.GLOBAL_XPUB) {
15
+ throw new Error('Decode Error: could not decode globalXpub with key 0x' +
16
+ tools.toHex(keyVal.key));
17
+ }
18
+ if (keyVal.key.length !== 79 || ![2, 3].includes(keyVal.key[46])) {
19
+ throw new Error('Decode Error: globalXpub has invalid extended pubkey in key 0x' +
20
+ tools.toHex(keyVal.key));
21
+ }
22
+ if ((keyVal.value.length / 4) % 1 !== 0) {
23
+ throw new Error('Decode Error: Global GLOBAL_XPUB value length should be multiple of 4');
24
+ }
25
+ const extendedPubkey = keyVal.key.slice(1);
26
+ const data = {
27
+ masterFingerprint: keyVal.value.slice(0, 4),
28
+ extendedPubkey,
29
+ path: 'm',
30
+ };
31
+ for (const i of range(keyVal.value.length / 4 - 1)) {
32
+ const val = tools.readUInt32(keyVal.value, i * 4 + 4, 'LE');
33
+ const isHard = !!(val & 0x80000000);
34
+ const idx = val & 0x7fffffff;
35
+ data.path += '/' + idx.toString(10) + (isHard ? "'" : '');
36
+ }
37
+ return data;
38
+ }
39
+ exports.decode = decode;
40
+ function encode(data) {
41
+ const head = new Uint8Array([typeFields_js_1.GlobalTypes.GLOBAL_XPUB]);
42
+ const key = tools.concat([head, data.extendedPubkey]);
43
+ const splitPath = data.path.split('/');
44
+ const value = new Uint8Array(splitPath.length * 4);
45
+ value.set(data.masterFingerprint, 0);
46
+ let offset = 4;
47
+ splitPath.slice(1).forEach(level => {
48
+ const isHard = level.slice(-1) === "'";
49
+ let num = 0x7fffffff & parseInt(isHard ? level.slice(0, -1) : level, 10);
50
+ if (isHard)
51
+ num += 0x80000000;
52
+ tools.writeUInt32(value, offset, num, 'LE');
53
+ offset += 4;
54
+ });
55
+ return {
56
+ key,
57
+ value,
58
+ };
59
+ }
60
+ exports.encode = encode;
61
+ exports.expected = '{ masterFingerprint: Uint8Array; extendedPubkey: Uint8Array; path: string; }';
62
+ function check(data) {
63
+ const epk = data.extendedPubkey;
64
+ const mfp = data.masterFingerprint;
65
+ const p = data.path;
66
+ return (epk instanceof Uint8Array &&
67
+ epk.length === 78 &&
68
+ [2, 3].indexOf(epk[45]) > -1 &&
69
+ mfp instanceof Uint8Array &&
70
+ mfp.length === 4 &&
71
+ typeof p === 'string' &&
72
+ !!p.match(/^m(\/\d+'?)*$/));
73
+ }
74
+ exports.check = check;
75
+ function canAddToArray(array, item, dupeSet) {
76
+ const dupeString = tools.toHex(item.extendedPubkey);
77
+ if (dupeSet.has(dupeString))
78
+ return false;
79
+ dupeSet.add(dupeString);
80
+ return (array.filter(v => tools.compare(v.extendedPubkey, item.extendedPubkey))
81
+ .length === 0);
82
+ }
83
+ exports.canAddToArray = canAddToArray;
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const typeFields_js_1 = require("../../typeFields.js");
4
+ function encode(data) {
5
+ return {
6
+ key: new Uint8Array([typeFields_js_1.GlobalTypes.UNSIGNED_TX]),
7
+ value: data.toBuffer(),
8
+ };
9
+ }
10
+ exports.encode = encode;
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+ var __importStar = (this && this.__importStar) || function (mod) {
3
+ if (mod && mod.__esModule) return mod;
4
+ var result = {};
5
+ if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
6
+ result["default"] = mod;
7
+ return result;
8
+ };
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ const typeFields_js_1 = require("../typeFields.js");
11
+ const globalXpub = __importStar(require("./global/globalXpub.js"));
12
+ const unsignedTx = __importStar(require("./global/unsignedTx.js"));
13
+ const finalScriptSig = __importStar(require("./input/finalScriptSig.js"));
14
+ const finalScriptWitness = __importStar(require("./input/finalScriptWitness.js"));
15
+ const nonWitnessUtxo = __importStar(require("./input/nonWitnessUtxo.js"));
16
+ const partialSig = __importStar(require("./input/partialSig.js"));
17
+ const porCommitment = __importStar(require("./input/porCommitment.js"));
18
+ const sighashType = __importStar(require("./input/sighashType.js"));
19
+ const tapKeySig = __importStar(require("./input/tapKeySig.js"));
20
+ const tapLeafScript = __importStar(require("./input/tapLeafScript.js"));
21
+ const tapMerkleRoot = __importStar(require("./input/tapMerkleRoot.js"));
22
+ const tapScriptSig = __importStar(require("./input/tapScriptSig.js"));
23
+ const witnessUtxo = __importStar(require("./input/witnessUtxo.js"));
24
+ const opcatUtxo = __importStar(require("./input/opcatUtxo.js"));
25
+ const tapTree = __importStar(require("./output/tapTree.js"));
26
+ const bip32Derivation = __importStar(require("./shared/bip32Derivation.js"));
27
+ const checkPubkey = __importStar(require("./shared/checkPubkey.js"));
28
+ const redeemScript = __importStar(require("./shared/redeemScript.js"));
29
+ const tapBip32Derivation = __importStar(require("./shared/tapBip32Derivation.js"));
30
+ const tapInternalKey = __importStar(require("./shared/tapInternalKey.js"));
31
+ const witnessScript = __importStar(require("./shared/witnessScript.js"));
32
+ const globals = {
33
+ unsignedTx,
34
+ globalXpub,
35
+ // pass an Array of key bytes that require pubkey beside the key
36
+ checkPubkey: checkPubkey.makeChecker([]),
37
+ };
38
+ exports.globals = globals;
39
+ const inputs = {
40
+ nonWitnessUtxo,
41
+ partialSig,
42
+ sighashType,
43
+ finalScriptSig,
44
+ finalScriptWitness,
45
+ porCommitment,
46
+ witnessUtxo,
47
+ opcatUtxo,
48
+ bip32Derivation: bip32Derivation.makeConverter(typeFields_js_1.InputTypes.BIP32_DERIVATION),
49
+ redeemScript: redeemScript.makeConverter(typeFields_js_1.InputTypes.REDEEM_SCRIPT),
50
+ witnessScript: witnessScript.makeConverter(typeFields_js_1.InputTypes.WITNESS_SCRIPT),
51
+ checkPubkey: checkPubkey.makeChecker([
52
+ typeFields_js_1.InputTypes.PARTIAL_SIG,
53
+ typeFields_js_1.InputTypes.BIP32_DERIVATION,
54
+ ]),
55
+ tapKeySig,
56
+ tapScriptSig,
57
+ tapLeafScript,
58
+ tapBip32Derivation: tapBip32Derivation.makeConverter(typeFields_js_1.InputTypes.TAP_BIP32_DERIVATION),
59
+ tapInternalKey: tapInternalKey.makeConverter(typeFields_js_1.InputTypes.TAP_INTERNAL_KEY),
60
+ tapMerkleRoot,
61
+ };
62
+ exports.inputs = inputs;
63
+ const outputs = {
64
+ bip32Derivation: bip32Derivation.makeConverter(typeFields_js_1.OutputTypes.BIP32_DERIVATION),
65
+ redeemScript: redeemScript.makeConverter(typeFields_js_1.OutputTypes.REDEEM_SCRIPT),
66
+ witnessScript: witnessScript.makeConverter(typeFields_js_1.OutputTypes.WITNESS_SCRIPT),
67
+ checkPubkey: checkPubkey.makeChecker([typeFields_js_1.OutputTypes.BIP32_DERIVATION]),
68
+ tapBip32Derivation: tapBip32Derivation.makeConverter(typeFields_js_1.OutputTypes.TAP_BIP32_DERIVATION),
69
+ tapTree,
70
+ tapInternalKey: tapInternalKey.makeConverter(typeFields_js_1.OutputTypes.TAP_INTERNAL_KEY),
71
+ };
72
+ exports.outputs = outputs;
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ var __importStar = (this && this.__importStar) || function (mod) {
3
+ if (mod && mod.__esModule) return mod;
4
+ var result = {};
5
+ if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
6
+ result["default"] = mod;
7
+ return result;
8
+ };
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ const typeFields_js_1 = require("../../typeFields.js");
11
+ const tools = __importStar(require("uint8array-tools"));
12
+ function decode(keyVal) {
13
+ if (keyVal.key[0] !== typeFields_js_1.InputTypes.FINAL_SCRIPTSIG) {
14
+ throw new Error('Decode Error: could not decode finalScriptSig with key 0x' +
15
+ tools.toHex(keyVal.key));
16
+ }
17
+ return keyVal.value;
18
+ }
19
+ exports.decode = decode;
20
+ function encode(data) {
21
+ const key = new Uint8Array([typeFields_js_1.InputTypes.FINAL_SCRIPTSIG]);
22
+ return {
23
+ key,
24
+ value: data,
25
+ };
26
+ }
27
+ exports.encode = encode;
28
+ exports.expected = 'Uint8Array';
29
+ function check(data) {
30
+ return data instanceof Uint8Array;
31
+ }
32
+ exports.check = check;
33
+ function canAdd(currentData, newData) {
34
+ return !!currentData && !!newData && currentData.finalScriptSig === undefined;
35
+ }
36
+ exports.canAdd = canAdd;
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ var __importStar = (this && this.__importStar) || function (mod) {
3
+ if (mod && mod.__esModule) return mod;
4
+ var result = {};
5
+ if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
6
+ result["default"] = mod;
7
+ return result;
8
+ };
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ const typeFields_js_1 = require("../../typeFields.js");
11
+ const tools = __importStar(require("uint8array-tools"));
12
+ function decode(keyVal) {
13
+ if (keyVal.key[0] !== typeFields_js_1.InputTypes.FINAL_SCRIPTWITNESS) {
14
+ throw new Error('Decode Error: could not decode finalScriptWitness with key 0x' +
15
+ tools.toHex(keyVal.key));
16
+ }
17
+ return keyVal.value;
18
+ }
19
+ exports.decode = decode;
20
+ function encode(data) {
21
+ const key = new Uint8Array([typeFields_js_1.InputTypes.FINAL_SCRIPTWITNESS]);
22
+ return {
23
+ key,
24
+ value: data,
25
+ };
26
+ }
27
+ exports.encode = encode;
28
+ exports.expected = 'Uint8Array';
29
+ function check(data) {
30
+ return data instanceof Uint8Array;
31
+ }
32
+ exports.check = check;
33
+ function canAdd(currentData, newData) {
34
+ return (!!currentData && !!newData && currentData.finalScriptWitness === undefined);
35
+ }
36
+ exports.canAdd = canAdd;
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ var __importStar = (this && this.__importStar) || function (mod) {
3
+ if (mod && mod.__esModule) return mod;
4
+ var result = {};
5
+ if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
6
+ result["default"] = mod;
7
+ return result;
8
+ };
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ const typeFields_js_1 = require("../../typeFields.js");
11
+ const tools = __importStar(require("uint8array-tools"));
12
+ function decode(keyVal) {
13
+ if (keyVal.key[0] !== typeFields_js_1.InputTypes.NON_WITNESS_UTXO) {
14
+ throw new Error('Decode Error: could not decode nonWitnessUtxo with key 0x' +
15
+ tools.toHex(keyVal.key));
16
+ }
17
+ return keyVal.value;
18
+ }
19
+ exports.decode = decode;
20
+ function encode(data) {
21
+ return {
22
+ key: new Uint8Array([typeFields_js_1.InputTypes.NON_WITNESS_UTXO]),
23
+ value: data,
24
+ };
25
+ }
26
+ exports.encode = encode;
27
+ exports.expected = 'Uint8Array';
28
+ function check(data) {
29
+ return data instanceof Uint8Array;
30
+ }
31
+ exports.check = check;
32
+ function canAdd(currentData, newData) {
33
+ return !!currentData && !!newData && currentData.nonWitnessUtxo === undefined;
34
+ }
35
+ exports.canAdd = canAdd;
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+ var __importStar = (this && this.__importStar) || function (mod) {
3
+ if (mod && mod.__esModule) return mod;
4
+ var result = {};
5
+ if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
6
+ result["default"] = mod;
7
+ return result;
8
+ };
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ const typeFields_js_1 = require("../../typeFields.js");
11
+ const tools = __importStar(require("uint8array-tools"));
12
+ const varuint = __importStar(require("varuint-bitcoin"));
13
+ function decode(keyVal) {
14
+ if (keyVal.key[0] !== typeFields_js_1.InputTypes.OPCAT_UTXO) {
15
+ throw new Error('Decode Error: could not decode opcatUtxo with key 0x' +
16
+ tools.toHex(keyVal.key));
17
+ }
18
+ let offset = 0;
19
+ const value = tools.readInt64(keyVal.value, offset, 'LE');
20
+ offset += 8;
21
+ const { numberValue: scriptLen, bytes: scriptLenBytes } = varuint.decode(keyVal.value, offset);
22
+ if (scriptLen == null)
23
+ throw new Error('Decode Error: scriptLen is null');
24
+ offset += scriptLenBytes;
25
+ const script = keyVal.value.slice(offset, offset + scriptLen);
26
+ offset += scriptLen;
27
+ const { numberValue: dataLen, bytes: dataLenBytes } = varuint.decode(keyVal.value, offset);
28
+ if (dataLen == null)
29
+ throw new Error('Decode Error: dataLen is null');
30
+ offset += dataLenBytes;
31
+ const data = keyVal.value.slice(offset, offset + dataLen);
32
+ offset += dataLen;
33
+ return {
34
+ script,
35
+ data,
36
+ value,
37
+ };
38
+ }
39
+ exports.decode = decode;
40
+ function encode(data) {
41
+ const { script, data: opcatData, value } = data;
42
+ const scriptLen = script.length;
43
+ const dataLen = opcatData.length;
44
+ const scriptLenVar = varuint.encodingLength(scriptLen);
45
+ const dataLenVar = varuint.encodingLength(dataLen);
46
+ const totalLen = 8 + scriptLenVar + scriptLen + dataLenVar + dataLen;
47
+ const result = new Uint8Array(totalLen);
48
+ let offset = 0;
49
+ tools.writeInt64(result, offset, BigInt(value), 'LE');
50
+ offset += 8;
51
+ varuint.encode(scriptLen, result, offset);
52
+ offset += scriptLenVar;
53
+ result.set(script, offset);
54
+ offset += scriptLen;
55
+ varuint.encode(dataLen, result, offset);
56
+ offset += dataLenVar;
57
+ result.set(opcatData, offset);
58
+ return {
59
+ key: Uint8Array.from([typeFields_js_1.InputTypes.OPCAT_UTXO]),
60
+ value: result,
61
+ };
62
+ }
63
+ exports.encode = encode;
64
+ exports.expected = '{ script: Uint8Array; data: Uint8Array; value: bigint; }';
65
+ function check(data) {
66
+ return (data.script instanceof Uint8Array &&
67
+ data.data instanceof Uint8Array &&
68
+ typeof data.value === 'bigint');
69
+ }
70
+ exports.check = check;
71
+ function canAdd(currentData, newData) {
72
+ return !!currentData && !!newData && currentData.opcatUtxo === undefined;
73
+ }
74
+ exports.canAdd = canAdd;