You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

154 lines
5.2 KiB

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