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 2480c96 commit ac2e4d5
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 26 deletions.
7 changes: 7 additions & 0 deletions service/chain_service/chain_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
6 changes: 6 additions & 0 deletions service/chain_service/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
38 changes: 21 additions & 17 deletions service/operator/try_pay_user_op_execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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": [
Expand Down Expand Up @@ -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)
}
Expand Down
15 changes: 6 additions & 9 deletions service/operator/try_pay_user_op_execute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit ac2e4d5

Please sign in to comment.