Skip to content

Commit

Permalink
update PackUserOp
Browse files Browse the repository at this point in the history
  • Loading branch information
cherry-yl-sh committed Mar 18, 2024
1 parent 8efed7c commit 37316b1
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
46 changes: 46 additions & 0 deletions service/operator/try_pay_user_op_execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ import (
"AAStarCommunity/EthPaymaster_BackService/service/pay_service"
"AAStarCommunity/EthPaymaster_BackService/service/validator_service"
"encoding/hex"
"encoding/json"
"fmt"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"golang.org/x/xerrors"
"strconv"
"strings"
"time"
)

Expand Down Expand Up @@ -128,6 +131,49 @@ func getPayMasterSignature(strategy *model.Strategy, userOp *model.UserOperation
signatureBytes, _ := utils.SignUserOp("1d8a58126e87e53edc7b24d58d1328230641de8c4242c135492bf5560e0ff421", userOp)
return hex.EncodeToString(signatureBytes)
}
func packUserOp(userOp *model.UserOperation) (string, error) {
abiEncoder, err := abi.JSON(strings.NewReader(`[
{"type":"address","name":"sender"},
{"type":"uint256","name":"nonce"},
{"type":"bytes","name":"init_code"},
{"type":"bytes","name":"call_data"},
{"type":"uint256","name":"call_gas_limit"},
{"type":"uint256","name":"verification_gas_limit"},
{"type":"uint256","name":"pre_verification_gas"},
{"type":"uint256","name":"max_fee_per_gas"},
{"type":"uint256","name":"max_priority_fee_per_gas"},
{"type":"bytes","name":"signature"},
{"type":"bytes","name":"paymaster_and_data"}
]`))
if err != nil {
return "", err
}

encoded, err := abiEncoder.Pack("", userOp.Sender.String(), userOp.Nonce, userOp.InitCode,
userOp.CallData, userOp.CallGasLimit, userOp.VerificationGasLimit, userOp.PreVerificationGas,
userOp.MaxFeePerGas, userOp.MaxPriorityFeePerGas)
if err != nil {
return "", err
}
hexString := hex.EncodeToString(encoded)
return hexString, nil
}

func packUserOpSimple(userOp *model.UserOperation) (string, error) {
data, err := json.Marshal(userOp)
if err != nil {
return "", err
}
hexString := hex.EncodeToString(data)

return hexString, nil

}

// func UserOpHash(userOp *model.UserOperation, chainId string, strategy *model.Strategy, validStart string, validEnd string) []byte {
// packUserOp(userOp)
//
// }
func getPayMasterAndData(strategy *model.Strategy, userOp *model.UserOperation, gasResponse *model.ComputeGasResponse) (string, string, error) {
return generatePayMasterAndData(strategy)
}
Expand Down
58 changes: 58 additions & 0 deletions service/operator/try_pay_user_op_execute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,61 @@ func TestGenerateTestData(t *testing.T) {
fmt.Println(signature)
fmt.Println(len(signature))
}
func TestPackUserOp(t *testing.T) {

userOp, _ := model.NewUserOp(utils.GenerateMockUserOperation())
userOp.Signature = nil
userOp.PaymasterAndData = nil
res, err := packUserOp(userOp)

assert.NoError(t, err)

fmt.Println(res)
}
func TestPackUserOpV2(t *testing.T) {
userOp, _ := model.NewUserOp(utils.GenerateMockUserOperation())
userOp.Signature = nil
userOp.PaymasterAndData = nil
res, err := packUserOpSimple(userOp)
assert.NoError(t, err)
fmt.Println(res)
}
func TestUserOP(t *testing.T) {
userOp, _ := model.NewUserOp(utils.GenerateMockUserOperation())
fmt.Println(userOp.Sender.String())
}
func TestGenerateTestPaymaterDataparse(t *testing.T) {
//contractABI, err := abi.JSON([]byte(`[
// {
// "constant": false,
// "inputs": [
// {
// "name": "userOp",
// "type": "tuple"
// },
// {
// "name": "requiredPreFund",
// "type": "uint256"
// }
// ],
// "name": "_validatePaymasterUserOp",
// "outputs": [
// {
// "name": "context",
// "type": "bytes"
// },
// {
// "name": "validationData",
// "type": "uint256"
// }
// ],
// "payable": false,
// "stateMutability": "nonpayable",
// "type": "function"
// }
//]`))
//if err != nil {
// log.Fatal(err)
//}
//str := "0x
}

0 comments on commit 37316b1

Please sign in to comment.