@btc-vision/transaction 1.0.1 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (90) hide show
  1. package/browser/_version.d.ts +1 -1
  2. package/browser/generators/AddressGenerator.d.ts +7 -0
  3. package/browser/generators/builders/DeploymentGenerator.d.ts +1 -0
  4. package/browser/index.js +1 -1
  5. package/browser/keypair/Wallet.d.ts +1 -0
  6. package/browser/opnet.d.ts +2 -0
  7. package/browser/tests/Regtest.d.ts +3 -0
  8. package/browser/tests/gen.d.ts +1 -0
  9. package/browser/tests/test.d.ts +1 -0
  10. package/browser/tests/transfer.d.ts +1 -0
  11. package/browser/transaction/builders/TransactionBuilder.d.ts +4 -3
  12. package/browser/verification/TapscriptVerificator.d.ts +17 -0
  13. package/build/_version.d.ts +1 -1
  14. package/build/_version.js +1 -1
  15. package/build/generators/AddressGenerator.d.ts +7 -0
  16. package/build/generators/AddressGenerator.js +21 -0
  17. package/build/generators/OPNetAddressGenerator.d.ts +0 -0
  18. package/build/generators/OPNetAddressGenerator.js +1 -0
  19. package/build/generators/builders/DeploymentGenerator.d.ts +1 -0
  20. package/build/generators/builders/DeploymentGenerator.js +10 -7
  21. package/build/keypair/Wallet.d.ts +1 -0
  22. package/build/keypair/Wallet.js +6 -0
  23. package/build/opnet.d.ts +2 -0
  24. package/build/opnet.js +2 -0
  25. package/build/scripts/Regtest.d.ts +0 -1
  26. package/build/scripts/Regtest.js +0 -14
  27. package/build/scripts/test.js +7 -7
  28. package/build/tests/gen.d.ts +1 -0
  29. package/build/tests/gen.js +15 -0
  30. package/build/tests/test.d.ts +1 -0
  31. package/build/tests/test.js +51 -1
  32. package/build/tests/transfer.d.ts +1 -0
  33. package/build/tests/transfer.js +74 -0
  34. package/build/transaction/builders/TransactionBuilder.d.ts +4 -3
  35. package/build/transaction/builders/TransactionBuilder.js +9 -4
  36. package/build/verification/TapscriptVerificator.d.ts +17 -0
  37. package/build/verification/TapscriptVerificator.js +43 -0
  38. package/docs/assets/navigation.js +1 -1
  39. package/docs/assets/search.js +1 -1
  40. package/docs/classes/AddressGenerator.html +178 -0
  41. package/docs/classes/BitcoinUtils.html +3 -3
  42. package/docs/classes/CalldataGenerator.html +10 -10
  43. package/docs/classes/Compressor.html +4 -4
  44. package/docs/classes/ContractBaseMetadata.html +4 -4
  45. package/docs/classes/DeploymentGenerator.html +10 -9
  46. package/docs/classes/EcKeyPair.html +16 -16
  47. package/docs/classes/FundingTransaction.html +73 -50
  48. package/docs/classes/Generator.html +9 -9
  49. package/docs/classes/InteractionTransaction.html +94 -71
  50. package/docs/classes/TapscriptVerificator.html +180 -0
  51. package/docs/classes/TransactionBuilder.html +76 -53
  52. package/docs/classes/TransactionFactory.html +2 -2
  53. package/docs/classes/TweakedSigner.html +2 -2
  54. package/docs/classes/UTXOManager.html +3 -3
  55. package/docs/classes/Wallet.html +11 -9
  56. package/docs/classes/wBTC.html +7 -7
  57. package/docs/enums/TransactionType.html +2 -2
  58. package/docs/interfaces/ContractAddressVerificationParams.html +179 -0
  59. package/docs/interfaces/FetchUTXOParams.html +2 -2
  60. package/docs/interfaces/IFundingTransactionParameters.html +2 -2
  61. package/docs/interfaces/IInteractionParameters.html +2 -2
  62. package/docs/interfaces/ITransactionDataContractDeployment.html +2 -2
  63. package/docs/interfaces/ITransactionDataContractInteractionWrap.html +2 -2
  64. package/docs/interfaces/ITransactionParameters.html +2 -2
  65. package/docs/interfaces/IWallet.html +4 -4
  66. package/docs/interfaces/NetworkInformation.html +2 -2
  67. package/docs/interfaces/PsbtInputExtended.html +1 -1
  68. package/docs/interfaces/PsbtOutputExtendedAddress.html +2 -2
  69. package/docs/interfaces/PsbtOutputExtendedScript.html +2 -2
  70. package/docs/interfaces/RawUTXOResponse.html +2 -2
  71. package/docs/interfaces/TapLeafScript.html +2 -2
  72. package/docs/interfaces/TweakSettings.html +3 -3
  73. package/docs/interfaces/UTXO.html +2 -2
  74. package/docs/interfaces/UpdateInput.html +2 -2
  75. package/docs/modules.html +5 -2
  76. package/docs/types/PsbtOutputExtended.html +1 -1
  77. package/docs/variables/version.html +1 -1
  78. package/package.json +2 -1
  79. package/src/_version.ts +1 -1
  80. package/src/generators/AddressGenerator.ts +29 -0
  81. package/src/generators/builders/DeploymentGenerator.ts +16 -13
  82. package/src/keypair/Wallet.ts +12 -0
  83. package/src/opnet.ts +4 -0
  84. package/src/tests/gen.ts +24 -0
  85. package/src/tests/test.ts +17 -54
  86. package/src/tests/transfer.ts +102 -0
  87. package/src/transaction/builders/TransactionBuilder.ts +12 -5
  88. package/src/verification/TapscriptVerificator.ts +89 -0
  89. package/tests/TransactionBuilder.test.ts +58 -58
  90. package/tsconfig.webpack.json +8 -0
