@btc-vision/transaction 1.0.7 → 1.0.9

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 (137) hide show
  1. package/Deploy.md +186 -0
  2. package/README.md +7 -0
  3. package/Wrap.md +192 -0
  4. package/browser/_version.d.ts +1 -1
  5. package/browser/index.js +1 -1
  6. package/browser/opnet.d.ts +6 -1
  7. package/browser/tests/deploy.d.ts +1 -0
  8. package/browser/tests/wrap.d.ts +1 -0
  9. package/browser/tests/wrapTest.d.ts +1 -0
  10. package/browser/transaction/TransactionFactory.d.ts +15 -1
  11. package/browser/transaction/builders/DeploymentTransaction.d.ts +40 -0
  12. package/browser/transaction/builders/InteractionTransaction.d.ts +2 -35
  13. package/browser/transaction/builders/SharedInteractionTransaction.d.ts +46 -0
  14. package/browser/transaction/builders/TransactionBuilder.d.ts +8 -1
  15. package/browser/transaction/builders/WrapTransaction.d.ts +27 -0
  16. package/browser/transaction/interfaces/ITransactionParameters.d.ts +16 -12
  17. package/browser/utils/BitcoinUtils.d.ts +1 -0
  18. package/browser/utxo/OPNetLimitedProvider.d.ts +10 -0
  19. package/browser/wbtc/Generate.d.ts +13 -0
  20. package/browser/wbtc/WrappedGenerationParameters.d.ts +11 -0
  21. package/build/_version.d.ts +1 -1
  22. package/build/_version.js +1 -1
  23. package/build/opnet.d.ts +6 -1
  24. package/build/opnet.js +6 -1
  25. package/build/tests/deploy.d.ts +1 -0
  26. package/build/tests/deploy.js +60 -0
  27. package/build/tests/test.js +3 -3
  28. package/build/tests/transfer.js +2 -2
  29. package/build/tests/wrap.d.ts +1 -0
  30. package/build/tests/wrap.js +64 -0
  31. package/build/tests/wrapTest.d.ts +1 -0
  32. package/build/tests/wrapTest.js +64 -0
  33. package/build/tests/wrapTestg.d.ts +1 -0
  34. package/build/tests/wrapTestg.js +66 -0
  35. package/build/transaction/TransactionFactory.d.ts +15 -1
  36. package/build/transaction/TransactionFactory.js +68 -0
  37. package/build/transaction/builders/DeploymentTransaction.d.ts +40 -0
  38. package/build/transaction/builders/DeploymentTransaction.js +216 -0
  39. package/build/transaction/builders/FundingTransaction.js +4 -1
  40. package/build/transaction/builders/InteractionTransaction.d.ts +2 -35
  41. package/build/transaction/builders/InteractionTransaction.js +4 -207
  42. package/build/transaction/builders/SharedInteractionTransaction.d.ts +46 -0
  43. package/build/transaction/builders/SharedInteractionTransaction.js +213 -0
  44. package/build/transaction/builders/TransactionBuilder.d.ts +8 -1
  45. package/build/transaction/builders/TransactionBuilder.js +19 -11
  46. package/build/transaction/builders/WrapTransaction.d.ts +27 -0
  47. package/build/transaction/builders/WrapTransaction.js +132 -0
  48. package/build/transaction/interfaces/ITransactionParameters.d.ts +16 -12
  49. package/build/utils/BitcoinUtils.d.ts +1 -0
  50. package/build/utils/BitcoinUtils.js +8 -1
  51. package/build/utxo/OPNetLimitedProvider.d.ts +10 -0
  52. package/build/utxo/OPNetLimitedProvider.js +85 -0
  53. package/build/utxo/OPNetUtils.d.ts +7 -0
  54. package/build/utxo/OPNetUtils.js +47 -0
  55. package/build/verification/TapscriptVerificator.js +3 -3
  56. package/build/wbtc/Generate.d.ts +13 -0
  57. package/build/wbtc/Generate.js +1 -0
  58. package/build/wbtc/WrappedGenerationParameters.d.ts +11 -0
  59. package/build/wbtc/WrappedGenerationParameters.js +16 -0
  60. package/bytecode/contract.wasm +0 -0
  61. package/docs/assets/navigation.js +1 -1
  62. package/docs/assets/search.js +1 -1
  63. package/docs/classes/AddressGenerator.html +2 -2
  64. package/docs/classes/BitcoinUtils.html +7 -3
  65. package/docs/classes/CalldataGenerator.html +10 -10
  66. package/docs/classes/Compressor.html +4 -4
  67. package/docs/classes/ContractBaseMetadata.html +4 -4
  68. package/docs/classes/DeploymentGenerator.html +9 -9
  69. package/docs/classes/DeploymentTransaction.html +368 -0
  70. package/docs/classes/EcKeyPair.html +16 -16
  71. package/docs/classes/FundingTransaction.html +50 -47
  72. package/docs/classes/Generator.html +9 -9
  73. package/docs/classes/InteractionTransaction.html +89 -109
  74. package/docs/classes/OPNetLimitedProvider.html +197 -0
  75. package/docs/classes/SharedInteractionTransaction.html +392 -0
  76. package/docs/classes/TapscriptVerificator.html +2 -2
  77. package/docs/classes/TransactionBuilder.html +53 -50
  78. package/docs/classes/TransactionFactory.html +11 -4
  79. package/docs/classes/TweakedSigner.html +2 -2
  80. package/docs/classes/Wallet.html +10 -10
  81. package/docs/classes/WrapTransaction.html +404 -0
  82. package/docs/classes/WrappedGeneration.html +187 -0
  83. package/docs/classes/wBTC.html +7 -7
  84. package/docs/enums/TransactionSequence.html +177 -0
  85. package/docs/enums/TransactionType.html +2 -2
  86. package/docs/hierarchy.html +1 -1
  87. package/docs/index.html +5 -1
  88. package/docs/interfaces/ContractAddressVerificationParams.html +2 -2
  89. package/docs/interfaces/DeploymentResult.html +177 -0
  90. package/docs/interfaces/FetchUTXOParams.html +2 -2
  91. package/docs/interfaces/GenerationConstraints.html +182 -0
  92. package/docs/interfaces/IDeploymentParameters.html +183 -0
  93. package/docs/interfaces/IFundingTransactionParameters.html +3 -3
  94. package/docs/interfaces/IInteractionParameters.html +4 -6
  95. package/docs/interfaces/ITransactionParameters.html +3 -3
  96. package/docs/interfaces/IWallet.html +4 -4
  97. package/docs/interfaces/IWrapParameters.html +186 -0
  98. package/docs/interfaces/NetworkInformation.html +2 -2
  99. package/docs/interfaces/PsbtInputExtended.html +1 -1
  100. package/docs/interfaces/PsbtOutputExtendedAddress.html +2 -2
  101. package/docs/interfaces/PsbtOutputExtendedScript.html +2 -2
  102. package/docs/interfaces/RawUTXOResponse.html +2 -2
  103. package/docs/interfaces/SharedInteractionParameters.html +183 -0
  104. package/docs/interfaces/TapLeafScript.html +2 -2
  105. package/docs/interfaces/TweakSettings.html +3 -3
  106. package/docs/interfaces/UTXO.html +2 -2
  107. package/docs/interfaces/UpdateInput.html +2 -2
  108. package/docs/interfaces/WrapResult.html +178 -0
  109. package/docs/interfaces/WrappedGenerationParameters.html +184 -0
  110. package/docs/modules.html +14 -4
  111. package/docs/types/PsbtOutputExtended.html +1 -1
  112. package/docs/variables/version.html +1 -1
  113. package/package.json +1 -1
  114. package/src/_version.ts +1 -1
  115. package/src/opnet.ts +8 -1
  116. package/src/tests/deploy.ts +81 -0
  117. package/src/tests/test.ts +3 -3
  118. package/src/tests/transfer.ts +2 -2
  119. package/src/tests/wrap.ts +89 -0
  120. package/src/tests/wrapTest.ts +88 -0
  121. package/src/transaction/TransactionFactory.ts +119 -1
  122. package/src/transaction/builders/DeploymentTransaction.ts +418 -0
  123. package/src/transaction/builders/FundingTransaction.ts +5 -1
  124. package/src/transaction/builders/InteractionTransaction.ts +5 -404
  125. package/src/transaction/builders/SharedInteractionTransaction.ts +432 -0
  126. package/src/transaction/builders/TransactionBuilder.ts +40 -15
  127. package/src/transaction/builders/WrapTransaction.ts +286 -0
  128. package/src/transaction/interfaces/ITransactionParameters.ts +20 -13
  129. package/src/utils/BitcoinUtils.ts +17 -1
  130. package/src/utxo/{UTXOManager.ts → OPNetLimitedProvider.ts} +53 -2
  131. package/src/verification/TapscriptVerificator.ts +3 -3
  132. package/src/wbtc/Generate.ts +30 -0
  133. package/src/wbtc/WrappedGenerationParameters.ts +33 -0
  134. package/tests/TransactionBuilder.test.ts +58 -58
  135. package/docs/classes/UTXOManager.html +0 -187
  136. package/docs/interfaces/ITransactionDataContractDeployment.html +0 -184
  137. package/docs/interfaces/ITransactionDataContractInteractionWrap.html +0 -186
