Skip to main content

Smart Contracts

Shamwari supports Java-based on-chain contracts that run within the node's JVM. Contracts are compiled Java classes uploaded to the blockchain and executed by the contract runner at specified trigger points.

Contract Model​

The contract framework is defined in src/java/nxt/blockchain/contracts/. A contract implements the Contract<InvocationData, ReturnedData> interface (contracts/Contract.java:24), which provides five lifecycle callbacks:

public interface Contract<InvocationData, ReturnedData> {
JO processBlock(BlockContext context); // Triggered per block
JO processTransaction(TransactionContext context); // Triggered per transaction
JO processRequest(RequestContext context); // On-demand HTTP request
JO processVoucher(VoucherContext context); // Voucher-based invocation
ReturnedData processInvocation(DelegatedContext context, InvocationData data); // Delegated call
<T extends TransactionResponse> boolean isDuplicate(T myTransaction, List<T> existing);
default void init(InitializationContext context) {}
default void shutdown(ShutdownContext context) {}
}

Context Types​

ContextTriggerFile
BlockContextExecuted once per blockBlockContext.java
TransactionContextExecuted when a transaction is processedTransactionContext.java
RequestContextInvoked via HTTP API requestRequestContext.java
VoucherContextInvoked from a voucherVoucherContext.java
DelegatedContextInvoked by another contractDelegatedContext.java
InitializationContextAfter class load, before callbacksInitializationContext.java
ShutdownContextBefore class destructionShutdownContext.java

An AbstractContract base class (contracts/AbstractContract.java) provides default no-op implementations.

Building Contracts​

Contracts are plain Java classes. Compile them against the Shamwari classpath:

# After running ./compile.sh, the 'classes' directory contains the node's classes
javac -cp classes:lib/*:conf -d contract_classes MyContract.java
jar cf mycontract.jar -C contract_classes .

Deployment​

Upload Contract File​

Upload the compiled contract JAR to the node via the uploadContractFile API:

curl -X POST "http://localhost:22024/nxt" \
-F "requestType=uploadContractFile" \
-F "contractName=MyContract" \
-F "contractFile=@mycontract.jar" \
-F "secretPhrase=your_secret_phrase"

Response includes a transactionFullHash that identifies the uploaded contract.

Set Contract Reference​

Link an account to a deployed contract using setContractReference:

curl -X POST "http://localhost:22024/nxt" \
-d "requestType=setContractReference" \
-d "contractName=MyContract" \
-d "contractParams={\"param1\":\"value1\"}" \
-d "transactionFullHash=<full_hash_from_upload>" \
-d "secretPhrase=your_secret_phrase"

The reference stores the contract name, parameters, and the transaction full hash pointing to the uploaded bytecode. Each account can have at most one reference per contract name.

Manage Contracts with the Contract Manager​

The contractManager.sh script launches the ContractManager tool with a security policy that grants full permissions to the contract environment:

./contractManager.sh MyContract

Verification​

Verify Contract Integrity​

Check the deployed bytecode against the transaction on chain:

curl "http://localhost:22024/nxt" \
-d "requestType=verifyContract" \
-d "transactionFullHash=<full_hash>" \
-d "name=MyContract"

Response flags [OK] if the hash matches, or [CHANGED] if the bytecode was modified.

Contract Runner Status​

Check whether the contract runner is active and processing:

curl "http://localhost:22024/nxt" \
-d "requestType=getContractRunnerStatus"

Querying Contracts​

Download a Contract​

curl "http://localhost:22024/nxt" \
-d "requestType=downloadContractFile" \
-d "transactionFullHash=<full_hash>"

List Contract References​

# References for a specific account
curl "http://localhost:22024/nxt?requestType=getContractReferences&account=<account_id>"

# Search contracts by keyword
curl "http://localhost:22024/nxt?requestType=searchContractFiles&query=keyword"

List All Contracts​

curl "http://localhost:22024/nxt?requestType=getAllContractFiles&firstIndex=0&lastIndex=49"
APITagDescription
uploadContractFileContractsDeploy a contract JAR
downloadContractFileContractsRetrieve uploaded contract
verifyContractContractsVerify bytecode integrity
setContractReferenceContractsLink account to contract
deleteContractReferenceContractsRemove a contract reference
getContractReferencesContractsList account contract references
getContractRunnerStatusContractsCheck runner state
getAllContractFilesContractsList all uploaded contracts
getAccountContractFilesContractsContracts owned by an account
getContractFileContractsGet specific contract metadata
searchContractFilesContractsSearch by keyword

Limitations​

  • Contracts are Java classes compiled to bytecode; only JVM-compatible bytecode (class version 65 for Java 21) is accepted.
  • Maximum upload size is governed by nxt.maxUploadFileSize (default 42 KB). Increase in nxt.properties.
  • Contracts run in a security-managed sandbox; file and network access must be declared in the policy.
  • The contract manager uses contractManager.policy which grants AllPermission; review this policy before deploying in production.