hermez-node/eth/helpers.go
eth/helpers.goRaw
1package eth
2
3import (
4 "encoding/hex"
5 "fmt"
6 "math/big"
7 "net/http"
8 "strconv"
9 "strings"
10
11 ethCommon "github.com/ethereum/go-ethereum/common"
12 "golang.org/x/crypto/sha3"
13)
14
15func addBlock(url string) {
16 method := "POST"
17
18 payload := strings.NewReader("{\n \"jsonrpc\":\"2.0\",\n \"method\":\"evm_mine\",\n \"params\":[],\n \"id\":1\n}")
19
20 client := &http.Client{}
21 req, err := http.NewRequest(method, url, payload)
22
23 if err != nil {
24 fmt.Println(err)
25 }
26 req.Header.Add("Content-Type", "application/json")
27
28 res, err := client.Do(req)
29 if err != nil {
30 fmt.Println(err)
31 }
32 defer func() {
33 if err := res.Body.Close(); err != nil {
34 fmt.Println("Error when closing:", err)
35 }
36 }()
37}
38
39func addBlocks(numBlocks int64, url string) {
40 for i := int64(0); i < numBlocks; i++ {
41 addBlock(url)
42 }
43}
44
45func addTime(seconds float64, url string) {
46 secondsStr := strconv.FormatFloat(seconds, 'E', -1, 32)
47
48 method := "POST"
49 payload := strings.NewReader("{\n \"jsonrpc\":\"2.0\",\n \"method\":\"evm_increaseTime\",\n \"params\":[" + secondsStr + "],\n \"id\":1\n}")
50
51 client := &http.Client{}
52 req, err := http.NewRequest(method, url, payload)
53
54 if err != nil {
55 fmt.Println(err)
56 }
57 req.Header.Add("Content-Type", "application/json")
58
59 res, err := client.Do(req)
60 if err != nil {
61 fmt.Println(err)
62 }
63 defer func() {
64 if err := res.Body.Close(); err != nil {
65 fmt.Println("Error when closing:", err)
66 }
67 }()
68}
69
70func createPermitDigest(tokenAddr, owner, spender ethCommon.Address, chainID, value, nonce, deadline *big.Int, tokenName string) ([]byte, error) {
71 abiPermit := []byte("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)")
72 hashPermit := sha3.NewLegacyKeccak256()
73 hashPermit.Write(abiPermit)
74 abiEIP712Domain := []byte("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)")
75 hashEIP712Domain := sha3.NewLegacyKeccak256()
76 hashEIP712Domain.Write(abiEIP712Domain)
77 var encodeBytes []byte
78 paddedHash := ethCommon.LeftPadBytes(hashEIP712Domain.Sum(nil), 32)
79 hashName := sha3.NewLegacyKeccak256()
80 hashName.Write([]byte(tokenName))
81 paddedName := ethCommon.LeftPadBytes(hashName.Sum(nil), 32)
82 hashVersion := sha3.NewLegacyKeccak256()
83 hashVersion.Write([]byte("1"))
84 paddedX := ethCommon.LeftPadBytes(hashVersion.Sum(nil), 32)
85 paddedChainID := ethCommon.LeftPadBytes(chainID.Bytes(), 32)
86 paddedAddr := ethCommon.LeftPadBytes(tokenAddr.Bytes(), 32)
87 encodeBytes = append(encodeBytes, paddedHash...)
88 encodeBytes = append(encodeBytes, paddedName...)
89 encodeBytes = append(encodeBytes, paddedX...)
90 encodeBytes = append(encodeBytes, paddedChainID...)
91 encodeBytes = append(encodeBytes, paddedAddr...)
92 _domainSeparator := sha3.NewLegacyKeccak256()
93 _domainSeparator.Write(encodeBytes)
94
95 var bytes1 []byte
96 paddedHashPermit := ethCommon.LeftPadBytes(hashPermit.Sum(nil), 32)
97 paddedOwner := ethCommon.LeftPadBytes(owner.Bytes(), 32)
98 paddedSpender := ethCommon.LeftPadBytes(spender.Bytes(), 32)
99 paddedValue := ethCommon.LeftPadBytes(value.Bytes(), 32)
100 paddedNonce := ethCommon.LeftPadBytes(nonce.Bytes(), 32)
101 paddedDeadline := ethCommon.LeftPadBytes(deadline.Bytes(), 32)
102 bytes1 = append(bytes1, paddedHashPermit...)
103 bytes1 = append(bytes1, paddedOwner...)
104 bytes1 = append(bytes1, paddedSpender...)
105 bytes1 = append(bytes1, paddedValue...)
106 bytes1 = append(bytes1, paddedNonce...)
107 bytes1 = append(bytes1, paddedDeadline...)
108 hashBytes1 := sha3.NewLegacyKeccak256()
109 hashBytes1.Write(bytes1)
110
111 var bytes2 []byte
112 byte19, err := hex.DecodeString("19")
113 if err != nil {
114 return nil, err
115 }
116 byte01, err := hex.DecodeString("01")
117 if err != nil {
118 return nil, err
119 }
120 paddedY := ethCommon.LeftPadBytes(byte19, 1)
121 paddedZ := ethCommon.LeftPadBytes(byte01, 1)
122 paddedDomainSeparator := ethCommon.LeftPadBytes(_domainSeparator.Sum(nil), 32)
123 paddedHashBytes1 := ethCommon.LeftPadBytes(hashBytes1.Sum(nil), 32)
124 bytes2 = append(bytes2, paddedY...)
125 bytes2 = append(bytes2, paddedZ...)
126 bytes2 = append(bytes2, paddedDomainSeparator...)
127 bytes2 = append(bytes2, paddedHashBytes1...)
128 hashBytes2 := sha3.NewLegacyKeccak256()
129 hashBytes2.Write(bytes2)
130
131 return hashBytes2.Sum(nil), nil
132}
133
134func createPermit(owner, spender ethCommon.Address, amount, deadline *big.Int, digest, signature []byte) []byte {
135 r := signature[0:32]
136 s := signature[32:64]
137 v := signature[64] + byte(27)
138
139 ABIpermit := []byte("permit(address,address,uint256,uint256,uint8,bytes32,bytes32)")
140 hash := sha3.NewLegacyKeccak256()
141 hash.Write(ABIpermit)
142 methodID := hash.Sum(nil)[:4]
143
144 var permit []byte
145 paddedOwner := ethCommon.LeftPadBytes(owner.Bytes(), 32)
146 paddedSpender := ethCommon.LeftPadBytes(spender.Bytes(), 32)
147 paddedAmount := ethCommon.LeftPadBytes(amount.Bytes(), 32)
148 paddedDeadline := ethCommon.LeftPadBytes(deadline.Bytes(), 32)
149 paddedV := ethCommon.LeftPadBytes([]byte{v}, 32)
150
151 permit = append(permit, methodID...)
152 permit = append(permit, paddedOwner...)
153 permit = append(permit, paddedSpender...)
154 permit = append(permit, paddedAmount...)
155 permit = append(permit, paddedDeadline...)
156 permit = append(permit, paddedV...)
157 permit = append(permit, r...)
158 permit = append(permit, s...)
159
160 return permit
161}