From ca6fcc54617cfe8eaf4bb3bd65cd2c388efe15d7 Mon Sep 17 00:00:00 2001 From: dylanyang Date: Tue, 19 Mar 2024 15:03:39 +0800 Subject: [PATCH] update PackUserOp --- service/operator/try_pay_user_op_execute.go | 98 ++++++++++++------- .../operator/try_pay_user_op_execute_test.go | 5 +- 2 files changed, 63 insertions(+), 40 deletions(-) diff --git a/service/operator/try_pay_user_op_execute.go b/service/operator/try_pay_user_op_execute.go index 95ad8909..c15a6f14 100644 --- a/service/operator/try_pay_user_op_execute.go +++ b/service/operator/try_pay_user_op_execute.go @@ -16,6 +16,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" "golang.org/x/xerrors" + "math/big" "strconv" "strings" "time" @@ -216,62 +217,82 @@ func packUserOp(userOp *model.UserOperation) (string, []byte, error) { return hexString, encoded, nil } -func UserOpHash(userOp *model.UserOperation, strategy *model.Strategy, validStart string, validEnd string) ([]byte, error) { +func UserOpHash(userOp *model.UserOperation, strategy *model.Strategy, validStart *big.Int, validEnd *big.Int) ([]byte, error) { _, packUserOpStrByte, err := packUserOp(userOp) if err != nil { return nil, err } + abiEncoder, err := abi.JSON(strings.NewReader(`[ { - "name": "bar", - "type": "function", "inputs": [ - { - "type": "uint256", - "name": "userOp" - }, - { - "type": "uint256", - "name": "_chainID" - }, - { - "type": "address", - "name": "_thisAddress" - }, - { - "type": "uint256", - "name": "_senderNonce" - }, { - "type": "uint256", - "name": "_validUntil" - }, - { - "type": "uint256", - "name": "_validAfter" + "components": [ + { + "internalType": "bytes", + "name": "userOpHash", + "type": "bytes" + }, + { + "internalType": "uint256", + "name": "chainId", + "type": "uint256" + }, + { + "internalType": "address", + "name": "address", + "type": "address" + }, + { + "internalType": "uint48", + "name": "validUtil", + "type": "uint48" + }, + { + "internalType": "uint48", + "name": "validAfter", + "type": "uint48" + } + ], + "internalType": "struct hash", + "name": "hash", + "type": "tuple" } ], - "outputs": [ - { - "type": "bytes32", - "name": "_result" - } - ] + "name": "Hash", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" } ]`)) - if err != nil { - return nil, err - } chainId, err := chain_service.GetChainId(strategy.NetWork) if err != nil { return nil, err } - data, err := abiEncoder.Pack("bar", &packUserOpStrByte, &chainId, &strategy.PayMasterAddress, &userOp.Nonce, &validStart, &validEnd) + hashStruct := struct { + UserOpHash []byte + ChainId *big.Int + Address common.Address + Nonce *big.Int + ValidUtil *big.Int + ValidAfter *big.Int + }{ + packUserOpStrByte, + chainId, + common.HexToAddress(strategy.PayMasterAddress), + userOp.Nonce, + validStart, + validEnd, + } + + chainId.Int64() + + data, err := abiEncoder.Pack("Hash", hashStruct) if err != nil { return nil, err } + fmt.Println(hex.EncodeToString(data)) encodeHash := crypto.Keccak256Hash(data) - return encodeHash.Bytes(), nil } @@ -297,7 +318,10 @@ func generatePayMasterAndData(userOp *model.UserOperation, strategy *model.Strat } func SignPaymaster(userOp *model.UserOperation, strategy *model.Strategy, validStart string, validEnd string) ([]byte, error) { - userOpHash, err := UserOpHash(userOp, strategy, validStart, validEnd) + //string to int + validStartInt, _ := strconv.ParseInt(validStart, 10, 64) + validEndInt, _ := strconv.ParseInt(validEnd, 10, 64) + userOpHash, err := UserOpHash(userOp, strategy, big.NewInt(validStartInt), big.NewInt(validEndInt)) if err != nil { return nil, err } diff --git a/service/operator/try_pay_user_op_execute_test.go b/service/operator/try_pay_user_op_execute_test.go index 37b48e1c..9389c068 100644 --- a/service/operator/try_pay_user_op_execute_test.go +++ b/service/operator/try_pay_user_op_execute_test.go @@ -8,6 +8,7 @@ import ( "encoding/json" "fmt" "github.com/stretchr/testify/assert" + "math/big" "testing" ) @@ -47,13 +48,11 @@ func TestPackUserOp(t *testing.T) { } func TestUserOpHash(t *testing.T) { - validStart, validEnd := getValidTime() strategy := dashboard_service.GetStrategyById("1") userOp, _ := model.NewUserOp(utils.GenerateMockUserOperation()) - userOpHash, err := UserOpHash(userOp, strategy, validStart, validEnd) + userOpHash, err := UserOpHash(userOp, strategy, big.NewInt(1), big.NewInt(2)) assert.NoError(t, err) fmt.Println(hex.EncodeToString(userOpHash)) - } func TestUserOP(t *testing.T) {