Skip to content

Commit

Permalink
add eip155 test
Browse files Browse the repository at this point in the history
  • Loading branch information
MatusKysel committed Nov 11, 2024
1 parent f85cc7e commit e651e14
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
6 changes: 3 additions & 3 deletions crypto/signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ func VerifySignedMessageByOwner(
if isEOASignature {
// EIP 155 https://github.com/ethereum/EIPs/blob/master/EIPS/eip-155.md
if signature[64] == 27 || signature[64] == 28 {
//{0,1} + 27
// v = {0,1} + 27
signature[64] -= byte(27)
} else if signature[64] != 0 && signature[64] != 1 {
//{0,1} + CHAIN_ID * 2 + 35
chainID, err := client.NetworkID(context.Background())
chainID, err := client.ChainID(context.Background())
if err != nil {
return err
}
// v = {0,1} + CHAIN_ID * 2 + 35
signature[64] -= byte(chainID.Uint64()*2 + 35)
}

Expand Down
30 changes: 30 additions & 0 deletions crypto/signature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,36 @@ func TestVerifySignedReshare(t *testing.T) {
sig), "invalid EOA signature")
})

t.Run("valid EIP155 EOA signature", func(t *testing.T) {
stubClient := &stubs.Client{
CallContractF: func(call ethereum.CallMsg) ([]byte, error) {
return nil, nil
},
}
// based on https://etherscan.io/verifySig/260391
address := common.HexToAddress("0x5acdc73e2e418c51d77f9a209379a4cd0b7ffb71")
sig, err := hex.DecodeString("da16d6616c9291dab5d0c1afe706d0431dbdce26b2472bd5294eda42f8d6861f53f06551129874cb3172a0774c1e9c4bb28b1176ac947f692e6fab679f665a941b")
require.NoError(t, err)

var finalMsg []byte
message := []byte("assetify")
prefix := []byte("\x19Ethereum Signed Message:\n")
len := []byte(strconv.Itoa(len(message)))

finalMsg = append(finalMsg, prefix...)
finalMsg = append(finalMsg, len...)
finalMsg = append(finalMsg, message...)

var hash [32]byte
keccak256 := eth_crypto.Keccak256(finalMsg)
copy(hash[:], keccak256)

require.NoError(t, VerifySignedMessageByOwner(stubClient,
address,
hash,
sig))
})

t.Run("valid contract signature", func(t *testing.T) {
sk, err := eth_crypto.GenerateKey()
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion eip1271/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ var InvalidSigValue = [4]byte{0xff, 0xff, 0xff, 0xff}

type ETHClient interface {
BlockNumber(ctx context.Context) (uint64, error)
NetworkID(ctx context.Context) (*big.Int, error)
ChainID(ctx context.Context) (*big.Int, error)
bind.ContractBackend
}
2 changes: 1 addition & 1 deletion testing/stubs/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (c *Client) BlockNumber(ctx context.Context) (uint64, error) {
return 100, nil
}

func (c *Client) NetworkID(ctx context.Context) (*big.Int, error) {
func (c *Client) ChainID(ctx context.Context) (*big.Int, error) {
big := big.NewInt(1)
return big, nil
}
Expand Down

0 comments on commit e651e14

Please sign in to comment.