Browse Source

Codegen wrappers for privkey and signature as well

pull/1782/head
Ethan Frey 8 years ago
parent
commit
746a2e286d
4 changed files with 131 additions and 86 deletions
  1. +3
    -43
      priv_key.go
  2. +62
    -0
      privkeyinner_holder.go
  3. +4
    -43
      signature.go
  4. +62
    -0
      signatureinner_holder.go

+ 3
- 43
priv_key.go View File

@ -6,9 +6,9 @@ import (
secp256k1 "github.com/btcsuite/btcd/btcec" secp256k1 "github.com/btcsuite/btcd/btcec"
"github.com/tendermint/ed25519" "github.com/tendermint/ed25519"
"github.com/tendermint/ed25519/extra25519" "github.com/tendermint/ed25519/extra25519"
. "github.com/tendermint/tmlibs/common"
data "github.com/tendermint/go-wire/data"
"github.com/tendermint/go-wire" "github.com/tendermint/go-wire"
data "github.com/tendermint/go-wire/data"
. "github.com/tendermint/tmlibs/common"
) )
func PrivKeyFromBytes(privKeyBytes []byte) (privKey PrivKey, err error) { func PrivKeyFromBytes(privKeyBytes []byte) (privKey PrivKey, err error) {
@ -18,12 +18,9 @@ func PrivKeyFromBytes(privKeyBytes []byte) (privKey PrivKey, err error) {
//---------------------------------------- //----------------------------------------
type PrivKey struct {
PrivKeyInner `json:"unwrap"`
}
// DO NOT USE THIS INTERFACE. // DO NOT USE THIS INTERFACE.
// You probably want to use PubKey // You probably want to use PubKey
// +gen holder:"PrivKey,Impl[PrivKeyEd25519,PrivKeySecp256k1],ed25519,secp256k1"
type PrivKeyInner interface { type PrivKeyInner interface {
AssertIsPrivKeyInner() AssertIsPrivKeyInner()
Bytes() []byte Bytes() []byte
@ -33,35 +30,6 @@ type PrivKeyInner interface {
Wrap() PrivKey Wrap() PrivKey
} }
func (p PrivKey) MarshalJSON() ([]byte, error) {
return privKeyMapper.ToJSON(p.PrivKeyInner)
}
func (p *PrivKey) UnmarshalJSON(data []byte) (err error) {
parsed, err := privKeyMapper.FromJSON(data)
if err == nil && parsed != nil {
p.PrivKeyInner = parsed.(PrivKeyInner)
}
return
}
// Unwrap recovers the concrete interface safely (regardless of levels of embeds)
func (p PrivKey) Unwrap() PrivKeyInner {
pk := p.PrivKeyInner
for wrap, ok := pk.(PrivKey); ok; wrap, ok = pk.(PrivKey) {
pk = wrap.PrivKeyInner
}
return pk
}
func (p PrivKey) Empty() bool {
return p.PrivKeyInner == nil
}
var privKeyMapper = data.NewMapper(PrivKey{}).
RegisterImplementation(PrivKeyEd25519{}, NameEd25519, TypeEd25519).
RegisterImplementation(PrivKeySecp256k1{}, NameSecp256k1, TypeSecp256k1)
//------------------------------------- //-------------------------------------
var _ PrivKeyInner = PrivKeyEd25519{} var _ PrivKeyInner = PrivKeyEd25519{}
@ -128,10 +96,6 @@ func (privKey PrivKeyEd25519) Generate(index int) PrivKeyEd25519 {
return PrivKeyEd25519(newKey) return PrivKeyEd25519(newKey)
} }
func (privKey PrivKeyEd25519) Wrap() PrivKey {
return PrivKey{privKey}
}
func GenPrivKeyEd25519() PrivKeyEd25519 { func GenPrivKeyEd25519() PrivKeyEd25519 {
privKeyBytes := new([64]byte) privKeyBytes := new([64]byte)
copy(privKeyBytes[:32], CRandBytes(32)) copy(privKeyBytes[:32], CRandBytes(32))
@ -201,10 +165,6 @@ func (privKey PrivKeySecp256k1) String() string {
return Fmt("PrivKeySecp256k1{*****}") return Fmt("PrivKeySecp256k1{*****}")
} }
func (privKey PrivKeySecp256k1) Wrap() PrivKey {
return PrivKey{privKey}
}
/* /*
// Deterministically generates new priv-key bytes from key. // Deterministically generates new priv-key bytes from key.
func (key PrivKeySecp256k1) Generate(index int) PrivKeySecp256k1 { func (key PrivKeySecp256k1) Generate(index int) PrivKeySecp256k1 {


+ 62
- 0
privkeyinner_holder.go View File

@ -0,0 +1,62 @@
// Generated by: main
// TypeWriter: holder
// Directive: +gen on PrivKeyInner
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 PrivKey struct {
PrivKeyInner "json:\"unwrap\""
}
var PrivKeyMapper = data.NewMapper(PrivKey{})
func (h PrivKey) MarshalJSON() ([]byte, error) {
return PrivKeyMapper.ToJSON(h.PrivKeyInner)
}
func (h *PrivKey) UnmarshalJSON(data []byte) (err error) {
parsed, err := PrivKeyMapper.FromJSON(data)
if err == nil && parsed != nil {
h.PrivKeyInner = parsed.(PrivKeyInner)
}
return err
}
// Unwrap recovers the concrete interface safely (regardless of levels of embeds)
func (h PrivKey) Unwrap() PrivKeyInner {
hi := h.PrivKeyInner
for wrap, ok := hi.(PrivKey); ok; wrap, ok = hi.(PrivKey) {
hi = wrap.PrivKeyInner
}
return hi
}
func (h PrivKey) Empty() bool {
return h.PrivKeyInner == nil
}
/*** below are bindings for each implementation ***/
func init() {
PrivKeyMapper.RegisterImplementation(PrivKeyEd25519{}, "ed25519", 0x1)
}
func (hi PrivKeyEd25519) Wrap() PrivKey {
return PrivKey{hi}
}
func init() {
PrivKeyMapper.RegisterImplementation(PrivKeySecp256k1{}, "secp256k1", 0x2)
}
func (hi PrivKeySecp256k1) Wrap() PrivKey {
return PrivKey{hi}
}

+ 4
- 43
signature.go View File

@ -4,9 +4,9 @@ import (
"bytes" "bytes"
"fmt" "fmt"
. "github.com/tendermint/tmlibs/common"
data "github.com/tendermint/go-wire/data"
"github.com/tendermint/go-wire" "github.com/tendermint/go-wire"
data "github.com/tendermint/go-wire/data"
. "github.com/tendermint/tmlibs/common"
) )
func SignatureFromBytes(sigBytes []byte) (sig Signature, err error) { func SignatureFromBytes(sigBytes []byte) (sig Signature, err error) {
@ -16,12 +16,9 @@ func SignatureFromBytes(sigBytes []byte) (sig Signature, err error) {
//---------------------------------------- //----------------------------------------
type Signature struct {
SignatureInner `json:"unwrap"`
}
// DO NOT USE THIS INTERFACE. // DO NOT USE THIS INTERFACE.
// You probably want to use Signature. // You probably want to use Signature.
// +gen holder:"Signature,Impl[SignatureEd25519,SignatureSecp256k1],ed25519,secp256k1"
type SignatureInner interface { type SignatureInner interface {
AssertIsSignatureInner() AssertIsSignatureInner()
Bytes() []byte Bytes() []byte
@ -31,35 +28,6 @@ type SignatureInner interface {
Wrap() Signature Wrap() Signature
} }
func (sig Signature) MarshalJSON() ([]byte, error) {
return sigMapper.ToJSON(sig.SignatureInner)
}
func (sig *Signature) UnmarshalJSON(data []byte) (err error) {
parsed, err := sigMapper.FromJSON(data)
if err == nil && parsed != nil {
sig.SignatureInner = parsed.(SignatureInner)
}
return
}
// Unwrap recovers the concrete interface safely (regardless of levels of embeds)
func (sig Signature) Unwrap() SignatureInner {
pk := sig.SignatureInner
for wrap, ok := pk.(Signature); ok; wrap, ok = pk.(Signature) {
pk = wrap.SignatureInner
}
return pk
}
func (sig Signature) Empty() bool {
return sig.SignatureInner == nil
}
var sigMapper = data.NewMapper(Signature{}).
RegisterImplementation(SignatureEd25519{}, NameEd25519, TypeEd25519).
RegisterImplementation(SignatureSecp256k1{}, NameSecp256k1, TypeSecp256k1)
//------------------------------------- //-------------------------------------
var _ SignatureInner = SignatureEd25519{} var _ SignatureInner = SignatureEd25519{}
@ -96,10 +64,6 @@ func (sig *SignatureEd25519) UnmarshalJSON(enc []byte) error {
return err return err
} }
func (sig SignatureEd25519) Wrap() Signature {
return Signature{sig}
}
//------------------------------------- //-------------------------------------
var _ SignatureInner = SignatureSecp256k1{} var _ SignatureInner = SignatureSecp256k1{}
@ -124,6 +88,7 @@ func (sig SignatureSecp256k1) Equals(other Signature) bool {
return false return false
} }
} }
func (sig SignatureSecp256k1) MarshalJSON() ([]byte, error) { func (sig SignatureSecp256k1) MarshalJSON() ([]byte, error) {
return data.Encoder.Marshal(sig) return data.Encoder.Marshal(sig)
} }
@ -131,7 +96,3 @@ func (sig SignatureSecp256k1) MarshalJSON() ([]byte, error) {
func (sig *SignatureSecp256k1) UnmarshalJSON(enc []byte) error { func (sig *SignatureSecp256k1) UnmarshalJSON(enc []byte) error {
return data.Encoder.Unmarshal((*[]byte)(sig), enc) return data.Encoder.Unmarshal((*[]byte)(sig), enc)
} }
func (sig SignatureSecp256k1) Wrap() Signature {
return Signature{sig}
}

+ 62
- 0
signatureinner_holder.go View File

@ -0,0 +1,62 @@
// Generated by: main
// TypeWriter: holder
// Directive: +gen on SignatureInner
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 Signature struct {
SignatureInner "json:\"unwrap\""
}
var SignatureMapper = data.NewMapper(Signature{})
func (h Signature) MarshalJSON() ([]byte, error) {
return SignatureMapper.ToJSON(h.SignatureInner)
}
func (h *Signature) UnmarshalJSON(data []byte) (err error) {
parsed, err := SignatureMapper.FromJSON(data)
if err == nil && parsed != nil {
h.SignatureInner = parsed.(SignatureInner)
}
return err
}
// Unwrap recovers the concrete interface safely (regardless of levels of embeds)
func (h Signature) Unwrap() SignatureInner {
hi := h.SignatureInner
for wrap, ok := hi.(Signature); ok; wrap, ok = hi.(Signature) {
hi = wrap.SignatureInner
}
return hi
}
func (h Signature) Empty() bool {
return h.SignatureInner == nil
}
/*** below are bindings for each implementation ***/
func init() {
SignatureMapper.RegisterImplementation(SignatureEd25519{}, "ed25519", 0x1)
}
func (hi SignatureEd25519) Wrap() Signature {
return Signature{hi}
}
func init() {
SignatureMapper.RegisterImplementation(SignatureSecp256k1{}, "secp256k1", 0x2)
}
func (hi SignatureSecp256k1) Wrap() Signature {
return Signature{hi}
}

Loading…
Cancel
Save