Skip to content

Commit

Permalink
add IsEIP191Message & MessageToEIP191
Browse files Browse the repository at this point in the history
  • Loading branch information
marino39 committed Feb 16, 2024
1 parent d591885 commit fb469ce
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
15 changes: 15 additions & 0 deletions signature.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sequence

import (
"bytes"
"context"
"fmt"
"math/big"
Expand Down Expand Up @@ -218,6 +219,20 @@ func IsValidUndeployedSignature(walletAddress common.Address, digest common.Hash
return V2IsValidUndeployedSignature(walletAddress, digest, seqSig, walletContext, chainID, provider)
}

func IsEIP191Message(msg []byte) bool {
return len(msg) > 0 && msg[0] == 0x19
}

func MessageToEIP191(msg []byte) []byte {
if !IsEIP191Message(msg) {
return bytes.Join([][]byte{
[]byte("\x19Ethereum Signed Message:\n"),
[]byte(fmt.Sprintf("%v", len(msg))),
msg}, nil)
}
return msg
}

func MessageDigest(message []byte) common.Hash {
return common.BytesToHash(ethcoder.Keccak256(message))
}
Expand Down
46 changes: 46 additions & 0 deletions signature_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package sequence_test

import (
"testing"

"github.com/0xsequence/go-sequence"
"github.com/stretchr/testify/assert"
)

/*
const message = "hi!"
Expand Down Expand Up @@ -547,3 +554,42 @@ func areSignaturesIsomorphic(a *signature, b *signature) bool {
return true
}
*/

func TestIsEIP191Message(t *testing.T) {
t.Run("EIP191", func(t *testing.T) {
// EIP191 message
msg := []byte("\x19Ethereum Signed Message:\n5hello")
ok := sequence.IsEIP191Message(msg)
if !ok {
t.Error("expected EIP191 message")
}
})

t.Run("non-EIP191", func(t *testing.T) {
// non-EIP191 message
msg := []byte("hello")
ok := sequence.IsEIP191Message(msg)
if ok {
t.Error("expected non-EIP191 message")
}
})
}

func TestMessageToEIP191(t *testing.T) {
t.Run("EIP191_none", func(t *testing.T) {
// EIP191 message
msg := []byte("hello")
expectedEIP191 := []byte("\x19Ethereum Signed Message:\n5hello")

eip191 := sequence.MessageToEIP191(msg)
assert.Equal(t, eip191, expectedEIP191)
})

t.Run("EIP191_already", func(t *testing.T) {
// EIP191 message
msg := []byte("\x19Ethereum Signed Message:\n5hello")
eip191 := sequence.MessageToEIP191(msg)

assert.Equal(t, msg, eip191)
})
}

0 comments on commit fb469ce

Please sign in to comment.