utxo-lib 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (249) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +59 -0
  3. package/dist/src/address.d.ts +11 -0
  4. package/dist/src/address.d.ts.map +1 -0
  5. package/dist/src/address.js +37 -0
  6. package/dist/src/addressFormat.d.ts +53 -0
  7. package/dist/src/addressFormat.d.ts.map +1 -0
  8. package/dist/src/addressFormat.js +110 -0
  9. package/dist/src/bitgo/Musig2.d.ts +51 -0
  10. package/dist/src/bitgo/Musig2.d.ts.map +1 -0
  11. package/dist/src/bitgo/Musig2.js +175 -0
  12. package/dist/src/bitgo/Unspent.d.ts +95 -0
  13. package/dist/src/bitgo/Unspent.d.ts.map +1 -0
  14. package/dist/src/bitgo/Unspent.js +138 -0
  15. package/dist/src/bitgo/UtxoPsbt.d.ts +132 -0
  16. package/dist/src/bitgo/UtxoPsbt.d.ts.map +1 -0
  17. package/dist/src/bitgo/UtxoPsbt.js +469 -0
  18. package/dist/src/bitgo/UtxoTransaction.d.ts +22 -0
  19. package/dist/src/bitgo/UtxoTransaction.d.ts.map +1 -0
  20. package/dist/src/bitgo/UtxoTransaction.js +97 -0
  21. package/dist/src/bitgo/UtxoTransactionBuilder.d.ts +26 -0
  22. package/dist/src/bitgo/UtxoTransactionBuilder.d.ts.map +1 -0
  23. package/dist/src/bitgo/UtxoTransactionBuilder.js +67 -0
  24. package/dist/src/bitgo/bbc/DashPsbt.d.ts +12 -0
  25. package/dist/src/bitgo/bbc/DashPsbt.d.ts.map +1 -0
  26. package/dist/src/bitgo/bbc/DashPsbt.js +27 -0
  27. package/dist/src/bitgo/bbc/DashTransaction.d.ts +39 -0
  28. package/dist/src/bitgo/bbc/DashTransaction.d.ts.map +1 -0
  29. package/dist/src/bitgo/bbc/DashTransaction.js +109 -0
  30. package/dist/src/bitgo/bbc/DashTransactionBuilder.d.ts +14 -0
  31. package/dist/src/bitgo/bbc/DashTransactionBuilder.d.ts.map +1 -0
  32. package/dist/src/bitgo/bbc/DashTransactionBuilder.js +28 -0
  33. package/dist/src/bitgo/bbc/index.d.ts +4 -0
  34. package/dist/src/bitgo/bbc/index.d.ts.map +1 -0
  35. package/dist/src/bitgo/bbc/index.js +16 -0
  36. package/dist/src/bitgo/bitcoincash/address.d.ts +35 -0
  37. package/dist/src/bitgo/bitcoincash/address.d.ts.map +1 -0
  38. package/dist/src/bitgo/bitcoincash/address.js +151 -0
  39. package/dist/src/bitgo/bitcoincash/index.d.ts +2 -0
  40. package/dist/src/bitgo/bitcoincash/index.d.ts.map +1 -0
  41. package/dist/src/bitgo/bitcoincash/index.js +14 -0
  42. package/dist/src/bitgo/dash/DashPsbt.d.ts +12 -0
  43. package/dist/src/bitgo/dash/DashPsbt.d.ts.map +1 -0
  44. package/dist/src/bitgo/dash/DashPsbt.js +27 -0
  45. package/dist/src/bitgo/dash/DashTransaction.d.ts +39 -0
  46. package/dist/src/bitgo/dash/DashTransaction.d.ts.map +1 -0
  47. package/dist/src/bitgo/dash/DashTransaction.js +109 -0
  48. package/dist/src/bitgo/dash/DashTransactionBuilder.d.ts +14 -0
  49. package/dist/src/bitgo/dash/DashTransactionBuilder.d.ts.map +1 -0
  50. package/dist/src/bitgo/dash/DashTransactionBuilder.js +28 -0
  51. package/dist/src/bitgo/dash/index.d.ts +4 -0
  52. package/dist/src/bitgo/dash/index.d.ts.map +1 -0
  53. package/dist/src/bitgo/dash/index.js +16 -0
  54. package/dist/src/bitgo/index.d.ts +17 -0
  55. package/dist/src/bitgo/index.d.ts.map +1 -0
  56. package/dist/src/bitgo/index.js +30 -0
  57. package/dist/src/bitgo/keyutil.d.ts +17 -0
  58. package/dist/src/bitgo/keyutil.d.ts.map +1 -0
  59. package/dist/src/bitgo/keyutil.js +37 -0
  60. package/dist/src/bitgo/nonStandardHalfSigned.d.ts +7 -0
  61. package/dist/src/bitgo/nonStandardHalfSigned.d.ts.map +1 -0
  62. package/dist/src/bitgo/nonStandardHalfSigned.js +56 -0
  63. package/dist/src/bitgo/outputScripts.d.ts +84 -0
  64. package/dist/src/bitgo/outputScripts.d.ts.map +1 -0
  65. package/dist/src/bitgo/outputScripts.js +297 -0
  66. package/dist/src/bitgo/parseInput.d.ts +92 -0
  67. package/dist/src/bitgo/parseInput.d.ts.map +1 -0
  68. package/dist/src/bitgo/parseInput.js +344 -0
  69. package/dist/src/bitgo/psbt/fromHalfSigned.d.ts +24 -0
  70. package/dist/src/bitgo/psbt/fromHalfSigned.d.ts.map +1 -0
  71. package/dist/src/bitgo/psbt/fromHalfSigned.js +91 -0
  72. package/dist/src/bitgo/psbt/scriptTypes.d.ts +6 -0
  73. package/dist/src/bitgo/psbt/scriptTypes.d.ts.map +1 -0
  74. package/dist/src/bitgo/psbt/scriptTypes.js +23 -0
  75. package/dist/src/bitgo/signature.d.ts +84 -0
  76. package/dist/src/bitgo/signature.d.ts.map +1 -0
  77. package/dist/src/bitgo/signature.js +222 -0
  78. package/dist/src/bitgo/tnumber.d.ts +9 -0
  79. package/dist/src/bitgo/tnumber.d.ts.map +1 -0
  80. package/dist/src/bitgo/tnumber.js +31 -0
  81. package/dist/src/bitgo/transaction.d.ts +29 -0
  82. package/dist/src/bitgo/transaction.d.ts.map +1 -0
  83. package/dist/src/bitgo/transaction.js +247 -0
  84. package/dist/src/bitgo/types.d.ts +5 -0
  85. package/dist/src/bitgo/types.d.ts.map +1 -0
  86. package/dist/src/bitgo/types.js +12 -0
  87. package/dist/src/bitgo/wallet/Psbt.d.ts +47 -0
  88. package/dist/src/bitgo/wallet/Psbt.d.ts.map +1 -0
  89. package/dist/src/bitgo/wallet/Psbt.js +232 -0
  90. package/dist/src/bitgo/wallet/Unspent.d.ts +47 -0
  91. package/dist/src/bitgo/wallet/Unspent.d.ts.map +1 -0
  92. package/dist/src/bitgo/wallet/Unspent.js +154 -0
  93. package/dist/src/bitgo/wallet/WalletKeys.d.ts +72 -0
  94. package/dist/src/bitgo/wallet/WalletKeys.d.ts.map +1 -0
  95. package/dist/src/bitgo/wallet/WalletKeys.js +104 -0
  96. package/dist/src/bitgo/wallet/WalletOutput.d.ts +18 -0
  97. package/dist/src/bitgo/wallet/WalletOutput.d.ts.map +1 -0
  98. package/dist/src/bitgo/wallet/WalletOutput.js +69 -0
  99. package/dist/src/bitgo/wallet/WalletScripts.d.ts +7 -0
  100. package/dist/src/bitgo/wallet/WalletScripts.d.ts.map +1 -0
  101. package/dist/src/bitgo/wallet/WalletScripts.js +15 -0
  102. package/dist/src/bitgo/wallet/WalletUnspentSigner.d.ts +19 -0
  103. package/dist/src/bitgo/wallet/WalletUnspentSigner.d.ts.map +1 -0
  104. package/dist/src/bitgo/wallet/WalletUnspentSigner.js +47 -0
  105. package/dist/src/bitgo/wallet/chains.d.ts +57 -0
  106. package/dist/src/bitgo/wallet/chains.d.ts.map +1 -0
  107. package/dist/src/bitgo/wallet/chains.js +106 -0
  108. package/dist/src/bitgo/wallet/index.d.ts +8 -0
  109. package/dist/src/bitgo/wallet/index.d.ts.map +1 -0
  110. package/dist/src/bitgo/wallet/index.js +20 -0
  111. package/dist/src/bitgo/zcash/ZcashBufferutils.d.ts +26 -0
  112. package/dist/src/bitgo/zcash/ZcashBufferutils.d.ts.map +1 -0
  113. package/dist/src/bitgo/zcash/ZcashBufferutils.js +157 -0
  114. package/dist/src/bitgo/zcash/ZcashPsbt.d.ts +36 -0
  115. package/dist/src/bitgo/zcash/ZcashPsbt.d.ts.map +1 -0
  116. package/dist/src/bitgo/zcash/ZcashPsbt.js +146 -0
  117. package/dist/src/bitgo/zcash/ZcashTransaction.d.ts +61 -0
  118. package/dist/src/bitgo/zcash/ZcashTransaction.d.ts.map +1 -0
  119. package/dist/src/bitgo/zcash/ZcashTransaction.js +341 -0
  120. package/dist/src/bitgo/zcash/ZcashTransactionBuilder.d.ts +21 -0
  121. package/dist/src/bitgo/zcash/ZcashTransactionBuilder.d.ts.map +1 -0
  122. package/dist/src/bitgo/zcash/ZcashTransactionBuilder.js +105 -0
  123. package/dist/src/bitgo/zcash/address.d.ts +8 -0
  124. package/dist/src/bitgo/zcash/address.d.ts.map +1 -0
  125. package/dist/src/bitgo/zcash/address.js +57 -0
  126. package/dist/src/bitgo/zcash/hashZip0244.d.ts +27 -0
  127. package/dist/src/bitgo/zcash/hashZip0244.d.ts.map +1 -0
  128. package/dist/src/bitgo/zcash/hashZip0244.js +184 -0
  129. package/dist/src/bitgo/zcash/index.d.ts +4 -0
  130. package/dist/src/bitgo/zcash/index.d.ts.map +1 -0
  131. package/dist/src/bitgo/zcash/index.js +16 -0
  132. package/dist/src/classify.d.ts +19 -0
  133. package/dist/src/classify.d.ts.map +1 -0
  134. package/dist/src/classify.js +89 -0
  135. package/dist/src/index.d.ts +13 -0
  136. package/dist/src/index.d.ts.map +1 -0
  137. package/dist/src/index.js +36 -0
  138. package/dist/src/networks.d.ts +119 -0
  139. package/dist/src/networks.d.ts.map +1 -0
  140. package/dist/src/networks.js +477 -0
  141. package/dist/src/noble_ecc.d.ts +28 -0
  142. package/dist/src/noble_ecc.d.ts.map +1 -0
  143. package/dist/src/noble_ecc.js +168 -0
  144. package/dist/src/payments/index.d.ts +4 -0
  145. package/dist/src/payments/index.d.ts.map +1 -0
  146. package/dist/src/payments/index.js +8 -0
  147. package/dist/src/payments/p2tr.d.ts +3 -0
  148. package/dist/src/payments/p2tr.d.ts.map +1 -0
  149. package/dist/src/payments/p2tr.js +348 -0
  150. package/dist/src/payments/p2tr_ns.d.ts +3 -0
  151. package/dist/src/payments/p2tr_ns.d.ts.map +1 -0
  152. package/dist/src/payments/p2tr_ns.js +134 -0
  153. package/dist/src/taproot.d.ts +141 -0
  154. package/dist/src/taproot.d.ts.map +1 -0
  155. package/dist/src/taproot.js +384 -0
  156. package/dist/src/templates/multisig/index.d.ts +4 -0
  157. package/dist/src/templates/multisig/index.d.ts.map +1 -0
  158. package/dist/src/templates/multisig/index.js +8 -0
  159. package/dist/src/templates/multisig/input.d.ts +7 -0
  160. package/dist/src/templates/multisig/input.d.ts.map +1 -0
  161. package/dist/src/templates/multisig/input.js +25 -0
  162. package/dist/src/templates/multisig/output.d.ts +7 -0
  163. package/dist/src/templates/multisig/output.d.ts.map +1 -0
  164. package/dist/src/templates/multisig/output.js +38 -0
  165. package/dist/src/templates/nulldata.d.ts +10 -0
  166. package/dist/src/templates/nulldata.d.ts.map +1 -0
  167. package/dist/src/templates/nulldata.js +17 -0
  168. package/dist/src/templates/pubkey/index.d.ts +4 -0
  169. package/dist/src/templates/pubkey/index.d.ts.map +1 -0
  170. package/dist/src/templates/pubkey/index.js +8 -0
  171. package/dist/src/templates/pubkey/input.d.ts +7 -0
  172. package/dist/src/templates/pubkey/input.d.ts.map +1 -0
  173. package/dist/src/templates/pubkey/input.js +14 -0
  174. package/dist/src/templates/pubkey/output.d.ts +7 -0
  175. package/dist/src/templates/pubkey/output.d.ts.map +1 -0
  176. package/dist/src/templates/pubkey/output.js +15 -0
  177. package/dist/src/templates/pubkeyhash/index.d.ts +4 -0
  178. package/dist/src/templates/pubkeyhash/index.d.ts.map +1 -0
  179. package/dist/src/templates/pubkeyhash/index.js +8 -0
  180. package/dist/src/templates/pubkeyhash/input.d.ts +7 -0
  181. package/dist/src/templates/pubkeyhash/input.d.ts.map +1 -0
  182. package/dist/src/templates/pubkeyhash/input.js +16 -0
  183. package/dist/src/templates/pubkeyhash/output.d.ts +6 -0
  184. package/dist/src/templates/pubkeyhash/output.d.ts.map +1 -0
  185. package/dist/src/templates/pubkeyhash/output.js +20 -0
  186. package/dist/src/templates/scripthash/index.d.ts +4 -0
  187. package/dist/src/templates/scripthash/index.d.ts.map +1 -0
  188. package/dist/src/templates/scripthash/index.js +8 -0
  189. package/dist/src/templates/scripthash/input.d.ts +6 -0
  190. package/dist/src/templates/scripthash/input.d.ts.map +1 -0
  191. package/dist/src/templates/scripthash/input.js +43 -0
  192. package/dist/src/templates/scripthash/output.d.ts +6 -0
  193. package/dist/src/templates/scripthash/output.d.ts.map +1 -0
  194. package/dist/src/templates/scripthash/output.js +15 -0
  195. package/dist/src/templates/taproot/index.d.ts +4 -0
  196. package/dist/src/templates/taproot/index.d.ts.map +1 -0
  197. package/dist/src/templates/taproot/index.js +8 -0
  198. package/dist/src/templates/taproot/input.d.ts +6 -0
  199. package/dist/src/templates/taproot/input.d.ts.map +1 -0
  200. package/dist/src/templates/taproot/input.js +23 -0
  201. package/dist/src/templates/taproot/output.d.ts +6 -0
  202. package/dist/src/templates/taproot/output.d.ts.map +1 -0
  203. package/dist/src/templates/taproot/output.js +15 -0
  204. package/dist/src/templates/taprootnofn/index.d.ts +4 -0
  205. package/dist/src/templates/taprootnofn/index.d.ts.map +1 -0
  206. package/dist/src/templates/taprootnofn/index.js +8 -0
  207. package/dist/src/templates/taprootnofn/input.d.ts +7 -0
  208. package/dist/src/templates/taprootnofn/input.d.ts.map +1 -0
  209. package/dist/src/templates/taprootnofn/input.js +24 -0
  210. package/dist/src/templates/taprootnofn/output.d.ts +7 -0
  211. package/dist/src/templates/taprootnofn/output.d.ts.map +1 -0
  212. package/dist/src/templates/taprootnofn/output.js +28 -0
  213. package/dist/src/templates/witnesscommitment/index.d.ts +3 -0
  214. package/dist/src/templates/witnesscommitment/index.d.ts.map +1 -0
  215. package/dist/src/templates/witnesscommitment/index.js +6 -0
  216. package/dist/src/templates/witnesscommitment/output.d.ts +8 -0
  217. package/dist/src/templates/witnesscommitment/output.d.ts.map +1 -0
  218. package/dist/src/templates/witnesscommitment/output.js +31 -0
  219. package/dist/src/templates/witnesspubkeyhash/index.d.ts +4 -0
  220. package/dist/src/templates/witnesspubkeyhash/index.d.ts.map +1 -0
  221. package/dist/src/templates/witnesspubkeyhash/index.js +8 -0
  222. package/dist/src/templates/witnesspubkeyhash/input.d.ts +7 -0
  223. package/dist/src/templates/witnesspubkeyhash/input.d.ts.map +1 -0
  224. package/dist/src/templates/witnesspubkeyhash/input.js +19 -0
  225. package/dist/src/templates/witnesspubkeyhash/output.d.ts +6 -0
  226. package/dist/src/templates/witnesspubkeyhash/output.d.ts.map +1 -0
  227. package/dist/src/templates/witnesspubkeyhash/output.js +15 -0
  228. package/dist/src/templates/witnessscripthash/index.d.ts +4 -0
  229. package/dist/src/templates/witnessscripthash/index.d.ts.map +1 -0
  230. package/dist/src/templates/witnessscripthash/index.js +8 -0
  231. package/dist/src/templates/witnessscripthash/input.d.ts +6 -0
  232. package/dist/src/templates/witnessscripthash/input.d.ts.map +1 -0
  233. package/dist/src/templates/witnessscripthash/input.js +35 -0
  234. package/dist/src/templates/witnessscripthash/output.d.ts +6 -0
  235. package/dist/src/templates/witnessscripthash/output.d.ts.map +1 -0
  236. package/dist/src/templates/witnessscripthash/output.js +15 -0
  237. package/dist/src/testutil/index.d.ts +3 -0
  238. package/dist/src/testutil/index.d.ts.map +1 -0
  239. package/dist/src/testutil/index.js +15 -0
  240. package/dist/src/testutil/keys.d.ts +10 -0
  241. package/dist/src/testutil/keys.d.ts.map +1 -0
  242. package/dist/src/testutil/keys.js +40 -0
  243. package/dist/src/testutil/mock.d.ts +21 -0
  244. package/dist/src/testutil/mock.d.ts.map +1 -0
  245. package/dist/src/testutil/mock.js +86 -0
  246. package/dist/src/transaction_builder.d.ts +47 -0
  247. package/dist/src/transaction_builder.d.ts.map +1 -0
  248. package/dist/src/transaction_builder.js +1084 -0
  249. package/package.json +87 -0
