@btc-vision/transaction 1.0.120 → 1.0.121
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.
- package/browser/_version.d.ts +1 -1
- package/browser/buffer/BinaryWriter.d.ts +1 -1
- package/browser/index.js +1 -1
- package/browser/keypair/Address.d.ts +2 -5
- package/browser/keypair/EcKeyPair.d.ts +1 -1
- package/build/_version.d.ts +1 -1
- package/build/_version.js +1 -1
- package/build/buffer/BinaryWriter.d.ts +1 -1
- package/build/buffer/BinaryWriter.js +4 -5
- package/build/keypair/Address.d.ts +2 -5
- package/build/keypair/Address.js +28 -24
- package/build/keypair/EcKeyPair.d.ts +1 -1
- package/build/keypair/EcKeyPair.js +14 -7
- package/build/keypair/Wallet.js +1 -1
- package/build/utils/BitcoinUtils.js +1 -5
- package/gulpfile.js +1 -1
- package/package.json +20 -20
- package/src/_version.ts +1 -1
- package/src/buffer/BinaryWriter.ts +5 -6
- package/src/keypair/Address.ts +41 -38
- package/src/keypair/EcKeyPair.ts +19 -11
- package/src/keypair/Wallet.ts +1 -4
- package/src/utils/BitcoinUtils.ts +3 -7
package/src/keypair/EcKeyPair.ts
CHANGED
|
@@ -4,17 +4,21 @@ import { address, initEccLib, Network, networks, payments, Signer } from '@btc-v
|
|
|
4
4
|
import { toXOnly } from '@btc-vision/bitcoin/src/psbt/bip371.js';
|
|
5
5
|
import { ECPairAPI, ECPairFactory, ECPairInterface } from 'ecpair';
|
|
6
6
|
import { IWallet } from './interfaces/IWallet.js';
|
|
7
|
-
import { CURVE, Point
|
|
7
|
+
import { CURVE, ProjectivePoint as Point } from '@noble/secp256k1';
|
|
8
8
|
import { taggedHash } from '@btc-vision/bitcoin/src/crypto.js';
|
|
9
9
|
|
|
10
10
|
initEccLib(ecc);
|
|
11
11
|
|
|
12
12
|
const BIP32factory = typeof bip32 === 'function' ? bip32 : BIP32Factory;
|
|
13
|
-
|
|
14
13
|
if (!BIP32factory) {
|
|
15
14
|
throw new Error('Failed to load BIP32 library');
|
|
16
15
|
}
|
|
17
16
|
|
|
17
|
+
const mod = (a: bigint, b: bigint): bigint => {
|
|
18
|
+
const result = a % b;
|
|
19
|
+
return result >= 0n ? result : result + b;
|
|
20
|
+
};
|
|
21
|
+
|
|
18
22
|
/**
|
|
19
23
|
* Class for handling EC key pairs
|
|
20
24
|
* @class EcKeyPair
|
|
@@ -212,20 +216,24 @@ export class EcKeyPair {
|
|
|
212
216
|
|
|
213
217
|
/**
|
|
214
218
|
* Tweak a public key
|
|
215
|
-
* @param {string} compressedPubKeyHex - The compressed public key hex string
|
|
216
|
-
* @returns {
|
|
219
|
+
* @param {string | Buffer} compressedPubKeyHex - The compressed public key hex string
|
|
220
|
+
* @returns {Buffer} - The tweaked public key hex string
|
|
217
221
|
* @throws {Error} - If the public key cannot be tweaked
|
|
218
222
|
*/
|
|
219
|
-
public static tweakPublicKey(compressedPubKeyHex: string):
|
|
220
|
-
if (compressedPubKeyHex.startsWith('0x')) {
|
|
223
|
+
public static tweakPublicKey(compressedPubKeyHex: string | Buffer): Buffer {
|
|
224
|
+
if (typeof compressedPubKeyHex === 'string' && compressedPubKeyHex.startsWith('0x')) {
|
|
221
225
|
compressedPubKeyHex = compressedPubKeyHex.slice(2);
|
|
222
226
|
}
|
|
223
227
|
|
|
228
|
+
if (typeof compressedPubKeyHex !== 'string') {
|
|
229
|
+
compressedPubKeyHex = compressedPubKeyHex.toString('hex');
|
|
230
|
+
}
|
|
231
|
+
|
|
224
232
|
// Convert the compressed public key hex string to a Point on the curve
|
|
225
233
|
let P = Point.fromHex(compressedPubKeyHex);
|
|
226
234
|
|
|
227
235
|
// Ensure the point has an even y-coordinate
|
|
228
|
-
if (
|
|
236
|
+
if ((P.y & 1n) !== 0n) {
|
|
229
237
|
// Negate the point to get an even y-coordinate
|
|
230
238
|
P = P.negate();
|
|
231
239
|
}
|
|
@@ -235,13 +243,13 @@ export class EcKeyPair {
|
|
|
235
243
|
|
|
236
244
|
// Compute the tweak t = H_tapTweak(x)
|
|
237
245
|
const tHash = taggedHash('TapTweak', Buffer.from(x));
|
|
238
|
-
const t =
|
|
246
|
+
const t = mod(BigInt('0x' + Buffer.from(tHash).toString('hex')), CURVE.n);
|
|
239
247
|
|
|
240
248
|
// Compute Q = P + t*G (where G is the generator point)
|
|
241
|
-
const Q = P.add(Point.BASE.
|
|
249
|
+
const Q = P.add(Point.BASE.mul(t));
|
|
242
250
|
|
|
243
251
|
// Return the tweaked public key in compressed form (hex string)
|
|
244
|
-
return Q.
|
|
252
|
+
return Buffer.from(Q.toRawBytes(true));
|
|
245
253
|
}
|
|
246
254
|
|
|
247
255
|
/**
|
|
@@ -417,6 +425,6 @@ export class EcKeyPair {
|
|
|
417
425
|
const privKey = fromSeed.privateKey;
|
|
418
426
|
if (!privKey) throw new Error('Failed to generate key pair');
|
|
419
427
|
|
|
420
|
-
return this.ECPair.fromPrivateKey(privKey, { network });
|
|
428
|
+
return this.ECPair.fromPrivateKey(Buffer.from(privKey), { network });
|
|
421
429
|
}
|
|
422
430
|
}
|
package/src/keypair/Wallet.ts
CHANGED
|
@@ -68,10 +68,7 @@ export class Wallet {
|
|
|
68
68
|
this._legacy = EcKeyPair.getLegacyAddress(this._keypair, this.network);
|
|
69
69
|
this._segwitLegacy = EcKeyPair.getLegacySegwitAddress(this._keypair, this.network);
|
|
70
70
|
|
|
71
|
-
this._tweakedKey =
|
|
72
|
-
EcKeyPair.tweakPublicKey(this._keypair.publicKey.toString('hex')),
|
|
73
|
-
'hex',
|
|
74
|
-
);
|
|
71
|
+
this._tweakedKey = EcKeyPair.tweakPublicKey(this._keypair.publicKey.toString('hex'));
|
|
75
72
|
|
|
76
73
|
this._bufferPubKey = this._keypair.publicKey;
|
|
77
74
|
this._address = new Address(this._keypair.publicKey);
|
|
@@ -37,13 +37,9 @@ export class BitcoinUtils {
|
|
|
37
37
|
|
|
38
38
|
return Buffer.from(array);
|
|
39
39
|
} else {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
randomValues.push(Math.floor(Math.random() * 256));
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
return Buffer.from(randomValues);
|
|
40
|
+
throw new Error(
|
|
41
|
+
'No secure random number generator available. Please upgrade your environment.',
|
|
42
|
+
);
|
|
47
43
|
}
|
|
48
44
|
}
|
|
49
45
|
|