Skip to content

Commit c421a9c

Browse files
committed
🍻 add crypto sign data function
1 parent 1cd1e2e commit c421a9c

File tree

2 files changed

+168
-1
lines changed

2 files changed

+168
-1
lines changed

readme.md

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
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+
-->
111
# golang-web3
212

313
## install
@@ -30,4 +40,114 @@ func NewCli(url string) *jsonrpc.Client {
3040
}
3141
```
3242

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+
33153
- [关于事件监听](./event/event_test.go)

units.go

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @Author: small_ant [email protected]
33
* @Time: 2022-01-15 13:50:16
44
* @LastAuthor: small_ant [email protected]
5-
* @lastTime: 2022-11-18 16:49:13
5+
* @lastTime: 2022-12-08 14:14:42
66
* @FileName: units
77
* @Desc:
88
*
@@ -11,11 +11,14 @@
1111
package web3
1212

1313
import (
14+
"fmt"
1415
"math/big"
1516
"strconv"
1617

1718
"github.com/ethereum/go-ethereum/common"
1819
"github.com/ethereum/go-ethereum/common/hexutil"
20+
"github.com/ethereum/go-ethereum/crypto"
21+
1922
"github.com/shopspring/decimal"
2023
)
2124

@@ -126,3 +129,47 @@ func ToWei(iamount interface{}, decimals int) *big.Int {
126129

127130
return wei
128131
}
132+
133+
// It takes a byte array, and returns a byte array
134+
//
135+
// Args:
136+
// buf ([]byte): The data to be hashed.
137+
//
138+
// Returns:
139+
// The hash of the input buffer.
140+
func Keccak256(buf []byte) []byte {
141+
return crypto.Keccak256(buf)
142+
}
143+
144+
// It takes a byte array, converts it to a string, prepends a string to it, and then hashes the result
145+
//
146+
// Args:
147+
// data ([]byte): The data to sign.
148+
//
149+
// Returns:
150+
// The hash of the message.
151+
func SignHash(data []byte) []byte {
152+
msg := fmt.Sprintf("\x19Ethereum Signed Message:\n%d%s", len(data), data)
153+
return Keccak256([]byte(msg))
154+
}
155+
156+
// It takes a string and returns a byte array
157+
//
158+
// Args:
159+
// data (string): The data to sign.
160+
//
161+
// Returns:
162+
// The return value is a byte array.
163+
func SignString(data string) []byte {
164+
msg := fmt.Sprintf("\x19Ethereum Signed Message:\n%d", 32)
165+
return []byte(msg)
166+
}
167+
168+
// It takes a variable number of byte slices and returns a single byte slice
169+
//
170+
// Returns:
171+
// The hash of the data.
172+
func Keccak256HashData(data ...[]byte) []byte {
173+
h := crypto.Keccak256Hash(data...)
174+
return h.Bytes()
175+
}

0 commit comments

Comments
 (0)