From f16f71199218d8c0e7e9ef0284c361a51b258afd Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Mon, 15 May 2017 02:31:05 +0200 Subject: [PATCH] First code from codegen... wrong names --- pub_key.go | 47 +++----------------------------- pubkeyinner_holder.go | 62 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 43 deletions(-) create mode 100644 pubkeyinner_holder.go diff --git a/pub_key.go b/pub_key.go index 99839f288..a4da59d2f 100644 --- a/pub_key.go +++ b/pub_key.go @@ -7,9 +7,9 @@ import ( secp256k1 "github.com/btcsuite/btcd/btcec" "github.com/tendermint/ed25519" "github.com/tendermint/ed25519/extra25519" - . "github.com/tendermint/tmlibs/common" - data "github.com/tendermint/go-wire/data" "github.com/tendermint/go-wire" + data "github.com/tendermint/go-wire/data" + . "github.com/tendermint/tmlibs/common" "golang.org/x/crypto/ripemd160" ) @@ -20,12 +20,10 @@ func PubKeyFromBytes(pubKeyBytes []byte) (pubKey PubKey, err error) { //---------------------------------------- -type PubKey struct { - PubKeyInner `json:"unwrap"` -} - // DO NOT USE THIS INTERFACE. // You probably want to use PubKey + +// +gen holder:"PubKey,Impl[PubKeyEd25519,PubKeySecp256k1]" type PubKeyInner interface { AssertIsPubKeyInner() Address() []byte @@ -36,35 +34,6 @@ type PubKeyInner interface { Wrap() PubKey } -func (pk PubKey) MarshalJSON() ([]byte, error) { - return pubKeyMapper.ToJSON(pk.PubKeyInner) -} - -func (pk *PubKey) UnmarshalJSON(data []byte) (err error) { - parsed, err := pubKeyMapper.FromJSON(data) - if err == nil && parsed != nil { - pk.PubKeyInner = parsed.(PubKeyInner) - } - return -} - -// Unwrap recovers the concrete interface safely (regardless of levels of embeds) -func (pk PubKey) Unwrap() PubKeyInner { - pkI := pk.PubKeyInner - for wrap, ok := pkI.(PubKey); ok; wrap, ok = pkI.(PubKey) { - pkI = wrap.PubKeyInner - } - return pkI -} - -func (p PubKey) Empty() bool { - return p.PubKeyInner == nil -} - -var pubKeyMapper = data.NewMapper(PubKey{}). - RegisterImplementation(PubKeyEd25519{}, NameEd25519, TypeEd25519). - RegisterImplementation(PubKeySecp256k1{}, NameSecp256k1, TypeSecp256k1) - //------------------------------------- var _ PubKeyInner = PubKeyEd25519{} @@ -142,10 +111,6 @@ func (pubKey PubKeyEd25519) Equals(other PubKey) bool { } } -func (pubKey PubKeyEd25519) Wrap() PubKey { - return PubKey{pubKey} -} - //------------------------------------- var _ PubKeyInner = PubKeySecp256k1{} @@ -218,7 +183,3 @@ func (pubKey PubKeySecp256k1) Equals(other PubKey) bool { return false } } - -func (pubKey PubKeySecp256k1) Wrap() PubKey { - return PubKey{pubKey} -} diff --git a/pubkeyinner_holder.go b/pubkeyinner_holder.go new file mode 100644 index 000000000..16bfcf8a8 --- /dev/null +++ b/pubkeyinner_holder.go @@ -0,0 +1,62 @@ +// Generated by: main +// TypeWriter: holder +// Directive: +gen on PubKeyInner + +package crypto + +import ( + "github.com/tendermint/go-wire/data" +) + +// Auto-generated adapters for happily unmarshaling interfaces +// Apache License 2.0 +// Copyright (c) 2017 Ethan Frey (ethan.frey@tendermint.com) + +type PubKey struct { + PubKeyInner +} + +var PubKeyMapper = data.NewMapper(PubKey{}) + +func (h PubKey) MarshalJSON() ([]byte, error) { + return PubKeyMapper.ToJSON(h.PubKeyInner) +} + +func (h *PubKey) UnmarshalJSON(data []byte) (err error) { + parsed, err := PubKeyMapper.FromJSON(data) + if err == nil && parsed != nil { + h.PubKeyInner = parsed.(PubKeyInner) + } + return err +} + +// Unwrap recovers the concrete interface safely (regardless of levels of embeds) +func (h PubKey) Unwrap() PubKeyInner { + hi := h.PubKeyInner + for wrap, ok := hi.(PubKey); ok; wrap, ok = hi.(PubKey) { + hi = wrap.PubKeyInner + } + return hi +} + +func (h PubKey) Empty() bool { + return h.PubKeyInner == nil +} + +/*** below are bindings for each implementation ***/ + +func init() { + PubKeyMapper.RegisterImplementation(PubKeyEd25519{}, "pubkeyed25519", 0x1) +} + +func (hi PubKeyEd25519) Wrap() PubKey { + return PubKey{hi} +} + +func init() { + PubKeyMapper.RegisterImplementation(PubKeySecp256k1{}, "pubkeysecp256k1", 0x2) +} + +func (hi PubKeySecp256k1) Wrap() PubKey { + return PubKey{hi} +}