@btc-vision/transaction 1.2.1 → 1.2.3

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 (84) hide show
  1. package/browser/_version.d.ts +1 -1
  2. package/browser/crypto/crypto-browser.d.ts +1 -1
  3. package/browser/generators/builders/CalldataGenerator.d.ts +1 -1
  4. package/browser/generators/builders/DeploymentGenerator.d.ts +1 -1
  5. package/browser/generators/builders/MineableReward.d.ts +7 -0
  6. package/browser/index.js +1 -1
  7. package/browser/opnet.d.ts +4 -0
  8. package/browser/transaction/ContractAddress.d.ts +1 -0
  9. package/browser/transaction/TransactionFactory.d.ts +19 -4
  10. package/browser/transaction/browser/WalletConnection.d.ts +18 -0
  11. package/browser/transaction/browser/types/Xverse.d.ts +24 -10
  12. package/browser/transaction/builders/ChallengeSolutionTransaction.d.ts +18 -0
  13. package/browser/transaction/builders/DeploymentTransaction.d.ts +4 -0
  14. package/browser/transaction/builders/SharedInteractionTransaction.d.ts +5 -0
  15. package/browser/transaction/builders/TransactionBuilder.d.ts +2 -0
  16. package/browser/transaction/enums/TransactionType.d.ts +3 -4
  17. package/browser/transaction/interfaces/ITransactionParameters.d.ts +6 -0
  18. package/browser/transaction/mineable/ChallengeGenerator.d.ts +9 -0
  19. package/browser/utxo/interfaces/IUTXO.d.ts +1 -1
  20. package/browser/verification/TapscriptVerificator.d.ts +1 -1
  21. package/build/_version.d.ts +1 -1
  22. package/build/_version.js +1 -1
  23. package/build/generators/Generator.js +1 -1
  24. package/build/generators/builders/CalldataGenerator.d.ts +1 -1
  25. package/build/generators/builders/CalldataGenerator.js +7 -26
  26. package/build/generators/builders/DeploymentGenerator.d.ts +1 -1
  27. package/build/generators/builders/DeploymentGenerator.js +9 -6
  28. package/build/generators/builders/LegacyCalldataGenerator.js +5 -1
  29. package/build/generators/builders/MineableReward.d.ts +7 -0
  30. package/build/generators/builders/MineableReward.js +47 -0
  31. package/build/opnet.d.ts +4 -0
  32. package/build/opnet.js +4 -0
  33. package/build/transaction/ContractAddress.d.ts +1 -0
  34. package/build/transaction/ContractAddress.js +4 -1
  35. package/build/transaction/TransactionFactory.d.ts +19 -4
  36. package/build/transaction/TransactionFactory.js +32 -0
  37. package/build/transaction/browser/WalletConnection.d.ts +18 -0
  38. package/build/transaction/browser/WalletConnection.js +95 -0
  39. package/build/transaction/browser/extensions/UnisatSigner.js +5 -2
  40. package/build/transaction/browser/extensions/XverseSigner.js +15 -9
  41. package/build/transaction/browser/types/Xverse.d.ts +24 -10
  42. package/build/transaction/builders/ChallengeSolutionTransaction.d.ts +18 -0
  43. package/build/transaction/builders/ChallengeSolutionTransaction.js +51 -0
  44. package/build/transaction/builders/DeploymentTransaction.d.ts +4 -0
  45. package/build/transaction/builders/DeploymentTransaction.js +23 -4
  46. package/build/transaction/builders/InteractionTransaction.js +1 -1
  47. package/build/transaction/builders/SharedInteractionTransaction.d.ts +5 -0
  48. package/build/transaction/builders/SharedInteractionTransaction.js +35 -13
  49. package/build/transaction/builders/TransactionBuilder.d.ts +2 -0
  50. package/build/transaction/builders/TransactionBuilder.js +2 -0
  51. package/build/transaction/enums/TransactionType.d.ts +3 -4
  52. package/build/transaction/enums/TransactionType.js +3 -4
  53. package/build/transaction/interfaces/ITransactionParameters.d.ts +6 -0
  54. package/build/transaction/mineable/ChallengeGenerator.d.ts +9 -0
  55. package/build/transaction/mineable/ChallengeGenerator.js +28 -0
  56. package/build/transaction/shared/TweakedTransaction.js +1 -1
  57. package/build/utxo/interfaces/IUTXO.d.ts +1 -1
  58. package/build/verification/TapscriptVerificator.d.ts +1 -1
  59. package/build/verification/TapscriptVerificator.js +2 -8
  60. package/package.json +3 -3
  61. package/src/_version.ts +1 -1
  62. package/src/generators/Generator.ts +1 -1
  63. package/src/generators/builders/CalldataGenerator.ts +10 -41
  64. package/src/generators/builders/DeploymentGenerator.ts +18 -7
  65. package/src/generators/builders/LegacyCalldataGenerator.ts +5 -1
  66. package/src/generators/builders/MineableReward.ts +63 -0
  67. package/src/opnet.ts +5 -0
  68. package/src/transaction/ContractAddress.ts +5 -1
  69. package/src/transaction/TransactionFactory.ts +66 -3
  70. package/src/transaction/browser/WalletConnection.ts +110 -0
  71. package/src/transaction/browser/extensions/UnisatSigner.ts +7 -3
  72. package/src/transaction/browser/extensions/XverseSigner.ts +24 -23
  73. package/src/transaction/browser/types/Xverse.ts +50 -36
  74. package/src/transaction/builders/ChallengeSolutionTransaction.ts +88 -0
  75. package/src/transaction/builders/DeploymentTransaction.ts +46 -3
  76. package/src/transaction/builders/InteractionTransaction.ts +1 -0
  77. package/src/transaction/builders/SharedInteractionTransaction.ts +54 -14
  78. package/src/transaction/builders/TransactionBuilder.ts +3 -0
  79. package/src/transaction/enums/TransactionType.ts +3 -4
  80. package/src/transaction/interfaces/ITransactionParameters.ts +8 -15
  81. package/src/transaction/mineable/ChallengeGenerator.ts +39 -0
  82. package/src/transaction/shared/TweakedTransaction.ts +1 -1
  83. package/src/utxo/interfaces/IUTXO.ts +1 -1
  84. package/src/verification/TapscriptVerificator.ts +3 -18
