Browse Source

Merge pull request #16 from tendermint/data-codegen

Use codegen to create wrappers for interfaces
pull/1782/head
Ethan Frey 7 years ago
committed by GitHub
parent
commit
438b16f1f8
10 changed files with 215 additions and 133 deletions
  1. +10
    -1
      Makefile
  2. +6
    -0
      _gen.go
  3. +2
    -2
      encode_test.go
  4. +1
    -1
      glide.lock
  5. +3
    -43
      priv_key.go
  6. +62
    -0
      privkeyinner_wrapper.go
  7. +3
    -43
      pub_key.go
  8. +62
    -0
      pubkeyinner_wrapper.go
  9. +4
    -43
      signature.go
  10. +62
    -0
      signatureinner_wrapper.go

+ 10
- 1
Makefile View File

@ -1,4 +1,4 @@
.PHONEY: all docs test install get_vendor_deps ensure_tools
.PHONEY: all docs test install get_vendor_deps ensure_tools codegen
GOTOOLS = \
github.com/Masterminds/glide
@ -24,4 +24,13 @@ get_vendor_deps: ensure_tools
ensure_tools:
go get $(GOTOOLS)
prepgen: install
go install ./vendor/github.com/btcsuite/btcutil/base58
go install ./vendor/github.com/stretchr/testify/assert
go install ./vendor/github.com/stretchr/testify/require
go install ./vendor/golang.org/x/crypto/bcrypt
codegen:
@echo "--> regenerating all interface wrappers"
@gen
@echo "Done!"

+ 6
- 0
_gen.go View File

@ -0,0 +1,6 @@
package main
import (
_ "github.com/tendermint/go-wire/gen"
_ "github.com/clipperhouse/stringer"
)

+ 2
- 2
encode_test.go View File

@ -7,8 +7,8 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
data "github.com/tendermint/go-wire/data"
wire "github.com/tendermint/go-wire"
data "github.com/tendermint/go-wire/data"
)
type byter interface {
@ -58,7 +58,7 @@ func checkWireJSON(t *testing.T, in interface{}, reader interface{}, typ byte) {
var err error
js := wire.JSONBytes(in)
btyp := fmt.Sprintf("[%d,", typ)
assert.True(t, strings.HasPrefix(string(js), btyp), string(js))
assert.True(t, strings.HasPrefix(string(js), btyp), string(js), btyp)
wire.ReadJSON(reader, js, &err)
require.Nil(t, err, "%+v", err)


+ 1
- 1
glide.lock View File

@ -88,7 +88,7 @@ imports:
- edwards25519
- extra25519
- name: github.com/tendermint/go-wire
version: 59ba5af720a8cbaf5594595e1d8aae23bfd18e31
version: 97beaedf0f4dbc035309157c92be3b30cc6e5d74
subpackages:
- data
- data/base58


+ 3
- 43
priv_key.go View File

@ -6,9 +6,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"
)
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.
// You probably want to use PubKey
// +gen wrapper:"PrivKey,Impl[PrivKeyEd25519,PrivKeySecp256k1],ed25519,secp256k1"
type PrivKeyInner interface {
AssertIsPrivKeyInner()
Bytes() []byte
@ -33,35 +30,6 @@ type PrivKeyInner interface {
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{}
@ -128,10 +96,6 @@ func (privKey PrivKeyEd25519) Generate(index int) PrivKeyEd25519 {
return PrivKeyEd25519(newKey)
}
func (privKey PrivKeyEd25519) Wrap() PrivKey {
return PrivKey{privKey}
}
func GenPrivKeyEd25519() PrivKeyEd25519 {
privKeyBytes := new([64]byte)
copy(privKeyBytes[:32], CRandBytes(32))
@ -201,10 +165,6 @@ func (privKey PrivKeySecp256k1) String() string {
return Fmt("PrivKeySecp256k1{*****}")
}
func (privKey PrivKeySecp256k1) Wrap() PrivKey {
return PrivKey{privKey}
}
/*
// Deterministically generates new priv-key bytes from key.
func (key PrivKeySecp256k1) Generate(index int) PrivKeySecp256k1 {


+ 62
- 0
privkeyinner_wrapper.go View File

@ -0,0 +1,62 @@
// Generated by: main
// TypeWriter: wrapper
// 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}
}

+ 3
- 43
pub_key.go View File

@ -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,9 @@ 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 wrapper:"PubKey,Impl[PubKeyEd25519,PubKeySecp256k1],ed25519,secp256k1"
type PubKeyInner interface {
AssertIsPubKeyInner()
Address() []byte
@ -36,35 +33,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 +110,6 @@ func (pubKey PubKeyEd25519) Equals(other PubKey) bool {
}
}
func (pubKey PubKeyEd25519) Wrap() PubKey {
return PubKey{pubKey}
}
//-------------------------------------
var _ PubKeyInner = PubKeySecp256k1{}
@ -218,7 +182,3 @@ func (pubKey PubKeySecp256k1) Equals(other PubKey) bool {
return false
}
}
func (pubKey PubKeySecp256k1) Wrap() PubKey {
return PubKey{pubKey}
}

+ 62
- 0
pubkeyinner_wrapper.go View File

@ -0,0 +1,62 @@
// Generated by: main
// TypeWriter: wrapper
// 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 "json:\"unwrap\""
}
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{}, "ed25519", 0x1)
}
func (hi PubKeyEd25519) Wrap() PubKey {
return PubKey{hi}
}
func init() {
PubKeyMapper.RegisterImplementation(PubKeySecp256k1{}, "secp256k1", 0x2)
}
func (hi PubKeySecp256k1) Wrap() PubKey {
return PubKey{hi}
}

+ 4
- 43
signature.go View File

@ -4,9 +4,9 @@ import (
"bytes"
"fmt"
. "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"
)
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.
// You probably want to use Signature.
// +gen wrapper:"Signature,Impl[SignatureEd25519,SignatureSecp256k1],ed25519,secp256k1"
type SignatureInner interface {
AssertIsSignatureInner()
Bytes() []byte
@ -31,35 +28,6 @@ type SignatureInner interface {
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{}
@ -96,10 +64,6 @@ func (sig *SignatureEd25519) UnmarshalJSON(enc []byte) error {
return err
}
func (sig SignatureEd25519) Wrap() Signature {
return Signature{sig}
}
//-------------------------------------
var _ SignatureInner = SignatureSecp256k1{}
@ -124,6 +88,7 @@ func (sig SignatureSecp256k1) Equals(other Signature) bool {
return false
}
}
func (sig SignatureSecp256k1) MarshalJSON() ([]byte, error) {
return data.Encoder.Marshal(sig)
}
@ -131,7 +96,3 @@ func (sig SignatureSecp256k1) MarshalJSON() ([]byte, error) {
func (sig *SignatureSecp256k1) UnmarshalJSON(enc []byte) error {
return data.Encoder.Unmarshal((*[]byte)(sig), enc)
}
func (sig SignatureSecp256k1) Wrap() Signature {
return Signature{sig}
}

+ 62
- 0
signatureinner_wrapper.go View File

@ -0,0 +1,62 @@
// Generated by: main
// TypeWriter: wrapper
// 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