@@ -0,0 +1,102 @@
1
+ import { wBTC } from '../metadata/contracts/wBTC.js';
2
+ import { Wallet } from '../keypair/Wallet.js';
3
+ import { Testnet } 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.testnet;
13
+ const rpc: BitcoinRPC = new BitcoinRPC();
14
+ const wBtc: wBTC = new wBTC(network);
15
+ const wallet: Wallet = new Wallet(Testnet.wallet, network);
16
+
17
+ const utxoManager: UTXOManager = new UTXOManager('https://testnet.opnet.org');
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 = false;
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(Testnet.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(
63
+ 'tb1pt3ncc5ktfzpry2uvnag06v3jkv4quvmdydf09q8fx6rkgd7f5s8q3aenuk',
64
+ 5000000n,
65
+ );
66
+
67
+ const interactionParameters: IInteractionParameters = {
68
+ from: wallet.p2wpkh,
69
+ to: wBtc.getAddress(),
70
+ utxos: utxos,
71
+ signer: wallet.keypair,
72
+ network: network,
73
+ feeRate: 150,
74
+ priorityFee: 50000n,
75
+ calldata: calldata,
76
+ };
77
+
78
+ const finalTx = factory.signInteraction(interactionParameters);
79
+ const firstTxBroadcast = await rpc.sendRawTransaction({
80
+ hexstring: finalTx[0],
81
+ });
82
+
83
+ console.log(`First transaction broadcasted: ${firstTxBroadcast}`);
84
+
85
+ if (!firstTxBroadcast) {
86
+ throw new Error('Could not broadcast first transaction');
87
+ }
88
+
89
+ const secondTxBroadcast = await rpc.sendRawTransaction({
90
+ hexstring: finalTx[1],
91
+ });
92
+
93
+ console.log(`Second transaction broadcasted: ${secondTxBroadcast}`);
94
+
95
+ if (!secondTxBroadcast) {
96
+ throw new Error('Could not broadcast second transaction');
97
+ }
98
+
99
+ if (shouldMineBlock) {
100
+ await mineBlock();
101
+ }
102
+ })();
@@ -9,6 +9,7 @@ import { EcKeyPair } from '../../keypair/EcKeyPair.js';
9
9
  import { Address } from '@btc-vision/bsi-binary';
10
10
  import { UTXO } from '../../utxo/interfaces/IUTXO.js';
11
11
  import { ECPairInterface } from 'ecpair';
12
+ import { Logger } from '@btc-vision/logger';
12
13
 
13
14
  /**
14
15
  * Allows to build a transaction like you would on Ethereum.
@@ -16,9 +17,13 @@ import { ECPairInterface } from 'ecpair';
16
17
  * @abstract
17
18
  * @class TransactionBuilder
18
19
  */
