UNPKG

@okxweb3/coin-bitcoin

Version:

@ok/coin-bitcoin is a Bitcoin SDK for building Web3 wallets and applications. It supports BTC, BSV, DOGE, LTC, and TBTC, enabling private key management, transaction signing, address generation, and inscriptions like BRC-20, Runes, CAT, and Atomicals.

49 lines 2.07 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.taprootSignSighash = exports.taprootTweakPubkey = exports.taprootTweakPrivKey = void 0; const crypto_lib_1 = require("@okxweb3/crypto-lib"); const coin_base_1 = require("@okxweb3/coin-base"); const txBuild_1 = require("./txBuild"); const secp256k1 = crypto_lib_1.signUtil.schnorr.secp256k1; const schnorr = secp256k1.schnorr; const ProjPoint = secp256k1.secp256k1.ProjectivePoint; const CURVE_ORDER = secp256k1.secp256k1.CURVE.n; function tapTweak(a, b) { const u = schnorr.utils; const t = u.taggedHash('TapTweak', a, b); const tn = u.bytesToNumberBE(t); if (tn >= CURVE_ORDER) throw new Error('tweak higher than curve order'); return tn; } function taprootTweakPrivKey(privKey, merkleRoot = new Uint8Array()) { const u = schnorr.utils; const seckey0 = u.bytesToNumberBE(privKey); const P = ProjPoint.fromPrivateKey(seckey0); const seckey = P.hasEvenY() ? seckey0 : u.mod(-seckey0, CURVE_ORDER); const xP = u.pointToBytes(P); const t = tapTweak(xP, merkleRoot); return u.numberToBytesBE(u.mod(seckey + t, CURVE_ORDER), 32); } exports.taprootTweakPrivKey = taprootTweakPrivKey; function taprootTweakPubkey(pubKey, h) { if (!h) h = new Uint8Array(); const u = schnorr.utils; const t = tapTweak(pubKey, h); const P = u.lift_x(u.bytesToNumberBE(pubKey)); const Q = P.add(ProjPoint.fromPrivateKey(t)); const parity = Q.hasEvenY() ? 0 : 1; return [u.pointToBytes(Q), parity]; } exports.taprootTweakPubkey = taprootTweakPubkey; function taprootSignSighash(privKey, sighashes, aux) { const tweakedPrivKey = taprootTweakPrivKey(coin_base_1.base.fromHex((0, txBuild_1.privateKeyFromWIF)(privKey))); const signatures = sighashes.map((sighash) => { const signature = Buffer.from(schnorr.sign(sighash, tweakedPrivKey, aux)); return coin_base_1.base.toHex(signature); }); return signatures; } exports.taprootSignSighash = taprootSignSighash; //# sourceMappingURL=taproot.js.map