@btc-vision/transaction 1.0.5 → 1.0.7

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 (48) hide show
  1. package/browser/_version.d.ts +1 -1
  2. package/browser/index.js +1 -1
  3. package/browser/index.js.LICENSE.txt +2 -0
  4. package/build/_version.d.ts +1 -1
  5. package/build/_version.js +1 -1
  6. package/build/generators/AddressGenerator.js +1 -1
  7. package/build/keypair/EcKeyPair.js +1 -1
  8. package/build/signer/TweakedSigner.js +1 -1
  9. package/build/transaction/builders/TransactionBuilder.js +1 -1
  10. package/cjs/_version.d.ts +1 -1
  11. package/cjs/_version.js +1 -1
  12. package/cjs/bytecode/Compressor.js +1 -1
  13. package/cjs/generators/AddressGenerator.d.ts +7 -0
  14. package/cjs/generators/AddressGenerator.js +48 -0
  15. package/cjs/generators/Generator.d.ts +1 -2
  16. package/cjs/generators/Generator.js +1 -3
  17. package/cjs/generators/builders/DeploymentGenerator.d.ts +1 -0
  18. package/cjs/generators/builders/DeploymentGenerator.js +10 -7
  19. package/cjs/keypair/Wallet.d.ts +1 -0
  20. package/cjs/keypair/Wallet.js +6 -0
  21. package/cjs/opnet.d.ts +23 -0
  22. package/cjs/opnet.js +37 -0
  23. package/{browser/scripts → cjs/tests}/Regtest.d.ts +1 -0
  24. package/cjs/tests/Regtest.js +32 -0
  25. package/cjs/tests/gen.js +17 -0
  26. package/cjs/tests/test.js +53 -0
  27. package/cjs/tests/transfer.js +76 -0
  28. package/cjs/transaction/TransactionFactory.js +7 -4
  29. package/cjs/transaction/builders/FundingTransaction.js +1 -1
  30. package/cjs/transaction/builders/InteractionTransaction.d.ts +2 -0
  31. package/cjs/transaction/builders/InteractionTransaction.js +11 -2
  32. package/cjs/transaction/builders/TransactionBuilder.d.ts +3 -2
  33. package/cjs/transaction/builders/TransactionBuilder.js +22 -7
  34. package/cjs/transaction/interfaces/ITransactionParameters.d.ts +1 -0
  35. package/cjs/verification/TapscriptVerificator.d.ts +17 -0
  36. package/cjs/verification/TapscriptVerificator.js +70 -0
  37. package/package.json +1 -2
  38. package/src/_version.ts +1 -1
  39. package/src/bytecode/Compressor.ts +27 -27
  40. package/src/generators/AddressGenerator.ts +1 -1
  41. package/src/keypair/EcKeyPair.ts +1 -1
  42. package/src/signer/TweakedSigner.ts +1 -1
  43. package/src/transaction/builders/TransactionBuilder.ts +1 -1
  44. package/webpack.config.js +2 -1
  45. package/browser/873e754d6c7c6e9361f1.module.wasm +0 -0
  46. /package/{browser/metadata/ContractMetadataManager.d.ts → cjs/tests/gen.d.ts} +0 -0
  47. /package/{browser/scripts → cjs/tests}/test.d.ts +0 -0
  48. /package/{browser/transaction/TransactionBuilder.d.ts → cjs/tests/transfer.d.ts} +0 -0
@@ -31,7 +31,9 @@ const ecc = __importStar(require("tiny-secp256k1"));
31
31
  const EcKeyPair_js_1 = require("../../keypair/EcKeyPair.js");
32
32
  const logger_1 = require("@btc-vision/logger");
33
33
  class TransactionBuilder extends logger_1.Logger {
34
- static LOCK_LEAF_SCRIPT = bitcoinjs_lib_1.script.compile([bitcoinjs_lib_1.opcodes.OP_0]);
34
+ static LOCK_LEAF_SCRIPT = bitcoinjs_lib_1.script.compile([
35
+ bitcoinjs_lib_1.opcodes.OP_0,
36
+ ]);
35
37
  static MINIMUM_DUST = 330n;
36
38
  logColor = '#785def';
37
39
  transactionFee = 0n;
@@ -118,6 +120,13 @@ class TransactionBuilder extends logger_1.Logger {
118
120
  }
119
121
  return this.scriptData.address;
120
122
  }
