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.

112 lines 4.38 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.tweakKey = exports.tapTweakHash = exports.tapleafHash = exports.findScriptPath = exports.toHashTree = exports.rootHashFromPath = exports.MAX_TAPTREE_DEPTH = exports.LEAF_VERSION_TAPSCRIPT = void 0; const buffer_1 = require("buffer"); const bcrypto = __importStar(require("../crypto")); const bufferutils_1 = require("../bufferutils"); const types_1 = require("../types"); const taproot_1 = require("../../taproot"); exports.LEAF_VERSION_TAPSCRIPT = 0xc0; exports.MAX_TAPTREE_DEPTH = 128; const isHashBranch = (ht) => 'left' in ht && 'right' in ht; function rootHashFromPath(controlBlock, leafHash) { if (controlBlock.length < 33) throw new TypeError(`The control-block length is too small. Got ${controlBlock.length}, expected min 33.`); const m = (controlBlock.length - 33) / 32; let kj = leafHash; for (let j = 0; j < m; j++) { const ej = controlBlock.slice(33 + 32 * j, 65 + 32 * j); if (kj.compare(ej) < 0) { kj = tapBranchHash(kj, ej); } else { kj = tapBranchHash(ej, kj); } } return kj; } exports.rootHashFromPath = rootHashFromPath; function toHashTree(scriptTree) { if ((0, types_1.isTapleaf)(scriptTree)) return { hash: tapleafHash(scriptTree) }; const hashes = [toHashTree(scriptTree[0]), toHashTree(scriptTree[1])]; hashes.sort((a, b) => a.hash.compare(b.hash)); const [left, right] = hashes; return { hash: tapBranchHash(left.hash, right.hash), left, right, }; } exports.toHashTree = toHashTree; function findScriptPath(node, hash) { if (isHashBranch(node)) { const leftPath = findScriptPath(node.left, hash); if (leftPath !== undefined) return [...leftPath, node.right.hash]; const rightPath = findScriptPath(node.right, hash); if (rightPath !== undefined) return [...rightPath, node.left.hash]; } else if (node.hash.equals(hash)) { return []; } return undefined; } exports.findScriptPath = findScriptPath; function tapleafHash(leaf) { const version = leaf.version || exports.LEAF_VERSION_TAPSCRIPT; return bcrypto.taggedHash('TapLeaf', buffer_1.Buffer.concat([buffer_1.Buffer.from([version]), serializeScript(leaf.output)])); } exports.tapleafHash = tapleafHash; function tapTweakHash(pubKey, h) { return bcrypto.taggedHash('TapTweak', buffer_1.Buffer.concat(h ? [pubKey, h] : [pubKey])); } exports.tapTweakHash = tapTweakHash; function tweakKey(pubKey, h) { if (!buffer_1.Buffer.isBuffer(pubKey)) return null; if (pubKey.length !== 32) return null; if (h && h.length !== 32) return null; const res = (0, taproot_1.taprootTweakPubkey)(pubKey, h); return { parity: res[1], x: buffer_1.Buffer.from(res[0]), }; } exports.tweakKey = tweakKey; function tapBranchHash(a, b) { return bcrypto.taggedHash('TapBranch', buffer_1.Buffer.concat([a, b])); } function serializeScript(s) { const varintLen = bufferutils_1.varuint.encodingLength(s.length); const buffer = buffer_1.Buffer.allocUnsafe(varintLen); bufferutils_1.varuint.encode(s.length, buffer); return buffer_1.Buffer.concat([buffer, s]); } //# sourceMappingURL=bip341.js.map