@btc-vision/transaction 1.2.7 → 1.2.9

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.
@@ -1,4 +1,3 @@
1
- import { IWallet } from './interfaces/IWallet.js';
2
1
  import { ECPairInterface } from 'ecpair';
3
2
  import { Network } from '@btc-vision/bitcoin';
4
3
  import { Address } from './Address.js';
@@ -12,7 +11,7 @@ export declare class Wallet {
12
11
  private readonly _bufferPubKey;
13
12
  private readonly _tweakedKey;
14
13
  private readonly _address;
15
- constructor(wallet: IWallet, network?: Network);
14
+ constructor(privateKeyOrWif: string, network?: Network);
16
15
  get address(): Address;
17
16
  get tweakedPubKeyKey(): Buffer;
18
17
  get keypair(): ECPairInterface;
@@ -24,4 +23,5 @@ export declare class Wallet {
24
23
  get publicKey(): Buffer;
25
24
  get xOnly(): Buffer;
26
25
  static fromWif(wif: string, network?: Network): Wallet;
26
+ static new(network?: Network): Wallet;
27
27
  }
@@ -2,5 +2,6 @@ export declare class BitcoinUtils {
2
2
  static btcToSatoshi(btc: number): bigint;
3
3
  static rndBytes(): Buffer;
4
4
  static getSafeRandomValues(length: number): Buffer;
5
+ static isValidHex(hex: string): boolean;
5
6
  static opnetHash(data: Buffer): string;
6
7
  }
@@ -1 +1 @@
1
- export declare const version = "1.2.7";
1
+ export declare const version = "1.2.9";
package/build/_version.js CHANGED
@@ -1 +1 @@
1
- export const version = '1.2.7';
1
+ export const version = '1.2.9';
@@ -5,7 +5,7 @@ export declare class Map<K, V> {
5
5
  get size(): i32;
6
6
  keys(): K[];
7
7
  values(): V[];
8
- entires(): [K, V][];
8
+ entries(): [K, V][];
9
9
  set(key: K, value: V): void;
10
10
  indexOf(key: K): i32;
11
11
  get(key: K): V | undefined;
@@ -12,7 +12,7 @@ export class Map {
12
12
  values() {
13
13
  return this._values;
14
14
  }
15
- entires() {
15
+ entries() {
16
16
  const result = [];
17
17
  for (let i = 0; i < this._keys.length; i++) {
18
18
  result.push([this._keys[i], this._values[i]]);
@@ -15,10 +15,7 @@ import { ADDRESS_BYTE_LENGTH } from '../utils/lengths.js';
15
15
  import { AddressVerificator } from './AddressVerificator.js';
16
16
  import { EcKeyPair } from './EcKeyPair.js';
17
17
  import { ContractAddress } from '../transaction/ContractAddress.js';
18
- const hexPattern = /^[0-9a-fA-F]+$/;
19
- const isHexadecimal = (input) => {
20
- return hexPattern.test(input);
21
- };
18
+ import { BitcoinUtils } from '../utils/BitcoinUtils.js';
22
19
  export class Address extends Uint8Array {
23
20
  constructor(bytes) {
24
21
  super(ADDRESS_BYTE_LENGTH);
@@ -52,7 +49,7 @@ export class Address extends Uint8Array {
52
49
  if (pubKey.startsWith('0x')) {
53
50
  pubKey = pubKey.slice(2);
54
51
  }
55
- if (!isHexadecimal(pubKey)) {
52
+ if (!BitcoinUtils.isValidHex(pubKey)) {
56
53
  throw new Error('You must only pass public keys in hexadecimal format. If you have an address such as bc1q... you must convert it to a public key first. Please refer to await provider.getPublicKeyInfo("bc1q..."). If the public key associated with the address is not found, you must force the user to enter the destination public key. It looks like: 0x020373626d317ae8788ce3280b491068610d840c23ecb64c14075bbb9f670af52c.');
57
54
  }
58
55
  return new Address(Buffer.from(pubKey, 'hex'));
@@ -1,6 +1,7 @@
1
1
  import { address, initEccLib } from '@btc-vision/bitcoin';
2
2
  import * as ecc from '@bitcoinerlab/secp256k1';
3
3
  import { EcKeyPair } from './EcKeyPair.js';
4
+ import { BitcoinUtils } from '../utils/BitcoinUtils.js';
4
5
  initEccLib(ecc);
5
6
  export var AddressTypes;
6
7
  (function (AddressTypes) {
@@ -53,8 +54,7 @@ export class AddressVerificator {
53
54
  if (input.startsWith('0x')) {
54
55
  input = input.slice(2);
55
56
  }
56
- const hexRegex = /^[0-9a-fA-F]+$/;
57
- if (!hexRegex.test(input)) {
57
+ if (!BitcoinUtils.isValidHex(input)) {
58
58
  return false;
59
59
  }
60
60
  if (input.length === 64) {
@@ -1,4 +1,3 @@
1
- import { IWallet } from './interfaces/IWallet.js';
2
1
  import { ECPairInterface } from 'ecpair';
3
2
  import { Network } from '@btc-vision/bitcoin';
4
3
  import { Address } from './Address.js';
@@ -12,7 +11,7 @@ export declare class Wallet {
12
11
  private readonly _bufferPubKey;
13
12
  private readonly _tweakedKey;
14
13
  private readonly _address;
15
- constructor(wallet: IWallet, network?: Network);
14
+ constructor(privateKeyOrWif: string, network?: Network);
16
15
  get address(): Address;
17
16
  get tweakedPubKeyKey(): Buffer;
18
17
  get keypair(): ECPairInterface;
@@ -24,4 +23,5 @@ export declare class Wallet {
24
23
  get publicKey(): Buffer;
25
24
  get xOnly(): Buffer;
26
25
  static fromWif(wif: string, network?: Network): Wallet;
26
+ static new(network?: Network): Wallet;
27
27
  }
@@ -1,17 +1,26 @@
1
1
  import { EcKeyPair } from './EcKeyPair.js';
2
2
  import { networks, toXOnly } from '@btc-vision/bitcoin';
3
3
  import { Address } from './Address.js';
4
+ import { BitcoinUtils } from '../utils/BitcoinUtils.js';
4
5
  export class Wallet {
5
- constructor(wallet, network = networks.bitcoin) {
6
+ constructor(privateKeyOrWif, network = networks.bitcoin) {
6
7
  this.network = network;
7
- this._keypair = EcKeyPair.fromWIF(wallet.privateKey, this.network);
8
- this._p2wpkh = EcKeyPair.getP2WPKHAddress(this._keypair, this.network);
9
- this._p2tr = EcKeyPair.getTaprootAddress(this._keypair, this.network);
10
- this._legacy = EcKeyPair.getLegacyAddress(this._keypair, this.network);
11
- this._segwitLegacy = EcKeyPair.getLegacySegwitAddress(this._keypair, this.network);
12
- this._tweakedKey = EcKeyPair.tweakPublicKey(this._keypair.publicKey.toString('hex'));
8
+ const parsedPrivateKey = privateKeyOrWif.startsWith('0x')
9
+ ? privateKeyOrWif.replace('0x', '')
10
+ : privateKeyOrWif;
11
+ if (BitcoinUtils.isValidHex(parsedPrivateKey)) {
12
+ this._keypair = EcKeyPair.fromPrivateKey(Buffer.from(parsedPrivateKey, 'hex'), this.network);
13
+ }
14
+ else {
15
+ this._keypair = EcKeyPair.fromWIF(parsedPrivateKey, this.network);
16
+ }
13
17
  this._bufferPubKey = this._keypair.publicKey;
14
18
  this._address = new Address(this._keypair.publicKey);
19
+ this._p2tr = this._address.p2tr(this.network);
20
+ this._p2wpkh = this._address.p2wpkh(this.network);
21
+ this._legacy = this._address.p2pkh(this.network);
22
+ this._segwitLegacy = this._address.p2wpkh(this.network);
23
+ this._tweakedKey = this._address.toBuffer();
15
24
  }
16
25
  get address() {
17
26
  return this._address;
@@ -50,6 +59,9 @@ export class Wallet {
50
59
  return toXOnly(this._bufferPubKey);
51
60
  }
52
61
  static fromWif(wif, network = networks.bitcoin) {
53
- return new Wallet({ privateKey: wif, address: '', publicKey: '' }, network);
62
+ return new Wallet(wif, network);
63
+ }
64
+ static new(network = networks.bitcoin) {
65
+ return new Wallet(EcKeyPair.generateWallet(network).privateKey, network);
54
66
  }
55
67
  }
@@ -48,6 +48,9 @@ export class SharedInteractionTransaction extends TransactionBuilder {
48
48
  generateSecret() {
49
49
  if (!this.to)
50
50
  throw new Error('To address is required');
51
+ if (this.to.startsWith('0x')) {
52
+ throw new Error(`Legacy not support at this time. Reserved for future use.`);
53
+ }
51
54
  return address.fromBech32(this.to).data;
52
55
  }
53
56
  scriptSignerXOnlyPubKey() {
@@ -195,11 +198,11 @@ export class SharedInteractionTransaction extends TransactionBuilder {
195
198
  }
196
199
  }
197
200
  getPubKeys() {
198
- const pubkeys = [Buffer.from(this.signer.publicKey)];
201
+ const pubKeys = [Buffer.from(this.signer.publicKey)];
199
202
  if (this.scriptSigner) {
200
- pubkeys.push(Buffer.from(this.scriptSigner.publicKey));
203
+ pubKeys.push(Buffer.from(this.scriptSigner.publicKey));
201
204
  }
202
- return pubkeys;
205
+ return pubKeys;
203
206
  }
204
207
  generateRedeemScripts() {
205
208
  this.targetScriptRedeem = {
@@ -2,5 +2,6 @@ export declare class BitcoinUtils {
2
2
  static btcToSatoshi(btc: number): bigint;
3
3
  static rndBytes(): Buffer;
4
4
  static getSafeRandomValues(length: number): Buffer;
5
+ static isValidHex(hex: string): boolean;
5
6
  static opnetHash(data: Buffer): string;
6
7
  }
@@ -1,4 +1,5 @@
1
1
  import { createHash } from 'crypto';
2
+ const hexPattern = /^[0-9a-fA-F]+$/;
2
3
  export class BitcoinUtils {
3
4
  static btcToSatoshi(btc) {
4
5
  return BigInt(btc * 100000000);
@@ -25,6 +26,9 @@ export class BitcoinUtils {
25
26
  throw new Error('No secure random number generator available. Please upgrade your environment.');
26
27
  }
27
28
  }
29
+ static isValidHex(hex) {
30
+ return hexPattern.test(hex);
31
+ }
28
32
  static opnetHash(data) {
29
33
  const hashed = createHash('sha512');
30
34
  hashed.update(data);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@btc-vision/transaction",
3
3
  "type": "module",
4
- "version": "1.2.7",
4
+ "version": "1.2.9",
5
5
  "author": "BlobMaster41",
6
6
  "description": "OPNet transaction library allows you to create and sign transactions for the OPNet network.",
7
7
  "engines": {
@@ -89,7 +89,7 @@
89
89
  "dependencies": {
90
90
  "@babel/plugin-proposal-object-rest-spread": "^7.20.7",
91
91
  "@bitcoinerlab/secp256k1": "^1.1.1",
92
- "@btc-vision/bitcoin": "^6.3.3",
92
+ "@btc-vision/bitcoin": "^6.3.5",
93
93
  "@btc-vision/bitcoin-rpc": "^1.0.0",
94
94
  "@btc-vision/logger": "^1.0.6",
95
95
  "@eslint/js": "^9.14.0",
package/src/_version.ts CHANGED
@@ -1 +1 @@
1
- export const version = '1.2.7';
1
+ export const version = '1.2.9';
@@ -15,8 +15,8 @@ export class Map<K, V> {
15
15
  public values(): V[] {
16
16
  return this._values;
17
17
  }
18
-
19
- public entires(): [K, V][] {
18
+
19
+ public entries(): [K, V][] {
20
20
  const result: [K, V][] = [];
21
21
  for (let i: i32 = 0; i < this._keys.length; i++) {
22
22
  result.push([this._keys[i], this._values[i]]);
@@ -4,11 +4,7 @@ import { ADDRESS_BYTE_LENGTH } from '../utils/lengths.js';
4
4
  import { AddressVerificator } from './AddressVerificator.js';
5
5
  import { EcKeyPair } from './EcKeyPair.js';
6
6
  import { ContractAddress } from '../transaction/ContractAddress.js';
7
-
8
- const hexPattern = /^[0-9a-fA-F]+$/;
9
- const isHexadecimal = (input: string): boolean => {
10
- return hexPattern.test(input);
11
- };
7
+ import { BitcoinUtils } from '../utils/BitcoinUtils.js';
12
8
 
13
9
  /**
14
10
  * Objects of type "Address" are the representation of tweaked public keys. They can be converted to different address formats.
@@ -72,7 +68,7 @@ export class Address extends Uint8Array {
72
68
  pubKey = pubKey.slice(2);
73
69
  }
74
70
 
75
- if (!isHexadecimal(pubKey)) {
71
+ if (!BitcoinUtils.isValidHex(pubKey)) {
76
72
  throw new Error(
77
73
  'You must only pass public keys in hexadecimal format. If you have an address such as bc1q... you must convert it to a public key first. Please refer to await provider.getPublicKeyInfo("bc1q..."). If the public key associated with the address is not found, you must force the user to enter the destination public key. It looks like: 0x020373626d317ae8788ce3280b491068610d840c23ecb64c14075bbb9f670af52c.',
78
74
  );
@@ -1,6 +1,7 @@
1
1
  import { address, initEccLib, Network } from '@btc-vision/bitcoin';
2
2
  import * as ecc from '@bitcoinerlab/secp256k1';
3
3
  import { EcKeyPair } from './EcKeyPair.js';
4
+ import { BitcoinUtils } from '../utils/BitcoinUtils.js';
4
5
 
5
6
  initEccLib(ecc);
6
7
 
@@ -100,8 +101,7 @@ export class AddressVerificator {
100
101
 
101
102
  // Compressed public keys are 66 characters long (0x02 or 0x03 prefix + 32 bytes)
102
103
  // Uncompressed public keys are 130 characters long (0x04 prefix + 64 bytes)
103
- const hexRegex = /^[0-9a-fA-F]+$/;
104
- if (!hexRegex.test(input)) {
104
+ if (!BitcoinUtils.isValidHex(input)) {
105
105
  return false;
106
106
  }
107
107
 
@@ -1,8 +1,8 @@
1
- import { IWallet } from './interfaces/IWallet.js';
2
1
  import { ECPairInterface } from 'ecpair';
3
2
  import { EcKeyPair } from './EcKeyPair.js';
4
3
  import { Network, networks, toXOnly } from '@btc-vision/bitcoin';
5
4
  import { Address } from './Address.js';
5
+ import { BitcoinUtils } from '../utils/BitcoinUtils.js';
6
6
 
7
7
  /**
8
8
  * Wallet class
@@ -57,20 +57,31 @@ export class Wallet {
57
57
  private readonly _address: Address;
58
58
 
59
59
  constructor(
60
- wallet: IWallet,
60
+ privateKeyOrWif: string,
61
61
  public readonly network: Network = networks.bitcoin,
62
62
  ) {
63
- this._keypair = EcKeyPair.fromWIF(wallet.privateKey, this.network);
64
-
65
- this._p2wpkh = EcKeyPair.getP2WPKHAddress(this._keypair, this.network);
66
- this._p2tr = EcKeyPair.getTaprootAddress(this._keypair, this.network);
67
- this._legacy = EcKeyPair.getLegacyAddress(this._keypair, this.network);
68
- this._segwitLegacy = EcKeyPair.getLegacySegwitAddress(this._keypair, this.network);
69
-
70
- this._tweakedKey = EcKeyPair.tweakPublicKey(this._keypair.publicKey.toString('hex'));
63
+ const parsedPrivateKey = privateKeyOrWif.startsWith('0x')
64
+ ? privateKeyOrWif.replace('0x', '')
65
+ : privateKeyOrWif;
66
+
67
+ if (BitcoinUtils.isValidHex(parsedPrivateKey)) {
68
+ this._keypair = EcKeyPair.fromPrivateKey(
69
+ Buffer.from(parsedPrivateKey, 'hex'),
70
+ this.network,
71
+ );
72
+ } else {
73
+ this._keypair = EcKeyPair.fromWIF(parsedPrivateKey, this.network);
74
+ }
71
75
 
72
76
  this._bufferPubKey = this._keypair.publicKey;
73
77
  this._address = new Address(this._keypair.publicKey);
78
+
79
+ this._p2tr = this._address.p2tr(this.network);
80
+ this._p2wpkh = this._address.p2wpkh(this.network);
81
+ this._legacy = this._address.p2pkh(this.network);
82
+ this._segwitLegacy = this._address.p2wpkh(this.network);
83
+
84
+ this._tweakedKey = this._address.toBuffer();
74
85
  }
75
86
 
76
87
  /**
@@ -168,6 +179,14 @@ export class Wallet {
168
179
  * @returns {Wallet} The wallet
169
180
  */
170
181
  public static fromWif(wif: string, network: Network = networks.bitcoin): Wallet {
171
- return new Wallet({ privateKey: wif, address: '', publicKey: '' }, network);
182
+ return new Wallet(wif, network);
183
+ }
184
+
185
+ /**
186
+ * Create a new fresh wallet
187
+ * @param {Network} network The network
188
+ */
189
+ public static new(network: Network = networks.bitcoin): Wallet {
190
+ return new Wallet(EcKeyPair.generateWallet(network).privateKey, network);
172
191
  }
173
192
  }
@@ -119,6 +119,10 @@ export abstract class SharedInteractionTransaction<
119
119
  protected generateSecret(): Buffer {
120
120
  if (!this.to) throw new Error('To address is required');
121
121
 
122
+ if (this.to.startsWith('0x')) {
123
+ throw new Error(`Legacy not support at this time. Reserved for future use.`);
124
+ }
125
+
122
126
  return address.fromBech32(this.to).data;
123
127
  }
124
128
 
@@ -384,13 +388,13 @@ export abstract class SharedInteractionTransaction<
384
388
  * @returns {Buffer[]} The public keys
385
389
  */
386
390
  private getPubKeys(): Buffer[] {
387
- const pubkeys = [Buffer.from(this.signer.publicKey)];
391
+ const pubKeys = [Buffer.from(this.signer.publicKey)];
388
392
 
389
393
  if (this.scriptSigner) {
390
- pubkeys.push(Buffer.from(this.scriptSigner.publicKey));
394
+ pubKeys.push(Buffer.from(this.scriptSigner.publicKey));
391
395
  }
392
396
 
393
- return pubkeys;
397
+ return pubKeys;
394
398
  }
395
399
 
396
400
  /**
@@ -1,5 +1,7 @@
1
1
  import { createHash } from 'crypto';
2
2
 
3
+ const hexPattern = /^[0-9a-fA-F]+$/;
4
+
3
5
  /**
4
6
  * Utility class for Bitcoin related functions
5
7
  */
@@ -51,6 +53,10 @@ export class BitcoinUtils {
51
53
  }
52
54
  }
53
55
 
56
+ public static isValidHex(hex: string): boolean {
57
+ return hexPattern.test(hex);
58
+ }
59
+
54
60
  /**
55
61
  * Hashes the given data
56
62
  * @param {Buffer} data - The data to hash
@@ -64,37 +70,4 @@ export class BitcoinUtils {
64
70
 
65
71
  return `0x${Buffer.from(hash).toString('hex')}`;
66
72
  }
67
-
68
- /**
69
- * Deterministically order vaults by address
70
- * @param {VaultUTXOs[]} vaults - The vaults to order
71
- * @returns {VaultUTXOs[]} The ordered vaults
72
- */
73
- /*public static orderVaultsByAddress(vaults: VaultUTXOs[]): VaultUTXOs[] {
74
- return vaults.sort((a, b) => {
75
- return a.vault.localeCompare(b.vault);
76
- });
77
- }*/
78
-
79
- /**
80
- * Find the vault with the most public keys in a deterministic way.
81
- * @param {VaultUTXOs[]} vaults - The vaults to search
82
- * @returns {VaultUTXOs} The vault with the most public keys
83
- */
84
- /*public static findVaultWithMostPublicKeys(vaults: VaultUTXOs[]): VaultUTXOs {
85
- vaults = BitcoinUtils.orderVaultsByAddress(vaults);
86
-
87
- let mostPublicKeys: number = 0;
88
- let vault: VaultUTXOs | undefined;
89
- for (const v of vaults) {
90
- if (v.publicKeys.length > mostPublicKeys) {
91
- mostPublicKeys = v.publicKeys.length;
92
- vault = v;
93
- }
94
- }
95
-
96
- if (!vault) throw new Error('No vault with public keys found.');
97
-
98
- return vault;
99
- }*/
100
73
  }