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 19, 2024
1 parent 1282794 commit ca6fcc5
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 40 deletions.
98 changes: 61 additions & 37 deletions service/operator/try_pay_user_op_execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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

}
Expand All @@ -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
}
Expand Down
5 changes: 2 additions & 3 deletions service/operator/try_pay_user_op_execute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"encoding/json"
"fmt"
"github.com/stretchr/testify/assert"
"math/big"
"testing"
)

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit ca6fcc5

Please sign in to comment.