19
- export abstract class TransactionBuilder<T extends TransactionType> {
20
- protected static readonly LOCK_LEAF_SCRIPT: Buffer = script.compile([opcodes.OP_0]);
21
- protected static readonly MINIMUM_DUST: bigint = 330n;
20
+ export abstract class TransactionBuilder<T extends TransactionType> extends Logger {
21
+ public static readonly LOCK_LEAF_SCRIPT: Buffer = script.compile([
22
+ opcodes.OP_0,
23
+ opcodes.OP_VERIFY,
24
+ ]);
25
+
26
+ public static readonly MINIMUM_DUST: bigint = 330n;
22
27
 
23
28
  public abstract readonly type: T;
24
29
  public readonly logColor: string = '#785def';
@@ -105,6 +110,8 @@ export abstract class TransactionBuilder<T extends TransactionType> {
105
110
  * @param {ITransactionParameters} parameters - The transaction parameters
106
111
  */
107
112
  protected constructor(parameters: ITransactionParameters) {
113
+ super();
114
+
108
115
  this.signer = parameters.signer;
109
116
  this.network = parameters.network;
110
117
  this.feeRate = parameters.feeRate;
@@ -277,7 +284,7 @@ export abstract class TransactionBuilder<T extends TransactionType> {
277
284
  return;
278
285
  }
279
286
 
280
- console.warn(
287
+ this.warn(
281
288
  `Amount to send back is less than the minimum dust, will be consumed in fees instead.`,
282
289
  );
283
290
  }
@@ -568,7 +575,7 @@ export abstract class TransactionBuilder<T extends TransactionType> {
568
575
  } catch (e) {
569
576
  const err: Error = e as Error;
570
577
 
571
- console.error(
578
+ this.error(
572
579
  `[internalBuildTransaction] Something went wrong while getting building the transaction: ${err.stack}`,
573
580
  );
574
581
  }
@@ -0,0 +1,89 @@
1
+ import bitcoin, { Network, networks, Payment, payments } from 'bitcoinjs-lib';
2
+ import { toXOnly } from 'bitcoinjs-lib/src/psbt/bip371.js';
3
+ import { Taptree } from 'bitcoinjs-lib/src/types.js';
4
+ import { DeploymentGenerator } from '../generators/builders/DeploymentGenerator.js';
5
+ import { TransactionBuilder } from '../transaction/builders/TransactionBuilder.js';
6
+ import { AddressGenerator } from '../generators/AddressGenerator.js';
7
+
8
+ export interface ContractAddressVerificationParams {
9
+ readonly deployerPubKeyXOnly: Buffer;
10
+ readonly contractSaltPubKey: Buffer;
11
+ readonly originalSalt: Buffer;
12
+ readonly bytecode: Buffer;
13
+ readonly network?: Network;
14
+ }
15
+
16
+ export class TapscriptVerificator {
17
+ private static readonly TAP_SCRIPT_VERSION: number = 192;
18
+
19
+ public static getContractAddress(
20
+ params: ContractAddressVerificationParams,
21
+ ): string | undefined {
22
+ const network = params.network || networks.bitcoin;
23
+ const scriptBuilder: DeploymentGenerator = new DeploymentGenerator(
24
+ params.deployerPubKeyXOnly,
25
+ toXOnly(params.contractSaltPubKey),
26
+ network,
27
+ );
28
+
29
+ const compiledTargetScript: Buffer = scriptBuilder.compile(
30
+ params.bytecode,
31
+ params.originalSalt,
32
+ );
33
+
34
+ const scriptTree: Taptree = [
35
+ {
36
+ output: compiledTargetScript,
37
+ version: TapscriptVerificator.TAP_SCRIPT_VERSION,
38
+ },
39
+ {
40
+ output: TransactionBuilder.LOCK_LEAF_SCRIPT,
41
+ version: TapscriptVerificator.TAP_SCRIPT_VERSION,
42
+ },
43
+ ];
44
+
45
+ return TapscriptVerificator.generateAddressFromScript(params, scriptTree);
46
+ }
47
+
48
+ public static getContractSeed(
49
+ deployerPubKey: Buffer,
50
+ bytecode: Buffer,
51
+ saltHash: Buffer,
52
+ ): Buffer {
53
+ const sha256OfBytecode: Buffer = bitcoin.crypto.hash256(bytecode);
54
+ const buf: Buffer = Buffer.concat([deployerPubKey, saltHash, sha256OfBytecode]);
55
+
56
+ return bitcoin.crypto.hash256(buf);
57
+ }
58
+
59
+ public static generateContractVirtualAddress(
60
+ deployerPubKey: Buffer,
61
+ bytecode: Buffer,
62
+ saltHash: Buffer,
63
+ network: Network = networks.bitcoin,
64
+ ): string {
65
+ const virtualAddress: Buffer = TapscriptVerificator.getContractSeed(
66
+ deployerPubKey,
67
+ bytecode,
68
+ saltHash,
69
+ );
70
+
71
+ return AddressGenerator.generatePKSH(virtualAddress, network);
72
+ }
73
+
74
+ public static generateAddressFromScript(
75
+ params: ContractAddressVerificationParams,
76
+ scriptTree: Taptree,
77
+ ): string | undefined {
78
+ const network = params.network || networks.bitcoin;
79
+
80
+ const transactionData: Payment = {
81
+ internalPubkey: params.deployerPubKeyXOnly,
82
+ network: network,
83
+ scriptTree: scriptTree,
84
+ };
85
+
86
+ const tx: Payment = payments.p2tr(transactionData);
87
+ return tx.address;
88
+ }
89
+ }
@@ -1,58 +1,58 @@
1
- import 'jest';
2
- import { Regtest } from '../src/tests/Regtest.js';
3
- import { Logger } from '@btc-vision/logger';
4
- import { networks } from 'bitcoinjs-lib';
5
- import {
6
- FetchUTXOParams,
7
- IInteractionParameters,
8
- TransactionFactory,
9
- UTXO,
10
- UTXOManager,
11
- Wallet,
12
- wBTC,
13
- } from '../src/index.js';
14
-
15
- const logger: Logger = new Logger();
16
- const network: networks.Network = networks.regtest;
17
-
18
- describe('Transaction Builder', () => {
19
- const wBtc: wBTC = new wBTC(network);
20
-
21
- const wallet: Wallet = new Wallet(Regtest.wallet, network);
22
- logger.log(`Loaded wallet: ${wallet.p2tr} - ${wallet.p2wpkh}`);
23
-
24
- const utxoManager: UTXOManager = new UTXOManager('http://localhost:9001');
25
- const factory: TransactionFactory = new TransactionFactory();
26
-
27
- /** @test {TransactionBuilder#build} */
28
- test('should be able to build a transaction', async () => {
29
- const utxoSetting: FetchUTXOParams = {
30
- address: wallet.p2wpkh,
31
- minAmount: 10000n,
32
- requestedAmount: 100000n,
33
- };
34
-
35
- const utxos: UTXO[] = await utxoManager.fetchUTXO(utxoSetting);
36
- console.log(`UTXOs:`, utxos);
37
-
38
- if (!utxos) {
39
- throw new Error('No UTXOs found');
40
- }
41
-
42
- logger.log(`UTXOs fetched. Count: ${utxos.length}`);
43
-
44
- const interactionParameters: IInteractionParameters = {
45
- from: wallet.p2wpkh,
46
- to: wBtc.getAddress(),
47
- utxos: utxos,
48
- signer: wallet.keypair,
49
- network: network,
50
- feeRate: 150,
51
- priorityFee: 500n,
52
- calldata: Buffer.from('test'),
53
- };
54
-
55
- const finalTx = factory.signInteraction(interactionParameters);
56
- console.log(`Transaction:`, finalTx);
57
- });
58
- });
1
+ import 'jest';
2
+ import { Regtest } from '../src/tests/Regtest.js';
3
+ import { Logger } from '@btc-vision/logger';
4
+ import { networks } from 'bitcoinjs-lib';
5
+ import {
6
+ FetchUTXOParams,
7
+ IInteractionParameters,
8
+ TransactionFactory,
9
+ UTXO,
10
+ UTXOManager,
11
+ Wallet,
12
+ wBTC,
13
+ } from '../src/index.js';
14
+
15
+ const logger: Logger = new Logger();
16
+ const network: networks.Network = networks.regtest;
17
+
18
+ describe('Transaction Builder', () => {
19
+ const wBtc: wBTC = new wBTC(network);
20
+
21
+ const wallet: Wallet = new Wallet(Regtest.wallet, network);
22
+ logger.log(`Loaded wallet: ${wallet.p2tr} - ${wallet.p2wpkh}`);
23
+
24
+ const utxoManager: UTXOManager = new UTXOManager('http://localhost:9001');
25
+ const factory: TransactionFactory = new TransactionFactory();
26
+
27
+ /** @test {TransactionBuilder#build} */
28
+ test('should be able to build a transaction', async () => {
29
+ const utxoSetting: FetchUTXOParams = {
30
+ address: wallet.p2wpkh,
31
+ minAmount: 10000n,
32
+ requestedAmount: 100000n,
33
+ };
34
+
35
+ const utxos: UTXO[] = await utxoManager.fetchUTXO(utxoSetting);
36
+ console.log(`UTXOs:`, utxos);
37
+
38
+ if (!utxos) {
39
+ throw new Error('No UTXOs found');
40
+ }
41
+
42
+ logger.log(`UTXOs fetched. Count: ${utxos.length}`);
43
+
44
+ const interactionParameters: IInteractionParameters = {
45
+ from: wallet.p2wpkh,
46
+ to: wBtc.getAddress(),
47
+ utxos: utxos,
48
+ signer: wallet.keypair,
49
+ network: network,
50
+ feeRate: 150,
51
+ priorityFee: 500n,
52
+ calldata: Buffer.from('test'),
53
+ };
54
+
55
+ const finalTx = factory.signInteraction(interactionParameters);
56
+ console.log(`Transaction:`, finalTx);
57
+ });
58
+ });
@@ -6,6 +6,14 @@
6
6
  "target": "esnext",
7
7
  "declaration": true,
8
8
  "esModuleInterop": true,
9
+ "skipLibCheck": true,
10
+ "strict": true,
11
+ "baseUrl": ".",
12
+ "paths": {
13
+ "@btc-vision/logger": [
14
+ "node_modules/@btc-vision/logger"
15
+ ]
16
+ },
9
17
  "moduleResolution": "bundler"
10
18
  }
11
19
  }