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 86c5be9 commit bbb2fd9
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 75 deletions.
2 changes: 1 addition & 1 deletion common/utils/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func GenerateMockUserOperation() *map[string]any {
"init_code": "0x9406cc6185a346906296840746125a0e449764545fbfb9cf000000000000000000000000340966abb6e37a06014546e0542b3aafad4550810000000000000000000000000000000000000000000000000000000000000000",
"max_fee_per_gas": "0x2aa887baca",
"max_priority_fee_per_gas": "0x59682f00",
"nonce": "0x01",
"nonce": "0x00",
"pre_verification_gas": "0xae64",
"sender": "0xF8498599744BC37e141cb800B67Dbf103a6b5881",
"signature": "0xfffffffffffffffffffffffffffffff0000000000000000000000000000000007aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa1c",
Expand Down
114 changes: 42 additions & 72 deletions service/operator/try_pay_user_op_execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,97 +211,67 @@ func packUserOp(userOp *model.UserOperation) (string, []byte, error) {
method := abiEncoder.Methods["UserOp"]
encoded, err := method.Inputs.Pack(userOp)


if err != nil {
return "", nil, err
}
//https://github.com/jayden-sudo/SoulWalletCore/blob/dc76bdb9a156d4f99ef41109c59ab99106c193ac/contracts/utils/CalldataPack.sol#L51-L65


hexString := hex.EncodeToString(encoded)

hexString = hexString[64:]
hexString = hexString[:640]
hexLen := len(hexString)
hexString = hexString[:hexLen-128]
return hexString, encoded, nil
}

func UserOpHash(userOp *model.UserOperation, strategy *model.Strategy, validStart *big.Int, validEnd *big.Int) ([]byte, error) {
_, packUserOpStrByte, err := packUserOp(userOp)
func UserOpHash(userOp *model.UserOperation, strategy *model.Strategy, validStart *big.Int, validEnd *big.Int) ([]byte, string, error) {
packUserOpStr, _, err := packUserOp(userOp)
if err != nil {
return nil, err
return nil, "", err
}

abiEncoder, err := abi.JSON(strings.NewReader(`[
{
"inputs": [
{
"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"
}
],
"name": "Hash",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
]`))
chainId, err := chain_service.GetChainId(strategy.NetWork)
//
bytesTy, err := abi.NewType("bytes", "", nil)
if err != nil {
return nil, err
fmt.Println(err)
}
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,
uint256Ty, err := abi.NewType("uint256", "", nil)
if err != nil {
fmt.Println(err)
}

addressTy, _ := abi.NewType("address", "", nil)
arguments := abi.Arguments{
{
Type: bytesTy,
},
{
Type: uint256Ty,
},
{
Type: addressTy,
},
{
Type: uint256Ty,
},
{
Type: uint256Ty,
},
{
Type: uint256Ty,
},
}
chainId, err := chain_service.GetChainId(strategy.NetWork)
if err != nil {
return nil, "", err
}
packUserOpStrByteNew, _ := hex.DecodeString(packUserOpStr)
chainId.Int64()

data, err := abiEncoder.Pack("Hash", hashStruct)
bytesRes, err := arguments.Pack(packUserOpStrByteNew, chainId, common.HexToAddress(strategy.PayMasterAddress), userOp.Nonce, validStart, validEnd)
if err != nil {
return nil, err
return nil, "", err
}
fmt.Println(hex.EncodeToString(data))
encodeHash := crypto.Keccak256Hash(data)
return encodeHash.Bytes(), nil
encodeHash := crypto.Keccak256Hash(bytesRes)
return encodeHash.Bytes(), hex.EncodeToString(bytesRes), nil

}

Expand Down Expand Up @@ -329,7 +299,7 @@ func SignPaymaster(userOp *model.UserOperation, strategy *model.Strategy, validS
//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))
userOpHash, _, err := UserOpHash(userOp, strategy, big.NewInt(validStartInt), big.NewInt(validEndInt))
if err != nil {
return nil, err
}
Expand Down
52 changes: 50 additions & 2 deletions service/operator/try_pay_user_op_execute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/assert"
"math/big"
"testing"
Expand Down Expand Up @@ -41,17 +43,24 @@ func TestPackUserOp(t *testing.T) {
userOp.PaymasterAndData = nil
userOp.Signature = nil
res, byteres, err := packUserOp(userOp)

shouldEqualStr := "000000000000000000000000f8498599744bc37e141cb800b67dbf103a6b58810000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001e000000000000000000000000000000000000000000000000000000000000054fa000000000000000000000000000000000000000000000000000000000005fa35000000000000000000000000000000000000000000000000000000000000ae640000000000000000000000000000000000000000000000000000002aa887baca0000000000000000000000000000000000000000000000000000000059682f000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000034000000000000000000000000000000000000000000000000000000000000000589406cc6185a346906296840746125a0e449764545fbfb9cf000000000000000000000000340966abb6e37a06014546e0542b3aafad4550810000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e4b61d27f60000000000000000000000001c7d4b196cb0c7b01d743fbc6116a902379c7238000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000044095ea7b30000000000000000000000000000000000325602a77416a16136fdafd04b299fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
assert.NoError(t, err)
if shouldEqualStr != res {
fmt.Println("not equal")
}
fmt.Println(res)
fmt.Println(shouldEqualStr)
fmt.Println(byteres)
}

func TestUserOpHash(t *testing.T) {
strategy := dashboard_service.GetStrategyById("1")
userOp, _ := model.NewUserOp(utils.GenerateMockUserOperation())
userOpHash, err := UserOpHash(userOp, strategy, big.NewInt(1), big.NewInt(2))
userOpHash, userOpHashStr, err := UserOpHash(userOp, strategy, big.NewInt(0xffffffffff), big.NewInt(0xaa))
assert.NoError(t, err)
shouldEqualStr := "00000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000aa36a7000000000000000000000000e99c4db5e360b8c84bf3660393cb2a85c3029b440000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000ffffffffff00000000000000000000000000000000000000000000000000000000000000aa0000000000000000000000000000000000000000000000000000000000000300000000000000000000000000f8498599744bc37e141cb800b67dbf103a6b58810000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001e000000000000000000000000000000000000000000000000000000000000054fa000000000000000000000000000000000000000000000000000000000005fa35000000000000000000000000000000000000000000000000000000000000ae640000000000000000000000000000000000000000000000000000002aa887baca0000000000000000000000000000000000000000000000000000000059682f000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000034000000000000000000000000000000000000000000000000000000000000000589406cc6185a346906296840746125a0e449764545fbfb9cf000000000000000000000000340966abb6e37a06014546e0542b3aafad4550810000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e4b61d27f60000000000000000000000001c7d4b196cb0c7b01d743fbc6116a902379c7238000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000044095ea7b30000000000000000000000000000000000325602a77416a16136fdafd04b299fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
fmt.Println(userOpHashStr)
fmt.Println(shouldEqualStr)
fmt.Println(hex.EncodeToString(userOpHash))
}

Expand Down Expand Up @@ -94,3 +103,42 @@ func TestGenerateTestPaymaterDataparse(t *testing.T) {
//}
//str := "0x
}
func TestDemo(t *testing.T) {
//strategy := dashboard_service.GetStrategyById("1")
userOp, _ := model.NewUserOp(utils.GenerateMockUserOperation())

//str := "0x"
//fmt.Println(len(str))
//fmt.Println(str[:2])
//fmt.Println(str[:2] !=
bytesTy, err := abi.NewType("bytes", "", nil)
//uint256Ty, err := abi.NewType("uint256", "", nil)
if err != nil {
fmt.Println(err)
}
uint256Ty, _ := abi.NewType("uint256", "", nil)
if err != nil {
fmt.Println(err)
}
addressTy, _ := abi.NewType("address", "", nil)
arguments := abi.Arguments{
{
Type: bytesTy,
},
{
Type: uint256Ty,
},
{
Type: addressTy,
},
}
packUserOpStr, _, err := packUserOp(userOp)
//Btypelen := len(packUserOpStrByte)
//byteArray := [Btypelen]byte(packUserOpStrByte)
strByte, _ := hex.DecodeString(packUserOpStr)
bytesRes, err := arguments.Pack(strByte, big.NewInt(1), common.Address{})
if err != nil {
fmt.Println(err)
}
fmt.Println(hex.EncodeToString(bytesRes))
}

0 comments on commit bbb2fd9

Please sign in to comment.