@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
@@ -0,0 +1,150 @@
1
+ import { CustomKeypair } from '../BrowserSignerBase.js';
2
+ import { networks, Psbt } from 'bitcoinjs-lib';
3
+ import { UnisatNetwork } from '../types/Unisat.js';
4
+ import { EcKeyPair } from '../../../keypair/EcKeyPair.js';
5
+ export class UnisatSigner extends CustomKeypair {
6
+ isInitialized = false;
7
+ constructor() {
8
+ super();
9
+ if (!window) {
10
+ throw new Error('UnisatSigner can only be used in a browser environment');
11
+ }
12
+ }
13
+ _p2tr;
14
+ get p2tr() {
15
+ if (!this._p2tr) {
16
+ throw new Error('P2TR address not set');
17
+ }
18
+ return this._p2tr;
19
+ }
20
+ _p2pkh;
21
+ get p2pkh() {
22
+ if (!this._p2pkh) {
23
+ throw new Error('P2PKH address not set');
24
+ }
25
+ return this._p2pkh;
26
+ }
27
+ _addresses;
28
+ get addresses() {
29
+ if (!this._addresses) {
30
+ throw new Error('Addresses not set');
31
+ }
32
+ return this._addresses;
33
+ }
34
+ _publicKey;
35
+ get publicKey() {
36
+ if (!this._publicKey) {
37
+ throw new Error('Public key not set');
38
+ }
39
+ return this._publicKey;
40
+ }
41
+ _network;
42
+ get network() {
43
+ if (!this._network) {
44
+ throw new Error('Network not set');
45
+ }
46
+ return this._network;
47
+ }
48
+ get unisat() {
49
+ if (!window.unisat) {
50
+ throw new Error('Unisat extension not found');
51
+ }
52
+ return window.unisat;
53
+ }
54
+ async init() {
55
+ if (this.isInitialized) {
56
+ return;
57
+ }
58
+ const network = await this.unisat.getNetwork();
59
+ switch (network) {
60
+ case UnisatNetwork.mainnet:
61
+ this._network = networks.bitcoin;
62
+ break;
63
+ case UnisatNetwork.testnet:
64
+ this._network = networks.testnet;
65
+ break;
66
+ case UnisatNetwork.regtest:
67
+ this._network = networks.regtest;
68
+ break;
69
+ default:
70
+ throw new Error('Invalid network');
71
+ }
72
+ const publicKey = await this.unisat.getPublicKey();
73
+ this._publicKey = Buffer.from(publicKey, 'hex');
74
+ this._p2pkh = EcKeyPair.getP2WPKHAddress(this, this.network);
75
+ this._p2tr = EcKeyPair.getTaprootAddress(this, this.network);
76
+ this._addresses = [this._p2pkh, this._p2tr];
77
+ this.isInitialized = true;
78
+ }
79
+ getPublicKey() {
80
+ if (!this.isInitialized) {
81
+ throw new Error('UnisatSigner not initialized');
82
+ }
83
+ return this.publicKey;
84
+ }
85
+ sign(hash, lowR) {
86
+ throw new Error('Not implemented: sign');
87
+ }
88
+ signSchnorr(hash) {
89
+ throw new Error('Not implemented: signSchnorr');
90
+ }
91
+ verify(hash, signature) {
92
+ throw new Error('Not implemented: verify');
93
+ }
94
+ async signTaprootInput(transaction, i, sighashTypes) {
95
+ let firstSignature = await this.signTweaked(transaction, i, sighashTypes, false);
96
+ this.combine(transaction, firstSignature, i);
97
+ }
98
+ async signInput(transaction, i, sighashTypes) {
99
+ const secondSignature = await this.signTweaked(transaction, i, sighashTypes, true);
100
+ this.combine(transaction, secondSignature, i);
101
+ }
102
+ combine(transaction, newPsbt, i) {
103
+ const signedInput = newPsbt.data.inputs[i];
104
+ const originalInput = transaction.data.inputs[i];
105
+ if (signedInput.partialSig) {
106
+ transaction.updateInput(i, { partialSig: signedInput.partialSig });
107
+ }
108
+ if (signedInput.tapKeySig && !originalInput.tapKeySig) {
109
+ transaction.updateInput(i, { tapKeySig: signedInput.tapKeySig });
110
+ }
111
+ if (signedInput.tapScriptSig?.length) {
112
+ const lastScriptSig = originalInput.tapScriptSig;
113
+ if (lastScriptSig) {
114
+ const getNonDuplicate = this.getNonDuplicateScriptSig(lastScriptSig, signedInput.tapScriptSig);
115
+ if (getNonDuplicate.length) {
116
+ transaction.updateInput(i, { tapScriptSig: getNonDuplicate });
117
+ }
118
+ }
119
+ else {
120
+ transaction.updateInput(i, { tapScriptSig: signedInput.tapScriptSig });
121
+ }
122
+ }
123
+ }
124
+ async signTweaked(transaction, i, sighashTypes, disableTweakSigner = false) {
125
+ const opts = {
126
+ autoFinalized: false,
127
+ toSignInputs: [
128
+ {
129
+ index: i,
130
+ publicKey: this.publicKey.toString('hex'),
131
+ sighashTypes,
132
+ disableTweakSigner: disableTweakSigner,
133
+ },
134
+ ],
135
+ };
136
+ const psbt = transaction.toHex();
137
+ const signed = await this.unisat.signPsbt(psbt, opts);
138
+ return Psbt.fromHex(signed);
139
+ }
140
+ getNonDuplicateScriptSig(scriptSig1, scriptSig2) {
141
+ const nonDuplicate = [];
142
+ for (let i = 0; i < scriptSig2.length; i++) {
143
+ const found = scriptSig1.find((item) => item.pubkey.equals(scriptSig2[i].pubkey));
144
+ if (!found) {
145
+ nonDuplicate.push(scriptSig2[i]);
146
+ }
147
+ }
148
+ return nonDuplicate;
149
+ }
150
+ }
@@ -0,0 +1,54 @@
1
+ import { Address } from '@btc-vision/bsi-binary';
2
+ export declare enum UnisatNetwork {
3
+ testnet = "testnet",
4
+ mainnet = "livenet",
5
+ regtest = "regtest"
6
+ }
7
+ export interface Balance {
8
+ readonly confirmed: number;
9
+ readonly unconfirmed: number;
10
+ readonly total: number;
11
+ }
12
+ export declare enum MessageType {
13
+ ecdsa = "ecdsa",
14
+ bip322 = "bip322-simple"
15
+ }
16
+ interface ToSignInputBase {
17
+ readonly index: number;
18
+ readonly sighashTypes?: number[];
19
+ readonly disableTweakSigner?: boolean;
20
+ }
21
+ export interface ToSignInputPublicKey extends ToSignInputBase {
22
+ readonly publicKey: string;
23
+ }
24
+ export interface ToSignInputAddress extends ToSignInputBase {
25
+ readonly address: Address;
26
+ }
27
+ export type ToSignInput = ToSignInputPublicKey | ToSignInputAddress;
28
+ export interface PsbtSignatureOptions {
29
+ readonly autoFinalized?: boolean;
30
+ readonly toSignInputs?: ToSignInput[];
31
+ }
32
+ export interface Unisat {
33
+ sendBitcoin(toAddress: Address, satoshis: number, options: {
34
+ feeRate: number;
35
+ }): Promise<string>;
36
+ requestAccounts(): Promise<string[]>;
37
+ getNetwork(): Promise<UnisatNetwork>;
38
+ getAccounts(): Promise<string[]>;
39
+ switchNetwork(network: UnisatNetwork): Promise<void>;
40
+ getPublicKey(): Promise<string>;
41
+ getBalance(): Promise<Balance>;
42
+ signMessage(message: string, type?: MessageType): Promise<string>;
43
+ pushTx(options: {
44
+ rawtx: string;
45
+ }): Promise<string>;
46
+ signPsbt(psbtHex: string, psbtOptions: PsbtSignatureOptions): Promise<string>;
47
+ signPsbts(psbtHex: string[], psbtOptions: PsbtSignatureOptions): Promise<string[]>;
48
+ pushPsbt(psbtHex: string): Promise<string>;
49
+ on(event: 'accountsChanged', listener: (accounts: string[]) => void): void;
50
+ on(event: 'networkChanged', listener: (network: UnisatNetwork) => void): void;
51
+ removeListener(event: 'accountsChanged', listener: (accounts: string[]) => void): void;
52
+ removeListener(event: 'networkChanged', listener: (network: UnisatNetwork) => void): void;
53
+ }
54
+ export {};
@@ -0,0 +1,11 @@
1
+ export var UnisatNetwork;
2
+ (function (UnisatNetwork) {
3
+ UnisatNetwork["testnet"] = "testnet";
4
+ UnisatNetwork["mainnet"] = "livenet";
5
+ UnisatNetwork["regtest"] = "regtest";
6
+ })(UnisatNetwork || (UnisatNetwork = {}));
7
+ export var MessageType;
8
+ (function (MessageType) {
9
+ MessageType["ecdsa"] = "ecdsa";
10
+ MessageType["bip322"] = "bip322-simple";
11
+ })(MessageType || (MessageType = {}));
@@ -23,8 +23,8 @@ export declare class DeploymentTransaction extends TransactionBuilder<Transactio
23
23
  get p2trAddress(): Address;
