@btc-vision/transaction 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 (270) hide show
  1. package/.babelrc +7 -0
  2. package/.gitattributes +2 -0
  3. package/.github/workflows/node.js.yml +22 -0
  4. package/.idea/codeStyles/Project.xml +56 -0
  5. package/.idea/codeStyles/codeStyleConfig.xml +5 -0
  6. package/.idea/inspectionProfiles/Project_Default.xml +6 -0
  7. package/.idea/jsLibraryMappings.xml +6 -0
  8. package/.idea/modules.xml +8 -0
  9. package/.idea/prettier.xml +6 -0
  10. package/.idea/transaction.iml +16 -0
  11. package/.idea/vcs.xml +6 -0
  12. package/.prettierrc.json +12 -0
  13. package/CONTRIBUTING.md +71 -0
  14. package/LICENSE.md +16 -0
  15. package/README.md +213 -0
  16. package/browser/873e754d6c7c6e9361f1.module.wasm +0 -0
  17. package/browser/_version.d.ts +1 -0
  18. package/browser/bytecode/Compressor.d.ts +6 -0
  19. package/browser/crypto/crypto-browser.d.ts +40 -0
  20. package/browser/crypto/crypto.d.ts +2 -0
  21. package/browser/generators/Generator.d.ts +12 -0
  22. package/browser/generators/builders/CalldataGenerator.d.ts +8 -0
  23. package/browser/generators/builders/DeploymentGenerator.d.ts +7 -0
  24. package/browser/index.d.ts +3 -0
  25. package/browser/index.js +2 -0
  26. package/browser/index.js.LICENSE.txt +23 -0
  27. package/browser/keypair/EcKeyPair.d.ts +24 -0
  28. package/browser/keypair/Wallet.d.ts +16 -0
  29. package/browser/keypair/interfaces/IWallet.d.ts +5 -0
  30. package/browser/metadata/ContractBaseMetadata.d.ts +9 -0
  31. package/browser/metadata/ContractMetadataManager.d.ts +1 -0
  32. package/browser/metadata/contracts/wBTC.d.ts +12 -0
  33. package/browser/network/NetworkInformation.d.ts +6 -0
  34. package/browser/opnet.d.ts +22 -0
  35. package/browser/scripts/Regtest.d.ts +2 -0
  36. package/browser/scripts/test.d.ts +1 -0
  37. package/browser/signer/TweakedSigner.d.ts +9 -0
  38. package/browser/transaction/TransactionBuilder.d.ts +1 -0
  39. package/browser/transaction/TransactionFactory.d.ts +4 -0
  40. package/browser/transaction/builders/FundingTransaction.d.ts +11 -0
  41. package/browser/transaction/builders/InteractionTransaction.d.ts +47 -0
  42. package/browser/transaction/builders/TransactionBuilder.d.ts +64 -0
  43. package/browser/transaction/enums/TransactionType.d.ts +7 -0
  44. package/browser/transaction/interfaces/ITransactionParameters.d.ts +33 -0
  45. package/browser/transaction/interfaces/Tap.d.ts +22 -0
  46. package/browser/utils/BitcoinUtils.d.ts +5 -0
  47. package/browser/utxo/UTXOManager.d.ts +7 -0
  48. package/browser/utxo/interfaces/IUTXO.d.ts +19 -0
  49. package/build/Utils.d.ts +0 -0
  50. package/build/Utils.js +1 -0
  51. package/build/_version.d.ts +1 -0
  52. package/build/_version.js +1 -0
  53. package/build/bytecode/Compressor.d.ts +6 -0
  54. package/build/bytecode/Compressor.js +13 -0
  55. package/build/contracts/ContractMetadataManager.d.ts +0 -0
  56. package/build/contracts/ContractMetadataManager.js +1 -0
  57. package/build/crypto/crypto.d.ts +2 -0
  58. package/build/crypto/crypto.js +1 -0
  59. package/build/generators/Generator.d.ts +12 -0
  60. package/build/generators/Generator.js +25 -0
  61. package/build/generators/builders/CalldataGenerator.d.ts +8 -0
  62. package/build/generators/builders/CalldataGenerator.js +79 -0
  63. package/build/generators/builders/DeploymentGenerator.d.ts +7 -0
  64. package/build/generators/builders/DeploymentGenerator.js +38 -0
  65. package/build/index.d.ts +3 -0
  66. package/build/index.js +3 -0
  67. package/build/keypair/EcKeyPair.d.ts +24 -0
  68. package/build/keypair/EcKeyPair.js +107 -0
  69. package/build/keypair/Wallet.d.ts +16 -0
  70. package/build/keypair/Wallet.js +30 -0
  71. package/build/keypair/interfaces/GeneratedWallet.d.ts +5 -0
  72. package/build/keypair/interfaces/GeneratedWallet.js +1 -0
  73. package/build/keypair/interfaces/IWallet.d.ts +5 -0
  74. package/build/keypair/interfaces/IWallet.js +1 -0
  75. package/build/metadata/ContractBaseMetadata.d.ts +9 -0
  76. package/build/metadata/ContractBaseMetadata.js +13 -0
  77. package/build/metadata/ContractMetadataManager.d.ts +1 -0
  78. package/build/metadata/ContractMetadataManager.js +9 -0
  79. package/build/metadata/contracts/ContractBase.d.ts +9 -0
  80. package/build/metadata/contracts/ContractBase.js +13 -0
  81. package/build/metadata/contracts/ContractBaseMetadata.d.ts +9 -0
  82. package/build/metadata/contracts/ContractBaseMetadata.js +13 -0
  83. package/build/metadata/contracts/ContractMetadataManager.d.ts +0 -0
  84. package/build/metadata/contracts/ContractMetadataManager.js +1 -0
  85. package/build/metadata/contracts/wBTC.d.ts +12 -0
  86. package/build/metadata/contracts/wBTC.js +26 -0
  87. package/build/network/NetworkConverter.d.ts +0 -0
  88. package/build/network/NetworkConverter.js +14 -0
  89. package/build/network/NetworkInformation.d.ts +6 -0
  90. package/build/network/NetworkInformation.js +1 -0
  91. package/build/opnet.d.ts +22 -0
  92. package/build/opnet.js +22 -0
  93. package/build/scripts/Regtest.d.ts +2 -0
  94. package/build/scripts/Regtest.js +15 -0
  95. package/build/scripts/test.d.ts +1 -0
  96. package/build/scripts/test.js +74 -0
  97. package/build/signer/Regtest.d.ts +2 -0
  98. package/build/signer/Regtest.js +15 -0
  99. package/build/signer/TweakedSigner.d.ts +9 -0
  100. package/build/signer/TweakedSigner.js +22 -0
  101. package/build/transaction/TransactionBuilder.d.ts +60 -0
  102. package/build/transaction/TransactionBuilder.js +244 -0
  103. package/build/transaction/TransactionFactory.d.ts +4 -0
  104. package/build/transaction/TransactionFactory.js +32 -0
  105. package/build/transaction/builders/FundingTransaction.d.ts +11 -0
  106. package/build/transaction/builders/FundingTransaction.js +23 -0
  107. package/build/transaction/builders/GenericTransaction.d.ts +11 -0
  108. package/build/transaction/builders/GenericTransaction.js +23 -0
  109. package/build/transaction/builders/InteractionTransaction.d.ts +47 -0
  110. package/build/transaction/builders/InteractionTransaction.js +219 -0
  111. package/build/transaction/builders/TransactionBuilder.d.ts +64 -0
  112. package/build/transaction/builders/TransactionBuilder.js +288 -0
  113. package/build/transaction/enums/TransactionType.d.ts +7 -0
  114. package/build/transaction/enums/TransactionType.js +8 -0
  115. package/build/transaction/interfaces/ITransactionParameters.d.ts +33 -0
  116. package/build/transaction/interfaces/ITransactionParameters.js +1 -0
  117. package/build/transaction/interfaces/ITransactions.d.ts +32 -0
  118. package/build/transaction/interfaces/ITransactions.js +1 -0
  119. package/build/transaction/interfaces/Tap.d.ts +22 -0
  120. package/build/transaction/interfaces/Tap.js +1 -0
  121. package/build/utils/BitcoinUtils.d.ts +5 -0
  122. package/build/utils/BitcoinUtils.js +9 -0
  123. package/build/utxo/IUTXO.d.ts +0 -0
  124. package/build/utxo/IUTXO.js +1 -0
  125. package/build/utxo/UTXOManager.d.ts +7 -0
  126. package/build/utxo/UTXOManager.js +47 -0
  127. package/build/utxo/interfaces/IUTXO.d.ts +19 -0
  128. package/build/utxo/interfaces/IUTXO.js +1 -0
  129. package/cjs/_version.d.ts +1 -0
  130. package/cjs/_version.js +4 -0
  131. package/cjs/bytecode/Compressor.d.ts +6 -0
  132. package/cjs/bytecode/Compressor.js +20 -0
  133. package/cjs/crypto/crypto.d.ts +2 -0
  134. package/cjs/crypto/crypto.js +8 -0
  135. package/cjs/generators/Generator.d.ts +13 -0
  136. package/cjs/generators/Generator.js +31 -0
  137. package/cjs/generators/builders/CalldataGenerator.d.ts +8 -0
  138. package/cjs/generators/builders/CalldataGenerator.js +83 -0
  139. package/cjs/generators/builders/DeploymentGenerator.d.ts +7 -0
  140. package/cjs/generators/builders/DeploymentGenerator.js +42 -0
  141. package/cjs/index.d.ts +3 -0
  142. package/cjs/index.js +32 -0
  143. package/cjs/keypair/EcKeyPair.d.ts +24 -0
  144. package/cjs/keypair/EcKeyPair.js +137 -0
  145. package/cjs/keypair/Wallet.d.ts +16 -0
  146. package/cjs/keypair/Wallet.js +34 -0
  147. package/cjs/keypair/interfaces/IWallet.d.ts +5 -0
  148. package/cjs/keypair/interfaces/IWallet.js +2 -0
  149. package/cjs/metadata/ContractBaseMetadata.d.ts +9 -0
  150. package/cjs/metadata/ContractBaseMetadata.js +17 -0
  151. package/cjs/metadata/ContractMetadataManager.d.ts +0 -0
  152. package/cjs/metadata/ContractMetadataManager.js +1 -0
  153. package/cjs/metadata/contracts/wBTC.d.ts +12 -0
  154. package/cjs/metadata/contracts/wBTC.js +30 -0
  155. package/cjs/network/NetworkInformation.d.ts +6 -0
  156. package/cjs/network/NetworkInformation.js +2 -0
  157. package/cjs/opnet.d.ts +1 -0
  158. package/cjs/opnet.js +5 -0
  159. package/cjs/scripts/Regtest.d.ts +2 -0
  160. package/cjs/scripts/Regtest.js +18 -0
  161. package/cjs/scripts/test.d.ts +1 -0
  162. package/cjs/scripts/test.js +44 -0
  163. package/cjs/signer/TweakedSigner.d.ts +9 -0
  164. package/cjs/signer/TweakedSigner.js +49 -0
  165. package/cjs/transaction/TransactionBuilder.d.ts +0 -0
  166. package/cjs/transaction/TransactionBuilder.js +1 -0
  167. package/cjs/transaction/TransactionFactory.d.ts +4 -0
  168. package/cjs/transaction/TransactionFactory.js +33 -0
  169. package/cjs/transaction/builders/FundingTransaction.d.ts +11 -0
  170. package/cjs/transaction/builders/FundingTransaction.js +27 -0
  171. package/cjs/transaction/builders/InteractionTransaction.d.ts +45 -0
  172. package/cjs/transaction/builders/InteractionTransaction.js +214 -0
  173. package/cjs/transaction/builders/TransactionBuilder.d.ts +64 -0
  174. package/cjs/transaction/builders/TransactionBuilder.js +304 -0
  175. package/cjs/transaction/enums/TransactionType.d.ts +7 -0
  176. package/cjs/transaction/enums/TransactionType.js +11 -0
  177. package/cjs/transaction/interfaces/ITransactionParameters.d.ts +32 -0
  178. package/cjs/transaction/interfaces/ITransactionParameters.js +2 -0
  179. package/cjs/transaction/interfaces/Tap.d.ts +22 -0
  180. package/cjs/transaction/interfaces/Tap.js +2 -0
  181. package/cjs/utils/BitcoinUtils.d.ts +5 -0
  182. package/cjs/utils/BitcoinUtils.js +13 -0
  183. package/cjs/utxo/UTXOManager.d.ts +7 -0
  184. package/cjs/utxo/UTXOManager.js +51 -0
  185. package/cjs/utxo/interfaces/IUTXO.d.ts +19 -0
  186. package/cjs/utxo/interfaces/IUTXO.js +2 -0
  187. package/docs/.nojekyll +1 -0
  188. package/docs/assets/highlight.css +92 -0
  189. package/docs/assets/icons.js +15 -0
  190. package/docs/assets/icons.svg +1 -0
  191. package/docs/assets/main.js +59 -0
  192. package/docs/assets/material-style.css +247 -0
  193. package/docs/assets/navigation.js +1 -0
  194. package/docs/assets/search.js +1 -0
  195. package/docs/assets/style.css +1412 -0
  196. package/docs/classes/BitcoinUtils.html +183 -0
  197. package/docs/classes/CalldataGenerator.html +211 -0
  198. package/docs/classes/Compressor.html +185 -0
  199. package/docs/classes/ContractBaseMetadata.html +182 -0
  200. package/docs/classes/DeploymentGenerator.html +200 -0
  201. package/docs/classes/EcKeyPair.html +280 -0
  202. package/docs/classes/FundingTransaction.html +293 -0
  203. package/docs/classes/Generator.html +199 -0
  204. package/docs/classes/InteractionTransaction.html +365 -0
  205. package/docs/classes/TransactionBuilder.html +303 -0
  206. package/docs/classes/TransactionFactory.html +180 -0
  207. package/docs/classes/TweakedSigner.html +181 -0
  208. package/docs/classes/UTXOManager.html +187 -0
  209. package/docs/classes/Wallet.html +191 -0
  210. package/docs/classes/wBTC.html +189 -0
  211. package/docs/enums/TransactionType.html +179 -0
  212. package/docs/hierarchy.html +174 -0
  213. package/docs/index.html +237 -0
  214. package/docs/interfaces/FetchUTXOParams.html +178 -0
  215. package/docs/interfaces/IFundingTransactionParameters.html +182 -0
  216. package/docs/interfaces/IInteractionParameters.html +185 -0
  217. package/docs/interfaces/ITransactionDataContractDeployment.html +184 -0
  218. package/docs/interfaces/ITransactionDataContractInteractionWrap.html +186 -0
  219. package/docs/interfaces/ITransactionParameters.html +181 -0
  220. package/docs/interfaces/IWallet.html +181 -0
  221. package/docs/interfaces/NetworkInformation.html +176 -0
  222. package/docs/interfaces/PsbtInputExtended.html +194 -0
  223. package/docs/interfaces/PsbtOutputExtendedAddress.html +183 -0
  224. package/docs/interfaces/PsbtOutputExtendedScript.html +183 -0
  225. package/docs/interfaces/RawUTXOResponse.html +178 -0
  226. package/docs/interfaces/TapLeafScript.html +177 -0
  227. package/docs/interfaces/TweakSettings.html +179 -0
  228. package/docs/interfaces/UTXO.html +178 -0
  229. package/docs/interfaces/UpdateInput.html +175 -0
  230. package/docs/modules.html +208 -0
  231. package/docs/types/PsbtOutputExtended.html +174 -0
  232. package/docs/variables/version.html +174 -0
  233. package/gulpfile.js +152 -0
  234. package/jest.config.ts +52 -0
  235. package/package.json +116 -0
  236. package/src/_version.ts +1 -0
  237. package/src/bytecode/Compressor.ts +27 -0
  238. package/src/crypto/crypto-browser.js +71 -0
  239. package/src/crypto/crypto.ts +1 -0
  240. package/src/generators/Generator.ts +75 -0
  241. package/src/generators/builders/CalldataGenerator.ts +143 -0
  242. package/src/generators/builders/DeploymentGenerator.ts +63 -0
  243. package/src/index.ts +4 -0
  244. package/src/keypair/EcKeyPair.ts +265 -0
  245. package/src/keypair/Wallet.ts +75 -0
  246. package/src/keypair/interfaces/IWallet.ts +19 -0
  247. package/src/metadata/ContractBaseMetadata.ts +23 -0
  248. package/src/metadata/contracts/wBTC.ts +44 -0
  249. package/src/network/NetworkInformation.ts +7 -0
  250. package/src/opnet.ts +42 -0
  251. package/src/scripts/Regtest.ts +19 -0
  252. package/src/scripts/test.ts +98 -0
  253. package/src/signer/TweakedSigner.ts +57 -0
  254. package/src/transaction/TransactionFactory.ts +57 -0
  255. package/src/transaction/builders/FundingTransaction.ts +36 -0
  256. package/src/transaction/builders/InteractionTransaction.ts +439 -0
  257. package/src/transaction/builders/TransactionBuilder.ts +603 -0
  258. package/src/transaction/enums/TransactionType.ts +7 -0
  259. package/src/transaction/interfaces/ITransactionParameters.ts +41 -0
  260. package/src/transaction/interfaces/Tap.ts +26 -0
  261. package/src/utils/BitcoinUtils.ts +24 -0
  262. package/src/utxo/UTXOManager.ts +67 -0
  263. package/src/utxo/interfaces/IUTXO.ts +22 -0
  264. package/tests/TransactionBuilder.test.ts +58 -0
  265. package/tests/contracts/wbtc.wasm +0 -0
  266. package/tsconfig.base.json +43 -0
  267. package/tsconfig.cjs.json +9 -0
  268. package/tsconfig.json +8 -0
  269. package/tsconfig.webpack.json +11 -0
  270. package/webpack.config.js +77 -0
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TransactionFactory = void 0;
4
+ const FundingTransaction_js_1 = require("./builders/FundingTransaction.js");
5
+ const InteractionTransaction_js_1 = require("./builders/InteractionTransaction.js");
6
+ class TransactionFactory {
7
+ signInteraction(interactionParameters) {
8
+ const preTransaction = new InteractionTransaction_js_1.InteractionTransaction(interactionParameters);
9
+ preTransaction.signTransaction();
10
+ const parameters = preTransaction.getFundingTransactionParameters();
11
+ console.log('funding transaction parameters:', parameters);
12
+ const fundingTransaction = new FundingTransaction_js_1.FundingTransaction(parameters);
13
+ const signedTransaction = fundingTransaction.signTransaction();
14
+ if (!signedTransaction) {
15
+ throw new Error('Could not sign funding transaction.');
16
+ }
17
+ const out = signedTransaction.outs[0];
18
+ const newUtxo = {
19
+ transactionId: signedTransaction.getId(),
20
+ outputIndex: 0,
21
+ scriptPubKey: {
22
+ hex: out.script.toString('hex'),
23
+ },
24
+ value: BigInt(out.value),
25
+ };
26
+ interactionParameters.utxos = [newUtxo];
27
+ console.log(`new utxo`, newUtxo);
28
+ const finalTransaction = new InteractionTransaction_js_1.InteractionTransaction(interactionParameters);
29
+ const outTx = finalTransaction.signTransaction();
30
+ return [signedTransaction.toHex(), outTx.toHex()];
31
+ }
32
+ }
33
+ exports.TransactionFactory = TransactionFactory;
@@ -0,0 +1,11 @@
1
+ import { TransactionType } from '../enums/TransactionType.js';
2
+ import { IFundingTransactionParameters } from '../interfaces/ITransactionParameters.js';
3
+ import { Signer } from 'bitcoinjs-lib';
4
+ import { TransactionBuilder } from './TransactionBuilder.js';
5
+ export declare class FundingTransaction extends TransactionBuilder<TransactionType.FUNDING> {
6
+ readonly type: TransactionType.FUNDING;
7
+ protected childTransactionRequiredFees: bigint;
8
+ constructor(parameters: IFundingTransactionParameters);
9
+ protected buildTransaction(): void;
10
+ protected getSignerKey(): Signer;
11
+ }
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FundingTransaction = void 0;
4
+ const TransactionType_js_1 = require("../enums/TransactionType.js");
5
+ const TransactionBuilder_js_1 = require("./TransactionBuilder.js");
6
+ class FundingTransaction extends TransactionBuilder_js_1.TransactionBuilder {
7
+ type = TransactionType_js_1.TransactionType.FUNDING;
8
+ childTransactionRequiredFees;
9
+ constructor(parameters) {
10
+ super(parameters);
11
+ this.childTransactionRequiredFees = parameters.childTransactionRequiredFees;
12
+ this.internalInit();
13
+ }
14
+ buildTransaction() {
15
+ this.addInputsFromUTXO();
16
+ const amountSpent = this.getTransactionOPNetFee() + this.childTransactionRequiredFees;
17
+ this.setFeeOutput({
18
+ value: Number(amountSpent),
19
+ address: this.to,
20
+ });
21
+ this.addRefundOutput(amountSpent);
22
+ }
23
+ getSignerKey() {
24
+ return this.signer;
25
+ }
26
+ }
27
+ exports.FundingTransaction = FundingTransaction;
@@ -0,0 +1,45 @@
1
+ /// <reference types="node" />
2
+ import { PsbtInput } from 'bip174/src/lib/interfaces.js';
3
+ import { Payment, Psbt, Signer } from 'bitcoinjs-lib';
4
+ import { Taptree } from 'bitcoinjs-lib/src/types.js';
5
+ import { ECPairInterface } from 'ecpair';
6
+ import { TransactionBuilder } from './TransactionBuilder.js';
7
+ import { TransactionType } from '../enums/TransactionType.js';
8
+ import { TapLeafScript } from '../interfaces/Tap.js';
9
+ import { CalldataGenerator } from '../../generators/builders/CalldataGenerator.js';
10
+ import { IInteractionParameters } from '../interfaces/ITransactionParameters.js';
11
+ export declare class InteractionTransaction extends TransactionBuilder<TransactionType.INTERACTION> {
12
+ type: TransactionType.INTERACTION;
13
+ readonly randomBytes: Buffer;
14
+ protected targetScriptRedeem: Payment | null;
15
+ protected leftOverFundsScriptRedeem: Payment | null;
16
+ protected readonly compiledTargetScript: Buffer;
17
+ protected readonly scriptTree: Taptree;
18
+ protected tapLeafScript: TapLeafScript | null;
19
+ protected calldataGenerator: CalldataGenerator;
20
+ protected readonly calldata: Buffer;
21
+ protected readonly contractSecret: Buffer;
22
+ protected tweakedSigner?: Signer;
23
+ protected readonly scriptSigner: Signer;
24
+ protected readonly interactionPubKeys: Buffer[];
25
+ protected readonly minimumSignatures: number;
26
+ constructor(parameters: IInteractionParameters);
27
+ protected generateSecret(): Buffer;
28
+ protected tweakSigner(): void;
29
+ protected scriptSignerXOnlyPubKey(): Buffer;
30
+ protected generateKeyPairFromSeed(): ECPairInterface;
31
+ protected addInputsFromUTXO(): void;
32
+ protected buildTransaction(): void;
33
+ protected signInputs(transaction: Psbt): void;
34
+ protected getSignerKey(): Signer;
35
+ protected generateScriptAddress(): Payment;
36
+ protected generateTapData(): Payment;
37
+ protected getScriptSolution(input: PsbtInput): Buffer[];
38
+ private getPubKeys;
39
+ private customFinalizer;
40
+ private getTweakerHash;
41
+ private getTweakedSigner;
42
+ private generateRedeemScripts;
43
+ private getLeafScript;
44
+ private getScriptTree;
45
+ }
@@ -0,0 +1,214 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.InteractionTransaction = void 0;
4
+ const bitcoinjs_lib_1 = require("bitcoinjs-lib");
5
+ const TransactionBuilder_js_1 = require("./TransactionBuilder.js");
6
+ const TransactionType_js_1 = require("../enums/TransactionType.js");
7
+ const CalldataGenerator_js_1 = require("../../generators/builders/CalldataGenerator.js");
8
+ const Compressor_js_1 = require("../../bytecode/Compressor.js");
9
+ const EcKeyPair_js_1 = require("../../keypair/EcKeyPair.js");
10
+ const BitcoinUtils_js_1 = require("../../utils/BitcoinUtils.js");
11
+ const bip371_js_1 = require("bitcoinjs-lib/src/psbt/bip371.js");
12
+ const TweakedSigner_js_1 = require("../../signer/TweakedSigner.js");
13
+ class InteractionTransaction extends TransactionBuilder_js_1.TransactionBuilder {
14
+ type = TransactionType_js_1.TransactionType.INTERACTION;
15
+ randomBytes = BitcoinUtils_js_1.BitcoinUtils.rndBytes();
16
+ targetScriptRedeem = null;
17
+ leftOverFundsScriptRedeem = null;
18
+ compiledTargetScript;
19
+ scriptTree;
20
+ tapLeafScript = null;
21
+ calldataGenerator;
22
+ calldata;
23
+ contractSecret = this.generateSecret();
24
+ tweakedSigner;
25
+ scriptSigner = this.generateKeyPairFromSeed();
26
+ interactionPubKeys;
27
+ minimumSignatures;
28
+ constructor(parameters) {
29
+ super(parameters);
30
+ if (!parameters.calldata) {
31
+ throw new Error('Calldata is required');
32
+ }
33
+ this.calldata = Compressor_js_1.Compressor.compress(parameters.calldata);
34
+ this.interactionPubKeys = parameters.pubKeys || [];
35
+ this.minimumSignatures = parameters.minimumSignatures || 0;
36
+ this.calldataGenerator = new CalldataGenerator_js_1.CalldataGenerator(this.internalPubKeyToXOnly(), this.scriptSignerXOnlyPubKey(), this.network);
37
+ this.compiledTargetScript = this.calldataGenerator.compile(this.calldata, this.contractSecret, this.interactionPubKeys, this.minimumSignatures);
38
+ this.scriptTree = this.getScriptTree();
39
+ this.internalInit();
40
+ }
41
+ generateSecret() {
42
+ return bitcoinjs_lib_1.address.fromBech32(this.to).data;
43
+ }
44
+ tweakSigner() {
45
+ this.tweakedSigner = this.getTweakedSigner();
46
+ }
47
+ scriptSignerXOnlyPubKey() {
48
+ return (0, bip371_js_1.toXOnly)(this.scriptSigner.publicKey);
49
+ }
50
+ generateKeyPairFromSeed() {
51
+ return EcKeyPair_js_1.EcKeyPair.fromSeedKeyPair(this.randomBytes, this.network);
52
+ }
53
+ addInputsFromUTXO() {
54
+ if (!this.tapLeafScript)
55
+ throw new Error('Tap leaf script is required');
56
+ for (let utxo of this.utxos) {
57
+ const input = {
58
+ hash: utxo.transactionId,
59
+ index: utxo.outputIndex,
60
+ witnessUtxo: {
61
+ value: Number(utxo.value),
62
+ script: this.getTapOutput(),
63
+ },
64
+ tapLeafScript: [this.tapLeafScript],
65
+ };
66
+ this.addInput(input);
67
+ }
68
+ }
69
+ buildTransaction() {
70
+ const selectedRedeem = !!this.scriptSigner
71
+ ? this.targetScriptRedeem
72
+ : this.leftOverFundsScriptRedeem;
73
+ if (!selectedRedeem) {
74
+ throw new Error('Left over funds script redeem is required');
75
+ }
76
+ if (!selectedRedeem.redeemVersion) {
77
+ throw new Error('Left over funds script redeem version is required');
78
+ }
79
+ if (!selectedRedeem.output) {
80
+ throw new Error('Left over funds script redeem output is required');
81
+ }
82
+ this.tapLeafScript = {
83
+ leafVersion: selectedRedeem.redeemVersion,
84
+ script: selectedRedeem.output,
85
+ controlBlock: this.getWitness(),
86
+ };
87
+ this.addInputsFromUTXO();
88
+ const amountSpent = this.getTransactionOPNetFee();
89
+ this.addOutput({
90
+ value: Number(amountSpent),
91
+ address: this.to,
92
+ });
93
+ this.addRefundOutput(amountSpent);
94
+ }
95
+ signInputs(transaction) {
96
+ if (!this.scriptSigner) {
97
+ super.signInputs(transaction);
98
+ return;
99
+ }
100
+ transaction.signInput(0, this.scriptSigner);
101
+ transaction.signInput(0, this.getSignerKey());
102
+ transaction.finalizeInput(0, this.customFinalizer);
103
+ }
104
+ getSignerKey() {
105
+ if (this.tweakedSigner) {
106
+ return this.tweakedSigner;
107
+ }
108
+ return this.signer;
109
+ }
110
+ generateScriptAddress() {
111
+ return {
112
+ internalPubkey: this.internalPubKeyToXOnly(),
113
+ network: this.network,
114
+ scriptTree: this.scriptTree,
115
+ };
116
+ }
117
+ generateTapData() {
118
+ const selectedRedeem = !!this.scriptSigner
119
+ ? this.targetScriptRedeem
120
+ : this.leftOverFundsScriptRedeem;
121
+ if (!selectedRedeem) {
122
+ throw new Error('Left over funds script redeem is required');
123
+ }
124
+ if (!this.scriptTree) {
125
+ throw new Error('Script tree is required');
126
+ }
127
+ return {
128
+ internalPubkey: this.internalPubKeyToXOnly(),
129
+ network: this.network,
130
+ scriptTree: this.scriptTree,
131
+ redeem: selectedRedeem,
132
+ };
133
+ }
134
+ getScriptSolution(input) {
135
+ if (!input.tapScriptSig) {
136
+ throw new Error('Tap script signature is required');
137
+ }
138
+ return [
139
+ this.contractSecret,
140
+ this.internalPubKeyToXOnly(),
141
+ input.tapScriptSig[0].signature,
142
+ input.tapScriptSig[1].signature,
143
+ ];
144
+ }
145
+ getPubKeys() {
146
+ const pubkeys = [this.signer.publicKey];
147
+ if (this.scriptSigner) {
148
+ pubkeys.push(this.scriptSigner.publicKey);
149
+ }
150
+ return pubkeys;
151
+ }
152
+ customFinalizer = (_inputIndex, input) => {
153
+ if (!this.tapLeafScript) {
154
+ throw new Error('Tap leaf script is required');
155
+ }
156
+ if (!input.tapScriptSig) {
157
+ throw new Error('Tap script signature is required');
158
+ }
159
+ if (!this.contractSecret) {
160
+ throw new Error('Contract secret is required');
161
+ }
162
+ const scriptSolution = this.getScriptSolution(input);
163
+ const witness = scriptSolution
164
+ .concat(this.tapLeafScript.script)
165
+ .concat(this.tapLeafScript.controlBlock);
166
+ return {
167
+ finalScriptWitness: this.witnessStackToScriptWitness(witness),
168
+ };
169
+ };
170
+ getTweakerHash() {
171
+ return this.tapData?.hash;
172
+ }
173
+ getTweakedSigner(useTweakedHash = false) {
174
+ const settings = {
175
+ network: this.network,
176
+ };
177
+ if (useTweakedHash) {
178
+ settings.tweakHash = this.getTweakerHash();
179
+ }
180
+ return TweakedSigner_js_1.TweakedSigner.tweakSigner(this.signer, settings);
181
+ }
182
+ generateRedeemScripts() {
183
+ this.targetScriptRedeem = {
184
+ pubkeys: this.getPubKeys(),
185
+ output: this.compiledTargetScript,
186
+ redeemVersion: 192,
187
+ };
188
+ this.leftOverFundsScriptRedeem = {
189
+ pubkeys: this.getPubKeys(),
190
+ output: this.getLeafScript(),
191
+ redeemVersion: 192,
192
+ };
193
+ }
194
+ getLeafScript() {
195
+ return InteractionTransaction.LOCK_LEAF_SCRIPT;
196
+ }
197
+ getScriptTree() {
198
+ if (!this.calldata) {
199
+ throw new Error('Calldata is required');
200
+ }
201
+ this.generateRedeemScripts();
202
+ return [
203
+ {
204
+ output: this.compiledTargetScript,
205
+ version: 192,
206
+ },
207
+ {
208
+ output: this.getLeafScript(),
209
+ version: 192,
210
+ },
211
+ ];
212
+ }
213
+ }
214
+ exports.InteractionTransaction = InteractionTransaction;
@@ -0,0 +1,64 @@
1
+ /// <reference types="node" />
2
+ import { Network, Payment, Psbt, Signer, Transaction } from 'bitcoinjs-lib';
3
+ import { PsbtInputExtended, PsbtOutputExtended, UpdateInput } from '../interfaces/Tap.js';
4
+ import { TransactionType } from '../enums/TransactionType.js';
5
+ import { IFundingTransactionParameters, ITransactionParameters } from '../interfaces/ITransactionParameters.js';
6
+ import { Address } from '@btc-vision/bsi-binary';
7
+ import { UTXO } from '../../utxo/interfaces/IUTXO.js';
8
+ import { Logger } from '@btc-vision/logger';
9
+ export declare abstract class TransactionBuilder<T extends TransactionType> extends Logger {
10
+ protected static readonly LOCK_LEAF_SCRIPT: Buffer;
11
+ protected static readonly MINIMUM_DUST: bigint;
12
+ abstract readonly type: T;
13
+ readonly logColor: string;
14
+ transactionFee: bigint;
15
+ protected readonly transaction: Psbt;
16
+ protected readonly inputs: PsbtInputExtended[];
17
+ protected readonly updateInputs: UpdateInput[];
18
+ protected readonly outputs: PsbtOutputExtended[];
19
+ protected feeOutput: PsbtOutputExtended | null;
20
+ protected signed: boolean;
21
+ protected tapData: Payment | null;
22
+ protected scriptData: Payment | null;
23
+ protected totalInputAmount: bigint;
24
+ protected readonly signer: Signer;
25
+ protected readonly network: Network;
26
+ protected readonly feeRate: number;
27
+ protected readonly priorityFee: bigint;
28
+ protected utxos: UTXO[];
29
+ protected to: Address;
30
+ protected from: Address;
31
+ private _maximumFeeRate;
32
+ protected constructor(parameters: ITransactionParameters);
33
+ getFundingTransactionParameters(): IFundingTransactionParameters;
34
+ setDestinationAddress(address: Address): void;
35
+ setMaximumFeeRate(feeRate: number): void;
36
+ signTransaction(): Transaction;
37
+ getTransaction(): Transaction;
38
+ getScriptAddress(): string;
39
+ getTapAddress(): string;
40
+ addInput(input: PsbtInputExtended): void;
41
+ addOutput(output: PsbtOutputExtended): void;
42
+ protected addRefundOutput(amountSpent: bigint): void;
43
+ protected getTransactionOPNetFee(): bigint;
44
+ protected calculateTotalUTXOAmount(): bigint;
45
+ protected calculateTotalVOutAmount(): bigint;
46
+ protected addInputsFromUTXO(): void;
47
+ protected witnessStackToScriptWitness(witness: Buffer[]): Buffer;
48
+ protected internalInit(): void;
49
+ protected abstract buildTransaction(): void;
50
+ protected generateScriptAddress(): Payment;
51
+ protected generateTapData(): Payment;
52
+ protected updateInput(input: UpdateInput): void;
53
+ protected getWitness(): Buffer;
54
+ protected getTapOutput(): Buffer;
55
+ protected getInputs(): PsbtInputExtended[];
56
+ protected getOutputs(): PsbtOutputExtended[];
57
+ protected verifyUTXOValidity(): void;
58
+ protected setFeeOutput(output: PsbtOutputExtended): void;
59
+ protected abstract getSignerKey(): Signer;
60
+ protected internalPubKeyToXOnly(): Buffer;
61
+ protected signInputs(transaction: Psbt): void;
62
+ private internalBuildTransaction;
63
+ private estimateTransactionFees;
64
+ }