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.

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