@@ -30,6 +30,7 @@ export class InteractionTransaction extends SharedInteractionTransaction<Transac
30
30
  this.compiledTargetScript = this.calldataGenerator.compile(
31
31
  this.calldata,
32
32
  this.contractSecret,
33
+ this.preimage,
33
34
  );
34
35
 
35
36
  this.scriptTree = this.getScriptTree();
@@ -1,6 +1,6 @@
1
1
  import { address, Payment, Psbt, PsbtInput, Signer, Taptree, toXOnly } from '@btc-vision/bitcoin';
2
2
  import { ECPairInterface } from 'ecpair';
3
- import { TransactionBuilder } from './TransactionBuilder.js';
3
+ import { MINIMUM_AMOUNT_CA, MINIMUM_AMOUNT_REWARD, TransactionBuilder } from './TransactionBuilder.js';
4
4
  import { TransactionType } from '../enums/TransactionType.js';
5
5
  import { CalldataGenerator } from '../../generators/builders/CalldataGenerator.js';
6
6
  import { SharedInteractionParameters } from '../interfaces/ITransactionParameters.js';
@@ -8,6 +8,7 @@ import { Compressor } from '../../bytecode/Compressor.js';
8
8
  import { EcKeyPair } from '../../keypair/EcKeyPair.js';
9
9
  import { BitcoinUtils } from '../../utils/BitcoinUtils.js';
10
10
  import { UnisatSigner } from '../browser/extensions/UnisatSigner.js';
11
+ import { ChallengeGenerator, IMineableReward } from '../mineable/ChallengeGenerator.js';
11
12
 
