arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
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(
 18		"{\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(
 50		"{\n    \"jsonrpc\":\"2.0\",\n    \"method\":\"evm_increaseTime\",\n    \"params\":[" +
 51			secondsStr + "],\n    \"id\":1\n}")
 52
 53	client := &http.Client{}
 54	req, err := http.NewRequest(method, url, payload)
 55
 56	if err != nil {
 57		fmt.Println(err)
 58	}
 59	req.Header.Add("Content-Type", "application/json")
 60
 61	res, err := client.Do(req)
 62	if err != nil {
 63		fmt.Println(err)
 64	}
 65	defer func() {
 66		if err := res.Body.Close(); err != nil {
 67			fmt.Println("Error when closing:", err)
 68		}
 69	}()
 70}
 71
 72func createPermitDigest(tokenAddr, owner, spender ethCommon.Address, chainID, value, nonce,
 73	deadline *big.Int, tokenName string) ([]byte, error) {
 74	// NOTE: We ignore hash.Write errors because we are writing to a memory
 75	// buffer and don't expect any errors to occur.
 76	abiPermit :=
 77		[]byte("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)")
 78	hashPermit := sha3.NewLegacyKeccak256()
 79	hashPermit.Write(abiPermit) //nolint:errcheck,gosec
 80	abiEIP712Domain :=
 81		[]byte("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)")
 82	hashEIP712Domain := sha3.NewLegacyKeccak256()
 83	hashEIP712Domain.Write(abiEIP712Domain) //nolint:errcheck,gosec
 84	var encodeBytes []byte
 85	paddedHash := ethCommon.LeftPadBytes(hashEIP712Domain.Sum(nil), 32)
 86	hashName := sha3.NewLegacyKeccak256()
 87	hashName.Write([]byte(tokenName)) //nolint:errcheck,gosec
 88	paddedName := ethCommon.LeftPadBytes(hashName.Sum(nil), 32)
 89	hashVersion := sha3.NewLegacyKeccak256()
 90	hashVersion.Write([]byte("1")) //nolint:errcheck,gosec
 91	paddedX := ethCommon.LeftPadBytes(hashVersion.Sum(nil), 32)
 92	paddedChainID := ethCommon.LeftPadBytes(chainID.Bytes(), 32)
 93	paddedAddr := ethCommon.LeftPadBytes(tokenAddr.Bytes(), 32)
 94	encodeBytes = append(encodeBytes, paddedHash...)
 95	encodeBytes = append(encodeBytes, paddedName...)
 96	encodeBytes = append(encodeBytes, paddedX...)
 97	encodeBytes = append(encodeBytes, paddedChainID...)
 98	encodeBytes = append(encodeBytes, paddedAddr...)
 99	_domainSeparator := sha3.NewLegacyKeccak256()
100	_domainSeparator.Write(encodeBytes) //nolint:errcheck,gosec
101
102	var bytes1 []byte
103	paddedHashPermit := ethCommon.LeftPadBytes(hashPermit.Sum(nil), 32)
104	paddedOwner := ethCommon.LeftPadBytes(owner.Bytes(), 32)
105	paddedSpender := ethCommon.LeftPadBytes(spender.Bytes(), 32)
106	paddedValue := ethCommon.LeftPadBytes(value.Bytes(), 32)
107	paddedNonce := ethCommon.LeftPadBytes(nonce.Bytes(), 32)
108	paddedDeadline := ethCommon.LeftPadBytes(deadline.Bytes(), 32)
109	bytes1 = append(bytes1, paddedHashPermit...)
110	bytes1 = append(bytes1, paddedOwner...)
111	bytes1 = append(bytes1, paddedSpender...)
112	bytes1 = append(bytes1, paddedValue...)
113	bytes1 = append(bytes1, paddedNonce...)
114	bytes1 = append(bytes1, paddedDeadline...)
115	hashBytes1 := sha3.NewLegacyKeccak256()
116	hashBytes1.Write(bytes1) //nolint:errcheck,gosec
117
118	var bytes2 []byte
119	paddedY := ethCommon.LeftPadBytes([]byte{0x19}, 1)
120	paddedZ := ethCommon.LeftPadBytes([]byte{0x01}, 1)
121	paddedDomainSeparator := ethCommon.LeftPadBytes(_domainSeparator.Sum(nil), 32)
122	paddedHashBytes1 := ethCommon.LeftPadBytes(hashBytes1.Sum(nil), 32)
123	bytes2 = append(bytes2, paddedY...)
124	bytes2 = append(bytes2, paddedZ...)
125	bytes2 = append(bytes2, paddedDomainSeparator...)
126	bytes2 = append(bytes2, paddedHashBytes1...)
127	hashBytes2 := sha3.NewLegacyKeccak256()
128	hashBytes2.Write(bytes2) //nolint:errcheck,gosec
129
130	return hashBytes2.Sum(nil), nil
131}
132
133func createPermit(owner, spender ethCommon.Address, amount, deadline *big.Int, digest,
134	signature []byte) []byte {
135	r := signature[0:32]
136	s := signature[32:64]
137	v := signature[64] + byte(27) //nolint:gomnd
138
139	ABIpermit := []byte("permit(address,address,uint256,uint256,uint8,bytes32,bytes32)")
140	hash := sha3.NewLegacyKeccak256()
141	hash.Write(ABIpermit) //nolint:errcheck,gosec
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}