@btc-vision/transaction 1.0.0
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.
- package/.babelrc +7 -0
- package/.gitattributes +2 -0
- package/.github/workflows/node.js.yml +22 -0
- package/.idea/codeStyles/Project.xml +56 -0
- package/.idea/codeStyles/codeStyleConfig.xml +5 -0
- package/.idea/inspectionProfiles/Project_Default.xml +6 -0
- package/.idea/jsLibraryMappings.xml +6 -0
- package/.idea/modules.xml +8 -0
- package/.idea/prettier.xml +6 -0
- package/.idea/transaction.iml +16 -0
- package/.idea/vcs.xml +6 -0
- package/.prettierrc.json +12 -0
- package/CONTRIBUTING.md +71 -0
- package/LICENSE.md +16 -0
- package/README.md +213 -0
- package/browser/873e754d6c7c6e9361f1.module.wasm +0 -0
- package/browser/_version.d.ts +1 -0
- package/browser/bytecode/Compressor.d.ts +6 -0
- package/browser/crypto/crypto-browser.d.ts +40 -0
- package/browser/crypto/crypto.d.ts +2 -0
- package/browser/generators/Generator.d.ts +12 -0
- package/browser/generators/builders/CalldataGenerator.d.ts +8 -0
- package/browser/generators/builders/DeploymentGenerator.d.ts +7 -0
- package/browser/index.d.ts +3 -0
- package/browser/index.js +2 -0
- package/browser/index.js.LICENSE.txt +23 -0
- package/browser/keypair/EcKeyPair.d.ts +24 -0
- package/browser/keypair/Wallet.d.ts +16 -0
- package/browser/keypair/interfaces/IWallet.d.ts +5 -0
- package/browser/metadata/ContractBaseMetadata.d.ts +9 -0
- package/browser/metadata/ContractMetadataManager.d.ts +1 -0
- package/browser/metadata/contracts/wBTC.d.ts +12 -0
- package/browser/network/NetworkInformation.d.ts +6 -0
- package/browser/opnet.d.ts +22 -0
- package/browser/scripts/Regtest.d.ts +2 -0
- package/browser/scripts/test.d.ts +1 -0
- package/browser/signer/TweakedSigner.d.ts +9 -0
- package/browser/transaction/TransactionBuilder.d.ts +1 -0
- package/browser/transaction/TransactionFactory.d.ts +4 -0
- package/browser/transaction/builders/FundingTransaction.d.ts +11 -0
- package/browser/transaction/builders/InteractionTransaction.d.ts +47 -0
- package/browser/transaction/builders/TransactionBuilder.d.ts +64 -0
- package/browser/transaction/enums/TransactionType.d.ts +7 -0
- package/browser/transaction/interfaces/ITransactionParameters.d.ts +33 -0
- package/browser/transaction/interfaces/Tap.d.ts +22 -0
- package/browser/utils/BitcoinUtils.d.ts +5 -0
- package/browser/utxo/UTXOManager.d.ts +7 -0
- package/browser/utxo/interfaces/IUTXO.d.ts +19 -0
- package/build/Utils.d.ts +0 -0
- package/build/Utils.js +1 -0
- package/build/_version.d.ts +1 -0
- package/build/_version.js +1 -0
- package/build/bytecode/Compressor.d.ts +6 -0
- package/build/bytecode/Compressor.js +13 -0
- package/build/contracts/ContractMetadataManager.d.ts +0 -0
- package/build/contracts/ContractMetadataManager.js +1 -0
- package/build/crypto/crypto.d.ts +2 -0
- package/build/crypto/crypto.js +1 -0
- package/build/generators/Generator.d.ts +12 -0
- package/build/generators/Generator.js +25 -0
- package/build/generators/builders/CalldataGenerator.d.ts +8 -0
- package/build/generators/builders/CalldataGenerator.js +79 -0
- package/build/generators/builders/DeploymentGenerator.d.ts +7 -0
- package/build/generators/builders/DeploymentGenerator.js +38 -0
- package/build/index.d.ts +3 -0
- package/build/index.js +3 -0
- package/build/keypair/EcKeyPair.d.ts +24 -0
- package/build/keypair/EcKeyPair.js +107 -0
- package/build/keypair/Wallet.d.ts +16 -0
- package/build/keypair/Wallet.js +30 -0
- package/build/keypair/interfaces/GeneratedWallet.d.ts +5 -0
- package/build/keypair/interfaces/GeneratedWallet.js +1 -0
- package/build/keypair/interfaces/IWallet.d.ts +5 -0
- package/build/keypair/interfaces/IWallet.js +1 -0
- package/build/metadata/ContractBaseMetadata.d.ts +9 -0
- package/build/metadata/ContractBaseMetadata.js +13 -0
- package/build/metadata/ContractMetadataManager.d.ts +1 -0
- package/build/metadata/ContractMetadataManager.js +9 -0
- package/build/metadata/contracts/ContractBase.d.ts +9 -0
- package/build/metadata/contracts/ContractBase.js +13 -0
- package/build/metadata/contracts/ContractBaseMetadata.d.ts +9 -0
- package/build/metadata/contracts/ContractBaseMetadata.js +13 -0
- package/build/metadata/contracts/ContractMetadataManager.d.ts +0 -0
- package/build/metadata/contracts/ContractMetadataManager.js +1 -0
- package/build/metadata/contracts/wBTC.d.ts +12 -0
- package/build/metadata/contracts/wBTC.js +26 -0
- package/build/network/NetworkConverter.d.ts +0 -0
- package/build/network/NetworkConverter.js +14 -0
- package/build/network/NetworkInformation.d.ts +6 -0
- package/build/network/NetworkInformation.js +1 -0
- package/build/opnet.d.ts +22 -0
- package/build/opnet.js +22 -0
- package/build/scripts/Regtest.d.ts +2 -0
- package/build/scripts/Regtest.js +15 -0
- package/build/scripts/test.d.ts +1 -0
- package/build/scripts/test.js +74 -0
- package/build/signer/Regtest.d.ts +2 -0
- package/build/signer/Regtest.js +15 -0
- package/build/signer/TweakedSigner.d.ts +9 -0
- package/build/signer/TweakedSigner.js +22 -0
- package/build/transaction/TransactionBuilder.d.ts +60 -0
- package/build/transaction/TransactionBuilder.js +244 -0
- package/build/transaction/TransactionFactory.d.ts +4 -0
- package/build/transaction/TransactionFactory.js +32 -0
- package/build/transaction/builders/FundingTransaction.d.ts +11 -0
- package/build/transaction/builders/FundingTransaction.js +23 -0
- package/build/transaction/builders/GenericTransaction.d.ts +11 -0
- package/build/transaction/builders/GenericTransaction.js +23 -0
- package/build/transaction/builders/InteractionTransaction.d.ts +47 -0
- package/build/transaction/builders/InteractionTransaction.js +219 -0
- package/build/transaction/builders/TransactionBuilder.d.ts +64 -0
- package/build/transaction/builders/TransactionBuilder.js +288 -0
- package/build/transaction/enums/TransactionType.d.ts +7 -0
- package/build/transaction/enums/TransactionType.js +8 -0
- package/build/transaction/interfaces/ITransactionParameters.d.ts +33 -0
- package/build/transaction/interfaces/ITransactionParameters.js +1 -0
- package/build/transaction/interfaces/ITransactions.d.ts +32 -0
- package/build/transaction/interfaces/ITransactions.js +1 -0
- package/build/transaction/interfaces/Tap.d.ts +22 -0
- package/build/transaction/interfaces/Tap.js +1 -0
- package/build/utils/BitcoinUtils.d.ts +5 -0
- package/build/utils/BitcoinUtils.js +9 -0
- package/build/utxo/IUTXO.d.ts +0 -0
- package/build/utxo/IUTXO.js +1 -0
- package/build/utxo/UTXOManager.d.ts +7 -0
- package/build/utxo/UTXOManager.js +47 -0
- package/build/utxo/interfaces/IUTXO.d.ts +19 -0
- package/build/utxo/interfaces/IUTXO.js +1 -0
- package/cjs/_version.d.ts +1 -0
- package/cjs/_version.js +4 -0
- package/cjs/bytecode/Compressor.d.ts +6 -0
- package/cjs/bytecode/Compressor.js +20 -0
- package/cjs/crypto/crypto.d.ts +2 -0
- package/cjs/crypto/crypto.js +8 -0
- package/cjs/generators/Generator.d.ts +13 -0
- package/cjs/generators/Generator.js +31 -0
- package/cjs/generators/builders/CalldataGenerator.d.ts +8 -0
- package/cjs/generators/builders/CalldataGenerator.js +83 -0
- package/cjs/generators/builders/DeploymentGenerator.d.ts +7 -0
- package/cjs/generators/builders/DeploymentGenerator.js +42 -0
- package/cjs/index.d.ts +3 -0
- package/cjs/index.js +32 -0
- package/cjs/keypair/EcKeyPair.d.ts +24 -0
- package/cjs/keypair/EcKeyPair.js +137 -0
- package/cjs/keypair/Wallet.d.ts +16 -0
- package/cjs/keypair/Wallet.js +34 -0
- package/cjs/keypair/interfaces/IWallet.d.ts +5 -0
- package/cjs/keypair/interfaces/IWallet.js +2 -0
- package/cjs/metadata/ContractBaseMetadata.d.ts +9 -0
- package/cjs/metadata/ContractBaseMetadata.js +17 -0
- package/cjs/metadata/ContractMetadataManager.d.ts +0 -0
- package/cjs/metadata/ContractMetadataManager.js +1 -0
- package/cjs/metadata/contracts/wBTC.d.ts +12 -0
- package/cjs/metadata/contracts/wBTC.js +30 -0
- package/cjs/network/NetworkInformation.d.ts +6 -0
- package/cjs/network/NetworkInformation.js +2 -0
- package/cjs/opnet.d.ts +1 -0
- package/cjs/opnet.js +5 -0
- package/cjs/scripts/Regtest.d.ts +2 -0
- package/cjs/scripts/Regtest.js +18 -0
- package/cjs/scripts/test.d.ts +1 -0
- package/cjs/scripts/test.js +44 -0
- package/cjs/signer/TweakedSigner.d.ts +9 -0
- package/cjs/signer/TweakedSigner.js +49 -0
- package/cjs/transaction/TransactionBuilder.d.ts +0 -0
- package/cjs/transaction/TransactionBuilder.js +1 -0
- package/cjs/transaction/TransactionFactory.d.ts +4 -0
- package/cjs/transaction/TransactionFactory.js +33 -0
- package/cjs/transaction/builders/FundingTransaction.d.ts +11 -0
- package/cjs/transaction/builders/FundingTransaction.js +27 -0
- package/cjs/transaction/builders/InteractionTransaction.d.ts +45 -0
- package/cjs/transaction/builders/InteractionTransaction.js +214 -0
- package/cjs/transaction/builders/TransactionBuilder.d.ts +64 -0
- package/cjs/transaction/builders/TransactionBuilder.js +304 -0
- package/cjs/transaction/enums/TransactionType.d.ts +7 -0
- package/cjs/transaction/enums/TransactionType.js +11 -0
- package/cjs/transaction/interfaces/ITransactionParameters.d.ts +32 -0
- package/cjs/transaction/interfaces/ITransactionParameters.js +2 -0
- package/cjs/transaction/interfaces/Tap.d.ts +22 -0
- package/cjs/transaction/interfaces/Tap.js +2 -0
- package/cjs/utils/BitcoinUtils.d.ts +5 -0
- package/cjs/utils/BitcoinUtils.js +13 -0
- package/cjs/utxo/UTXOManager.d.ts +7 -0
- package/cjs/utxo/UTXOManager.js +51 -0
- package/cjs/utxo/interfaces/IUTXO.d.ts +19 -0
- package/cjs/utxo/interfaces/IUTXO.js +2 -0
- package/docs/.nojekyll +1 -0
- package/docs/assets/highlight.css +92 -0
- package/docs/assets/icons.js +15 -0
- package/docs/assets/icons.svg +1 -0
- package/docs/assets/main.js +59 -0
- package/docs/assets/material-style.css +247 -0
- package/docs/assets/navigation.js +1 -0
- package/docs/assets/search.js +1 -0
- package/docs/assets/style.css +1412 -0
- package/docs/classes/BitcoinUtils.html +183 -0
- package/docs/classes/CalldataGenerator.html +211 -0
- package/docs/classes/Compressor.html +185 -0
- package/docs/classes/ContractBaseMetadata.html +182 -0
- package/docs/classes/DeploymentGenerator.html +200 -0
- package/docs/classes/EcKeyPair.html +280 -0
- package/docs/classes/FundingTransaction.html +293 -0
- package/docs/classes/Generator.html +199 -0
- package/docs/classes/InteractionTransaction.html +365 -0
- package/docs/classes/TransactionBuilder.html +303 -0
- package/docs/classes/TransactionFactory.html +180 -0
- package/docs/classes/TweakedSigner.html +181 -0
- package/docs/classes/UTXOManager.html +187 -0
- package/docs/classes/Wallet.html +191 -0
- package/docs/classes/wBTC.html +189 -0
- package/docs/enums/TransactionType.html +179 -0
- package/docs/hierarchy.html +174 -0
- package/docs/index.html +237 -0
- package/docs/interfaces/FetchUTXOParams.html +178 -0
- package/docs/interfaces/IFundingTransactionParameters.html +182 -0
- package/docs/interfaces/IInteractionParameters.html +185 -0
- package/docs/interfaces/ITransactionDataContractDeployment.html +184 -0
- package/docs/interfaces/ITransactionDataContractInteractionWrap.html +186 -0
- package/docs/interfaces/ITransactionParameters.html +181 -0
- package/docs/interfaces/IWallet.html +181 -0
- package/docs/interfaces/NetworkInformation.html +176 -0
- package/docs/interfaces/PsbtInputExtended.html +194 -0
- package/docs/interfaces/PsbtOutputExtendedAddress.html +183 -0
- package/docs/interfaces/PsbtOutputExtendedScript.html +183 -0
- package/docs/interfaces/RawUTXOResponse.html +178 -0
- package/docs/interfaces/TapLeafScript.html +177 -0
- package/docs/interfaces/TweakSettings.html +179 -0
- package/docs/interfaces/UTXO.html +178 -0
- package/docs/interfaces/UpdateInput.html +175 -0
- package/docs/modules.html +208 -0
- package/docs/types/PsbtOutputExtended.html +174 -0
- package/docs/variables/version.html +174 -0
- package/gulpfile.js +152 -0
- package/jest.config.ts +52 -0
- package/package.json +116 -0
- package/src/_version.ts +1 -0
- package/src/bytecode/Compressor.ts +27 -0
- package/src/crypto/crypto-browser.js +71 -0
- package/src/crypto/crypto.ts +1 -0
- package/src/generators/Generator.ts +75 -0
- package/src/generators/builders/CalldataGenerator.ts +143 -0
- package/src/generators/builders/DeploymentGenerator.ts +63 -0
- package/src/index.ts +4 -0
- package/src/keypair/EcKeyPair.ts +265 -0
- package/src/keypair/Wallet.ts +75 -0
- package/src/keypair/interfaces/IWallet.ts +19 -0
- package/src/metadata/ContractBaseMetadata.ts +23 -0
- package/src/metadata/contracts/wBTC.ts +44 -0
- package/src/network/NetworkInformation.ts +7 -0
- package/src/opnet.ts +42 -0
- package/src/scripts/Regtest.ts +19 -0
- package/src/scripts/test.ts +98 -0
- package/src/signer/TweakedSigner.ts +57 -0
- package/src/transaction/TransactionFactory.ts +57 -0
- package/src/transaction/builders/FundingTransaction.ts +36 -0
- package/src/transaction/builders/InteractionTransaction.ts +439 -0
- package/src/transaction/builders/TransactionBuilder.ts +603 -0
- package/src/transaction/enums/TransactionType.ts +7 -0
- package/src/transaction/interfaces/ITransactionParameters.ts +41 -0
- package/src/transaction/interfaces/Tap.ts +26 -0
- package/src/utils/BitcoinUtils.ts +24 -0
- package/src/utxo/UTXOManager.ts +67 -0
- package/src/utxo/interfaces/IUTXO.ts +22 -0
- package/tests/TransactionBuilder.test.ts +58 -0
- package/tests/contracts/wbtc.wasm +0 -0
- package/tsconfig.base.json +43 -0
- package/tsconfig.cjs.json +9 -0
- package/tsconfig.json +8 -0
- package/tsconfig.webpack.json +11 -0
- package/webpack.config.js +77 -0
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
import { initEccLib, opcodes, payments, Psbt, script } from 'bitcoinjs-lib';
|
|
2
|
+
import { varuint } from 'bitcoinjs-lib/src/bufferutils.js';
|
|
3
|
+
import { toXOnly } from 'bitcoinjs-lib/src/psbt/bip371.js';
|
|
4
|
+
import * as ecc from 'tiny-secp256k1';
|
|
5
|
+
import { EcKeyPair } from '../../keypair/EcKeyPair.js';
|
|
6
|
+
export class TransactionBuilder {
|
|
7
|
+
static LOCK_LEAF_SCRIPT = script.compile([opcodes.OP_0]);
|
|
8
|
+
static MINIMUM_DUST = 330n;
|
|
9
|
+
logColor = '#785def';
|
|
10
|
+
transactionFee = 0n;
|
|
11
|
+
transaction;
|
|
12
|
+
inputs = [];
|
|
13
|
+
updateInputs = [];
|
|
14
|
+
outputs = [];
|
|
15
|
+
feeOutput = null;
|
|
16
|
+
signed = false;
|
|
17
|
+
tapData = null;
|
|
18
|
+
scriptData = null;
|
|
19
|
+
totalInputAmount;
|
|
20
|
+
signer;
|
|
21
|
+
network;
|
|
22
|
+
feeRate;
|
|
23
|
+
priorityFee;
|
|
24
|
+
utxos;
|
|
25
|
+
to;
|
|
26
|
+
from;
|
|
27
|
+
_maximumFeeRate = 100000000;
|
|
28
|
+
constructor(parameters) {
|
|
29
|
+
this.signer = parameters.signer;
|
|
30
|
+
this.network = parameters.network;
|
|
31
|
+
this.feeRate = parameters.feeRate;
|
|
32
|
+
this.priorityFee = parameters.priorityFee;
|
|
33
|
+
this.utxos = parameters.utxos;
|
|
34
|
+
this.to = parameters.to;
|
|
35
|
+
this.from =
|
|
36
|
+
parameters.from ||
|
|
37
|
+
EcKeyPair.getTaprootAddress(this.signer, this.network);
|
|
38
|
+
this.totalInputAmount = this.calculateTotalUTXOAmount();
|
|
39
|
+
const totalVOut = this.calculateTotalVOutAmount();
|
|
40
|
+
if (totalVOut < this.totalInputAmount) {
|
|
41
|
+
throw new Error(`Vout value is less than the value to send`);
|
|
42
|
+
}
|
|
43
|
+
if (this.totalInputAmount < TransactionBuilder.MINIMUM_DUST) {
|
|
44
|
+
throw new Error(`Value is less than the minimum dust`);
|
|
45
|
+
}
|
|
46
|
+
this.transaction = new Psbt({
|
|
47
|
+
network: this.network,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
getFundingTransactionParameters() {
|
|
51
|
+
return {
|
|
52
|
+
utxos: this.utxos,
|
|
53
|
+
to: this.getScriptAddress(),
|
|
54
|
+
signer: this.signer,
|
|
55
|
+
network: this.network,
|
|
56
|
+
feeRate: this.feeRate,
|
|
57
|
+
priorityFee: this.priorityFee,
|
|
58
|
+
from: this.from,
|
|
59
|
+
childTransactionRequiredFees: this.transactionFee,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
setDestinationAddress(address) {
|
|
63
|
+
this.to = address;
|
|
64
|
+
}
|
|
65
|
+
setMaximumFeeRate(feeRate) {
|
|
66
|
+
this._maximumFeeRate = feeRate;
|
|
67
|
+
}
|
|
68
|
+
signTransaction() {
|
|
69
|
+
if (!this.to)
|
|
70
|
+
throw new Error('Transaction must have a recipient');
|
|
71
|
+
if (!EcKeyPair.verifyContractAddress(this.to, this.network)) {
|
|
72
|
+
throw new Error('Invalid contract address. The contract address must be a taproot address.');
|
|
73
|
+
}
|
|
74
|
+
if (this.signed)
|
|
75
|
+
throw new Error('Transaction is already signed');
|
|
76
|
+
this.signed = true;
|
|
77
|
+
this.buildTransaction();
|
|
78
|
+
const builtTx = this.internalBuildTransaction(this.transaction);
|
|
79
|
+
if (builtTx) {
|
|
80
|
+
return this.transaction.extractTransaction(false);
|
|
81
|
+
}
|
|
82
|
+
throw new Error('Could not sign transaction');
|
|
83
|
+
}
|
|
84
|
+
getTransaction() {
|
|
85
|
+
return this.transaction.extractTransaction(false);
|
|
86
|
+
}
|
|
87
|
+
getScriptAddress() {
|
|
88
|
+
if (!this.scriptData || !this.scriptData.address) {
|
|
89
|
+
throw new Error('Tap data is required');
|
|
90
|
+
}
|
|
91
|
+
return this.scriptData.address;
|
|
92
|
+
}
|
|
93
|
+
disableRBF() {
|
|
94
|
+
if (this.signed)
|
|
95
|
+
throw new Error('Transaction is already signed');
|
|
96
|
+
for (let input of this.inputs) {
|
|
97
|
+
input.sequence = 0xffffffff;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
getTapAddress() {
|
|
101
|
+
if (!this.tapData || !this.tapData.address) {
|
|
102
|
+
throw new Error('Tap data is required');
|
|
103
|
+
}
|
|
104
|
+
return this.tapData.address;
|
|
105
|
+
}
|
|
106
|
+
addInput(input) {
|
|
107
|
+
this.inputs.push(input);
|
|
108
|
+
}
|
|
109
|
+
addOutput(output) {
|
|
110
|
+
if (output.value < TransactionBuilder.MINIMUM_DUST) {
|
|
111
|
+
throw new Error(`Output value is less than the minimum dust ${output.value} < ${TransactionBuilder.MINIMUM_DUST}`);
|
|
112
|
+
}
|
|
113
|
+
this.outputs.push(output);
|
|
114
|
+
}
|
|
115
|
+
addRefundOutput(amountSpent) {
|
|
116
|
+
const sendBackAmount = this.totalInputAmount - amountSpent;
|
|
117
|
+
if (sendBackAmount >= TransactionBuilder.MINIMUM_DUST) {
|
|
118
|
+
this.setFeeOutput({
|
|
119
|
+
value: Number(sendBackAmount),
|
|
120
|
+
address: this.from,
|
|
121
|
+
});
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
console.warn(`Amount to send back is less than the minimum dust, will be consumed in fees instead.`);
|
|
125
|
+
}
|
|
126
|
+
getTransactionOPNetFee() {
|
|
127
|
+
if (this.priorityFee > TransactionBuilder.MINIMUM_DUST) {
|
|
128
|
+
return this.priorityFee;
|
|
129
|
+
}
|
|
130
|
+
return TransactionBuilder.MINIMUM_DUST;
|
|
131
|
+
}
|
|
132
|
+
calculateTotalUTXOAmount() {
|
|
133
|
+
let total = 0n;
|
|
134
|
+
for (let utxo of this.utxos) {
|
|
135
|
+
total += utxo.value;
|
|
136
|
+
}
|
|
137
|
+
return total;
|
|
138
|
+
}
|
|
139
|
+
calculateTotalVOutAmount() {
|
|
140
|
+
let total = 0n;
|
|
141
|
+
for (let utxo of this.utxos) {
|
|
142
|
+
total += utxo.value;
|
|
143
|
+
}
|
|
144
|
+
return total;
|
|
145
|
+
}
|
|
146
|
+
addInputsFromUTXO() {
|
|
147
|
+
for (let utxo of this.utxos) {
|
|
148
|
+
const input = {
|
|
149
|
+
hash: utxo.transactionId,
|
|
150
|
+
index: utxo.outputIndex,
|
|
151
|
+
witnessUtxo: {
|
|
152
|
+
value: Number(utxo.value),
|
|
153
|
+
script: Buffer.from(utxo.scriptPubKey.hex, 'hex'),
|
|
154
|
+
},
|
|
155
|
+
sequence: 0xfffffffd,
|
|
156
|
+
};
|
|
157
|
+
this.addInput(input);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
witnessStackToScriptWitness(witness) {
|
|
161
|
+
let buffer = Buffer.allocUnsafe(0);
|
|
162
|
+
function writeSlice(slice) {
|
|
163
|
+
buffer = Buffer.concat([buffer, Buffer.from(slice)]);
|
|
164
|
+
}
|
|
165
|
+
function writeVarInt(i) {
|
|
166
|
+
const currentLen = buffer.length;
|
|
167
|
+
const varintLen = varuint.encodingLength(i);
|
|
168
|
+
buffer = Buffer.concat([buffer, Buffer.allocUnsafe(varintLen)]);
|
|
169
|
+
varuint.encode(i, buffer, currentLen);
|
|
170
|
+
}
|
|
171
|
+
function writeVarSlice(slice) {
|
|
172
|
+
writeVarInt(slice.length);
|
|
173
|
+
writeSlice(slice);
|
|
174
|
+
}
|
|
175
|
+
function writeVector(vector) {
|
|
176
|
+
writeVarInt(vector.length);
|
|
177
|
+
vector.forEach(writeVarSlice);
|
|
178
|
+
}
|
|
179
|
+
writeVector(witness);
|
|
180
|
+
return buffer;
|
|
181
|
+
}
|
|
182
|
+
internalInit() {
|
|
183
|
+
this.verifyUTXOValidity();
|
|
184
|
+
this.scriptData = payments.p2tr(this.generateScriptAddress());
|
|
185
|
+
this.tapData = payments.p2tr(this.generateTapData());
|
|
186
|
+
}
|
|
187
|
+
generateScriptAddress() {
|
|
188
|
+
return {
|
|
189
|
+
internalPubkey: this.internalPubKeyToXOnly(),
|
|
190
|
+
network: this.network,
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
generateTapData() {
|
|
194
|
+
return {
|
|
195
|
+
internalPubkey: this.internalPubKeyToXOnly(),
|
|
196
|
+
network: this.network,
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
updateInput(input) {
|
|
200
|
+
this.updateInputs.push(input);
|
|
201
|
+
}
|
|
202
|
+
getWitness() {
|
|
203
|
+
if (!this.tapData || !this.tapData.witness) {
|
|
204
|
+
throw new Error('Witness is required');
|
|
205
|
+
}
|
|
206
|
+
if (this.tapData.witness.length === 0) {
|
|
207
|
+
throw new Error('Witness is empty');
|
|
208
|
+
}
|
|
209
|
+
return this.tapData.witness[this.tapData.witness.length - 1];
|
|
210
|
+
}
|
|
211
|
+
getTapOutput() {
|
|
212
|
+
if (!this.tapData || !this.tapData.output) {
|
|
213
|
+
throw new Error('Tap data is required');
|
|
214
|
+
}
|
|
215
|
+
return this.tapData.output;
|
|
216
|
+
}
|
|
217
|
+
getInputs() {
|
|
218
|
+
return this.inputs;
|
|
219
|
+
}
|
|
220
|
+
getOutputs() {
|
|
221
|
+
const outputs = [...this.outputs];
|
|
222
|
+
if (this.feeOutput)
|
|
223
|
+
outputs.push(this.feeOutput);
|
|
224
|
+
return outputs;
|
|
225
|
+
}
|
|
226
|
+
verifyUTXOValidity() {
|
|
227
|
+
for (let utxo of this.utxos) {
|
|
228
|
+
if (!utxo.scriptPubKey) {
|
|
229
|
+
throw new Error('Address is required');
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
setFeeOutput(output) {
|
|
234
|
+
const initialValue = output.value;
|
|
235
|
+
this.feeOutput = output;
|
|
236
|
+
const fee = this.estimateTransactionFees();
|
|
237
|
+
if (fee > BigInt(initialValue)) {
|
|
238
|
+
throw new Error('Insufficient funds');
|
|
239
|
+
}
|
|
240
|
+
this.feeOutput.value = initialValue - Number(fee);
|
|
241
|
+
if (this.feeOutput.value < TransactionBuilder.MINIMUM_DUST) {
|
|
242
|
+
this.feeOutput = null;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
internalPubKeyToXOnly() {
|
|
246
|
+
return toXOnly(this.signer.publicKey);
|
|
247
|
+
}
|
|
248
|
+
signInputs(transaction) {
|
|
249
|
+
transaction.signAllInputs(this.getSignerKey());
|
|
250
|
+
transaction.finalizeAllInputs();
|
|
251
|
+
}
|
|
252
|
+
internalBuildTransaction(transaction) {
|
|
253
|
+
const inputs = this.getInputs();
|
|
254
|
+
const outputs = this.getOutputs();
|
|
255
|
+
transaction.setMaximumFeeRate(this._maximumFeeRate);
|
|
256
|
+
transaction.addInputs(inputs);
|
|
257
|
+
for (let i = 0; i < this.updateInputs.length; i++) {
|
|
258
|
+
transaction.updateInput(i, this.updateInputs[i]);
|
|
259
|
+
}
|
|
260
|
+
transaction.addOutputs(outputs);
|
|
261
|
+
try {
|
|
262
|
+
this.signInputs(transaction);
|
|
263
|
+
this.transactionFee = BigInt(transaction.getFee());
|
|
264
|
+
return true;
|
|
265
|
+
}
|
|
266
|
+
catch (e) {
|
|
267
|
+
const err = e;
|
|
268
|
+
console.error(`[internalBuildTransaction] Something went wrong while getting building the transaction: ${err.stack}`);
|
|
269
|
+
}
|
|
270
|
+
return false;
|
|
271
|
+
}
|
|
272
|
+
estimateTransactionFees() {
|
|
273
|
+
const fakeTx = new Psbt({
|
|
274
|
+
network: this.network,
|
|
275
|
+
});
|
|
276
|
+
const builtTx = this.internalBuildTransaction(fakeTx);
|
|
277
|
+
if (builtTx) {
|
|
278
|
+
const tx = fakeTx.extractTransaction(false);
|
|
279
|
+
const size = tx.virtualSize();
|
|
280
|
+
const fee = this.feeRate * size + 1;
|
|
281
|
+
return BigInt(Math.ceil(fee));
|
|
282
|
+
}
|
|
283
|
+
else {
|
|
284
|
+
throw new Error(`Could not build transaction to estimate fee. Something went wrong while building the transaction.`);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
initEccLib(ecc);
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export var TransactionType;
|
|
2
|
+
(function (TransactionType) {
|
|
3
|
+
TransactionType[TransactionType["GENERIC"] = 0] = "GENERIC";
|
|
4
|
+
TransactionType[TransactionType["FUNDING"] = 1] = "FUNDING";
|
|
5
|
+
TransactionType[TransactionType["DEPLOYMENT"] = 2] = "DEPLOYMENT";
|
|
6
|
+
TransactionType[TransactionType["INTERACTION"] = 3] = "INTERACTION";
|
|
7
|
+
TransactionType[TransactionType["WBTC_WRAP"] = 4] = "WBTC_WRAP";
|
|
8
|
+
})(TransactionType || (TransactionType = {}));
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { Signer } from 'bitcoinjs-lib';
|
|
3
|
+
import { UTXO } from '../../utxo/interfaces/IUTXO.js';
|
|
4
|
+
import { Network } from 'bitcoinjs-lib/src/networks.js';
|
|
5
|
+
import { Address } from '@btc-vision/bsi-binary';
|
|
6
|
+
export interface ITransactionParameters {
|
|
7
|
+
readonly from?: Address;
|
|
8
|
+
readonly to: Address;
|
|
9
|
+
utxos: UTXO[];
|
|
10
|
+
readonly signer: Signer;
|
|
11
|
+
readonly network: Network;
|
|
12
|
+
readonly feeRate: number;
|
|
13
|
+
readonly priorityFee: bigint;
|
|
14
|
+
}
|
|
15
|
+
export interface IFundingTransactionParameters extends ITransactionParameters {
|
|
16
|
+
readonly childTransactionRequiredFees: bigint;
|
|
17
|
+
}
|
|
18
|
+
export interface IInteractionParameters extends ITransactionParameters {
|
|
19
|
+
readonly calldata: Buffer;
|
|
20
|
+
readonly pubKeys?: Buffer[];
|
|
21
|
+
readonly minimumSignatures?: number;
|
|
22
|
+
readonly randomBytes?: Buffer;
|
|
23
|
+
}
|
|
24
|
+
export interface ITransactionDataContractInteractionWrap extends IInteractionParameters {
|
|
25
|
+
readonly amount: bigint;
|
|
26
|
+
readonly minimumSignatures: number;
|
|
27
|
+
readonly pubKeys: Buffer[];
|
|
28
|
+
}
|
|
29
|
+
export interface ITransactionDataContractDeployment extends ITransactionParameters {
|
|
30
|
+
readonly bytecode: Buffer;
|
|
31
|
+
readonly salt: Buffer;
|
|
32
|
+
readonly customSigner: Signer;
|
|
33
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { Signer } from 'bitcoinjs-lib';
|
|
3
|
+
import { UTXO } from '../../utxo/interfaces/IUTXO.js';
|
|
4
|
+
import { ECPairInterface } from 'ecpair';
|
|
5
|
+
import { Network } from 'bitcoinjs-lib/src/networks.js';
|
|
6
|
+
export interface ITransactionParameters {
|
|
7
|
+
readonly from: string;
|
|
8
|
+
readonly utxos: UTXO[];
|
|
9
|
+
readonly signer: ECPairInterface;
|
|
10
|
+
readonly network: Network;
|
|
11
|
+
readonly feeRate: number;
|
|
12
|
+
readonly priorityFee: bigint;
|
|
13
|
+
}
|
|
14
|
+
export interface SewgitTransactionParameters extends ITransactionParameters {
|
|
15
|
+
}
|
|
16
|
+
export interface ITransactionDataContractInteraction extends ITransactionParameters {
|
|
17
|
+
readonly contractSecret: Buffer;
|
|
18
|
+
readonly calldata: Buffer;
|
|
19
|
+
readonly customSigner: Signer;
|
|
20
|
+
readonly pubKeys?: Buffer[];
|
|
21
|
+
readonly minimumSignatures?: number;
|
|
22
|
+
}
|
|
23
|
+
export interface ITransactionDataContractInteractionWrap extends ITransactionDataContractInteraction {
|
|
24
|
+
readonly amount: bigint;
|
|
25
|
+
readonly minimumSignatures: number;
|
|
26
|
+
readonly pubKeys: Buffer[];
|
|
27
|
+
}
|
|
28
|
+
export interface ITransactionDataContractDeployment extends ITransactionParameters {
|
|
29
|
+
readonly bytecode: Buffer;
|
|
30
|
+
readonly salt: Buffer;
|
|
31
|
+
readonly customSigner: Signer;
|
|
32
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { PsbtInput, PsbtOutput } from 'bip174/src/lib/interfaces.js';
|
|
3
|
+
import { TransactionInput } from 'bitcoinjs-lib/src/psbt.js';
|
|
4
|
+
export interface TapLeafScript {
|
|
5
|
+
readonly leafVersion: number;
|
|
6
|
+
readonly controlBlock: Buffer;
|
|
7
|
+
readonly script: Buffer;
|
|
8
|
+
}
|
|
9
|
+
export interface UpdateInput {
|
|
10
|
+
tapLeafScript: TapLeafScript[];
|
|
11
|
+
}
|
|
12
|
+
export interface PsbtInputExtended extends PsbtInput, TransactionInput {
|
|
13
|
+
}
|
|
14
|
+
export interface PsbtOutputExtendedAddress extends PsbtOutput {
|
|
15
|
+
address: string;
|
|
16
|
+
value: number;
|
|
17
|
+
}
|
|
18
|
+
export interface PsbtOutputExtendedScript extends PsbtOutput {
|
|
19
|
+
script: Buffer;
|
|
20
|
+
value: number;
|
|
21
|
+
}
|
|
22
|
+
export type PsbtOutputExtended = PsbtOutputExtendedAddress | PsbtOutputExtendedScript;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
export class UTXOManager {
|
|
2
|
+
opnetAPIUrl;
|
|
3
|
+
utxoPath = 'address/utxos';
|
|
4
|
+
constructor(opnetAPIUrl) {
|
|
5
|
+
this.opnetAPIUrl = opnetAPIUrl;
|
|
6
|
+
}
|
|
7
|
+
async fetchUTXO(settings) {
|
|
8
|
+
const params = {
|
|
9
|
+
method: 'GET',
|
|
10
|
+
headers: {
|
|
11
|
+
'Content-Type': 'application/json',
|
|
12
|
+
},
|
|
13
|
+
};
|
|
14
|
+
const url = `${this.opnetAPIUrl}/api/v1/${this.utxoPath}?address=${settings.address}`;
|
|
15
|
+
const resp = await fetch(url, params);
|
|
16
|
+
if (!resp.ok) {
|
|
17
|
+
throw new Error(`Failed to fetch UTXO data: ${resp.statusText}`);
|
|
18
|
+
}
|
|
19
|
+
const fetchedData = await resp.json();
|
|
20
|
+
if (fetchedData.length === 0) {
|
|
21
|
+
throw new Error('No UTXO found');
|
|
22
|
+
}
|
|
23
|
+
const meetCriteria = fetchedData.filter((utxo) => {
|
|
24
|
+
return BigInt(utxo.value) >= settings.minAmount;
|
|
25
|
+
});
|
|
26
|
+
if (meetCriteria.length === 0) {
|
|
27
|
+
throw new Error('No UTXO found (minAmount)');
|
|
28
|
+
}
|
|
29
|
+
let finalUTXOs = [];
|
|
30
|
+
let currentAmount = 0n;
|
|
31
|
+
const amountRequested = settings.requestedAmount;
|
|
32
|
+
for (const utxo of meetCriteria) {
|
|
33
|
+
const utxoValue = BigInt(utxo.value);
|
|
34
|
+
currentAmount += utxoValue;
|
|
35
|
+
finalUTXOs.push({
|
|
36
|
+
transactionId: utxo.transactionId,
|
|
37
|
+
outputIndex: utxo.outputIndex,
|
|
38
|
+
value: utxoValue,
|
|
39
|
+
scriptPubKey: utxo.scriptPubKey,
|
|
40
|
+
});
|
|
41
|
+
if (currentAmount + utxoValue > amountRequested) {
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return finalUTXOs;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { ScriptPubKey } from '@btc-vision/bsi-bitcoin-rpc';
|
|
2
|
+
export interface UTXO {
|
|
3
|
+
readonly transactionId: string;
|
|
4
|
+
readonly outputIndex: number;
|
|
5
|
+
readonly value: bigint;
|
|
6
|
+
readonly scriptPubKey: ScriptPubKey;
|
|
7
|
+
}
|
|
8
|
+
export interface FetchUTXOParams {
|
|
9
|
+
readonly address: string;
|
|
10
|
+
readonly minAmount: bigint;
|
|
11
|
+
readonly requestedAmount: bigint;
|
|
12
|
+
readonly optimized?: boolean;
|
|
13
|
+
}
|
|
14
|
+
export interface RawUTXOResponse {
|
|
15
|
+
readonly transactionId: string;
|
|
16
|
+
readonly outputIndex: number;
|
|
17
|
+
readonly value: string;
|
|
18
|
+
readonly scriptPubKey: ScriptPubKey;
|
|
19
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const version = "1.0.0";
|
package/cjs/_version.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.Compressor = void 0;
|
|
7
|
+
const zlib_1 = __importDefault(require("zlib"));
|
|
8
|
+
class Compressor {
|
|
9
|
+
static zlibOptions = {
|
|
10
|
+
level: zlib_1.default.constants.Z_MAX_LEVEL,
|
|
11
|
+
maxOutputLength: 1024 * 1024 * 16,
|
|
12
|
+
};
|
|
13
|
+
static compress(data) {
|
|
14
|
+
return zlib_1.default.gzipSync(data, Compressor.zlibOptions);
|
|
15
|
+
}
|
|
16
|
+
static decompress(data) {
|
|
17
|
+
return zlib_1.default.gunzipSync(data, Compressor.zlibOptions);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
exports.Compressor = Compressor;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.randomBytes = exports.pbkdf2Sync = exports.createHmac = exports.createHash = void 0;
|
|
4
|
+
var crypto_1 = require("crypto");
|
|
5
|
+
Object.defineProperty(exports, "createHash", { enumerable: true, get: function () { return crypto_1.createHash; } });
|
|
6
|
+
Object.defineProperty(exports, "createHmac", { enumerable: true, get: function () { return crypto_1.createHmac; } });
|
|
7
|
+
Object.defineProperty(exports, "pbkdf2Sync", { enumerable: true, get: function () { return crypto_1.pbkdf2Sync; } });
|
|
8
|
+
Object.defineProperty(exports, "randomBytes", { enumerable: true, get: function () { return crypto_1.randomBytes; } });
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { Logger } from '@btc-vision/logger';
|
|
3
|
+
import { Network } from 'bitcoinjs-lib';
|
|
4
|
+
export declare abstract class Generator extends Logger {
|
|
5
|
+
static readonly DATA_CHUNK_SIZE: number;
|
|
6
|
+
static readonly MAGIC: Buffer;
|
|
7
|
+
protected readonly senderPubKey: Buffer;
|
|
8
|
+
protected readonly contractSaltPubKey: Buffer;
|
|
9
|
+
protected readonly network: Network;
|
|
10
|
+
protected constructor(senderPubKey: Buffer, contractSaltPubKey: Buffer, network?: Network);
|
|
11
|
+
abstract compile(...args: unknown[]): Buffer;
|
|
12
|
+
protected splitBufferIntoChunks(buffer: Buffer, chunkSize?: number): Array<Buffer[]>;
|
|
13
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Generator = void 0;
|
|
4
|
+
const logger_1 = require("@btc-vision/logger");
|
|
5
|
+
const bitcoinjs_lib_1 = require("bitcoinjs-lib");
|
|
6
|
+
class Generator extends logger_1.Logger {
|
|
7
|
+
static DATA_CHUNK_SIZE = 512;
|
|
8
|
+
static MAGIC = Buffer.from('bsi', 'utf-8');
|
|
9
|
+
senderPubKey;
|
|
10
|
+
contractSaltPubKey;
|
|
11
|
+
network = bitcoinjs_lib_1.networks.bitcoin;
|
|
12
|
+
constructor(senderPubKey, contractSaltPubKey, network = bitcoinjs_lib_1.networks.bitcoin) {
|
|
13
|
+
super();
|
|
14
|
+
this.senderPubKey = senderPubKey;
|
|
15
|
+
this.contractSaltPubKey = contractSaltPubKey;
|
|
16
|
+
this.network = network;
|
|
17
|
+
}
|
|
18
|
+
splitBufferIntoChunks(buffer, chunkSize = Generator.DATA_CHUNK_SIZE) {
|
|
19
|
+
const chunks = [];
|
|
20
|
+
for (let i = 0; i < buffer.length; i += chunkSize) {
|
|
21
|
+
const dataLength = Math.min(chunkSize, buffer.length - i);
|
|
22
|
+
const buf2 = Buffer.alloc(dataLength);
|
|
23
|
+
for (let j = 0; j < dataLength; j++) {
|
|
24
|
+
buf2.writeUInt8(buffer[i + j], j);
|
|
25
|
+
}
|
|
26
|
+
chunks.push([buf2]);
|
|
27
|
+
}
|
|
28
|
+
return chunks;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
exports.Generator = Generator;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { Network } from 'bitcoinjs-lib';
|
|
3
|
+
import { Generator } from '../Generator.js';
|
|
4
|
+
export declare class CalldataGenerator extends Generator {
|
|
5
|
+
constructor(senderPubKey: Buffer, contractSaltPubKey: Buffer, network?: Network);
|
|
6
|
+
compile(calldata: Buffer, contractSecret: Buffer, vaultPublicKeys?: Buffer[], minimumSignatures?: number): Buffer;
|
|
7
|
+
private getPubKeyAsBuffer;
|
|
8
|
+
}
|