Extract constants to a separate file

This commit is contained in:
Alex Forshtat 2024-08-12 15:53:13 +02:00
parent fa6e8fea5a
commit 80242b7199
3 changed files with 103 additions and 61 deletions

View file

@ -9,49 +9,6 @@ import (
"strings"
)
const AcceptAccountMethodSig = uint64(0x1256ebd1) // acceptAccount(uint256,uint256)
const AcceptPaymasterMethodSig = uint64(0x03be8439) // acceptPaymaster(uint256,uint256,bytes)
const SigFailAccountMethodSig = uint64(0x7715fac2) // sigFailAccount(uint256,uint256)
const PaymasterMaxContextSize = 65536
func abiEncodeValidateTransaction(tx *types.Rip7560AccountAbstractionTx, signingHash common.Hash) ([]byte, error) {
jsondata := `[
{"type":"function","name":"validateTransaction","inputs": [{"name": "version","type": "uint256"},{"name": "txHash","type": "bytes32"},{"name": "transaction","type": "bytes"}]}
]`
jsonAbi, err := abi.JSON(strings.NewReader(jsondata))
if err != nil {
return nil, err
}
txAbiEncoding, err := tx.AbiEncode()
validateTransactionData, err := jsonAbi.Pack("validateTransaction", big.NewInt(0), signingHash, txAbiEncoding)
return validateTransactionData, err
}
func abiEncodeValidatePaymasterTransaction(tx *types.Rip7560AccountAbstractionTx, signingHash common.Hash) ([]byte, error) {
jsondata := `[
{"type":"function","name":"validatePaymasterTransaction","inputs": [{"name": "version","type": "uint256"},{"name": "txHash","type": "bytes32"},{"name": "transaction","type": "bytes"}]}
]`
jsonAbi, err := abi.JSON(strings.NewReader(jsondata))
txAbiEncoding, err := tx.AbiEncode()
data, err := jsonAbi.Pack("validatePaymasterTransaction", big.NewInt(0), signingHash, txAbiEncoding)
return data, err
}
func abiEncodePostPaymasterTransaction(context []byte) ([]byte, error) {
jsondata := `[
{"type":"function","name":"postPaymasterTransaction","inputs": [{"name": "success","type": "bool"},{"name": "actualGasCost","type": "uint256"},{"name": "context","type": "bytes"}]}
]`
jsonAbi, err := abi.JSON(strings.NewReader(jsondata))
if err != nil {
return nil, err
}
postOpData, err := jsonAbi.Pack("postPaymasterTransaction", true, big.NewInt(0), context)
return postOpData, err
}
type AcceptAccountData struct {
ValidAfter *big.Int
ValidUntil *big.Int
@ -63,13 +20,35 @@ type AcceptPaymasterData struct {
Context []byte
}
func abiEncodeValidateTransaction(tx *types.Rip7560AccountAbstractionTx, signingHash common.Hash) ([]byte, error) {
jsonAbi, err := abi.JSON(strings.NewReader(ValidateTransactionAbi))
if err != nil {
return nil, err
}
txAbiEncoding, err := tx.AbiEncode()
validateTransactionData, err := jsonAbi.Pack("validateTransaction", big.NewInt(0), signingHash, txAbiEncoding)
return validateTransactionData, err
}
func abiEncodeValidatePaymasterTransaction(tx *types.Rip7560AccountAbstractionTx, signingHash common.Hash) ([]byte, error) {
jsonAbi, err := abi.JSON(strings.NewReader(ValidatePaymasterTransactionAbi))
txAbiEncoding, err := tx.AbiEncode()
data, err := jsonAbi.Pack("validatePaymasterTransaction", big.NewInt(0), signingHash, txAbiEncoding)
return data, err
}
func abiEncodePostPaymasterTransaction(context []byte) ([]byte, error) {
jsonAbi, err := abi.JSON(strings.NewReader(PostPaymasterTransactionAbi))
if err != nil {
return nil, err
}
postOpData, err := jsonAbi.Pack("postPaymasterTransaction", true, big.NewInt(0), context)
return postOpData, err
}
func abiDecodeAcceptAccount(input []byte) (*AcceptAccountData, error) {
// this is not a true ABI of the "acceptAccount" function
// this ABI swaps inputs and outputs as there is no suitable "abi.decode" function
jsondata := `[
{"type":"function","name":"acceptAccount","outputs": [{"name": "validAfter","type": "uint256"},{"name": "validUntil","type": "uint256"}]}
]`
jsonAbi, err := abi.JSON(strings.NewReader(jsondata))
jsonAbi, err := abi.JSON(strings.NewReader(AcceptAccountAbi))
if err != nil {
return nil, err
}
@ -86,10 +65,7 @@ func abiDecodeAcceptAccount(input []byte) (*AcceptAccountData, error) {
}
func abiDecodeAcceptPaymaster(input []byte) (*AcceptPaymasterData, error) {
jsondata := `[
{"type":"function","name":"acceptPaymaster","outputs": [{"name": "validAfter","type": "uint256"},{"name": "validUntil","type": "uint256"},{"name": "context","type": "bytes"}]}
]`
jsonAbi, err := abi.JSON(strings.NewReader(jsondata))
jsonAbi, err := abi.JSON(strings.NewReader(AcceptPaymasterAbi))
if err != nil {
return nil, err
}

View file

@ -0,0 +1,74 @@
package core
const AcceptAccountMethodSig = uint64(0x1256ebd1) // acceptAccount(uint256,uint256)
const AcceptPaymasterMethodSig = uint64(0x03be8439) // acceptPaymaster(uint256,uint256,bytes)
const SigFailAccountMethodSig = uint64(0x7715fac2) // sigFailAccount(uint256,uint256)
const PaymasterMaxContextSize = 65536
const ValidateTransactionAbi = `
[
{
"type":"function",
"name":"validateTransaction",
"inputs": [
{"name": "version","type": "uint256"},
{"name": "txHash","type": "bytes32"},
{"name": "transaction","type": "bytes"}
]
}
]`
const ValidatePaymasterTransactionAbi = `
[
{
"type":"function",
"name":"validatePaymasterTransaction","
inputs": [
{"name": "version","type": "uint256"},
{"name": "txHash","type": "bytes32"},
{"name": "transaction","type": "bytes"}
]
}
]`
const PostPaymasterTransactionAbi = `
[
{
"type":"function",
"name":"postPaymasterTransaction",
"inputs": [
{"name": "success","type": "bool"},
{"name": "actualGasCost","type": "uint256"},
{"name": "context","type": "bytes"}
]
}
]`
// AcceptAccountAbi Note that this is not a true ABI of the "acceptAccount" function.
// This ABI swaps inputs and outputs to simplify the ABI decoding.
const AcceptAccountAbi = `
[
{
"type":"function",
"name":"acceptAccount",
"outputs": [
{"name": "validAfter","type": "uint256"},
{"name": "validUntil","type": "uint256"}
]
}
]`
// AcceptPaymasterAbi Note that this is not a true ABI of the "acceptPaymaster" function.
// This ABI swaps inputs and outputs to simplify the ABI decoding.
const AcceptPaymasterAbi = `
[
{
"type":"function",
"name":"acceptPaymaster",
"outputs": [
{"name": "validAfter","type": "uint256"},
{"name": "validUntil","type": "uint256"},
{"name": "context","type": "bytes"}
]
}
]`

View file

@ -16,14 +16,6 @@ import (
var AA_ENTRY_POINT = common.HexToAddress("0x0000000000000000000000000000000000007560")
var AA_SENDER_CREATOR = common.HexToAddress("0x00000000000000000000000000000000ffff7560")
func PackValidationData(authorizerMagic uint64, validUntil, validAfter uint64) []byte {
t := new(big.Int).SetUint64(uint64(validAfter))
t = t.Lsh(t, 48).Add(t, new(big.Int).SetUint64(validUntil&0xffffff))
t = t.Lsh(t, 160).Add(t, new(big.Int).SetUint64(uint64(authorizerMagic)))
return common.LeftPadBytes(t.Bytes(), 32)
}
type EntryPointCall struct {
OnEnterSuper tracing.EnterHook
Input []byte