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