DefaultSmartContractService

class DefaultSmartContractService @JvmOverloads constructor(credentialService: CredentialService? = null, blockchainRegistry: BlockchainAnchorRegistry? = null, engines: EvaluationEngines = EvaluationEngines(), credentialResolver: ContractCredentialResolver? = null, trustedIssuerPolicy: TrustedIssuerPolicy? = null, verifyAnchorOnVerify: Boolean = false, limits: ContractStoreLimits = ContractStoreLimits()) : SmartContractService

Default in-memory implementation of SmartContractService.

This is a simple implementation suitable for testing and development. Production implementations should use persistent storage.

All state changes of one contract are serialised by a per-contract Mutex and written with a compare-and-set against the record that was read, so two concurrent executions of the same ACTIVE contract cannot both execute it. A slow credential issuance, anchor write or engine evaluation therefore only delays the one contract it belongs to, never an unrelated one; the mutex of a contract exists only while an operation on it is running or waiting.

Lifecycle rules.

  • A contract can only become ACTIVE (through activateContract or updateStatus) once bindContract bound a credential to it, and, when a blockchain registry is configured, an anchor.

  • updateStatus refuses ContractStatus.EXECUTED: a contract is executed only by executeContract, which evaluates its conditions first. A ExecutionModel.Manual contract has no engine; it executes when its terms contain no conditions (conditions cannot be evaluated automatically, so such a contract is refused rather than executed unchecked).

  • Re-binding replaces the previous credential and drops everything this service kept for it.

Parameters

credentialResolver

optional lookup for contract credentials that this instance did not issue itself; used by verifyContract.

trustedIssuerPolicy

optional extra policy deciding which issuers may vouch for a contract in verifyContract. Without it, the credential issuer must be a party to the contract or the issuer DID this service itself used when issuing the credential.

verifyAnchorOnVerify

when true, verifyContract also checks that the anchored payload attests to this contract and credential (default false). Fails closed: a contract with no anchor, or whose anchor's chain has no client in the registry, does not verify.

limits

capacity limits of the in-memory stores, see ContractStoreLimits.

Constructors

Link copied to clipboard
constructor(credentialService: CredentialService? = null, blockchainRegistry: BlockchainAnchorRegistry? = null, engines: EvaluationEngines = EvaluationEngines(), credentialResolver: ContractCredentialResolver? = null, trustedIssuerPolicy: TrustedIssuerPolicy? = null, verifyAnchorOnVerify: Boolean = false, limits: ContractStoreLimits = ContractStoreLimits())

Functions

Link copied to clipboard
open suspend override fun activateContract(contractId: String): Result<SmartContract>

Activate a contract (move from PENDING to ACTIVE).

Link copied to clipboard
open suspend override fun anchorContract(contract: SmartContract, credential: VerifiableCredential, chainId: String = "algorand:mainnet"): Result<AnchorRef>

Anchor contract to blockchain for audit trail.

Link copied to clipboard
open suspend override fun bindContract(contractId: String, issuerDid: String, issuerKeyId: String, chainId: String = "algorand:mainnet"): Result<BoundContract>

Bind a contract (issue credential and anchor to blockchain).

Link copied to clipboard
open suspend override fun createDraft(request: ContractDraftRequest): Result<SmartContract>

Create a contract draft.

Link copied to clipboard
open suspend override fun evaluateConditions(contract: SmartContract, inputData: JsonElement): Result<ConditionEvaluation>

Evaluate contract conditions.

Link copied to clipboard
open suspend override fun executeContract(contract: SmartContract, executionContext: ExecutionContext): Result<ExecutionResult>

Execute contract based on execution model.

Link copied to clipboard
suspend fun SmartContractService.executeEvent(contract: SmartContract, vararg eventData: Pair<String, Any?>): Result<ExecutionResult>

Execute a contract with event data supplied as key/value pairs.

Link copied to clipboard
suspend fun SmartContractService.executeParametric(contract: SmartContract, vararg triggerData: Pair<String, Any?>): Result<ExecutionResult>

Execute a contract with parametric trigger data supplied as key/value pairs.

Link copied to clipboard
open suspend override fun getContract(contractId: String): Result<SmartContract>

Get contract by ID.

Link copied to clipboard
open suspend override fun issueContractCredential(contract: SmartContract, issuerDid: String, issuerKeyId: String): Result<VerifiableCredential>

Issue verifiable credential for the contract.

Link copied to clipboard
Link copied to clipboard

Recorded status changes (including reason and metadata) for contractId, oldest first.

Link copied to clipboard
open suspend override fun updateStatus(contractId: String, newStatus: ContractStatus, reason: String? = null, metadata: JsonElement? = null): Result<SmartContract>

Update contract status.

Link copied to clipboard
open suspend override fun verifyContract(credentialId: String): Result<Boolean>

Verifies the contract bound to credentialId.