modulepackage
1.1.6
Repository: https://github.com/mover-code/golang-web3.git
Documentation: pkg.go.dev
# README
golang-web3
install
go get -u github.com/mover-code/golang-web3.git@latest
go get -u github.com/btcsuite/[email protected] 遇到ambiguous import
desc
使用go操作区块链账户,发起交易,查询账户资产、合约交互、部署,事件监听等功能
use
- 创建一个web3实例
// It creates a new client for the given url.
//
// Args:
// url (string): The address of the RPC service.
func NewCli(url string) *jsonrpc.Client {
cli, err := jsonrpc.NewClient(url)
if err != nil {
panic(fmt.Sprintf("error:%s", url))
}
return cli
}
- 数据签名示例
privateKey, _ := crypto.HexToECDSA(key) // 账户私钥
u256, _ := abi.NewType("uint256")
account, _ := abi.NewType("address")
type Ord struct {
Id *big.Int
Account web3.Address
Amount *big.Int
Fee *big.Int
Solt *big.Int
End *big.Int
Type *big.Int
State *big.Int
}
argumentInfo := []*abi.TupleElem{
&abi.TupleElem{
Name: "id",
Elem: u256,
},
&abi.TupleElem{
Name: "account",
Elem: account,
},
&abi.TupleElem{
Name: "amount",
Elem: u256,
},
&abi.TupleElem{
Name: "solt",
Elem: u256,
},
&abi.TupleElem{
Name: "end",
Elem: u256,
},
&abi.TupleElem{
Name: "type",
Elem: u256,
},
}
param := abi.NewTupleType(argumentInfo)
// 数据hash
hash, _ := param.Encode(&Ord{
Id: id,
Account: addr,
Amount: withDrawAmount,
Fee: big.NewInt(0),
Solt: solt,
End: endTime,
Type: wType,
State: big.NewInt(0),
})
hashs := crypto.Keccak256Hash(signString(""), crypto.Keccak256(hash)) // 按照合约中的方式组装数据
signature, _ := crypto.Sign(hashs.Bytes(), privateKey) // 使用私钥签名获取签名后的数据
r,s,v := SignRSV(signature) // 签名后的rsv数据
- 验证签名信息示例
func VerifySig(msg, from, sigHex string) bool {
sig := hexutil.MustDecode(sigHex)
if sig[64] != 27 && sig[64] != 28 {
return false
}
sig[64] -= 27
addr, _ := wallet.Ecrecover(web3.SignHash([]byte(msg)), sig)
return addr == web3.HexToAddress(from)
}
- 代币转账示例 主币
import(
web3 "github.com/mover-code/golang-web3"
"github.com/mover-code/golang-web3/jsonrpc"
"github.com/mover-code/golang-web3/wallet"
)
// rpc地址
cli, err := jsonrpc.NewClient(url)
if err != nil {
panic(fmt.Sprintf("error:%s", url))
}
block, _ := cli.Eth().BlockNumber() // 获取当前区块
balance, _ := cli.Eth().GetBalance(web3.HexToAddress(addr), web3.BlockNumber(block)) // 查询账户余额
nonce, err := cli.Eth().GetNonce(sender, web3.BlockNumber(block)) // 发起交易账户的序号
gas, _ := cli.Eth().GasPrice() // 当前gas
gasLimit, err := cli.Eth().EstimateGas(&web3.CallMsg{
From: sender, // 发起账户
To: &receiver, // 接收账户
GasPrice: gas,
Value: big.NewInt(amount), // 转账金额
})
// 组装交易信息
t := &web3.Transaction{
From: sender,
To: &receiver,
Value: big.NewInt(100000000000000000),
Nonce: nonce,
BlockNumber: uint64(block),
GasPrice: gas,
Gas: gasLimit,
}
chainId, _ := cli.Eth().ChainID()
signer := wallet.NewEIP155Signer(chainId.Uint64())
byteKey, _ := hex.DecodeString(private_hash) // 发起交易账户 私钥
key, _ := wallet.NewWalletFromPrivKey(byteKey)
data, err := signer.SignTx(t, key) // 签名数据
cli.Eth().SenSignTransaction(data) // 发起交易
# Packages
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
# Functions
BytesToAddress converts bytes to an address object.
BytesToHash converts bytes to a hash object.
No description provided by the author
Ether converts a value to the ether unit with 18 decimals.
`FWei` converts a `big.Int` from wei to ether
Args: i: The amount of Wei you want to convert.
No description provided by the author
HexToAddress converts an hex string value to an address object.
HexToHash converts an hex string value to a hash object.
It takes a byte array, and returns a byte array
Args: buf ([]byte): The data to be hashed.
It takes a variable number of byte slices and returns a single byte slice
Returns: The hash of the data.
It takes a byte array, converts it to a string, prepends a string to it, and then hashes the result
Args: data ([]byte): The data to sign.
It takes a signature in the form of a byte array or hex string, and returns the R, S, and V values as byte arrays and a uint8
Args: isig: signature.
It takes a string and returns a byte array
Args: data (string): The data to sign.
It takes an interface{} and converts it to a decimal.Decimal
Args: ivalue: The value to convert to a decimal.
It takes an amount and a decimal count, and returns the amount in wei
Args: iamount: The amount of tokens you want to send.
# Interfaces
No description provided by the author
# Type aliases
Address is an Ethereum address.
No description provided by the author
Hash is an Ethereum hash.
Network is a chain id.