import { SmartContractLib } from '../smartContractLib.js'; import { PubKey, ByteString, Sig, Int32, UInt32, PrivKey, SigHashPreimage } from '../types/primitives.js'; import { SHPreimage, SpentScriptHashes, SpentAmounts, Prevouts, Outpoint, SpentDataHashes } from '../types/structs.js'; /** * Utility class for Bitcoin smart contract context operations. * Provides methods for: * - ECDSA signature generation and verification * - Transaction preimage serialization and validation * - Prevouts and spent data verification * - Lock time checking * - Various cryptographic and serialization utilities * * Contains precomputed cryptographic constants for optimized signing operations. * All methods are static and can be used without class instantiation. * @category Library * @onchain */ export declare class ContextUtils extends SmartContractLib { static readonly privKey: PrivKey; static readonly pubKey: PubKey; static readonly invK: bigint; static readonly r: bigint; static readonly rBigEndian: ByteString; /** * Normalizes a value to be within the range [0, modulus) by taking modulo and ensuring positivity. * @param k - The value to normalize * @param modulus - The modulus value * @returns The normalized value (k mod modulus) guaranteed to be non-negative */ static normalize(k: bigint, modulus: bigint): bigint; /** * Generates a DER-encoded ECDSA signature from given parameters. * * @param h - The message hash to sign (bigint) * @param privKey - The private key to sign with (PrivKey) * @param inverseK - The inverse of the nonce value (bigint) * @param r - The r component of the signature (bigint) * @param rBigEndian - The r component in big-endian byte format (ByteString) * @param sigHashType - The signature hash type (ByteString) * @returns A DER-encoded signature (Sig) * * @remarks * - Normalizes the s value to be in the lower range of the curve order * - Conforms to strict DER encoding rules * - Handles endianness conversion for signature components */ static sign(h: bigint, privKey: PrivKey, inverseK: bigint, r: bigint, rBigEndian: ByteString, sigHashType: ByteString): Sig; /** * Converts a big-endian unsigned byte string to a signed integer. * @param b The input byte string in big-endian format. * @returns The converted Int32 value. */ static fromBEUnsigned(b: ByteString): Int32; /** * Checks and signs a SHPreimage by serializing it, hashing, and creating a signature. * * @param shPreimage - The SHPreimage to be checked and signed. * @param sigHashType - The signature hash type to use for signing. * @returns The generated signature for the provided SHPreimage. */ static checkSHPreimage(shPreimage: SHPreimage, sigHashType: ByteString): Sig; /** * Serializes a SHPreimage object into a SigHashPreimage. * Validates all fields of the input SHPreimage meet required specifications before serialization. * @param shPreimage - The SHPreimage object containing transaction preimage data * @returns A SigHashPreimage constructed from the validated and concatenated fields * @throws If any field validation fails (invalid length, negative value, or unsupported sigHashType) */ static serializeSHPreimage(shPreimage: SHPreimage): SigHashPreimage; /** * Extract outpoint directly from SHPreimage. * Use this for current input's outpoint instead of slicing from prevouts. * @param shPreimage - The SHPreimage containing the outpoint field * @returns The Outpoint struct with txHash and outputIndex */ static getOutpoint(shPreimage: SHPreimage): Outpoint; /** * Verify that the prevouts context passed in by the user is authentic * @param prevouts prevouts context passed in by the user that need to be verified * @param prevout prevout context passed in by the user that need to be verified * @param t_hashPrevouts hashPrevouts in preimage which is trustable * @param t_inputIndex the index of the input, which is trustable * @returns the number of inputs, which is trustable */ static checkPrevouts(prevouts: Prevouts, t_hashPrevouts: ByteString, t_inputIndex: UInt32, t_inputCount: Int32): Outpoint; /** * Check if the spent scripts array passed in matches the shaSpentScripts * @param spentScriptHashes array of spent scripts passed in that need to be verified * @param t_hashSpentScripts the hash of the merged spent scripts, which comes from preimage and is trustable * @param t_inputCount must be trustable, the number of inputs */ static checkSpentScripts(spentScriptHashes: SpentScriptHashes, t_hashSpentScripts: ByteString, t_inputCount: bigint): void; /** * Check if the spent amounts array passed in matches the hashSpentAmounts * @param spentAmounts array of spent amounts passed in that need to be verified * @param hashSpentAmounts the hash of the merged spent amounts, which comes from preimage and is trustable */ static checkSpentAmounts(spentAmounts: SpentAmounts, hashSpentAmounts: ByteString): Int32; /** * Verifies the integrity of spent data hashes against the provided transaction hash and input count. * * @param spentDataHashes - The hashes of spent data to be verified * @param hashSpentDataHashes - The expected hash of all spent data hashes * @param inputCount - The expected number of inputs (must match the length of spentDataHashes) * @throws Throws an error if hash verification fails or if input count doesn't match */ static checkSpentDataHashes(spentDataHashes: SpentDataHashes, hashSpentDataHashes: ByteString, inputCount: bigint): void; /** * Retrieves the spent script hash for a specific input index from the given spent scripts buffer. * @param spentScriptHashes - Buffer containing all spent script hashes * @param inputIndex - Index of the input to retrieve the spent script hash for * @returns The spent script hash for the specified input index as a 32-byte string */ static getSpentScriptHash(spentScriptHashes: SpentScriptHashes, inputIndex: Int32): ByteString; /** * Retrieves the spent amount for a specific input index from the given spent amounts buffer. * @param spentAmounts - Buffer containing all spent amounts in little-endian format * @param inputIndex - Index of the input to retrieve the spent amount for * @returns The spent amount for the specified input index as a 32-bit integer */ static getSpentAmount(spentAmounts: SpentAmounts, inputIndex: Int32): Int32; /** * Retrieves the data hash for a specific input index from spent data hashes. * @param spentDataHashes - The collection of spent data hashes * @param inputIndex - The index of the input to retrieve * @returns The 32-byte data hash corresponding to the specified input index */ static getSpentDataHash(spentDataHashes: SpentDataHashes, inputIndex: Int32): ByteString; /** * Checks if the lock time conditions are met for a given preimage and lock time. * @param shPreimage The preimage containing lock time and sequence values. * @param nlockTime The required minimum lock time to validate against. * @returns True if the lock time conditions are satisfied, false otherwise. */ static checknLockTime(shPreimage: SHPreimage, nlockTime: UInt32): boolean; } //# sourceMappingURL=contextUtils.d.ts.map