@btc-vision/transaction 1.1.11 → 1.1.13

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 (29) hide show
  1. package/browser/_version.d.ts +1 -1
  2. package/browser/index.js +1 -1
  3. package/browser/keypair/Address.d.ts +1 -0
  4. package/browser/transaction/builders/TransactionBuilder.d.ts +1 -0
  5. package/browser/transaction/interfaces/ITransactionParameters.d.ts +1 -1
  6. package/build/_version.d.ts +1 -1
  7. package/build/_version.js +1 -1
  8. package/build/deterministic/AddressMap.js +1 -1
  9. package/build/keypair/Address.d.ts +1 -0
  10. package/build/keypair/Address.js +3 -0
  11. package/build/transaction/TransactionFactory.js +6 -3
  12. package/build/transaction/builders/CustomScriptTransaction.js +1 -1
  13. package/build/transaction/builders/DeploymentTransaction.js +1 -1
  14. package/build/transaction/builders/FundingTransaction.js +1 -1
  15. package/build/transaction/builders/SharedInteractionTransaction.js +2 -1
  16. package/build/transaction/builders/TransactionBuilder.d.ts +1 -0
  17. package/build/transaction/builders/TransactionBuilder.js +10 -1
  18. package/build/transaction/interfaces/ITransactionParameters.d.ts +1 -1
  19. package/package.json +1 -1
  20. package/src/_version.ts +1 -1
  21. package/src/deterministic/AddressMap.ts +1 -1
  22. package/src/keypair/Address.ts +7 -0
  23. package/src/transaction/TransactionFactory.ts +6 -4
  24. package/src/transaction/builders/CustomScriptTransaction.ts +1 -1
  25. package/src/transaction/builders/DeploymentTransaction.ts +1 -1
  26. package/src/transaction/builders/FundingTransaction.ts +1 -1
  27. package/src/transaction/builders/SharedInteractionTransaction.ts +2 -1
  28. package/src/transaction/builders/TransactionBuilder.ts +12 -2
  29. package/src/transaction/interfaces/ITransactionParameters.ts +1 -2
@@ -18,5 +18,6 @@ export declare class Address extends Uint8Array {
18
18
  p2pkh(network: Network): string;
19
19
  p2shp2wpkh(network: Network): string;
20
20
  toString(): string;
21
+ toJSON(): string;
21
22
  p2tr(network: Network): string;
22
23
  }
@@ -47,6 +47,7 @@ export declare abstract class TransactionBuilder<T extends TransactionType> exte
47
47
  setPSBT(psbt: Psbt): void;
48
48
  getInputs(): PsbtInputExtended[];
49
49
  getOutputs(): PsbtOutputExtended[];
50
+ getOptionalOutputValue(): bigint;
50
51
  protected addRefundOutput(amountSpent: bigint): Promise<void>;
51
52
  protected addValueToToOutput(value: number | bigint): void;
52
53
  protected getTransactionOPNetFee(): bigint;
@@ -22,7 +22,7 @@ export interface SharedInteractionParameters extends ITransactionParameters {
22
22
  disableAutoRefund?: boolean;
23
23
  readonly randomBytes?: Buffer;
24
24
  }
