One name, type byte per algorithm

This commit is contained in:
Ethan Frey 2017-02-24 00:31:43 +01:00
parent 8c9b889ccf
commit 0e92dd5bb5
5 changed files with 21 additions and 37 deletions

View File

@ -56,13 +56,13 @@ func TestKeyEncodings(t *testing.T) {
}{ }{
{ {
privKey: PrivKeyS{GenPrivKeyEd25519()}, privKey: PrivKeyS{GenPrivKeyEd25519()},
keyType: PrivKeyTypeEd25519, keyType: TypeEd25519,
keyName: PrivKeyNameEd25519, keyName: NameEd25519,
}, },
{ {
privKey: PrivKeyS{GenPrivKeySecp256k1()}, privKey: PrivKeyS{GenPrivKeySecp256k1()},
keyType: PrivKeyTypeSecp256k1, keyType: TypeSecp256k1,
keyName: PrivKeyNameSecp256k1, keyName: NameSecp256k1,
}, },
} }

View File

@ -19,12 +19,12 @@ type PrivKey interface {
Equals(PrivKey) bool Equals(PrivKey) bool
} }
// Types of PrivKey implementations // Types of implementations
const ( const (
PrivKeyTypeEd25519 = byte(0x01) TypeEd25519 = byte(0x01)
PrivKeyTypeSecp256k1 = byte(0x02) TypeSecp256k1 = byte(0x02)
PrivKeyNameEd25519 = "ed25519" NameEd25519 = "ed25519"
PrivKeyNameSecp256k1 = "secp256k1" NameSecp256k1 = "secp256k1"
) )
var privKeyMapper data.Mapper var privKeyMapper data.Mapper
@ -32,8 +32,8 @@ var privKeyMapper data.Mapper
// register both private key types with go-data (and thus go-wire) // register both private key types with go-data (and thus go-wire)
func init() { func init() {
privKeyMapper = data.NewMapper(PrivKeyS{}). privKeyMapper = data.NewMapper(PrivKeyS{}).
RegisterInterface(PrivKeyEd25519{}, PrivKeyNameEd25519, PrivKeyTypeEd25519). RegisterInterface(PrivKeyEd25519{}, NameEd25519, TypeEd25519).
RegisterInterface(PrivKeySecp256k1{}, PrivKeyNameSecp256k1, PrivKeyTypeSecp256k1) RegisterInterface(PrivKeySecp256k1{}, NameSecp256k1, TypeSecp256k1)
} }
// PrivKeyS add json serialization to PrivKey // PrivKeyS add json serialization to PrivKey

View File

@ -21,21 +21,13 @@ type PubKey interface {
Equals(PubKey) bool Equals(PubKey) bool
} }
// Types of PubKey implementations
const (
PubKeyTypeEd25519 = byte(0x01)
PubKeyTypeSecp256k1 = byte(0x02)
PubKeyNameEd25519 = "ed25519"
PubKeyNameSecp256k1 = "secp256k1"
)
var pubKeyMapper data.Mapper var pubKeyMapper data.Mapper
// register both public key types with go-data (and thus go-wire) // register both public key types with go-data (and thus go-wire)
func init() { func init() {
pubKeyMapper = data.NewMapper(PubKeyS{}). pubKeyMapper = data.NewMapper(PubKeyS{}).
RegisterInterface(PubKeyEd25519{}, PubKeyNameEd25519, PubKeyTypeEd25519). RegisterInterface(PubKeyEd25519{}, NameEd25519, TypeEd25519).
RegisterInterface(PubKeySecp256k1{}, PubKeyNameSecp256k1, PubKeyTypeSecp256k1) RegisterInterface(PubKeySecp256k1{}, NameSecp256k1, TypeSecp256k1)
} }
// PubKeyS add json serialization to PubKey // PubKeyS add json serialization to PubKey
@ -76,7 +68,7 @@ func (pubKey PubKeyEd25519) Address() []byte {
PanicCrisis(*err) PanicCrisis(*err)
} }
// append type byte // append type byte
encodedPubkey := append([]byte{PubKeyTypeEd25519}, w.Bytes()...) encodedPubkey := append([]byte{TypeEd25519}, w.Bytes()...)
hasher := ripemd160.New() hasher := ripemd160.New()
hasher.Write(encodedPubkey) // does not error hasher.Write(encodedPubkey) // does not error
return hasher.Sum(nil) return hasher.Sum(nil)
@ -153,7 +145,7 @@ func (pubKey PubKeySecp256k1) Address() []byte {
PanicCrisis(*err) PanicCrisis(*err)
} }
// append type byte // append type byte
encodedPubkey := append([]byte{PubKeyTypeSecp256k1}, w.Bytes()...) encodedPubkey := append([]byte{TypeSecp256k1}, w.Bytes()...)
hasher := ripemd160.New() hasher := ripemd160.New()
hasher.Write(encodedPubkey) // does not error hasher.Write(encodedPubkey) // does not error
return hasher.Sum(nil) return hasher.Sum(nil)

View File

@ -17,21 +17,13 @@ type Signature interface {
Equals(Signature) bool Equals(Signature) bool
} }
// Types of Signature implementations
const (
SignatureTypeEd25519 = byte(0x01)
SignatureTypeSecp256k1 = byte(0x02)
SignatureNameEd25519 = "ed25519"
SignatureNameSecp256k1 = "secp256k1"
)
var sigMapper data.Mapper var sigMapper data.Mapper
// register both public key types with go-data (and thus go-wire) // register both public key types with go-data (and thus go-wire)
func init() { func init() {
sigMapper = data.NewMapper(SignatureS{}). sigMapper = data.NewMapper(SignatureS{}).
RegisterInterface(SignatureEd25519{}, SignatureNameEd25519, SignatureTypeEd25519). RegisterInterface(SignatureEd25519{}, NameEd25519, TypeEd25519).
RegisterInterface(SignatureSecp256k1{}, SignatureNameSecp256k1, SignatureTypeSecp256k1) RegisterInterface(SignatureSecp256k1{}, NameSecp256k1, TypeSecp256k1)
} }
// SignatureS add json serialization to Signature // SignatureS add json serialization to Signature

View File

@ -56,14 +56,14 @@ func TestSignatureEncodings(t *testing.T) {
{ {
privKey: PrivKeyS{GenPrivKeyEd25519()}, privKey: PrivKeyS{GenPrivKeyEd25519()},
sigSize: ed25519.SignatureSize, sigSize: ed25519.SignatureSize,
sigType: SignatureTypeEd25519, sigType: TypeEd25519,
sigName: SignatureNameEd25519, sigName: NameEd25519,
}, },
{ {
privKey: PrivKeyS{GenPrivKeySecp256k1()}, privKey: PrivKeyS{GenPrivKeySecp256k1()},
sigSize: 0, // unknown sigSize: 0, // unknown
sigType: SignatureTypeSecp256k1, sigType: TypeSecp256k1,
sigName: SignatureNameSecp256k1, sigName: NameSecp256k1,
}, },
} }