Skip to content

Commit

Permalink
Merge pull request #176 from InjectiveLabs/feat/sync_dev_with_master_…
Browse files Browse the repository at this point in the history
…1_48_10

Feat/sync dev with master 1.48.10
  • Loading branch information
aarmoa authored Nov 7, 2023
2 parents b40a48c + 21d896b commit 8c79823
Show file tree
Hide file tree
Showing 68 changed files with 2,645 additions and 2,000 deletions.
23 changes: 23 additions & 0 deletions chain/peggy/types/ethereum_signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package types

import (
"crypto/ecdsa"
"fmt"
"math/big"

"cosmossdk.io/errors"
"github.com/ethereum/go-ethereum/common"
Expand All @@ -21,12 +23,33 @@ func NewEthereumSignature(hash common.Hash, privateKey *ecdsa.PrivateKey) ([]byt
return crypto.Sign(protectedHash.Bytes(), privateKey)
}

// decodeSignature was duplicated from go-ethereum with slight modifications
func decodeSignature(sig []byte) (r, s *big.Int, v byte) {
if len(sig) != crypto.SignatureLength {
panic(fmt.Sprintf("wrong size for signature: got %d, want %d", len(sig), crypto.SignatureLength))
}
r = new(big.Int).SetBytes(sig[:32])
s = new(big.Int).SetBytes(sig[32:64])
if sig[64] == 27 || sig[64] == 28 {
v = sig[64] - 27
} else {
v = sig[64]
}
return r, s, v
}

// ValidateEthereumSignature takes a message, an associated signature and public key and
// returns an error if the signature isn't valid
func EthAddressFromSignature(hash common.Hash, signature []byte) (common.Address, error) {
if len(signature) < 65 {
return common.Address{}, errors.Wrap(ErrInvalid, "signature too short")
}

r, s, v := decodeSignature(signature)
if !crypto.ValidateSignatureValues(v, r, s, true) {
return common.Address{}, errors.Wrap(ErrInvalid, "Signature values failed validation")
}

// To verify signature
// - use crypto.SigToPub to get the public key
// - use crypto.PubkeyToAddress to get the address
Expand Down
Loading

0 comments on commit 8c79823

Please sign in to comment.