24
24
  getRndBytes(): Buffer;
25
25
  protected contractSignerXOnlyPubKey(): Buffer;
26
- protected buildTransaction(): void;
27
- protected signInputs(transaction: Psbt): void;
26
+ protected buildTransaction(): Promise<void>;
27
+ protected signInputs(transaction: Psbt): Promise<void>;
28
28
  protected generateScriptAddress(): Payment;
29
29
  protected generateTapData(): Payment;
30
30
  private getContractSeed;
@@ -46,7 +46,7 @@ export class DeploymentTransaction extends TransactionBuilder {
46
46
  contractSignerXOnlyPubKey() {
47
47
  return toXOnly(this.contractSigner.publicKey);
48
48
  }
49
- buildTransaction() {
49
+ async buildTransaction() {
50
50
  if (!this.to) {
51
51
  this.to = this.getScriptAddress();
52
52
  }
@@ -73,11 +73,11 @@ export class DeploymentTransaction extends TransactionBuilder {
73
73
  value: Number(amountSpent),
74
74
  address: this.to,
75
75
  });
76
- this.addRefundOutput(amountSpent);
76
+ await this.addRefundOutput(amountSpent);
77
77
  }
78
- signInputs(transaction) {
78
+ async signInputs(transaction) {
79
79
  if (!this.contractSigner) {
80
- super.signInputs(transaction);
80
+ await super.signInputs(transaction);
81
81
  return;
82
82
  }
83
83
  for (let i = 0; i < transaction.data.inputs.length; i++) {
@@ -6,6 +6,6 @@ export declare class FundingTransaction extends TransactionBuilder<TransactionTy
6
6
  readonly type: TransactionType.FUNDING;
7
7
  protected childTransactionRequiredFees: bigint;
8
8
  constructor(parameters: IFundingTransactionParameters);
9
- protected buildTransaction(): void;
9
+ protected buildTransaction(): Promise<void>;
10
10
  protected getSignerKey(): Signer;
11
11
  }
@@ -8,7 +8,7 @@ export class FundingTransaction extends TransactionBuilder {
8
8
  this.childTransactionRequiredFees = parameters.childTransactionRequiredValue;
9
9
  this.internalInit();
10
10
  }
11
- buildTransaction() {
11
+ async buildTransaction() {
12
12
  if (!this.to) {
13
13
  throw new Error('Recipient address is required');
14
14
  }
@@ -18,7 +18,7 @@ export class FundingTransaction extends TransactionBuilder {
18
18
  value: Number(amountSpent),
19
19
  address: this.to,
20
20
  });
21
- this.addRefundOutput(amountSpent);
21
+ await this.addRefundOutput(amountSpent);
22
22
  }
23
23
  getSignerKey() {
24
24
  return this.signer;
@@ -48,10 +48,10 @@ export declare class MultiSignTransaction extends TransactionBuilder<Transaction
48
48
  static dedupeSignatures(original: TapScriptSig[], partial: TapScriptSig[]): TapScriptSig[];
49
49
  static attemptFinalizeInputs(psbt: Psbt, startIndex: number, orderedPubKeys: Buffer[][], isFinal: boolean): boolean;
50
50
  finalizeTransactionInputs(): boolean;
51
- signPSBT(): Psbt;
52
- protected buildTransaction(): void;
53
- protected internalBuildTransaction(transaction: Psbt): boolean;
54
- protected signInputs(_transaction: Psbt): void;
51
+ signPSBT(): Promise<Psbt>;
52
+ protected buildTransaction(): Promise<void>;
53
+ protected internalBuildTransaction(transaction: Psbt): Promise<boolean>;
54
+ protected signInputs(_transaction: Psbt): Promise<void>;
55
55
  protected generateScriptAddress(): Payment;
56
56
  protected generateTapData(): Payment;
57
57
  protected getScriptSolution(input: PsbtInput): Buffer[];
@@ -1,4 +1,4 @@
1
- import { crypto as bitcoinCrypto, opcodes, Psbt, script, } from 'bitcoinjs-lib';
1
+ import { crypto as bitcoinCrypto, opcodes, Psbt, script } from 'bitcoinjs-lib';
2
2
  import { TransactionBuilder } from './TransactionBuilder.js';
3
3
  import { TransactionType } from '../enums/TransactionType.js';
4
4
  import { MultiSignGenerator } from '../../generators/builders/MultiSignGenerator.js';
@@ -239,13 +239,13 @@ export class MultiSignTransaction extends TransactionBuilder {
239
239
  }
240
240
  return finalized;
241
241
  }
242
- signPSBT() {
243
- if (this.signTransaction()) {
242
+ async signPSBT() {
243
+ if (await this.signTransaction()) {
244
244
  return this.transaction;
245
245
  }
246
246
  throw new Error('Could not sign transaction');
247
247
  }
248
- buildTransaction() {
248
+ async buildTransaction() {
249
249
  const selectedRedeem = this.targetScriptRedeem;
250
250
  if (!selectedRedeem) {
251
251
  throw new Error('Left over funds script redeem is required');
@@ -275,7 +275,7 @@ export class MultiSignTransaction extends TransactionBuilder {
275
275
  value: Number(this.requestedAmount),
276
276
  });
277
277
  }
278
- internalBuildTransaction(transaction) {
278
+ async internalBuildTransaction(transaction) {
279
279
  const inputs = this.getInputs();
280
280
  const outputs = this.getOutputs();
281
281
  transaction.setMaximumFeeRate(this._maximumFeeRate);
@@ -285,7 +285,7 @@ export class MultiSignTransaction extends TransactionBuilder {
285
285
  }
286
286
  transaction.addOutputs(outputs);
287
287
  try {
288
- this.signInputs(transaction);
288
+ await this.signInputs(transaction);
289
289
  return this.finalizeTransactionInputs();
290
290
  }
291
291
  catch (e) {
@@ -294,7 +294,7 @@ export class MultiSignTransaction extends TransactionBuilder {
294
294
  }
295
295
  return false;
296
296
  }
297
- signInputs(_transaction) { }
297
+ async signInputs(_transaction) { }
298
298
  generateScriptAddress() {
299
299
  return {
300
300
  internalPubkey: toXOnly(MultiSignTransaction.numsPoint),
@@ -24,8 +24,8 @@ export declare abstract class SharedInteractionTransaction<T extends Transaction
24
24
  protected generateSecret(): Buffer;
25
25
  protected scriptSignerXOnlyPubKey(): Buffer;
26
26
  protected generateKeyPairFromSeed(): ECPairInterface;
27
- protected buildTransaction(): void;
28
- protected signInputs(transaction: Psbt): void;
27
+ protected buildTransaction(): Promise<void>;
28
+ protected signInputs(transaction: Psbt): Promise<void>;
29
29
  protected generateScriptAddress(): Payment;
30
30
  protected generateTapData(): Payment;
31
31
  protected getScriptSolution(input: PsbtInput): Buffer[];
@@ -41,7 +41,7 @@ export class SharedInteractionTransaction extends TransactionBuilder {
41
41
  generateKeyPairFromSeed() {
42
42
  return EcKeyPair.fromSeedKeyPair(this.randomBytes, this.network);
43
43
  }
44
- buildTransaction() {
44
+ async buildTransaction() {
45
45
  if (!this.to)
46
46
  throw new Error('To address is required');
47
47
  const selectedRedeem = !!this.scriptSigner
@@ -70,12 +70,12 @@ export class SharedInteractionTransaction extends TransactionBuilder {
70
70
  address: this.to,
71
71
  });
72
72
  if (!this.disableAutoRefund) {
73
- this.addRefundOutput(amountSpent);
73
+ await this.addRefundOutput(amountSpent);
74
74
  }
75
75
  }
76
- signInputs(transaction) {
76
+ async signInputs(transaction) {
77
77
  if (!this.scriptSigner) {
78
- super.signInputs(transaction);
78
+ await super.signInputs(transaction);
79
79
  return;
80
80
  }
81
81
  for (let i = 0; i < transaction.data.inputs.length; i++) {
@@ -83,12 +83,12 @@ export class SharedInteractionTransaction extends TransactionBuilder {
83
83
  let finalized = false;
84
84
  let signed = false;
85
85
  try {
86
- this.signInput(transaction, input, i, this.scriptSigner);
86
+ await this.signInput(transaction, input, i, this.scriptSigner);
87
87
  signed = true;
88
88
  }
89
89
  catch (e) { }
90
90
  try {
91
- this.signInput(transaction, input, i);
91
+ await this.signInput(transaction, input, i);
92
92
  signed = true;
93
93
  }
94
94
  catch (e) { }
@@ -14,6 +14,7 @@ export declare abstract class TransactionBuilder<T extends TransactionType> exte
14
14
  readonly logColor: string;
15
15
  overflowFees: bigint;
16
16
  transactionFee: bigint;
17
+ estimatedFees: bigint;
17
18
  protected transaction: Psbt;
18
19
  protected readonly updateInputs: UpdateInput[];
19
20
  protected readonly outputs: PsbtOutputExtended[];
@@ -30,32 +31,32 @@ export declare abstract class TransactionBuilder<T extends TransactionType> exte
30
31
  protected constructor(parameters: ITransactionParameters);
31
32
  static getFrom(from: string | undefined, keypair: ECPairInterface, network: Network): Address;
32
33
  static witnessStackToScriptWitness(witness: Buffer[]): Buffer;
33
- getFundingTransactionParameters(): IFundingTransactionParameters;
34
+ getFundingTransactionParameters(): Promise<IFundingTransactionParameters>;
34
35
  setDestinationAddress(address: Address): void;
35
36
  setMaximumFeeRate(feeRate: number): void;
36
- signTransaction(): Transaction;
37
- signPSBT(): Psbt;
37
+ signTransaction(): Promise<Transaction>;
38
+ signPSBT(): Promise<Psbt>;
38
39
  addInput(input: PsbtInputExtended): void;
39
40
  addOutput(output: PsbtOutputExtended): void;
40
41
  toAddress(): string | undefined;
41
42
  address(): Address | undefined;
42
- estimateTransactionFees(): bigint;
43
- rebuildFromBase64(base64: string): Psbt;
43
+ estimateTransactionFees(): Promise<bigint>;
44
+ rebuildFromBase64(base64: string): Promise<Psbt>;
44
45
  setPSBT(psbt: Psbt): void;
45
- protected addRefundOutput(amountSpent: bigint): void;
46
+ protected addRefundOutput(amountSpent: bigint): Promise<void>;
46
47
  protected addValueToToOutput(value: number | bigint): void;
47
48
  protected getTransactionOPNetFee(): bigint;
48
49
  protected calculateTotalUTXOAmount(): bigint;
49
50
  protected calculateTotalVOutAmount(): bigint;
50
51
  protected addInputsFromUTXO(): void;
51
52
  protected internalInit(): void;
52
- protected abstract buildTransaction(): void;
53
+ protected abstract buildTransaction(): Promise<void>;
53
54
  protected updateInput(input: UpdateInput): void;
54
55
  protected getWitness(): Buffer;
55
56
  protected getTapOutput(): Buffer;
56
57
  protected getInputs(): PsbtInputExtended[];
57
58
  protected getOutputs(): PsbtOutputExtended[];
58
59
  protected verifyUTXOValidity(): void;
59
- protected setFeeOutput(output: PsbtOutputExtended): void;
60
- protected internalBuildTransaction(transaction: Psbt): boolean;
60
+ protected setFeeOutput(output: PsbtOutputExtended): Promise<void>;
61
+ protected internalBuildTransaction(transaction: Psbt): Promise<boolean>;
61
62
  }
@@ -13,6 +13,7 @@ export class TransactionBuilder extends TweakedTransaction {
13
13
  logColor = '#785def';
14
14
  overflowFees = 0n;
15
15
  transactionFee = 0n;
16
+ estimatedFees = 0n;
16
17
  transaction;
17
18
  updateInputs = [];
18
19
  outputs = [];
@@ -28,6 +29,9 @@ export class TransactionBuilder extends TweakedTransaction {
28
29
  _maximumFeeRate = 100000000;
29
30
  constructor(parameters) {
30
31
  super(parameters);
32
+ if (parameters.estimatedFees) {
33
+ this.estimatedFees = parameters.estimatedFees;
34
+ }
31
35
  this.signer = parameters.signer;
32
36
  this.network = parameters.network;
33
37
  this.feeRate = parameters.feeRate;
@@ -69,9 +73,9 @@ export class TransactionBuilder extends TweakedTransaction {
69
73
  writeVector(witness);
70
74
  return buffer;
71
75
  }
72
- getFundingTransactionParameters() {
76
+ async getFundingTransactionParameters() {
73
77
  if (!this.transactionFee) {
74
- this.transactionFee = this.estimateTransactionFees();
78
+ this.transactionFee = await this.estimateTransactionFees();
75
79
  }
76
80
  return {
77
81
  utxos: this.utxos,
@@ -90,15 +94,15 @@ export class TransactionBuilder extends TweakedTransaction {
90
94
  setMaximumFeeRate(feeRate) {
91
95
  this._maximumFeeRate = feeRate;
92
96
  }
93
- signTransaction() {
97
+ async signTransaction() {
94
98
  if (this.to && !EcKeyPair.verifyContractAddress(this.to, this.network)) {
95
99
  throw new Error('Invalid contract address. The contract address must be a taproot address.');
96
100
  }
97
101
  if (this.signed)
98
102
  throw new Error('Transaction is already signed');
99
103
  this.signed = true;
100
- this.buildTransaction();
101
- const builtTx = this.internalBuildTransaction(this.transaction);
104
+ await this.buildTransaction();
105
+ const builtTx = await this.internalBuildTransaction(this.transaction);
102
106
  if (builtTx) {
103
107
  if (this.regenerated) {
104
108
  throw new Error('Transaction was regenerated');
@@ -107,8 +111,8 @@ export class TransactionBuilder extends TweakedTransaction {
107
111
  }
108
112
  throw new Error('Could not sign transaction');
109
113
  }
110
- signPSBT() {
111
- if (this.signTransaction()) {
114
+ async signPSBT() {
115
+ if (await this.signTransaction()) {
112
116
  return this.transaction;
113
117
  }
114
118
  throw new Error('Could not sign transaction');
@@ -130,42 +134,45 @@ export class TransactionBuilder extends TweakedTransaction {
130
134
  address() {
131
135
  return this.tapData?.address;
132
136
  }
133
- estimateTransactionFees() {
137
+ async estimateTransactionFees() {
138
+ if (this.estimatedFees)
139
+ return this.estimatedFees;
134
140
  const fakeTx = new Psbt({
135
141
  network: this.network,
136
142
  });
137
- const builtTx = this.internalBuildTransaction(fakeTx);
143
+ const builtTx = await this.internalBuildTransaction(fakeTx);
138
144
  if (builtTx) {
139
145
  const tx = fakeTx.extractTransaction(false);
140
146
  const size = tx.virtualSize();
141
147
  const fee = this.feeRate * size + 1;
142
- return BigInt(Math.ceil(fee) + 1);
148
+ this.estimatedFees = BigInt(Math.ceil(fee) + 1);
149
+ return this.estimatedFees;
143
150
  }
144
151
  else {
145
152
  throw new Error(`Could not build transaction to estimate fee. Something went wrong while building the transaction.`);
146
153
  }
147
154
  }
148
- rebuildFromBase64(base64) {
155
+ async rebuildFromBase64(base64) {
149
156
  this.transaction = Psbt.fromBase64(base64, { network: this.network });
150
157
  this.signed = false;
151
158
  this.sighashTypes = [Transaction.SIGHASH_ANYONECANPAY, Transaction.SIGHASH_ALL];
152
- return this.signPSBT();
159
+ return await this.signPSBT();
153
160
  }
154
161
  setPSBT(psbt) {
155
162
  this.transaction = psbt;
156
163
  }
157
- addRefundOutput(amountSpent) {
164
+ async addRefundOutput(amountSpent) {
158
165
  const sendBackAmount = this.totalInputAmount - amountSpent;
159
166
  if (sendBackAmount >= TransactionBuilder.MINIMUM_DUST) {
160
167
  if (AddressVerificator.isValidP2TRAddress(this.from, this.network)) {
161
- this.setFeeOutput({
168
+ await this.setFeeOutput({
162
169
  value: Number(sendBackAmount),
163
170
  address: this.from,
164
171
  tapInternalKey: this.internalPubKeyToXOnly(),
165
172
  });
166
173
  }
167
174
  else {
168
- this.setFeeOutput({
175
+ await this.setFeeOutput({
169
176
  value: Number(sendBackAmount),
170
177
  address: this.from,
171
178
  });
@@ -256,21 +263,21 @@ export class TransactionBuilder extends TweakedTransaction {
256
263
  }
257
264
  }
258
265
  }
259
- setFeeOutput(output) {
266
+ async setFeeOutput(output) {
260
267
  const initialValue = output.value;
261
- let fee = this.estimateTransactionFees();
268
+ let fee = await this.estimateTransactionFees();
262
269
  output.value = initialValue - Number(fee);
263
270
  if (output.value < TransactionBuilder.MINIMUM_DUST) {
264
271
  this.feeOutput = null;
265
272
  if (output.value < 0) {
266
- throw new Error(`Insufficient funds to pay the fees. Fee: ${fee} > Value: ${initialValue}. Total input: ${this.totalInputAmount} sat`);
273
+ throw new Error(`setFeeOutput: Insufficient funds to pay the fees. Fee: ${fee} > Value: ${initialValue}. Total input: ${this.totalInputAmount} sat`);
267
274
  }
268
275
  }
269
276
  else {
270
277
  this.feeOutput = output;
271
- let fee = this.estimateTransactionFees();
278
+ let fee = await this.estimateTransactionFees();
272
279
  if (fee > BigInt(initialValue)) {
273
- throw new Error(`Insufficient funds to pay the fees. Fee: ${fee} > Value: ${initialValue}. Total input: ${this.totalInputAmount} sat`);
280
+ throw new Error(`estimateTransactionFees: Insufficient funds to pay the fees. Fee: ${fee} > Value: ${initialValue}. Total input: ${this.totalInputAmount} sat`);
274
281
  }
275
282
  const valueLeft = initialValue - Number(fee);
276
283
  if (valueLeft < TransactionBuilder.MINIMUM_DUST) {
@@ -282,7 +289,7 @@ export class TransactionBuilder extends TweakedTransaction {
282
289
  this.overflowFees = BigInt(valueLeft);
283
290
  }
284
291
  }
285
- internalBuildTransaction(transaction) {
292
+ async internalBuildTransaction(transaction) {
286
293
  if (transaction.data.inputs.length === 0) {
287
294
  const inputs = this.getInputs();
288
295
  const outputs = this.getOutputs();
@@ -294,7 +301,7 @@ export class TransactionBuilder extends TweakedTransaction {
294
301
  transaction.addOutputs(outputs);
295
302
  }
296
303
  try {
297
- this.signInputs(transaction);
304
+ await this.signInputs(transaction);
298
305
  if (this.finalized) {
299
306
  this.transactionFee = BigInt(transaction.getFee());
300
307
  }
@@ -18,9 +18,9 @@ export declare class UnwrapSegwitTransaction extends SharedInteractionTransactio
18
18
  private readonly calculatedSignHash;
19
19
  constructor(parameters: IUnwrapParameters);
20
20
  static generateBurnCalldata(amount: bigint): Buffer;
21
- signPSBT(): Psbt;
22
- mergeVaults(input: VaultUTXOs[]): void;
23
- protected internalBuildTransaction(transaction: Psbt): boolean;
21
+ signPSBT(): Promise<Psbt>;
22
+ mergeVaults(input: VaultUTXOs[]): Promise<void>;
23
+ protected internalBuildTransaction(transaction: Psbt): Promise<boolean>;
24
24
  protected generateMultiSignRedeemScript(publicKeys: string[], minimum: number): {
25
25
  witnessUtxo: Buffer;
26
26
  redeemScript: Buffer;