@@ -0,0 +1,89 @@
1
+ import { Wallet } from '../keypair/Wallet.js';
2
+ import { Regtest } from './Regtest.js';
3
+ import { OPNetLimitedProvider } from '../utxo/OPNetLimitedProvider.js';
4
+ import { networks } from 'bitcoinjs-lib';
5
+ import { TransactionFactory } from '../transaction/TransactionFactory.js';
6
+ import { BitcoinRPC } from '@btc-vision/bsi-bitcoin-rpc';
7
+ import { IWrapParameters } from '../transaction/interfaces/ITransactionParameters.js';
8
+ import { FetchUTXOParams, UTXO } from '../utxo/interfaces/IUTXO.js';
9
+ import { wBTC } from '../metadata/contracts/wBTC.js';
10
+
11
+ const network: networks.Network = networks.regtest;
12
+ const rpc: BitcoinRPC = new BitcoinRPC();
13
+ const wallet: Wallet = new Wallet(Regtest.wallet, network);
14
+
15
+ const opnet: OPNetLimitedProvider = new OPNetLimitedProvider('http://localhost:9001');
16
+ const factory: TransactionFactory = new TransactionFactory();
17
+
18
+ const shouldMineBlock: boolean = true;
19
+
20
+ async function mineBlock(): Promise<boolean> {
21
+ // lets mine 1 block.
22
+ const ok = await rpc.generateToAddress(1, wallet.p2wpkh, 'default');
23
+ if (!ok) {
24
+ throw new Error('Could not mine block');
25
+ }
26
+
27
+ console.log(`Mined block`, ok);
28
+
29
+ return !!ok.length;
30
+ }
31
+
32
+ await rpc.init(Regtest.config);
33
+
34
+ const wrapAmount: bigint = 1000000n;
35
+ const utxoSetting: FetchUTXOParams = {
36
+ address: wallet.p2wpkh,
37
+ minAmount: 10000n,
38
+ requestedAmount: wrapAmount,
39
+ };
40
+
41
+ const utxos: UTXO[] = await opnet.fetchUTXO(utxoSetting);
42
+ if (!utxos) {
43
+ throw new Error('No UTXOs found');
44
+ }
45
+
46
+ const generationParameters = await opnet.fetchWrapParameters(wrapAmount);
47
+ if (!generationParameters) {
48
+ throw new Error('No generation parameters found');
49
+ }
50
+
51
+ const wrapParameters: IWrapParameters = {
52
+ from: wallet.p2wpkh,
53
+
54
+ utxos: utxos,
55
+ signer: wallet.keypair,
56
+ network: network,
57
+ feeRate: 150,
58
+ priorityFee: 50000n,
59
+
60
+ amount: wrapAmount,
61
+ generationParameters: generationParameters,
62
+ };
63
+
64
+ const finalTx = factory.wrap(wrapParameters);
65
+ console.log(`Final transaction:`, finalTx);
66
+
67
+ const firstTxBroadcast = await rpc.sendRawTransaction({
68
+ hexstring: finalTx.transaction[0],
69
+ });
70
+
71
+ console.log(`First transaction broadcasted: ${firstTxBroadcast}`);
72
+
73
+ if (!firstTxBroadcast) {
74
+ throw new Error('Could not broadcast first transaction');
75
+ }
76
+
77
+ const secondTxBroadcast = await rpc.sendRawTransaction({
78
+ hexstring: finalTx.transaction[1],
79
+ });
80
+
81
+ console.log(`Second transaction broadcasted: ${secondTxBroadcast}`);
82
+
83
+ if (!secondTxBroadcast) {
84
+ throw new Error('Could not broadcast second transaction');
85
+ }
86
+
87
+ if (shouldMineBlock) {
88
+ await mineBlock();
89
+ }
@@ -0,0 +1,88 @@
1
+ import { Wallet } from '../keypair/Wallet.js';
2
+ import { Testnet } from './Regtest.js';
3
+ import { OPNetLimitedProvider } from '../utxo/OPNetLimitedProvider.js';
4
+ import { networks } from 'bitcoinjs-lib';
5
+ import { TransactionFactory } from '../transaction/TransactionFactory.js';
6
+ import { BitcoinRPC } from '@btc-vision/bsi-bitcoin-rpc';
7
+ import { IWrapParameters } from '../transaction/interfaces/ITransactionParameters.js';
8
+ import { FetchUTXOParams, UTXO } from '../utxo/interfaces/IUTXO.js';
9
+
10
+ const network: networks.Network = networks.testnet;
11
+ const rpc: BitcoinRPC = new BitcoinRPC();
12
+ const wallet: Wallet = new Wallet(Testnet.wallet, network);
13
+
14
+ const opnet: OPNetLimitedProvider = new OPNetLimitedProvider('https://testnet.opnet.org');
15
+ const factory: TransactionFactory = new TransactionFactory();
16
+
17
+ const shouldMineBlock: boolean = true;
18
+
19
+ async function mineBlock(): Promise<boolean> {
20
+ // lets mine 1 block.
21
+ const ok = await rpc.generateToAddress(1, wallet.p2wpkh, 'default');
22
+ if (!ok) {
23
+ throw new Error('Could not mine block');
24
+ }
25
+
26
+ console.log(`Mined block`, ok);
27
+
28
+ return !!ok.length;
29
+ }
30
+
31
+ await rpc.init(Testnet.config);
32
+
33
+ const wrapAmount: bigint = 100000000n;
34
+ const utxoSetting: FetchUTXOParams = {
35
+ address: wallet.p2wpkh,
36
+ minAmount: 10000n,
37
+ requestedAmount: wrapAmount,
38
+ };
39
+
40
+ const utxos: UTXO[] = await opnet.fetchUTXO(utxoSetting);
41
+ if (!utxos) {
42
+ throw new Error('No UTXOs found');
43
+ }
44
+
45
+ const generationParameters = await opnet.fetchWrapParameters(wrapAmount);
46
+ if (!generationParameters) {
47
+ throw new Error('No generation parameters found');
48
+ }
49
+
50
+ const wrapParameters: IWrapParameters = {
51
+ from: wallet.p2wpkh,
52
+
53
+ utxos: utxos,
54
+ signer: wallet.keypair,
55
+ network: network,
56
+ feeRate: 350,
57
+ priorityFee: 50000n,
58
+
59
+ amount: wrapAmount,
60
+ generationParameters: generationParameters,
61
+ };
62
+
63
+ const finalTx = factory.wrap(wrapParameters);
64
+ console.log(`Final transaction:`, finalTx);
65
+
66
+ const firstTxBroadcast = await rpc.sendRawTransaction({
67
+ hexstring: finalTx.transaction[0],
68
+ });
69
+
70
+ console.log(`First transaction broadcasted: ${firstTxBroadcast}`);
71
+
72
+ if (!firstTxBroadcast) {
73
+ throw new Error('Could not broadcast first transaction');
74
+ }
75
+
76
+ const secondTxBroadcast = await rpc.sendRawTransaction({
77
+ hexstring: finalTx.transaction[1],
78
+ });
79
+
80
+ console.log(`Second transaction broadcasted: ${secondTxBroadcast}`);
81
+
82
+ if (!secondTxBroadcast) {
83
+ throw new Error('Could not broadcast second transaction');
84
+ }
85
+
86
+ if (shouldMineBlock) {
87
+ await mineBlock();
88
+ }
@@ -1,12 +1,31 @@
1
1
  import { Transaction } from 'bitcoinjs-lib';
