@btc-vision/transaction 1.7.7 → 1.7.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.
Files changed (173) hide show
  1. package/browser/_version.d.ts +1 -0
  2. package/browser/chain/ChainData.d.ts +4 -0
  3. package/browser/{src/epoch → epoch}/interfaces/IChallengeSolution.d.ts +4 -4
  4. package/browser/{src/generators → generators}/Features.d.ts +6 -1
  5. package/browser/{src/generators → generators}/Generator.d.ts +1 -0
  6. package/browser/generators/MLDSAData.d.ts +15 -0
  7. package/browser/index.js +1 -1
  8. package/browser/{src/keypair → keypair}/Address.d.ts +9 -3
  9. package/browser/{src/keypair → keypair}/MessageSigner.d.ts +9 -0
  10. package/browser/{src/opnet.d.ts → opnet.d.ts} +1 -0
  11. package/browser/{src/transaction → transaction}/browser/Web3Provider.d.ts +15 -4
  12. package/browser/transaction/browser/types/OPWallet.d.ts +6 -0
  13. package/browser/{src/transaction → transaction}/builders/CustomScriptTransaction.d.ts +1 -0
  14. package/browser/{src/transaction → transaction}/builders/DeploymentTransaction.d.ts +1 -0
  15. package/browser/{src/transaction → transaction}/builders/TransactionBuilder.d.ts +4 -0
  16. package/browser/{src/transaction → transaction}/interfaces/ITransactionParameters.d.ts +3 -0
  17. package/browser/{src/transaction → transaction}/shared/TweakedTransaction.d.ts +6 -0
  18. package/browser/{src/utxo → utxo}/OPNetLimitedProvider.d.ts +1 -0
  19. package/build/_version.d.ts +1 -1
  20. package/build/_version.js +1 -1
  21. package/build/chain/ChainData.d.ts +4 -0
  22. package/build/chain/ChainData.js +20 -0
  23. package/build/epoch/ChallengeSolution.js +4 -4
  24. package/build/epoch/interfaces/IChallengeSolution.d.ts +4 -4
  25. package/build/generators/Features.d.ts +6 -1
  26. package/build/generators/Features.js +1 -0
  27. package/build/generators/Generator.d.ts +1 -0
  28. package/build/generators/Generator.js +24 -2
  29. package/build/generators/MLDSAData.d.ts +15 -0
  30. package/build/generators/MLDSAData.js +19 -0
  31. package/build/generators/builders/CalldataGenerator.js +1 -1
  32. package/build/generators/builders/DeploymentGenerator.js +1 -1
  33. package/build/generators/builders/P2WDAGenerator.js +1 -1
  34. package/build/keypair/Address.d.ts +9 -3
  35. package/build/keypair/Address.js +63 -38
  36. package/build/keypair/MessageSigner.d.ts +9 -0
  37. package/build/keypair/MessageSigner.js +101 -3
  38. package/build/opnet.d.ts +1 -0
  39. package/build/opnet.js +1 -0
  40. package/build/transaction/TransactionFactory.js +3 -0
  41. package/build/transaction/browser/Web3Provider.d.ts +15 -4
  42. package/build/transaction/browser/types/OPWallet.d.ts +2 -10
  43. package/build/transaction/browser/types/OPWallet.js +4 -2
  44. package/build/transaction/builders/CustomScriptTransaction.d.ts +1 -0
  45. package/build/transaction/builders/CustomScriptTransaction.js +3 -0
  46. package/build/transaction/builders/DeploymentTransaction.d.ts +1 -0
  47. package/build/transaction/builders/DeploymentTransaction.js +26 -1
  48. package/build/transaction/builders/InteractionTransaction.js +14 -1
  49. package/build/transaction/builders/InteractionTransactionP2WDA.js +14 -1
  50. package/build/transaction/builders/TransactionBuilder.d.ts +4 -0
  51. package/build/transaction/builders/TransactionBuilder.js +77 -0
  52. package/build/transaction/interfaces/ITransactionParameters.d.ts +3 -0
  53. package/build/transaction/shared/P2TR_MS.js +1 -0
  54. package/build/transaction/shared/TweakedTransaction.d.ts +6 -0
  55. package/build/transaction/shared/TweakedTransaction.js +19 -0
  56. package/build/utxo/OPNetLimitedProvider.d.ts +1 -0
  57. package/build/utxo/OPNetLimitedProvider.js +11 -1
  58. package/eslint.config.js +2 -1
  59. package/package.json +1 -1
  60. package/src/_version.ts +1 -1
  61. package/src/chain/ChainData.ts +32 -0
  62. package/src/epoch/ChallengeSolution.ts +4 -4
  63. package/src/epoch/interfaces/IChallengeSolution.ts +4 -4
  64. package/src/generators/Features.ts +8 -2
  65. package/src/generators/Generator.ts +35 -2
  66. package/src/generators/MLDSAData.ts +30 -0
  67. package/src/generators/builders/CalldataGenerator.ts +1 -1
  68. package/src/generators/builders/DeploymentGenerator.ts +2 -1
  69. package/src/generators/builders/LegacyCalldataGenerator.ts +1 -0
  70. package/src/generators/builders/P2WDAGenerator.ts +5 -1
  71. package/src/keypair/Address.ts +78 -38
  72. package/src/keypair/MessageSigner.ts +214 -15
  73. package/src/opnet.ts +2 -0
  74. package/src/transaction/TransactionFactory.ts +3 -0
  75. package/src/transaction/browser/Web3Provider.ts +64 -4
  76. package/src/transaction/browser/types/OPWallet.ts +6 -53
  77. package/src/transaction/builders/CustomScriptTransaction.ts +4 -0
  78. package/src/transaction/builders/DeploymentTransaction.ts +36 -8
  79. package/src/transaction/builders/InteractionTransaction.ts +17 -7
  80. package/src/transaction/builders/InteractionTransactionP2WDA.ts +17 -7
  81. package/src/transaction/builders/TransactionBuilder.ts +107 -0
  82. package/src/transaction/interfaces/ITransactionParameters.ts +12 -0
  83. package/src/transaction/shared/P2TR_MS.ts +1 -0
  84. package/src/transaction/shared/TweakedTransaction.ts +35 -0
  85. package/src/utxo/OPNetLimitedProvider.ts +19 -2
  86. package/test/address.test.ts +18 -20
  87. package/test/addressmap.test.ts +783 -0
  88. package/test/addressverificator-mldsa.test.ts +40 -16
  89. package/test/messagesigner-mldsa.test.ts +50 -50
  90. package/test/messagesigner-schnorr.test.ts +40 -40
  91. package/tsconfig.webpack.json +2 -6
  92. package/webpack.config.js +1 -1
  93. package/browser/src/_version.d.ts +0 -1
  94. package/browser/src/transaction/browser/types/OPWallet.d.ts +0 -14
  95. package/browser/test/address.test.d.ts +0 -1
  96. package/browser/test/addressverificator-mldsa.test.d.ts +0 -1
  97. package/browser/test/derivePath.test.d.ts +0 -1
  98. package/browser/test/fastmap-setall.test.d.ts +0 -1
  99. package/browser/test/fastmap.test.d.ts +0 -1
  100. package/browser/test/messagesigner-mldsa.test.d.ts +0 -1
  101. package/browser/test/messagesigner-schnorr.test.d.ts +0 -1
  102. package/browser/test/network-awareness.test.d.ts +0 -1
  103. package/browser/test/old/FastBigIntMap.d.ts +0 -18
  104. package/browser/test/oldfastmap.test.d.ts +0 -1
  105. /package/browser/{src/abi → abi}/ABICoder.d.ts +0 -0
  106. /package/browser/{src/buffer → buffer}/BinaryReader.d.ts +0 -0
  107. /package/browser/{src/buffer → buffer}/BinaryWriter.d.ts +0 -0
  108. /package/browser/{src/bytecode → bytecode}/Compressor.d.ts +0 -0
  109. /package/browser/{src/consensus → consensus}/Consensus.d.ts +0 -0
  110. /package/browser/{src/consensus → consensus}/ConsensusConfig.d.ts +0 -0
  111. /package/browser/{src/consensus → consensus}/metadata/RoswellConsensus.d.ts +0 -0
  112. /package/browser/{src/crypto → crypto}/crypto-browser.d.ts +0 -0
  113. /package/browser/{src/crypto → crypto}/crypto.d.ts +0 -0
  114. /package/browser/{src/deterministic → deterministic}/AddressMap.d.ts +0 -0
  115. /package/browser/{src/deterministic → deterministic}/AddressSet.d.ts +0 -0
  116. /package/browser/{src/deterministic → deterministic}/CustomMap.d.ts +0 -0
  117. /package/browser/{src/deterministic → deterministic}/DeterministicMap.d.ts +0 -0
  118. /package/browser/{src/deterministic → deterministic}/DeterministicSet.d.ts +0 -0
  119. /package/browser/{src/deterministic → deterministic}/FastMap.d.ts +0 -0
  120. /package/browser/{src/epoch → epoch}/ChallengeSolution.d.ts +0 -0
  121. /package/browser/{src/epoch → epoch}/validator/EpochValidator.d.ts +0 -0
  122. /package/browser/{src/event → event}/NetEvent.d.ts +0 -0
  123. /package/browser/{src/generators → generators}/AddressGenerator.d.ts +0 -0
  124. /package/browser/{src/generators → generators}/builders/CalldataGenerator.d.ts +0 -0
  125. /package/browser/{src/generators → generators}/builders/CustomGenerator.d.ts +0 -0
  126. /package/browser/{src/generators → generators}/builders/DeploymentGenerator.d.ts +0 -0
  127. /package/browser/{src/generators → generators}/builders/LegacyCalldataGenerator.d.ts +0 -0
  128. /package/browser/{src/generators → generators}/builders/MultiSignGenerator.d.ts +0 -0
  129. /package/browser/{src/generators → generators}/builders/P2WDAGenerator.d.ts +0 -0
  130. /package/browser/{src/index.d.ts → index.d.ts} +0 -0
  131. /package/browser/{src/keypair → keypair}/AddressVerificator.d.ts +0 -0
  132. /package/browser/{src/keypair → keypair}/EcKeyPair.d.ts +0 -0
  133. /package/browser/{src/keypair → keypair}/Secp256k1PointDeriver.d.ts +0 -0
  134. /package/browser/{src/keypair → keypair}/Wallet.d.ts +0 -0
  135. /package/browser/{src/keypair → keypair}/interfaces/IWallet.d.ts +0 -0
  136. /package/browser/{src/metadata → metadata}/ContractBaseMetadata.d.ts +0 -0
  137. /package/browser/{src/metadata → metadata}/tokens.d.ts +0 -0
  138. /package/browser/{src/mnemonic → mnemonic}/BIPStandard.d.ts +0 -0
  139. /package/browser/{src/mnemonic → mnemonic}/Mnemonic.d.ts +0 -0
  140. /package/browser/{src/mnemonic → mnemonic}/MnemonicStrength.d.ts +0 -0
  141. /package/browser/{src/network → network}/ChainId.d.ts +0 -0
  142. /package/browser/{src/p2wda → p2wda}/P2WDADetector.d.ts +0 -0
  143. /package/browser/{src/signer → signer}/SignerUtils.d.ts +0 -0
  144. /package/browser/{src/signer → signer}/TweakedSigner.d.ts +0 -0
  145. /package/browser/{src/transaction → transaction}/ContractAddress.d.ts +0 -0
  146. /package/browser/{src/transaction → transaction}/TransactionFactory.d.ts +0 -0
  147. /package/browser/{src/transaction → transaction}/browser/BrowserSignerBase.d.ts +0 -0
  148. /package/browser/{src/transaction → transaction}/browser/extensions/UnisatSigner.d.ts +0 -0
  149. /package/browser/{src/transaction → transaction}/browser/extensions/XverseSigner.d.ts +0 -0
  150. /package/browser/{src/transaction → transaction}/browser/types/Unisat.d.ts +0 -0
  151. /package/browser/{src/transaction → transaction}/browser/types/Xverse.d.ts +0 -0
  152. /package/browser/{src/transaction → transaction}/builders/CancelTransaction.d.ts +0 -0
  153. /package/browser/{src/transaction → transaction}/builders/ChallengeSolutionTransaction.d.ts +0 -0
  154. /package/browser/{src/transaction → transaction}/builders/FundingTransaction.d.ts +0 -0
  155. /package/browser/{src/transaction → transaction}/builders/InteractionTransaction.d.ts +0 -0
  156. /package/browser/{src/transaction → transaction}/builders/InteractionTransactionP2WDA.d.ts +0 -0
  157. /package/browser/{src/transaction → transaction}/builders/MultiSignTransaction.d.ts +0 -0
  158. /package/browser/{src/transaction → transaction}/builders/SharedInteractionTransaction.d.ts +0 -0
  159. /package/browser/{src/transaction → transaction}/enums/TransactionType.d.ts +0 -0
  160. /package/browser/{src/transaction → transaction}/interfaces/Tap.d.ts +0 -0
  161. /package/browser/{src/transaction → transaction}/mineable/IP2WSHAddress.d.ts +0 -0
  162. /package/browser/{src/transaction → transaction}/mineable/TimelockGenerator.d.ts +0 -0
  163. /package/browser/{src/transaction → transaction}/processor/PsbtTransaction.d.ts +0 -0
  164. /package/browser/{src/transaction → transaction}/psbt/PSBTTypes.d.ts +0 -0
  165. /package/browser/{src/transaction → transaction}/shared/P2TR_MS.d.ts +0 -0
  166. /package/browser/{src/utils → utils}/BitcoinUtils.d.ts +0 -0
  167. /package/browser/{src/utils → utils}/BufferHelper.d.ts +0 -0
  168. /package/browser/{src/utils → utils}/StringToBuffer.d.ts +0 -0
  169. /package/browser/{src/utils → utils}/lengths.d.ts +0 -0
  170. /package/browser/{src/utils → utils}/types.d.ts +0 -0
  171. /package/browser/{src/utxo → utxo}/interfaces/BroadcastResponse.d.ts +0 -0
  172. /package/browser/{src/utxo → utxo}/interfaces/IUTXO.d.ts +0 -0
  173. /package/browser/{src/verification → verification}/TapscriptVerificator.d.ts +0 -0
