This repository serves as a template for developing Hyperledger Fabric chaincodes using Go. It provides a basic structure and configuration for building smart contracts that can be deployed to a Hyperledger Fabric network.
chaincode-fabric-go-tmpl/
├── chaincode/
│ └── contract.go # Main chaincode contract implementation
├── Dockerfile # Container definition for chaincode deployment
├── go.mod # Go module dependencies
├── go.sum # Go module checksums
└── main.go # Entry point and server configuration
- Go 1.23 or later
- Air for live reloading during development
- Docker (for building deployment images)
- Access to a Hyperledger Fabric network
-
Clone this template:
git clone <your-repo-url> cd chaincode-fabric-go-tmpl
-
Install dependencies:
go mod download
-
Install Air for development:
go install github.com/cosmtrek/air@latest
Air enables automatic rebuilding of your chaincode as you make changes. To use Air:
-
Create a
.air.tomlconfiguration file in your project root:root = "." tmp_dir = "tmp" [build] cmd = "go build -o ./tmp/main ." bin = "./tmp/main" include_ext = ["go"] exclude_dir = ["tmp"] delay = 1000 # ms [log] time = true
-
Run Air:
air
Air will now watch your Go files and automatically rebuild the project when changes are detected.
The chaincode server requires several environment variables to be set:
CORE_CHAINCODE_ID=your-chaincode-id
CORE_CHAINCODE_ADDRESS=:7052
CHAINCODE_TLS_DISABLED=true # Set to false in productionFor TLS configuration (when enabled):
CHAINCODE_TLS_KEY=path/to/key
CHAINCODE_TLS_CERT=path/to/cert
CHAINCODE_CLIENT_CA_CERT=path/to/ca-certBuild the Docker image:
docker build -t your-org/chaincode-name:version .- Fork the repository
- Create your feature branch
- Commit your changes
- Push to the branch
- Create a new Pull Request