2
2
  import {
3
+ IDeploymentParameters,
3
4
  IFundingTransactionParameters,
4
5
  IInteractionParameters,
6
+ IWrapParameters,
5
7
  } from './interfaces/ITransactionParameters.js';
6
8
  import { FundingTransaction } from './builders/FundingTransaction.js';
7
9
  import { Output } from 'bitcoinjs-lib/src/transaction.js';
8
10
  import { UTXO } from '../utxo/interfaces/IUTXO.js';
9
11
  import { InteractionTransaction } from './builders/InteractionTransaction.js';
12
+ import { DeploymentTransaction } from './builders/DeploymentTransaction.js';
13
+ import { Address } from '@btc-vision/bsi-binary';
14
+ import { WrapTransaction } from './builders/WrapTransaction.js';
15
+
16
+ export interface DeploymentResult {
17
+ readonly transaction: [string, string];
18
+
19
+ readonly contractAddress: Address;
20
+ readonly p2trAddress: Address;
21
+ }
22
+
23
+ export interface WrapResult {
24
+ readonly transaction: [string, string];
25
+ readonly vaultAddress: Address;
26
+ readonly amount: bigint;
27
+ readonly receiverAddress: Address;
28
+ }
10
29
 
11
30
  export class TransactionFactory {
12
31
  /**
@@ -33,7 +52,7 @@ export class TransactionFactory {
33
52
  const out: Output = signedTransaction.outs[0];
34
53
  const newUtxo: UTXO = {
35
54
  transactionId: signedTransaction.getId(),
36
- outputIndex: 0,
55
+ outputIndex: 0, // always 0
37
56
  scriptPubKey: {
38
57
  hex: out.script.toString('hex'),
39
58
  address: preTransaction.getScriptAddress(),
@@ -54,4 +73,103 @@ export class TransactionFactory {
54
73
 
55
74
  return [signedTransaction.toHex(), outTx.toHex()];
56
75
  }
76
+
77
+ /**
78
+ * @description Generates the required transactions.
79
+ * @param {IDeploymentParameters} deploymentParameters - The deployment parameters
80
+ * @returns {DeploymentResult} - The signed transaction
81
+ */
82
+ public signDeployment(deploymentParameters: IDeploymentParameters): DeploymentResult {
83
+ const preTransaction: DeploymentTransaction = new DeploymentTransaction(
84
+ deploymentParameters,
85
+ );
86
+
87
+ // Initial generation
88
+ preTransaction.signTransaction();
89
+
90
+ const parameters: IFundingTransactionParameters =
91
+ preTransaction.getFundingTransactionParameters();
92
+
93
+ const fundingTransaction: FundingTransaction = new FundingTransaction(parameters);
94
+ const signedTransaction: Transaction = fundingTransaction.signTransaction();
95
+ if (!signedTransaction) {
96
+ throw new Error('Could not sign funding transaction.');
97
+ }
98
+
99
+ const out: Output = signedTransaction.outs[0];
100
+ const newUtxo: UTXO = {
101
+ transactionId: signedTransaction.getId(),
102
+ outputIndex: 0, // always 0
103
+ scriptPubKey: {
104
+ hex: out.script.toString('hex'),
105
+ address: preTransaction.getScriptAddress(),
106
+ },
107
+ value: BigInt(out.value),
108
+ };
109
+
110
+ const newParams: IDeploymentParameters = {
111
+ ...deploymentParameters,
112
+ utxos: [newUtxo],
113
+ randomBytes: preTransaction.getRndBytes(),
114
+ };
115
+
116
+ const finalTransaction: DeploymentTransaction = new DeploymentTransaction(newParams);
117
+
118
+ // We have to regenerate using the new utxo
119
+ const outTx: Transaction = finalTransaction.signTransaction();
120
+
121
+ return {
122
+ transaction: [signedTransaction.toHex(), outTx.toHex()],
123
+ contractAddress: finalTransaction.contractAddress,
124
+ p2trAddress: finalTransaction.p2trAddress,
125
+ };
126
+ }
127
+
128
+ public wrap(warpParameters: IWrapParameters): WrapResult {
129
+ const preTransaction: WrapTransaction = new WrapTransaction(warpParameters);
130
+
131
+ // Initial generation
132
+ preTransaction.signTransaction();
133
+
134
+ const parameters: IFundingTransactionParameters =
135
+ preTransaction.getFundingTransactionParameters();
136
+
137
+ // We add the amount
138
+ parameters.childTransactionRequiredValue += warpParameters.amount;
139
+
140
+ const fundingTransaction: FundingTransaction = new FundingTransaction(parameters);
141
+ const signedTransaction: Transaction = fundingTransaction.signTransaction();
142
+ if (!signedTransaction) {
143
+ throw new Error('Could not sign funding transaction.');
144
+ }
145
+
146
+ const out: Output = signedTransaction.outs[0];
147
+ const newUtxo: UTXO = {
148
+ transactionId: signedTransaction.getId(),
149
+ outputIndex: 0, // always 0
150
+ scriptPubKey: {
151
+ hex: out.script.toString('hex'),
152
+ address: preTransaction.getScriptAddress(),
153
+ },
154
+ value: BigInt(out.value),
155
+ };
156
+
157
+ const newParams: IWrapParameters = {
158
+ ...warpParameters,
159
+ utxos: [newUtxo],
160
+ randomBytes: preTransaction.getRndBytes(),
161
+ };
162
+
163
+ const finalTransaction: WrapTransaction = new WrapTransaction(newParams);
164
+
165
+ // We have to regenerate using the new utxo
166
+ const outTx: Transaction = finalTransaction.signTransaction();
167
+
168
+ return {
169
+ transaction: [signedTransaction.toHex(), outTx.toHex()],
170
+ vaultAddress: finalTransaction.vault,
171
+ amount: finalTransaction.amount,
172
+ receiverAddress: finalTransaction.receiver,
173
+ };
174
+ }
57
175
  }