@@ -9,6 +9,7 @@ import { TimeLockGenerator } from '../transaction/mineable/TimelockGenerator.js'
9
9
  import { IP2WSHAddress } from '../transaction/mineable/IP2WSHAddress.js';
10
10
  import { P2WDADetector } from '../p2wda/P2WDADetector.js';
11
11
  import { sha256 } from '@noble/hashes/sha2';
12
+ import { MLDSASecurityLevel } from '@btc-vision/bip32';
12
13
 
13
14
  /**
14
15
  * Objects of type "Address" represent hashed ML-DSA (quantum) public keys (using SHA256 of quantum keys) and maintain classical public keys separately.
@@ -32,8 +33,10 @@ export class Address extends Uint8Array {
32
33
  #mldsaPublicKey: Uint8Array | undefined;
33
34
  #cachedBigInt: bigint | undefined;
34
35
  #cachedUint64Array: [bigint, bigint, bigint, bigint] | undefined;
36
+ #originalMDLSAPublicKey: Uint8Array | undefined;
37
+ #mldsaLevel: MLDSASecurityLevel | undefined;
35
38
 
36
- private classicPublicKey: Uint8Array | undefined;
39
+ private legacyPublicKey: Uint8Array | undefined;
37
40
 
38
41
  public constructor(mldsaPublicKey?: ArrayLike<number>, publicKeyOrTweak?: ArrayLike<number>) {
39
42
  super(ADDRESS_BYTE_LENGTH);
@@ -43,13 +46,29 @@ export class Address extends Uint8Array {
43
46
  }
44
47
 
45
48
  if (publicKeyOrTweak) {
46
- this.classicPublicKey = new Uint8Array(publicKeyOrTweak.length);
47
- this.classicPublicKey.set(publicKeyOrTweak);
49
+ this.legacyPublicKey = new Uint8Array(publicKeyOrTweak.length);
50
+ this.legacyPublicKey.set(publicKeyOrTweak);
48
51
  }
49
52
 
50
53
  this.set(mldsaPublicKey);
51
54
  }
52
55
 
56
+ public get mldsaLevel(): MLDSASecurityLevel | undefined {
57
+ return this.#mldsaLevel;
58
+ }
59
+
60
+ public set mldsaLevel(level: MLDSASecurityLevel) {
61
+ this.#mldsaLevel = level;
62
+ }
63
+
64
+ public get originalMDLSAPublicKey(): Uint8Array | undefined {
65
+ return this.#originalMDLSAPublicKey;
66
+ }
67
+
68
+ public set originalMDLSAPublicKey(key: Buffer | Uint8Array) {
69
+ this.#originalMDLSAPublicKey = new Uint8Array(key);
70
+ }
71
+
53
72
  /**
54
73
  * If available, this will return the original public key associated with the address.
55
74
  * @returns {Uint8Array} The original public key used to create the address.
@@ -68,7 +87,7 @@ export class Address extends Uint8Array {
68
87
  */
