@btc-vision/transaction 1.1.8 → 1.1.10

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.
@@ -0,0 +1,11 @@
1
+ import { ECPairInterface } from 'ecpair';
2
+ import { Network } from '@btc-vision/bitcoin';
3
+ declare class MessageSignerBase {
4
+ sha256(message: Buffer | Uint8Array): Buffer;
5
+ tweakAndSignMessage(keypair: ECPairInterface, message: Uint8Array | Buffer | string, network: Network): Uint8Array;
6
+ signMessage(keypair: ECPairInterface, message: Uint8Array | Buffer | string): Uint8Array;
7
+ verifySignature(publicKey: Uint8Array | Buffer, message: Uint8Array | Buffer | string, signature: Uint8Array | Buffer): boolean;
8
+ tweakAndVerifySignature(publicKey: Uint8Array | Buffer, message: Uint8Array | Buffer | string, signature: Uint8Array | Buffer): boolean;
9
+ }
10
+ export declare const MessageSigner: MessageSignerBase;
11
+ export {};
@@ -13,7 +13,9 @@ export * from './keypair/EcKeyPair.js';
13
13
  export * from './keypair/Wallet.js';
14
14
  export * from './keypair/interfaces/IWallet.js';
15
15
  export * from './keypair/AddressVerificator.js';
16
+ export * from './keypair/MessageSigner.js';
16
17
  export * from './metadata/ContractBaseMetadata.js';
18
+ export * from './network/ChainId.js';
17
19
  export * from './signer/TweakedSigner.js';
18
20
  export * from './transaction/TransactionFactory.js';
19
21
  export * from './transaction/interfaces/ITransactionParameters.js';
@@ -26,7 +28,6 @@ export * from './transaction/builders/SharedInteractionTransaction.js';
26
28
  export * from './transaction/builders/DeploymentTransaction.js';
27
29
  export * from './transaction/builders/CustomScriptTransaction.js';
28
30
  export * from './transaction/builders/MultiSignTransaction.js';
29
- export * from './network/ChainId.js';
30
31
  export * from './utils/BitcoinUtils.js';
31
32
  export * from './utxo/interfaces/IUTXO.js';
32
33
  export * from './utxo/OPNetLimitedProvider.js';
@@ -1 +1 @@
1
- export declare const version = "1.1.8";
1
+ export declare const version = "1.1.10";
package/build/_version.js CHANGED
@@ -1 +1 @@
1
- export const version = '1.1.8';
1
+ export const version = '1.1.10';
@@ -131,9 +131,6 @@ export class Address extends Uint8Array {
131
131
  return EcKeyPair.getLegacySegwitAddress(this.keyPair, network);
132
132
  }
133
133
  toString() {
134
- if (__classPrivateFieldGet(this, _Address_p2tr, "f")) {
135
- return __classPrivateFieldGet(this, _Address_p2tr, "f");
136
- }
137
134
  return this.toHex();
138
135
  }
