aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--core/crypto/ecdsa/ecdsa.go72
-rw-r--r--core/crypto/ecdsa/ecdsa_test.go9
-rw-r--r--core/test/tcp-transport.go5
3 files changed, 35 insertions, 51 deletions
diff --git a/core/crypto/ecdsa/ecdsa.go b/core/crypto/ecdsa/ecdsa.go
index 9556981..49d5b28 100644
--- a/core/crypto/ecdsa/ecdsa.go
+++ b/core/crypto/ecdsa/ecdsa.go
@@ -20,7 +20,7 @@ package ecdsa
import (
"crypto/ecdsa"
- ethcrypto "github.com/dexon-foundation/dexon/crypto"
+ dexCrypto "github.com/dexon-foundation/dexon/crypto"
"github.com/dexon-foundation/dexon-consensus-core/common"
"github.com/dexon-foundation/dexon-consensus-core/core/crypto"
@@ -35,61 +35,49 @@ func init() {
// PrivateKey represents a private key structure used in geth and implments
// Crypto.PrivateKey interface.
type PrivateKey struct {
- privateKey ecdsa.PrivateKey
- publicKey publicKey
+ privateKey *ecdsa.PrivateKey
}
-// publicKey represents a public key structure used in geth and implements
+// PublicKey represents a public key structure used in geth and implements
// Crypto.PublicKey interface.
-type publicKey struct {
- publicKey []byte
+type PublicKey struct {
+ publicKey *ecdsa.PublicKey
}
// NewPrivateKey creates a new PrivateKey structure.
func NewPrivateKey() (*PrivateKey, error) {
- key, err := ethcrypto.GenerateKey()
+ key, err := dexCrypto.GenerateKey()
if err != nil {
return nil, err
}
- return &PrivateKey{
- privateKey: *key,
- publicKey: *newPublicKey(key),
- }, nil
+ return &PrivateKey{privateKey: key}, nil
}
// NewPrivateKeyFromECDSA creates a new PrivateKey structure from
// ecdsa.PrivateKey.
func NewPrivateKeyFromECDSA(key *ecdsa.PrivateKey) *PrivateKey {
- return &PrivateKey{
- privateKey: *key,
- publicKey: *newPublicKey(key),
- }
+ return &PrivateKey{privateKey: key}
}
-// newPublicKey creates a new PublicKey structure.
-func newPublicKey(prvKey *ecdsa.PrivateKey) *publicKey {
- return &publicKey{
- publicKey: ethcrypto.CompressPubkey(&prvKey.PublicKey),
- }
+// NewPublicKeyFromECDSA creates a new PublicKey structure from
+// ecdsa.PublicKey.
+func NewPublicKeyFromECDSA(key *ecdsa.PublicKey) *PublicKey {
+ return &PublicKey{publicKey: key}
}
// NewPublicKeyFromByteSlice constructs an eth.publicKey instance from
// a byte slice.
-func NewPublicKeyFromByteSlice(b []byte) crypto.PublicKey {
- return publicKey{publicKey: b}
-}
-
-// decompressPubkey parses a public key in the 33-byte compressed format.
-func decompressPubkey(pubkey []byte) (publicKey, error) {
- _, err := ethcrypto.DecompressPubkey(pubkey)
- return publicKey{
- publicKey: pubkey,
- }, err
+func NewPublicKeyFromByteSlice(b []byte) (crypto.PublicKey, error) {
+ pub, err := dexCrypto.UnmarshalPubkey(b)
+ if err != nil {
+ return &PublicKey{}, err
+ }
+ return &PublicKey{publicKey: pub}, nil
}
// PublicKey returns the public key associate this private key.
func (prv *PrivateKey) PublicKey() crypto.PublicKey {
- return prv.publicKey
+ return NewPublicKeyFromECDSA(&(prv.privateKey.PublicKey))
}
// Sign calculates an ECDSA signature.
@@ -102,7 +90,7 @@ func (prv *PrivateKey) PublicKey() crypto.PublicKey {
// The produced signature is in the [R || S || V] format where V is 0 or 1.
func (prv *PrivateKey) Sign(hash common.Hash) (
sig crypto.Signature, err error) {
- s, err := ethcrypto.Sign(hash[:], &prv.privateKey)
+ s, err := dexCrypto.Sign(hash[:], prv.privateKey)
sig = crypto.Signature{
Type: cryptoType,
Signature: s,
@@ -114,32 +102,32 @@ func (prv *PrivateKey) Sign(hash common.Hash) (
// The public key should be in compressed (33 bytes) or uncompressed (65 bytes)
// format.
// The signature should have the 64 byte [R || S] format.
-func (pub publicKey) VerifySignature(
+func (pub *PublicKey) VerifySignature(
hash common.Hash, signature crypto.Signature) bool {
sig := signature.Signature
if len(sig) == 65 {
// The last byte is for ecrecover.
sig = sig[:64]
}
- return ethcrypto.VerifySignature(pub.publicKey, hash[:], sig)
+ return dexCrypto.VerifySignature(pub.Bytes(), hash[:], sig)
}
// Compress encodes a public key to the 33-byte compressed format.
-func (pub publicKey) Compress() []byte {
- return pub.publicKey
+func (pub *PublicKey) Compress() []byte {
+ return dexCrypto.CompressPubkey(pub.publicKey)
}
-// Bytes returns the []byte representation of public key.
-func (pub publicKey) Bytes() []byte {
- return pub.Compress()
+// Bytes returns the []byte representation of uncompressed public key. (65 bytes)
+func (pub *PublicKey) Bytes() []byte {
+ return dexCrypto.FromECDSAPub(pub.publicKey)
}
// SigToPub returns the PublicKey that created the given signature.
func SigToPub(
hash common.Hash, signature crypto.Signature) (crypto.PublicKey, error) {
- key, err := ethcrypto.SigToPub(hash[:], signature.Signature[:])
+ key, err := dexCrypto.SigToPub(hash[:], signature.Signature[:])
if err != nil {
- return publicKey{}, err
+ return &PublicKey{}, err
}
- return publicKey{publicKey: ethcrypto.CompressPubkey(key)}, nil
+ return &PublicKey{publicKey: key}, nil
}
diff --git a/core/crypto/ecdsa/ecdsa_test.go b/core/crypto/ecdsa/ecdsa_test.go
index 3543b04..dc4645d 100644
--- a/core/crypto/ecdsa/ecdsa_test.go
+++ b/core/crypto/ecdsa/ecdsa_test.go
@@ -55,7 +55,7 @@ func (s *ETHCryptoTestSuite) TestSignature() {
s.NotEqual(sig11, sig12)
// Test VerifySignature with correct public key.
- pub1, ok := prv1.PublicKey().(publicKey)
+ pub1, ok := prv1.PublicKey().(*PublicKey)
s.Require().True(ok)
s.True(pub1.VerifySignature(hash1, sig11))
@@ -66,13 +66,6 @@ func (s *ETHCryptoTestSuite) TestSignature() {
// Test VerifySignature with wrong public key.
pub2 := prv2.PublicKey()
s.False(pub2.VerifySignature(hash1, sig11))
-
- // Test compress and decompress of public key.
- compressPub1 := pub1.Compress()
- decompressPub1, err := decompressPubkey(compressPub1)
- s.Require().Nil(err)
- s.Equal(pub1, decompressPub1)
- s.True(decompressPub1.VerifySignature(hash1, sig11))
}
func (s *ETHCryptoTestSuite) TestSigToPub() {
diff --git a/core/test/tcp-transport.go b/core/test/tcp-transport.go
index 64f915d..f6a5e9e 100644
--- a/core/test/tcp-transport.go
+++ b/core/test/tcp-transport.go
@@ -68,7 +68,10 @@ func parsePeerInfo(info string) (key crypto.PublicKey, conn string) {
if err != nil {
panic(err)
}
- key = ecdsa.NewPublicKeyFromByteSlice(data)
+ key, err = ecdsa.NewPublicKeyFromByteSlice(data)
+ if err != nil {
+ panic(err)
+ }
return
}