123
+ disableRBF() {
124
+ if (this.signed)
125
+ throw new Error('Transaction is already signed');
126
+ for (let input of this.inputs) {
127
+ input.sequence = 0xffffffff;
128
+ }
129
+ }
121
130
  getTapAddress() {
122
131
  if (!this.tapData || !this.tapData.address) {
123
132
  throw new Error('Tap data is required');
@@ -128,6 +137,9 @@ class TransactionBuilder extends logger_1.Logger {
128
137
  this.inputs.push(input);
129
138
  }
130
139
  addOutput(output) {
140
+ if (output.value < TransactionBuilder.MINIMUM_DUST) {
141
+ throw new Error(`Output value is less than the minimum dust ${output.value} < ${TransactionBuilder.MINIMUM_DUST}`);
142
+ }
131
143
  this.outputs.push(output);
132
144
  }
133
145
  addRefundOutput(amountSpent) {
@@ -170,6 +182,7 @@ class TransactionBuilder extends logger_1.Logger {
170
182
  value: Number(utxo.value),
171
183
  script: Buffer.from(utxo.scriptPubKey.hex, 'hex'),
172
184
  },
185
+ sequence: 0xfffffffd,
173
186
  };
174
187
  this.addInput(input);
175
188
  }
@@ -235,10 +248,9 @@ class TransactionBuilder extends logger_1.Logger {
235
248
  return this.inputs;
236
249
  }
237
250
  getOutputs() {
238
- if (!this.feeOutput)
239
- throw new Error('Fee output is required');
240
251
  const outputs = [...this.outputs];
241
- outputs.push(this.feeOutput);
252
+ if (this.feeOutput)
253
+ outputs.push(this.feeOutput);
242
254
  return outputs;
243
255
  }
244
256
  verifyUTXOValidity() {
@@ -249,12 +261,16 @@ class TransactionBuilder extends logger_1.Logger {
249
261
  }
250
262
  }
251
263
  setFeeOutput(output) {
264
+ const initialValue = output.value;
252
265
  this.feeOutput = output;
253
266
  const fee = this.estimateTransactionFees();
254
- if (fee > BigInt(output.value)) {
267
+ if (fee > BigInt(initialValue)) {
255
268
  throw new Error('Insufficient funds');
256
269
  }
257
- this.feeOutput.value = this.feeOutput.value - Number(fee);
270
+ this.feeOutput.value = initialValue - Number(fee);
271
+ if (this.feeOutput.value < TransactionBuilder.MINIMUM_DUST) {
272
+ this.feeOutput = null;
273
+ }
258
274
  }
259
275
  internalPubKeyToXOnly() {
260
276
  return (0, bip371_js_1.toXOnly)(this.signer.publicKey);
@@ -292,7 +308,6 @@ class TransactionBuilder extends logger_1.Logger {
292
308
  const tx = fakeTx.extractTransaction(false);
293
309
  const size = tx.virtualSize();
294
310
  const fee = this.feeRate * size + 1;
295
- this.log(`Transaction fee estimated to: ${fee} - ${fakeTx.getFeeRate()}`);
296
311
  return BigInt(Math.ceil(fee));
297
312
  }
298
313
  else {
@@ -19,6 +19,7 @@ export interface IInteractionParameters extends ITransactionParameters {
19
19
  readonly calldata: Buffer;
20
20
  readonly pubKeys?: Buffer[];
21
21
  readonly minimumSignatures?: number;
22
+ readonly randomBytes?: Buffer;
22
23
  }
23
24
  export interface ITransactionDataContractInteractionWrap extends IInteractionParameters {
24
25
  readonly amount: bigint;
@@ -0,0 +1,17 @@
1
+ /// <reference types="node" />
2
+ import { Network } from 'bitcoinjs-lib';
3
+ import { Taptree } from 'bitcoinjs-lib/src/types.js';
4
+ export interface ContractAddressVerificationParams {
5
+ readonly deployerPubKeyXOnly: Buffer;
6
+ readonly contractSaltPubKey: Buffer;
7
+ readonly originalSalt: Buffer;
8
+ readonly bytecode: Buffer;
9
+ readonly network?: Network;
10
+ }
11
+ export declare class TapscriptVerificator {
12
+ private static readonly TAP_SCRIPT_VERSION;
13
+ static getContractAddress(params: ContractAddressVerificationParams): string | undefined;
14
+ static getContractSeed(deployerPubKey: Buffer, bytecode: Buffer, saltHash: Buffer): Buffer;
15
+ static generateContractVirtualAddress(deployerPubKey: Buffer, bytecode: Buffer, saltHash: Buffer, network?: Network): string;
16
+ static generateAddressFromScript(params: ContractAddressVerificationParams, scriptTree: Taptree): string | undefined;
17
+ }
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.TapscriptVerificator = void 0;
27
+ const bitcoinjs_lib_1 = __importStar(require("bitcoinjs-lib"));
28
+ const bip371_js_1 = require("bitcoinjs-lib/src/psbt/bip371.js");
29
+ const DeploymentGenerator_js_1 = require("../generators/builders/DeploymentGenerator.js");
30
+ const TransactionBuilder_js_1 = require("../transaction/builders/TransactionBuilder.js");
31
+ const AddressGenerator_js_1 = require("../generators/AddressGenerator.js");
32
+ class TapscriptVerificator {
33
+ static TAP_SCRIPT_VERSION = 192;
34
+ static getContractAddress(params) {
35
+ const network = params.network || bitcoinjs_lib_1.networks.bitcoin;
36
+ const scriptBuilder = new DeploymentGenerator_js_1.DeploymentGenerator(params.deployerPubKeyXOnly, (0, bip371_js_1.toXOnly)(params.contractSaltPubKey), network);
37
+ const compiledTargetScript = scriptBuilder.compile(params.bytecode, params.originalSalt);
38
+ const scriptTree = [
39
+ {
40
+ output: compiledTargetScript,
41
+ version: TapscriptVerificator.TAP_SCRIPT_VERSION,
42
+ },
43
+ {
44
+ output: TransactionBuilder_js_1.TransactionBuilder.LOCK_LEAF_SCRIPT,
45
+ version: TapscriptVerificator.TAP_SCRIPT_VERSION,
46
+ },
47
+ ];
48
+ return TapscriptVerificator.generateAddressFromScript(params, scriptTree);
49
+ }
50
+ static getContractSeed(deployerPubKey, bytecode, saltHash) {
51
+ const sha256OfBytecode = bitcoinjs_lib_1.default.crypto.hash256(bytecode);
52
+ const buf = Buffer.concat([deployerPubKey, saltHash, sha256OfBytecode]);
53
+ return bitcoinjs_lib_1.default.crypto.hash256(buf);
54
+ }
55
+ static generateContractVirtualAddress(deployerPubKey, bytecode, saltHash, network = bitcoinjs_lib_1.networks.bitcoin) {
56
+ const virtualAddress = TapscriptVerificator.getContractSeed(deployerPubKey, bytecode, saltHash);
57
+ return AddressGenerator_js_1.AddressGenerator.generatePKSH(virtualAddress, network);
58
+ }
59
+ static generateAddressFromScript(params, scriptTree) {
60
+ const network = params.network || bitcoinjs_lib_1.networks.bitcoin;
61
+ const transactionData = {
62
+ internalPubkey: params.deployerPubKeyXOnly,
63
+ network: network,
64
+ scriptTree: scriptTree,
65
+ };
66
+ const tx = bitcoinjs_lib_1.payments.p2tr(transactionData);
67
+ return tx.address;
68
+ }
69
+ }
70
+ exports.TapscriptVerificator = TapscriptVerificator;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "type": "module",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
4
4
  "author": "BlobMaster41",
5
5
  "description": "OPNet transaction library allows you to create and sign transactions for the OPNet network.",
6
6
  "engines": {
@@ -110,7 +110,6 @@
110
110
  "gulp-logger": "^0.0.2",
111
111
  "gulp-typescript": "^6.0.0-alpha.1",
112
112
  "gulplog": "^2.2.0",
113
- "tiny-secp256k1": "^2.2.3",
114
113
  "ts-loader": "^9.5.1",
115
114
  "ts-node": "^10.9.2"
116
115
  }
package/src/_version.ts CHANGED
@@ -1 +1 @@
1
- export const version = '1.0.5';
1
+ export const version = '1.0.7';
@@ -1,27 +1,27 @@
1
- import zlib, { ZlibOptions } from 'zlib';
2
-
3
- /** Compressor class for compressing and decompressing data. */
4
- export class Compressor {
5
- private static readonly zlibOptions: ZlibOptions = {
6
- level: 9,
7
- maxOutputLength: 1024 * 1024 * 16, // 16mb, limit it to 16mb.
8
- };
9
-
10
- /**
11
- * Compresses the data using gzip.
12
- * @param {Uint8Array | Buffer} data The data to compress.
13
- * @returns {Buffer} The compressed data.
14
- */
15
- public static compress(data: Uint8Array | Buffer): Buffer {
16
- return zlib.gzipSync(data, Compressor.zlibOptions);
17
- }
18
-
19
- /**
20
- * Decompresses the data using gunzip.
21
- * @param {Uint8Array | Buffer} data The data to decompress.
22
- * @returns {Buffer} The decompressed data.
23
- */
24
- public static decompress(data: Uint8Array | Buffer): Buffer {
25
- return zlib.gunzipSync(data, Compressor.zlibOptions);
26
- }
27
- }
1
+ import zlib, { ZlibOptions } from 'zlib';
2
+
3
+ /** Compressor class for compressing and decompressing data. */
4
+ export class Compressor {
5
+ private static readonly zlibOptions: ZlibOptions = {
6
+ level: 9,
7
+ maxOutputLength: 1024 * 1024 * 16, // 16mb, limit it to 16mb.
8
+ };
9
+
10
+ /**
11
+ * Compresses the data using gzip.
12
+ * @param {Uint8Array | Buffer} data The data to compress.
13
+ * @returns {Buffer} The compressed data.
14
+ */
15
+ public static compress(data: Uint8Array | Buffer): Buffer {
16
+ return zlib.gzipSync(data, Compressor.zlibOptions);
17
+ }
18
+
19
+ /**
20
+ * Decompresses the data using gunzip.
21
+ * @param {Uint8Array | Buffer} data The data to decompress.
22
+ * @returns {Buffer} The decompressed data.
23
+ */
24
+ public static decompress(data: Uint8Array | Buffer): Buffer {
25
+ return zlib.gunzipSync(data, Compressor.zlibOptions);
26
+ }
27
+ }
@@ -1,7 +1,7 @@
1
1
  import { createHash } from 'crypto';
2
2
  import { bech32 } from 'bech32';
3
3
  import { initEccLib, Network } from 'bitcoinjs-lib';
4
- import * as ecc from 'tiny-secp256k1';
4
+ import * as ecc from '@bitcoinerlab/secp256k1';
5
5
 
6
6
  initEccLib(ecc);
7
7
 
@@ -1,7 +1,7 @@
1
1
  import bip32, { BIP32Interface } from 'bip32';
2
2
  import { address, initEccLib, Network, networks, payments } from 'bitcoinjs-lib';
3
3
  import { ECPairFactory, ECPairInterface } from 'ecpair';
4
- import * as ecc from 'tiny-secp256k1';
4
+ import * as ecc from '@bitcoinerlab/secp256k1';
5
5
  import { Address } from '@btc-vision/bsi-binary';
6
6
  import { IWallet } from './interfaces/IWallet.js';
7
7
 
@@ -1,7 +1,7 @@
1
1
  import { initEccLib, Network, Signer } from 'bitcoinjs-lib';
2
2
  import { tapTweakHash } from 'bitcoinjs-lib/src/payments/bip341.js';
3
3
  import { toXOnly } from 'bitcoinjs-lib/src/psbt/bip371.js';
4
- import * as ecc from 'tiny-secp256k1';
4
+ import * as ecc from '@bitcoinerlab/secp256k1';
5
5
  import { EcKeyPair } from '../keypair/EcKeyPair.js';
6
6
 
7
7
  initEccLib(ecc);
@@ -1,7 +1,7 @@
1
1
  import { initEccLib, Network, opcodes, Payment, payments, Psbt, script, Signer, Transaction } from 'bitcoinjs-lib';
2
2
  import { varuint } from 'bitcoinjs-lib/src/bufferutils.js';
3
3
  import { toXOnly } from 'bitcoinjs-lib/src/psbt/bip371.js';
4
- import * as ecc from 'tiny-secp256k1';
4
+ import * as ecc from '@bitcoinerlab/secp256k1';
5
5
  import { PsbtInputExtended, PsbtOutputExtended, UpdateInput } from '../interfaces/Tap.js';
6
6
  import { TransactionType } from '../enums/TransactionType.js';
7
7
  import { IFundingTransactionParameters, ITransactionParameters } from '../interfaces/ITransactionParameters.js';
package/webpack.config.js CHANGED
@@ -18,7 +18,7 @@ export default {
18
18
  },
19
19
  experiments: {
20
20
  outputModule: true,
21
- asyncWebAssembly: true,
21
+ asyncWebAssembly: false,
22
22
  syncWebAssembly: true,
23
23
  },
24
24
  resolve: {
@@ -72,6 +72,7 @@ export default {
72
72
  process: 'process/browser',
73
73
  stream: 'stream-browserify',
74
74
  zlib: 'browserify-zlib',
75
+ bitcoin: 'bitcoinjs-lib',
75
76
  }),
76
77
  ],
77
78
  };
File without changes