Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QGB Use Ecrecover to validate Ethereum signatures #605

Merged
merged 5 commits into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 9 additions & 26 deletions x/qgb/types/ethereum_signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ package types
import (
"crypto/ecdsa"

"github.com/ethereum/go-ethereum/common"

sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
)

Expand All @@ -18,40 +17,24 @@ func NewEthereumSignature(hash []byte, privateKey *ecdsa.PrivateKey) ([]byte, er
if privateKey == nil {
return nil, sdkerrors.Wrap(ErrEmpty, "private key")
}
protectedHash := crypto.Keccak256Hash(append([]uint8(signaturePrefix), hash...))
protectedHash := crypto.Keccak256Hash([]uint8(signaturePrefix), hash)
return crypto.Sign(protectedHash.Bytes(), privateKey)
}

func EthAddressFromSignature(hash []byte, signature []byte) (common.Address, error) {
if len(signature) < 65 {
return common.Address{}, sdkerrors.Wrap(ErrInvalid, "signature too short")
}
// To verify signature
// - use crypto.SigToPub to get the public key
// - use crypto.PubkeyToAddress to get the address
// - compare this to the address given.

// for backwards compatibility reasons the V value of an Ethereum sig is presented
// as 27 or 28, internally though it should be a 0-3 value due to changed formats.
// It seems that go-ethereum expects this to be done before sigs actually reach it's
// internal validation functions. In order to comply with this requirement we check
// the sig an dif it's in standard format we correct it. If it's in go-ethereum's expected
// format already we make no changes.
//
// We could attempt to break or otherwise exit early on obviously invalid values for this
// byte, but that's a task best left to go-ethereum
if signature[64] == 27 || signature[64] == 28 {
signature[64] -= 27
Comment on lines -43 to -44
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, why is this no longer necessary?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wow so a bunch of smart contracts include this check even though it's unnecessary

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes. And also in our case we were adding this check while it's not needed.

protectedHash := crypto.Keccak256Hash([]uint8(signaturePrefix), hash)
sigPublicKey, err := crypto.Ecrecover(protectedHash.Bytes(), signature)
if err != nil {
return common.Address{}, sdkerrors.Wrap(err, "ec recover failed")
}

protectedHash := crypto.Keccak256Hash(append([]uint8(signaturePrefix), hash...))

pubkey, err := crypto.SigToPub(protectedHash.Bytes(), signature)
pubKey, err := crypto.UnmarshalPubkey(sigPublicKey)
if err != nil {
return common.Address{}, sdkerrors.Wrap(err, "signature to public key")
return common.Address{}, sdkerrors.Wrap(err, "unmarshalling signature public key failed")
}

addr := crypto.PubkeyToAddress(*pubkey)
addr := crypto.PubkeyToAddress(*pubKey)
return addr, nil
}

Expand Down
4 changes: 2 additions & 2 deletions x/qgb/types/ethereum_signer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (

func TestValsetConfirmSig(t *testing.T) {
const (
correctSig = "e108a7776de6b87183b0690484a74daef44aa6daf907e91abaf7bbfa426ae7706b12e0bd44ef7b0634710d99c2d81087a2f39e075158212343a3b2948ecf33d01c"
invalidSig = "fffff7776de6b87183b0690484a74daef44aa6daf907e91abaf7bbfa426ae7706b12e0bd44ef7b0634710d99c2d81087a2f39e075158212343a3b2948ecf33d01c"
correctSig = "e108a7776de6b87183b0690484a74daef44aa6daf907e91abaf7bbfa426ae7706b12e0bd44ef7b0634710d99c2d81087a2f39e075158212343a3b2948ecf33d001"
invalidSig = "fffff7776de6b87183b0690484a74daef44aa6daf907e91abaf7bbfa426ae7706b12e0bd44ef7b0634710d99c2d81087a2f39e075158212343a3b2948ecf33d001"
ethAddress = "0xc783df8a850f42e7F7e57013759C285caa701eB6"
hash = "88165860d955aee7dc3e83d9d1156a5864b708841965585d206dbef6e9e1a499"
)
Expand Down