12
13
  /**
13
14
  * Shared interaction transaction
@@ -30,6 +31,9 @@ export abstract class SharedInteractionTransaction<
30
31
  protected abstract readonly compiledTargetScript: Buffer;
31
32
  protected abstract readonly scriptTree: Taptree;
32
33
 
34
+ protected readonly preimage: Buffer; // ALWAYS 128 bytes for the preimage
35
+ protected readonly rewardChallenge: IMineableReward;
36
+
33
37
  protected calldataGenerator: CalldataGenerator;
34
38
 
35
39
  /**
@@ -63,7 +67,13 @@ export abstract class SharedInteractionTransaction<
63
67
  throw new Error('Calldata is required');
64
68
  }
65
69
 
70
+ this.preimage = parameters.preimage || BitcoinUtils.getSafeRandomValues(128);
71
+
66
72
  this.disableAutoRefund = parameters.disableAutoRefund || false;
73
+ this.rewardChallenge = ChallengeGenerator.generateMineableReward(
74
+ this.preimage,
75
+ this.network,
76
+ );
67
77
 
68
78
  this.calldata = Compressor.compress(parameters.calldata);
69
79
 
@@ -93,6 +103,13 @@ export abstract class SharedInteractionTransaction<
93
103
  return this.randomBytes;
94
104
  }
95
105
 
106
+ /**
107
+ * Get the preimage
108
+ */
109
+ public getPreimage(): Buffer {
110
+ return this.preimage;
111
+ }
112
+
96
113
  /**
97
114
  * Generate the secret for the interaction
98
115
  * @protected
@@ -134,8 +151,6 @@ export abstract class SharedInteractionTransaction<
134
151
  * @throws {Error} If the to address is required
135
152
  */
136
153
  protected override async buildTransaction(): Promise<void> {
137
- if (!this.to) throw new Error('To address is required');
138
-
139
154
  const selectedRedeem = this.scriptSigner
140
155
  ? this.targetScriptRedeem
141
156
  : this.leftOverFundsScriptRedeem;
@@ -162,16 +177,7 @@ export abstract class SharedInteractionTransaction<
162
177
  this.addInputsFromUTXO();
163
178
  }
164
179
 
165
- const amountSpent: bigint = this.getTransactionOPNetFee();
166
- this.addOutput({
167
- value: Number(amountSpent),
168
- address: this.to,
169
- });
170
-
171
- const amount = this.addOptionalOutputsAndGetAmount();
172
- if (!this.disableAutoRefund) {
173
- await this.addRefundOutput(amountSpent + amount);
174
- }
180
+ await this.createMineableRewardOutputs();
175
181
  }
176
182
 
