ChaincodeStubInterface is the primary interface for Hyperledger Fabric Go chaincodes to interact with the distributed ledger. The interface is organized into three categories: auxiliary functions, transaction information retrieval, and ledger data operations.
For the full interface definition and source implementation, see the fabric-chaincode-go shim package.
Auxiliary functions
These methods handle chaincode invocation parameters, cross-chaincode calls, composite key management, and event emission.
| Method signature | Description |
|---|---|
GetArgs() [][]byte | Returns the arguments from the chaincode invocation request as a byte slice array. Each element is one argument. |
GetStringArgs() []string | Returns the arguments from the chaincode invocation request as a string slice. |
GetFunctionAndParameters() (string, []string) | Returns the function name and remaining arguments from the chaincode invocation. By default, the first argument is treated as the function name. |
GetArgsSlice() ([]byte, error) | Returns all arguments from the chaincode invocation request joined into a single byte slice. |
InvokeChaincode(chaincodeName string, args [][]byte, channel string) pb.Response | Calls the Invoke method of another chaincode. |
CreateCompositeKey(objectType string, attributes []string) (string, error) | Combines an object type and a set of attributes into a composite key. Use SplitCompositeKey to decompose the resulting key. |
SplitCompositeKey(compositeKey string) (string, []string, error) | Splits a composite key back into its object type and attributes. See also CreateCompositeKey. |
SetEvent(name string, payload []byte) error | Sets a chaincode event to include in the transaction proposal response. |
Transaction information
These methods expose metadata about the current transaction: its ID, channel, creator identity, timestamp, and proposal payload.
| Method signature | Description |
|---|---|
GetTxID() string | Returns the transaction ID of the current transaction. |
GetChannelID() string | Returns the name of the channel on which the transaction is executing. |
GetCreator() ([]byte, error) | Returns the serialized identity of the transaction submitter. |
GetTransient() (map[string][]byte, error) | Returns the transient map from the transaction proposal. Transient data is used for application-level communication and is not written to the ledger. |
GetBinding() ([]byte, error) | Returns the transaction binding, which links application-specific data to the transient data. |
GetDecorations() map[string][]byte | Returns additional decorations attached to the transaction proposal by the peer. |
GetSignedProposal() (*pb.SignedProposal, error) | Returns the complete signed transaction proposal, including all associated data. |
GetTxTimestamp() (*timestamp.Timestamp, error) | Returns the timestamp at which the transaction was created. |
Ledger data operations
These methods read from and write to both the public ledger state and private data collections.
Public state
| Method signature | Description |
|---|---|
GetState(key string) ([]byte, error) | Returns the value of the specified key from the public ledger state. |
PutState(key string, value []byte) error | Adds or updates a key-value pair in the public ledger state. |
DelState(key string) error | Deletes a key-value pair from the public ledger state. |
SetStateValidationParameter(key string, ep []byte) error | Sets the endorsement policy for a specific key in the public ledger state. |
GetStateValidationParameter(key string) ([]byte, error) | Returns the endorsement policy for a specific key in the public ledger state. |
Range and composite key queries
| Method signature | Description |
|---|---|
GetStateByRange(startKey, endKey string) (StateQueryIteratorInterface, error) | Returns all key-value pairs with keys in a specified range. |
GetStateByRangeWithPagination(startKey, endKey string, pageSize int32, bookmark string) (StateQueryIteratorInterface, *pb.QueryResponseMetadata, error) | Returns key-value pairs in a specified range by page. |
GetStateByPartialCompositeKey(objectType string, keys []string) (StateQueryIteratorInterface, error) | Returns all key-value pairs whose composite key prefix matches the given objectType and partial key attributes. See also CreateCompositeKey and SplitCompositeKey. |
GetStateByPartialCompositeKeyWithPagination(objectType string, keys []string, pageSize int32, bookmark string) (StateQueryIteratorInterface, *pb.QueryResponseMetadata, error) | Paged version of GetStateByPartialCompositeKey. |
Rich queries
Rich queries require a state database that supports them.
| Method signature | Description |
|---|---|
GetQueryResult(query string) (StateQueryIteratorInterface, error) | Executes a rich query against the state database. The state database must support rich queries. |
GetQueryResultWithPagination(query string, pageSize int32, bookmark string) (StateQueryIteratorInterface, *pb.QueryResponseMetadata, error) | Executes a rich query against the state database by page. The state database must support rich queries. |
History queries
| Method signature | Description |
|---|---|
GetHistoryForKey(key string) (HistoryQueryIteratorInterface, error) | Returns all historical values of the specified key. |
Private data
These methods operate on private data collections. The collection parameter identifies the target collection by name.
| Method signature | Description |
|---|---|
GetPrivateData(collection, key string) ([]byte, error) | Returns the value of the specified key from the given private data collection. |
GetPrivateDataHash(collection, key string) ([]byte, error) | Returns the hash of the value of the specified key in the given private data collection. |
PutPrivateData(collection string, key string, value []byte) error | Adds or updates a key-value pair in the given private data collection. |
DelPrivateData(collection, key string) error | Deletes the specified key from the given private data collection. |
SetPrivateDataValidationParameter(collection, key string, ep []byte) error | Sets the endorsement policy for a specific key in the given private data collection. |
GetPrivateDataValidationParameter(collection, key string) ([]byte, error) | Returns the endorsement policy for a specific key in the given private data collection. |
GetPrivateDataByRange(collection, startKey, endKey string) (StateQueryIteratorInterface, error) | Returns key-value pairs within a specified range from the given private data collection. |
GetPrivateDataByPartialCompositeKey(collection, objectType string, keys []string) (StateQueryIteratorInterface, error) | Returns all key-value pairs whose composite key prefix matches the given objectType and partial key attributes in the given private data collection. See also CreateCompositeKey and SplitCompositeKey. |
GetPrivateDataQueryResult(collection, query string) (StateQueryIteratorInterface, error) | Executes a rich query against the given private data collection. Requires a state database that supports rich queries. |