69
88
  private get keyPair(): ECPairInterface {
70
89
  if (!this.#keyPair) {
71
- throw new Error('Classical public key not set for address');
90
+ throw new Error('Legacy public key not set for address');
72
91
  }
73
92
 
74
93
  return this.#keyPair;
@@ -84,10 +103,10 @@ export class Address extends Uint8Array {
84
103
  /**
85
104
  * Create an address from a hex string
86
105
  * @param {string} mldsaPublicKey The ml-dsa public key in hex format
87
- * @param {string} classicPublicKey The classical public key in hex format
106
+ * @param {string} legacyPublicKey The classical public key in hex format
88
107
  * @returns {Address} The address
89
108
  */
90
- public static fromString(mldsaPublicKey: string, classicPublicKey?: string): Address {
109
+ public static fromString(mldsaPublicKey: string, legacyPublicKey?: string): Address {
91
110
  if (!mldsaPublicKey) {
92
111
  throw new Error('Invalid public key');
93
112
  }
@@ -103,18 +122,18 @@ export class Address extends Uint8Array {
103
122
  }
104
123
 
105
124
  let classicBuffer: Buffer | undefined;
106
- if (classicPublicKey) {
107
- if (classicPublicKey.startsWith('0x')) {
108
- classicPublicKey = classicPublicKey.slice(2);
125
+ if (legacyPublicKey) {
126
+ if (legacyPublicKey.startsWith('0x')) {
127
+ legacyPublicKey = legacyPublicKey.slice(2);
109
128
  }
110
129
 
111
- if (!BitcoinUtils.isValidHex(classicPublicKey)) {
130
+ if (!BitcoinUtils.isValidHex(legacyPublicKey)) {
112
131
  throw new Error(
113
132
  'You must only pass classical 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.',
114
133
  );
115
134
  }
116
135
 
117
- classicBuffer = Buffer.from(classicPublicKey, 'hex');
136
+ classicBuffer = Buffer.from(legacyPublicKey, 'hex');
118
137
  }
119
138
 
120
139
  return new Address(Buffer.from(mldsaPublicKey, 'hex'), classicBuffer);
@@ -245,6 +264,20 @@ export class Address extends Uint8Array {
245
264
  return this.#cachedUint64Array;
246
265
  }
247
266
 
267
+ /**
268
+ * Check if the address is the dead address
269
+ * @returns {boolean}
270
+ */
271
+ public isDead(): boolean {
272
+ for (let i = 0; i < ADDRESS_BYTE_LENGTH; i++) {
273
+ if (this[i] !== 0) {
274
+ return false;
275
+ }
276
+ }
277
+
278
+ return true;
279
+ }
280
+
248
281
  /**
249
282
  * Converts the address to a hex string
250
283
  * @returns {string} The hex string
@@ -258,11 +291,11 @@ export class Address extends Uint8Array {
258
291
  * @returns {string} The hex string
259
292
  */
260
293
  public tweakedToHex(): string {
261
- if (!this.classicPublicKey) {
262
- throw new Error('Classical public key not set');
294
+ if (!this.legacyPublicKey) {
295
+ throw new Error('Legacy public key not set');
263
296
  }
264
297
 
265
- return '0x' + Buffer.from(this.classicPublicKey).toString('hex');
298
+ return '0x' + Buffer.from(this.legacyPublicKey).toString('hex');
266
299
  }
267
300
 
268
301
  /**
@@ -278,16 +311,16 @@ export class Address extends Uint8Array {
278
311
  * @returns {Buffer} The buffer
279
312
  */
280
313
  public tweakedPublicKeyToBuffer(): Buffer {
281
- if (!this.classicPublicKey) {
282
- throw new Error('Classical public key not set');
314
+ if (!this.legacyPublicKey) {
315
+ throw new Error('Legacy public key not set');
283
316
  }
284
317
 
285
- return Buffer.from(this.classicPublicKey);
318
+ return Buffer.from(this.legacyPublicKey);
286
319
  }
287
320
 
288
321
  public toUncompressedHex(): string {
289
322
  if (!this.#uncompressed) {
290
- throw new Error('Classical public key not set');
323
+ throw new Error('Legacy public key not set');
291
324
  }
292
325
 
293
326
  return '0x' + this.#uncompressed.uncompressed.toString('hex');
@@ -295,7 +328,7 @@ export class Address extends Uint8Array {
295
328
 
296
329
  public toUncompressedBuffer(): Buffer {
297
330
  if (!this.#uncompressed) {
298
- throw new Error('Classical public key not set');
331
+ throw new Error('Legacy public key not set');
299
332
  }
300
333
 
301
334
  return this.#uncompressed.uncompressed;
@@ -303,7 +336,7 @@ export class Address extends Uint8Array {
303
336
 
304
337
  public toHybridPublicKeyHex(): string {
305
338
  if (!this.#uncompressed) {
306
- throw new Error('Classical public key not set');
339
+ throw new Error('Legacy public key not set');
307
340
  }
308
341
 
309
342
  return '0x' + this.#uncompressed.hybrid.toString('hex');
@@ -311,7 +344,7 @@ export class Address extends Uint8Array {
311
344
 
312
345
  public toHybridPublicKeyBuffer(): Buffer {
313
346
  if (!this.#uncompressed) {
314
- throw new Error('Classical public key not set');
347
+ throw new Error('Legacy public key not set');
315
348
  }
316
349
 
317
350
  return this.#uncompressed.hybrid;
@@ -319,7 +352,7 @@ export class Address extends Uint8Array {
319
352
 
320
353
  public originalPublicKeyBuffer(): Buffer {
321
354
  if (!this.#originalPublicKey) {
322
- throw new Error('Classical public key not set');
355
+ throw new Error('Legacy public key not set');
323
356
  }
324
357
 
325
358
  return Buffer.from(this.#originalPublicKey);
@@ -421,19 +454,19 @@ export class Address extends Uint8Array {
421
454
  * @returns {void}
422
455
  */
423
456
  public override set(mldsaPublicKey: ArrayLike<number>): void {
424
- if (this.classicPublicKey) {
457
+ if (this.legacyPublicKey) {
425
458
  const validLengths = [ADDRESS_BYTE_LENGTH, 33, 65];
426
- if (!validLengths.includes(this.classicPublicKey.length)) {
427
- throw new Error(`Invalid public key length ${this.classicPublicKey.length}`);
459
+ if (!validLengths.includes(this.legacyPublicKey.length)) {
460
+ throw new Error(`Invalid public key length ${this.legacyPublicKey.length}`);
428
461
  }
429
462
 
430
- if (this.classicPublicKey.length === ADDRESS_BYTE_LENGTH) {
463
+ if (this.legacyPublicKey.length === ADDRESS_BYTE_LENGTH) {
431
464
  const buf = Buffer.alloc(ADDRESS_BYTE_LENGTH);
432
- buf.set(this.classicPublicKey);
465
+ buf.set(this.legacyPublicKey);
433
466
 
434
467
  this.#tweakedUncompressed = ContractAddress.generateHybridKeyFromHash(buf);
435
468
  } else {
436
- this.autoFormat(this.classicPublicKey);
469
+ this.autoFormat(this.legacyPublicKey);
437
470
  }
438
471
  }
439
472
 
@@ -475,8 +508,15 @@ export class Address extends Uint8Array {
475
508
  * @param {Network} network The network
476
509
  * @returns {boolean} If the public key is valid
477
510
  */
478
- public isValid(network: Network): boolean {
479
- return AddressVerificator.isValidPublicKey(Buffer.from(this).toString('hex'), network);
511
+ public isValidLegacyPublicKey(network: Network): boolean {
512
+ if (!this.legacyPublicKey) {
513
+ throw new Error(`Legacy key not set.`);
514
+ }
515
+
516
+ return AddressVerificator.isValidPublicKey(
517
+ Buffer.from(this.legacyPublicKey).toString('hex'),
518
+ network,
519
+ );
480
520
  }
481
521
 
482
522
  /**
@@ -529,8 +569,8 @@ export class Address extends Uint8Array {
529
569
  * @param {Network} network The network
530
570
  */
531
571
  public p2tr(network: Network): string {
532
- if (!this.classicPublicKey) {
533
- throw new Error('Classical public key not set');
572
+ if (!this.legacyPublicKey) {
573
+ throw new Error('Legacy public key not set');
534
574
  }
535
575
 
536
576
  if (this.#p2tr && this.#network === network) {
@@ -538,7 +578,7 @@ export class Address extends Uint8Array {
538
578
  }
539
579
 
540
580
  const p2trAddy: string | undefined = EcKeyPair.tweakedPubKeyBufferToAddress(
541
- this.classicPublicKey,
581
+ this.legacyPublicKey,
542
582
  network,
543
583
  );
544
584
 
@@ -549,7 +589,7 @@ export class Address extends Uint8Array {
549
589
  return p2trAddy;
550
590
  }
551
591
 
552
- throw new Error('Classical public key not set');
592
+ throw new Error('Legacy public key not set');
553
593
  }
554
594
 
555
595
  /**
@@ -665,7 +705,7 @@ export class Address extends Uint8Array {
665
705
 
666
706
  public toTweakedHybridPublicKeyHex(): string {
667
707
  if (!this.#tweakedUncompressed) {
668
- throw new Error('Classical public key not set');
708
+ throw new Error('Legacy public key not set');
669
709
  }
670
710
 
671
711
  return '0x' + this.#tweakedUncompressed.toString('hex');
@@ -673,7 +713,7 @@ export class Address extends Uint8Array {
673
713
 
674
714
  public toTweakedHybridPublicKeyBuffer(): Buffer {
675
715
  if (!this.#tweakedUncompressed) {
676
- throw new Error('Classical public key not set');
716
+ throw new Error('Legacy public key not set');
677
717
  }
678
718
 
679
719
  return this.#tweakedUncompressed;
@@ -699,7 +739,7 @@ export class Address extends Uint8Array {
699
739
 
700
740
  this.#tweakedUncompressed = ContractAddress.generateHybridKeyFromHash(tweakedBytes);
701
741
 
702
- this.classicPublicKey = new Uint8Array(ADDRESS_BYTE_LENGTH);
703
- this.classicPublicKey.set(tweakedBytes);
742
+ this.legacyPublicKey = new Uint8Array(ADDRESS_BYTE_LENGTH);
743
+ this.legacyPublicKey.set(tweakedBytes);
704
744
  }
705
745
  }
@@ -4,6 +4,8 @@ import { crypto, Network, toXOnly } from '@btc-vision/bitcoin';
4
4
  import { TweakedSigner } from '../signer/TweakedSigner.js';
5
5
  import { EcKeyPair } from './EcKeyPair.js';
6
6
  import { MLDSASecurityLevel, QuantumBIP32Interface } from '@btc-vision/bip32';
7
+ import { isOPWallet, OPWallet } from '../transaction/browser/types/OPWallet.js';
8
+ import { MLDSASignature } from '../transaction/browser/Web3Provider.js';
7
9
 
8
10
  export interface SignedMessage {
9
11
  readonly signature: Uint8Array;
@@ -17,27 +19,204 @@ export interface MLDSASignedMessage {
17
19
  readonly securityLevel: MLDSASecurityLevel;
18
20
  }
19
21
 
22
+ interface WindowWithOPWallet {
23
+ opnet?: OPWallet;
24
+ }
25
+
20
26
  class MessageSignerBase {
21
27
  public sha256(message: Buffer | Uint8Array): Buffer {
22
28
  return crypto.sha256(Buffer.from(message));
23
29
  }
24
30
 
31
+ /**
32
+ * Attempts to sign a message using OP_WALLET if available in browser environment.
33
+ * Returns null if not in browser or OP_WALLET is not available.
34
+ * @param {Uint8Array | Buffer | string} message - The message to sign.
35
+ * @returns {Promise<SignedMessage | null>} The Schnorr signature or null if OP_WALLET unavailable.
36
+ */
37
+ public async trySignSchnorrWithOPWallet(
38
+ message: Uint8Array | Buffer | string,
39
+ ): Promise<SignedMessage | null> {
40
+ const wallet = this.getOPWallet();
41
+ if (!wallet) {
42
+ return null;
43
+ }
44
+
45
+ const messageBuffer =
46
+ typeof message === 'string' ? Buffer.from(message, 'utf-8') : Buffer.from(message);
47
+
48
+ const hashedMessage = this.sha256(messageBuffer);
49
+ const messageHex = hashedMessage.toString('hex');
50
+
51
+ const signatureHex = await wallet.web3.signSchnorr(messageHex);
52
+ return {
53
+ signature: Buffer.from(signatureHex, 'hex'),
54
+ message: hashedMessage,
55
+ };
56
+ }
57
+
58
+ /**
59
+ * Attempts to sign a message using OP_WALLET ML-DSA if available.
60
+ * Returns null if not in browser or OP_WALLET is not available.
61
+ * @param {Uint8Array | Buffer | string} message - The message to sign.
62
+ * @returns {Promise<MLDSASignedMessage | null>} The ML-DSA signature or null if OP_WALLET unavailable.
63
+ */
64
+ public async trySignMLDSAWithOPWallet(
65
+ message: Uint8Array | Buffer | string,
66
+ ): Promise<MLDSASignedMessage | null> {
67
+ const wallet = this.getOPWallet();
68
+ if (!wallet) {
69
+ return null;
70
+ }
71
+
72
+ const messageBuffer =
73
+ typeof message === 'string' ? Buffer.from(message, 'utf-8') : Buffer.from(message);
74
+
75
+ const hashedMessage = this.sha256(messageBuffer);
76
+ const messageHex = hashedMessage.toString('hex');
77
+
78
+ const result: MLDSASignature = await wallet.web3.signMLDSAMessage(messageHex);
79
+ return {
80
+ signature: Buffer.from(result.signature, 'hex'),
81
+ message: hashedMessage,
82
+ publicKey: Buffer.from(result.publicKey, 'hex'),
83
+ securityLevel: result.securityLevel,
84
+ };
85
+ }
86
+
87
+ /**
88
+ * Signs a message using Schnorr, automatically using OP_WALLET if available and no keypair provided.
89
+ * @param {Uint8Array | Buffer | string} message - The message to sign.
90
+ * @param {ECPairInterface} [keypair] - Optional keypair for local signing.
91
+ * @returns {Promise<SignedMessage>} The Schnorr signature.
92
+ * @throws {Error} If no keypair provided and OP_WALLET is not available.
93
+ */
94
+ public async signMessageAuto(
95
+ message: Uint8Array | Buffer | string,
96
+ keypair?: ECPairInterface,
97
+ ): Promise<SignedMessage> {
98
+ if (!keypair) {
99
+ const walletResult = await this.trySignSchnorrWithOPWallet(message);
100
+ if (walletResult) {
101
+ return walletResult;
102
+ }
103
+ throw new Error('No keypair provided and OP_WALLET is not available.');
104
+ }
105
+
106
+ return this.signMessage(keypair, message);
107
+ }
108
+
109
+ /**
110
+ * Signs a message with tweaking, automatically using OP_WALLET if available.
111
+ * Note: OP_WALLET signSchnorr may already return a tweaked signature depending on wallet implementation.
112
+ * @param {Uint8Array | Buffer | string} message - The message to sign.
113
+ * @param {ECPairInterface} [keypair] - Optional keypair for local signing.
114
+ * @param {Network} [network] - Network required when signing with a local keypair.
115
+ * @returns {Promise<SignedMessage>} The Schnorr signature.
116
+ * @throws {Error} If no keypair provided and OP_WALLET is not available.
117
+ * @throws {Error} If keypair provided but network is missing.
118
+ */
119
+ public async tweakAndSignMessageAuto(
120
+ message: Uint8Array | Buffer | string,
121
+ keypair?: ECPairInterface,
122
+ network?: Network,
123
+ ): Promise<SignedMessage> {
124
+ if (!keypair) {
125
+ const walletResult = await this.trySignSchnorrWithOPWallet(message);
126
+ if (walletResult) {
127
+ return walletResult;
128
+ }
129
+ throw new Error('No keypair provided and OP_WALLET is not available.');
130
+ }
131
+
132
+ if (!network) {
133
+ throw new Error('Network is required when signing with a local keypair.');
134
+ }
135
+
136
+ return this.tweakAndSignMessage(keypair, message, network);
137
+ }
138
+
139
+ /**
140
+ * Signs an ML-DSA message, automatically using OP_WALLET if available.
141
+ * @param {Uint8Array | Buffer | string} message - The message to sign.
142
+ * @param {QuantumBIP32Interface} [mldsaKeypair] - Optional ML-DSA keypair for local signing.
143
+ * @returns {Promise<MLDSASignedMessage>} The ML-DSA signature with metadata.
144
+ * @throws {Error} If no keypair provided and OP_WALLET is not available.
145
+ */
146
+ public async signMLDSAMessageAuto(
147
+ message: Uint8Array | Buffer | string,
148
+ mldsaKeypair?: QuantumBIP32Interface,
149
+ ): Promise<MLDSASignedMessage> {
150
+ if (!mldsaKeypair) {
151
+ const walletResult = await this.trySignMLDSAWithOPWallet(message);
152
+ if (walletResult) {
153
+ return walletResult;
154
+ }
155
+ throw new Error('No ML-DSA keypair provided and OP_WALLET is not available.');
156
+ }
157
+
158
+ return this.signMLDSAMessage(mldsaKeypair, message);
159
+ }
160
+
161
+ /**
162
+ * Verifies an ML-DSA signature using OP_WALLET if available.
163
+ * Returns null if OP_WALLET is not available.
164
+ * @param {Uint8Array | Buffer | string} message - The message to verify.
165
+ * @param {MLDSASignedMessage} signature - The ML-DSA signature to verify.
166
+ * @returns {Promise<boolean | null>} True if valid, false if invalid, null if OP_WALLET unavailable.
167
+ */
168
+ public async verifyMLDSAWithOPWallet(
169
+ message: Uint8Array | Buffer | string,
170
+ signature: MLDSASignedMessage,
171
+ ): Promise<boolean | null> {
172
+ const wallet = this.getOPWallet();
173
+ if (!wallet) {
174
+ return null;
175
+ }
176
+
177
+ const messageBuffer =
178
+ typeof message === 'string' ? Buffer.from(message, 'utf-8') : Buffer.from(message);
179
+
180
+ const hashedMessage = this.sha256(messageBuffer);
181
+
182
+ const mldsaSignature: MLDSASignature = {
183
+ signature: Buffer.from(signature.signature).toString('hex'),
184
+ publicKey: Buffer.from(signature.publicKey).toString('hex'),
185
+ securityLevel: signature.securityLevel,
186
+ messageHash: hashedMessage.toString('hex'),
187
+ };
188
+
189
+ return wallet.web3.verifyMLDSASignature(hashedMessage.toString('hex'), mldsaSignature);
190
+ }
191
+
192
+ /**
193
+ * Gets the ML-DSA public key from OP_WALLET if available.
194
+ * Returns null if OP_WALLET is not available.
195
+ * @returns {Promise<Buffer | null>} The ML-DSA public key or null if OP_WALLET unavailable.
196
+ */
197
+ public async getMLDSAPublicKeyFromOPWallet(): Promise<Buffer | null> {
198
+ const wallet = this.getOPWallet();
199
+ if (!wallet) {
200
+ return null;
201
+ }
202
+
203
+ const publicKeyHex = await wallet.web3.getMLDSAPublicKey();
204
+ return Buffer.from(publicKeyHex, 'hex');
205
+ }
206
+
25
207
  /**
26
208
  * Tweak the keypair and sign a message.
27
209
  * @param {ECPairInterface} keypair - The keypair to sign the message with. Must contain a private key.
28
210
  * @param {Uint8Array | Buffer | string} message - The message to sign.
29
211
  * @param {Network} network - The network to sign the message for.
30
- * @returns The Schnorr signature.
212
+ * @returns {SignedMessage} The Schnorr signature.
31
213
  */
32
214
  public tweakAndSignMessage(
33
215
  keypair: ECPairInterface,
34
216
  message: Uint8Array | Buffer | string,
35
217
  network: Network,
36
218
  ): SignedMessage {
37
- const tweaked = TweakedSigner.tweakSigner(keypair, {
38
- network,
39
- });
40
-
219
+ const tweaked = TweakedSigner.tweakSigner(keypair, { network });
41
220
  return this.signMessage(tweaked, message);
42
221
  }
43
222
 
@@ -45,8 +224,8 @@ class MessageSignerBase {
45
224
  * Signs a message using the provided keypair.
46
225
  * @param {ECPairInterface} keypair - The keypair to sign the message with. Must contain a private key.
47
226
  * @param {Uint8Array | Buffer | string} message - The message to sign.
48
- * @returns The Schnorr signature.
49
- * @throws Error if the private key is missing or invalid.
227
+ * @returns {SignedMessage} The Schnorr signature.
228
+ * @throws {Error} If the private key is missing or invalid.
50
229
  */
51
230
  public signMessage(
52
231
  keypair: ECPairInterface,
@@ -72,8 +251,8 @@ class MessageSignerBase {
72
251
  * @param {Uint8Array | Buffer} publicKey - The public key as a Uint8Array or Buffer.
73
252
  * @param {Uint8Array | Buffer | string} message - The message to verify.
74
253
  * @param {Uint8Array | Buffer} signature - The signature to verify.
75
- * @returns True if the signature is valid, false otherwise.
76
- * @throws Error if the signature length is invalid.
254
+ * @returns {boolean} True if the signature is valid, false otherwise.
255
+ * @throws {Error} If the signature length is invalid.
77
256
  */
78
257
  public verifySignature(
79
258
  publicKey: Uint8Array | Buffer,
@@ -97,8 +276,8 @@ class MessageSignerBase {
97
276
  * @param {Uint8Array | Buffer} publicKey - The public key as a Uint8Array or Buffer.
98
277
  * @param {Uint8Array | Buffer | string} message - The message to verify.
99
278
  * @param {Uint8Array | Buffer} signature - The signature to verify.
100
- * @returns True if the signature is valid, false otherwise.
101
- * @throws Error if the signature length is invalid.
279
+ * @returns {boolean} True if the signature is valid, false otherwise.
280
+ * @throws {Error} If the signature length is invalid.
102
281
  */
103
282
  public tweakAndVerifySignature(
104
283
  publicKey: Uint8Array | Buffer,
@@ -106,7 +285,6 @@ class MessageSignerBase {
106
285
  signature: Uint8Array | Buffer,
107
286
  ): boolean {
108
287
  const tweakedPublicKey = EcKeyPair.tweakPublicKey(Buffer.from(publicKey));
109
-
110
288
  return this.verifySignature(tweakedPublicKey, message, signature);
111
289
  }
112
290
 
@@ -114,8 +292,8 @@ class MessageSignerBase {
114
292
  * Signs a message using ML-DSA signature scheme.
115
293
  * @param {QuantumBIP32Interface} mldsaKeypair - The ML-DSA keypair to sign with. Must contain a private key.
116
294
  * @param {Uint8Array | Buffer | string} message - The message to sign.
117
- * @returns The ML-DSA signature with metadata.
118
- * @throws Error if the private key is missing.
295
+ * @returns {MLDSASignedMessage} The ML-DSA signature with metadata.
296
+ * @throws {Error} If the private key is missing.
119
297
  */
120
298
  public signMLDSAMessage(
121
299
  mldsaKeypair: QuantumBIP32Interface,
@@ -145,7 +323,7 @@ class MessageSignerBase {
145
323
  * @param {QuantumBIP32Interface} mldsaKeypair - The ML-DSA keypair with the public key.
146
324
  * @param {Uint8Array | Buffer | string} message - The message to verify.
147
325
  * @param {Uint8Array | Buffer} signature - The ML-DSA signature to verify.
148
- * @returns True if the signature is valid, false otherwise.
326
+ * @returns {boolean} True if the signature is valid, false otherwise.
149
327
  */
150
328
  public verifyMLDSASignature(
151
329
  mldsaKeypair: QuantumBIP32Interface,
@@ -159,6 +337,27 @@ class MessageSignerBase {
159
337
  const hashedMessage = this.sha256(message);
160
338
  return mldsaKeypair.verify(hashedMessage, signature);
161
339
  }
340
+
341
+ /**
342
+ * Checks if OP_WALLET is available in the current environment.
343
+ * @returns {boolean} True if OP_WALLET is available, false otherwise.
344
+ */
345
+ public isOPWalletAvailable(): boolean {
346
+ return this.getOPWallet() !== null;
347
+ }
348
+
349
+ private getOPWallet(): OPWallet | null {
350
+ if (typeof window === 'undefined') {
351
+ return null;
352
+ }
353
+
354
+ const _window = window as WindowWithOPWallet;
355
+ if (!_window.opnet || !isOPWallet(_window.opnet)) {
356
+ return null;
357
+ }
358
+
359
+ return _window.opnet;
360
+ }
162
361
  }
163
362
 
164
363
  export const MessageSigner = new MessageSignerBase();
package/src/opnet.ts CHANGED
@@ -46,6 +46,8 @@ export {
46
46
  QuantumDerivationPath,
47
47
  } from '@btc-vision/bip32';
48
48
 
49
+ export * from './generators/MLDSAData.js';
50
+
49
51
  /** Metadata */
50
52
  export * from './metadata/ContractBaseMetadata.js';
51
53
  export * from './network/ChainId.js';
@@ -188,6 +188,7 @@ export class TransactionFactory {
188
188
  randomBytes: finalTransaction.getRndBytes(),
189
189
  nonWitnessUtxo: signedTransaction.tx.toBuffer(),
190
190
  estimatedFees: finalTransaction.estimatedFees,
191
+ compiledTargetScript: finalTransaction.exportCompiledTargetScript(),
191
192
  optionalInputs: inputs,
192
193
  };
193
194
 
@@ -288,6 +289,7 @@ export class TransactionFactory {
288
289
  utxos: fundingUTXO,
289
290
  randomBytes: finalTransaction.getRndBytes(),
290
291
  challenge: challenge,
292
+ compiledTargetScript: finalTransaction.exportCompiledTargetScript(),
291
293
  nonWitnessUtxo: signedTransaction.tx.toBuffer(),
292
294
  estimatedFees: finalTransaction.estimatedFees,
293
295
  optionalInputs: inputs,
@@ -392,6 +394,7 @@ export class TransactionFactory {
392
394
  ...deploymentParameters,
393
395
  utxos: [newUtxo],
394
396
  randomBytes: finalTransaction.getRndBytes(),
397
+ compiledTargetScript: finalTransaction.exportCompiledTargetScript(),
395
398
  challenge: challenge,
396
399
  nonWitnessUtxo: signedTransaction.toBuffer(),
397
400
  estimatedFees: finalTransaction.estimatedFees,