0. Reference documents
GitHub - FISCO-BCOS/go-sdk: golang SDK of FISCO BCOS
1. Environment configuration
Operating system: CentOS7
Golang version: 1.17.2
WeBASE version: 1.5.2 (enabled) can be found at: WeBASE deployment - build FISCO node_ling1998's blog-CSDN blog
Git version: 1.8.3.1
2. Download go-sdk
(1) Enter the project directory
[root@localhost ~]# cd /home/tracy/GoContract/
(2) Download Go-sdk
[root@localhost GoContract]# git clone https://github.com/FISCO-BCOS/go-sdk.git
Cloning into 'go-sdk'...
remote: Enumerating objects: 1739, done.
remote: Counting objects: 100% (258/258), done.
remote: Compressing objects: 100% (174/174), done.
remote: Total 1739 (delta 107), reused 145 (delta 62), pack-reused 1481
Receiving objects: 100% (1739/1739), 1.01 MiB | 634.00 KiB/s, done.
Resolving deltas: 100% (930/930), done.
(3) Enter the go-sdk directory
[root@localhost gosdkdemo]# cd ../go-sdk
(4) Download dependencies
[root@localhost go-sdk]# go mod tidy
(5) Compile and generate the abigen tool
[root@localhost go-sdk]# go build ./cmd/abigen/
Compilation time is a bit long, wait patiently
(6) Create a project directory and move abigen to the project directory
[root@localhost go-sdk]# cd .. #返回上一级目录
[root@localhost GoContract]# mkdir gosdkdemo #创建工程目录
[root@localhost GoContract]# cd gosdkdemo #进入工程目录
[root@localhost gosdkdemo]# mkdir tools #创建目录,存放ABI工具及相关文件
[root@localhost gosdkdemo]# cp ../go-sdk/abigen tools/ #拷贝上一步生成的abigen到此目录中
3. Configuration file config.toml
(1) Enter the project directory
[root@localhost GoContract]# cd gosdkdemo
(2) Create a configuration file (config.toml)
[root@localhost gosdkdemo]# vi config.toml
Configuration file content
[Network]
#type rpc or channel
Type="channel"
CAFile="ca.crt"
Cert="sdk.crt"
Key="sdk.key"
[[Network.Connection]]
NodeURL="127.0.0.1:20200"
GroupID=1
# [[Network.Connection]]
# NodeURL="127.0.0.1:20200"
# GroupID=2
[Account]
# only support PEM format for now
KeyFile=".ci/0x83309d045a19c44dc3722d15a6abd472f95866ac.pem"
[Chain]
ChainID=1
SMCrypto=false
Press Esc to exit editing, enter: wq to save and exit.
(3) Copy the certificate and key files in the configuration file
The following three files in the configuration file
CAFile="ca.crt"
Cert="sdk.crt"
Key="sdk.key"
There are three files in webase-deploy/nodes/127.0.0.1/sdk
Copy the above three files to the project directory
[root@localhost gosdkdemo]# mkdir sdk #创建目录,保存证书及密钥文件
[root@localhost gosdkdemo]# cp /usr/local/webase-deploy/nodes/127.0.0.1/sdk/ca.crt sdk/
[root@localhost gosdkdemo]# cp /usr/local/webase-deploy/nodes/127.0.0.1/sdk/sdk.crt sdk/
[root@localhost gosdkdemo]# cp /usr/local/webase-deploy/nodes/127.0.0.1/sdk/sdk.key sdk/
Change the configuration file config.toml
[root@localhost gosdkdemo]# vi config.toml
The content is as follows, plus the path:
CAFile="sdk/ca.crt"
Cert="sdk/sdk.crt"
Key="sdk/sdk.key"
Press Esc to exit editing, enter: wq to save and exit.
(4) Generate private key file
Generate the private key file in the configuration file config.toml, as follows:
KeyFile=".ci/0x83309d045a19c44dc3722d15a6abd472f95866ac.pem"
download console
[root@localhost gosdkdemo]# cd .. #返回上一级目录
[root@localhost GoContract]# git clone https://github.com/FISCO-BCOS/console.git
Use the get_account.sh script to generate a private key file
[root@localhost GoContract]# cd console/
[root@localhost console]# ls
build.gradle CONTRIBUTING.md gradle gradlew.bat README.md settings.gradle tools
Changelog.md doc gradlew LICENSE release_note.txt src
[root@localhost console]# ./tools/get_account.sh
[INFO] Account Address : 0xde54d4143e33780c5cb5eed919ce25d2608789ce
[INFO] Private Key (pem) : accounts/0xde54d4143e33780c5cb5eed919ce25d2608789ce.pem
[INFO] Public Key (pem) : accounts/0xde54d4143e33780c5cb5eed919ce25d2608789ce.pem.pub
Move the generated private key file directory to the project directory
[root@localhost console]# cp -r accounts/ ../gosdkdemo/
View project directory structure
[root@localhost console]# cd ../gosdkdemo/ #进入工程目录
[root@localhost gosdkdemo]# tree
.
├── accounts
│ ├── 0xde54d4143e33780c5cb5eed919ce25d2608789ce.pem
│ └── 0xde54d4143e33780c5cb5eed919ce25d2608789ce.pem.pub
├── config.toml
└── sdk
├── ca.crt
├── sdk.crt
└── sdk.key
2 directories, 6 files
(5) Change the KeyFile value in the configuration file config.toml
[root@localhost gosdkdemo]# vi config.toml
Change the KeyFile value to the private key account file generated above
KeyFile="accounts/0xde54d4143e33780c5cb5eed919ce25d2608789ce.pem"
or
KeyFile=".accounts/0xde54d4143e33780c5cb5eed919ce25d2608789ce.pem"
The dot in front of accounts is to hide the file.
Press Esc to exit editing, enter: wq to save and exit.
4. Use the ABI tool to generate the go file
Note: A smart contract has been written and compiled and deployed in WebASE, as shown in the figure below
(1) Generate smart contract .abi file
[root@localhost gosdkdemo]# cd tools #进入tools目录
[root@localhost tools]# vi hello.abi
The abi content can be viewed in the contract IDE of WeBASE and copied to the hello.abi file
Press Esc to exit editing, enter: wq to save and exit.
(2) Generate smart contract .bin file
[root@localhost tools]# vi hello.bin
The bin content can be viewed in WeBASE’s contract IDE and copied to the hello.bin file
Press Esc to exit editing, enter: wq to save and exit.
(3) Use abigen to generate go files
[root@localhost tools]# ./abigen -abi hello.abi -bin hello.bin -type hello -pkg main -out ../hello.go
[root@localhost tools]# cd .. #返回上一级目录
[root@localhost gosdkdemo]# ls #查看已经生成的hello.go文件
accounts config.toml hello.go sdk tools
Note: If you have installed Geth and configured environment variables, because there is an abigen with the same name under Geth, it will rely on ethereum related packages to generate GO files. If you do not specify abigen, the abigen in Geth will be used by default, and an error will be reported after compiling. Therefore, you need to specify abigen as the abigen just generated by go-sdk. As above, use ./abigen in the tools directory to specify the current abigen tool.
The GO file import package generated by abigen in Geth is:
// Code generated - DO NOT EDIT.
// This file is a generated binding and any manual changes will be lost.
package main
import (
"errors"
"math/big"
"strings"
ethereum "github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/event"
)
The abigen tool generated by go-sdk, the regenerated GO file import package is:
// Code generated - DO NOT EDIT.
// This file is a generated binding and any manual changes will be lost.
package main
import (
"math/big"
"strings"
"github.com/FISCO-BCOS/go-sdk/abi"
"github.com/FISCO-BCOS/go-sdk/abi/bind"
"github.com/FISCO-BCOS/go-sdk/core/types"
"github.com/FISCO-BCOS/go-sdk/event"
ethereum "github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
)
5. Call the smart contract
Enter the CentOS visual interface, open VSCode, write and execute code here
(1) Write code (main.go)
package main
import (
"fmt"
"log"
"github.com/FISCO-BCOS/go-sdk/client"
"github.com/FISCO-BCOS/go-sdk/conf"
"github.com/ethereum/go-ethereum/common"
)
func main() {
//parse config
configs, err := conf.ParseConfigFile("config.toml")
if err != nil {
log.Fatal(err)
}
config := &configs[0]
//connect peer
cli, err := client.Dial(config)
if err != nil {
log.Fatal(err)
}
//create a contract instance
contractAddr := "0x85aa38ac471df787e16a7323d9e8b721af5f7d48"
instance, err := NewHello(common.HexToAddress(contractAddr), cli)
if err != nil {
log.Fatal(err)
}
//call function of smart contract
name, err := instance.GetName(cli.GetCallOpts())
if err != nil {
log.Panic(err)
}
fmt.Println(name)
}
(2) Generate go.mod
[tracy@localhost gosdkdemo]$ go mod init gosdkdemo
(3) Download dependent packages
[tracy@localhost gosdkdemo]$ go mod tidy
go: finding module for package github.com/FISCO-BCOS/go-sdk/conf
go: finding module for package github.com/FISCO-BCOS/go-sdk/client
go: found github.com/FISCO-BCOS/go-sdk/client in github.com/FISCO-BCOS/go-sdk v0.11.0
go: found github.com/FISCO-BCOS/go-sdk/conf in github.com/FISCO-BCOS/go-sdk v0.11.0
gosdkdemo imports
github.com/FISCO-BCOS/go-sdk/client imports
github.com/ethereum/go-ethereum/crypto imports
github.com/btcsuite/btcd/btcec/v2/ecdsa tested by
github.com/btcsuite/btcd/btcec/v2/ecdsa.test imports
github.com/btcsuite/btcd/chaincfg/chainhash: ambiguous import: found package github.com/btcsuite/btcd/chaincfg/chainhash in multiple modules:
github.com/btcsuite/btcd v0.21.0-beta (/home/tracy/gopath/pkg/mod/github.com/btcsuite/[email protected]/chaincfg/chainhash)
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.0 (/home/tracy/gopath/pkg/mod/github.com/btcsuite/btcd/chaincfg/[email protected])
If you encounter the above error, change github.com/ethereum/go-ethereum v1.10.17 in the go.mod file to
github.com/ethereum/go-ethereum v1.10.16 (just modify the version number), as shown in the figure below:
(4) compile
[tracy@localhost gosdkdemo]$ go build *.go
# command-line-arguments
./main.go:28:47: cannot use cli (type *client.Client) as type "github.com/ethereum/go-ethereum/accounts/abi/bind".ContractBackend in argument to NewHello:
*client.Client does not implement "github.com/ethereum/go-ethereum/accounts/abi/bind".ContractBackend (missing EstimateGas method)
./main.go:34:32: undefined: client.GetCallOpts
If the above error occurs, it is because Geth's abigen is used by default to generate Go files, otherwise the compilation is successful.
(5) run
[tracy@localhost gosdkdemo]$ go run *.go
tracy
Summarize
The above-mentioned abigen tool generated by go-sdk (that is, step 2) and the get_account.sh script in the console generate a private key file (that is, steps 3-(4)), no need to configure it again, and then use it directly.