Skip to content

Commit

Permalink
feat(clients): add BaseClient.IsMessageSignatureCorrect
Browse files Browse the repository at this point in the history
  • Loading branch information
danijelTxFusion committed Jul 27, 2024
1 parent 09d9b5d commit e2d1312
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
27 changes: 27 additions & 0 deletions clients/base_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
Expand All @@ -15,6 +16,7 @@ import (
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
"github.com/zksync-sdk/zksync2-go/contracts/contractdeployer"
"github.com/zksync-sdk/zksync2-go/contracts/erc1271"
"github.com/zksync-sdk/zksync2-go/contracts/l2bridge"
"github.com/zksync-sdk/zksync2-go/contracts/l2sharedbridge"
"github.com/zksync-sdk/zksync2-go/contracts/zksync"
Expand Down Expand Up @@ -1042,6 +1044,19 @@ func (c *BaseClient) IsL2BridgeLegacy(ctx context.Context, address common.Addres
return false, nil
}

// IsMessageSignatureCorrect checks whether the account abstraction message signature is correct.
// Signature can be created using EIP1271 or ECDSA.
func (c *BaseClient) IsMessageSignatureCorrect(ctx context.Context, address common.Address, msg, sig []byte) (bool, error) {
code, err := c.CodeAt(ctx, address, nil)
if err != nil {
return false, err
}
if len(code) > 0 {
return c.isEIP1271SignatureCorrect(ctx, address, common.Hash(accounts.TextHash(msg)), sig)
}
return utils.IsMessageSignatureCorrect(address, msg, sig)
}

func (c *BaseClient) cacheMainContract(ctx context.Context) error {
mainContractAddress, err := c.MainContractAddress(ctx)
if err != nil {
Expand All @@ -1057,6 +1072,18 @@ func (c *BaseClient) cacheMainContract(ctx context.Context) error {
return nil
}

func (c *BaseClient) isEIP1271SignatureCorrect(ctx context.Context, address common.Address, hash common.Hash, sig []byte) (bool, error) {
account, err := erc1271.NewIERC1271(address, c)
if err != nil {
return false, err
}
value, err := account.IsValidSignature(&bind.CallOpts{Context: ctx}, hash, sig)
if err != nil {
return false, err
}
return value == utils.Eip1271MagicValue, nil
}

func (c *BaseClient) getBlock(ctx context.Context, method string, args ...interface{}) (*zkTypes.Block, error) {
var raw json.RawMessage
err := c.rpcClient.CallContext(ctx, &raw, method, args...)
Expand Down
2 changes: 2 additions & 0 deletions utils/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ var (
// L1ToL2AliasOffset Used for applying and undoing aliases on contract addresses during bridging from L1 to L2.
L1ToL2AliasOffset = common.HexToAddress("0x1111000000000000000000000000000000001111")
AddressModulo = new(big.Int).Exp(big.NewInt(2), big.NewInt(160), nil)

Eip1271MagicValue = [4]byte(common.FromHex("0x1626ba7e"))
)

var contractDeployerABI *abi.ABI
Expand Down

0 comments on commit e2d1312

Please sign in to comment.