@btc-vision/transaction 1.0.41 → 1.0.44

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 (151) hide show
  1. package/browser/_version.d.ts +1 -1
  2. package/browser/index.js +1 -1
  3. package/browser/opnet.d.ts +3 -0
  4. package/browser/transaction/TransactionFactory.d.ts +3 -3
  5. package/browser/transaction/browser/BrowserSigner.d.ts +9 -0
  6. package/browser/transaction/browser/BrowserSignerBase.d.ts +18 -0
  7. package/browser/transaction/browser/extensions/UnisatSigner.d.ts +35 -0
  8. package/browser/transaction/browser/types/Unisat.d.ts +54 -0
  9. package/browser/transaction/builders/DeploymentTransaction.d.ts +2 -2
  10. package/browser/transaction/builders/FundingTransaction.d.ts +1 -1
  11. package/browser/transaction/builders/MultiSignTransaction.d.ts +4 -4
  12. package/browser/transaction/builders/SharedInteractionTransaction.d.ts +2 -2
  13. package/browser/transaction/builders/TransactionBuilder.d.ts +10 -9
  14. package/browser/transaction/builders/UnwrapSegwitTransaction.d.ts +3 -3
  15. package/browser/transaction/builders/UnwrapTransaction.d.ts +2 -2
  16. package/browser/transaction/builders/WrapTransaction.d.ts +1 -1
  17. package/browser/transaction/interfaces/ITransactionParameters.d.ts +1 -0
  18. package/browser/transaction/processor/PsbtTransaction.d.ts +1 -1
  19. package/browser/transaction/shared/TweakedTransaction.d.ts +5 -4
  20. package/build/_version.d.ts +1 -1
  21. package/build/_version.js +1 -1
  22. package/build/metadata/contracts/wBTC.js +1 -1
  23. package/build/opnet.d.ts +3 -0
  24. package/build/opnet.js +3 -0
  25. package/build/tests/adaptPSBT.js +1 -1
  26. package/build/tests/deploy.js +1 -2
  27. package/build/tests/deployMotoRegStep1.d.ts +1 -0
  28. package/build/tests/deployMotoRegStep1.js +85 -0
  29. package/build/tests/deployReg.js +2 -2
  30. package/build/tests/factory/createPairReg.js +6 -77
  31. package/build/tests/gen.js +6 -17
  32. package/build/tests/interaction.d.ts +5 -0
  33. package/build/tests/interaction.js +62 -0
  34. package/build/tests/mineReg.js +1 -4
  35. package/build/tests/moto/airdropToken.d.ts +1 -0
  36. package/build/tests/moto/airdropToken.js +21 -0
  37. package/build/tests/moto/allowance.d.ts +1 -0
  38. package/build/tests/moto/allowance.js +5 -0
  39. package/build/tests/moto/approve.d.ts +1 -0
  40. package/build/tests/moto/approve.js +6 -0
  41. package/build/tests/moto/approveWBTC.d.ts +1 -0
  42. package/build/tests/moto/approveWBTC.js +12 -0
  43. package/build/tests/moto/balanceOf.d.ts +1 -0
  44. package/build/tests/moto/balanceOf.js +13 -0
  45. package/build/tests/motoswap/airdropToken.d.ts +11 -0
  46. package/build/tests/motoswap/airdropToken.js +36 -0
  47. package/build/tests/motoswap/deployMoto.d.ts +4 -0
  48. package/build/tests/motoswap/deployMoto.js +87 -0
  49. package/build/tests/motoswap/deployMotoRegStep1.d.ts +1 -0
  50. package/build/tests/motoswap/deployMotoRegStep1.js +91 -0
  51. package/build/tests/motoswap/deployMotoRegStep2.d.ts +1 -0
  52. package/build/tests/motoswap/deployMotoRegStep2.js +91 -0
  53. package/build/tests/motoswap/deployStep1.d.ts +1 -0
  54. package/build/tests/motoswap/deployStep1.js +5 -0
  55. package/build/tests/motoswap/deployStep2.d.ts +1 -0
  56. package/build/tests/motoswap/deployStep2.js +5 -0
  57. package/build/tests/motoswap/deployStep3.d.ts +1 -0
  58. package/build/tests/motoswap/deployStep3.js +5 -0
  59. package/build/tests/motoswap/interaction.d.ts +3 -0
  60. package/build/tests/motoswap/interaction.js +63 -0
  61. package/build/tests/motoswap/routerAddLiquidity.d.ts +11 -0
  62. package/build/tests/motoswap/routerAddLiquidity.js +36 -0
  63. package/build/tests/multisign.js +1 -1
  64. package/build/tests/shared/Utils.d.ts +2 -0
  65. package/build/tests/shared/Utils.js +9 -0
  66. package/build/tests/shared/interaction.d.ts +5 -0
  67. package/build/tests/shared/interaction.js +76 -0
  68. package/build/tests/shared/tokens.d.ts +6 -0
  69. package/build/tests/shared/tokens.js +5 -0
  70. package/build/tests/stakeReg.js +1 -1
  71. package/build/tests/test.js +1 -1
  72. package/build/tests/testReg.js +1 -1
  73. package/build/tests/tokens.d.ts +6 -0
  74. package/build/tests/tokens.js +5 -0
  75. package/build/tests/transfer.js +1 -1
  76. package/build/tests/wbtc/approve.d.ts +1 -0
  77. package/build/tests/wbtc/approve.js +6 -0
  78. package/build/tests/wbtc/approveWBTC.d.ts +1 -0
  79. package/build/tests/wbtc/approveWBTC.js +12 -0
  80. package/build/tests/wbtc/massWrapReg.d.ts +1 -0
  81. package/build/tests/wbtc/massWrapReg.js +105 -0
  82. package/build/tests/wbtc/transferReg.d.ts +1 -0
  83. package/build/tests/wbtc/transferReg.js +16 -0
  84. package/build/tests/wbtc/unStakeReg.d.ts +1 -0
  85. package/build/tests/wbtc/unStakeReg.js +72 -0
  86. package/build/tests/wbtc/unwrapReg.d.ts +1 -0
  87. package/build/tests/wbtc/unwrapReg.js +60 -0
  88. package/build/tests/wbtc/unwrapRegSegwit.d.ts +1 -0
  89. package/build/tests/wbtc/unwrapRegSegwit.js +83 -0
  90. package/build/tests/wbtc/withdrawalRequestReg.d.ts +1 -0
  91. package/build/tests/wbtc/withdrawalRequestReg.js +71 -0
  92. package/build/tests/wbtc/wrapReg.d.ts +1 -0
  93. package/build/tests/wbtc/wrapReg.js +69 -0
  94. package/build/tests/wbtc/wrapTest.d.ts +1 -0
  95. package/build/tests/wbtc/wrapTest.js +66 -0
  96. package/build/tests/writers/allowance.d.ts +3 -0
  97. package/build/tests/writers/allowance.js +10 -0
  98. package/build/tests/writers/approve.d.ts +4 -0
  99. package/build/tests/writers/approve.js +11 -0
  100. package/build/transaction/TransactionFactory.d.ts +3 -3
  101. package/build/transaction/TransactionFactory.js +41 -38
  102. package/build/transaction/browser/BrowserSigner.d.ts +11 -0
  103. package/build/transaction/browser/BrowserSigner.js +10 -0
  104. package/build/transaction/browser/BrowserSignerBase.d.ts +18 -0
  105. package/build/transaction/browser/BrowserSignerBase.js +3 -0
  106. package/build/transaction/browser/extensions/Unisat.d.ts +54 -0
  107. package/build/transaction/browser/extensions/Unisat.js +11 -0
  108. package/build/transaction/browser/extensions/UnisatSigner.d.ts +35 -0
  109. package/build/transaction/browser/extensions/UnisatSigner.js +150 -0
  110. package/build/transaction/browser/types/Unisat.d.ts +54 -0
  111. package/build/transaction/browser/types/Unisat.js +11 -0
  112. package/build/transaction/builders/DeploymentTransaction.d.ts +2 -2
  113. package/build/transaction/builders/DeploymentTransaction.js +4 -4
  114. package/build/transaction/builders/FundingTransaction.d.ts +1 -1
  115. package/build/transaction/builders/FundingTransaction.js +2 -2
  116. package/build/transaction/builders/MultiSignTransaction.d.ts +4 -4
  117. package/build/transaction/builders/MultiSignTransaction.js +7 -7
  118. package/build/transaction/builders/SharedInteractionTransaction.d.ts +2 -2
  119. package/build/transaction/builders/SharedInteractionTransaction.js +6 -6
  120. package/build/transaction/builders/TransactionBuilder.d.ts +10 -9
  121. package/build/transaction/builders/TransactionBuilder.js +29 -22
  122. package/build/transaction/builders/UnwrapSegwitTransaction.d.ts +3 -3
  123. package/build/transaction/builders/UnwrapSegwitTransaction.js +10 -10
  124. package/build/transaction/builders/UnwrapTransaction.d.ts +2 -2
  125. package/build/transaction/builders/UnwrapTransaction.js +9 -6
  126. package/build/transaction/builders/WrapTransaction.d.ts +1 -1
  127. package/build/transaction/builders/WrapTransaction.js +2 -2
  128. package/build/transaction/interfaces/ITransactionParameters.d.ts +1 -0
  129. package/build/transaction/processor/PsbtTransaction.d.ts +1 -1
  130. package/build/transaction/processor/PsbtTransaction.js +2 -2
  131. package/build/transaction/shared/TweakedTransaction.d.ts +5 -4
  132. package/build/transaction/shared/TweakedTransaction.js +59 -23
  133. package/package.json +2 -2
  134. package/src/_version.ts +1 -1
  135. package/src/metadata/contracts/wBTC.ts +1 -1
  136. package/src/opnet.ts +81 -76
  137. package/src/transaction/TransactionFactory.ts +52 -44
  138. package/src/transaction/browser/BrowserSignerBase.ts +44 -0
  139. package/src/transaction/browser/extensions/UnisatSigner.ts +218 -0
  140. package/src/transaction/browser/types/Unisat.ts +77 -0
  141. package/src/transaction/builders/DeploymentTransaction.ts +4 -4
  142. package/src/transaction/builders/FundingTransaction.ts +40 -40
  143. package/src/transaction/builders/MultiSignTransaction.ts +9 -17
  144. package/src/transaction/builders/SharedInteractionTransaction.ts +368 -368
  145. package/src/transaction/builders/TransactionBuilder.ts +637 -632
  146. package/src/transaction/builders/UnwrapSegwitTransaction.ts +365 -362
  147. package/src/transaction/builders/UnwrapTransaction.ts +503 -499
  148. package/src/transaction/builders/WrapTransaction.ts +346 -346
  149. package/src/transaction/interfaces/ITransactionParameters.ts +55 -54
  150. package/src/transaction/processor/PsbtTransaction.ts +192 -192
  151. package/src/transaction/shared/TweakedTransaction.ts +109 -56
