UNPKG

@covenance/dlc

Version:

Crypto and Bitcoin functions for Covenance DLC implementation

161 lines 4.89 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.hexToBytes = hexToBytes; exports.bytesToHex = bytesToHex; exports.pointToHex = pointToHex; exports.hexToPoint = hexToPoint; exports.signatureToHex = signatureToHex; exports.hexToSignature = hexToSignature; exports.adaptorSignatureToHex = adaptorSignatureToHex; exports.hexToAdaptorSignature = hexToAdaptorSignature; exports.sha256 = sha256; exports.sha256Hex = sha256Hex; exports.be32 = be32; exports.csvTimeDelay = csvTimeDelay; exports.encodeScriptNum = encodeScriptNum; const secp256k1_1 = require("./crypto/secp256k1"); /** * Converts a hex string to a Uint8Array * @param hex - The hex string to convert * @returns The Uint8Array representation of the hex string */ function hexToBytes(hex) { const bytes = new Uint8Array(hex.length / 2); for (let i = 0; i < hex.length; i += 2) { bytes[i / 2] = parseInt(hex.slice(i, i + 2), 16); } return bytes; } /** * Converts a Uint8Array to a hex string * @param bytes - The Uint8Array to convert * @returns The hex string representation of the Uint8Array */ function bytesToHex(bytes) { return Array.from(bytes) .map(b => b.toString(16).padStart(2, '0')) .join(''); } /** * Converts a Point to a hex string * @param point - The Point to convert * @returns The hex string representation of the Point */ function pointToHex(point) { return bytesToHex(point.toRawBytes(true)); } /** * Converts a hex string to a Point * @param hex - The hex string to convert * @returns The Point representation of the hex string */ function hexToPoint(hex) { return secp256k1_1.Point.fromHex(hex); } /** * Converts a Signature to a hex string representation * @param sig - The Signature to convert * @returns The hex string representation of the Signature */ function signatureToHex(sig) { return { R: pointToHex(sig.R), s: sig.s.toString(16) }; } /** * Converts a hex string representation to a Signature * @param hex - The hex string representation to convert * @returns The Signature representation of the hex string */ function hexToSignature(hex) { return { R: hexToPoint(hex.R), s: BigInt('0x' + hex.s) }; } /** * Converts an AdaptorSignature to a hex string representation * @param sig - The AdaptorSignature to convert * @returns The hex string representation of the AdaptorSignature */ function adaptorSignatureToHex(sig) { return { R_prime: pointToHex(sig.R_prime), s_prime: sig.s_prime.toString(16) }; } /** * Converts a hex string representation to an AdaptorSignature * @param hex - The hex string representation to convert * @returns The AdaptorSignature representation of the hex string */ function hexToAdaptorSignature(hex) { return { R_prime: hexToPoint(hex.R_prime), s_prime: BigInt('0x' + hex.s_prime) }; } /** * Computes the hash of a message using SHA256 * @param message - The message to hash * @returns The hash of the message */ async function sha256(message) { return secp256k1_1.utils.sha256(message); } /** * Computes the hash of a message using SHA256 and returns it as a hex string * @param message - The message to hash * @returns The hex string representation of the hash */ async function sha256Hex(message) { const hash = await sha256(hexToBytes(message)); return bytesToHex(hash); } /** * Copies a bigint to a Uint8Array as big-endian * @param dst - The Uint8Array to copy to * @param offset - The offset to copy to * @param n - The bigint to copy */ function be32(dst, offset, n) { for (let i = 31; i >= 0; i--) { dst[offset + i] = Number(n & 0xffn); n >>= 8n; } if (n) throw new RangeError('integer > 256 bits'); } /** * Convert seconds → BIP68 buffer. * Uses 512-second granularity, sets the type flag (bit 22). * Throws if out of range (> 0xffff * 512 seconds). * @param seconds - The number of seconds to convert * @returns The BIP68 buffer */ function csvTimeDelay(seconds) { if (!Number.isFinite(seconds) || seconds < 0) throw new RangeError("seconds ≥ 0"); const units = Math.ceil(seconds / 512); if (units > 0xffff) throw new RangeError("max 0xffff * 512s ≈ 388.5 days"); const TYPE_FLAG = 1 << 22; const value = TYPE_FLAG | units; const data = encodeScriptNum(value); // minimal ScriptNum return data; } function encodeScriptNum(n) { if (n === 0) return Buffer.alloc(0); // minimal zero const bytes = []; let v = n >>> 0; // n ≤ 0x0040ffff fits 32-bit while (v) { bytes.push(v & 0xff); v >>>= 8; } // little-endian magnitude if (bytes[bytes.length - 1] & 0x80) bytes.push(0x00); // keep positive (no sign bit set) return Buffer.from(bytes); } //# sourceMappingURL=utils.js.map