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.1 KiB

  1. package eth
  2. import (
  3. "encoding/hex"
  4. "fmt"
  5. "math/big"
  6. "net/http"
  7. "strconv"
  8. "strings"
  9. ethCommon "github.com/ethereum/go-ethereum/common"
  10. "golang.org/x/crypto/sha3"
  11. )
  12. func addBlock(url string) {
  13. method := "POST"
  14. payload := strings.NewReader("{\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("{\n \"jsonrpc\":\"2.0\",\n \"method\":\"evm_increaseTime\",\n \"params\":[" + secondsStr + "],\n \"id\":1\n}")
  40. client := &http.Client{}
  41. req, err := http.NewRequest(method, url, payload)
  42. if err != nil {
  43. fmt.Println(err)
  44. }
  45. req.Header.Add("Content-Type", "application/json")
  46. res, err := client.Do(req)
  47. if err != nil {
  48. fmt.Println(err)
  49. }
  50. defer func() {
  51. if err := res.Body.Close(); err != nil {
  52. fmt.Println("Error when closing:", err)
  53. }
  54. }()
  55. }
  56. func createPermitDigest(tokenAddr, owner, spender ethCommon.Address, chainID, value, nonce, deadline *big.Int, tokenName string) ([]byte, error) {
  57. abiPermit := []byte("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)")
  58. hashPermit := sha3.NewLegacyKeccak256()
  59. hashPermit.Write(abiPermit)
  60. abiEIP712Domain := []byte("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)")
  61. hashEIP712Domain := sha3.NewLegacyKeccak256()
  62. hashEIP712Domain.Write(abiEIP712Domain)
  63. var encodeBytes []byte
  64. paddedHash := ethCommon.LeftPadBytes(hashEIP712Domain.Sum(nil), 32)
  65. hashName := sha3.NewLegacyKeccak256()
  66. hashName.Write([]byte(tokenName))
  67. paddedName := ethCommon.LeftPadBytes(hashName.Sum(nil), 32)
  68. hashVersion := sha3.NewLegacyKeccak256()
  69. hashVersion.Write([]byte("1"))
  70. paddedX := ethCommon.LeftPadBytes(hashVersion.Sum(nil), 32)
  71. paddedChainID := ethCommon.LeftPadBytes(chainID.Bytes(), 32)
  72. paddedAddr := ethCommon.LeftPadBytes(tokenAddr.Bytes(), 32)
  73. encodeBytes = append(encodeBytes, paddedHash...)
  74. encodeBytes = append(encodeBytes, paddedName...)
  75. encodeBytes = append(encodeBytes, paddedX...)
  76. encodeBytes = append(encodeBytes, paddedChainID...)
  77. encodeBytes = append(encodeBytes, paddedAddr...)
  78. _domainSeparator := sha3.NewLegacyKeccak256()
  79. _domainSeparator.Write(encodeBytes)
  80. var bytes1 []byte
  81. paddedHashPermit := ethCommon.LeftPadBytes(hashPermit.Sum(nil), 32)
  82. paddedOwner := ethCommon.LeftPadBytes(owner.Bytes(), 32)
  83. paddedSpender := ethCommon.LeftPadBytes(spender.Bytes(), 32)
  84. paddedValue := ethCommon.LeftPadBytes(value.Bytes(), 32)
  85. paddedNonce := ethCommon.LeftPadBytes(nonce.Bytes(), 32)
  86. paddedDeadline := ethCommon.LeftPadBytes(deadline.Bytes(), 32)
  87. bytes1 = append(bytes1, paddedHashPermit...)
  88. bytes1 = append(bytes1, paddedOwner...)
  89. bytes1 = append(bytes1, paddedSpender...)
  90. bytes1 = append(bytes1, paddedValue...)
  91. bytes1 = append(bytes1, paddedNonce...)
  92. bytes1 = append(bytes1, paddedDeadline...)
  93. hashBytes1 := sha3.NewLegacyKeccak256()
  94. hashBytes1.Write(bytes1)
  95. var bytes2 []byte
  96. byte19, err := hex.DecodeString("19")
  97. if err != nil {
  98. return nil, err
  99. }
  100. byte01, err := hex.DecodeString("01")
  101. if err != nil {
  102. return nil, err
  103. }
  104. paddedY := ethCommon.LeftPadBytes(byte19, 1)
  105. paddedZ := ethCommon.LeftPadBytes(byte01, 1)
  106. paddedDomainSeparator := ethCommon.LeftPadBytes(_domainSeparator.Sum(nil), 32)
  107. paddedHashBytes1 := ethCommon.LeftPadBytes(hashBytes1.Sum(nil), 32)
  108. bytes2 = append(bytes2, paddedY...)
  109. bytes2 = append(bytes2, paddedZ...)
  110. bytes2 = append(bytes2, paddedDomainSeparator...)
  111. bytes2 = append(bytes2, paddedHashBytes1...)
  112. hashBytes2 := sha3.NewLegacyKeccak256()
  113. hashBytes2.Write(bytes2)
  114. return hashBytes2.Sum(nil), nil
  115. }
  116. func createPermit(owner, spender ethCommon.Address, amount, deadline *big.Int, digest, signature []byte) []byte {
  117. r := signature[0:32]
  118. s := signature[32:64]
  119. v := signature[64] + byte(27)
  120. ABIpermit := []byte("permit(address,address,uint256,uint256,uint8,bytes32,bytes32)")
  121. hash := sha3.NewLegacyKeccak256()
  122. hash.Write(ABIpermit)
  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. }