|
| 1 | +<!-- |
| 2 | + * @Author: small_ant [email protected] |
| 3 | + * @Time: 2022-01-15 13:50:16 |
| 4 | + * @LastAuthor: small_ant [email protected] |
| 5 | + * @lastTime: 2022-12-08 14:33:47 |
| 6 | + * @FileName: readme |
| 7 | + * @Desc: |
| 8 | + * |
| 9 | + * Copyright (c) 2022 by small_ant [email protected], All Rights Reserved. |
| 10 | +--> |
1 | 11 | # golang-web3 |
2 | 12 |
|
3 | 13 | ## install |
@@ -30,4 +40,114 @@ func NewCli(url string) *jsonrpc.Client { |
30 | 40 | } |
31 | 41 | ``` |
32 | 42 |
|
| 43 | +- 数据签名示例 |
| 44 | + |
| 45 | +```go |
| 46 | + privateKey, _ := crypto.HexToECDSA(key) // 账户私钥 |
| 47 | + u256, _ := abi.NewType("uint256") |
| 48 | + account, _ := abi.NewType("address") |
| 49 | + type Ord struct { |
| 50 | + Id *big.Int |
| 51 | + Account web3.Address |
| 52 | + Amount *big.Int |
| 53 | + Fee *big.Int |
| 54 | + Solt *big.Int |
| 55 | + End *big.Int |
| 56 | + Type *big.Int |
| 57 | + State *big.Int |
| 58 | + } |
| 59 | + |
| 60 | + argumentInfo := []*abi.TupleElem{ |
| 61 | + &abi.TupleElem{ |
| 62 | + Name: "id", |
| 63 | + Elem: u256, |
| 64 | + }, |
| 65 | + &abi.TupleElem{ |
| 66 | + Name: "account", |
| 67 | + Elem: account, |
| 68 | + }, |
| 69 | + &abi.TupleElem{ |
| 70 | + Name: "amount", |
| 71 | + Elem: u256, |
| 72 | + }, |
| 73 | + &abi.TupleElem{ |
| 74 | + Name: "solt", |
| 75 | + Elem: u256, |
| 76 | + }, |
| 77 | + &abi.TupleElem{ |
| 78 | + Name: "end", |
| 79 | + Elem: u256, |
| 80 | + }, |
| 81 | + &abi.TupleElem{ |
| 82 | + Name: "type", |
| 83 | + Elem: u256, |
| 84 | + }, |
| 85 | + } |
| 86 | + |
| 87 | + param := abi.NewTupleType(argumentInfo) |
| 88 | + |
| 89 | + // 数据hash |
| 90 | + hash, _ := param.Encode(&Ord{ |
| 91 | + Id: id, |
| 92 | + Account: addr, |
| 93 | + Amount: withDrawAmount, |
| 94 | + Fee: big.NewInt(0), |
| 95 | + Solt: solt, |
| 96 | + End: endTime, |
| 97 | + Type: wType, |
| 98 | + State: big.NewInt(0), |
| 99 | + }) |
| 100 | + |
| 101 | + hashs := crypto.Keccak256Hash(signString(""), crypto.Keccak256(hash)) // 按照合约中的方式组装数据 |
| 102 | + signature, _ := crypto.Sign(hashs.Bytes(), privateKey) // 使用私钥签名获取签名后的数据 |
| 103 | + r,s,v := SignRSV(signature) // 签名后的rsv数据 |
| 104 | +``` |
| 105 | + |
| 106 | +- 代币转账示例 主币 |
| 107 | + |
| 108 | +```go |
| 109 | + |
| 110 | +import( |
| 111 | + web3 "github.com/mover-code/golang-web3" |
| 112 | + "github.com/mover-code/golang-web3/jsonrpc" |
| 113 | + "github.com/mover-code/golang-web3/wallet" |
| 114 | + |
| 115 | +) |
| 116 | + // rpc地址 |
| 117 | + cli, err := jsonrpc.NewClient(url) |
| 118 | + if err != nil { |
| 119 | + panic(fmt.Sprintf("error:%s", url)) |
| 120 | + } |
| 121 | + |
| 122 | + block, _ := cli.Eth().BlockNumber() // 获取当前区块 |
| 123 | + balance, _ := cli.Eth().GetBalance(web3.HexToAddress(addr), web3.BlockNumber(block)) // 查询账户余额 |
| 124 | + |
| 125 | + nonce, err := cli.Eth().GetNonce(sender, web3.BlockNumber(block)) // 发起交易账户的序号 |
| 126 | + gas, _ := cli.Eth().GasPrice() // 当前gas |
| 127 | + gasLimit, err := cli.Eth().EstimateGas(&web3.CallMsg{ |
| 128 | + From: sender, // 发起账户 |
| 129 | + To: &receiver, // 接收账户 |
| 130 | + GasPrice: gas, |
| 131 | + Value: big.NewInt(amount), // 转账金额 |
| 132 | + }) |
| 133 | + // 组装交易信息 |
| 134 | + t := &web3.Transaction{ |
| 135 | + From: sender, |
| 136 | + To: &receiver, |
| 137 | + Value: big.NewInt(100000000000000000), |
| 138 | + Nonce: nonce, |
| 139 | + BlockNumber: uint64(block), |
| 140 | + GasPrice: gas, |
| 141 | + Gas: gasLimit, |
| 142 | + } |
| 143 | + |
| 144 | + chainId, _ := cli.Eth().ChainID() |
| 145 | + signer := wallet.NewEIP155Signer(chainId.Uint64()) |
| 146 | + byteKey, _ := hex.DecodeString(private_hash) // 发起交易账户 私钥 |
| 147 | + key, _ := wallet.NewWalletFromPrivKey(byteKey) |
| 148 | + data, err := signer.SignTx(t, key) // 签名数据 |
| 149 | + cli.Eth().SenSignTransaction(data) // 发起交易 |
| 150 | + |
| 151 | +``` |
| 152 | + |
33 | 153 | - [关于事件监听](./event/event_test.go) |
0 commit comments