Skip to content

Commit

Permalink
Merge pull request #19 from AAStarCommunity/v1.0-try-pay
Browse files Browse the repository at this point in the history
v1.0 tryPay
  • Loading branch information
cherry-yl-sh committed Mar 7, 2024
2 parents 7004dee + ff7d4c5 commit 4991f0d
Show file tree
Hide file tree
Showing 28 changed files with 665 additions and 67 deletions.
11 changes: 7 additions & 4 deletions common/model/api_request.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package model

import "errors"
import (
"AAStarCommunity/EthPaymaster_BackService/common/types"
"errors"
)

type TryPayUserOpRequest struct {
ForceStrategyId string `json:"force_strategy_id"`
ForceNetWork string `json:"force_network"`
ForceTokens string `json:"force_tokens"`
ForceNetwork types.NetWork `json:"force_network"`
ForceToken string `json:"force_token"`
ForceEntryPointAddress string `json:"force_entrypoint_address"`
UserOperation UserOperationItem `json:"user_operation"`
Extra interface{} `json:"extra"`
}

func (request *TryPayUserOpRequest) Validate() error {
if len(request.ForceStrategyId) == 0 {
if len(request.ForceNetWork) == 0 || len(request.ForceTokens) == 0 || len(request.ForceEntryPointAddress) == 0 {
if len(request.ForceNetwork) == 0 || len(request.ForceToken) == 0 || len(request.ForceEntryPointAddress) == 0 {
return errors.New("strategy configuration illegal")
}
}
Expand Down
24 changes: 18 additions & 6 deletions common/model/api_response.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
package model

import (
"AAStarCommunity/EthPaymaster_BackService/common/types"
"math/big"
)

type TryPayUserOpResponse struct {
StrategyId string `json:"strategy_id"`
EntryPointAddress string `json:"entrypoint_address"`
PayMasterAddress string `json:"paymaster_address"`
PayMasterSignature string `json:"paymaster_signature"`
PayReceipt interface{} `json:"pay_receipt"`
PayReceipt *PayReceipt `json:"pay_receipt"`
GasInfo *ComputeGasResponse `json:"gas_info"`
}

type ComputeGasResponse struct {
StrategyId string `json:"strategy_id"`
TokenCost string `json:"token_cost"`
Network string `json:"network"`
Token string `json:"token"`
UsdCost string `json:"usd_cost"`
GasPriceInWei uint64 `json:"gas_price_wei"` // wei
GasPriceInGwei *big.Float `json:"gas_price_gwei"`
GasPriceInEther string `json:"gas_price_ether"`
TokenCost string `json:"token_cost"`
Network types.NetWork `json:"network"`
Token types.TokenType `json:"token"`
UsdCost string `json:"usd_cost"`
BlobEnable bool `json:"blob_enable"`
}
type PayReceipt struct {
TransactionHash string `json:"transaction_hash"`
Sponsor string `json:"sponsor"`
}
29 changes: 26 additions & 3 deletions common/model/strategy.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
package model

import "AAStarCommunity/EthPaymaster_BackService/common/types"

type Strategy struct {
Id string
EntryPointAddress string
PayMasterAddress string
Id string `json:"id"`
EntryPointAddress string `json:"entrypoint_address"`
PayMasterAddress string `json:"paymaster_address"`
NetWork types.NetWork `json:"network"`
Token types.TokenType `json:"token"`
Description string `json:"description"`
ExecuteRestriction StrategyExecuteRestriction `json:"execute_restriction"`
EnableEoa bool `json:"enable_eoa"`
Enable7560 bool `json:"enable_7560"`
EnableErc20 bool `json:"enable_erc20"`
Enable4844 bool `json:"enable_4844"`
EnableCurrency bool `json:"enable_currency"`
}
type StrategyExecuteRestriction struct {
BanSenderAddress string `json:"ban_sender_address"`
EffectiveStartTime int64 `json:"effective_start_time"`
EffectiveEndTime int64 `json:"effective_end_time"`
GlobalMaxUSD int64 `json:"global_max_usd"`
GlobalMaxOpCount int64 `json:"global_max_op_count"`
DayMaxUSD int64 `json:"day_max_usd"`
}

type StrategyValidateConfig struct {
ValidateContractAddress string `json:"validate_contract_address"`
}
3 changes: 2 additions & 1 deletion common/model/user_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ type UserOperationItem struct {
Sender string `json:"sender" binding:"required"`
Nonce string `json:"nonce" binding:"required"`
InitCode string `json:"init_code"`
CallData string `json:"call_data" binding:"required"`
CallGasLimit string `json:"call_gas_limit" binding:"required"`
VerificationGasList string `json:"verification_gas_list" binding:"required"`
PerVerificationGas string `json:"per_verification_gas" binding:"required"`
PreVerificationGas string `json:"per_verification_gas" binding:"required"`
MaxFeePerGas string `json:"max_fee_per_gas" binding:"required"`
MaxPriorityFeePerGas string `json:"max_priority_fee_per_gas" binding:"required"`
Signature string `json:"signature"`
Expand Down
22 changes: 22 additions & 0 deletions common/types/chain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package types

type NetworkInfo struct {
Name string `json:"main_net_name"`
RpcUrl string `json:"main_net_rpc_url"`
}

//type Chain string
//
//const (
// Ethereum Chain = "Ethereum"
// Arbitrum Chain = "Arbitrum"
// Optimism Chain = "Optimism"
//)

type NetWork string

const (
Ethereum NetWork = "ethereum"
Sepolia NetWork = "sepolia"
Arbitrum NetWork = "arbitrum"
)
7 changes: 7 additions & 0 deletions common/types/currency.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package types

type Currency string

const (
USD Currency = "USD"
)
8 changes: 8 additions & 0 deletions common/types/token.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package types

type TokenType string

const (
USDT TokenType = "USDT"
ETH TokenType = "ETH"
)
57 changes: 57 additions & 0 deletions common/utils/util.go
Original file line number Diff line number Diff line change
@@ -1 +1,58 @@
package utils

import (
"AAStarCommunity/EthPaymaster_BackService/common/model"
"encoding/json"
"github.com/ethereum/go-ethereum/crypto"
)

func GenerateMockUserOperation() *model.UserOperationItem {
//TODO use config
return &model.UserOperationItem{
Sender: "0x4A2FD3215420376DA4eD32853C19E4755deeC4D1",
Nonce: "1",
InitCode: "0x",
CallData: "0xb61d27f6000000000000000000000000c206b552ab127608c3f666156c8e03a8471c72df000000000000000000000000000000000000000000000000002386f26fc1000000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000",
CallGasLimit: "39837",
VerificationGasList: "100000",
PreVerificationGas: "44020",
MaxFeePerGas: "1743509478",
MaxPriorityFeePerGas: "1500000000",
Signature: "0x760868cd7d9539c6e31c2169c4cab6817beb8247516a90e4301e929011451658623455035b83d38e987ef2e57558695040a25219c39eaa0e31a0ead16a5c925c1c",
}
}
func GenerateUserOperation() *model.UserOperationItem {
return &model.UserOperationItem{}
}

func SignUserOp(privateKeyHex string, userOp *model.UserOperationItem) ([]byte, error) {

serializedUserOp, err := json.Marshal(userOp)
if err != nil {
return nil, err
}

signature, err := SignMessage(privateKeyHex, string(serializedUserOp))
if err != nil {
return nil, err
}

return signature, nil
}
func SignMessage(privateKeyHex string, message string) ([]byte, error) {
privateKey, err := crypto.HexToECDSA(privateKeyHex)
if err != nil {
return nil, err
}

// Hash the message
hash := crypto.Keccak256([]byte(message))

// Sign the hash
signature, err := crypto.Sign(hash, privateKey)
if err != nil {
return nil, err
}

return signature, nil
}
33 changes: 33 additions & 0 deletions common/utils/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package utils

import (
"crypto/ecdsa"
"encoding/hex"
"fmt"
"github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/assert"
"testing"
)

func TestGenerateKeypair(t *testing.T) {
privateKey, _ := crypto.GenerateKey()
privateKeyHex := crypto.FromECDSA(privateKey)
publicKey := privateKey.Public()
publicKeyECDSA, _ := publicKey.(*ecdsa.PublicKey)
publicKeyBytes := crypto.FromECDSAPub(publicKeyECDSA)
address := crypto.PubkeyToAddress(*publicKeyECDSA).Hex()
fmt.Printf("privateKeyHex: %x\n", privateKeyHex)
fmt.Printf("publicKey: %x\n", publicKeyBytes)
fmt.Printf("address: %s\n", address)
}
func TestSignUserOp(t *testing.T) {
//privateKeyHex: 1d8a58126e87e53edc7b24d58d1328230641de8c4242c135492bf5560e0ff421
//publicKey: 044eaed6b1f16e60354156fa334a094affc76d7b7061875a0b04290af9a14cc14ce2bce6ceba941856bd55c63f8199f408fff6495ce9d4c76899055972d23bdb3e
//address: 0x0E1375d18a4A2A867bEfe908E87322ad031386a6
signByte, err := SignUserOp("1d8a58126e87e53edc7b24d58d1328230641de8c4242c135492bf5560e0ff421", GenerateMockUserOperation())
assert.NoError(t, err)
fmt.Printf("signByte: %x\n", signByte)
singature := hex.EncodeToString(signByte)
fmt.Printf("singature: %s\n", singature)

}
2 changes: 1 addition & 1 deletion conf/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func getConfFilePath() *string {
var Environment *Env

func init() {
envName := ProdEnv
envName := DevEnv
if len(os.Getenv(envKey)) > 0 {
envName = os.Getenv(envKey)
}
Expand Down
21 changes: 19 additions & 2 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,12 @@ const docTemplate = `{
"type": "string"
},
"force_network": {
"type": "string"
"$ref": "#/definitions/types.NetWork"
},
"force_strategy_id": {
"type": "string"
},
"force_tokens": {
"force_token": {
"type": "string"
},
"user_operation": {
Expand All @@ -172,6 +172,7 @@ const docTemplate = `{
"model.UserOperationItem": {
"type": "object",
"required": [
"call_data",
"call_gas_limit",
"max_fee_per_gas",
"max_priority_fee_per_gas",
Expand All @@ -181,6 +182,9 @@ const docTemplate = `{
"verification_gas_list"
],
"properties": {
"call_data": {
"type": "string"
},
"call_gas_limit": {
"type": "string"
},
Expand Down Expand Up @@ -209,6 +213,19 @@ const docTemplate = `{
"type": "string"
}
}
},
"types.NetWork": {
"type": "string",
"enum": [
"ethereum",
"sepolia",
"arbitrum"
],
"x-enum-varnames": [
"Ethereum",
"Sepolia",
"Arbitrum"
]
}
},
"securityDefinitions": {
Expand Down
21 changes: 19 additions & 2 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,12 @@
"type": "string"
},
"force_network": {
"type": "string"
"$ref": "#/definitions/types.NetWork"
},
"force_strategy_id": {
"type": "string"
},
"force_tokens": {
"force_token": {
"type": "string"
},
"user_operation": {
Expand All @@ -161,6 +161,7 @@
"model.UserOperationItem": {
"type": "object",
"required": [
"call_data",
"call_gas_limit",
"max_fee_per_gas",
"max_priority_fee_per_gas",
Expand All @@ -170,6 +171,9 @@
"verification_gas_list"
],
"properties": {
"call_data": {
"type": "string"
},
"call_gas_limit": {
"type": "string"
},
Expand Down Expand Up @@ -198,6 +202,19 @@
"type": "string"
}
}
},
"types.NetWork": {
"type": "string",
"enum": [
"ethereum",
"sepolia",
"arbitrum"
],
"x-enum-varnames": [
"Ethereum",
"Sepolia",
"Arbitrum"
]
}
},
"securityDefinitions": {
Expand Down
Loading

0 comments on commit 4991f0d

Please sign in to comment.