@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
@@ -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
+ function decode(keyVal) {
13
+ if (keyVal.key[0] !== typeFields_js_1.InputTypes.PARTIAL_SIG) {
14
+ throw new Error('Decode Error: could not decode partialSig with key 0x' +
15
+ tools.toHex(keyVal.key));
16
+ }
17
+ if (!(keyVal.key.length === 34 || keyVal.key.length === 66) ||
18
+ ![2, 3, 4].includes(keyVal.key[1])) {
19
+ throw new Error('Decode Error: partialSig has invalid pubkey in key 0x' +
20
+ tools.toHex(keyVal.key));
21
+ }
22
+ const pubkey = keyVal.key.slice(1);
23
+ return {
24
+ pubkey,
25
+ signature: keyVal.value,
26
+ };
27
+ }
28
+ exports.decode = decode;
29
+ function encode(pSig) {
30
+ const head = new Uint8Array([typeFields_js_1.InputTypes.PARTIAL_SIG]);
31
+ return {
32
+ key: tools.concat([head, pSig.pubkey]),
33
+ value: pSig.signature,
34
+ };
35
+ }
36
+ exports.encode = encode;
37
+ exports.expected = '{ pubkey: Uint8Array; signature: Uint8Array; }';
38
+ function check(data) {
39
+ return (data.pubkey instanceof Uint8Array &&
40
+ data.signature instanceof Uint8Array &&
41
+ [33, 65].includes(data.pubkey.length) &&
42
+ [2, 3, 4].includes(data.pubkey[0]) &&
43
+ isDerSigWithSighash(data.signature));
44
+ }
45
+ exports.check = check;
46
+ function isDerSigWithSighash(buf) {
47
+ if (!(buf instanceof Uint8Array) || buf.length < 9)
48
+ return false;
49
+ if (buf[0] !== 0x30)
50
+ return false;
51
+ if (buf.length !== buf[1] + 3)
52
+ return false;
53
+ if (buf[2] !== 0x02)
54
+ return false;
55
+ const rLen = buf[3];
56
+ if (rLen > 33 || rLen < 1)
57
+ return false;
58
+ if (buf[3 + rLen + 1] !== 0x02)
59
+ return false;
60
+ const sLen = buf[3 + rLen + 2];
61
+ if (sLen > 33 || sLen < 1)
62
+ return false;
63
+ if (buf.length !== 3 + rLen + 2 + sLen + 2)
64
+ return false;
65
+ return true;
66
+ }
67
+ function canAddToArray(array, item, dupeSet) {
68
+ const dupeString = tools.toHex(item.pubkey);
69
+ if (dupeSet.has(dupeString))
70
+ return false;
71
+ dupeSet.add(dupeString);
72
+ return (array.filter(v => tools.compare(v.pubkey, item.pubkey) === 0).length === 0);
73
+ }
74
+ exports.canAddToArray = canAddToArray;
@@ -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.POR_COMMITMENT) {
14
+ throw new Error('Decode Error: could not decode porCommitment with key 0x' +
15
+ tools.toHex(keyVal.key));
16
+ }
17
+ return tools.toUtf8(keyVal.value);
18
+ }
19
+ exports.decode = decode;
20
+ function encode(data) {
21
+ const key = new Uint8Array([typeFields_js_1.InputTypes.POR_COMMITMENT]);
22
+ return {
23
+ key,
24
+ value: tools.fromUtf8(data),
25
+ };
26
+ }
27
+ exports.encode = encode;
28
+ exports.expected = 'string';
29
+ function check(data) {
30
+ return typeof data === 'string';
31
+ }
32
+ exports.check = check;
33
+ function canAdd(currentData, newData) {
34
+ return !!currentData && !!newData && currentData.porCommitment === undefined;
35
+ }
36
+ exports.canAdd = canAdd;
@@ -0,0 +1,38 @@
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.SIGHASH_TYPE) {
14
+ throw new Error('Decode Error: could not decode sighashType with key 0x' +
15
+ tools.toHex(keyVal.key));
16
+ }
17
+ return Number(tools.readUInt32(keyVal.value, 0, 'LE'));
18
+ }
19
+ exports.decode = decode;
20
+ function encode(data) {
21
+ const key = Uint8Array.from([typeFields_js_1.InputTypes.SIGHASH_TYPE]);
22
+ const value = new Uint8Array(4);
23
+ tools.writeUInt32(value, 0, data, 'LE');
24
+ return {
25
+ key,
26
+ value,
27
+ };
28
+ }
29
+ exports.encode = encode;
30
+ exports.expected = 'number';
31
+ function check(data) {
32
+ return typeof data === 'number';
33
+ }
34
+ exports.check = check;
35
+ function canAdd(currentData, newData) {
36
+ return !!currentData && !!newData && currentData.sighashType === undefined;
37
+ }
38
+ 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.TAP_KEY_SIG || keyVal.key.length !== 1) {
14
+ throw new Error('Decode Error: could not decode tapKeySig with key 0x' +
15
+ tools.toHex(keyVal.key));
16
+ }
17
+ if (!check(keyVal.value)) {
18
+ throw new Error('Decode Error: tapKeySig not a valid 64-65-byte BIP340 signature');
19
+ }
20
+ return keyVal.value;
21
+ }
22
+ exports.decode = decode;
23
+ function encode(value) {
24
+ const key = Uint8Array.from([typeFields_js_1.InputTypes.TAP_KEY_SIG]);
25
+ return { key, value };
26
+ }
27
+ exports.encode = encode;
28
+ exports.expected = 'Uint8Array';
29
+ function check(data) {
30
+ return (data instanceof Uint8Array && (data.length === 64 || data.length === 65));
31
+ }
32
+ exports.check = check;
33
+ function canAdd(currentData, newData) {
34
+ return !!currentData && !!newData && currentData.tapKeySig === undefined;
35
+ }
36
+ exports.canAdd = canAdd;
@@ -0,0 +1,56 @@
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.TAP_LEAF_SCRIPT) {
14
+ throw new Error('Decode Error: could not decode tapLeafScript with key 0x' +
15
+ tools.toHex(keyVal.key));
16
+ }
17
+ if ((keyVal.key.length - 2) % 32 !== 0) {
18
+ throw new Error('Decode Error: tapLeafScript has invalid control block in key 0x' +
19
+ tools.toHex(keyVal.key));
20
+ }
21
+ const leafVersion = keyVal.value[keyVal.value.length - 1];
22
+ if ((keyVal.key[1] & 0xfe) !== leafVersion) {
23
+ throw new Error('Decode Error: tapLeafScript bad leaf version in key 0x' +
24
+ tools.toHex(keyVal.key));
25
+ }
26
+ const script = keyVal.value.slice(0, -1);
27
+ const controlBlock = keyVal.key.slice(1);
28
+ return { controlBlock, script, leafVersion };
29
+ }
30
+ exports.decode = decode;
31
+ function encode(tScript) {
32
+ const head = Uint8Array.from([typeFields_js_1.InputTypes.TAP_LEAF_SCRIPT]);
33
+ const verBuf = Uint8Array.from([tScript.leafVersion]);
34
+ return {
35
+ key: tools.concat([head, tScript.controlBlock]),
36
+ value: tools.concat([tScript.script, verBuf]),
37
+ };
38
+ }
39
+ exports.encode = encode;
40
+ exports.expected = '{ controlBlock: Uint8Array; leafVersion: number, script: Uint8Array; }';
41
+ function check(data) {
42
+ return (data.controlBlock instanceof Uint8Array &&
43
+ (data.controlBlock.length - 1) % 32 === 0 &&
44
+ (data.controlBlock[0] & 0xfe) === data.leafVersion &&
45
+ data.script instanceof Uint8Array);
46
+ }
47
+ exports.check = check;
48
+ function canAddToArray(array, item, dupeSet) {
49
+ const dupeString = tools.toHex(item.controlBlock);
50
+ if (dupeSet.has(dupeString))
51
+ return false;
52
+ dupeSet.add(dupeString);
53
+ return (array.filter(v => tools.compare(v.controlBlock, item.controlBlock) === 0)
54
+ .length === 0);
55
+ }
56
+ exports.canAddToArray = canAddToArray;
@@ -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.TAP_MERKLE_ROOT || keyVal.key.length !== 1) {
14
+ throw new Error('Decode Error: could not decode tapMerkleRoot with key 0x' +
15
+ tools.toHex(keyVal.key));
16
+ }
17
+ if (!check(keyVal.value)) {
18
+ throw new Error('Decode Error: tapMerkleRoot not a 32-byte hash');
19
+ }
20
+ return keyVal.value;
21
+ }
22
+ exports.decode = decode;
23
+ function encode(value) {
24
+ const key = Uint8Array.from([typeFields_js_1.InputTypes.TAP_MERKLE_ROOT]);
25
+ return { key, value };
26
+ }
27
+ exports.encode = encode;
28
+ exports.expected = 'Uint8Array';
29
+ function check(data) {
30
+ return data instanceof Uint8Array && data.length === 32;
31
+ }
32
+ exports.check = check;
33
+ function canAdd(currentData, newData) {
34
+ return !!currentData && !!newData && currentData.tapMerkleRoot === undefined;
35
+ }
36
+ exports.canAdd = canAdd;
@@ -0,0 +1,59 @@
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.TAP_SCRIPT_SIG) {
14
+ throw new Error('Decode Error: could not decode tapScriptSig with key 0x' +
15
+ tools.toHex(keyVal.key));
16
+ }
17
+ if (keyVal.key.length !== 65) {
18
+ throw new Error('Decode Error: tapScriptSig has invalid key 0x' + tools.toHex(keyVal.key));
19
+ }
20
+ if (keyVal.value.length !== 64 && keyVal.value.length !== 65) {
21
+ throw new Error('Decode Error: tapScriptSig has invalid signature in key 0x' +
22
+ tools.toHex(keyVal.key));
23
+ }
24
+ const pubkey = keyVal.key.slice(1, 33);
25
+ const leafHash = keyVal.key.slice(33);
26
+ return {
27
+ pubkey,
28
+ leafHash,
29
+ signature: keyVal.value,
30
+ };
31
+ }
32
+ exports.decode = decode;
33
+ function encode(tSig) {
34
+ const head = Uint8Array.from([typeFields_js_1.InputTypes.TAP_SCRIPT_SIG]);
35
+ return {
36
+ key: tools.concat([head, tSig.pubkey, tSig.leafHash]),
37
+ value: tSig.signature,
38
+ };
39
+ }
40
+ exports.encode = encode;
41
+ exports.expected = '{ pubkey: Uint8Array; leafHash: Uint8Array; signature: Uint8Array; }';
42
+ function check(data) {
43
+ return (data.pubkey instanceof Uint8Array &&
44
+ data.leafHash instanceof Uint8Array &&
45
+ data.signature instanceof Uint8Array &&
46
+ data.pubkey.length === 32 &&
47
+ data.leafHash.length === 32 &&
48
+ (data.signature.length === 64 || data.signature.length === 65));
49
+ }
50
+ exports.check = check;
51
+ function canAddToArray(array, item, dupeSet) {
52
+ const dupeString = tools.toHex(item.pubkey) + tools.toHex(item.leafHash);
53
+ if (dupeSet.has(dupeString))
54
+ return false;
55
+ dupeSet.add(dupeString);
56
+ return (array.filter(v => tools.compare(v.pubkey, item.pubkey) === 0 &&
57
+ tools.compare(v.leafHash, item.leafHash) === 0).length === 0);
58
+ }
59
+ exports.canAddToArray = canAddToArray;
@@ -0,0 +1,53 @@
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.WITNESS_UTXO) {
15
+ throw new Error('Decode Error: could not decode witnessUtxo with key 0x' +
16
+ tools.toHex(keyVal.key));
17
+ }
18
+ const value = tools.readInt64(keyVal.value, 0, 'LE');
19
+ let _offset = 8;
20
+ const { numberValue: scriptLen, bytes } = varuint.decode(keyVal.value, _offset);
21
+ _offset += bytes;
22
+ const script = keyVal.value.slice(_offset);
23
+ if (script.length !== scriptLen) {
24
+ throw new Error('Decode Error: WITNESS_UTXO script is not proper length');
25
+ }
26
+ return {
27
+ script,
28
+ value,
29
+ };
30
+ }
31
+ exports.decode = decode;
32
+ function encode(data) {
33
+ const { script, value } = data;
34
+ const varuintlen = varuint.encodingLength(script.length);
35
+ const result = new Uint8Array(8 + varuintlen + script.length);
36
+ tools.writeInt64(result, 0, BigInt(value), 'LE');
37
+ varuint.encode(script.length, result, 8);
38
+ result.set(script, 8 + varuintlen);
39
+ return {
40
+ key: Uint8Array.from([typeFields_js_1.InputTypes.WITNESS_UTXO]),
41
+ value: result,
42
+ };
43
+ }
44
+ exports.encode = encode;
45
+ exports.expected = '{ script: Uint8Array; value: bigint; }';
46
+ function check(data) {
47
+ return data.script instanceof Uint8Array && typeof data.value === 'bigint';
48
+ }
49
+ exports.check = check;
50
+ function canAdd(currentData, newData) {
51
+ return !!currentData && !!newData && currentData.witnessUtxo === undefined;
52
+ }
53
+ exports.canAdd = canAdd;
@@ -0,0 +1,60 @@
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 varuint = __importStar(require("varuint-bitcoin"));
12
+ const tools = __importStar(require("uint8array-tools"));
13
+ function decode(keyVal) {
14
+ if (keyVal.key[0] !== typeFields_js_1.OutputTypes.TAP_TREE || keyVal.key.length !== 1) {
15
+ throw new Error('Decode Error: could not decode tapTree with key 0x' +
16
+ tools.toHex(keyVal.key));
17
+ }
18
+ let _offset = 0;
19
+ const data = [];
20
+ while (_offset < keyVal.value.length) {
21
+ const depth = keyVal.value[_offset++];
22
+ const leafVersion = keyVal.value[_offset++];
23
+ const { numberValue: scriptLen, bytes } = varuint.decode(keyVal.value, _offset);
24
+ _offset += bytes;
25
+ data.push({
26
+ depth,
27
+ leafVersion,
28
+ script: keyVal.value.slice(_offset, _offset + scriptLen),
29
+ });
30
+ _offset += scriptLen;
31
+ }
32
+ return { leaves: data };
33
+ }
34
+ exports.decode = decode;
35
+ function encode(tree) {
36
+ const key = Uint8Array.from([typeFields_js_1.OutputTypes.TAP_TREE]);
37
+ const bufs = [].concat(...tree.leaves.map(tapLeaf => [
38
+ Uint8Array.of(tapLeaf.depth, tapLeaf.leafVersion),
39
+ varuint.encode(BigInt(tapLeaf.script.length)).buffer,
40
+ tapLeaf.script,
41
+ ]));
42
+ return {
43
+ key,
44
+ value: tools.concat(bufs),
45
+ };
46
+ }
47
+ exports.encode = encode;
48
+ exports.expected = '{ leaves: [{ depth: number; leafVersion: number, script: Uint8Array; }] }';
49
+ function check(data) {
50
+ return (Array.isArray(data.leaves) &&
51
+ data.leaves.every((tapLeaf) => tapLeaf.depth >= 0 &&
52
+ tapLeaf.depth <= 128 &&
53
+ (tapLeaf.leafVersion & 0xfe) === tapLeaf.leafVersion &&
54
+ tapLeaf.script instanceof Uint8Array));
55
+ }
56
+ exports.check = check;
57
+ function canAdd(currentData, newData) {
58
+ return !!currentData && !!newData && currentData.tapTree === undefined;
59
+ }
60
+ exports.canAdd = canAdd;
@@ -0,0 +1,84 @@
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 tools = __importStar(require("uint8array-tools"));
11
+ const range = (n) => [...Array(n).keys()];
12
+ const isValidDERKey = (pubkey) => (pubkey.length === 33 && [2, 3].includes(pubkey[0])) ||
13
+ (pubkey.length === 65 && 4 === pubkey[0]);
14
+ function makeConverter(TYPE_BYTE, isValidPubkey = isValidDERKey) {
15
+ function decode(keyVal) {
16
+ if (keyVal.key[0] !== TYPE_BYTE) {
17
+ throw new Error('Decode Error: could not decode bip32Derivation with key 0x' +
18
+ tools.toHex(keyVal.key));
19
+ }
20
+ const pubkey = keyVal.key.slice(1);
21
+ if (!isValidPubkey(pubkey)) {
22
+ throw new Error('Decode Error: bip32Derivation has invalid pubkey in key 0x' +
23
+ tools.toHex(keyVal.key));
24
+ }
25
+ if ((keyVal.value.length / 4) % 1 !== 0) {
26
+ throw new Error('Decode Error: Input BIP32_DERIVATION value length should be multiple of 4');
27
+ }
28
+ const data = {
29
+ masterFingerprint: keyVal.value.slice(0, 4),
30
+ pubkey,
31
+ path: 'm',
32
+ };
33
+ for (const i of range(keyVal.value.length / 4 - 1)) {
34
+ const val = tools.readUInt32(keyVal.value, i * 4 + 4, 'LE');
35
+ const isHard = !!(val & 0x80000000);
36
+ const idx = val & 0x7fffffff;
37
+ data.path += '/' + idx.toString(10) + (isHard ? "'" : '');
38
+ }
39
+ return data;
40
+ }
41
+ function encode(data) {
42
+ const head = Uint8Array.from([TYPE_BYTE]);
43
+ const key = tools.concat([head, data.pubkey]);
44
+ const splitPath = data.path.split('/');
45
+ const value = new Uint8Array(splitPath.length * 4);
46
+ value.set(data.masterFingerprint, 0);
47
+ let offset = 4;
48
+ splitPath.slice(1).forEach(level => {
49
+ const isHard = level.slice(-1) === "'";
50
+ let num = 0x7fffffff & parseInt(isHard ? level.slice(0, -1) : level, 10);
51
+ if (isHard)
52
+ num += 0x80000000;
53
+ tools.writeUInt32(value, offset, num, 'LE');
54
+ offset += 4;
55
+ });
56
+ return {
57
+ key,
58
+ value,
59
+ };
60
+ }
61
+ const expected = '{ masterFingerprint: Uint8Array; pubkey: Uint8Array; path: string; }';
62
+ function check(data) {
63
+ return (data.pubkey instanceof Uint8Array &&
64
+ data.masterFingerprint instanceof Uint8Array &&
65
+ typeof data.path === 'string' &&
66
+ isValidPubkey(data.pubkey) &&
67
+ data.masterFingerprint.length === 4);
68
+ }
69
+ function canAddToArray(array, item, dupeSet) {
70
+ const dupeString = tools.toHex(item.pubkey);
71
+ if (dupeSet.has(dupeString))
72
+ return false;
73
+ dupeSet.add(dupeString);
74
+ return (array.filter(v => tools.compare(v.pubkey, item.pubkey) === 0).length === 0);
75
+ }
76
+ return {
77
+ decode,
78
+ encode,
79
+ check,
80
+ expected,
81
+ canAddToArray,
82
+ };
83
+ }
84
+ exports.makeConverter = makeConverter;
@@ -0,0 +1,25 @@
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 tools = __importStar(require("uint8array-tools"));
11
+ function makeChecker(pubkeyTypes) {
12
+ return checkPubkey;
13
+ function checkPubkey(keyVal) {
14
+ let pubkey;
15
+ if (pubkeyTypes.includes(keyVal.key[0])) {
16
+ pubkey = keyVal.key.slice(1);
17
+ if (!(pubkey.length === 33 || pubkey.length === 65) ||
18
+ ![2, 3, 4].includes(pubkey[0])) {
19
+ throw new Error('Format Error: invalid pubkey in key 0x' + tools.toHex(keyVal.key));
20
+ }
21
+ }
22
+ return pubkey;
23
+ }
24
+ }
25
+ exports.makeChecker = makeChecker;
@@ -0,0 +1,41 @@
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 tools = __importStar(require("uint8array-tools"));
11
+ function makeConverter(TYPE_BYTE) {
12
+ function decode(keyVal) {
13
+ if (keyVal.key[0] !== TYPE_BYTE) {
14
+ throw new Error('Decode Error: could not decode redeemScript with key 0x' +
15
+ tools.toHex(keyVal.key));
16
+ }
17
+ return keyVal.value;
18
+ }
19
+ function encode(data) {
20
+ const key = Uint8Array.from([TYPE_BYTE]);
21
+ return {
22
+ key,
23
+ value: data,
24
+ };
25
+ }
26
+ const expected = 'Uint8Array';
27
+ function check(data) {
28
+ return data instanceof Uint8Array;
29
+ }
30
+ function canAdd(currentData, newData) {
31
+ return !!currentData && !!newData && currentData.redeemScript === undefined;
32
+ }
33
+ return {
34
+ decode,
35
+ encode,
36
+ check,
37
+ expected,
38
+ canAdd,
39
+ };
40
+ }
41
+ exports.makeConverter = makeConverter;
@@ -0,0 +1,55 @@
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 varuint = __importStar(require("varuint-bitcoin"));
11
+ const tools = __importStar(require("uint8array-tools"));
12
+ const bip32Derivation = __importStar(require("./bip32Derivation.js"));
13
+ const isValidBIP340Key = (pubkey) => pubkey.length === 32;
14
+ function makeConverter(TYPE_BYTE) {
15
+ const parent = bip32Derivation.makeConverter(TYPE_BYTE, isValidBIP340Key);
16
+ function decode(keyVal) {
17
+ const { numberValue: nHashes, bytes: nHashesLen } = varuint.decode(keyVal.value);
18
+ const base = parent.decode({
19
+ key: keyVal.key,
20
+ value: keyVal.value.slice(nHashesLen + Number(nHashes) * 32),
21
+ });
22
+ const leafHashes = new Array(Number(nHashes));
23
+ for (let i = 0, _offset = nHashesLen; i < nHashes; i++, _offset += 32) {
24
+ leafHashes[i] = keyVal.value.slice(_offset, _offset + 32);
25
+ }
26
+ return { ...base, leafHashes };
27
+ }
28
+ function encode(data) {
29
+ const base = parent.encode(data);
30
+ const nHashesLen = varuint.encodingLength(data.leafHashes.length);
31
+ const nHashesBuf = new Uint8Array(nHashesLen);
32
+ varuint.encode(data.leafHashes.length, nHashesBuf);
33
+ const value = tools.concat([nHashesBuf, ...data.leafHashes, base.value]);
34
+ return { ...base, value };
35
+ }
36
+ const expected = '{ ' +
37
+ 'masterFingerprint: Uint8Array; ' +
38
+ 'pubkey: Uint8Array; ' +
39
+ 'path: string; ' +
40
+ 'leafHashes: Uint8Array[]; ' +
41
+ '}';
42
+ function check(data) {
43
+ return (Array.isArray(data.leafHashes) &&
44
+ data.leafHashes.every((leafHash) => leafHash instanceof Uint8Array && leafHash.length === 32) &&
45
+ parent.check(data));
46
+ }
47
+ return {
48
+ decode,
49
+ encode,
50
+ check,
51
+ expected,
52
+ canAddToArray: parent.canAddToArray,
53
+ };
54
+ }
55
+ exports.makeConverter = makeConverter;