Ethclient remove ERC777

This commit is contained in:
laisolizq
2020-10-21 20:58:40 +02:00
committed by Eduard S
parent b70001f92f
commit d4753c7e1b
18 changed files with 1566 additions and 2365 deletions

View File

@@ -1,10 +1,15 @@
package eth
import (
"encoding/hex"
"fmt"
"math/big"
"net/http"
"strconv"
"strings"
ethCommon "github.com/ethereum/go-ethereum/common"
"golang.org/x/crypto/sha3"
)
func addBlock(url string) {
@@ -61,3 +66,96 @@ func addTime(seconds float64, url string) {
}
}()
}
func createPermitDigest(tokenAddr, owner, spender ethCommon.Address, chainID, value, nonce, deadline *big.Int, tokenName string) ([]byte, error) {
abiPermit := []byte("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)")
hashPermit := sha3.NewLegacyKeccak256()
hashPermit.Write(abiPermit)
abiEIP712Domain := []byte("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)")
hashEIP712Domain := sha3.NewLegacyKeccak256()
hashEIP712Domain.Write(abiEIP712Domain)
var encodeBytes []byte
paddedHash := ethCommon.LeftPadBytes(hashEIP712Domain.Sum(nil), 32)
hashName := sha3.NewLegacyKeccak256()
hashName.Write([]byte(tokenName))
paddedName := ethCommon.LeftPadBytes(hashName.Sum(nil), 32)
hashVersion := sha3.NewLegacyKeccak256()
hashVersion.Write([]byte("1"))
paddedX := ethCommon.LeftPadBytes(hashVersion.Sum(nil), 32)
paddedChainID := ethCommon.LeftPadBytes(chainID.Bytes(), 32)
paddedAddr := ethCommon.LeftPadBytes(tokenAddr.Bytes(), 32)
encodeBytes = append(encodeBytes, paddedHash...)
encodeBytes = append(encodeBytes, paddedName...)
encodeBytes = append(encodeBytes, paddedX...)
encodeBytes = append(encodeBytes, paddedChainID...)
encodeBytes = append(encodeBytes, paddedAddr...)
_domainSeparator := sha3.NewLegacyKeccak256()
_domainSeparator.Write(encodeBytes)
var bytes1 []byte
paddedHashPermit := ethCommon.LeftPadBytes(hashPermit.Sum(nil), 32)
paddedOwner := ethCommon.LeftPadBytes(owner.Bytes(), 32)
paddedSpender := ethCommon.LeftPadBytes(spender.Bytes(), 32)
paddedValue := ethCommon.LeftPadBytes(value.Bytes(), 32)
paddedNonce := ethCommon.LeftPadBytes(nonce.Bytes(), 32)
paddedDeadline := ethCommon.LeftPadBytes(deadline.Bytes(), 32)
bytes1 = append(bytes1, paddedHashPermit...)
bytes1 = append(bytes1, paddedOwner...)
bytes1 = append(bytes1, paddedSpender...)
bytes1 = append(bytes1, paddedValue...)
bytes1 = append(bytes1, paddedNonce...)
bytes1 = append(bytes1, paddedDeadline...)
hashBytes1 := sha3.NewLegacyKeccak256()
hashBytes1.Write(bytes1)
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)
paddedDomainSeparator := ethCommon.LeftPadBytes(_domainSeparator.Sum(nil), 32)
paddedHashBytes1 := ethCommon.LeftPadBytes(hashBytes1.Sum(nil), 32)
bytes2 = append(bytes2, paddedY...)
bytes2 = append(bytes2, paddedZ...)
bytes2 = append(bytes2, paddedDomainSeparator...)
bytes2 = append(bytes2, paddedHashBytes1...)
hashBytes2 := sha3.NewLegacyKeccak256()
hashBytes2.Write(bytes2)
return hashBytes2.Sum(nil), nil
}
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)
ABIpermit := []byte("permit(address,address,uint256,uint256,uint8,bytes32,bytes32)")
hash := sha3.NewLegacyKeccak256()
hash.Write(ABIpermit)
methodID := hash.Sum(nil)[:4]
var permit []byte
paddedOwner := ethCommon.LeftPadBytes(owner.Bytes(), 32)
paddedSpender := ethCommon.LeftPadBytes(spender.Bytes(), 32)
paddedAmount := ethCommon.LeftPadBytes(amount.Bytes(), 32)
paddedDeadline := ethCommon.LeftPadBytes(deadline.Bytes(), 32)
paddedV := ethCommon.LeftPadBytes([]byte{v}, 32)
permit = append(permit, methodID...)
permit = append(permit, paddedOwner...)
permit = append(permit, paddedSpender...)
permit = append(permit, paddedAmount...)
permit = append(permit, paddedDeadline...)
permit = append(permit, paddedV...)
permit = append(permit, r...)
permit = append(permit, s...)
return permit
}