Update importers of eth, review eth/README.md

This commit is contained in:
Eduard S
2020-10-26 16:01:38 +01:00
parent 2e67008142
commit 79b105b497
17 changed files with 157 additions and 126 deletions

View File

@@ -1,7 +1,6 @@
package eth
import (
"encoding/hex"
"fmt"
"math/big"
"net/http"
@@ -68,19 +67,21 @@ func addTime(seconds float64, url string) {
}
func createPermitDigest(tokenAddr, owner, spender ethCommon.Address, chainID, value, nonce, deadline *big.Int, tokenName string) ([]byte, error) {
// NOTE: We ignore hash.Write errors because we are writing to a memory
// buffer and don't expect any errors to occur.
abiPermit := []byte("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)")
hashPermit := sha3.NewLegacyKeccak256()
hashPermit.Write(abiPermit)
hashPermit.Write(abiPermit) //nolint:errcheck,gosec
abiEIP712Domain := []byte("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)")
hashEIP712Domain := sha3.NewLegacyKeccak256()
hashEIP712Domain.Write(abiEIP712Domain)
hashEIP712Domain.Write(abiEIP712Domain) //nolint:errcheck,gosec
var encodeBytes []byte
paddedHash := ethCommon.LeftPadBytes(hashEIP712Domain.Sum(nil), 32)
hashName := sha3.NewLegacyKeccak256()
hashName.Write([]byte(tokenName))
hashName.Write([]byte(tokenName)) //nolint:errcheck,gosec
paddedName := ethCommon.LeftPadBytes(hashName.Sum(nil), 32)
hashVersion := sha3.NewLegacyKeccak256()
hashVersion.Write([]byte("1"))
hashVersion.Write([]byte("1")) //nolint:errcheck,gosec
paddedX := ethCommon.LeftPadBytes(hashVersion.Sum(nil), 32)
paddedChainID := ethCommon.LeftPadBytes(chainID.Bytes(), 32)
paddedAddr := ethCommon.LeftPadBytes(tokenAddr.Bytes(), 32)
@@ -90,7 +91,7 @@ func createPermitDigest(tokenAddr, owner, spender ethCommon.Address, chainID, va
encodeBytes = append(encodeBytes, paddedChainID...)
encodeBytes = append(encodeBytes, paddedAddr...)
_domainSeparator := sha3.NewLegacyKeccak256()
_domainSeparator.Write(encodeBytes)
_domainSeparator.Write(encodeBytes) //nolint:errcheck,gosec
var bytes1 []byte
paddedHashPermit := ethCommon.LeftPadBytes(hashPermit.Sum(nil), 32)
@@ -106,19 +107,11 @@ func createPermitDigest(tokenAddr, owner, spender ethCommon.Address, chainID, va
bytes1 = append(bytes1, paddedNonce...)
bytes1 = append(bytes1, paddedDeadline...)
hashBytes1 := sha3.NewLegacyKeccak256()
hashBytes1.Write(bytes1)
hashBytes1.Write(bytes1) //nolint:errcheck,gosec
var bytes2 []byte
byte19, err := hex.DecodeString("19")
if err != nil {
return nil, err
}
byte01, err := hex.DecodeString("01")
if err != nil {
return nil, err
}
paddedY := ethCommon.LeftPadBytes(byte19, 1)
paddedZ := ethCommon.LeftPadBytes(byte01, 1)
paddedY := ethCommon.LeftPadBytes([]byte{0x19}, 1)
paddedZ := ethCommon.LeftPadBytes([]byte{0x01}, 1)
paddedDomainSeparator := ethCommon.LeftPadBytes(_domainSeparator.Sum(nil), 32)
paddedHashBytes1 := ethCommon.LeftPadBytes(hashBytes1.Sum(nil), 32)
bytes2 = append(bytes2, paddedY...)
@@ -126,7 +119,7 @@ func createPermitDigest(tokenAddr, owner, spender ethCommon.Address, chainID, va
bytes2 = append(bytes2, paddedDomainSeparator...)
bytes2 = append(bytes2, paddedHashBytes1...)
hashBytes2 := sha3.NewLegacyKeccak256()
hashBytes2.Write(bytes2)
hashBytes2.Write(bytes2) //nolint:errcheck,gosec
return hashBytes2.Sum(nil), nil
}
@@ -134,11 +127,11 @@ func createPermitDigest(tokenAddr, owner, spender ethCommon.Address, chainID, va
func createPermit(owner, spender ethCommon.Address, amount, deadline *big.Int, digest, signature []byte) []byte {
r := signature[0:32]
s := signature[32:64]
v := signature[64] + byte(27)
v := signature[64] + byte(27) //nolint:gomnd
ABIpermit := []byte("permit(address,address,uint256,uint256,uint8,bytes32,bytes32)")
hash := sha3.NewLegacyKeccak256()
hash.Write(ABIpermit)
hash.Write(ABIpermit) //nolint:errcheck,gosec
methodID := hash.Sum(nil)[:4]
var permit []byte