import { AbstractContract } from './abstractContract.js'; import { Artifact } from './types/artifact.js'; import { ByteString, PubKey, SHPreimage, Sig } from './types/index.js'; import { Script } from './types/script.js'; import { ExtUtxo, Optional, UTXO } from '../globalTypes.js'; import { Contextual, InputContext, IContext } from './types/context.js'; import { Int32, SigHashType, OpcatState, SupportedParamType } from './types/primitives.js'; import { BacktraceInfo } from './types/structs.js'; import { Arguments } from './types/abi.js'; /** * Used to invoke public methods of a contract, corresponding to the witness of a Taproot input */ interface MethodCallData { method: string; args: SupportedParamType[]; unlockingScript: Script; } /** * The main contract class. To write a contract, extend this class as such: * @onchain * @example * ```ts * class YourSmartContract extends SmartContract { * // your smart contract code here * } * ``` * @category SmartContract */ export declare class SmartContract extends AbstractContract { /** * Bitcoin Contract Artifact */ static artifact: Artifact; private static newFromCreate; static readonly tags: string[]; /** * * @ignore */ static stateType?: string; /** * * @ignore */ getStateType(): string | undefined; /** * Get the data of the contract. * @returns the data of the contract */ get data(): ByteString; /** * Set the data of the contract. * Only stateless contracts (without stateType) can set data. * Stateful contracts should use the state property instead. * @param value the data to set */ set data(value: ByteString); utxo?: ExtUtxo; /** * The state of the contract UTXO, usually committed to the first OP_RETURN output, is revealed when spending. * @onchain */ state: StateT; /** * Raw data for stateless contracts (contracts without stateType). * For stateful contracts, use the `state` property instead. * Access this field through the `data` getter/setter. * @private * @onchain */ private _data; private contractHeader; /** * Locking script corresponding to the SmartContract */ readonly lockingScript: Script; private _lockingScriptHexWithoutHeader; /** * This function is usually called on the frontend. * The contract class needs to call this function before instantiating. * @param artifact a contract artifact json object */ static loadArtifact(artifact: Artifact): typeof SmartContract; static get artifactHexMD5(): string; constructor(...args: SupportedParamType[]); /** * @ignore * @param args */ private initLockingScript; private updateLockingScript; /** * Used to create an instance of a smart contract. If your smart contract has a base class, you must use this method to instantiate it. * @param this * @param args * @returns */ static create): InstanceType; }>(this: T, ...args: ConstructorParameters): InstanceType; /** * Using the [OP_PUSH_TX]{@link https://medium.com/@xiaohuiliu/op-push-tx-3d3d279174c1} technique, check if `shPreimage` is the preimage of the current transaction. * @onchain * @param txPreimage The format of the preimage is [specified]{@link https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki#signature-validation-rules} * @returns true if `shPreimage` is the preimage of the current transaction. Otherwise false. */ checkSHPreimage(shPreimage: SHPreimage): boolean; /** * A built-in function verifies an Schnorr signature. It takes two inputs from the stack, a public key (on top of the stack) and an Schnorr signature in its DER_CANONISED format concatenated with sighash flags. * It outputs true or false on the stack based on whether the signature check passes or fails. [see]{@link https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki#specification} * @onchain * @category Signature Verification */ checkSig(signature: Sig, publickey: PubKey, errorMsg?: string): boolean; /** * Verifies an ECDSA signature with an explicit sighash flag check. * This method validates that the signature's trailing sighash flag byte matches * the explicitly provided flag parameter before performing the signature verification. * * @param signature - The signature to verify (DER encoded with sighash flag appended) * @param publickey - The public key to verify the signature against * @param sigHashFlag - The expected sighash flag value (e.g., 1n for ALL, 2n for NONE, 129n for ANYONECANPAY_ALL) * @returns true if the flag matches AND the signature is valid, false otherwise * @onchain * @category Signature Verification */ checkSigWithFlag(signature: Sig, publickey: PubKey, sigHashFlag: bigint): boolean; /** * Compares the first signature against each public key until it finds an ECDSA match. * Starting with the subsequent public key, it compares the second signature against * each remaining public key until it finds an ECDSA match. The process is repeated * until all signatures have been checked or not enough public keys remain * to produce a successful result. All signatures need to match a public key. * Because public keys are not checked again if they fail any signature comparison, * signatures must be placed in the scriptSig using the same order as their corresponding * public keys were placed in the scriptPubKey or redeemScript. If all signatures are * valid, 1 is returned, 0 otherwise. Due to a bug, one extra unused value is removed from the stack. * @onchain * @category Signature Verification */ checkMultiSig(signatures: Sig[], publickeys: PubKey[]): boolean; /** * Verifies an ECDSA signature against an explicit message and public key. * Unlike checkSig which uses the transaction preimage as the implicit message, * checkDataSig allows verifying signatures on arbitrary data. * * Uses OP_CHECKSIGFROMSTACK (0xba) under the hood. * Stack order: (bottom to top) * * @param signature - The signature to verify (DER encoded with sighash type) * @param message - The message that was signed (will be SHA256 hashed once) * @param publickey - The public key to verify the signature against * @returns true if the signature is valid, false otherwise * @onchain * @category Signature Verification */ checkDataSig(signature: Sig, message: ByteString, publickey: PubKey): boolean; /** * A built-in function to create an [change output]{@link https://en.bitcoin.it/wiki/Change}. * @onchain * @returns */ buildChangeOutput(): ByteString; /** * A built-in function to create all outputs added by `appendStateOutput()` * @onchain * @returns an output containing the new state */ /** * Serializes the contract state into a ByteString. * @template T - Type of the contract state extending OpcatState * @param this - Constructor reference for type inference * @param state - The contract state object to serialize * @returns Serialized state as ByteString * @throws Error if artifact is not loaded or state type is not defined */ static serializeState(this: { new (...args: any[]): SmartContract; }, state: T): ByteString; /** * Deserializes the contract state from a ByteString. * @template T - The type of the contract state extending OpcatState * @param serializedState - The serialized state as a ByteString * @returns The deserialized state object of type T * @throws Error if artifact is not loaded or state type is not defined */ static deserializeState(this: { new (...args: any[]): SmartContract; }, serializedState: ByteString): T; /** * Computes the SHA-256 hash of the serialized contract state. * * @template T - Type extending OpcatState * @param state - The contract state to hash * @returns ByteString containing the SHA-256 hash of the serialized state * @override */ static stateHash(this: { new (...args: any[]): SmartContract; }, state: T): ByteString; /** * check state of the input. * option in the `@method()` decorator to false. * @onchain * @param inputIndex index of the input * @param serializedState the state of the input * @returns success if stateHash is valid */ checkInputState(inputIndex: Int32, serializedState: ByteString): boolean; /** * The current Extended PSBT object that is spending this contract instance. */ private _curPsbt?; /** * The input index in the current Extended PSBT object that is spending this contract instance. */ private _curInputIndex?; /** * Get the context details of the current PSBT input in which this contract is called or spent. * @onchain */ get ctx(): IContext; /** * Mark the contract instance as spent from the input of the PSBT. * @param psbt * @param inputIndex */ spentFromInput(psbt: Contextual, inputIndex: number): void; /** * Gets the PSBT (Partially Signed Bitcoin Transaction) that was used to spend this contract. * Returns undefined if no spending transaction exists. */ get spentPsbt(): Contextual | undefined; /** * Sets the signature hash type for the current input in the PSBT. * @param sigHashType - The signature hash type to set */ setSighashType(sigHashType: SigHashType): void; /** * Get the context of the current PSBT input in which this contract is called or spent. * @returns the context of the current PSBT input */ get inputContext(): InputContext; private _abiCoder; private _methodCall?; /** * Extends method arguments with context if needed and encodes them into a unlocking script. * @ignore * @param method - The method name to call. * @param args - The arguments to pass to the method. * @param autoCheckInputState - Whether to automatically check input state before injection. */ extendMethodArgs(method: string, args: SupportedParamType[], autoCheckInputState: boolean): void; /** * Checks if the given method is a public function in the smart contract. * @param method - The method name to check. * @returns True if the method is a public function, false otherwise. */ isPubFunction(method: string): boolean; /** * @ignore * Checks if the current instance is a smart contract. * @returns {boolean} Always returns true indicating this is a smart contract. */ isSmartContract(): boolean; private _shouldInjectCtx; private _autoInject; private _checkPsbtBinding; private _checkState; /** * Get the change info of the change output for current psbt. * @onchain * @returns the change info of current psbt */ get changeInfo(): import("./types/structs.js").ChangeInfo; /** * A set of functions for debugging contracts, which can only be called in `@method` methods. * @onchain */ get debug(): { diffOutputs: (outputsByte: ByteString) => void; }; /** * Check the outputs with the context of current transaction. * @param outputs the expected serialized outputs of the current transaction * @returns true if the outputs are not consistent with the transaction context, otherwise false. * @onchain */ checkOutputs(outputs: ByteString): boolean; /** * Returns the raw arguments from the call data of the smart contract. * @returns The raw arguments extracted from the call data. */ getUnlockingScript(): Script; /** * Gets the method call data for the current smart contract. * @throws {Error} If no method call is found. * @returns {MethodCallData} The method call data object. */ getCallData(): MethodCallData; /** * Transform unlocking script from the testimony into arguments previously used to invoke the contract. * @ignore * @param _args * @param _method */ unlockingScriptToCallData(_args: Script, _method: string): Arguments; /** * Checks a locktime parameter with the transaction's locktime. * There are two times of nLockTime: lock-by-blockheight and lock-by-blocktime, * distinguished by whether nLockTime < LOCKTIME_THRESHOLD = 500000000 * * See the corresponding code on bitcoin core: * https://github.com/bitcoin/bitcoin/blob/ffd75adce01a78b3461b3ff05bcc2b530a9ce994/src/script/interpreter.cpp#L1129 * * See the bip65 for specification * https://github.com/bitcoin/bips/blob/master/bip-0065.mediawiki#summary * @onchain * @param {BN} nLockTime the locktime read from the script * @return {boolean} true if the transaction's locktime is less than or equal to * the transaction's locktime */ timeLock(nLockTime: Int32): boolean; /** * Check whether the contract can be traced back to the genesis outpoint. * @param backtraceInfo the backtrace info to verify, including prevTx and prevPrevTx informations * @param genesisOutpoint expected genesis outpoint to be traced back to * @returns true if the contract can be backtraced to the genesis outpoint. Otherwise false. * @onchain * @category Backtrace */ backtraceToOutpoint(backtraceInfo: BacktraceInfo, genesisOutpoint: ByteString): boolean; /** * Check whether the contract can be traced back to the genesis script. * @param backtraceInfo the backtrace info to verify, including prevTx and prevPrevTx informations * @param genesisScript expected genesis script to be traced back to * @returns true if the contract can be backtraced to the genesis script. Otherwise false. * @onchain * @category Backtrace */ backtraceToScript(backtraceInfo: BacktraceInfo, genesisScript: ByteString): boolean; /** * Get a new contract instance with the new state. * @param newState the new state * @returns the new covenant */ next(newState: StateT): this; /** * Binds the smart contract to a UTXO by verifying and setting its script. * @param utxo - The UTXO to bind to (script field is optional) * @returns The contract instance for chaining * @throws Error if the UTXO's script exists and doesn't match the contract's locking script */ bindToUtxo(utxo: Optional | Optional): this; /** * Checks if the contract has state by verifying if the state object exists and is not empty. * @returns {boolean} True if the contract has state, false otherwise. */ static isStateful(): boolean; } export {}; //# sourceMappingURL=smartContract.d.ts.map