@@ -52,9 +52,11 @@ export class TransactionFactory {
52
52
 
53
53
  /**
54
54
  * @description Generates the required transactions.
55
- * @returns {[Transaction, Transaction]} - The signed transaction
55
+ * @returns {Promise<[string, string]>} - The signed transaction
56
56
  */
57
- public signInteraction(interactionParameters: IInteractionParameters): [string, string] {
57
+ public async signInteraction(
58
+ interactionParameters: IInteractionParameters,
59
+ ): Promise<[string, string]> {
58
60
  if (!interactionParameters.to) {
59
61
  throw new Error('Field "to" not provided.');
60
62
  }
@@ -63,16 +65,16 @@ export class TransactionFactory {
63
65
  interactionParameters,
64
66
  );
65
67
 
66
- transaction.signTransaction();
68
+ await transaction.signTransaction();
67
69
 
68
70
  // Initial generation
69
- const estimatedGas = transaction.estimateTransactionFees();
71
+ const estimatedGas = await transaction.estimateTransactionFees();
70
72
  const fundingParameters: IFundingTransactionParameters = {
71
73
  ...interactionParameters,
72
74
  childTransactionRequiredValue: estimatedGas,
73
75
  };
74
76
 
75
- const preFundingTransaction = this.createFundTransaction(fundingParameters);
77
+ const preFundingTransaction = await this.createFundTransaction(fundingParameters);
76
78
  interactionParameters.utxos = this.getUTXOAsTransaction(
77
79
  preFundingTransaction.tx,
78
80
  interactionParameters.to,
@@ -84,15 +86,16 @@ export class TransactionFactory {
84
86
  );
85
87
 
86
88
  // Initial generation
87
- preTransaction.signTransaction();
89
+ await preTransaction.signTransaction();
88
90
 
89
91
  const parameters: IFundingTransactionParameters =
90
- preTransaction.getFundingTransactionParameters();
92
+ await preTransaction.getFundingTransactionParameters();
91
93
 
92
94
  parameters.utxos = fundingParameters.utxos;
93
- parameters.childTransactionRequiredValue = preTransaction.estimateTransactionFees();
95
+ parameters.childTransactionRequiredValue = await preTransaction.estimateTransactionFees();
96
+ parameters.estimatedFees = preFundingTransaction.estimatedFees;
94
97
 
95
- const signedTransaction = this.createFundTransaction(parameters);
98
+ const signedTransaction = await this.createFundTransaction(parameters);
96
99
  if (!signedTransaction) {
97
100
  throw new Error('Could not sign funding transaction.');
98
101
  }
@@ -102,12 +105,13 @@ export class TransactionFactory {
102
105
  utxos: this.getUTXOAsTransaction(signedTransaction.tx, interactionParameters.to, 0), // always 0
103
106
  randomBytes: preTransaction.getRndBytes(),
104
107
  nonWitnessUtxo: signedTransaction.tx.toBuffer(),
108
+ estimatedFees: preTransaction.estimatedFees,
105
109
  };
106
110
 
107
111
  const finalTransaction: InteractionTransaction = new InteractionTransaction(newParams);
108
112
 
109
113
  // We have to regenerate using the new utxo
110
- const outTx: Transaction = finalTransaction.signTransaction();
114
+ const outTx: Transaction = await finalTransaction.signTransaction();
111
115
 
112
116
  return [signedTransaction.tx.toHex(), outTx.toHex()];
113
117
  }
@@ -115,21 +119,23 @@ export class TransactionFactory {
115
119
  /**
116
120
  * @description Generates the required transactions.
117
121
  * @param {IDeploymentParameters} deploymentParameters - The deployment parameters
118
- * @returns {DeploymentResult} - The signed transaction
122
+ * @returns {Promise<DeploymentResult>} - The signed transaction
119
123
  */
120
- public signDeployment(deploymentParameters: IDeploymentParameters): DeploymentResult {
124
+ public async signDeployment(
125
+ deploymentParameters: IDeploymentParameters,
126
+ ): Promise<DeploymentResult> {
121
127
  const preTransaction: DeploymentTransaction = new DeploymentTransaction(
122
128
  deploymentParameters,
123
129
  );
124
130
 
125
131
  // Initial generation
126
- preTransaction.signTransaction();
132
+ await preTransaction.signTransaction();
127
133
 
128
134
  const parameters: IFundingTransactionParameters =
129
- preTransaction.getFundingTransactionParameters();
135
+ await preTransaction.getFundingTransactionParameters();
130
136
 
131
137
  const fundingTransaction: FundingTransaction = new FundingTransaction(parameters);
132
- const signedTransaction: Transaction = fundingTransaction.signTransaction();
138
+ const signedTransaction: Transaction = await fundingTransaction.signTransaction();
133
139
  if (!signedTransaction) {
134
140
  throw new Error('Could not sign funding transaction.');
135
141
  }
@@ -155,7 +161,7 @@ export class TransactionFactory {
155
161
  const finalTransaction: DeploymentTransaction = new DeploymentTransaction(newParams);
156
162
 
157
163
  // We have to regenerate using the new utxo
158
- const outTx: Transaction = finalTransaction.signTransaction();
164
+ const outTx: Transaction = await finalTransaction.signTransaction();
159
165
 
160
166
  return {
161
167
  transaction: [signedTransaction.toHex(), outTx.toHex()],
@@ -167,10 +173,10 @@ export class TransactionFactory {
167
173
  /**
168
174
  * Basically it's fun to manage UTXOs.
169
175
  * @param {IWrapParameters} warpParameters - The wrap parameters
170
- * @returns {WrapResult | undefined} - The signed transaction
176
+ * @returns {Promise<WrapResult>} - The signed transaction
171
177
  * @throws {Error} - If the transaction could not be signed
172
178
  */
173
- public wrap(warpParameters: IWrapParameters): WrapResult {
179
+ public async wrap(warpParameters: IWrapParameters): Promise<WrapResult> {
174
180
  if (warpParameters.amount < currentConsensusConfig.VAULT_MINIMUM_AMOUNT) {
175
181
  throw new Error(
176
182
  `Amount is too low. Minimum consolidation is ${currentConsensusConfig.VAULT_MINIMUM_AMOUNT} sat. Received ${warpParameters.amount} sat. Make sure that you cover the unwrap consolidation fees of ${currentConsensusConfig.UNWRAP_CONSOLIDATION_PREPAID_FEES_SAT}sat.`,
@@ -188,22 +194,22 @@ export class TransactionFactory {
188
194
  to: to,
189
195
  };
190
196
 
191
- const preFundingTransaction = this.createFundTransaction(fundingParameters);
197
+ const preFundingTransaction = await this.createFundTransaction(fundingParameters);
192
198
  warpParameters.utxos = this.getUTXOAsTransaction(preFundingTransaction.tx, to, 0);
193
199
 
194
200
  const preTransaction: WrapTransaction = new WrapTransaction(warpParameters);
195
201
 
196
202
  // Initial generation
197
- preTransaction.signTransaction();
203
+ await preTransaction.signTransaction();
198
204
 
199
205
  const parameters: IFundingTransactionParameters =
200
- preTransaction.getFundingTransactionParameters();
206
+ await preTransaction.getFundingTransactionParameters();
201
207
 
202
208
  // We add the amount
203
209
  parameters.childTransactionRequiredValue += childTransactionRequiredValue;
204
210
  parameters.utxos = fundingParameters.utxos;
205
211
 
206
- const signedTransaction = this.createFundTransaction(parameters);
212
+ const signedTransaction = await this.createFundTransaction(parameters);
207
213
  if (!signedTransaction) {
208
214
  throw new Error('Could not sign funding transaction.');
209
215
  }
@@ -218,7 +224,7 @@ export class TransactionFactory {
218
224
  const finalTransaction: WrapTransaction = new WrapTransaction(newParams);
219
225
 
220
226
  // We have to regenerate using the new utxo
221
- const outTx: Transaction = finalTransaction.signTransaction();
227
+ const outTx: Transaction = await finalTransaction.signTransaction();
222
228
  return {
223
229
  transaction: [signedTransaction.tx.toHex(), outTx.toHex()],
224
230
  vaultAddress: finalTransaction.vault,
@@ -230,7 +236,7 @@ export class TransactionFactory {
230
236
  /**
231
237
  * Unwrap bitcoin.
232
238
  * @param {IUnwrapParameters} unwrapParameters - The unwrap parameters
233
- * @returns {UnwrapResult} - The signed transaction
239
+ * @returns {Promise<UnwrapResult>} - The signed transaction
234
240
  * @throws {Error} - If the transaction could not be signed
235
241
  * @deprecated
236
242
  */
@@ -238,13 +244,13 @@ export class TransactionFactory {
238
244
  console.error('The "unwrap" method is deprecated. Use unwrapTap instead.');
239
245
 
240
246
  const transaction: UnwrapSegwitTransaction = new UnwrapSegwitTransaction(unwrapParameters);
241
- transaction.signTransaction();
247
+ await transaction.signTransaction();
242
248
 
243
249
  const to = transaction.toAddress();
244
250
  if (!to) throw new Error('To address is required');
245
251
 
246
252
  // Initial generation
247
- const estimatedGas = transaction.estimateTransactionFees();
253
+ const estimatedGas = await transaction.estimateTransactionFees();
248
254
  const estimatedFees = transaction.preEstimateTransactionFees(
249
255
  BigInt(unwrapParameters.feeRate),
250
256
  this.calculateNumInputs(unwrapParameters.unwrapUTXOs),
@@ -259,7 +265,7 @@ export class TransactionFactory {
259
265
  to: to,
260
266
  };
261
267
 
262
- const preFundingTransaction = this.createFundTransaction(fundingParameters);
268
+ const preFundingTransaction = await this.createFundTransaction(fundingParameters);
263
269
  unwrapParameters.utxos = this.getUTXOAsTransaction(preFundingTransaction.tx, to, 0);
264
270
 
265
271
  const preTransaction: UnwrapSegwitTransaction = new UnwrapSegwitTransaction({
@@ -268,16 +274,16 @@ export class TransactionFactory {
268
274
  });
269
275
 
270
276
  // Initial generation
271
- preTransaction.signTransaction();
277
+ await preTransaction.signTransaction();
272
278
 
273
279
  const parameters: IFundingTransactionParameters =
274
- preTransaction.getFundingTransactionParameters();
280
+ await preTransaction.getFundingTransactionParameters();
275
281
 
276
282
  parameters.utxos = fundingParameters.utxos;
277
283
  parameters.childTransactionRequiredValue =
278
- preTransaction.estimateTransactionFees() + estimatedFees;
284
+ (await preTransaction.estimateTransactionFees()) + estimatedFees;
279
285
 
280
- const signedTransaction = this.createFundTransaction(parameters);
286
+ const signedTransaction = await this.createFundTransaction(parameters);
281
287
  if (!signedTransaction) {
282
288
  throw new Error('Could not sign funding transaction.');
283
289
  }
@@ -292,7 +298,7 @@ export class TransactionFactory {
292
298
  const finalTransaction: UnwrapSegwitTransaction = new UnwrapSegwitTransaction(newParams);
293
299
 
294
300
  // We have to regenerate using the new utxo
295
- const outTx: Psbt = finalTransaction.signPSBT();
301
+ const outTx: Psbt = await finalTransaction.signPSBT();
296
302
  const asBase64 = outTx.toBase64();
297
303
  const psbt = this.writePSBTHeader(PSBTTypes.UNWRAP, asBase64);
298
304
 
@@ -306,25 +312,25 @@ export class TransactionFactory {
306
312
  /**
307
313
  * Unwrap bitcoin via taproot.
308
314
  * @param {IUnwrapParameters} unwrapParameters - The unwrap parameters
309
- * @returns {UnwrapResult} - The signed transaction
315
+ * @returns {Promise<UnwrapResult>} - The signed transaction
310
316
  * @throws {Error} - If the transaction could not be signed
311
317
  */
312
318
  public async unwrap(unwrapParameters: IUnwrapParameters): Promise<UnwrapResult> {
313
319
  const transaction: UnwrapTransaction = new UnwrapTransaction(unwrapParameters);
314
- transaction.signTransaction();
320
+ await transaction.signTransaction();
315
321
 
316
322
  const to = transaction.toAddress();
317
323
  if (!to) throw new Error('To address is required');
318
324
 
319
325
  // Initial generation
320
- const estimatedGas = transaction.estimateTransactionFees();
326
+ const estimatedGas = await transaction.estimateTransactionFees();
321
327
  const fundingParameters: IFundingTransactionParameters = {
322
328
  ...unwrapParameters,
323
329
  childTransactionRequiredValue: estimatedGas,
324
330
  to: to,
325
331
  };
326
332
 
327
- const preFundingTransaction = this.createFundTransaction(fundingParameters);
333
+ const preFundingTransaction = await this.createFundTransaction(fundingParameters);
328
334
  unwrapParameters.utxos = this.getUTXOAsTransaction(preFundingTransaction.tx, to, 0);
329
335
 
330
336
  const preTransaction: UnwrapTransaction = new UnwrapTransaction({
@@ -333,15 +339,15 @@ export class TransactionFactory {
333
339
  });
334
340
 
335
341
  // Initial generation
336
- preTransaction.signTransaction();
342
+ await preTransaction.signTransaction();
337
343
 
338
344
  const parameters: IFundingTransactionParameters =
339
- preTransaction.getFundingTransactionParameters();
345
+ await preTransaction.getFundingTransactionParameters();
340
346
 
341
347
  parameters.utxos = fundingParameters.utxos;
342
- parameters.childTransactionRequiredValue = preTransaction.estimateTransactionFees();
348
+ parameters.childTransactionRequiredValue = await preTransaction.estimateTransactionFees();
343
349
 
344
- const signedTransaction = this.createFundTransaction(parameters);
350
+ const signedTransaction = await this.createFundTransaction(parameters);
345
351
  if (!signedTransaction) {
346
352
  throw new Error('Could not sign funding transaction.');
347
353
  }
@@ -356,7 +362,7 @@ export class TransactionFactory {
356
362
  const finalTransaction: UnwrapTransaction = new UnwrapTransaction(newParams);
357
363
 
358
364
  // We have to regenerate using the new utxo
359
- const outTx: Psbt = finalTransaction.signPSBT();
365
+ const outTx: Psbt = await finalTransaction.signPSBT();
360
366
  const asBase64 = outTx.toBase64();
361
367
  const psbt = this.writePSBTHeader(PSBTTypes.UNWRAP, asBase64);
362
368
 
@@ -422,12 +428,13 @@ export class TransactionFactory {
422
428
  return [newUtxo];
423
429
  }
424
430
 
425
- private createFundTransaction(parameters: IFundingTransactionParameters): {
431
+ private async createFundTransaction(parameters: IFundingTransactionParameters): Promise<{
426
432
  tx: Transaction;
427
433
  original: FundingTransaction;
428
- } {
434
+ estimatedFees: bigint;
435
+ }> {
429
436
  const fundingTransaction: FundingTransaction = new FundingTransaction(parameters);
430
- const signedTransaction: Transaction = fundingTransaction.signTransaction();
437
+ const signedTransaction: Transaction = await fundingTransaction.signTransaction();
431
438
  if (!signedTransaction) {
432
439
  throw new Error('Could not sign funding transaction.');
433
440
  }
@@ -435,6 +442,7 @@ export class TransactionFactory {
435
442
  return {
436
443
  tx: signedTransaction,
437
444
  original: fundingTransaction,
445
+ estimatedFees: await fundingTransaction.estimateTransactionFees(),
438
446
  };
439
447
  }
440
448
  }
@@ -0,0 +1,44 @@
1
+ import { Network, Psbt, Signer } from 'bitcoinjs-lib';
2
+ import { Address } from '@btc-vision/bsi-binary';
3
+
4
+ /**
5
+ * Create a custom keypair.
6
+ * @class CustomKeypair
7
+ */
8
+ export abstract class CustomKeypair implements Signer {
9
+ public abstract network: Network;
10
+
11
+ public abstract publicKey: Buffer;
12
+
13
+ public abstract addresses: Address[];
14
+
15
+ public abstract p2tr: Address;
16
+ public abstract p2pkh: Address;
17
+
18
+ protected constructor() {}
19
+
20
+ /*public abstract signTransaction(
21
+ transaction: Psbt,
22
+ input: PsbtInput,
23
+ i: number,
24
+ sighashTypes: number[],
25
+ ): Promise<Psbt>;*/
26
+
27
+ public abstract signTaprootInput(
28
+ transaction: Psbt,
29
+ i: number,
30
+ sighashTypes: number[],
31
+ ): Promise<void>;
32
+
33
+ public abstract signInput(transaction: Psbt, i: number, sighashTypes: number[]): Promise<void>;
34
+
35
+ public abstract getPublicKey(): Buffer;
36
+
37
+ public abstract sign(hash: Buffer, lowR?: boolean): Buffer;
38
+
39
+ public abstract signSchnorr(hash: Buffer): Buffer;
40
+
41
+ public abstract verify(hash: Buffer, signature: Buffer): boolean;
42
+
43
+ public abstract init(): Promise<void>;
44
+ }
@@ -0,0 +1,218 @@
1
+ import { CustomKeypair } from '../BrowserSignerBase.js';
2
+ import { Network, networks, Psbt } from 'bitcoinjs-lib';
3
+ import { PsbtSignatureOptions, Unisat, UnisatNetwork } from '../types/Unisat.js';
4
+ import { Address } from '@btc-vision/bsi-binary';
5
+ import { EcKeyPair } from '../../../keypair/EcKeyPair.js';
6
+ import { ECPairInterface } from 'ecpair';
7
+ import { TapScriptSig } from 'bip174/src/lib/interfaces.js';
8
+
9
+ declare global {
10
+ interface Window {
11
+ unisat?: Unisat;
12
+ }
13
+ }
14
+
15
+ export class UnisatSigner extends CustomKeypair {
16
+ private isInitialized: boolean = false;
17
+
18
+ constructor() {
19
+ super();
20
+
21
+ if (!window) {
22
+ throw new Error('UnisatSigner can only be used in a browser environment');
23
+ }
24
+ }
25
+
26
+ private _p2tr: Address | undefined;
27
+
28
+ public get p2tr(): Address {
29
+ if (!this._p2tr) {
30
+ throw new Error('P2TR address not set');
31
+ }
32
+
33
+ return this._p2tr;
34
+ }
35
+
36
+ private _p2pkh: Address | undefined;
37
+
38
+ public get p2pkh(): Address {
39
+ if (!this._p2pkh) {
40
+ throw new Error('P2PKH address not set');
41
+ }
42
+
43
+ return this._p2pkh;
44
+ }
45
+
46
+ private _addresses: Address[] | undefined;
47
+
48
+ public get addresses(): Address[] {
49
+ if (!this._addresses) {
50
+ throw new Error('Addresses not set');
51
+ }
52
+
53
+ return this._addresses;
54
+ }
55
+
56
+ private _publicKey: Buffer | undefined;
57
+
58
+ public get publicKey(): Buffer {
59
+ if (!this._publicKey) {
60
+ throw new Error('Public key not set');
61
+ }
62
+
63
+ return this._publicKey;
64
+ }
65
+
66
+ public _network: Network | undefined;
67
+
68
+ public get network(): Network {
69
+ if (!this._network) {
70
+ throw new Error('Network not set');
71
+ }
72
+
73
+ return this._network;
74
+ }
75
+
76
+ public get unisat(): Unisat {
77
+ if (!window.unisat) {
78
+ throw new Error('Unisat extension not found');
79
+ }
80
+
81
+ return window.unisat;
82
+ }
83
+
84
+ public async init(): Promise<void> {
85
+ if (this.isInitialized) {
86
+ return;
87
+ }
88
+
89
+ const network = await this.unisat.getNetwork();
90
+ switch (network) {
91
+ case UnisatNetwork.mainnet:
92
+ this._network = networks.bitcoin;
93
+ break;
94
+ case UnisatNetwork.testnet:
95
+ this._network = networks.testnet;
96
+ break;
97
+ case UnisatNetwork.regtest:
98
+ this._network = networks.regtest;
99
+ break;
100
+ default:
101
+ throw new Error('Invalid network');
102
+ }
103
+
104
+ const publicKey = await this.unisat.getPublicKey();
105
+ this._publicKey = Buffer.from(publicKey, 'hex');
106
+
107
+ this._p2pkh = EcKeyPair.getP2WPKHAddress(this as unknown as ECPairInterface, this.network);
108
+
109
+ this._p2tr = EcKeyPair.getTaprootAddress(this as unknown as ECPairInterface, this.network);
110
+
111
+ this._addresses = [this._p2pkh, this._p2tr];
112
+
113
+ this.isInitialized = true;
114
+ }
115
+
116
+ public getPublicKey(): Buffer {
117
+ if (!this.isInitialized) {
118
+ throw new Error('UnisatSigner not initialized');
119
+ }
120
+
121
+ return this.publicKey;
122
+ }
123
+
124
+ public sign(hash: Buffer, lowR?: boolean): Buffer {
125
+ throw new Error('Not implemented: sign');
126
+ }
127
+
128
+ public signSchnorr(hash: Buffer): Buffer {
129
+ throw new Error('Not implemented: signSchnorr');
130
+ }
131
+
132
+ public verify(hash: Buffer, signature: Buffer): boolean {
133
+ throw new Error('Not implemented: verify');
134
+ }
135
+
136
+ public async signTaprootInput(
137
+ transaction: Psbt,
138
+ i: number,
139
+ sighashTypes: number[],
140
+ ): Promise<void> {
141
+ let firstSignature = await this.signTweaked(transaction, i, sighashTypes, false);
142
+ this.combine(transaction, firstSignature, i);
143
+ }
144
+
145
+ public async signInput(transaction: Psbt, i: number, sighashTypes: number[]): Promise<void> {
146
+ const secondSignature = await this.signTweaked(transaction, i, sighashTypes, true);
147
+
148
+ this.combine(transaction, secondSignature, i);
149
+ }
150
+
151
+ private combine(transaction: Psbt, newPsbt: Psbt, i: number): void {
152
+ const signedInput = newPsbt.data.inputs[i];
153
+ const originalInput = transaction.data.inputs[i];
154
+
155
+ if (signedInput.partialSig) {
156
+ transaction.updateInput(i, { partialSig: signedInput.partialSig });
157
+ }
158
+
159
+ if (signedInput.tapKeySig && !originalInput.tapKeySig) {
160
+ transaction.updateInput(i, { tapKeySig: signedInput.tapKeySig });
161
+ }
162
+
163
+ if (signedInput.tapScriptSig?.length) {
164
+ const lastScriptSig = originalInput.tapScriptSig;
165
+ if (lastScriptSig) {
166
+ const getNonDuplicate = this.getNonDuplicateScriptSig(
167
+ lastScriptSig,
168
+ signedInput.tapScriptSig,
169
+ );
170
+
171
+ if (getNonDuplicate.length) {
172
+ transaction.updateInput(i, { tapScriptSig: getNonDuplicate });
173
+ }
174
+ } else {
175
+ transaction.updateInput(i, { tapScriptSig: signedInput.tapScriptSig });
176
+ }
177
+ }
178
+ }
179
+
180
+ private async signTweaked(
181
+ transaction: Psbt,
182
+ i: number,
183
+ sighashTypes: number[],
184
+ disableTweakSigner: boolean = false,
185
+ ): Promise<Psbt> {
186
+ const opts: PsbtSignatureOptions = {
187
+ autoFinalized: false,
188
+ toSignInputs: [
189
+ {
190
+ index: i,
191
+ publicKey: this.publicKey.toString('hex'),
192
+ sighashTypes,
193
+ disableTweakSigner: disableTweakSigner,
194
+ },
195
+ ],
196
+ };
197
+
198
+ const psbt = transaction.toHex();
199
+ const signed = await this.unisat.signPsbt(psbt, opts);
200
+
201
+ return Psbt.fromHex(signed);
202
+ }
203
+
204
+ private getNonDuplicateScriptSig(
205
+ scriptSig1: TapScriptSig[],
206
+ scriptSig2: TapScriptSig[],
207
+ ): TapScriptSig[] {
208
+ const nonDuplicate: TapScriptSig[] = [];
209
+ for (let i = 0; i < scriptSig2.length; i++) {
210
+ const found = scriptSig1.find((item) => item.pubkey.equals(scriptSig2[i].pubkey));
211
+ if (!found) {
212
+ nonDuplicate.push(scriptSig2[i]);
213
+ }
214
+ }
215
+
216
+ return nonDuplicate;
217
+ }
218
+ }
@@ -0,0 +1,77 @@
1
+ import { Address } from '@btc-vision/bsi-binary';
2
+
3
+ export enum UnisatNetwork {
4
+ testnet = 'testnet',
5
+ mainnet = 'livenet',
6
+ regtest = 'regtest',
7
+ }
8
+
9
+ export interface Balance {
10
+ readonly confirmed: number;
11
+ readonly unconfirmed: number;
12
+ readonly total: number;
13
+ }
14
+
15
+ export enum MessageType {
16
+ ecdsa = 'ecdsa',
17
+ bip322 = 'bip322-simple',
18
+ }
19
+
20
+ interface ToSignInputBase {
21
+ readonly index: number;
22
+ readonly sighashTypes?: number[];
23
+ readonly disableTweakSigner?: boolean;
24
+ }
25
+
26
+ export interface ToSignInputPublicKey extends ToSignInputBase {
27
+ readonly publicKey: string;
28
+ }
29
+
30
+ export interface ToSignInputAddress extends ToSignInputBase {
31
+ readonly address: Address;
32
+ }
33
+
34
+ export type ToSignInput = ToSignInputPublicKey | ToSignInputAddress;
35
+
36
+ export interface PsbtSignatureOptions {
37
+ readonly autoFinalized?: boolean;
38
+ readonly toSignInputs?: ToSignInput[];
39
+ }
40
+
41
+ export interface Unisat {
42
+ sendBitcoin(
43
+ toAddress: Address,
44
+ satoshis: number,
45
+ options: { feeRate: number },
46
+ ): Promise<string>;
47
+
48
+ requestAccounts(): Promise<string[]>;
49
+
50
+ getNetwork(): Promise<UnisatNetwork>;
51
+
52
+ getAccounts(): Promise<string[]>;
53
+
54
+ switchNetwork(network: UnisatNetwork): Promise<void>;
55
+
56
+ getPublicKey(): Promise<string>;
57
+
58
+ getBalance(): Promise<Balance>;
59
+
60
+ signMessage(message: string, type?: MessageType): Promise<string>;
61
+
62
+ pushTx(options: { rawtx: string }): Promise<string>;
63
+
64
+ signPsbt(psbtHex: string, psbtOptions: PsbtSignatureOptions): Promise<string>;
65
+
66
+ signPsbts(psbtHex: string[], psbtOptions: PsbtSignatureOptions): Promise<string[]>;
67
+
68
+ pushPsbt(psbtHex: string): Promise<string>;
69
+
70
+ on(event: 'accountsChanged', listener: (accounts: string[]) => void): void;
71
+
72
+ on(event: 'networkChanged', listener: (network: UnisatNetwork) => void): void;
73
+
74
+ removeListener(event: 'accountsChanged', listener: (accounts: string[]) => void): void;
75
+
76
+ removeListener(event: 'networkChanged', listener: (network: UnisatNetwork) => void): void;
77
+ }
@@ -139,7 +139,7 @@ export class DeploymentTransaction extends TransactionBuilder<TransactionType.DE
139
139
  * Build the transaction
140
140
  * @protected
141
141
  */
142
- protected override buildTransaction(): void {
142
+ protected override async buildTransaction(): Promise<void> {
143
143
  if (!this.to) {
144
144
  this.to = this.getScriptAddress();
145
145
  }
@@ -174,7 +174,7 @@ export class DeploymentTransaction extends TransactionBuilder<TransactionType.DE
174
174
  address: this.to,
175
175
  });
176
176
 
177
- this.addRefundOutput(amountSpent);
177
+ await this.addRefundOutput(amountSpent);
178
178
  }
179
179
 
180
180
  /**
@@ -182,9 +182,9 @@ export class DeploymentTransaction extends TransactionBuilder<TransactionType.DE
182
182
  * @param {Psbt} transaction The transaction to sign
183
183
  * @protected
184
184
  */
185
- protected signInputs(transaction: Psbt): void {
185
+ protected async signInputs(transaction: Psbt): Promise<void> {
186
186
  if (!this.contractSigner) {
187
- super.signInputs(transaction);
187
+ await super.signInputs(transaction);
188
188
 
189
189
  return;
190
190
  }