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

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