Permalink
Browse files

make tendermint ETH compatible

  • Loading branch information...
jdkanani committed Sep 15, 2018
1 parent d419fff commit 53da24519011b432db5e6e92669bd0cbf3f328f1
Showing with 22 additions and 27 deletions.
  1. +1 −1 Makefile
  2. +18 −24 crypto/secp256k1/secp256k1.go
  3. +3 −2 privval/priv_validator.go
View
@@ -21,7 +21,7 @@ check: check_tools get_vendor_deps
### Build Tendermint
build:
CGO_ENABLED=0 go build $(BUILD_FLAGS) -tags $(BUILD_TAGS) -o build/tendermint ./cmd/tendermint/
CGO_ENABLED=1 go build $(BUILD_FLAGS) -tags $(BUILD_TAGS) -o build/tendermint ./cmd/tendermint/
build_race:
CGO_ENABLED=0 go build -race $(BUILD_FLAGS) -tags $(BUILD_TAGS) -o build/tendermint ./cmd/tendermint
@@ -7,10 +7,9 @@ import (
"fmt"
"io"
secp256k1 "github.com/tendermint/btcd/btcec"
ethCrypto "github.com/ethereum/go-ethereum/crypto"
amino "github.com/tendermint/go-amino"
"github.com/tendermint/tendermint/crypto"
"golang.org/x/crypto/ripemd160"
)
//-------------------------------------
@@ -45,20 +44,24 @@ func (privKey PrivKeySecp256k1) Bytes() []byte {
// Sign creates an ECDSA signature on curve Secp256k1, using SHA256 on the msg.
func (privKey PrivKeySecp256k1) Sign(msg []byte) ([]byte, error) {
priv, _ := secp256k1.PrivKeyFromBytes(secp256k1.S256(), privKey[:])
sig, err := priv.Sign(crypto.Sha256(msg))
privateObject, err := ethCrypto.ToECDSA(privKey[:])
if err != nil {
return nil, err
panic(err)
}
return sig.Serialize(), nil
return ethCrypto.Sign(ethCrypto.Keccak256(msg), privateObject)
}
// PubKey performs the point-scalar multiplication from the privKey on the
// generator point to get the pubkey.
func (privKey PrivKeySecp256k1) PubKey() crypto.PubKey {
_, pubkeyObject := secp256k1.PrivKeyFromBytes(secp256k1.S256(), privKey[:])
privateObject, err := ethCrypto.ToECDSA(privKey[:])
if err != nil {
panic(err)
}
var pubkeyBytes PubKeySecp256k1
copy(pubkeyBytes[:], pubkeyObject.SerializeCompressed())
copy(pubkeyBytes[:], ethCrypto.FromECDSAPub(&privateObject.PublicKey))
return pubkeyBytes
}
@@ -107,7 +110,7 @@ var _ crypto.PubKey = PubKeySecp256k1{}
// PubKeySecp256k1Size is comprised of 32 bytes for one field element
// (the x-coordinate), plus one byte for the parity of the y-coordinate.
const PubKeySecp256k1Size = 33
const PubKeySecp256k1Size = 65
// PubKeySecp256k1 implements crypto.PubKey.
// It is the compressed form of the pubkey. The first byte depends is a 0x02 byte
@@ -118,13 +121,8 @@ type PubKeySecp256k1 [PubKeySecp256k1Size]byte
// Address returns a Bitcoin style addresses: RIPEMD160(SHA256(pubkey))
func (pubKey PubKeySecp256k1) Address() crypto.Address {
hasherSHA256 := sha256.New()
hasherSHA256.Write(pubKey[:]) // does not error
sha := hasherSHA256.Sum(nil)
hasherRIPEMD160 := ripemd160.New()
hasherRIPEMD160.Write(sha) // does not error
return crypto.Address(hasherRIPEMD160.Sum(nil))
// ETH compliant
return crypto.Address(ethCrypto.Keccak256(pubKey[1:])[12:])
}
// Bytes returns the pubkey marshalled with amino encoding.
@@ -137,17 +135,13 @@ func (pubKey PubKeySecp256k1) Bytes() []byte {
}
func (pubKey PubKeySecp256k1) VerifyBytes(msg []byte, sig []byte) bool {
pub, err := secp256k1.ParsePubKey(pubKey[:], secp256k1.S256())
hash := ethCrypto.Keccak256(msg)
p, err := ethCrypto.Ecrecover(hash, sig)
if err != nil {
return false
}
parsedSig, err := secp256k1.ParseSignature(sig[:], secp256k1.S256())
if err != nil {
return false
}
// Underlying library ensures that this signature is in canonical form, to
// prevent Secp256k1 malleability from altering the sign of the s term.
return parsedSig.Verify(crypto.Sha256(msg), pub)
return bytes.Equal(pubKey[:], p[:])
}
func (pubKey PubKeySecp256k1) String() string {
@@ -9,7 +9,8 @@ import (
"time"
"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/ed25519"
// "github.com/tendermint/tendermint/crypto/ed25519"
"github.com/tendermint/tendermint/crypto/secp256k1"
cmn "github.com/tendermint/tendermint/libs/common"
"github.com/tendermint/tendermint/types"
tmtime "github.com/tendermint/tendermint/types/time"
@@ -69,7 +70,7 @@ func (pv *FilePV) GetPubKey() crypto.PubKey {
// GenFilePV generates a new validator with randomly generated private key
// and sets the filePath, but does not call Save().
func GenFilePV(filePath string) *FilePV {
privKey := ed25519.GenPrivKey()
privKey := secp256k1.GenPrivKey()
return &FilePV{
Address: privKey.PubKey().Address(),
PubKey: privKey.PubKey(),

0 comments on commit 53da245

Please sign in to comment.