177
183
  /**
@@ -236,7 +242,6 @@ export abstract class SharedInteractionTransaction<
236
242
 
237
243
  return [
238
244
  this.contractSecret,
239
- this.internalPubKeyToXOnly(),
240
245
  input.tapScriptSig[0].signature,
241
246
  input.tapScriptSig[1].signature,
242
247
  ] as Buffer[];
@@ -311,6 +316,41 @@ export abstract class SharedInteractionTransaction<
311
316
  }
312
317
  }
313
318
 
319
+ private async createMineableRewardOutputs(): Promise<void> {
320
+ if (!this.to) throw new Error('To address is required');
321
+
322
+ const amountSpent: bigint = this.getTransactionOPNetFee();
323
+
324
+ let amountToCA: bigint;
325
+ if (amountSpent > MINIMUM_AMOUNT_REWARD + MINIMUM_AMOUNT_CA) {
326
+ amountToCA = MINIMUM_AMOUNT_CA;
327
+ } else {
328
+ amountToCA = amountSpent;
329
+ }
330
+
331
+ // ALWAYS THE FIRST INPUT.
332
+ this.addOutput({
333
+ value: Number(amountToCA),
334
+ address: this.to,
335
+ });
336
+
337
+ // ALWAYS SECOND.
338
+ if (
339
+ amountToCA === MINIMUM_AMOUNT_CA &&
340
+ amountSpent - MINIMUM_AMOUNT_CA > MINIMUM_AMOUNT_REWARD
341
+ ) {
342
+ this.addOutput({
343
+ value: Number(amountSpent - amountToCA),
344
+ address: this.rewardChallenge.address,
345
+ });
346
+ }
347
+
348
+ const amount = this.addOptionalOutputsAndGetAmount();
349
+ if (!this.disableAutoRefund) {
350
+ await this.addRefundOutput(amountSpent + amount);
351
+ }
352
+ }
353
+
314
354
  private async signInputsNonWalletBased(transaction: Psbt): Promise<void> {
315
355
  for (let i = 0; i < transaction.data.inputs.length; i++) {
316
356
  if (i === 0) {
@@ -26,6 +26,9 @@ import { UnisatSigner } from '../browser/extensions/UnisatSigner.js';
26
26
 
27
27
  initEccLib(ecc);
28
28
 
29
+ export const MINIMUM_AMOUNT_REWARD: bigint = 540n;
30
+ export const MINIMUM_AMOUNT_CA: bigint = 330n;
31
+
29
32
  /**
30
33
  * Allows to build a transaction like you would on Ethereum.
31
34
  * @description The transaction builder class
@@ -3,8 +3,7 @@ export enum TransactionType {
3
3
  FUNDING = 1,
4
4
  DEPLOYMENT = 2,
5
5
  INTERACTION = 3,
6
- WBTC_WRAP = 4,
7
- WBTC_UNWRAP = 5,
8
- MULTI_SIG = 6,
9
- CUSTOM_CODE = 7,
6
+ MULTI_SIG = 4,
7
+ CUSTOM_CODE = 5,
8
+ CHALLENGE_SOLUTION = 6,
10
9
  }
@@ -26,10 +26,17 @@ export interface IFundingTransactionParameters extends ITransactionParameters {
26
26
  splitInputsInto?: number;
27
27
  }
28
28
 
29
+ export interface IChallengeSolutionTransactionParameters extends ITransactionParameters {
30
+ amount: bigint;
31
+
32
+ readonly challengeSolution: Buffer;
33
+ }
34
+
29
35
  export interface SharedInteractionParameters extends ITransactionParameters {
30
36
  calldata?: Buffer;
31
37
  disableAutoRefund?: boolean;
32
38
 
39
+ readonly preimage?: Buffer;
33
40
  readonly randomBytes?: Buffer;
34
41
  }
35
42
 
@@ -39,24 +46,10 @@ export interface IInteractionParameters extends SharedInteractionParameters {
39
46
  readonly to: string;
40
47
  }
41
48
 
42
- /*export interface IWrapParameters extends Omit<SharedInteractionParameters, 'optionalOutputs'> {
43
- readonly to?: string;
44
- readonly from: string;
45
-
46
- readonly amount: bigint;
47
- readonly receiver?: Address;
48
-
49
- readonly generationParameters: WrappedGeneration;
50
- }
51
-
52
- export interface IUnwrapParameters extends Omit<SharedInteractionParameters, 'optionalOutputs'> {
53
- readonly unwrapUTXOs: VaultUTXOs[];
54
- readonly amount: bigint;
55
- }*/
56
-
57
49
  export interface IDeploymentParameters extends Omit<ITransactionParameters, 'to'> {
58
50
  readonly bytecode: Buffer;
59
51
  readonly calldata?: Buffer;
60
52
 
61
53
  readonly randomBytes?: Buffer;
54
+ readonly preimage?: Buffer;
62
55
  }
