diff --git a/messagesignature_test.go b/messagesignature_test.go new file mode 100644 index 0000000..c743899 --- /dev/null +++ b/messagesignature_test.go @@ -0,0 +1,59 @@ +package sequence_test + +import ( + "context" + "math/big" + "testing" + + "github.com/0xsequence/ethkit/ethrpc" + "github.com/0xsequence/ethkit/ethwallet" + "github.com/0xsequence/ethkit/go-ethereum/accounts" + "github.com/0xsequence/ethkit/go-ethereum/crypto" + "github.com/0xsequence/go-sequence" + "github.com/stretchr/testify/assert" +) + +const rpcURL = "https://nodes.sequence.app/mainnet" + +func TestIsValidMessageSignatureEOA(t *testing.T) { + message := "hello world!" + + eoa, err := ethwallet.NewWalletFromRandomEntropy() + assert.NoError(t, err) + + signature, err := eoa.SignMessage([]byte(message)) + assert.NoError(t, err) + assert.Len(t, signature, crypto.SignatureLength) + + provider, err := ethrpc.NewProvider(rpcURL) + assert.NoError(t, err) + + isValid, err := sequence.IsValidMessageSignature(eoa.Address(), []byte(message), signature, big.NewInt(1), provider, nil) + assert.NoError(t, err) + assert.True(t, isValid) +} + +func TestIsValidMessageSignatureSequence(t *testing.T) { + message := "hello world!" + + eoa, err := ethwallet.NewWalletFromRandomEntropy() + assert.NoError(t, err) + wallet, err := sequence.NewWalletSingleOwner(eoa) + assert.NoError(t, err) + + provider, err := ethrpc.NewProvider(rpcURL) + assert.NoError(t, err) + + err = wallet.Connect(provider, nil) + assert.NoError(t, err) + + signature, _, err := wallet.SignMessage(context.Background(), accounts.TextHash([]byte(message))) + assert.NoError(t, err) + + signature, err = sequence.EIP6492Signature(signature, wallet.GetWalletConfig()) + assert.NoError(t, err) + + isValid, err := sequence.IsValidMessageSignature(wallet.Address(), []byte(message), signature, big.NewInt(1), provider, nil) + assert.NoError(t, err) + assert.True(t, isValid) +} diff --git a/signature.go b/signature.go index e294491..6ffd6eb 100644 --- a/signature.go +++ b/signature.go @@ -8,6 +8,7 @@ import ( "github.com/0xsequence/ethkit/ethcoder" "github.com/0xsequence/ethkit/ethrpc" "github.com/0xsequence/ethkit/ethwallet" + "github.com/0xsequence/ethkit/go-ethereum/accounts" "github.com/0xsequence/ethkit/go-ethereum/accounts/abi/bind" "github.com/0xsequence/ethkit/go-ethereum/common" "github.com/0xsequence/ethkit/go-ethereum/common/hexutil" @@ -156,6 +157,18 @@ func GeneralIsValidSignature(walletAddress common.Address, digest common.Hash, s return isValid, nil } +func IsValidMessageSignature(address common.Address, message []byte, signature []byte, chainID *big.Int, provider *ethrpc.Provider, log logger.Logger) (bool, error) { + if log == nil { + log = logger.Nop() + } + + if len(signature) == crypto.SignatureLength { + return ethwallet.IsValid191Signature(address, message, signature) + } else { + return IsValidSignature(log, address, common.BytesToHash(accounts.TextHash(message)), signature, SequenceContexts(), chainID, provider) + } +} + func IsValidSignature(log logger.Logger, walletAddress common.Address, digest common.Hash, seqSig []byte, walletContexts WalletContexts, chainID *big.Int, provider *ethrpc.Provider) (bool, error) { eip6492isValid, _ := eip6492.ValidateEIP6492Offchain(provider, walletAddress, digest, seqSig) if eip6492isValid {