Skip to content

Commit

Permalink
IsValidMessageSignature
Browse files Browse the repository at this point in the history
  • Loading branch information
attente committed Feb 20, 2024
1 parent 42c67c0 commit 5426f8f
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
59 changes: 59 additions & 0 deletions messagesignature_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
13 changes: 13 additions & 0 deletions signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 5426f8f

Please sign in to comment.