25
- export interface IInteractionParameters extends Omit<SharedInteractionParameters, 'optionalOutputs'> {
25
+ export interface IInteractionParameters extends SharedInteractionParameters {
26
26
  readonly calldata: Buffer;
27
27
  readonly to: string;
28
28
  }
@@ -1 +1 @@
1
- export declare const version = "1.1.11";
1
+ export declare const version = "1.1.13";
package/build/_version.js CHANGED
@@ -1 +1 @@
1
- export const version = '1.1.11';
1
+ export const version = '1.1.13';
@@ -1,7 +1,7 @@
1
1
  import { Map } from './Map.js';
2
2
  export class AddressMap extends Map {
3
3
  set(key, value) {
4
- const index = this._keys.indexOf(key);
4
+ const index = this.indexOf(key);
5
5
  if (index == -1) {
6
6
  this._keys.push(key);
7
7
  this._values.push(value);
@@ -18,5 +18,6 @@ export declare class Address extends Uint8Array {
18
18
  p2pkh(network: Network): string;
19
19
  p2shp2wpkh(network: Network): string;
20
20
  toString(): string;
21
+ toJSON(): string;
21
22
  p2tr(network: Network): string;
22
23
  }
@@ -133,6 +133,9 @@ export class Address extends Uint8Array {
133
133
  toString() {
134
134
  return this.toHex();
135
135
  }
136
+ toJSON() {
137
+ return this.toHex();
138
+ }
136
139
  p2tr(network) {
137
140
  if (__classPrivateFieldGet(this, _Address_p2tr, "f") && __classPrivateFieldGet(this, _Address_network, "f") === network) {
138
141
  return __classPrivateFieldGet(this, _Address_p2tr, "f");
@@ -21,7 +21,8 @@ export class TransactionFactory {
21
21
  parameters.utxos = interactionParameters.utxos;
22
22
  parameters.amount =
23
23
  (await preTransaction.estimateTransactionFees()) +
24
- this.getPriorityFee(interactionParameters);
24
+ this.getPriorityFee(interactionParameters) +
25
+ preTransaction.getOptionalOutputValue();
25
26
  const feeEstimationFundingTransaction = await this.createFundTransaction({ ...parameters });
26
27
  if (!feeEstimationFundingTransaction) {
27
28
  throw new Error('Could not sign funding transaction.');
@@ -63,7 +64,8 @@ export class TransactionFactory {
63
64
  parameters.utxos = interactionParameters.utxos;
64
65
  parameters.amount =
65
66
  (await preTransaction.estimateTransactionFees()) +
66
- this.getPriorityFee(interactionParameters);
67
+ this.getPriorityFee(interactionParameters) +
68
+ preTransaction.getOptionalOutputValue();
67
69
  const feeEstimationFundingTransaction = await this.createFundTransaction({ ...parameters });
68
70
  if (!feeEstimationFundingTransaction) {
69
71
  throw new Error('Could not sign funding transaction.');
@@ -95,7 +97,8 @@ export class TransactionFactory {
95
97
  const parameters = await preTransaction.getFundingTransactionParameters();
96
98
  parameters.amount =
97
99
  (await preTransaction.estimateTransactionFees()) +
98
- this.getPriorityFee(deploymentParameters);
100
+ this.getPriorityFee(deploymentParameters) +
101
+ preTransaction.getOptionalOutputValue();
99
102
  const fundingTransaction = new FundingTransaction(parameters);
100
103
  const signedTransaction = await fundingTransaction.signTransaction();
101
104
  if (!signedTransaction) {
@@ -78,7 +78,7 @@ export class CustomScriptTransaction extends TransactionBuilder {
78
78
  value: Number(amountSpent),
79
79
  address: this.to,
80
80
  });
81
- await this.addRefundOutput(amountSpent);
81
+ await this.addRefundOutput(amountSpent + this.addOptionalOutputsAndGetAmount());
82
82
  }
83
83
  async signInputs(transaction) {
84
84
  if (!this.contractSigner) {
@@ -93,7 +93,7 @@ export class DeploymentTransaction extends TransactionBuilder {
93
93
  value: Number(amountSpent),
94
94
  address: this.contractAddress.p2tr(this.network),
95
95
  });
96
- await this.addRefundOutput(amountSpent);
96
+ await this.addRefundOutput(amountSpent + this.addOptionalOutputsAndGetAmount());
97
97
  }
98
98
  async signInputsWalletBased(transaction) {
99
99
  const signer = this.signer;
@@ -28,7 +28,7 @@ export class FundingTransaction extends TransactionBuilder {
28
28
  address: this.to,
29
29
  });
30
30
  }
31
- await this.addRefundOutput(this.amount);
31
+ await this.addRefundOutput(this.amount + this.addOptionalOutputsAndGetAmount());
32
32
  }
33
33
  splitInputs(amountSpent) {
34
34
  if (!this.to) {
@@ -79,8 +79,9 @@ export class SharedInteractionTransaction extends TransactionBuilder {
79
79
  value: Number(amountSpent),
80
80
  address: this.to,
81
81
  });
82
+ const amount = this.addOptionalOutputsAndGetAmount();
82
83
  if (!this.disableAutoRefund) {
83
- await this.addRefundOutput(amountSpent);
84
+ await this.addRefundOutput(amountSpent + amount);
84
85
  }
85
86
  }
86
87
  async signInputs(transaction) {
@@ -47,6 +47,7 @@ export declare abstract class TransactionBuilder<T extends TransactionType> exte
47
47
  setPSBT(psbt: Psbt): void;
48
48
  getInputs(): PsbtInputExtended[];
49
49
  getOutputs(): PsbtOutputExtended[];
50
+ getOptionalOutputValue(): bigint;
50
51
  protected addRefundOutput(amountSpent: bigint): Promise<void>;
51
52
  protected addValueToToOutput(value: number | bigint): void;
52
53
  protected getTransactionOPNetFee(): bigint;
@@ -186,8 +186,17 @@ export class TransactionBuilder extends TweakedTransaction {
186
186
  outputs.push(this.feeOutput);
187
187
  return outputs;
188
188
  }
189
+ getOptionalOutputValue() {
190
+ if (!this.optionalOutputs)
191
+ return 0n;
192
+ let total = 0n;
193
+ for (let i = 0; i < this.optionalOutputs.length; i++) {
194
+ total += BigInt(this.optionalOutputs[i].value);
195
+ }
196
+ return total;
197
+ }
189
198
  async addRefundOutput(amountSpent) {
190
- const sendBackAmount = this.totalInputAmount - amountSpent - this.addOptionalOutputsAndGetAmount();
199
+ const sendBackAmount = this.totalInputAmount - amountSpent;
191
200
  if (sendBackAmount >= TransactionBuilder.MINIMUM_DUST) {
192
201
  if (AddressVerificator.isValidP2TRAddress(this.from, this.network)) {
193
202
  await this.setFeeOutput({
@@ -22,7 +22,7 @@ export interface SharedInteractionParameters extends ITransactionParameters {
22
22
  disableAutoRefund?: boolean;
23
23
  readonly randomBytes?: Buffer;
24
24
  }
25
- export interface IInteractionParameters extends Omit<SharedInteractionParameters, 'optionalOutputs'> {
25
+ export interface IInteractionParameters extends SharedInteractionParameters {
26
26
  readonly calldata: Buffer;
27
27
  readonly to: string;
28
28
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@btc-vision/transaction",
3
3
  "type": "module",
4
- "version": "1.1.11",
4
+ "version": "1.1.13",
5
5
  "author": "BlobMaster41",
6
6
  "description": "OPNet transaction library allows you to create and sign transactions for the OPNet network.",
7
7
  "engines": {
package/src/_version.ts CHANGED
@@ -1 +1 @@
1
- export const version = '1.1.11';
1
+ export const version = '1.1.13';
@@ -4,7 +4,7 @@ import { Map } from './Map.js';
4
4
 
5
5
  export class AddressMap<V> extends Map<Address, V> {
6
6
  public set(key: Address, value: V): void {
7
- const index: i32 = this._keys.indexOf(key);
7
+ const index: i32 = this.indexOf(key);
8
8
  if (index == -1) {
9
9
  this._keys.push(key);
10
10
  this._values.push(value);
@@ -228,6 +228,13 @@ export class Address extends Uint8Array {
228
228
  public toString(): string {
229
229
  return this.toHex();
230
230
  }
231
+
232
+ /**
233
+ * Convert the address to a JSON string
234
+ */
235
+ public toJSON(): string {
236
+ return this.toHex();
237
+ }
231
238
 
232
239
  /**
233
240
  * Get the address in p2tr format
@@ -91,7 +91,8 @@ export class TransactionFactory {
91
91
  parameters.utxos = interactionParameters.utxos;
92
92
  parameters.amount =
93
93
  (await preTransaction.estimateTransactionFees()) +
94
- this.getPriorityFee(interactionParameters);
94
+ this.getPriorityFee(interactionParameters) +
95
+ preTransaction.getOptionalOutputValue();
95
96
 
96
97
  const feeEstimationFundingTransaction = await this.createFundTransaction({ ...parameters });
97
98
  if (!feeEstimationFundingTransaction) {
@@ -151,7 +152,6 @@ export class TransactionFactory {
151
152
  });
152
153
 
153
154
  // we don't sign that transaction, we just need the parameters.
154
-
155
155
  await preTransaction.generateTransactionMinimalSignatures();
156
156
 
157
157
  const parameters: IFundingTransactionParameters =
@@ -160,7 +160,8 @@ export class TransactionFactory {
160
160
  parameters.utxos = interactionParameters.utxos;
161
161
  parameters.amount =
162
162
  (await preTransaction.estimateTransactionFees()) +
163
- this.getPriorityFee(interactionParameters);
163
+ this.getPriorityFee(interactionParameters) +
164
+ preTransaction.getOptionalOutputValue();
164
165
 
165
166
  const feeEstimationFundingTransaction = await this.createFundTransaction({ ...parameters });
166
167
  if (!feeEstimationFundingTransaction) {
@@ -219,7 +220,8 @@ export class TransactionFactory {
219
220
 
220
221
  parameters.amount =
221
222
  (await preTransaction.estimateTransactionFees()) +
222
- this.getPriorityFee(deploymentParameters);
223
+ this.getPriorityFee(deploymentParameters) +
224
+ preTransaction.getOptionalOutputValue();
223
225
 
224
226
  const fundingTransaction: FundingTransaction = new FundingTransaction(parameters);
225
227
  const signedTransaction: Transaction = await fundingTransaction.signTransaction();
@@ -177,7 +177,7 @@ export class CustomScriptTransaction extends TransactionBuilder<TransactionType.
177
177
  address: this.to,
178
178
  });
179
179
 
180
- await this.addRefundOutput(amountSpent);
180
+ await this.addRefundOutput(amountSpent + this.addOptionalOutputsAndGetAmount());
181
181
  }
182
182
 
183
183
  /**
@@ -201,7 +201,7 @@ export class DeploymentTransaction extends TransactionBuilder<TransactionType.DE
201
201
  address: this.contractAddress.p2tr(this.network),
202
202
  });
203
203
 
204
- await this.addRefundOutput(amountSpent);
204
+ await this.addRefundOutput(amountSpent + this.addOptionalOutputsAndGetAmount());
205
205
  }
206
206
 
207
207
  protected override async signInputsWalletBased(transaction: Psbt): Promise<void> {
@@ -40,7 +40,7 @@ export class FundingTransaction extends TransactionBuilder<TransactionType.FUNDI
40
40
  });
41
41
  }
42
42
 
43
- await this.addRefundOutput(this.amount);
43
+ await this.addRefundOutput(this.amount + this.addOptionalOutputsAndGetAmount());
44
44
  }
45
45
 
46
46
  protected splitInputs(amountSpent: bigint): void {
@@ -170,8 +170,9 @@ export abstract class SharedInteractionTransaction<
170
170
  address: this.to,
171
171
  });
172
172
 
173
+ const amount = this.addOptionalOutputsAndGetAmount();
173
174
  if (!this.disableAutoRefund) {
174
- await this.addRefundOutput(amountSpent);
175
+ await this.addRefundOutput(amountSpent + amount);
175
176
  }
176
177
  }
177
178
 
@@ -444,6 +444,17 @@ export abstract class TransactionBuilder<T extends TransactionType> extends Twea
444
444
  return outputs;
445
445
  }
446
446
 
447
+ public getOptionalOutputValue(): bigint {
448
+ if (!this.optionalOutputs) return 0n;
449
+
450
+ let total = 0n;
451
+ for (let i = 0; i < this.optionalOutputs.length; i++) {
452
+ total += BigInt(this.optionalOutputs[i].value);
453
+ }
454
+
455
+ return total;
456
+ }
457
+
447
458
  /**
448
459
  * @description Adds the refund output to the transaction
449
460
  * @param {bigint} amountSpent - The amount spent
@@ -452,8 +463,7 @@ export abstract class TransactionBuilder<T extends TransactionType> extends Twea
452
463
  */
453
464
  protected async addRefundOutput(amountSpent: bigint): Promise<void> {
454
465
  /** Add the refund output */
455
- const sendBackAmount: bigint =
456
- this.totalInputAmount - amountSpent - this.addOptionalOutputsAndGetAmount();
466
+ const sendBackAmount: bigint = this.totalInputAmount - amountSpent;
457
467
  if (sendBackAmount >= TransactionBuilder.MINIMUM_DUST) {
458
468
  if (AddressVerificator.isValidP2TRAddress(this.from, this.network)) {
459
469
  await this.setFeeOutput({
@@ -33,8 +33,7 @@ export interface SharedInteractionParameters extends ITransactionParameters {
33
33
  readonly randomBytes?: Buffer;
34
34
  }
35
35
 
36
- export interface IInteractionParameters
37
- extends Omit<SharedInteractionParameters, 'optionalOutputs'> {
36
+ export interface IInteractionParameters extends SharedInteractionParameters {
38
37
  readonly calldata: Buffer;
39
38
 
40
39
  readonly to: string;