mirror of
https://github.com/fluencelabs/tendermint
synced 2025-06-20 08:26:31 +00:00
Make PubKey struct compatible with go-wire.JSONBytes/ReadJSON
This commit is contained in:
@ -1,12 +1,14 @@
|
|||||||
package crypto
|
package crypto
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
data "github.com/tendermint/go-data"
|
data "github.com/tendermint/go-data"
|
||||||
|
wire "github.com/tendermint/go-wire"
|
||||||
)
|
)
|
||||||
|
|
||||||
type byter interface {
|
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]))
|
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) {
|
func TestKeyEncodings(t *testing.T) {
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
privKey PrivKey
|
privKey PrivKey
|
||||||
@ -74,6 +88,9 @@ func TestKeyEncodings(t *testing.T) {
|
|||||||
priv3 := PrivKey{}
|
priv3 := PrivKey{}
|
||||||
checkJSON(t, tc.privKey, &priv3, tc.keyName)
|
checkJSON(t, tc.privKey, &priv3, tc.keyName)
|
||||||
assert.EqualValues(t, tc.privKey, priv3)
|
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
|
// check (de/en)codings of public key
|
||||||
pubKey := tc.privKey.PubKey()
|
pubKey := tc.privKey.PubKey()
|
||||||
@ -83,6 +100,9 @@ func TestKeyEncodings(t *testing.T) {
|
|||||||
pub3 := PubKey{}
|
pub3 := PubKey{}
|
||||||
checkJSON(t, pubKey, &pub3, tc.keyName)
|
checkJSON(t, pubKey, &pub3, tc.keyName)
|
||||||
assert.EqualValues(t, pubKey, pub3)
|
assert.EqualValues(t, pubKey, pub3)
|
||||||
|
pub4 := PubKey{}
|
||||||
|
checkWireJSON(t, pubKey, &pub4, tc.keyType)
|
||||||
|
assert.EqualValues(t, pubKey, pub4)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -38,7 +38,7 @@ func init() {
|
|||||||
|
|
||||||
// PrivKey handles all encoding and exposes methods
|
// PrivKey handles all encoding and exposes methods
|
||||||
type PrivKey struct {
|
type PrivKey struct {
|
||||||
PrivKeyInner
|
PrivKeyInner `json:"unwrap"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func WrapPrivKey(pk PrivKeyInner) PrivKey {
|
func WrapPrivKey(pk PrivKeyInner) PrivKey {
|
||||||
|
@ -33,7 +33,7 @@ func init() {
|
|||||||
|
|
||||||
// PubKey add json serialization to PubKeyInner
|
// PubKey add json serialization to PubKeyInner
|
||||||
type PubKey struct {
|
type PubKey struct {
|
||||||
PubKeyInner
|
PubKeyInner `json:"unwrap"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func WrapPubKey(pk PubKeyInner) PubKey {
|
func WrapPubKey(pk PubKeyInner) PubKey {
|
||||||
|
@ -29,7 +29,7 @@ func init() {
|
|||||||
|
|
||||||
// Signature add json serialization to Signature
|
// Signature add json serialization to Signature
|
||||||
type Signature struct {
|
type Signature struct {
|
||||||
SignatureInner
|
SignatureInner `json:"unwrap"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func WrapSignature(pk SignatureInner) Signature {
|
func WrapSignature(pk SignatureInner) Signature {
|
||||||
|
Reference in New Issue
Block a user