@@ -0,0 +1,39 @@
1
+ import bitcoin, { Network } from '@btc-vision/bitcoin';
2
+ import { MineableReward } from '../../generators/builders/MineableReward.js';
3
+
4
+ export interface IMineableReward {
5
+ address: string;
6
+ p2shOutputScript: Buffer;
7
+ redeemScript: Buffer;
8
+ }
9
+
10
+ export class ChallengeGenerator {
11
+ public static generateMineableReward(preimage1: Buffer, network: Network): IMineableReward {
12
+ const mineableReward = new MineableReward(Buffer.alloc(0), network);
13
+ const redeemScript = mineableReward.compile(preimage1);
14
+ const p2sh = bitcoin.payments.p2sh({
15
+ redeem: { output: redeemScript },
16
+ network,
17
+ });
18
+
19
+ const outputRedeem: Buffer | undefined = p2sh.redeem?.output;
20
+ if (!outputRedeem) {
21
+ throw new Error('Output redeem is required');
22
+ }
23
+
24
+ if (!p2sh.address) {
25
+ throw new Error('P2SH address is required');
26
+ }
27
+
28
+ const p2shOutputScript: Buffer | undefined = p2sh?.redeem?.output;
29
+ if (!p2shOutputScript) {
30
+ throw new Error('No redeem output');
31
+ }
32
+
33
+ return {
34
+ address: p2sh.address,
35
+ p2shOutputScript,
36
+ redeemScript: redeemScript,
37
+ };
38
+ }
39
+ }
@@ -814,7 +814,7 @@ export abstract class TweakedTransaction extends Logger {
814
814
  } => {
815
815
  const inputDecoded = this.inputs[inputIndex];
816
816
  if (isP2SH && input.partialSig && inputDecoded && inputDecoded.redeemScript) {
817
- const signatures = input.partialSig.map((sig) => sig.signature);
817
+ const signatures = input.partialSig.map((sig) => sig.signature) || [];
818
818
  const scriptSig = script.compile([...signatures, inputDecoded.redeemScript]);
819
819
 
820
820
  return {
@@ -1,4 +1,4 @@
1
- import { ScriptPubKey } from '@btc-vision/bsi-bitcoin-rpc';
1
+ import { ScriptPubKey } from '@btc-vision/bitcoin-rpc';
2
2
 
3
3
  export interface UTXO {
4
4
  readonly transactionId: string;
@@ -9,13 +9,13 @@ import {
9
9
  } from '@btc-vision/bitcoin';
10
10
  import { DeploymentGenerator } from '../generators/builders/DeploymentGenerator.js';
11
11
  import { TransactionBuilder } from '../transaction/builders/TransactionBuilder.js';
12
- import { Address } from '../keypair/Address.js';
13
12
 
14
13
  export interface ContractAddressVerificationParams {
15
14
  readonly deployerPubKey: Buffer;
16
15
  readonly contractSaltPubKey: Buffer;
17
16
  readonly originalSalt: Buffer;
18
17
  readonly bytecode: Buffer;
18
+ readonly preimage: Buffer;
19
19
  readonly calldata?: Buffer;
20
20
  readonly network?: Network;
21
21
  }
@@ -36,6 +36,7 @@ export class TapscriptVerificator {
36
36
  const compiledTargetScript: Buffer = scriptBuilder.compile(
37
37
  params.bytecode,
38
38
  params.originalSalt,
39
+ params.preimage,
39
40
  params.calldata,
40
41
  );
41
42
 
@@ -67,6 +68,7 @@ export class TapscriptVerificator {
67
68
  const compiledTargetScript: Buffer = scriptBuilder.compile(
68
69
  params.bytecode,
69
70
  params.originalSalt,
71
+ params.preimage,
70
72
  params.calldata,
71
73
  );
72
74
 
@@ -112,23 +114,6 @@ export class TapscriptVerificator {
112
114
  return bitCrypto.hash256(buf);
113
115
  }
114
116
 
115
- public static generateContractVirtualAddress(
116
- deployerPubKey: Buffer,
117
- bytecode: Buffer,
118
- saltHash: Buffer,
119
- network: Network = networks.bitcoin,
120
- ): string {
121
- const virtualAddress: Buffer = TapscriptVerificator.getContractSeed(
122
- deployerPubKey,
123
- bytecode,
124
- saltHash,
125
- );
126
-
127
- const address = new Address(virtualAddress);
128
-
129
- return address.p2tr(network);
130
- }
131
-
132
117
  public static generateAddressFromScript(
133
118
  params: ContractAddressVerificationParams,
134
119
  scriptTree: Taptree,