Skip to content

Commit

Permalink
v1.0 tryPay
Browse files Browse the repository at this point in the history
  • Loading branch information
cherry-yl-sh committed Mar 6, 2024
1 parent 0738f71 commit 90989a4
Show file tree
Hide file tree
Showing 25 changed files with 508 additions and 60 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"
)
56 changes: 56 additions & 0 deletions common/utils/util.go
Original file line number Diff line number Diff line change
@@ -1 +1,57 @@
package utils

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

func GenerateMockUserOperation() *model.UserOperationItem {
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
23 changes: 23 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,24 @@ require (

require (
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/StackExchange/wmi v1.2.1 // indirect
github.com/bits-and-blooms/bitset v1.10.0 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.2.0 // indirect
github.com/bytedance/sonic v1.11.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.1 // indirect
github.com/consensys/bavard v0.1.13 // indirect
github.com/consensys/gnark-crypto v0.12.1 // indirect
github.com/crate-crypto/go-kzg-4844 v0.7.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/deckarep/golang-set/v2 v2.1.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
github.com/ethereum/c-kzg-4844 v0.4.0 // indirect
github.com/ethereum/go-ethereum v1.13.14 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-ole/go-ole v1.3.0 // indirect
github.com/go-openapi/jsonpointer v0.20.2 // indirect
github.com/go-openapi/jsonreference v0.20.4 // indirect
github.com/go-openapi/spec v0.20.14 // indirect
Expand All @@ -31,28 +43,39 @@ require (
github.com/go-playground/validator/v10 v10.18.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/holiman/uint256 v1.2.4 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mmcloughlin/addchain v0.4.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible // indirect
github.com/supranational/blst v0.3.11 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
golang.org/x/arch v0.7.0 // indirect
golang.org/x/crypto v0.20.0 // indirect
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa // indirect
golang.org/x/mod v0.15.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.18.0 // indirect
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 // indirect
google.golang.org/protobuf v1.32.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
rsc.io/tmplfunc v0.0.3 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)
Loading

0 comments on commit 90989a4

Please sign in to comment.