@@ -0,0 +1,247 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createTransactionBuilderFromTransaction = exports.createTransactionBuilderForNetwork = exports.createPsbtForNetwork = exports.setPsbtDefaults = exports.setTransactionBuilderDefaults = exports.getDefaultTransactionVersion = exports.createTransactionFromHex = exports.createPsbtFromTransaction = exports.createPsbtFromHex = exports.createPsbtFromBuffer = exports.createTransactionFromBuffer = void 0;
4
+ const networks_1 = require("../networks");
5
+ const UtxoPsbt_1 = require("./UtxoPsbt");
6
+ const UtxoTransaction_1 = require("./UtxoTransaction");
7
+ const UtxoTransactionBuilder_1 = require("./UtxoTransactionBuilder");
8
+ const DashPsbt_1 = require("./dash/DashPsbt");
9
+ const DashTransaction_1 = require("./dash/DashTransaction");
10
+ const DashTransactionBuilder_1 = require("./dash/DashTransactionBuilder");
11
+ const ZcashPsbt_1 = require("./zcash/ZcashPsbt");
12
+ const ZcashTransactionBuilder_1 = require("./zcash/ZcashTransactionBuilder");
13
+ const ZcashTransaction_1 = require("./zcash/ZcashTransaction");
14
+ // #9286F8 PECUNIA CHANGES
15
+ const DashPsbt_2 = require("./bbc/DashPsbt");
16
+ const DashTransaction_2 = require("./bbc/DashTransaction");
17
+ function createTransactionFromBuffer(buf, network, { version, amountType } = {}, deprecatedAmountType) {
18
+ if (amountType) {
19
+ if (deprecatedAmountType && amountType !== deprecatedAmountType) {
20
+ throw new Error(`invalid arguments`);
21
+ }
22
+ }
23
+ else {
24
+ if (deprecatedAmountType) {
25
+ amountType = deprecatedAmountType;
26
+ }
27
+ else {
28
+ amountType = 'number';
29
+ }
30
+ }
31
+ switch (networks_1.getMainnet(network)) {
32
+ case networks_1.networks.bitcoin:
33
+ case networks_1.networks.bitcoincash:
34
+ case networks_1.networks.bitcoinsv:
35
+ case networks_1.networks.bitcoingold:
36
+ case networks_1.networks.dogecoin:
37
+ case networks_1.networks.ecash:
38
+ case networks_1.networks.litecoin:
39
+ return UtxoTransaction_1.UtxoTransaction.fromBuffer(buf, false, amountType, network);
40
+ case networks_1.networks.dash:
41
+ return DashTransaction_1.DashTransaction.fromBuffer(buf, false, amountType, network);
42
+ case networks_1.networks.zcash:
43
+ return ZcashTransaction_1.ZcashTransaction.fromBufferWithVersion(buf, network, version, amountType);
44
+ case networks_1.networks.bbc:
45
+ return DashTransaction_2.DashTransaction.fromBuffer(buf, false, amountType, network);
46
+ }
47
+ /* istanbul ignore next */
48
+ throw new Error(`invalid network`);
49
+ }
50
+ exports.createTransactionFromBuffer = createTransactionFromBuffer;
51
+ function createPsbtFromBuffer(buf, network, bip32PathsAbsolute = false) {
52
+ switch (networks_1.getMainnet(network)) {
53
+ case networks_1.networks.bitcoin:
54
+ case networks_1.networks.bitcoincash:
55
+ case networks_1.networks.bitcoinsv:
56
+ case networks_1.networks.bitcoingold:
57
+ case networks_1.networks.dogecoin:
58
+ case networks_1.networks.ecash:
59
+ case networks_1.networks.litecoin:
60
+ return UtxoPsbt_1.UtxoPsbt.fromBuffer(buf, { network, bip32PathsAbsolute });
61
+ case networks_1.networks.dash:
62
+ return DashPsbt_1.DashPsbt.fromBuffer(buf, { network, bip32PathsAbsolute });
63
+ case networks_1.networks.zcash:
64
+ return ZcashPsbt_1.ZcashPsbt.fromBuffer(buf, { network, bip32PathsAbsolute });
65
+ // #9286F8 PECUNIA CHANGES
66
+ case networks_1.networks.bbc:
67
+ return DashPsbt_2.DashPsbt.fromBuffer(buf, { network, bip32PathsAbsolute });
68
+ }
69
+ /* istanbul ignore next */
70
+ throw new Error(`invalid network`);
71
+ }
72
+ exports.createPsbtFromBuffer = createPsbtFromBuffer;
73
+ function createPsbtFromHex(hex, network, bip32PathsAbsolute = false) {
74
+ return createPsbtFromBuffer(Buffer.from(hex, 'hex'), network, bip32PathsAbsolute);
75
+ }
76
+ exports.createPsbtFromHex = createPsbtFromHex;
77
+ function createPsbtFromTransaction(tx, prevOuts) {
78
+ switch (networks_1.getMainnet(tx.network)) {
79
+ case networks_1.networks.bitcoin:
80
+ case networks_1.networks.bitcoincash:
81
+ case networks_1.networks.bitcoinsv:
82
+ case networks_1.networks.bitcoingold:
83
+ case networks_1.networks.dogecoin:
84
+ case networks_1.networks.ecash:
85
+ case networks_1.networks.litecoin:
86
+ return UtxoPsbt_1.UtxoPsbt.fromTransaction(tx, prevOuts);
87
+ case networks_1.networks.dash:
88
+ return DashPsbt_1.DashPsbt.fromTransaction(tx, prevOuts);
89
+ case networks_1.networks.zcash:
90
+ return ZcashPsbt_1.ZcashPsbt.fromTransaction(tx, prevOuts);
91
+ // #9286F8 PECUNIA CHANGES
92
+ case networks_1.networks.bbc:
93
+ return DashPsbt_2.DashPsbt.fromTransaction(tx, prevOuts);
94
+ }
95
+ /* istanbul ignore next */
96
+ throw new Error(`invalid network`);
97
+ }
98
+ exports.createPsbtFromTransaction = createPsbtFromTransaction;
99
+ /* istanbul ignore next */
100
+ function createTransactionFromHex(hex, network, amountType = 'number') {
101
+ return createTransactionFromBuffer(Buffer.from(hex, 'hex'), network, { amountType });
102
+ }
103
+ exports.createTransactionFromHex = createTransactionFromHex;
104
+ function getDefaultTransactionVersion(network) {
105
+ switch (networks_1.getMainnet(network)) {
106
+ case networks_1.networks.bitcoincash:
107
+ case networks_1.networks.bitcoinsv:
108
+ case networks_1.networks.bitcoingold:
109
+ case networks_1.networks.ecash:
110
+ return 2;
111
+ case networks_1.networks.zcash:
112
+ return ZcashTransaction_1.ZcashTransaction.VERSION4_BRANCH_NU5;
113
+ default:
114
+ return 1;
115
+ }
116
+ }
117
+ exports.getDefaultTransactionVersion = getDefaultTransactionVersion;
118
+ function setTransactionBuilderDefaults(txb, network, { version = getDefaultTransactionVersion(network) } = {}) {
119
+ switch (networks_1.getMainnet(network)) {
120
+ case networks_1.networks.bitcoincash:
121
+ case networks_1.networks.bitcoinsv:
122
+ case networks_1.networks.bitcoingold:
123
+ case networks_1.networks.ecash:
124
+ if (version !== 2) {
125
+ throw new Error(`invalid version`);
126
+ }
127
+ txb.setVersion(version);
128
+ break;
129
+ case networks_1.networks.zcash:
130
+ txb.setDefaultsForVersion(network, version);
131
+ break;
132
+ default:
133
+ if (version !== 1) {
134
+ throw new Error(`invalid version`);
135
+ }
136
+ }
137
+ }
138
+ exports.setTransactionBuilderDefaults = setTransactionBuilderDefaults;
139
+ function setPsbtDefaults(psbt, network, { version = getDefaultTransactionVersion(network) } = {}) {
140
+ switch (networks_1.getMainnet(network)) {
141
+ case networks_1.networks.bitcoincash:
142
+ case networks_1.networks.bitcoinsv:
143
+ case networks_1.networks.bitcoingold:
144
+ case networks_1.networks.ecash:
145
+ if (version !== 2) {
146
+ throw new Error(`invalid version`);
147
+ }
148
+ break;
149
+ case networks_1.networks.zcash:
150
+ if (![
151
+ ZcashTransaction_1.ZcashTransaction.VERSION4_BRANCH_CANOPY,
152
+ ZcashTransaction_1.ZcashTransaction.VERSION4_BRANCH_NU5,
153
+ ZcashTransaction_1.ZcashTransaction.VERSION5_BRANCH_NU5,
154
+ ].includes(version)) {
155
+ throw new Error(`invalid version`);
156
+ }
157
+ psbt.setDefaultsForVersion(network, version);
158
+ break;
159
+ default:
160
+ if (version !== 1) {
161
+ throw new Error(`invalid version`);
162
+ }
163
+ // FIXME: set version here, because there's a bug in the upstream PSBT
164
+ // that defaults transactions to v2.
165
+ psbt.setVersion(version);
166
+ }
167
+ }
168
+ exports.setPsbtDefaults = setPsbtDefaults;
169
+ function createPsbtForNetwork(psbtOpts, { version } = {}) {
170
+ let psbt;
171
+ switch (networks_1.getMainnet(psbtOpts.network)) {
172
+ case networks_1.networks.bitcoin:
173
+ case networks_1.networks.bitcoincash:
174
+ case networks_1.networks.bitcoinsv:
175
+ case networks_1.networks.bitcoingold:
176
+ case networks_1.networks.dogecoin:
177
+ case networks_1.networks.ecash:
178
+ case networks_1.networks.litecoin: {
179
+ psbt = UtxoPsbt_1.UtxoPsbt.createPsbt(psbtOpts);
180
+ break;
181
+ }
182
+ case networks_1.networks.dash: {
183
+ psbt = DashPsbt_1.DashPsbt.createPsbt(psbtOpts);
184
+ break;
185
+ }
186
+ case networks_1.networks.zcash: {
187
+ psbt = ZcashPsbt_1.ZcashPsbt.createPsbt(psbtOpts);
188
+ break;
189
+ }
190
+ // #9286F8 PECUNIA CHANGES
191
+ case networks_1.networks.bbc: {
192
+ psbt = DashPsbt_2.DashPsbt.createPsbt(psbtOpts);
193
+ break;
194
+ }
195
+ default:
196
+ throw new Error(`unsupported network`);
197
+ }
198
+ setPsbtDefaults(psbt, psbtOpts.network, { version });
199
+ return psbt;
200
+ }
201
+ exports.createPsbtForNetwork = createPsbtForNetwork;
202
+ function createTransactionBuilderForNetwork(network, { version } = {}) {
203
+ let txb;
204
+ switch (networks_1.getMainnet(network)) {
205
+ case networks_1.networks.bitcoin:
206
+ case networks_1.networks.bitcoincash:
207
+ case networks_1.networks.bitcoinsv:
208
+ case networks_1.networks.bitcoingold:
209
+ case networks_1.networks.dogecoin:
210
+ case networks_1.networks.ecash:
211
+ case networks_1.networks.litecoin: {
212
+ txb = new UtxoTransactionBuilder_1.UtxoTransactionBuilder(network);
213
+ break;
214
+ }
215
+ case networks_1.networks.dash:
216
+ txb = new DashTransactionBuilder_1.DashTransactionBuilder(network);
217
+ break;
218
+ case networks_1.networks.zcash: {
219
+ txb = new ZcashTransactionBuilder_1.ZcashTransactionBuilder(network);
220
+ break;
221
+ }
222
+ default:
223
+ throw new Error(`unsupported network`);
224
+ }
225
+ setTransactionBuilderDefaults(txb, network, { version });
226
+ return txb;
227
+ }
228
+ exports.createTransactionBuilderForNetwork = createTransactionBuilderForNetwork;
229
+ function createTransactionBuilderFromTransaction(tx, prevOutputs) {
230
+ switch (networks_1.getMainnet(tx.network)) {
231
+ case networks_1.networks.bitcoin:
232
+ case networks_1.networks.bitcoincash:
233
+ case networks_1.networks.bitcoinsv:
234
+ case networks_1.networks.bitcoingold:
235
+ case networks_1.networks.dogecoin:
236
+ case networks_1.networks.ecash:
237
+ case networks_1.networks.litecoin:
238
+ return UtxoTransactionBuilder_1.UtxoTransactionBuilder.fromTransaction(tx, undefined, prevOutputs);
239
+ case networks_1.networks.dash:
240
+ return DashTransactionBuilder_1.DashTransactionBuilder.fromTransaction(tx, undefined, prevOutputs);
241
+ case networks_1.networks.zcash:
242
+ return ZcashTransactionBuilder_1.ZcashTransactionBuilder.fromTransaction(tx, undefined, prevOutputs);
243
+ }
244
+ throw new Error(`invalid network`);
245
+ }
246
+ exports.createTransactionBuilderFromTransaction = createTransactionBuilderFromTransaction;
247
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidHJhbnNhY3Rpb24uanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi9zcmMvYml0Z28vdHJhbnNh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AgIGNhc2UgbmV0d29ya3MubGl0ZWNvaW46XG4gICAgICByZXR1cm4gVXR4b1RyYW5zYWN0aW9uQnVpbGRlci5mcm9tVHJhbnNhY3Rpb248VE51bWJlcj4odHgsIHVuZGVmaW5lZCwgcHJldk91dHB1dHMpO1xuICAgIGNhc2UgbmV0d29ya3MuZGFzaDpcbiAgICAgIHJldHVybiBEYXNoVHJhbnNhY3Rpb25CdWlsZGVyLmZyb21UcmFuc2FjdGlvbjxUTnVtYmVyPihcbiAgICAgICAgdHggYXMgRGFzaFRyYW5zYWN0aW9uPFROdW1iZXI+LFxuICAgICAgICB1bmRlZmluZWQsXG4gICAgICAgIHByZXZPdXRwdXRzIGFzIFR4T3V0cHV0PFROdW1iZXI+W11cbiAgICAgICk7XG4gICAgY2FzZSBuZXR3b3Jrcy56Y2FzaDpcbiAgICAgIHJldHVybiBaY2FzaFRyYW5zYWN0aW9uQnVpbGRlci5mcm9tVHJhbnNhY3Rpb248VE51bWJlcj4oXG4gICAgICAgIHR4IGFzIFpjYXNoVHJhbnNhY3Rpb248VE51bWJlcj4sXG4gICAgICAgIHVuZGVmaW5lZCxcbiAgICAgICAgcHJldk91dHB1dHMgYXMgVHhPdXRwdXQ8VE51bWJlcj5bXVxuICAgICAgKTtcbiAgfVxuXG4gIHRocm93IG5ldyBFcnJvcihgaW52YWxpZCBuZXR3b3JrYCk7XG59XG4iXX0=
@@ -0,0 +1,5 @@
1
+ export declare type Tuple<T> = [T, T];
2
+ export declare function isTuple<T>(arr: T[]): arr is Tuple<T>;
3
+ export declare type Triple<T> = [T, T, T];
4
+ export declare function isTriple<T>(arr: T[]): arr is Triple<T>;
5
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/bitgo/types.ts"],"names":[],"mappings":"AAAA,oBAAY,KAAK,CAAC,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;AAE9B,wBAAgB,OAAO,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,GAAG,GAAG,IAAI,KAAK,CAAC,CAAC,CAAC,CAEpD;AAED,oBAAY,MAAM,CAAC,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;AAElC,wBAAgB,QAAQ,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,GAAG,GAAG,IAAI,MAAM,CAAC,CAAC,CAAC,CAEtD"}
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isTriple = exports.isTuple = void 0;
4
+ function isTuple(arr) {
5
+ return arr.length === 2;
6
+ }
7
+ exports.isTuple = isTuple;
8
+ function isTriple(arr) {
9
+ return arr.length === 3;
10
+ }
11
+ exports.isTriple = isTriple;
12
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidHlwZXMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi9zcmMvYml0Z28vdHlwZXMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7O0FBRUEsU0FBZ0IsT0FBTyxDQUFJLEdBQVE7SUFDakMsT0FBTyxHQUFHLENBQUMsTUFBTSxLQUFLLENBQUMsQ0FBQztBQUMxQixDQUFDO0FBRkQsMEJBRUM7QUFJRCxTQUFnQixRQUFRLENBQUksR0FBUTtJQUNsQyxPQUFPLEdBQUcsQ0FBQyxNQUFNLEtBQUssQ0FBQyxDQUFDO0FBQzFCLENBQUM7QUFGRCw0QkFFQyIsInNvdXJjZXNDb250ZW50IjpbImV4cG9ydCB0eXBlIFR1cGxlPFQ+ID0gW1QsIFRdO1xuXG5leHBvcnQgZnVuY3Rpb24gaXNUdXBsZTxUPihhcnI6IFRbXSk6IGFyciBpcyBUdXBsZTxUPiB7XG4gIHJldHVybiBhcnIubGVuZ3RoID09PSAyO1xufVxuXG5leHBvcnQgdHlwZSBUcmlwbGU8VD4gPSBbVCwgVCwgVF07XG5cbmV4cG9ydCBmdW5jdGlvbiBpc1RyaXBsZTxUPihhcnI6IFRbXSk6IGFyciBpcyBUcmlwbGU8VD4ge1xuICByZXR1cm4gYXJyLmxlbmd0aCA9PT0gMztcbn1cbiJdfQ==
@@ -0,0 +1,47 @@
1
+ /// <reference types="node" />
2
+ import { UtxoPsbt } from '../UtxoPsbt';
3
+ import { UtxoTransaction } from '../UtxoTransaction';
4
+ import { RootWalletKeys } from './WalletKeys';
5
+ import { BIP32Interface } from 'bip32';
6
+ import { WalletUnspent } from './Unspent';
7
+ import { ParsedPubScript2Of3, ParsedPubScriptTaprootScriptPath } from '../parseInput';
8
+ declare type Signatures = [Buffer] | [Buffer, Buffer] | undefined;
9
+ export interface ParsedPsbt2Of3 extends ParsedPubScript2Of3 {
10
+ signatures: Signatures;
11
+ }
12
+ export interface ParsedPsbtP2TR extends ParsedPubScriptTaprootScriptPath {
13
+ signatures: Signatures;
14
+ controlBlock: Buffer;
15
+ leafVersion: number;
16
+ /** Indicates the level inside the taptree. */
17
+ scriptPathLevel: number;
18
+ }
19
+ /**
20
+ * @return PSBT filled with metatdata as per input params tx, unspents and rootWalletKeys.
21
+ * Unsigned PSBT for taproot input with witnessUtxo
22
+ * Unsigned PSBT for other input with witnessUtxo/nonWitnessUtxo, redeemScript/witnessScript, bip32Derivation
23
+ * Signed PSBT for taproot input with witnessUtxo, tapLeafScript, tapBip32Derivation, tapScriptSig
24
+ * Signed PSBT for other input with witnessUtxo/nonWitnessUtxo, redeemScript/witnessScript, bip32Derivation, partialSig
25
+ */
26
+ export declare function toWalletPsbt(tx: UtxoTransaction<bigint>, unspents: WalletUnspent<bigint>[], rootWalletKeys: RootWalletKeys): UtxoPsbt;
27
+ /**
28
+ * @param psbt
29
+ * @param inputIndex
30
+ * @param signer
31
+ * @param unspent
32
+ * @return signed PSBT with signer's key for unspent
33
+ */
34
+ export declare function signWalletPsbt(psbt: UtxoPsbt, inputIndex: number, signer: BIP32Interface, unspent: WalletUnspent<bigint>): void;
35
+ /**
36
+ * @return psbt metadata are parsed as per below conditions.
37
+ * redeemScript/witnessScript/tapLeafScript matches BitGo.
38
+ * signature and public key count matches BitGo.
39
+ * P2SH => scriptType, redeemScript, public keys, signatures.
40
+ * PW2SH => scriptType, witnessScript, public keys, signatures.
41
+ * P2SH-PW2SH => scriptType, redeemScript, witnessScript, public keys, signatures.
42
+ * P2TR => scriptType, pubScript (witnessScript), controlBlock, scriptPathLevel, leafVersion, public keys, signatures.
43
+ * Any unsigned PSBT and without required metadata is returned with undefined.
44
+ */
45
+ export declare function parsePsbtInput(psbt: UtxoPsbt, inputIndex: number): ParsedPsbt2Of3 | ParsedPsbtP2TR | undefined;
46
+ export {};
47
+ //# sourceMappingURL=Psbt.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Psbt.d.ts","sourceRoot":"","sources":["../../../../src/bitgo/wallet/Psbt.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACvC,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAQrD,OAAO,EAAqB,cAAc,EAAE,MAAM,cAAc,CAAC;AAGjE,OAAO,EAAE,cAAc,EAAE,MAAM,OAAO,CAAC;AACvC,OAAO,EAAmB,aAAa,EAAE,MAAM,WAAW,CAAC;AAG3D,OAAO,EAIL,mBAAmB,EACnB,gCAAgC,EAEjC,MAAM,eAAe,CAAC;AAEvB,aAAK,UAAU,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,SAAS,CAAC;AAE1D,MAAM,WAAW,cAAe,SAAQ,mBAAmB;IACzD,UAAU,EAAE,UAAU,CAAC;CACxB;AAED,MAAM,WAAW,cAAe,SAAQ,gCAAgC;IACtE,UAAU,EAAE,UAAU,CAAC;IACvB,YAAY,EAAE,MAAM,CAAC;IACrB,WAAW,EAAE,MAAM,CAAC;IACpB,8CAA8C;IAC9C,eAAe,EAAE,MAAM,CAAC;CACzB;AAkFD;;;;;;GAMG;AACH,wBAAgB,YAAY,CAC1B,EAAE,EAAE,eAAe,CAAC,MAAM,CAAC,EAC3B,QAAQ,EAAE,aAAa,CAAC,MAAM,CAAC,EAAE,EACjC,cAAc,EAAE,cAAc,GAC7B,QAAQ,CASV;AAED;;;;;;GAMG;AACH,wBAAgB,cAAc,CAC5B,IAAI,EAAE,QAAQ,EACd,UAAU,EAAE,MAAM,EAClB,MAAM,EAAE,cAAc,EACtB,OAAO,EAAE,aAAa,CAAC,MAAM,CAAC,GAC7B,IAAI,CAMN;AA6GD;;;;;;;;;GASG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,GAAG,cAAc,GAAG,cAAc,GAAG,SAAS,CAa9G"}
@@ -0,0 +1,232 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parsePsbtInput = exports.signWalletPsbt = exports.toWalletPsbt = void 0;
4
+ const outputScripts_1 = require("../outputScripts");
5
+ const Unspent_1 = require("../Unspent");
6
+ const transaction_1 = require("../transaction");
7
+ const Unspent_2 = require("./Unspent");
8
+ const utils_1 = require("bip174/src/lib/utils");
9
+ const parseInput_1 = require("../parseInput");
10
+ function getTaprootSigners(script, walletKeys) {
11
+ const parsedPublicKeys = parseInput_1.parsePubScript(script, 'p2tr').publicKeys;
12
+ const walletSigners = parsedPublicKeys.map((publicKey) => {
13
+ const index = walletKeys.publicKeys.findIndex((walletPublicKey) => outputScripts_1.toXOnlyPublicKey(walletPublicKey).equals(publicKey));
14
+ if (index >= 0) {
15
+ return { walletKey: walletKeys.triple[index], rootKey: walletKeys.parent.triple[index] };
16
+ }
17
+ throw new Error('Taproot public key is not a wallet public key');
18
+ });
19
+ return [walletSigners[0], walletSigners[1]];
20
+ }
21
+ function updatePsbtInput(psbt, inputIndex, unspent, rootWalletKeys) {
22
+ const signatureCount = psbt.getSignatureCount(inputIndex);
23
+ const scriptType = outputScripts_1.scriptTypeForChain(unspent.chain);
24
+ if (signatureCount === 0 && scriptType === 'p2tr') {
25
+ return;
26
+ }
27
+ const walletKeys = rootWalletKeys.deriveForChainAndIndex(unspent.chain, unspent.index);
28
+ if (scriptType === 'p2tr') {
29
+ const input = psbt.data.inputs[inputIndex];
30
+ if (!Array.isArray(input.tapLeafScript) || input.tapLeafScript.length === 0) {
31
+ throw new Error('Invalid PSBT state. Missing required fields.');
32
+ }
33
+ if (input.tapLeafScript.length > 1) {
34
+ throw new Error('Bitgo only supports a single tap leaf script per input');
35
+ }
36
+ const [signer, cosigner] = getTaprootSigners(input.tapLeafScript[0].script, walletKeys);
37
+ const leafHash = outputScripts_1.getLeafHash({
38
+ publicKeys: walletKeys.publicKeys,
39
+ signer: signer.walletKey.publicKey,
40
+ cosigner: cosigner.walletKey.publicKey,
41
+ });
42
+ psbt.updateInput(inputIndex, {
43
+ tapBip32Derivation: [signer, cosigner].map((walletSigner) => ({
44
+ leafHashes: [leafHash],
45
+ pubkey: outputScripts_1.toXOnlyPublicKey(walletSigner.walletKey.publicKey),
46
+ path: rootWalletKeys.getDerivationPath(walletSigner.rootKey, unspent.chain, unspent.index),
47
+ masterFingerprint: walletSigner.rootKey.fingerprint,
48
+ })),
49
+ });
50
+ }
51
+ else {
52
+ if (signatureCount === 0) {
53
+ const { witnessScript, redeemScript } = outputScripts_1.createOutputScript2of3(walletKeys.publicKeys, scriptType);
54
+ if (witnessScript && psbt.data.inputs[inputIndex].witnessScript === undefined) {
55
+ psbt.updateInput(inputIndex, { witnessScript });
56
+ }
57
+ if (redeemScript && psbt.data.inputs[inputIndex].redeemScript === undefined) {
58
+ psbt.updateInput(inputIndex, { redeemScript });
59
+ }
60
+ }
61
+ psbt.updateInput(inputIndex, {
62
+ bip32Derivation: [0, 1, 2].map((idx) => ({
63
+ pubkey: walletKeys.triple[idx].publicKey,
64
+ path: walletKeys.paths[idx],
65
+ masterFingerprint: rootWalletKeys.triple[idx].fingerprint,
66
+ })),
67
+ });
68
+ }
69
+ }
70
+ /**
71
+ * @return PSBT filled with metatdata as per input params tx, unspents and rootWalletKeys.
72
+ * Unsigned PSBT for taproot input with witnessUtxo
73
+ * Unsigned PSBT for other input with witnessUtxo/nonWitnessUtxo, redeemScript/witnessScript, bip32Derivation
74
+ * Signed PSBT for taproot input with witnessUtxo, tapLeafScript, tapBip32Derivation, tapScriptSig
75
+ * Signed PSBT for other input with witnessUtxo/nonWitnessUtxo, redeemScript/witnessScript, bip32Derivation, partialSig
76
+ */
77
+ function toWalletPsbt(tx, unspents, rootWalletKeys) {
78
+ const prevOutputs = unspents.map((u) => Unspent_1.toPrevOutputWithPrevTx(u, tx.network));
79
+ const psbt = transaction_1.createPsbtFromTransaction(tx, prevOutputs);
80
+ unspents.forEach((u, i) => {
81
+ if (Unspent_2.isWalletUnspent(u) && u.index !== undefined) {
82
+ updatePsbtInput(psbt, i, u, rootWalletKeys);
83
+ }
84
+ });
85
+ return psbt;
86
+ }
87
+ exports.toWalletPsbt = toWalletPsbt;
88
+ /**
89
+ * @param psbt
90
+ * @param inputIndex
91
+ * @param signer
92
+ * @param unspent
93
+ * @return signed PSBT with signer's key for unspent
94
+ */
95
+ function signWalletPsbt(psbt, inputIndex, signer, unspent) {
96
+ if (outputScripts_1.scriptTypeForChain(unspent.chain) === 'p2tr') {
97
+ psbt.signTaprootInputHD(inputIndex, signer);
98
+ }
99
+ else {
100
+ psbt.signInputHD(inputIndex, signer);
101
+ }
102
+ }
103
+ exports.signWalletPsbt = signWalletPsbt;
104
+ function classifyScriptType(input) {
105
+ let scriptType;
106
+ if (Buffer.isBuffer(input.redeemScript) && Buffer.isBuffer(input.witnessScript)) {
107
+ scriptType = 'p2shP2wsh';
108
+ }
109
+ else if (Buffer.isBuffer(input.redeemScript)) {
110
+ scriptType = 'p2sh';
111
+ }
112
+ else if (Buffer.isBuffer(input.witnessScript)) {
113
+ scriptType = 'p2wsh';
114
+ }
115
+ if (Array.isArray(input.tapLeafScript) && input.tapLeafScript.length > 0) {
116
+ if (scriptType) {
117
+ throw new Error(`Found both ${scriptType} and p2tr PSBT metadata.`);
118
+ }
119
+ if (input.tapLeafScript.length > 1) {
120
+ throw new Error('Bitgo only supports a single tap leaf script per input.');
121
+ }
122
+ scriptType = 'p2tr';
123
+ }
124
+ return scriptType;
125
+ }
126
+ function parseSignatures(input, scriptType) {
127
+ const validate = (sig) => {
128
+ if (Buffer.isBuffer(sig)) {
129
+ return sig;
130
+ }
131
+ throw new Error('Invalid signature type');
132
+ };
133
+ if (scriptType === 'p2tr') {
134
+ if (input.partialSig && input.partialSig.length > 0) {
135
+ throw new Error('Invalid PSBT signature state');
136
+ }
137
+ if (!input.tapScriptSig || input.tapScriptSig.length === 0) {
138
+ return undefined;
139
+ }
140
+ if (input.tapScriptSig.length > 2) {
141
+ throw new Error('unexpected signature count');
142
+ }
143
+ return input.tapScriptSig.length === 1
144
+ ? [validate(input.tapScriptSig[0].signature)]
145
+ : [validate(input.tapScriptSig[0].signature), validate(input.tapScriptSig[1].signature)];
146
+ }
147
+ if (input.tapScriptSig && input.tapScriptSig.length > 0) {
148
+ throw new Error('Invalid PSBT signature state');
149
+ }
150
+ if (!input.partialSig || input.partialSig.length === 0) {
151
+ return undefined;
152
+ }
153
+ if (input.partialSig.length > 2) {
154
+ throw new Error('unexpected signature count');
155
+ }
156
+ return input.partialSig.length === 1
157
+ ? [validate(input.partialSig[0].signature)]
158
+ : [validate(input.partialSig[0].signature), validate(input.partialSig[1].signature)];
159
+ }
160
+ function parseScript(input, scriptType) {
161
+ let pubScript;
162
+ if (scriptType === 'p2sh') {
163
+ pubScript = input.redeemScript;
164
+ }
165
+ else if (scriptType === 'p2wsh' || scriptType === 'p2shP2wsh') {
166
+ pubScript = input.witnessScript;
167
+ }
168
+ else {
169
+ pubScript = input.tapLeafScript ? input.tapLeafScript[0].script : undefined;
170
+ }
171
+ if (!pubScript) {
172
+ throw new Error(`Invalid PSBT state for ${scriptType}. Missing required fields.`);
173
+ }
174
+ return parseInput_1.parsePubScript(pubScript, scriptType);
175
+ }
176
+ function parseInputMetadata(input, scriptType) {
177
+ const parsedPubScript = parseScript(input, scriptType);
178
+ const signatures = parseSignatures(input, scriptType);
179
+ if (parsedPubScript.scriptType === 'p2tr') {
180
+ if (!input.tapLeafScript) {
181
+ throw new Error('Invalid PSBT state for p2tr. Missing required fields.');
182
+ }
183
+ const controlBlock = input.tapLeafScript[0].controlBlock;
184
+ if (!parseInput_1.isValidControlBock(controlBlock)) {
185
+ throw new Error('Invalid PSBT p2tr script path controlBlock.');
186
+ }
187
+ const scriptPathLevel = parseInput_1.calculateScriptPathLevel(controlBlock);
188
+ const leafVersion = parseInput_1.getScriptPathLevel(controlBlock);
189
+ return {
190
+ ...parsedPubScript,
191
+ signatures,
192
+ controlBlock,
193
+ scriptPathLevel,
194
+ leafVersion,
195
+ };
196
+ }
197
+ else {
198
+ if (parsedPubScript.scriptType === 'p2shP2wsh') {
199
+ parsedPubScript.redeemScript = input.redeemScript;
200
+ }
201
+ return {
202
+ ...parsedPubScript,
203
+ signatures,
204
+ };
205
+ }
206
+ }
207
+ /**
208
+ * @return psbt metadata are parsed as per below conditions.
209
+ * redeemScript/witnessScript/tapLeafScript matches BitGo.
210
+ * signature and public key count matches BitGo.
211
+ * P2SH => scriptType, redeemScript, public keys, signatures.
212
+ * PW2SH => scriptType, witnessScript, public keys, signatures.
213
+ * P2SH-PW2SH => scriptType, redeemScript, witnessScript, public keys, signatures.
214
+ * P2TR => scriptType, pubScript (witnessScript), controlBlock, scriptPathLevel, leafVersion, public keys, signatures.
215
+ * Any unsigned PSBT and without required metadata is returned with undefined.
216
+ */
217
+ function parsePsbtInput(psbt, inputIndex) {
218
+ const input = utils_1.checkForInput(psbt.data.inputs, inputIndex);
219
+ if (psbt.isInputFinalized(inputIndex)) {
220
+ throw new Error('Finalized PSBT parsing is not supported');
221
+ }
222
+ const scriptType = classifyScriptType(input);
223
+ if (!scriptType) {
224
+ if (psbt.getSignatureCount(inputIndex) > 0) {
225
+ throw new Error('Invalid PSBT state. Signatures found without scripts.');
226
+ }
227
+ return undefined;
228
+ }
229
+ return parseInputMetadata(input, scriptType);
230
+ }
231
+ exports.parsePsbtInput = parsePsbtInput;
232
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiUHNidC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uLy4uL3NyYy9iaXRnby93YWxsZXQvUHNidC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7QUFFQSxvREFNMEI7QUFFMUIsd0NBQW9EO0FBQ3BELGdEQUEyRDtBQUUzRCx1Q0FBMkQ7QUFDM0QsZ0RBQXFEO0FBRXJELDhDQU91QjtBQXFCdkIsU0FBUyxpQkFBaUIsQ0FBQyxNQUFjLEVBQUUsVUFBNkI7SUFDdEUsTUFBTSxnQkFBZ0IsR0FBRywyQkFBYyxDQUFDLE1BQU0sRUFBRSxNQUFNLENBQUMsQ0FBQyxVQUFVLENBQUM7SUFDbkUsTUFBTSxhQUFhLEdBQUcsZ0JBQWdCLENBQUMsR0FBRyxDQUFDLENBQUMsU0FBUyxFQUFFLEVBQUU7UUFDdkQsTUFBTSxLQUFLLEdBQUcsVUFBVSxDQUFDLFVBQVUsQ0FBQyxTQUFTLENBQUMsQ0FBQyxlQUFlLEVBQUUsRUFBRSxDQUNoRSxnQ0FBZ0IsQ0FBQyxlQUFlLENBQUMsQ0FBQyxNQUFNLENBQUMsU0FBUyxDQUFDLENBQ3BELENBQUM7UUFDRixJQUFJLEtBQUssSUFBSSxDQUFDLEVBQUU7WUFDZCxPQUFPLEVBQUUsU0FBUyxFQUFFLFVBQVUsQ0FBQyxNQUFNLENBQUMsS0FBSyxDQUFDLEVBQUUsT0FBTyxFQUFFLFVBQVUsQ0FBQyxNQUFNLENBQUMsTUFBTSxDQUFDLEtBQUssQ0FBQyxFQU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