@dynamic-labs-wallet/btc-utils 1.0.99 → 1.0.101
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/index.cjs
CHANGED
|
@@ -1455,9 +1455,6 @@ function _type_of(obj) {
|
|
|
1455
1455
|
* Computes the BIP-322 message hash for a given message and public key,
|
|
1456
1456
|
* returning the result as a hex string.
|
|
1457
1457
|
*
|
|
1458
|
-
* This function implements the BIP-322 hash computation for
|
|
1459
|
-
* native_segwit and taproot address types.
|
|
1460
|
-
*
|
|
1461
1458
|
* @param message - The raw message to be signed
|
|
1462
1459
|
* @param publicKey - The public key (hex string, Buffer, or Uint8Array)
|
|
1463
1460
|
* @param addressType - The address type (BitcoinAddressType enum or 'native_segwit', 'taproot')
|
|
@@ -1466,70 +1463,11 @@ function _type_of(obj) {
|
|
|
1466
1463
|
* @returns The BIP-322 message hash as a hex string
|
|
1467
1464
|
*/ var computeBip322HashHex = function(message, publicKey, addressType) {
|
|
1468
1465
|
var network = arguments.length > 3 && arguments[3] !== void 0 ? arguments[3] : core.BitcoinNetwork.MAINNET, ecc = arguments.length > 4 ? arguments[4] : void 0;
|
|
1469
|
-
// Convert string network to BitcoinNetwork enum
|
|
1470
1466
|
var btcNetwork = toBitcoinNetwork(network);
|
|
1471
|
-
// Convert string addressType to enum
|
|
1472
1467
|
var addressTypeEnum = toBitcoinAddressType(addressType);
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
}
|
|
1477
|
-
// Convert publicKey to Buffer
|
|
1478
|
-
var pubKeyBuffer;
|
|
1479
|
-
if (typeof publicKey === 'string') {
|
|
1480
|
-
pubKeyBuffer = Buffer.from(publicKey.replace(/^0x/, ''), 'hex');
|
|
1481
|
-
} else {
|
|
1482
|
-
pubKeyBuffer = toBuffer(publicKey);
|
|
1483
|
-
}
|
|
1484
|
-
// Compress public key if uncompressed
|
|
1485
|
-
pubKeyBuffer = normalizeForCompressed(pubKeyBuffer);
|
|
1486
|
-
// Generate address from public key
|
|
1487
|
-
var address = publicKeyToBitcoinAddress(pubKeyBuffer, addressTypeEnum, btcNetwork, ecc);
|
|
1488
|
-
// Convert address to scriptPubKey using bip322-js
|
|
1489
|
-
var scriptPubKey = bip322Js.Address.convertAdressToScriptPubkey(address);
|
|
1490
|
-
// Create the BIP-322 to_spend transaction
|
|
1491
|
-
var toSpendTx = bip322Js.BIP322.buildToSpendTx(message, scriptPubKey);
|
|
1492
|
-
// Create the to_sign PSBT
|
|
1493
|
-
var toSignPsbt = bip322Js.BIP322.buildToSignTx(toSpendTx.getId(), scriptPubKey);
|
|
1494
|
-
// Get the transaction from the PSBT
|
|
1495
|
-
var txToSign = toSignPsbt.__CACHE.__TX;
|
|
1496
|
-
// Get bitcoinjs-lib network
|
|
1497
|
-
var bitcoinNetwork = getBitcoinNetwork(btcNetwork);
|
|
1498
|
-
// Compute sighash based on address type
|
|
1499
|
-
if (addressTypeEnum === core.BitcoinAddressType.TAPROOT) {
|
|
1500
|
-
// Taproot (BIP-341) uses hashForWitnessV1 with SIGHASH_DEFAULT
|
|
1501
|
-
var prevOutScripts = [
|
|
1502
|
-
scriptPubKey
|
|
1503
|
-
];
|
|
1504
|
-
var values = [
|
|
1505
|
-
0
|
|
1506
|
-
]; // Use number, not BigInt - BIP-322 uses 0 value
|
|
1507
|
-
var hash = txToSign.hashForWitnessV1(0, prevOutScripts, values, bitcoin__namespace.Transaction.SIGHASH_DEFAULT);
|
|
1508
|
-
return Buffer.from(hash).toString('hex');
|
|
1509
|
-
} else {
|
|
1510
|
-
// SegWit (BIP-143) uses hashForWitnessV0 with SIGHASH_ALL
|
|
1511
|
-
// Create a new transaction for signing
|
|
1512
|
-
var txToSignSegwit = new bitcoin__namespace.Transaction();
|
|
1513
|
-
txToSignSegwit.version = 0;
|
|
1514
|
-
txToSignSegwit.locktime = 0;
|
|
1515
|
-
var prevTxId = toSpendTx.getId();
|
|
1516
|
-
var prevHash = Buffer.from(prevTxId, 'hex').reverse();
|
|
1517
|
-
txToSignSegwit.addInput(new Uint8Array(prevHash), 0, 0);
|
|
1518
|
-
txToSignSegwit.addOutput(new Uint8Array(Buffer.from([
|
|
1519
|
-
0x6a
|
|
1520
|
-
])), BigInt(0));
|
|
1521
|
-
// Build P2PKH script code from pubkey
|
|
1522
|
-
var p2pkh = bitcoin__namespace.payments.p2pkh({
|
|
1523
|
-
pubkey: new Uint8Array(pubKeyBuffer),
|
|
1524
|
-
network: bitcoinNetwork
|
|
1525
|
-
});
|
|
1526
|
-
var scriptCode = p2pkh.output;
|
|
1527
|
-
if (!scriptCode) {
|
|
1528
|
-
throw new Error('Failed to generate scriptCode for BIP-322 hash');
|
|
1529
|
-
}
|
|
1530
|
-
var hash1 = txToSignSegwit.hashForWitnessV0(0, scriptCode, BigInt(0), bitcoin__namespace.Transaction.SIGHASH_ALL);
|
|
1531
|
-
return Buffer.from(hash1).toString('hex');
|
|
1532
|
-
}
|
|
1468
|
+
var normalizedPublicKey = typeof publicKey === 'string' ? publicKey.replace(/^0x/, '') : publicKey;
|
|
1469
|
+
var formattedMessage = calculateBip322Hash(message, normalizedPublicKey, addressTypeEnum, btcNetwork, ecc).formattedMessage;
|
|
1470
|
+
return Buffer.from(formattedMessage).toString('hex');
|
|
1533
1471
|
};
|
|
1534
1472
|
|
|
1535
1473
|
exports.calculateBip322Hash = calculateBip322Hash;
|
package/index.esm.js
CHANGED
|
@@ -1435,9 +1435,6 @@ function _type_of(obj) {
|
|
|
1435
1435
|
* Computes the BIP-322 message hash for a given message and public key,
|
|
1436
1436
|
* returning the result as a hex string.
|
|
1437
1437
|
*
|
|
1438
|
-
* This function implements the BIP-322 hash computation for
|
|
1439
|
-
* native_segwit and taproot address types.
|
|
1440
|
-
*
|
|
1441
1438
|
* @param message - The raw message to be signed
|
|
1442
1439
|
* @param publicKey - The public key (hex string, Buffer, or Uint8Array)
|
|
1443
1440
|
* @param addressType - The address type (BitcoinAddressType enum or 'native_segwit', 'taproot')
|
|
@@ -1446,70 +1443,11 @@ function _type_of(obj) {
|
|
|
1446
1443
|
* @returns The BIP-322 message hash as a hex string
|
|
1447
1444
|
*/ var computeBip322HashHex = function(message, publicKey, addressType) {
|
|
1448
1445
|
var network = arguments.length > 3 && arguments[3] !== void 0 ? arguments[3] : BitcoinNetwork.MAINNET, ecc = arguments.length > 4 ? arguments[4] : void 0;
|
|
1449
|
-
// Convert string network to BitcoinNetwork enum
|
|
1450
1446
|
var btcNetwork = toBitcoinNetwork(network);
|
|
1451
|
-
// Convert string addressType to enum
|
|
1452
1447
|
var addressTypeEnum = toBitcoinAddressType(addressType);
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
}
|
|
1457
|
-
// Convert publicKey to Buffer
|
|
1458
|
-
var pubKeyBuffer;
|
|
1459
|
-
if (typeof publicKey === 'string') {
|
|
1460
|
-
pubKeyBuffer = Buffer.from(publicKey.replace(/^0x/, ''), 'hex');
|
|
1461
|
-
} else {
|
|
1462
|
-
pubKeyBuffer = toBuffer(publicKey);
|
|
1463
|
-
}
|
|
1464
|
-
// Compress public key if uncompressed
|
|
1465
|
-
pubKeyBuffer = normalizeForCompressed(pubKeyBuffer);
|
|
1466
|
-
// Generate address from public key
|
|
1467
|
-
var address = publicKeyToBitcoinAddress(pubKeyBuffer, addressTypeEnum, btcNetwork, ecc);
|
|
1468
|
-
// Convert address to scriptPubKey using bip322-js
|
|
1469
|
-
var scriptPubKey = Address.convertAdressToScriptPubkey(address);
|
|
1470
|
-
// Create the BIP-322 to_spend transaction
|
|
1471
|
-
var toSpendTx = BIP322.buildToSpendTx(message, scriptPubKey);
|
|
1472
|
-
// Create the to_sign PSBT
|
|
1473
|
-
var toSignPsbt = BIP322.buildToSignTx(toSpendTx.getId(), scriptPubKey);
|
|
1474
|
-
// Get the transaction from the PSBT
|
|
1475
|
-
var txToSign = toSignPsbt.__CACHE.__TX;
|
|
1476
|
-
// Get bitcoinjs-lib network
|
|
1477
|
-
var bitcoinNetwork = getBitcoinNetwork(btcNetwork);
|
|
1478
|
-
// Compute sighash based on address type
|
|
1479
|
-
if (addressTypeEnum === BitcoinAddressType.TAPROOT) {
|
|
1480
|
-
// Taproot (BIP-341) uses hashForWitnessV1 with SIGHASH_DEFAULT
|
|
1481
|
-
var prevOutScripts = [
|
|
1482
|
-
scriptPubKey
|
|
1483
|
-
];
|
|
1484
|
-
var values = [
|
|
1485
|
-
0
|
|
1486
|
-
]; // Use number, not BigInt - BIP-322 uses 0 value
|
|
1487
|
-
var hash = txToSign.hashForWitnessV1(0, prevOutScripts, values, bitcoin.Transaction.SIGHASH_DEFAULT);
|
|
1488
|
-
return Buffer.from(hash).toString('hex');
|
|
1489
|
-
} else {
|
|
1490
|
-
// SegWit (BIP-143) uses hashForWitnessV0 with SIGHASH_ALL
|
|
1491
|
-
// Create a new transaction for signing
|
|
1492
|
-
var txToSignSegwit = new bitcoin.Transaction();
|
|
1493
|
-
txToSignSegwit.version = 0;
|
|
1494
|
-
txToSignSegwit.locktime = 0;
|
|
1495
|
-
var prevTxId = toSpendTx.getId();
|
|
1496
|
-
var prevHash = Buffer.from(prevTxId, 'hex').reverse();
|
|
1497
|
-
txToSignSegwit.addInput(new Uint8Array(prevHash), 0, 0);
|
|
1498
|
-
txToSignSegwit.addOutput(new Uint8Array(Buffer.from([
|
|
1499
|
-
0x6a
|
|
1500
|
-
])), BigInt(0));
|
|
1501
|
-
// Build P2PKH script code from pubkey
|
|
1502
|
-
var p2pkh = bitcoin.payments.p2pkh({
|
|
1503
|
-
pubkey: new Uint8Array(pubKeyBuffer),
|
|
1504
|
-
network: bitcoinNetwork
|
|
1505
|
-
});
|
|
1506
|
-
var scriptCode = p2pkh.output;
|
|
1507
|
-
if (!scriptCode) {
|
|
1508
|
-
throw new Error('Failed to generate scriptCode for BIP-322 hash');
|
|
1509
|
-
}
|
|
1510
|
-
var hash1 = txToSignSegwit.hashForWitnessV0(0, scriptCode, BigInt(0), bitcoin.Transaction.SIGHASH_ALL);
|
|
1511
|
-
return Buffer.from(hash1).toString('hex');
|
|
1512
|
-
}
|
|
1448
|
+
var normalizedPublicKey = typeof publicKey === 'string' ? publicKey.replace(/^0x/, '') : publicKey;
|
|
1449
|
+
var formattedMessage = calculateBip322Hash(message, normalizedPublicKey, addressTypeEnum, btcNetwork, ecc).formattedMessage;
|
|
1450
|
+
return Buffer.from(formattedMessage).toString('hex');
|
|
1513
1451
|
};
|
|
1514
1452
|
|
|
1515
1453
|
export { calculateBip322Hash, calculateTaprootTweak, collectPSBTInputData, computeBip322HashHex, convertSignatureToDER, convertSignatureToTaprootBuffer, createLegacyAddress, createNativeSegWitAddress, createSegWitAddress, createTaprootAddress, doesInputBelongToAddress, encodeBip322Signature, extractPsbtSighashes, extractPublicKeyHex, getAddressTypeFromDerivationPath, getBitcoinNetwork, getDefaultRpcUrl, getFeeRates, getPublicKeyFromPrivateKey, getUTXOs, initEccLib, normalizeForCompressed, normalizeForTaproot, normalizePublicKey, privateKeyToWIF, publicKeyToBitcoinAddress, selectUTXOs, toBitcoinNetwork, toBuffer, wifToPrivateKey };
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dynamic-labs-wallet/btc-utils",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.101",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
7
7
|
"dependencies": {
|
|
8
|
-
"@dynamic-labs-wallet/core": "1.0.
|
|
8
|
+
"@dynamic-labs-wallet/core": "1.0.101",
|
|
9
9
|
"bitcoinjs-lib": "^7.0.0",
|
|
10
10
|
"bip322-js": "^3.0.0",
|
|
11
11
|
"@noble/hashes": "1.7.1",
|
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
import { BitcoinAddressType
|
|
1
|
+
import type { BitcoinAddressType } from '@dynamic-labs-wallet/core';
|
|
2
|
+
import { BitcoinNetwork } from '@dynamic-labs-wallet/core';
|
|
2
3
|
/**
|
|
3
4
|
* Computes the BIP-322 message hash for a given message and public key,
|
|
4
5
|
* returning the result as a hex string.
|
|
5
6
|
*
|
|
6
|
-
* This function implements the BIP-322 hash computation for
|
|
7
|
-
* native_segwit and taproot address types.
|
|
8
|
-
*
|
|
9
7
|
* @param message - The raw message to be signed
|
|
10
8
|
* @param publicKey - The public key (hex string, Buffer, or Uint8Array)
|
|
11
9
|
* @param addressType - The address type (BitcoinAddressType enum or 'native_segwit', 'taproot')
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"computeBip322HashHex.d.ts","sourceRoot":"","sources":["../../src/computeBip322HashHex/computeBip322HashHex.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"computeBip322HashHex.d.ts","sourceRoot":"","sources":["../../src/computeBip322HashHex/computeBip322HashHex.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AACpE,OAAO,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAK3D;;;;;;;;;;GAUG;AACH,eAAO,MAAM,oBAAoB,YACtB,MAAM,aACJ,MAAM,GAAG,MAAM,GAAG,UAAU,eAC1B,kBAAkB,GAAG,eAAe,GAAG,SAAS,YACpD,cAAc,GAAG,SAAS,GAAG,SAAS,QACzC,GAAG,KACR,MASF,CAAC"}
|