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
| Context | Trigger | File |
|---|---|---|
BlockContext | Executed once per block | BlockContext.java |
TransactionContext | Executed when a transaction is processed | TransactionContext.java |
RequestContext | Invoked via HTTP API request | RequestContext.java |
VoucherContext | Invoked from a voucher | VoucherContext.java |
DelegatedContext | Invoked by another contract | DelegatedContext.java |
InitializationContext | After class load, before callbacks | InitializationContext.java |
ShutdownContext | Before class destruction | ShutdownContext.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"
Related APIs
| API | Tag | Description |
|---|---|---|
uploadContractFile | Contracts | Deploy a contract JAR |
downloadContractFile | Contracts | Retrieve uploaded contract |
verifyContract | Contracts | Verify bytecode integrity |
setContractReference | Contracts | Link account to contract |
deleteContractReference | Contracts | Remove a contract reference |
getContractReferences | Contracts | List account contract references |
getContractRunnerStatus | Contracts | Check runner state |
getAllContractFiles | Contracts | List all uploaded contracts |
getAccountContractFiles | Contracts | Contracts owned by an account |
getContractFile | Contracts | Get specific contract metadata |
searchContractFiles | Contracts | Search 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 innxt.properties. - Contracts run in a security-managed sandbox; file and network access must be declared in the policy.
- The contract manager uses
contractManager.policywhich grantsAllPermission; review this policy before deploying in production.