@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.
- package/browser/_version.d.ts +1 -1
- package/browser/index.js +1 -1
- package/browser/opnet.d.ts +3 -0
- package/browser/transaction/TransactionFactory.d.ts +3 -3
- package/browser/transaction/browser/BrowserSigner.d.ts +9 -0
- package/browser/transaction/browser/BrowserSignerBase.d.ts +18 -0
- package/browser/transaction/browser/extensions/UnisatSigner.d.ts +35 -0
- package/browser/transaction/browser/types/Unisat.d.ts +54 -0
- package/browser/transaction/builders/DeploymentTransaction.d.ts +2 -2
- package/browser/transaction/builders/FundingTransaction.d.ts +1 -1
- package/browser/transaction/builders/MultiSignTransaction.d.ts +4 -4
- package/browser/transaction/builders/SharedInteractionTransaction.d.ts +2 -2
- package/browser/transaction/builders/TransactionBuilder.d.ts +10 -9
- package/browser/transaction/builders/UnwrapSegwitTransaction.d.ts +3 -3
- package/browser/transaction/builders/UnwrapTransaction.d.ts +2 -2
- package/browser/transaction/builders/WrapTransaction.d.ts +1 -1
- package/browser/transaction/interfaces/ITransactionParameters.d.ts +1 -0
- package/browser/transaction/processor/PsbtTransaction.d.ts +1 -1
- package/browser/transaction/shared/TweakedTransaction.d.ts +5 -4
- package/build/_version.d.ts +1 -1
- package/build/_version.js +1 -1
- package/build/metadata/contracts/wBTC.js +1 -1
- package/build/opnet.d.ts +3 -0
- package/build/opnet.js +3 -0
- package/build/tests/adaptPSBT.js +1 -1
- package/build/tests/deploy.js +1 -2
- package/build/tests/deployMotoRegStep1.d.ts +1 -0
- package/build/tests/deployMotoRegStep1.js +85 -0
- package/build/tests/deployReg.js +2 -2
- package/build/tests/factory/createPairReg.js +6 -77
- package/build/tests/gen.js +6 -17
- package/build/tests/interaction.d.ts +5 -0
- package/build/tests/interaction.js +62 -0
- package/build/tests/mineReg.js +1 -4
- package/build/tests/moto/airdropToken.d.ts +1 -0
- package/build/tests/moto/airdropToken.js +21 -0
- package/build/tests/moto/allowance.d.ts +1 -0
- package/build/tests/moto/allowance.js +5 -0
- package/build/tests/moto/approve.d.ts +1 -0
- package/build/tests/moto/approve.js +6 -0
- package/build/tests/moto/approveWBTC.d.ts +1 -0
- package/build/tests/moto/approveWBTC.js +12 -0
- package/build/tests/moto/balanceOf.d.ts +1 -0
- package/build/tests/moto/balanceOf.js +13 -0
- package/build/tests/motoswap/airdropToken.d.ts +11 -0
- package/build/tests/motoswap/airdropToken.js +36 -0
- package/build/tests/motoswap/deployMoto.d.ts +4 -0
- package/build/tests/motoswap/deployMoto.js +87 -0
- package/build/tests/motoswap/deployMotoRegStep1.d.ts +1 -0
- package/build/tests/motoswap/deployMotoRegStep1.js +91 -0
- package/build/tests/motoswap/deployMotoRegStep2.d.ts +1 -0
- package/build/tests/motoswap/deployMotoRegStep2.js +91 -0
- package/build/tests/motoswap/deployStep1.d.ts +1 -0
- package/build/tests/motoswap/deployStep1.js +5 -0
- package/build/tests/motoswap/deployStep2.d.ts +1 -0
- package/build/tests/motoswap/deployStep2.js +5 -0
- package/build/tests/motoswap/deployStep3.d.ts +1 -0
- package/build/tests/motoswap/deployStep3.js +5 -0
- package/build/tests/motoswap/interaction.d.ts +3 -0
- package/build/tests/motoswap/interaction.js +63 -0
- package/build/tests/motoswap/routerAddLiquidity.d.ts +11 -0
- package/build/tests/motoswap/routerAddLiquidity.js +36 -0
- package/build/tests/multisign.js +1 -1
- package/build/tests/shared/Utils.d.ts +2 -0
- package/build/tests/shared/Utils.js +9 -0
- package/build/tests/shared/interaction.d.ts +5 -0
- package/build/tests/shared/interaction.js +76 -0
- package/build/tests/shared/tokens.d.ts +6 -0
- package/build/tests/shared/tokens.js +5 -0
- package/build/tests/stakeReg.js +1 -1
- package/build/tests/test.js +1 -1
- package/build/tests/testReg.js +1 -1
- package/build/tests/tokens.d.ts +6 -0
- package/build/tests/tokens.js +5 -0
- package/build/tests/transfer.js +1 -1
- package/build/tests/wbtc/approve.d.ts +1 -0
- package/build/tests/wbtc/approve.js +6 -0
- package/build/tests/wbtc/approveWBTC.d.ts +1 -0
- package/build/tests/wbtc/approveWBTC.js +12 -0
- package/build/tests/wbtc/massWrapReg.d.ts +1 -0
- package/build/tests/wbtc/massWrapReg.js +105 -0
- package/build/tests/wbtc/transferReg.d.ts +1 -0
- package/build/tests/wbtc/transferReg.js +16 -0
- package/build/tests/wbtc/unStakeReg.d.ts +1 -0
- package/build/tests/wbtc/unStakeReg.js +72 -0
- package/build/tests/wbtc/unwrapReg.d.ts +1 -0
- package/build/tests/wbtc/unwrapReg.js +60 -0
- package/build/tests/wbtc/unwrapRegSegwit.d.ts +1 -0
- package/build/tests/wbtc/unwrapRegSegwit.js +83 -0
- package/build/tests/wbtc/withdrawalRequestReg.d.ts +1 -0
- package/build/tests/wbtc/withdrawalRequestReg.js +71 -0
- package/build/tests/wbtc/wrapReg.d.ts +1 -0
- package/build/tests/wbtc/wrapReg.js +69 -0
- package/build/tests/wbtc/wrapTest.d.ts +1 -0
- package/build/tests/wbtc/wrapTest.js +66 -0
- package/build/tests/writers/allowance.d.ts +3 -0
- package/build/tests/writers/allowance.js +10 -0
- package/build/tests/writers/approve.d.ts +4 -0
- package/build/tests/writers/approve.js +11 -0
- package/build/transaction/TransactionFactory.d.ts +3 -3
- package/build/transaction/TransactionFactory.js +41 -38
- package/build/transaction/browser/BrowserSigner.d.ts +11 -0
- package/build/transaction/browser/BrowserSigner.js +10 -0
- package/build/transaction/browser/BrowserSignerBase.d.ts +18 -0
- package/build/transaction/browser/BrowserSignerBase.js +3 -0
- package/build/transaction/browser/extensions/Unisat.d.ts +54 -0
- package/build/transaction/browser/extensions/Unisat.js +11 -0
- package/build/transaction/browser/extensions/UnisatSigner.d.ts +35 -0
- package/build/transaction/browser/extensions/UnisatSigner.js +150 -0
- package/build/transaction/browser/types/Unisat.d.ts +54 -0
- package/build/transaction/browser/types/Unisat.js +11 -0
- package/build/transaction/builders/DeploymentTransaction.d.ts +2 -2
- package/build/transaction/builders/DeploymentTransaction.js +4 -4
- package/build/transaction/builders/FundingTransaction.d.ts +1 -1
- package/build/transaction/builders/FundingTransaction.js +2 -2
- package/build/transaction/builders/MultiSignTransaction.d.ts +4 -4
- package/build/transaction/builders/MultiSignTransaction.js +7 -7
- package/build/transaction/builders/SharedInteractionTransaction.d.ts +2 -2
- package/build/transaction/builders/SharedInteractionTransaction.js +6 -6
- package/build/transaction/builders/TransactionBuilder.d.ts +10 -9
- package/build/transaction/builders/TransactionBuilder.js +29 -22
- package/build/transaction/builders/UnwrapSegwitTransaction.d.ts +3 -3
- package/build/transaction/builders/UnwrapSegwitTransaction.js +10 -10
- package/build/transaction/builders/UnwrapTransaction.d.ts +2 -2
- package/build/transaction/builders/UnwrapTransaction.js +9 -6
- package/build/transaction/builders/WrapTransaction.d.ts +1 -1
- package/build/transaction/builders/WrapTransaction.js +2 -2
- package/build/transaction/interfaces/ITransactionParameters.d.ts +1 -0
- package/build/transaction/processor/PsbtTransaction.d.ts +1 -1
- package/build/transaction/processor/PsbtTransaction.js +2 -2
- package/build/transaction/shared/TweakedTransaction.d.ts +5 -4
- package/build/transaction/shared/TweakedTransaction.js +59 -23
- package/package.json +2 -2
- package/src/_version.ts +1 -1
- package/src/metadata/contracts/wBTC.ts +1 -1
- package/src/opnet.ts +81 -76
- package/src/transaction/TransactionFactory.ts +52 -44
- package/src/transaction/browser/BrowserSignerBase.ts +44 -0
- package/src/transaction/browser/extensions/UnisatSigner.ts +218 -0
- package/src/transaction/browser/types/Unisat.ts +77 -0
- package/src/transaction/builders/DeploymentTransaction.ts +4 -4
- package/src/transaction/builders/FundingTransaction.ts +40 -40
- package/src/transaction/builders/MultiSignTransaction.ts +9 -17
- package/src/transaction/builders/SharedInteractionTransaction.ts +368 -368
- package/src/transaction/builders/TransactionBuilder.ts +637 -632
- package/src/transaction/builders/UnwrapSegwitTransaction.ts +365 -362
- package/src/transaction/builders/UnwrapTransaction.ts +503 -499
- package/src/transaction/builders/WrapTransaction.ts +346 -346
- package/src/transaction/interfaces/ITransactionParameters.ts +55 -54
- package/src/transaction/processor/PsbtTransaction.ts +192 -192
- package/src/transaction/shared/TweakedTransaction.ts +109 -56
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { Wallet } from '../../keypair/Wallet.js';
|
|
2
|
+
import { Regtest } from '../Regtest.js';
|
|
3
|
+
import { OPNetLimitedProvider } from '../../utxo/OPNetLimitedProvider.js';
|
|
4
|
+
import { networks } from 'bitcoinjs-lib';
|
|
5
|
+
import { TransactionFactory } from '../../transaction/TransactionFactory.js';
|
|
6
|
+
import { BitcoinRPC } from '@btc-vision/bsi-bitcoin-rpc';
|
|
7
|
+
import { currentConsensusConfig } from '../../consensus/ConsensusConfig.js';
|
|
8
|
+
const network = networks.regtest;
|
|
9
|
+
const rpc = new BitcoinRPC();
|
|
10
|
+
const wallet = new Wallet(Regtest.wallet, network);
|
|
11
|
+
const opnet = new OPNetLimitedProvider('http://localhost:9001');
|
|
12
|
+
const factory = new TransactionFactory();
|
|
13
|
+
const shouldMineBlock = true;
|
|
14
|
+
async function mineBlock() {
|
|
15
|
+
const ok = await rpc.generateToAddress(1, wallet.p2wpkh, 'default');
|
|
16
|
+
if (!ok) {
|
|
17
|
+
throw new Error('Could not mine block');
|
|
18
|
+
}
|
|
19
|
+
console.log(`Mined block`, ok);
|
|
20
|
+
return !!ok.length;
|
|
21
|
+
}
|
|
22
|
+
await rpc.init(Regtest.config);
|
|
23
|
+
const wrapAmount = (currentConsensusConfig.VAULT_MINIMUM_AMOUNT * 30n) / 10n;
|
|
24
|
+
const utxoSetting = {
|
|
25
|
+
addresses: [wallet.p2wpkh, wallet.p2tr],
|
|
26
|
+
minAmount: 10000n,
|
|
27
|
+
requestedAmount: wrapAmount + currentConsensusConfig.UNWRAP_CONSOLIDATION_PREPAID_FEES_SAT,
|
|
28
|
+
};
|
|
29
|
+
const utxos = await opnet.fetchUTXOMultiAddr(utxoSetting);
|
|
30
|
+
if (!utxos) {
|
|
31
|
+
throw new Error('No UTXOs found');
|
|
32
|
+
}
|
|
33
|
+
const generationParameters = await opnet.fetchWrapParameters(wrapAmount);
|
|
34
|
+
if (!generationParameters) {
|
|
35
|
+
throw new Error('No generation parameters found');
|
|
36
|
+
}
|
|
37
|
+
console.log(`UTXOs:`, utxos);
|
|
38
|
+
const wrapParameters = {
|
|
39
|
+
from: wallet.p2wpkh,
|
|
40
|
+
utxos: utxos,
|
|
41
|
+
signer: wallet.keypair,
|
|
42
|
+
network: network,
|
|
43
|
+
feeRate: 1000,
|
|
44
|
+
priorityFee: 100000n,
|
|
45
|
+
receiver: wallet.p2tr,
|
|
46
|
+
amount: wrapAmount,
|
|
47
|
+
generationParameters: generationParameters,
|
|
48
|
+
};
|
|
49
|
+
const finalTx = await factory.wrap(wrapParameters);
|
|
50
|
+
console.log(`Final transaction:`, finalTx);
|
|
51
|
+
const firstTxBroadcast = await rpc.sendRawTransaction({
|
|
52
|
+
hexstring: finalTx.transaction[0],
|
|
53
|
+
});
|
|
54
|
+
console.log(`First transaction broadcasted: ${firstTxBroadcast}`);
|
|
55
|
+
if (!firstTxBroadcast) {
|
|
56
|
+
throw new Error('Could not broadcast first transaction');
|
|
57
|
+
}
|
|
58
|
+
const secondTxBroadcast = await rpc.sendRawTransaction({
|
|
59
|
+
hexstring: finalTx.transaction[1],
|
|
60
|
+
});
|
|
61
|
+
console.log(`Second transaction broadcasted: ${secondTxBroadcast}`);
|
|
62
|
+
if (!secondTxBroadcast) {
|
|
63
|
+
throw new Error('Could not broadcast second transaction');
|
|
64
|
+
}
|
|
65
|
+
if (shouldMineBlock) {
|
|
66
|
+
await mineBlock();
|
|
67
|
+
}
|
|
68
|
+
rpc.destroy();
|
|
69
|
+
console.log(`Wrapped ${wrapAmount} sats to ${wallet.p2tr}.`);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { Wallet } from '../../keypair/Wallet.js';
|
|
2
|
+
import { Testnet } from '../Regtest.js';
|
|
3
|
+
import { OPNetLimitedProvider } from '../../utxo/OPNetLimitedProvider.js';
|
|
4
|
+
import { networks } from 'bitcoinjs-lib';
|
|
5
|
+
import { TransactionFactory } from '../../transaction/TransactionFactory.js';
|
|
6
|
+
import { BitcoinRPC } from '@btc-vision/bsi-bitcoin-rpc';
|
|
7
|
+
const network = networks.testnet;
|
|
8
|
+
const rpc = new BitcoinRPC();
|
|
9
|
+
const wallet = new Wallet(Testnet.wallet, network);
|
|
10
|
+
const opnet = new OPNetLimitedProvider('https://testnet.opnet.org');
|
|
11
|
+
const factory = new TransactionFactory();
|
|
12
|
+
const shouldMineBlock = true;
|
|
13
|
+
async function mineBlock() {
|
|
14
|
+
const ok = await rpc.generateToAddress(1, wallet.p2wpkh, 'default');
|
|
15
|
+
if (!ok) {
|
|
16
|
+
throw new Error('Could not mine block');
|
|
17
|
+
}
|
|
18
|
+
console.log(`Mined block`, ok);
|
|
19
|
+
return !!ok.length;
|
|
20
|
+
}
|
|
21
|
+
await rpc.init(Testnet.config);
|
|
22
|
+
const wrapAmount = 1n * 100000000n;
|
|
23
|
+
const utxoSetting = {
|
|
24
|
+
addresses: [wallet.p2wpkh, wallet.p2tr],
|
|
25
|
+
minAmount: 10000n,
|
|
26
|
+
requestedAmount: wrapAmount,
|
|
27
|
+
};
|
|
28
|
+
const utxos = await opnet.fetchUTXOMultiAddr(utxoSetting);
|
|
29
|
+
if (!utxos) {
|
|
30
|
+
throw new Error('No UTXOs found');
|
|
31
|
+
}
|
|
32
|
+
const generationParameters = await opnet.fetchWrapParameters(wrapAmount);
|
|
33
|
+
if (!generationParameters) {
|
|
34
|
+
throw new Error('No generation parameters found');
|
|
35
|
+
}
|
|
36
|
+
const wrapParameters = {
|
|
37
|
+
from: wallet.p2wpkh,
|
|
38
|
+
utxos: utxos,
|
|
39
|
+
signer: wallet.keypair,
|
|
40
|
+
network: network,
|
|
41
|
+
feeRate: 500,
|
|
42
|
+
priorityFee: 50000n,
|
|
43
|
+
receiver: wallet.p2tr,
|
|
44
|
+
amount: wrapAmount,
|
|
45
|
+
generationParameters: generationParameters,
|
|
46
|
+
};
|
|
47
|
+
const finalTx = await factory.wrap(wrapParameters);
|
|
48
|
+
console.log(`Final transaction:`, finalTx);
|
|
49
|
+
const firstTxBroadcast = await rpc.sendRawTransaction({
|
|
50
|
+
hexstring: finalTx.transaction[0],
|
|
51
|
+
});
|
|
52
|
+
console.log(`First transaction broadcasted: ${firstTxBroadcast}`);
|
|
53
|
+
if (!firstTxBroadcast) {
|
|
54
|
+
throw new Error('Could not broadcast first transaction');
|
|
55
|
+
}
|
|
56
|
+
const secondTxBroadcast = await rpc.sendRawTransaction({
|
|
57
|
+
hexstring: finalTx.transaction[1],
|
|
58
|
+
});
|
|
59
|
+
console.log(`Second transaction broadcasted: ${secondTxBroadcast}`);
|
|
60
|
+
if (!secondTxBroadcast) {
|
|
61
|
+
throw new Error('Could not broadcast second transaction');
|
|
62
|
+
}
|
|
63
|
+
if (shouldMineBlock) {
|
|
64
|
+
await mineBlock();
|
|
65
|
+
}
|
|
66
|
+
rpc.destroy();
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ABICoder, BinaryWriter } from '@btc-vision/bsi-binary';
|
|
2
|
+
const abiCoder = new ABICoder();
|
|
3
|
+
const allowanceSelector = Number(`0x` + abiCoder.encodeSelector('allowance'));
|
|
4
|
+
export function allowance(owner, spender) {
|
|
5
|
+
const calldata = new BinaryWriter();
|
|
6
|
+
calldata.writeSelector(allowanceSelector);
|
|
7
|
+
calldata.writeAddress(owner);
|
|
8
|
+
calldata.writeAddress(spender);
|
|
9
|
+
return Buffer.from(calldata.getBuffer());
|
|
10
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { ABICoder, BinaryWriter } from '@btc-vision/bsi-binary';
|
|
2
|
+
const abiCoder = new ABICoder();
|
|
3
|
+
const approveSelector = Number(`0x` + abiCoder.encodeSelector('approve'));
|
|
4
|
+
export const MAX_UINT256 = 2n ** 256n - 2n;
|
|
5
|
+
export function approve(spender, amount = MAX_UINT256) {
|
|
6
|
+
const calldata = new BinaryWriter();
|
|
7
|
+
calldata.writeSelector(approveSelector);
|
|
8
|
+
calldata.writeAddress(spender);
|
|
9
|
+
calldata.writeU256(amount);
|
|
10
|
+
return Buffer.from(calldata.getBuffer());
|
|
11
|
+
}
|
|
@@ -18,9 +18,9 @@ export interface UnwrapResult {
|
|
|
18
18
|
}
|
|
19
19
|
export declare class TransactionFactory {
|
|
20
20
|
constructor();
|
|
21
|
-
signInteraction(interactionParameters: IInteractionParameters): [string, string]
|
|
22
|
-
signDeployment(deploymentParameters: IDeploymentParameters): DeploymentResult
|
|
23
|
-
wrap(warpParameters: IWrapParameters): WrapResult
|
|
21
|
+
signInteraction(interactionParameters: IInteractionParameters): Promise<[string, string]>;
|
|
22
|
+
signDeployment(deploymentParameters: IDeploymentParameters): Promise<DeploymentResult>;
|
|
23
|
+
wrap(warpParameters: IWrapParameters): Promise<WrapResult>;
|
|
24
24
|
unwrapSegwit(unwrapParameters: IUnwrapParameters): Promise<UnwrapResult>;
|
|
25
25
|
unwrap(unwrapParameters: IUnwrapParameters): Promise<UnwrapResult>;
|
|
26
26
|
private calculateNumSignatures;
|
|
@@ -9,25 +9,26 @@ import { UnwrapTransaction } from './builders/UnwrapTransaction.js';
|
|
|
9
9
|
import { currentConsensus, currentConsensusConfig } from '../consensus/ConsensusConfig.js';
|
|
10
10
|
export class TransactionFactory {
|
|
11
11
|
constructor() { }
|
|
12
|
-
signInteraction(interactionParameters) {
|
|
12
|
+
async signInteraction(interactionParameters) {
|
|
13
13
|
if (!interactionParameters.to) {
|
|
14
14
|
throw new Error('Field "to" not provided.');
|
|
15
15
|
}
|
|
16
16
|
const transaction = new InteractionTransaction(interactionParameters);
|
|
17
|
-
transaction.signTransaction();
|
|
18
|
-
const estimatedGas = transaction.estimateTransactionFees();
|
|
17
|
+
await transaction.signTransaction();
|
|
18
|
+
const estimatedGas = await transaction.estimateTransactionFees();
|
|
19
19
|
const fundingParameters = {
|
|
20
20
|
...interactionParameters,
|
|
21
21
|
childTransactionRequiredValue: estimatedGas,
|
|
22
22
|
};
|
|
23
|
-
const preFundingTransaction = this.createFundTransaction(fundingParameters);
|
|
23
|
+
const preFundingTransaction = await this.createFundTransaction(fundingParameters);
|
|
24
24
|
interactionParameters.utxos = this.getUTXOAsTransaction(preFundingTransaction.tx, interactionParameters.to, 0);
|
|
25
25
|
const preTransaction = new InteractionTransaction(interactionParameters);
|
|
26
|
-
preTransaction.signTransaction();
|
|
27
|
-
const parameters = preTransaction.getFundingTransactionParameters();
|
|
26
|
+
await preTransaction.signTransaction();
|
|
27
|
+
const parameters = await preTransaction.getFundingTransactionParameters();
|
|
28
28
|
parameters.utxos = fundingParameters.utxos;
|
|
29
|
-
parameters.childTransactionRequiredValue = preTransaction.estimateTransactionFees();
|
|
30
|
-
|
|
29
|
+
parameters.childTransactionRequiredValue = await preTransaction.estimateTransactionFees();
|
|
30
|
+
parameters.estimatedFees = preFundingTransaction.estimatedFees;
|
|
31
|
+
const signedTransaction = await this.createFundTransaction(parameters);
|
|
31
32
|
if (!signedTransaction) {
|
|
32
33
|
throw new Error('Could not sign funding transaction.');
|
|
33
34
|
}
|
|
@@ -36,17 +37,18 @@ export class TransactionFactory {
|
|
|
36
37
|
utxos: this.getUTXOAsTransaction(signedTransaction.tx, interactionParameters.to, 0),
|
|
37
38
|
randomBytes: preTransaction.getRndBytes(),
|
|
38
39
|
nonWitnessUtxo: signedTransaction.tx.toBuffer(),
|
|
40
|
+
estimatedFees: preTransaction.estimatedFees,
|
|
39
41
|
};
|
|
40
42
|
const finalTransaction = new InteractionTransaction(newParams);
|
|
41
|
-
const outTx = finalTransaction.signTransaction();
|
|
43
|
+
const outTx = await finalTransaction.signTransaction();
|
|
42
44
|
return [signedTransaction.tx.toHex(), outTx.toHex()];
|
|
43
45
|
}
|
|
44
|
-
signDeployment(deploymentParameters) {
|
|
46
|
+
async signDeployment(deploymentParameters) {
|
|
45
47
|
const preTransaction = new DeploymentTransaction(deploymentParameters);
|
|
46
|
-
preTransaction.signTransaction();
|
|
47
|
-
const parameters = preTransaction.getFundingTransactionParameters();
|
|
48
|
+
await preTransaction.signTransaction();
|
|
49
|
+
const parameters = await preTransaction.getFundingTransactionParameters();
|
|
48
50
|
const fundingTransaction = new FundingTransaction(parameters);
|
|
49
|
-
const signedTransaction = fundingTransaction.signTransaction();
|
|
51
|
+
const signedTransaction = await fundingTransaction.signTransaction();
|
|
50
52
|
if (!signedTransaction) {
|
|
51
53
|
throw new Error('Could not sign funding transaction.');
|
|
52
54
|
}
|
|
@@ -67,14 +69,14 @@ export class TransactionFactory {
|
|
|
67
69
|
nonWitnessUtxo: signedTransaction.toBuffer(),
|
|
68
70
|
};
|
|
69
71
|
const finalTransaction = new DeploymentTransaction(newParams);
|
|
70
|
-
const outTx = finalTransaction.signTransaction();
|
|
72
|
+
const outTx = await finalTransaction.signTransaction();
|
|
71
73
|
return {
|
|
72
74
|
transaction: [signedTransaction.toHex(), outTx.toHex()],
|
|
73
75
|
contractAddress: finalTransaction.contractAddress,
|
|
74
76
|
p2trAddress: finalTransaction.p2trAddress,
|
|
75
77
|
};
|
|
76
78
|
}
|
|
77
|
-
wrap(warpParameters) {
|
|
79
|
+
async wrap(warpParameters) {
|
|
78
80
|
if (warpParameters.amount < currentConsensusConfig.VAULT_MINIMUM_AMOUNT) {
|
|
79
81
|
throw new Error(`Amount is too low. Minimum consolidation is ${currentConsensusConfig.VAULT_MINIMUM_AMOUNT} sat. Received ${warpParameters.amount} sat. Make sure that you cover the unwrap consolidation fees of ${currentConsensusConfig.UNWRAP_CONSOLIDATION_PREPAID_FEES_SAT}sat.`);
|
|
80
82
|
}
|
|
@@ -86,14 +88,14 @@ export class TransactionFactory {
|
|
|
86
88
|
childTransactionRequiredValue: childTransactionRequiredValue,
|
|
87
89
|
to: to,
|
|
88
90
|
};
|
|
89
|
-
const preFundingTransaction = this.createFundTransaction(fundingParameters);
|
|
91
|
+
const preFundingTransaction = await this.createFundTransaction(fundingParameters);
|
|
90
92
|
warpParameters.utxos = this.getUTXOAsTransaction(preFundingTransaction.tx, to, 0);
|
|
91
93
|
const preTransaction = new WrapTransaction(warpParameters);
|
|
92
|
-
preTransaction.signTransaction();
|
|
93
|
-
const parameters = preTransaction.getFundingTransactionParameters();
|
|
94
|
+
await preTransaction.signTransaction();
|
|
95
|
+
const parameters = await preTransaction.getFundingTransactionParameters();
|
|
94
96
|
parameters.childTransactionRequiredValue += childTransactionRequiredValue;
|
|
95
97
|
parameters.utxos = fundingParameters.utxos;
|
|
96
|
-
const signedTransaction = this.createFundTransaction(parameters);
|
|
98
|
+
const signedTransaction = await this.createFundTransaction(parameters);
|
|
97
99
|
if (!signedTransaction) {
|
|
98
100
|
throw new Error('Could not sign funding transaction.');
|
|
99
101
|
}
|
|
@@ -104,7 +106,7 @@ export class TransactionFactory {
|
|
|
104
106
|
nonWitnessUtxo: signedTransaction.tx.toBuffer(),
|
|
105
107
|
};
|
|
106
108
|
const finalTransaction = new WrapTransaction(newParams);
|
|
107
|
-
const outTx = finalTransaction.signTransaction();
|
|
109
|
+
const outTx = await finalTransaction.signTransaction();
|
|
108
110
|
return {
|
|
109
111
|
transaction: [signedTransaction.tx.toHex(), outTx.toHex()],
|
|
110
112
|
vaultAddress: finalTransaction.vault,
|
|
@@ -115,29 +117,29 @@ export class TransactionFactory {
|
|
|
115
117
|
async unwrapSegwit(unwrapParameters) {
|
|
116
118
|
console.error('The "unwrap" method is deprecated. Use unwrapTap instead.');
|
|
117
119
|
const transaction = new UnwrapSegwitTransaction(unwrapParameters);
|
|
118
|
-
transaction.signTransaction();
|
|
120
|
+
await transaction.signTransaction();
|
|
119
121
|
const to = transaction.toAddress();
|
|
120
122
|
if (!to)
|
|
121
123
|
throw new Error('To address is required');
|
|
122
|
-
const estimatedGas = transaction.estimateTransactionFees();
|
|
124
|
+
const estimatedGas = await transaction.estimateTransactionFees();
|
|
123
125
|
const estimatedFees = transaction.preEstimateTransactionFees(BigInt(unwrapParameters.feeRate), this.calculateNumInputs(unwrapParameters.unwrapUTXOs), 2n, this.calculateNumSignatures(unwrapParameters.unwrapUTXOs), this.maxPubKeySize(unwrapParameters.unwrapUTXOs));
|
|
124
126
|
const fundingParameters = {
|
|
125
127
|
...unwrapParameters,
|
|
126
128
|
childTransactionRequiredValue: estimatedGas + estimatedFees,
|
|
127
129
|
to: to,
|
|
128
130
|
};
|
|
129
|
-
const preFundingTransaction = this.createFundTransaction(fundingParameters);
|
|
131
|
+
const preFundingTransaction = await this.createFundTransaction(fundingParameters);
|
|
130
132
|
unwrapParameters.utxos = this.getUTXOAsTransaction(preFundingTransaction.tx, to, 0);
|
|
131
133
|
const preTransaction = new UnwrapSegwitTransaction({
|
|
132
134
|
...unwrapParameters,
|
|
133
135
|
randomBytes: transaction.getRndBytes(),
|
|
134
136
|
});
|
|
135
|
-
preTransaction.signTransaction();
|
|
136
|
-
const parameters = preTransaction.getFundingTransactionParameters();
|
|
137
|
+
await preTransaction.signTransaction();
|
|
138
|
+
const parameters = await preTransaction.getFundingTransactionParameters();
|
|
137
139
|
parameters.utxos = fundingParameters.utxos;
|
|
138
140
|
parameters.childTransactionRequiredValue =
|
|
139
|
-
preTransaction.estimateTransactionFees() + estimatedFees;
|
|
140
|
-
const signedTransaction = this.createFundTransaction(parameters);
|
|
141
|
+
(await preTransaction.estimateTransactionFees()) + estimatedFees;
|
|
142
|
+
const signedTransaction = await this.createFundTransaction(parameters);
|
|
141
143
|
if (!signedTransaction) {
|
|
142
144
|
throw new Error('Could not sign funding transaction.');
|
|
143
145
|
}
|
|
@@ -148,7 +150,7 @@ export class TransactionFactory {
|
|
|
148
150
|
nonWitnessUtxo: signedTransaction.tx.toBuffer(),
|
|
149
151
|
};
|
|
150
152
|
const finalTransaction = new UnwrapSegwitTransaction(newParams);
|
|
151
|
-
const outTx = finalTransaction.signPSBT();
|
|
153
|
+
const outTx = await finalTransaction.signPSBT();
|
|
152
154
|
const asBase64 = outTx.toBase64();
|
|
153
155
|
const psbt = this.writePSBTHeader(PSBTTypes.UNWRAP, asBase64);
|
|
154
156
|
return {
|
|
@@ -159,27 +161,27 @@ export class TransactionFactory {
|
|
|
159
161
|
}
|
|
160
162
|
async unwrap(unwrapParameters) {
|
|
161
163
|
const transaction = new UnwrapTransaction(unwrapParameters);
|
|
162
|
-
transaction.signTransaction();
|
|
164
|
+
await transaction.signTransaction();
|
|
163
165
|
const to = transaction.toAddress();
|
|
164
166
|
if (!to)
|
|
165
167
|
throw new Error('To address is required');
|
|
166
|
-
const estimatedGas = transaction.estimateTransactionFees();
|
|
168
|
+
const estimatedGas = await transaction.estimateTransactionFees();
|
|
167
169
|
const fundingParameters = {
|
|
168
170
|
...unwrapParameters,
|
|
169
171
|
childTransactionRequiredValue: estimatedGas,
|
|
170
172
|
to: to,
|
|
171
173
|
};
|
|
172
|
-
const preFundingTransaction = this.createFundTransaction(fundingParameters);
|
|
174
|
+
const preFundingTransaction = await this.createFundTransaction(fundingParameters);
|
|
173
175
|
unwrapParameters.utxos = this.getUTXOAsTransaction(preFundingTransaction.tx, to, 0);
|
|
174
176
|
const preTransaction = new UnwrapTransaction({
|
|
175
177
|
...unwrapParameters,
|
|
176
178
|
randomBytes: transaction.getRndBytes(),
|
|
177
179
|
});
|
|
178
|
-
preTransaction.signTransaction();
|
|
179
|
-
const parameters = preTransaction.getFundingTransactionParameters();
|
|
180
|
+
await preTransaction.signTransaction();
|
|
181
|
+
const parameters = await preTransaction.getFundingTransactionParameters();
|
|
180
182
|
parameters.utxos = fundingParameters.utxos;
|
|
181
|
-
parameters.childTransactionRequiredValue = preTransaction.estimateTransactionFees();
|
|
182
|
-
const signedTransaction = this.createFundTransaction(parameters);
|
|
183
|
+
parameters.childTransactionRequiredValue = await preTransaction.estimateTransactionFees();
|
|
184
|
+
const signedTransaction = await this.createFundTransaction(parameters);
|
|
183
185
|
if (!signedTransaction) {
|
|
184
186
|
throw new Error('Could not sign funding transaction.');
|
|
185
187
|
}
|
|
@@ -190,7 +192,7 @@ export class TransactionFactory {
|
|
|
190
192
|
nonWitnessUtxo: signedTransaction.tx.toBuffer(),
|
|
191
193
|
};
|
|
192
194
|
const finalTransaction = new UnwrapTransaction(newParams);
|
|
193
|
-
const outTx = finalTransaction.signPSBT();
|
|
195
|
+
const outTx = await finalTransaction.signPSBT();
|
|
194
196
|
const asBase64 = outTx.toBase64();
|
|
195
197
|
const psbt = this.writePSBTHeader(PSBTTypes.UNWRAP, asBase64);
|
|
196
198
|
return {
|
|
@@ -240,15 +242,16 @@ export class TransactionFactory {
|
|
|
240
242
|
};
|
|
241
243
|
return [newUtxo];
|
|
242
244
|
}
|
|
243
|
-
createFundTransaction(parameters) {
|
|
245
|
+
async createFundTransaction(parameters) {
|
|
244
246
|
const fundingTransaction = new FundingTransaction(parameters);
|
|
245
|
-
const signedTransaction = fundingTransaction.signTransaction();
|
|
247
|
+
const signedTransaction = await fundingTransaction.signTransaction();
|
|
246
248
|
if (!signedTransaction) {
|
|
247
249
|
throw new Error('Could not sign funding transaction.');
|
|
248
250
|
}
|
|
249
251
|
return {
|
|
250
252
|
tx: signedTransaction,
|
|
251
253
|
original: fundingTransaction,
|
|
254
|
+
estimatedFees: await fundingTransaction.estimateTransactionFees(),
|
|
252
255
|
};
|
|
253
256
|
}
|
|
254
257
|
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Psbt, Transaction } from 'bitcoinjs-lib';
|
|
2
|
+
import { Unisat } from './types/Unisat.js';
|
|
3
|
+
declare global {
|
|
4
|
+
interface Window {
|
|
5
|
+
unisat?: Unisat;
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
export declare abstract class BrowserSigner {
|
|
9
|
+
signTransaction(transaction: Psbt): Promise<Transaction>;
|
|
10
|
+
generateSigner(): Promise<void>;
|
|
11
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { Network, Psbt, Signer } from 'bitcoinjs-lib';
|
|
3
|
+
import { Address } from '@btc-vision/bsi-binary';
|
|
4
|
+
export declare abstract class CustomKeypair implements Signer {
|
|
5
|
+
abstract network: Network;
|
|
6
|
+
abstract publicKey: Buffer;
|
|
7
|
+
abstract addresses: Address[];
|
|
8
|
+
abstract p2tr: Address;
|
|
9
|
+
abstract p2pkh: Address;
|
|
10
|
+
protected constructor();
|
|
11
|
+
abstract signTaprootInput(transaction: Psbt, i: number, sighashTypes: number[]): Promise<void>;
|
|
12
|
+
abstract signInput(transaction: Psbt, i: number, sighashTypes: number[]): Promise<void>;
|
|
13
|
+
abstract getPublicKey(): Buffer;
|
|
14
|
+
abstract sign(hash: Buffer, lowR?: boolean): Buffer;
|
|
15
|
+
abstract signSchnorr(hash: Buffer): Buffer;
|
|
16
|
+
abstract verify(hash: Buffer, signature: Buffer): boolean;
|
|
17
|
+
abstract init(): Promise<void>;
|
|
18
|
+
}
|
|
@@ -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 = {}));
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { CustomKeypair } from '../BrowserSignerBase.js';
|
|
3
|
+
import { Network, Psbt } from 'bitcoinjs-lib';
|
|
4
|
+
import { Unisat } from '../types/Unisat.js';
|
|
5
|
+
import { Address } from '@btc-vision/bsi-binary';
|
|
6
|
+
declare global {
|
|
7
|
+
interface Window {
|
|
8
|
+
unisat?: Unisat;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export declare class UnisatSigner extends CustomKeypair {
|
|
12
|
+
private isInitialized;
|
|
13
|
+
constructor();
|
|
14
|
+
private _p2tr;
|
|
15
|
+
get p2tr(): Address;
|
|
16
|
+
private _p2pkh;
|
|
17
|
+
get p2pkh(): Address;
|
|
18
|
+
private _addresses;
|
|
19
|
+
get addresses(): Address[];
|
|
20
|
+
private _publicKey;
|
|
21
|
+
get publicKey(): Buffer;
|
|
22
|
+
_network: Network | undefined;
|
|
23
|
+
get network(): Network;
|
|
24
|
+
get unisat(): Unisat;
|
|
25
|
+
init(): Promise<void>;
|
|
26
|
+
getPublicKey(): Buffer;
|
|
27
|
+
sign(hash: Buffer, lowR?: boolean): Buffer;
|
|
28
|
+
signSchnorr(hash: Buffer): Buffer;
|
|
29
|
+
verify(hash: Buffer, signature: Buffer): boolean;
|
|
30
|
+
signTaprootInput(transaction: Psbt, i: number, sighashTypes: number[]): Promise<void>;
|
|
31
|
+
signInput(transaction: Psbt, i: number, sighashTypes: number[]): Promise<void>;
|
|
32
|
+
private combine;
|
|
33
|
+
private signTweaked;
|
|
34
|
+
private getNonDuplicateScriptSig;
|
|
35
|
+
}
|