diff --git a/service/chain_service/chain_service.go b/service/chain_service/chain_service.go index 27e0c244..507082fe 100644 --- a/service/chain_service/chain_service.go +++ b/service/chain_service/chain_service.go @@ -160,3 +160,10 @@ func GetAddressTokenBalance(network types.Network, address common.Address, token return balanceResultFloat, nil } +func GetChainId(chain types.Network) (*big.Int, error) { + client, exist := EthCompatibleNetWorkClientMap[chain] + if !exist { + return nil, xerrors.Errorf("chain Client [%s] not exist", chain) + } + return client.ChainID(context.Background()) +} diff --git a/service/chain_service/chain_test.go b/service/chain_service/chain_test.go index 729d1154..7c55121e 100644 --- a/service/chain_service/chain_test.go +++ b/service/chain_service/chain_test.go @@ -36,3 +36,9 @@ func TestGetAddressTokenBalance(t *testing.T) { assert.NoError(t, err) fmt.Println(res) } + +func TestGetChainId(t *testing.T) { + res, err := GetChainId(types.Sepolia) + assert.NoError(t, err) + fmt.Println(res) +} diff --git a/service/operator/try_pay_user_op_execute.go b/service/operator/try_pay_user_op_execute.go index 1c343d1b..1b8fa678 100644 --- a/service/operator/try_pay_user_op_execute.go +++ b/service/operator/try_pay_user_op_execute.go @@ -11,10 +11,11 @@ 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" + "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/rlp" "golang.org/x/xerrors" "strconv" "strings" @@ -131,7 +132,7 @@ func getPayMasterSignature(strategy *model.Strategy, userOp *model.UserOperation signatureBytes, _ := utils.SignUserOp("1d8a58126e87e53edc7b24d58d1328230641de8c4242c135492bf5560e0ff421", userOp) return hex.EncodeToString(signatureBytes) } -func packUserOp(userOp *model.UserOperation) (string, error) { +func packUserOp(userOp *model.UserOperation) (string, []byte, error) { abiEncoder, err := abi.JSON(strings.NewReader(`[ { "inputs": [ @@ -205,32 +206,35 @@ func packUserOp(userOp *model.UserOperation) (string, error) { } ]`)) if err != nil { - return "", err + return "", nil, err } encoded, err := abiEncoder.Pack("test", userOp) if err != nil { - return "", err + return "", nil, err } hexString := hex.EncodeToString(encoded) - return hexString, nil + return hexString, encoded, nil } -func packUserOpSimple(userOp *model.UserOperation) (string, error) { - data, err := json.Marshal(userOp) +func UserOpHash(userOp *model.UserOperation, strategy *model.Strategy, validStart string, validEnd string) ([]byte, error) { + _, packUserOpStrByte, err := packUserOp(userOp) if err != nil { - return "", err - } - hexString := hex.EncodeToString(data) - - return hexString, nil + return nil, nil + } + chainId, err := chain_service.GetChainId(strategy.NetWork) + if err != nil { + return nil, nil + } + input := []interface{}{packUserOpStrByte, chainId.Int64(), strategy.PayMasterAddress, userOp.Nonce, validStart, validEnd} + encodeRes, err := rlp.EncodeToBytes(input) + if err != nil { + return nil, nil + } + byteRes := crypto.Keccak256(encodeRes) + return byteRes, 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) } diff --git a/service/operator/try_pay_user_op_execute_test.go b/service/operator/try_pay_user_op_execute_test.go index b9da05e5..cfdb7af2 100644 --- a/service/operator/try_pay_user_op_execute_test.go +++ b/service/operator/try_pay_user_op_execute_test.go @@ -37,20 +37,17 @@ func TestPackUserOp(t *testing.T) { userOp, _ := model.NewUserOp(utils.GenerateMockUserOperation()) userOp.PaymasterAndData = nil userOp.Signature = nil - res, err := packUserOp(userOp) + res, byteres, err := packUserOp(userOp) assert.NoError(t, err) fmt.Println(res) - + fmt.Println(byteres) } -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 TestUserOpHash(t *testing.T) { + } + func TestUserOP(t *testing.T) { userOp, _ := model.NewUserOp(utils.GenerateMockUserOperation()) fmt.Println(userOp.Sender.String())