ContextUtils
scrypt-ts-opcat / ContextUtils
Class: ContextUtils
Defined in: packages/scrypt-ts-opcat/src/smart-contract/builtin-libs/contextUtils.ts:25
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.
Onchain
Extends
Constructors
new ContextUtils()
new ContextUtils(...
args
):ContextUtils
Defined in: packages/scrypt-ts-opcat/src/smart-contract/smartContractLib.ts:32
Parameters
args
...SupportedParamType
[]
Returns
Inherited from
Properties
args
args:
any
[] =[]
Defined in: packages/scrypt-ts-opcat/src/smart-contract/smartContractLib.ts:31
Inherited from
artifact
static
artifact:Artifact
Defined in: packages/scrypt-ts-opcat/src/smart-contract/smartContractLib.ts:16
Inherited from
invK
readonly
static
invK:bigint
=0xc8ffdbaa05d93aa4ede79ec58f06a72562048b775a3507c2bf44bde4f007c40an
Defined in: packages/scrypt-ts-opcat/src/smart-contract/builtin-libs/contextUtils.ts:35
privKey
readonly
static
privKey:PrivKey
Defined in: packages/scrypt-ts-opcat/src/smart-contract/builtin-libs/contextUtils.ts:30
pubKey
readonly
static
pubKey:PubKey
Defined in: packages/scrypt-ts-opcat/src/smart-contract/builtin-libs/contextUtils.ts:32
r
readonly
static
r:bigint
=0x1008ce7480da41702918d1ec8e6849ba32b4d65b1e40dc669c31a1e6306b266cn
Defined in: packages/scrypt-ts-opcat/src/smart-contract/builtin-libs/contextUtils.ts:38
rBigEndian
readonly
static
rBigEndian:ByteString
Defined in: packages/scrypt-ts-opcat/src/smart-contract/builtin-libs/contextUtils.ts:41
stateType?
static
optional
stateType:string
Defined in: packages/scrypt-ts-opcat/src/smart-contract/smartContractLib.ts:17
Inherited from
Methods
checknLockTime()
static
checknLockTime(shPreimage
,nlockTime
):boolean
Defined in: packages/scrypt-ts-opcat/src/smart-contract/builtin-libs/contextUtils.ts:316
Checks if the lock time conditions are met for a given preimage and lock time.
Parameters
shPreimage
The preimage containing lock time and sequence values.
nlockTime
UInt32
The required minimum lock time to validate against.
Returns
boolean
True if the lock time conditions are satisfied, false otherwise.
checkPrevouts()
static
checkPrevouts(prevouts
,t_hashPrevouts
,t_inputIndex
,t_inputCount
):Outpoint
Defined in: packages/scrypt-ts-opcat/src/smart-contract/builtin-libs/contextUtils.ts:199
Verify that the prevouts context passed in by the user is authentic
Parameters
prevouts
prevouts context passed in by the user that need to be verified
t_hashPrevouts
hashPrevouts in preimage which is trustable
t_inputIndex
UInt32
the index of the input, which is trustable
t_inputCount
Returns
the number of inputs, which is trustable
checkSHPreimage()
static
checkSHPreimage(shPreimage
,sigHashType
):Sig
Defined in: packages/scrypt-ts-opcat/src/smart-contract/builtin-libs/contextUtils.ts:128
Checks and signs a SHPreimage by serializing it, hashing, and creating a signature.
Parameters
shPreimage
The SHPreimage to be checked and signed.
sigHashType
The signature hash type to use for signing.
Returns
The generated signature for the provided SHPreimage.
checkSpentAmounts()
static
checkSpentAmounts(spentAmounts
,hashSpentAmounts
):Int32
Defined in: packages/scrypt-ts-opcat/src/smart-contract/builtin-libs/contextUtils.ts:240
Check if the spent amounts array passed in matches the shaSpentAmounts
Parameters
spentAmounts
array of spent amounts passed in that need to be verified
hashSpentAmounts
the hash of the merged spent amounts, which comes from preimage and is trustable
Returns
checkSpentDataHashes()
static
checkSpentDataHashes(spentDataHashes
,hashSpentDataHashes
,inputCount
):void
Defined in: packages/scrypt-ts-opcat/src/smart-contract/builtin-libs/contextUtils.ts:257
Verifies the integrity of spent data hashes against the provided transaction hash and input count.
Parameters
spentDataHashes
The hashes of spent data to be verified
hashSpentDataHashes
The expected hash of all spent data hashes
inputCount
bigint
The expected number of inputs (must match the length of spentDataHashes)
Returns
void
Throws
Throws an error if hash verification fails or if input count doesn't match
checkSpentScripts()
static
checkSpentScripts(spentScriptHashes
,t_hashSpentScripts
,t_inputCount
):void
Defined in: packages/scrypt-ts-opcat/src/smart-contract/builtin-libs/contextUtils.ts:224
Check if the spent scripts array passed in matches the shaSpentScripts
Parameters
spentScriptHashes
array of spent scripts passed in that need to be verified
t_hashSpentScripts
the hash of the merged spent scripts, which comes from preimage and is trustable
t_inputCount
bigint
must be trustable, the number of inputs
Returns
void
fromBEUnsigned()
static
fromBEUnsigned(b
):Int32
Defined in: packages/scrypt-ts-opcat/src/smart-contract/builtin-libs/contextUtils.ts:113
Converts a big-endian unsigned byte string to a signed integer.
Parameters
b
The input byte string in big-endian format.
Returns
The converted Int32 value.
getSpentAmount()
static
getSpentAmount(spentAmounts
,inputIndex
):Int32
Defined in: packages/scrypt-ts-opcat/src/smart-contract/builtin-libs/contextUtils.ts:288
Retrieves the spent amount for a specific input index from the given spent amounts buffer.
Parameters
spentAmounts
Buffer containing all spent amounts in little-endian format
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
getSpentDataHash()
static
getSpentDataHash(spentDataHashes
,inputIndex
):ByteString
Defined in: packages/scrypt-ts-opcat/src/smart-contract/builtin-libs/contextUtils.ts:302
Retrieves the data hash for a specific input index from spent data hashes.
Parameters
spentDataHashes
The collection of spent data hashes
inputIndex
The index of the input to retrieve
Returns
The 32-byte data hash corresponding to the specified input index
getSpentScriptHash()
static
getSpentScriptHash(spentScriptHashes
,inputIndex
):ByteString
Defined in: packages/scrypt-ts-opcat/src/smart-contract/builtin-libs/contextUtils.ts:274
Retrieves the spent script hash for a specific input index from the given spent scripts buffer.
Parameters
spentScriptHashes
Buffer containing all spent script hashes
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
loadArtifact()
static
loadArtifact(artifact
): typeofSmartContractLib
Defined in: packages/scrypt-ts-opcat/src/smart-contract/smartContractLib.ts:19
Parameters
artifact
Returns
typeof SmartContractLib
Inherited from
normalize()
static
normalize(k
,modulus
):bigint
Defined in: packages/scrypt-ts-opcat/src/smart-contract/builtin-libs/contextUtils.ts:51
Normalizes a value to be within the range [0, modulus) by taking modulo and ensuring positivity.
Parameters
k
bigint
The value to normalize
modulus
bigint
The modulus value
Returns
bigint
The normalized value (k mod modulus) guaranteed to be non-negative
serializeSHPreimage()
static
serializeSHPreimage(shPreimage
):SigHashPreimage
Defined in: packages/scrypt-ts-opcat/src/smart-contract/builtin-libs/contextUtils.ts:151
Serializes a SHPreimage object into a SigHashPreimage. Validates all fields of the input SHPreimage meet required specifications before serialization.
Parameters
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)
sign()
static
sign(h
,privKey
,inverseK
,r
,rBigEndian
,sigHashType
):Sig
Defined in: packages/scrypt-ts-opcat/src/smart-contract/builtin-libs/contextUtils.ts:73
Generates a DER-encoded ECDSA signature from given parameters.
Parameters
h
bigint
The message hash to sign (bigint)
privKey
The private key to sign with (PrivKey)
inverseK
bigint
The inverse of the nonce value (bigint)
r
bigint
The r component of the signature (bigint)
rBigEndian
The r component in big-endian byte format (ByteString)
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