@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,44 @@
|
|
|
1
|
+
import { Network, networks } from 'bitcoinjs-lib';
|
|
2
|
+
import { ContractBaseMetadata } from '../ContractBaseMetadata.js';
|
|
3
|
+
import { Address } from '@btc-vision/bsi-binary';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @description Wrapped Bitcoin (wBTC) contract metadata.
|
|
7
|
+
* */
|
|
8
|
+
export class wBTC extends ContractBaseMetadata {
|
|
9
|
+
/**
|
|
10
|
+
* @description Token Name
|
|
11
|
+
*/
|
|
12
|
+
public readonly tokenName: string = 'Wrapped Bitcoin';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @description Token Symbol
|
|
16
|
+
*/
|
|
17
|
+
public readonly tokenSymbol: string = 'wBTC';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @description Token Decimals, same as Bitcoin
|
|
21
|
+
*/
|
|
22
|
+
public readonly decimals: number = 8;
|
|
23
|
+
|
|
24
|
+
protected readonly address: Address;
|
|
25
|
+
|
|
26
|
+
constructor(protected network: Network = networks.bitcoin) {
|
|
27
|
+
super(network);
|
|
28
|
+
|
|
29
|
+
this.address = wBTC.getAddress(network);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
public static getAddress(network: Network = networks.bitcoin): Address {
|
|
33
|
+
switch (network) {
|
|
34
|
+
case networks.bitcoin:
|
|
35
|
+
return 'bcrt1pcw0828yjrtlrc6mkp3lkq30j7wc7slsh7k7dyh53mrs4f8d74l6qumhqp4';
|
|
36
|
+
case networks.regtest:
|
|
37
|
+
return 'bcrt1pcw0828yjrtlrc6mkp3lkq30j7wc7slsh7k7dyh53mrs4f8d74l6qumhqp4';
|
|
38
|
+
case networks.testnet:
|
|
39
|
+
return 'tb1pq64lx73fwyrdp4asvl7xt5r5qvxvt9wy82x75taqtzvd64f58nasansurj';
|
|
40
|
+
default:
|
|
41
|
+
throw new Error(`Invalid network: ${network}`);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
package/src/opnet.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export { version } from './_version.js';
|
|
2
|
+
|
|
3
|
+
/** Bytecode */
|
|
4
|
+
export * from './bytecode/Compressor.js';
|
|
5
|
+
|
|
6
|
+
/** Generators */
|
|
7
|
+
export * from './generators/Generator.js';
|
|
8
|
+
export * from './generators/builders/CalldataGenerator.js';
|
|
9
|
+
export * from './generators/builders/DeploymentGenerator.js';
|
|
10
|
+
|
|
11
|
+
/** Key Pair */
|
|
12
|
+
export * from './keypair/EcKeyPair.js';
|
|
13
|
+
export * from './keypair/Wallet.js';
|
|
14
|
+
export * from './keypair/interfaces/IWallet.js';
|
|
15
|
+
|
|
16
|
+
/** Metadata */
|
|
17
|
+
export * from './metadata/contracts/wBTC.js';
|
|
18
|
+
export * from './metadata/ContractBaseMetadata.js';
|
|
19
|
+
|
|
20
|
+
/** Network */
|
|
21
|
+
export * from './network/NetworkInformation.js';
|
|
22
|
+
|
|
23
|
+
/** Signer */
|
|
24
|
+
export * from './signer/TweakedSigner.js';
|
|
25
|
+
|
|
26
|
+
/** Transaction */
|
|
27
|
+
export * from './transaction/TransactionFactory.js';
|
|
28
|
+
export * from './transaction/interfaces/ITransactionParameters.js';
|
|
29
|
+
export * from './transaction/interfaces/Tap.js';
|
|
30
|
+
export * from './transaction/enums/TransactionType.js';
|
|
31
|
+
|
|
32
|
+
/** Builders */
|
|
33
|
+
export * from './transaction/builders/InteractionTransaction.js';
|
|
34
|
+
export * from './transaction/builders/FundingTransaction.js';
|
|
35
|
+
export * from './transaction/builders/TransactionBuilder.js';
|
|
36
|
+
|
|
37
|
+
/** Utils */
|
|
38
|
+
export * from './utils/BitcoinUtils.js';
|
|
39
|
+
|
|
40
|
+
/** UTXO */
|
|
41
|
+
export * from './utxo/interfaces/IUTXO.js';
|
|
42
|
+
export * from './utxo/UTXOManager.js';
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { NetworkInformation } from '../network/NetworkInformation.js';
|
|
2
|
+
import { BitcoinNetwork } from '@btc-vision/bsi-common';
|
|
3
|
+
|
|
4
|
+
export const Regtest: NetworkInformation = {
|
|
5
|
+
wallet: {
|
|
6
|
+
address: 'bcrt1qfqsr3m7vjxheghcvw4ks0fryqxfq8qzjf8fxes',
|
|
7
|
+
publicKey: '020373626d317ae8788ce3280b491068610d840c23ecb64c14075bbb9f670af52c',
|
|
8
|
+
privateKey: 'cRCiYAgCBrU7hSaJBRuPqKVYXQqM5CKXbMfWHb25X4FDAWJ8Ai92',
|
|
9
|
+
},
|
|
10
|
+
|
|
11
|
+
config: {
|
|
12
|
+
BITCOIND_NETWORK: BitcoinNetwork.Regtest,
|
|
13
|
+
BITCOIND_HOST: '51.81.67.34',
|
|
14
|
+
BITCOIND_PORT: 9242,
|
|
15
|
+
|
|
16
|
+
BITCOIND_USERNAME: 'HJSiowseujhs',
|
|
17
|
+
BITCOIND_PASSWORD: 'YHEFHSDJ23JOIhjjef2ied9u290efu2930u90U',
|
|
18
|
+
},
|
|
19
|
+
};
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { wBTC } from '../metadata/contracts/wBTC.js';
|
|
2
|
+
import { Wallet } from '../keypair/Wallet.js';
|
|
3
|
+
import { Regtest } from './Regtest.js';
|
|
4
|
+
import { UTXOManager } from '../utxo/UTXOManager.js';
|
|
5
|
+
import { FetchUTXOParams, UTXO } from '../utxo/interfaces/IUTXO.js';
|
|
6
|
+
import { IInteractionParameters } from '../transaction/interfaces/ITransactionParameters.js';
|
|
7
|
+
import { networks } from 'bitcoinjs-lib';
|
|
8
|
+
import { TransactionFactory } from '../transaction/TransactionFactory.js';
|
|
9
|
+
import { BitcoinRPC } from '@btc-vision/bsi-bitcoin-rpc';
|
|
10
|
+
import { ABICoder, BinaryWriter } from '@btc-vision/bsi-binary';
|
|
11
|
+
|
|
12
|
+
const network: networks.Network = networks.regtest;
|
|
13
|
+
const rpc: BitcoinRPC = new BitcoinRPC();
|
|
14
|
+
const wBtc: wBTC = new wBTC(network);
|
|
15
|
+
const wallet: Wallet = new Wallet(Regtest.wallet, network);
|
|
16
|
+
|
|
17
|
+
const utxoManager: UTXOManager = new UTXOManager('http://localhost:9001');
|
|
18
|
+
const factory: TransactionFactory = new TransactionFactory();
|
|
19
|
+
|
|
20
|
+
const abiCoder: ABICoder = new ABICoder();
|
|
21
|
+
const transferSelector = Number(`0x` + abiCoder.encodeSelector('transfer'));
|
|
22
|
+
|
|
23
|
+
function getTransferToCalldata(to: string, amount: bigint): Buffer {
|
|
24
|
+
const addCalldata: BinaryWriter = new BinaryWriter();
|
|
25
|
+
addCalldata.writeSelector(transferSelector);
|
|
26
|
+
addCalldata.writeAddress(to);
|
|
27
|
+
addCalldata.writeU256(amount);
|
|
28
|
+
|
|
29
|
+
return Buffer.from(addCalldata.getBuffer());
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const shouldMineBlock: boolean = true;
|
|
33
|
+
|
|
34
|
+
async function mineBlock(): Promise<boolean> {
|
|
35
|
+
// lets mine 1 block.
|
|
36
|
+
const ok = await rpc.generateToAddress(1, wallet.p2wpkh, 'default');
|
|
37
|
+
if (!ok) {
|
|
38
|
+
throw new Error('Could not mine block');
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
console.log(`Mined block`, ok);
|
|
42
|
+
|
|
43
|
+
return !!ok.length;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
(async () => {
|
|
47
|
+
await rpc.init(Regtest.config);
|
|
48
|
+
|
|
49
|
+
const utxoSetting: FetchUTXOParams = {
|
|
50
|
+
address: wallet.p2wpkh,
|
|
51
|
+
minAmount: 10000n,
|
|
52
|
+
requestedAmount: 100000n,
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const utxos: UTXO[] = await utxoManager.fetchUTXO(utxoSetting);
|
|
56
|
+
console.log(`UTXOs:`, utxos);
|
|
57
|
+
|
|
58
|
+
if (!utxos) {
|
|
59
|
+
throw new Error('No UTXOs found');
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const calldata: Buffer = getTransferToCalldata(wBtc.getAddress(), 5000000n);
|
|
63
|
+
const interactionParameters: IInteractionParameters = {
|
|
64
|
+
from: wallet.p2wpkh,
|
|
65
|
+
to: wBtc.getAddress(),
|
|
66
|
+
utxos: utxos,
|
|
67
|
+
signer: wallet.keypair,
|
|
68
|
+
network: network,
|
|
69
|
+
feeRate: 150,
|
|
70
|
+
priorityFee: 50000n,
|
|
71
|
+
calldata: calldata,
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
const finalTx = factory.signInteraction(interactionParameters);
|
|
75
|
+
const firstTxBroadcast = await rpc.sendRawTransaction({
|
|
76
|
+
hexstring: finalTx[0],
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
console.log(`First transaction broadcasted: ${firstTxBroadcast}`);
|
|
80
|
+
|
|
81
|
+
if (!firstTxBroadcast) {
|
|
82
|
+
throw new Error('Could not broadcast first transaction');
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const secondTxBroadcast = await rpc.sendRawTransaction({
|
|
86
|
+
hexstring: finalTx[1],
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
console.log(`Second transaction broadcasted: ${secondTxBroadcast}`);
|
|
90
|
+
|
|
91
|
+
if (!secondTxBroadcast) {
|
|
92
|
+
throw new Error('Could not broadcast second transaction');
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (shouldMineBlock) {
|
|
96
|
+
await mineBlock();
|
|
97
|
+
}
|
|
98
|
+
})();
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { initEccLib, Network, Signer } from 'bitcoinjs-lib';
|
|
2
|
+
import { tapTweakHash } from 'bitcoinjs-lib/src/payments/bip341.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
|
+
|
|
7
|
+
initEccLib(ecc);
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Tweak settings
|
|
11
|
+
*/
|
|
12
|
+
export interface TweakSettings {
|
|
13
|
+
/**
|
|
14
|
+
* The network to use
|
|
15
|
+
*/
|
|
16
|
+
readonly network?: Network;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The tweak hash to use
|
|
20
|
+
*/
|
|
21
|
+
tweakHash?: Buffer;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Class for tweaking signers
|
|
26
|
+
* @class TweakedSigner
|
|
27
|
+
*/
|
|
28
|
+
export class TweakedSigner {
|
|
29
|
+
/**
|
|
30
|
+
* Tweak a signer
|
|
31
|
+
* @param {Signer} signer - The signer to tweak
|
|
32
|
+
* @param {TweakSettings} opts - The tweak settings
|
|
33
|
+
*/
|
|
34
|
+
public static tweakSigner(signer: Signer, opts: TweakSettings = {}): Signer {
|
|
35
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
36
|
+
// @ts-ignore
|
|
37
|
+
let privateKey: Uint8Array | undefined = signer.privateKey!;
|
|
38
|
+
if (!privateKey) {
|
|
39
|
+
throw new Error('Private key is required for tweaking signer!');
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (signer.publicKey[0] === 3) {
|
|
43
|
+
privateKey = ecc.privateNegate(privateKey);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const tweakedPrivateKey = ecc.privateAdd(
|
|
47
|
+
privateKey,
|
|
48
|
+
tapTweakHash(toXOnly(signer.publicKey), opts.tweakHash),
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
if (!tweakedPrivateKey) {
|
|
52
|
+
throw new Error('Invalid tweaked private key!');
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return EcKeyPair.fromPrivateKey(Buffer.from(tweakedPrivateKey), opts.network);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { Transaction } from 'bitcoinjs-lib';
|
|
2
|
+
import {
|
|
3
|
+
IFundingTransactionParameters,
|
|
4
|
+
IInteractionParameters,
|
|
5
|
+
} from './interfaces/ITransactionParameters.js';
|
|
6
|
+
import { FundingTransaction } from './builders/FundingTransaction.js';
|
|
7
|
+
import { Output } from 'bitcoinjs-lib/src/transaction.js';
|
|
8
|
+
import { UTXO } from '../utxo/interfaces/IUTXO.js';
|
|
9
|
+
import { InteractionTransaction } from './builders/InteractionTransaction.js';
|
|
10
|
+
|
|
11
|
+
export class TransactionFactory {
|
|
12
|
+
/**
|
|
13
|
+
* @description Generates the required transactions.
|
|
14
|
+
* @returns {[Transaction, Transaction]} - The signed transaction
|
|
15
|
+
*/
|
|
16
|
+
public signInteraction(interactionParameters: IInteractionParameters): [string, string] {
|
|
17
|
+
const preTransaction: InteractionTransaction = new InteractionTransaction(
|
|
18
|
+
interactionParameters,
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
// Initial generation
|
|
22
|
+
preTransaction.signTransaction();
|
|
23
|
+
|
|
24
|
+
const parameters: IFundingTransactionParameters =
|
|
25
|
+
preTransaction.getFundingTransactionParameters();
|
|
26
|
+
|
|
27
|
+
const fundingTransaction: FundingTransaction = new FundingTransaction(parameters);
|
|
28
|
+
const signedTransaction: Transaction = fundingTransaction.signTransaction();
|
|
29
|
+
if (!signedTransaction) {
|
|
30
|
+
throw new Error('Could not sign funding transaction.');
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const out: Output = signedTransaction.outs[0];
|
|
34
|
+
const newUtxo: UTXO = {
|
|
35
|
+
transactionId: signedTransaction.getId(),
|
|
36
|
+
outputIndex: 0,
|
|
37
|
+
scriptPubKey: {
|
|
38
|
+
hex: out.script.toString('hex'),
|
|
39
|
+
address: preTransaction.getScriptAddress(),
|
|
40
|
+
},
|
|
41
|
+
value: BigInt(out.value),
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const newParams: IInteractionParameters = {
|
|
45
|
+
...interactionParameters,
|
|
46
|
+
utxos: [newUtxo],
|
|
47
|
+
randomBytes: preTransaction.getRndBytes(),
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const finalTransaction: InteractionTransaction = new InteractionTransaction(newParams);
|
|
51
|
+
|
|
52
|
+
// We have to regenerate using the new utxo
|
|
53
|
+
const outTx: Transaction = finalTransaction.signTransaction();
|
|
54
|
+
|
|
55
|
+
return [signedTransaction.toHex(), outTx.toHex()];
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { TransactionType } from '../enums/TransactionType.js';
|
|
2
|
+
import { IFundingTransactionParameters } from '../interfaces/ITransactionParameters.js';
|
|
3
|
+
import { Signer } from 'bitcoinjs-lib';
|
|
4
|
+
import { TransactionBuilder } from './TransactionBuilder.js';
|
|
5
|
+
|
|
6
|
+
export class FundingTransaction extends TransactionBuilder<TransactionType.FUNDING> {
|
|
7
|
+
public readonly type: TransactionType.FUNDING = TransactionType.FUNDING;
|
|
8
|
+
|
|
9
|
+
protected childTransactionRequiredFees: bigint;
|
|
10
|
+
|
|
11
|
+
constructor(parameters: IFundingTransactionParameters) {
|
|
12
|
+
super(parameters);
|
|
13
|
+
|
|
14
|
+
this.childTransactionRequiredFees = parameters.childTransactionRequiredFees;
|
|
15
|
+
|
|
16
|
+
this.internalInit();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
protected override buildTransaction(): void {
|
|
20
|
+
this.addInputsFromUTXO();
|
|
21
|
+
|
|
22
|
+
const amountSpent: bigint =
|
|
23
|
+
this.getTransactionOPNetFee() + this.childTransactionRequiredFees;
|
|
24
|
+
|
|
25
|
+
this.addOutput({
|
|
26
|
+
value: Number(amountSpent),
|
|
27
|
+
address: this.to,
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
this.addRefundOutput(amountSpent);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
protected override getSignerKey(): Signer {
|
|
34
|
+
return this.signer;
|
|
35
|
+
}
|
|
36
|
+
}
|