-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from AAStarCommunity/v1.0-try-pay
v1.0 tryPay
- Loading branch information
Showing
28 changed files
with
665 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package types | ||
|
||
type Currency string | ||
|
||
const ( | ||
USD Currency = "USD" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.