139
136
  p2tr(network) {
@@ -1,10 +1,10 @@
1
1
  import * as ecc from '@bitcoinerlab/secp256k1';
2
2
  import bip32, { BIP32Factory } from 'bip32';
3
3
  import { address, initEccLib, networks, payments } from '@btc-vision/bitcoin';
4
- import { toXOnly } from '@btc-vision/bitcoin/src/psbt/bip371.js';
5
4
  import { ECPairFactory } from 'ecpair';
6
5
  import { CURVE, ProjectivePoint as Point } from '@noble/secp256k1';
7
6
  import { taggedHash } from '@btc-vision/bitcoin/src/crypto.js';
7
+ import { toXOnly } from '@btc-vision/bitcoin/src/psbt/bip371.js';
8
8
  initEccLib(ecc);
9
9
  const BIP32factory = typeof bip32 === 'function' ? bip32 : BIP32Factory;
10
10
  if (!BIP32factory) {
@@ -0,0 +1,11 @@
1
+ import { ECPairInterface } from 'ecpair';
2
+ import { Network } from '@btc-vision/bitcoin';
3
+ declare class MessageSignerBase {
4
+ sha256(message: Buffer | Uint8Array): Buffer;
5
+ tweakAndSignMessage(keypair: ECPairInterface, message: Uint8Array | Buffer | string, network: Network): Uint8Array;
6
+ signMessage(keypair: ECPairInterface, message: Uint8Array | Buffer | string): Uint8Array;
7
+ verifySignature(publicKey: Uint8Array | Buffer, message: Uint8Array | Buffer | string, signature: Uint8Array | Buffer): boolean;
8
+ tweakAndVerifySignature(publicKey: Uint8Array | Buffer, message: Uint8Array | Buffer | string, signature: Uint8Array | Buffer): boolean;
9
+ }
10
+ export declare const MessageSigner: MessageSignerBase;
11
+ export {};
@@ -0,0 +1,41 @@
1
+ import * as ecc from '@bitcoinerlab/secp256k1';
2
+ import { crypto } from '@btc-vision/bitcoin';
3
+ import { TweakedSigner } from '../signer/TweakedSigner.js';
4
+ import { EcKeyPair } from './EcKeyPair.js';
5
+ import { toXOnly } from '@btc-vision/bitcoin/src/psbt/bip371.js';
6
+ class MessageSignerBase {
7
+ sha256(message) {
8
+ return crypto.sha256(Buffer.from(message));
9
+ }
10
+ tweakAndSignMessage(keypair, message, network) {
11
+ const tweaked = TweakedSigner.tweakSigner(keypair, {
12
+ network,
13
+ });
14
+ return this.signMessage(tweaked, message);
15
+ }
16
+ signMessage(keypair, message) {
17
+ if (typeof message === 'string') {
18
+ message = Buffer.from(message, 'utf-8');
19
+ }
20
+ if (!keypair.privateKey) {
21
+ throw new Error('Private key not found in keypair.');
22
+ }
23
+ const hashedMessage = this.sha256(message);
24
+ return ecc.signSchnorr(hashedMessage, keypair.privateKey);
25
+ }
26
+ verifySignature(publicKey, message, signature) {
27
+ if (typeof message === 'string') {
28
+ message = Buffer.from(message, 'utf-8');
29
+ }
30
+ if (signature.length !== 64) {
31
+ throw new Error('Invalid signature length.');
32
+ }
33
+ const hashedMessage = this.sha256(message);
34
+ return ecc.verifySchnorr(hashedMessage, toXOnly(Buffer.from(publicKey)), signature);
35
+ }
36
+ tweakAndVerifySignature(publicKey, message, signature) {
37
+ const tweakedPublicKey = EcKeyPair.tweakPublicKey(Buffer.from(publicKey));
38
+ return this.verifySignature(tweakedPublicKey, message, signature);
39
+ }
40
+ }
41
+ export const MessageSigner = new MessageSignerBase();
package/build/opnet.d.ts CHANGED
@@ -13,7 +13,9 @@ export * from './keypair/EcKeyPair.js';
13
13
  export * from './keypair/Wallet.js';
14
14
  export * from './keypair/interfaces/IWallet.js';
15
15
  export * from './keypair/AddressVerificator.js';
16
+ export * from './keypair/MessageSigner.js';
16
17
  export * from './metadata/ContractBaseMetadata.js';
18
+ export * from './network/ChainId.js';
17
19
  export * from './signer/TweakedSigner.js';
18
20
  export * from './transaction/TransactionFactory.js';
19
21
  export * from './transaction/interfaces/ITransactionParameters.js';
@@ -26,7 +28,6 @@ export * from './transaction/builders/SharedInteractionTransaction.js';
26
28
  export * from './transaction/builders/DeploymentTransaction.js';
27
29
  export * from './transaction/builders/CustomScriptTransaction.js';
28
30
  export * from './transaction/builders/MultiSignTransaction.js';
29
- export * from './network/ChainId.js';
30
31
  export * from './utils/BitcoinUtils.js';
31
32
  export * from './utxo/interfaces/IUTXO.js';
32
33
  export * from './utxo/OPNetLimitedProvider.js';
package/build/opnet.js CHANGED
@@ -13,7 +13,9 @@ export * from './keypair/EcKeyPair.js';
13
13
  export * from './keypair/Wallet.js';
14
14
  export * from './keypair/interfaces/IWallet.js';
15
15
  export * from './keypair/AddressVerificator.js';
16
+ export * from './keypair/MessageSigner.js';
16
17
  export * from './metadata/ContractBaseMetadata.js';
18
+ export * from './network/ChainId.js';
17
19
  export * from './signer/TweakedSigner.js';
18
20
  export * from './transaction/TransactionFactory.js';
19
21
  export * from './transaction/interfaces/ITransactionParameters.js';
@@ -26,7 +28,6 @@ export * from './transaction/builders/SharedInteractionTransaction.js';
26
28
  export * from './transaction/builders/DeploymentTransaction.js';
27
29
  export * from './transaction/builders/CustomScriptTransaction.js';
28
30
  export * from './transaction/builders/MultiSignTransaction.js';
29
- export * from './network/ChainId.js';
30
31
  export * from './utils/BitcoinUtils.js';
31
32
  export * from './utxo/interfaces/IUTXO.js';
32
33
  export * from './utxo/OPNetLimitedProvider.js';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@btc-vision/transaction",
3
3
  "type": "module",
4
- "version": "1.1.8",
4
+ "version": "1.1.10",
5
5
  "author": "BlobMaster41",
6
6
  "description": "OPNet transaction library allows you to create and sign transactions for the OPNet network.",
7
7
  "engines": {
package/src/_version.ts CHANGED
@@ -1 +1 @@
1
- export const version = '1.1.8';
1
+ export const version = '1.1.10';
@@ -184,7 +184,7 @@ export class Address extends Uint8Array {
184
184
  const tweakedBytes = toXOnly(
185
185
  EcKeyPair.tweakPublicKey(Buffer.from(this.#originalPublicKey)),
186
186
  );
187
-
187
+
188
188
  super.set(tweakedBytes);
189
189
  }
190
190
  }
@@ -226,10 +226,6 @@ export class Address extends Uint8Array {
226
226
  * Convert the address to a string
227
227
  */
228
228
  public toString(): string {
229
- if (this.#p2tr) {
230
- return this.#p2tr;
231
- }
232
-
233
229
  return this.toHex();
234
230
  }
235
231
 
@@ -1,11 +1,11 @@
1
1
  import * as ecc from '@bitcoinerlab/secp256k1';
2
2
  import bip32, { BIP32API, BIP32Factory, BIP32Interface } from 'bip32';
3
3
  import { address, initEccLib, Network, networks, payments, Signer } from '@btc-vision/bitcoin';
4
- import { toXOnly } from '@btc-vision/bitcoin/src/psbt/bip371.js';
5
4
  import { ECPairAPI, ECPairFactory, ECPairInterface } from 'ecpair';
6
5
  import { IWallet } from './interfaces/IWallet.js';
7
6
  import { CURVE, ProjectivePoint as Point } from '@noble/secp256k1';
8
7
  import { taggedHash } from '@btc-vision/bitcoin/src/crypto.js';
8
+ import { toXOnly } from '@btc-vision/bitcoin/src/psbt/bip371.js';
9
9
 
10
10
  initEccLib(ecc);
11
11
 
@@ -0,0 +1,99 @@
1
+ import { ECPairInterface } from 'ecpair';
2
+ import * as ecc from '@bitcoinerlab/secp256k1';
3
+ import { crypto, Network } from '@btc-vision/bitcoin';
4
+ import { TweakedSigner } from '../signer/TweakedSigner.js';
5
+ import { EcKeyPair } from './EcKeyPair.js';
6
+ import { toXOnly } from '@btc-vision/bitcoin/src/psbt/bip371.js';
7
+
8
+ class MessageSignerBase {
9
+ public sha256(message: Buffer | Uint8Array): Buffer {
10
+ return crypto.sha256(Buffer.from(message));
11
+ }
12
+
13
+ /**
14
+ * Tweak the keypair and sign a message.
15
+ * @param {ECPairInterface} keypair - The keypair to sign the message with. Must contain a private key.
16
+ * @param {Uint8Array | Buffer | string} message - The message to sign.
17
+ * @param {Network} network - The network to sign the message for.
18
+ * @returns The Schnorr signature.
19
+ */
20
+ public tweakAndSignMessage(
21
+ keypair: ECPairInterface,
22
+ message: Uint8Array | Buffer | string,
23
+ network: Network,
24
+ ): Uint8Array {
25
+ const tweaked = TweakedSigner.tweakSigner(keypair, {
26
+ network,
27
+ });
28
+
29
+ return this.signMessage(tweaked, message);
30
+ }
31
+
32
+ /**
33
+ * Signs a message using the provided keypair.
34
+ * @param {ECPairInterface} keypair - The keypair to sign the message with. Must contain a private key.
35
+ * @param {Uint8Array | Buffer | string} message - The message to sign.
36
+ * @returns The Schnorr signature.
37
+ * @throws Error if the private key is missing or invalid.
38
+ */
39
+ public signMessage(
40
+ keypair: ECPairInterface,
41
+ message: Uint8Array | Buffer | string,
42
+ ): Uint8Array {
43
+ if (typeof message === 'string') {
44
+ message = Buffer.from(message, 'utf-8');
45
+ }
46
+
47
+ if (!keypair.privateKey) {
48
+ throw new Error('Private key not found in keypair.');
49
+ }
50
+
51
+ const hashedMessage = this.sha256(message);
52
+ return ecc.signSchnorr(hashedMessage, keypair.privateKey);
53
+ }
54
+
55
+ /**
56
+ * Verifies a Schnorr signature.
57
+ * @param {Uint8Array | Buffer} publicKey - The public key as a Uint8Array or Buffer.
58
+ * @param {Uint8Array | Buffer | string} message - The message to verify.
59
+ * @param {Uint8Array | Buffer} signature - The signature to verify.
60
+ * @returns True if the signature is valid, false otherwise.
61
+ * @throws Error if the signature length is invalid.
62
+ */
63
+ public verifySignature(
64
+ publicKey: Uint8Array | Buffer,
65
+ message: Uint8Array | Buffer | string,
66
+ signature: Uint8Array | Buffer,
67
+ ): boolean {
68
+ if (typeof message === 'string') {
69
+ message = Buffer.from(message, 'utf-8');
70
+ }
71
+
72
+ if (signature.length !== 64) {
73
+ throw new Error('Invalid signature length.');
74
+ }
75
+
76
+ const hashedMessage = this.sha256(message);
77
+ return ecc.verifySchnorr(hashedMessage, toXOnly(Buffer.from(publicKey)), signature);
78
+ }
79
+
80
+ /**
81
+ * Tweak the public key and verify a signature.
82
+ * @param {Uint8Array | Buffer} publicKey - The public key as a Uint8Array or Buffer.
83
+ * @param {Uint8Array | Buffer | string} message - The message to verify.
84
+ * @param {Uint8Array | Buffer} signature - The signature to verify.
85
+ * @returns True if the signature is valid, false otherwise.
86
+ * @throws Error if the signature length is invalid.
87
+ */
88
+ public tweakAndVerifySignature(
89
+ publicKey: Uint8Array | Buffer,
90
+ message: Uint8Array | Buffer | string,
91
+ signature: Uint8Array | Buffer,
92
+ ): boolean {
93
+ const tweakedPublicKey = EcKeyPair.tweakPublicKey(Buffer.from(publicKey));
94
+
95
+ return this.verifySignature(tweakedPublicKey, message, signature);
96
+ }
97
+ }
98
+
99
+ export const MessageSigner = new MessageSignerBase();
package/src/opnet.ts CHANGED
@@ -21,10 +21,11 @@ export * from './keypair/EcKeyPair.js';
21
21
  export * from './keypair/Wallet.js';
22
22
  export * from './keypair/interfaces/IWallet.js';
23
23
  export * from './keypair/AddressVerificator.js';
24
+ export * from './keypair/MessageSigner.js';
24
25
 
25
26
  /** Metadata */
26
- //export * from './metadata/contracts/wBTC.js';
27
27
  export * from './metadata/ContractBaseMetadata.js';
28
+ export * from './network/ChainId.js';
28
29
 
29
30
  /** Signer */
30
31
  export * from './signer/TweakedSigner.js';
@@ -39,18 +40,11 @@ export * from './transaction/enums/TransactionType.js';
39
40
  export * from './transaction/builders/InteractionTransaction.js';
40
41
  export * from './transaction/builders/FundingTransaction.js';
41
42
  export * from './transaction/builders/TransactionBuilder.js';
42
- //export * from './transaction/builders/WrapTransaction.ts.disabled';
43
43
  export * from './transaction/builders/SharedInteractionTransaction.js';
44
44
  export * from './transaction/builders/DeploymentTransaction.js';
45
- //export * from './transaction/builders/UnwrapTransaction.ts.disabled';
46
45
  export * from './transaction/builders/CustomScriptTransaction.js';
47
46
  export * from './transaction/builders/MultiSignTransaction.js';
48
47
 
49
- /** wBTC */
50
- //export * from '../wbtc_disabled/WrappedGenerationParameters.js';
51
- //export * from '../wbtc_disabled/Generate.js';
52
- export * from './network/ChainId.js';
53
-
54
48
  /** Utils */
55
49
  export * from './utils/BitcoinUtils.js';
56
50
 
@@ -67,7 +61,6 @@ export * from './utxo/interfaces/BroadcastResponse.js';
67
61
  export * from './transaction/psbt/PSBTTypes.js';
68
62
 
69
63
  export * from './transaction/shared/P2TR_MS.js';
70
- //export * from '../wbtc_disabled/UnwrapGeneration.ts.disabled';
71
64
 
72
65
  /** Consensus */
73
66
  export * from './consensus/ConsensusConfig.js';