From 5b94758d4c5f0aa7842f56b31e88a8d2bdb736e2 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Wed, 22 Mar 2017 15:59:00 +0100 Subject: [PATCH] Make PubKey struct compatible with go-wire.JSONBytes/ReadJSON --- encode_test.go | 20 ++++++++++++++++++++ priv_key.go | 2 +- pub_key.go | 2 +- signature.go | 2 +- 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/encode_test.go b/encode_test.go index 667132a6f..71b7b3ae6 100644 --- a/encode_test.go +++ b/encode_test.go @@ -1,12 +1,14 @@ package crypto import ( + "fmt" "strings" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" data "github.com/tendermint/go-data" + wire "github.com/tendermint/go-wire" ) type byter interface { @@ -48,6 +50,18 @@ func checkJSON(t *testing.T, in interface{}, reader interface{}, typ string) { assert.True(t, strings.Contains(string(js), parts[1])) } +// make sure go-wire json can still figure this out... +func checkWireJSON(t *testing.T, in interface{}, reader interface{}, typ byte) { + // test to and from binary + var err error + js := wire.JSONBytes(in) + btyp := fmt.Sprintf("[%d,", typ) + assert.True(t, strings.HasPrefix(string(js), btyp), string(js)) + + wire.ReadJSON(reader, js, &err) + require.Nil(t, err, "%+v", err) +} + func TestKeyEncodings(t *testing.T) { cases := []struct { privKey PrivKey @@ -74,6 +88,9 @@ func TestKeyEncodings(t *testing.T) { priv3 := PrivKey{} checkJSON(t, tc.privKey, &priv3, tc.keyName) assert.EqualValues(t, tc.privKey, priv3) + priv4 := PrivKey{} + checkWireJSON(t, tc.privKey, &priv4, tc.keyType) + assert.EqualValues(t, tc.privKey, priv4) // check (de/en)codings of public key pubKey := tc.privKey.PubKey() @@ -83,6 +100,9 @@ func TestKeyEncodings(t *testing.T) { pub3 := PubKey{} checkJSON(t, pubKey, &pub3, tc.keyName) assert.EqualValues(t, pubKey, pub3) + pub4 := PubKey{} + checkWireJSON(t, pubKey, &pub4, tc.keyType) + assert.EqualValues(t, pubKey, pub4) } } diff --git a/priv_key.go b/priv_key.go index f00a762d8..5aa2d05ad 100644 --- a/priv_key.go +++ b/priv_key.go @@ -38,7 +38,7 @@ func init() { // PrivKey handles all encoding and exposes methods type PrivKey struct { - PrivKeyInner + PrivKeyInner `json:"unwrap"` } func WrapPrivKey(pk PrivKeyInner) PrivKey { diff --git a/pub_key.go b/pub_key.go index fa91b12f4..8373eb347 100644 --- a/pub_key.go +++ b/pub_key.go @@ -33,7 +33,7 @@ func init() { // PubKey add json serialization to PubKeyInner type PubKey struct { - PubKeyInner + PubKeyInner `json:"unwrap"` } func WrapPubKey(pk PubKeyInner) PubKey { diff --git a/signature.go b/signature.go index e7b6b1745..ba40166cd 100644 --- a/signature.go +++ b/signature.go @@ -29,7 +29,7 @@ func init() { // Signature add json serialization to Signature type Signature struct { - SignatureInner + SignatureInner `json:"unwrap"` } func WrapSignature(pk SignatureInner) Signature {