@btc-vision/transaction 1.0.87 → 1.0.89

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.
@@ -1,6 +1,10 @@
1
+ import { Transaction } from 'bitcoinjs-lib';
1
2
  import { IDeploymentParameters, IFundingTransactionParameters, IInteractionParameters, IUnwrapParameters, IWrapParameters } from './interfaces/ITransactionParameters.js';
3
+ import { FundingTransaction } from './builders/FundingTransaction.js';
2
4
  import { UTXO } from '../utxo/interfaces/IUTXO.js';
3
5
  import { Address } from '@btc-vision/bsi-binary';
6
+ import { TransactionBuilder } from './builders/TransactionBuilder.js';
7
+ import { TransactionType } from './enums/TransactionType.js';
4
8
  export interface DeploymentResult {
5
9
  readonly transaction: [string, string];
6
10
  readonly contractAddress: Address;
@@ -14,6 +18,18 @@ export interface WrapResult {
14
18
  readonly receiverAddress: Address;
15
19
  readonly utxos: UTXO[];
16
20
  }
21
+ export interface FundingTransactionResponse {
22
+ readonly tx: Transaction;
23
+ readonly original: FundingTransaction;
24
+ readonly estimatedFees: bigint;
25
+ readonly nextUTXOs: UTXO[];
26
+ }
27
+ export interface BitcoinTransferResponse {
28
+ readonly tx: string;
29
+ readonly original: FundingTransaction;
30
+ readonly estimatedFees: bigint;
31
+ readonly nextUTXOs: UTXO[];
32
+ }
17
33
  export interface UnwrapResult {
18
34
  readonly fundingTransaction: string;
19
35
  readonly psbt: string;
@@ -27,11 +43,8 @@ export declare class TransactionFactory {
27
43
  wrap(warpParameters: Omit<IWrapParameters, 'calldata'>): Promise<WrapResult>;
28
44
  unwrapSegwit(unwrapParameters: IUnwrapParameters): Promise<UnwrapResult>;
29
45
  unwrap(unwrapParameters: IUnwrapParameters): Promise<UnwrapResult>;
30
- createBTCTransfer(parameters: IFundingTransactionParameters): Promise<{
31
- estimatedFees: bigint;
32
- tx: string;
33
- nextUTXOs: UTXO[];
34
- }>;
46
+ createBTCTransfer(parameters: IFundingTransactionParameters): Promise<BitcoinTransferResponse>;
47
+ getAllNewUTXOs(original: TransactionBuilder<TransactionType>, tx: Transaction, to: Address): UTXO[];
35
48
  private createFundTransaction;
36
49
  private calculateNumSignatures;
37
50
  private calculateNumInputs;
@@ -4,8 +4,10 @@ import { Signer } from 'bitcoinjs-lib';
4
4
  import { TransactionBuilder } from './TransactionBuilder.js';
5
5
  export declare class FundingTransaction extends TransactionBuilder<TransactionType.FUNDING> {
6
6
  readonly type: TransactionType.FUNDING;
7
- protected childTransactionRequiredFees: bigint;
7
+ protected amount: bigint;
8
+ protected splitInputsInto: number;
8
9
  constructor(parameters: IFundingTransactionParameters);
9
10
  protected buildTransaction(): Promise<void>;
11
+ protected splitInputs(amountSpent: bigint): Promise<void>;
10
12
  protected getSignerKey(): Signer;
11
13
  }
@@ -44,6 +44,8 @@ export declare abstract class TransactionBuilder<T extends TransactionType> exte
44
44
  estimateTransactionFees(): Promise<bigint>;
45
45
  rebuildFromBase64(base64: string): Promise<Psbt>;
46
46
  setPSBT(psbt: Psbt): void;
47
+ getInputs(): PsbtInputExtended[];
48
+ getOutputs(): PsbtOutputExtended[];
47
49
  protected addRefundOutput(amountSpent: bigint): Promise<void>;
48
50
  protected addValueToToOutput(value: number | bigint): void;
49
51
  protected getTransactionOPNetFee(): bigint;
@@ -55,8 +57,6 @@ export declare abstract class TransactionBuilder<T extends TransactionType> exte
55
57
  protected updateInput(input: UpdateInput): void;
56
58
  protected getWitness(): Buffer;
57
59
  protected getTapOutput(): Buffer;
58
- protected getInputs(): PsbtInputExtended[];
59
- protected getOutputs(): PsbtOutputExtended[];
60
60
  protected verifyUTXOValidity(): void;
61
61
  protected setFeeOutput(output: PsbtOutputExtended): Promise<void>;
62
62
  protected internalBuildTransaction(transaction: Psbt): Promise<boolean>;
@@ -13,10 +13,11 @@ export interface ITransactionParameters extends ITweakedTransactionData {
13
13
  estimatedFees?: bigint;
14
14
  chainId?: ChainId;
15
15
  readonly feeRate: number;
16
- readonly priorityFee: bigint;
16
+ readonly priorityFee?: bigint;
17
17
  }
18
18
  export interface IFundingTransactionParameters extends ITransactionParameters {
19
19
  amount: bigint;
20
+ splitInputsInto?: number;
20
21
  }
21
22
  export interface SharedInteractionParameters extends ITransactionParameters {
22
23
  calldata?: Buffer | undefined;
@@ -1 +1 @@
1
- export declare const version = "1.0.87";
1
+ export declare const version = "1.0.89";
package/build/_version.js CHANGED
@@ -1 +1 @@
1
- export const version = '1.0.87';
1
+ export const version = '1.0.89';
@@ -1,6 +1,10 @@
1
+ import { Transaction } from 'bitcoinjs-lib';
1
2
  import { IDeploymentParameters, IFundingTransactionParameters, IInteractionParameters, IUnwrapParameters, IWrapParameters } from './interfaces/ITransactionParameters.js';
3
+ import { FundingTransaction } from './builders/FundingTransaction.js';
2
4
  import { UTXO } from '../utxo/interfaces/IUTXO.js';
3
5
  import { Address } from '@btc-vision/bsi-binary';
6
+ import { TransactionBuilder } from './builders/TransactionBuilder.js';
7
+ import { TransactionType } from './enums/TransactionType.js';
4
8
  export interface DeploymentResult {
5
9
  readonly transaction: [string, string];
6
10
  readonly contractAddress: Address;
@@ -14,6 +18,18 @@ export interface WrapResult {
14
18
  readonly receiverAddress: Address;
15
19
  readonly utxos: UTXO[];
16
20
  }
21
+ export interface FundingTransactionResponse {
22
+ readonly tx: Transaction;
23
+ readonly original: FundingTransaction;
24
+ readonly estimatedFees: bigint;
25
+ readonly nextUTXOs: UTXO[];
26
+ }
27
+ export interface BitcoinTransferResponse {
28
+ readonly tx: string;
29
+ readonly original: FundingTransaction;
30
+ readonly estimatedFees: bigint;
31
+ readonly nextUTXOs: UTXO[];
32
+ }
17
33
  export interface UnwrapResult {
18
34
  readonly fundingTransaction: string;
19
35
  readonly psbt: string;
@@ -27,11 +43,8 @@ export declare class TransactionFactory {
27
43
  wrap(warpParameters: Omit<IWrapParameters, 'calldata'>): Promise<WrapResult>;
28
44
  unwrapSegwit(unwrapParameters: IUnwrapParameters): Promise<UnwrapResult>;
29
45
  unwrap(unwrapParameters: IUnwrapParameters): Promise<UnwrapResult>;
30
- createBTCTransfer(parameters: IFundingTransactionParameters): Promise<{
31
- estimatedFees: bigint;
32
- tx: string;
33
- nextUTXOs: UTXO[];
34
- }>;
46
+ createBTCTransfer(parameters: IFundingTransactionParameters): Promise<BitcoinTransferResponse>;
47
+ getAllNewUTXOs(original: TransactionBuilder<TransactionType>, tx: Transaction, to: Address): UTXO[];
35
48
  private createFundTransaction;
36
49
  private calculateNumSignatures;
37
50
  private calculateNumInputs;
@@ -230,11 +230,30 @@ export class TransactionFactory {
230
230
  const resp = await this.createFundTransaction(parameters);
231
231
  return {
232
232
  estimatedFees: resp.estimatedFees,
233
+ original: resp.original,
233
234
  tx: resp.tx.toHex(),
234
- nextUTXOs: this.getUTXOAsTransaction(resp.tx, parameters.from, 1),
235
+ nextUTXOs: this.getAllNewUTXOs(resp.original, resp.tx, parameters.from),
235
236
  };
236
237
  }
238
+ getAllNewUTXOs(original, tx, to) {
239
+ const outputs = original.getOutputs();
240
+ const utxos = [];
241
+ for (let i = 0; i < tx.outs.length; i++) {
242
+ const output = outputs[i];
243
+ if ('address' in output) {
244
+ if (output.address !== to)
245
+ continue;
246
+ }
247
+ else {
248
+ continue;
249
+ }
250
+ utxos.push(...this.getUTXOAsTransaction(tx, to, i));
251
+ }
252
+ return utxos;
253
+ }
237
254
  async createFundTransaction(parameters) {
255
+ if (!parameters.to)
256
+ throw new Error('Field "to" not provided.');
238
257
  const fundingTransaction = new FundingTransaction(parameters);
239
258
  const signedTransaction = await fundingTransaction.signTransaction();
240
259
  if (!signedTransaction) {
@@ -243,7 +262,8 @@ export class TransactionFactory {
243
262
  return {
244
263
  tx: signedTransaction,
245
264
  original: fundingTransaction,
246
- estimatedFees: await fundingTransaction.estimateTransactionFees(),
265
+ estimatedFees: fundingTransaction.estimatedFees,
266
+ nextUTXOs: this.getUTXOAsTransaction(signedTransaction, parameters.to, 0),
247
267
  };
248
268
  }
249
269
  calculateNumSignatures(vault) {
@@ -4,8 +4,10 @@ import { Signer } from 'bitcoinjs-lib';
4
4
  import { TransactionBuilder } from './TransactionBuilder.js';
5
5
  export declare class FundingTransaction extends TransactionBuilder<TransactionType.FUNDING> {
6
6
  readonly type: TransactionType.FUNDING;
7
- protected childTransactionRequiredFees: bigint;
7
+ protected amount: bigint;
8
+ protected splitInputsInto: number;
8
9
  constructor(parameters: IFundingTransactionParameters);
9
10
  protected buildTransaction(): Promise<void>;
11
+ protected splitInputs(amountSpent: bigint): Promise<void>;
10
12
  protected getSignerKey(): Signer;
11
13
  }
@@ -2,10 +2,12 @@ import { TransactionType } from '../enums/TransactionType.js';
2
2
  import { TransactionBuilder } from './TransactionBuilder.js';
3
3
  export class FundingTransaction extends TransactionBuilder {
4
4
  type = TransactionType.FUNDING;
5
- childTransactionRequiredFees;
5
+ amount;
6
+ splitInputsInto;
6
7
  constructor(parameters) {
7
8
  super(parameters);
8
- this.childTransactionRequiredFees = parameters.amount;
9
+ this.amount = parameters.amount;
10
+ this.splitInputsInto = parameters.splitInputsInto ?? 1;
9
11
  this.internalInit();
10
12
  }
11
13
  async buildTransaction() {
@@ -13,13 +15,38 @@ export class FundingTransaction extends TransactionBuilder {
13
15
  throw new Error('Recipient address is required');
14
16
  }
15
17
  this.addInputsFromUTXO();
16
- const amountSpent = this.getTransactionOPNetFee() + this.childTransactionRequiredFees;
17
- this.addOutput({
18
- value: Number(amountSpent),
19
- address: this.to,
20
- });
18
+ let amountSpent = this.amount;
19
+ if (this.getTransactionOPNetFee() === TransactionBuilder.MINIMUM_DUST) {
20
+ if (amountSpent < TransactionBuilder.MINIMUM_DUST) {
21
+ amountSpent += TransactionBuilder.MINIMUM_DUST;
22
+ }
23
+ }
24
+ else {
25
+ amountSpent += this.getTransactionOPNetFee();
26
+ }
27
+ if (this.splitInputsInto > 1) {
28
+ await this.splitInputs(amountSpent);
29
+ }
30
+ else {
31
+ this.addOutput({
32
+ value: Number(amountSpent),
33
+ address: this.to,
34
+ });
35
+ }
21
36
  await this.addRefundOutput(amountSpent);
22
37
  }
38
+ async splitInputs(amountSpent) {
39
+ if (!this.to) {
40
+ throw new Error('Recipient address is required');
41
+ }
42
+ const splitAmount = amountSpent / BigInt(this.splitInputsInto);
43
+ for (let i = 0; i < this.splitInputsInto; i++) {
44
+ this.addOutput({
45
+ value: Number(splitAmount),
46
+ address: this.to,
47
+ });
48
+ }
49
+ }
23
50
  getSignerKey() {
24
51
  return this.signer;
25
52
  }
@@ -44,6 +44,8 @@ export declare abstract class TransactionBuilder<T extends TransactionType> exte
44
44
  estimateTransactionFees(): Promise<bigint>;
45
45
  rebuildFromBase64(base64: string): Promise<Psbt>;
46
46
  setPSBT(psbt: Psbt): void;
47
+ getInputs(): PsbtInputExtended[];
48
+ getOutputs(): PsbtOutputExtended[];
47
49
  protected addRefundOutput(amountSpent: bigint): Promise<void>;
48
50
  protected addValueToToOutput(value: number | bigint): void;
49
51
  protected getTransactionOPNetFee(): bigint;
@@ -55,8 +57,6 @@ export declare abstract class TransactionBuilder<T extends TransactionType> exte
55
57
  protected updateInput(input: UpdateInput): void;
56
58
  protected getWitness(): Buffer;
57
59
  protected getTapOutput(): Buffer;
58
- protected getInputs(): PsbtInputExtended[];
59
- protected getOutputs(): PsbtOutputExtended[];
60
60
  protected verifyUTXOValidity(): void;
61
61
  protected setFeeOutput(output: PsbtOutputExtended): Promise<void>;
62
62
  protected internalBuildTransaction(transaction: Psbt): Promise<boolean>;
@@ -35,9 +35,12 @@ export class TransactionBuilder extends TweakedTransaction {
35
35
  this.signer = parameters.signer;
36
36
  this.network = parameters.network;
37
37
  this.feeRate = parameters.feeRate;
38
- this.priorityFee = parameters.priorityFee;
38
+ this.priorityFee = parameters.priorityFee ?? 0n;
39
39
  this.utxos = parameters.utxos;
40
40
  this.to = parameters.to || undefined;
41
+ if (!this.utxos.length) {
42
+ throw new Error('No UTXOs specified');
43
+ }
41
44
  this.from = TransactionBuilder.getFrom(parameters.from, this.signer, this.network);
42
45
  this.totalInputAmount = this.calculateTotalUTXOAmount();
43
46
  const totalVOut = this.calculateTotalVOutAmount();
@@ -83,7 +86,7 @@ export class TransactionBuilder extends TweakedTransaction {
83
86
  signer: this.signer,
84
87
  network: this.network,
85
88
  feeRate: this.feeRate,
86
- priorityFee: this.priorityFee,
89
+ priorityFee: this.priorityFee ?? 0n,
87
90
  from: this.from,
88
91
  amount: this.estimatedFees,
89
92
  };
@@ -177,6 +180,15 @@ export class TransactionBuilder extends TweakedTransaction {
177
180
  setPSBT(psbt) {
178
181
  this.transaction = psbt;
179
182
  }
183
+ getInputs() {
184
+ return this.inputs;
185
+ }
186
+ getOutputs() {
187
+ const outputs = [...this.outputs];
188
+ if (this.feeOutput)
189
+ outputs.push(this.feeOutput);
190
+ return outputs;
191
+ }
180
192
  async addRefundOutput(amountSpent) {
181
193
  const sendBackAmount = this.totalInputAmount - amountSpent;
182
194
  if (sendBackAmount >= TransactionBuilder.MINIMUM_DUST) {
@@ -263,15 +275,6 @@ export class TransactionBuilder extends TweakedTransaction {
263
275
  }
264
276
  return this.tapData.output;
265
277
  }
266
- getInputs() {
267
- return this.inputs;
268
- }
269
- getOutputs() {
270
- const outputs = [...this.outputs];
271
- if (this.feeOutput)
272
- outputs.push(this.feeOutput);
273
- return outputs;
274
- }
275
278
  verifyUTXOValidity() {
276
279
  for (let utxo of this.utxos) {
277
280
  if (!utxo.scriptPubKey) {
@@ -13,10 +13,11 @@ export interface ITransactionParameters extends ITweakedTransactionData {
13
13
  estimatedFees?: bigint;
14
14
  chainId?: ChainId;
15
15
  readonly feeRate: number;
16
- readonly priorityFee: bigint;
16
+ readonly priorityFee?: bigint;
17
17
  }
18
18
  export interface IFundingTransactionParameters extends ITransactionParameters {
19
19
  amount: bigint;
20
+ splitInputsInto?: number;
20
21
  }
21
22
  export interface SharedInteractionParameters extends ITransactionParameters {
22
23
  calldata?: Buffer | undefined;
@@ -101,27 +101,22 @@ export class OPNetLimitedProvider {
101
101
  }),
102
102
  };
103
103
  const url = `${this.opnetAPIUrl}/api/v1/${this.rpc}`;
104
- try {
105
- const resp = await fetch(url, params);
106
- if (!resp.ok) {
107
- throw new Error(`Failed to fetch to rpc: ${resp.statusText}`);
108
- }
109
- const fetchedData = await resp.json();
110
- if (!fetchedData) {
111
- throw new Error('No data fetched');
112
- }
113
- const result = fetchedData.result;
114
- if (!result) {
115
- throw new Error('No rpc parameters found');
116
- }
117
- if ('error' in result) {
118
- throw new Error(`Error in fetching to rpc ${result.error}`);
119
- }
120
- return result;
104
+ const resp = await fetch(url, params);
105
+ if (!resp.ok) {
106
+ throw new Error(`Failed to fetch to rpc: ${resp.statusText}`);
121
107
  }
122
- catch (e) {
123
- console.error(`Failed to fetch wrap parameters: ${e.stack}`);
108
+ const fetchedData = await resp.json();
109
+ if (!fetchedData) {
110
+ throw new Error('No data fetched');
111
+ }
112
+ const result = fetchedData.result;
113
+ if (!result) {
114
+ throw new Error('No rpc parameters found');
124
115
  }
116
+ if ('error' in result) {
117
+ throw new Error(`Error in fetching to rpc ${result.error}`);
118
+ }
119
+ return result;
125
120
  }
126
121
  async fetchWrapParameters(amount) {
127
122
  if (amount < currentConsensusConfig.VAULT_MINIMUM_AMOUNT) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "type": "module",
3
- "version": "1.0.87",
3
+ "version": "1.0.89",
4
4
  "author": "BlobMaster41",
5
5
  "description": "OPNet transaction library allows you to create and sign transactions for the OPNet network.",
6
6
  "engines": {
package/src/_version.ts CHANGED
@@ -1 +1 @@
1
- export const version = '1.0.87';
1
+ export const version = '1.0.89';