Release/0.22.5pull/2036/head v0.22.5
@ -1,37 +0,0 @@ | |||
package crypto | |||
import ( | |||
amino "github.com/tendermint/go-amino" | |||
) | |||
var cdc = amino.NewCodec() | |||
func init() { | |||
// NOTE: It's important that there be no conflicts here, | |||
// as that would change the canonical representations, | |||
// and therefore change the address. | |||
// TODO: Add feature to go-amino to ensure that there | |||
// are no conflicts. | |||
RegisterAmino(cdc) | |||
} | |||
// RegisterAmino registers all crypto related types in the given (amino) codec. | |||
func RegisterAmino(cdc *amino.Codec) { | |||
cdc.RegisterInterface((*PubKey)(nil), nil) | |||
cdc.RegisterConcrete(PubKeyEd25519{}, | |||
"tendermint/PubKeyEd25519", nil) | |||
cdc.RegisterConcrete(PubKeySecp256k1{}, | |||
"tendermint/PubKeySecp256k1", nil) | |||
cdc.RegisterInterface((*PrivKey)(nil), nil) | |||
cdc.RegisterConcrete(PrivKeyEd25519{}, | |||
"tendermint/PrivKeyEd25519", nil) | |||
cdc.RegisterConcrete(PrivKeySecp256k1{}, | |||
"tendermint/PrivKeySecp256k1", nil) | |||
cdc.RegisterInterface((*Signature)(nil), nil) | |||
cdc.RegisterConcrete(SignatureEd25519{}, | |||
"tendermint/SignatureEd25519", nil) | |||
cdc.RegisterConcrete(SignatureSecp256k1{}, | |||
"tendermint/SignatureSecp256k1", nil) | |||
} |
@ -0,0 +1,36 @@ | |||
package crypto | |||
import ( | |||
cmn "github.com/tendermint/tendermint/libs/common" | |||
) | |||
type PrivKey interface { | |||
Bytes() []byte | |||
Sign(msg []byte) (Signature, error) | |||
PubKey() PubKey | |||
Equals(PrivKey) bool | |||
} | |||
// An address is a []byte, but hex-encoded even in JSON. | |||
// []byte leaves us the option to change the address length. | |||
// Use an alias so Unmarshal methods (with ptr receivers) are available too. | |||
type Address = cmn.HexBytes | |||
type PubKey interface { | |||
Address() Address | |||
Bytes() []byte | |||
VerifyBytes(msg []byte, sig Signature) bool | |||
Equals(PubKey) bool | |||
} | |||
type Signature interface { | |||
Bytes() []byte | |||
IsZero() bool | |||
Equals(Signature) bool | |||
} | |||
type Symmetric interface { | |||
Keygen() []byte | |||
Encrypt(plaintext []byte, secret []byte) (ciphertext []byte) | |||
Decrypt(ciphertext []byte, secret []byte) (plaintext []byte, err error) | |||
} |
@ -0,0 +1,227 @@ | |||
package ed25519 | |||
import ( | |||
"bytes" | |||
"crypto/subtle" | |||
"fmt" | |||
"github.com/tendermint/ed25519" | |||
"github.com/tendermint/ed25519/extra25519" | |||
amino "github.com/tendermint/go-amino" | |||
"github.com/tendermint/tendermint/crypto" | |||
"github.com/tendermint/tendermint/crypto/tmhash" | |||
cmn "github.com/tendermint/tendermint/libs/common" | |||
) | |||
//------------------------------------- | |||
var _ crypto.PrivKey = PrivKeyEd25519{} | |||
const ( | |||
Ed25519PrivKeyAminoRoute = "tendermint/PrivKeyEd25519" | |||
Ed25519PubKeyAminoRoute = "tendermint/PubKeyEd25519" | |||
Ed25519SignatureAminoRoute = "tendermint/SignatureEd25519" | |||
) | |||
var cdc = amino.NewCodec() | |||
func init() { | |||
cdc.RegisterInterface((*crypto.PubKey)(nil), nil) | |||
cdc.RegisterConcrete(PubKeyEd25519{}, | |||
Ed25519PubKeyAminoRoute, nil) | |||
cdc.RegisterInterface((*crypto.PrivKey)(nil), nil) | |||
cdc.RegisterConcrete(PrivKeyEd25519{}, | |||
Ed25519PrivKeyAminoRoute, nil) | |||
cdc.RegisterInterface((*crypto.Signature)(nil), nil) | |||
cdc.RegisterConcrete(SignatureEd25519{}, | |||
Ed25519SignatureAminoRoute, nil) | |||
} | |||
// PrivKeyEd25519 implements crypto.PrivKey. | |||
type PrivKeyEd25519 [64]byte | |||
// Bytes marshals the privkey using amino encoding. | |||
func (privKey PrivKeyEd25519) Bytes() []byte { | |||
return cdc.MustMarshalBinaryBare(privKey) | |||
} | |||
// Sign produces a signature on the provided message. | |||
func (privKey PrivKeyEd25519) Sign(msg []byte) (crypto.Signature, error) { | |||
privKeyBytes := [64]byte(privKey) | |||
signatureBytes := ed25519.Sign(&privKeyBytes, msg) | |||
return SignatureEd25519(*signatureBytes), nil | |||
} | |||
// PubKey gets the corresponding public key from the private key. | |||
func (privKey PrivKeyEd25519) PubKey() crypto.PubKey { | |||
privKeyBytes := [64]byte(privKey) | |||
initialized := false | |||
// If the latter 32 bytes of the privkey are all zero, compute the pubkey | |||
// otherwise privkey is initialized and we can use the cached value inside | |||
// of the private key. | |||
for _, v := range privKeyBytes[32:] { | |||
if v != 0 { | |||
initialized = true | |||
break | |||
} | |||
} | |||
if initialized { | |||
var pubkeyBytes [PubKeyEd25519Size]byte | |||
copy(pubkeyBytes[:], privKeyBytes[32:]) | |||
return PubKeyEd25519(pubkeyBytes) | |||
} | |||
pubBytes := *ed25519.MakePublicKey(&privKeyBytes) | |||
return PubKeyEd25519(pubBytes) | |||
} | |||
// Equals - you probably don't need to use this. | |||
// Runs in constant time based on length of the keys. | |||
func (privKey PrivKeyEd25519) Equals(other crypto.PrivKey) bool { | |||
if otherEd, ok := other.(PrivKeyEd25519); ok { | |||
return subtle.ConstantTimeCompare(privKey[:], otherEd[:]) == 1 | |||
} else { | |||
return false | |||
} | |||
} | |||
// ToCurve25519 takes a private key and returns its representation on | |||
// Curve25519. Curve25519 is birationally equivalent to Edwards25519, | |||
// which Ed25519 uses internally. This method is intended for use in | |||
// an X25519 Diffie Hellman key exchange. | |||
func (privKey PrivKeyEd25519) ToCurve25519() *[PubKeyEd25519Size]byte { | |||
keyCurve25519 := new([32]byte) | |||
privKeyBytes := [64]byte(privKey) | |||
extra25519.PrivateKeyToCurve25519(keyCurve25519, &privKeyBytes) | |||
return keyCurve25519 | |||
} | |||
// GenPrivKey generates a new ed25519 private key. | |||
// It uses OS randomness in conjunction with the current global random seed | |||
// in tendermint/libs/common to generate the private key. | |||
func GenPrivKey() PrivKeyEd25519 { | |||
privKey := new([64]byte) | |||
copy(privKey[:32], crypto.CRandBytes(32)) | |||
// ed25519.MakePublicKey(privKey) alters the last 32 bytes of privKey. | |||
// It places the pubkey in the last 32 bytes of privKey, and returns the | |||
// public key. | |||
ed25519.MakePublicKey(privKey) | |||
return PrivKeyEd25519(*privKey) | |||
} | |||
// GenPrivKeyFromSecret hashes the secret with SHA2, and uses | |||
// that 32 byte output to create the private key. | |||
// NOTE: secret should be the output of a KDF like bcrypt, | |||
// if it's derived from user input. | |||
func GenPrivKeyFromSecret(secret []byte) PrivKeyEd25519 { | |||
privKey32 := crypto.Sha256(secret) // Not Ripemd160 because we want 32 bytes. | |||
privKey := new([64]byte) | |||
copy(privKey[:32], privKey32) | |||
// ed25519.MakePublicKey(privKey) alters the last 32 bytes of privKey. | |||
// It places the pubkey in the last 32 bytes of privKey, and returns the | |||
// public key. | |||
ed25519.MakePublicKey(privKey) | |||
return PrivKeyEd25519(*privKey) | |||
} | |||
//------------------------------------- | |||
var _ crypto.PubKey = PubKeyEd25519{} | |||
// PubKeyEd25519Size is the number of bytes in an Ed25519 signature. | |||
const PubKeyEd25519Size = 32 | |||
// PubKeyEd25519 implements crypto.PubKey for the Ed25519 signature scheme. | |||
type PubKeyEd25519 [PubKeyEd25519Size]byte | |||
// Address is the SHA256-20 of the raw pubkey bytes. | |||
func (pubKey PubKeyEd25519) Address() crypto.Address { | |||
return crypto.Address(tmhash.Sum(pubKey[:])) | |||
} | |||
// Bytes marshals the PubKey using amino encoding. | |||
func (pubKey PubKeyEd25519) Bytes() []byte { | |||
bz, err := cdc.MarshalBinaryBare(pubKey) | |||
if err != nil { | |||
panic(err) | |||
} | |||
return bz | |||
} | |||
func (pubKey PubKeyEd25519) VerifyBytes(msg []byte, sig_ crypto.Signature) bool { | |||
// make sure we use the same algorithm to sign | |||
sig, ok := sig_.(SignatureEd25519) | |||
if !ok { | |||
return false | |||
} | |||
pubKeyBytes := [PubKeyEd25519Size]byte(pubKey) | |||
sigBytes := [SignatureEd25519Size]byte(sig) | |||
return ed25519.Verify(&pubKeyBytes, msg, &sigBytes) | |||
} | |||
// ToCurve25519 takes a public key and returns its representation on | |||
// Curve25519. Curve25519 is birationally equivalent to Edwards25519, | |||
// which Ed25519 uses internally. This method is intended for use in | |||
// an X25519 Diffie Hellman key exchange. | |||
// | |||
// If there is an error, then this function returns nil. | |||
func (pubKey PubKeyEd25519) ToCurve25519() *[PubKeyEd25519Size]byte { | |||
keyCurve25519, pubKeyBytes := new([PubKeyEd25519Size]byte), [PubKeyEd25519Size]byte(pubKey) | |||
ok := extra25519.PublicKeyToCurve25519(keyCurve25519, &pubKeyBytes) | |||
if !ok { | |||
return nil | |||
} | |||
return keyCurve25519 | |||
} | |||
func (pubKey PubKeyEd25519) String() string { | |||
return fmt.Sprintf("PubKeyEd25519{%X}", pubKey[:]) | |||
} | |||
// nolint: golint | |||
func (pubKey PubKeyEd25519) Equals(other crypto.PubKey) bool { | |||
if otherEd, ok := other.(PubKeyEd25519); ok { | |||
return bytes.Equal(pubKey[:], otherEd[:]) | |||
} else { | |||
return false | |||
} | |||
} | |||
//------------------------------------- | |||
var _ crypto.Signature = SignatureEd25519{} | |||
// Size of an Edwards25519 signature. Namely the size of a compressed | |||
// Edwards25519 point, and a field element. Both of which are 32 bytes. | |||
const SignatureEd25519Size = 64 | |||
// SignatureEd25519 implements crypto.Signature | |||
type SignatureEd25519 [SignatureEd25519Size]byte | |||
func (sig SignatureEd25519) Bytes() []byte { | |||
bz, err := cdc.MarshalBinaryBare(sig) | |||
if err != nil { | |||
panic(err) | |||
} | |||
return bz | |||
} | |||
func (sig SignatureEd25519) IsZero() bool { return len(sig) == 0 } | |||
func (sig SignatureEd25519) String() string { return fmt.Sprintf("/%X.../", cmn.Fingerprint(sig[:])) } | |||
func (sig SignatureEd25519) Equals(other crypto.Signature) bool { | |||
if otherEd, ok := other.(SignatureEd25519); ok { | |||
return subtle.ConstantTimeCompare(sig[:], otherEd[:]) == 1 | |||
} else { | |||
return false | |||
} | |||
} | |||
func SignatureEd25519FromBytes(data []byte) crypto.Signature { | |||
var sig SignatureEd25519 | |||
copy(sig[:], data) | |||
return sig | |||
} |
@ -0,0 +1,31 @@ | |||
package ed25519_test | |||
import ( | |||
"testing" | |||
"github.com/stretchr/testify/assert" | |||
"github.com/stretchr/testify/require" | |||
"github.com/tendermint/tendermint/crypto" | |||
"github.com/tendermint/tendermint/crypto/ed25519" | |||
) | |||
func TestSignAndValidateEd25519(t *testing.T) { | |||
privKey := ed25519.GenPrivKey() | |||
pubKey := privKey.PubKey() | |||
msg := crypto.CRandBytes(128) | |||
sig, err := privKey.Sign(msg) | |||
require.Nil(t, err) | |||
// Test the signature | |||
assert.True(t, pubKey.VerifyBytes(msg, sig)) | |||
// Mutate the signature, just one bit. | |||
// TODO: Replace this with a much better fuzzer, tendermint/ed25519/issues/10 | |||
sigEd := sig.(ed25519.SignatureEd25519) | |||
sigEd[7] ^= byte(0x01) | |||
sig = sigEd | |||
assert.False(t, pubKey.VerifyBytes(msg, sig)) | |||
} |
@ -0,0 +1,57 @@ | |||
package cryptoAmino | |||
import ( | |||
amino "github.com/tendermint/go-amino" | |||
"github.com/tendermint/tendermint/crypto" | |||
"github.com/tendermint/tendermint/crypto/ed25519" | |||
"github.com/tendermint/tendermint/crypto/secp256k1" | |||
) | |||
var cdc = amino.NewCodec() | |||
func init() { | |||
// NOTE: It's important that there be no conflicts here, | |||
// as that would change the canonical representations, | |||
// and therefore change the address. | |||
// TODO: Remove above note when | |||
// https://github.com/tendermint/go-amino/issues/9 | |||
// is resolved | |||
RegisterAmino(cdc) | |||
} | |||
// RegisterAmino registers all crypto related types in the given (amino) codec. | |||
func RegisterAmino(cdc *amino.Codec) { | |||
// These are all written here instead of | |||
cdc.RegisterInterface((*crypto.PubKey)(nil), nil) | |||
cdc.RegisterConcrete(ed25519.PubKeyEd25519{}, | |||
"tendermint/PubKeyEd25519", nil) | |||
cdc.RegisterConcrete(secp256k1.PubKeySecp256k1{}, | |||
"tendermint/PubKeySecp256k1", nil) | |||
cdc.RegisterInterface((*crypto.PrivKey)(nil), nil) | |||
cdc.RegisterConcrete(ed25519.PrivKeyEd25519{}, | |||
"tendermint/PrivKeyEd25519", nil) | |||
cdc.RegisterConcrete(secp256k1.PrivKeySecp256k1{}, | |||
"tendermint/PrivKeySecp256k1", nil) | |||
cdc.RegisterInterface((*crypto.Signature)(nil), nil) | |||
cdc.RegisterConcrete(ed25519.SignatureEd25519{}, | |||
"tendermint/SignatureEd25519", nil) | |||
cdc.RegisterConcrete(secp256k1.SignatureSecp256k1{}, | |||
"tendermint/SignatureSecp256k1", nil) | |||
} | |||
func PrivKeyFromBytes(privKeyBytes []byte) (privKey crypto.PrivKey, err error) { | |||
err = cdc.UnmarshalBinaryBare(privKeyBytes, &privKey) | |||
return | |||
} | |||
func PubKeyFromBytes(pubKeyBytes []byte) (pubKey crypto.PubKey, err error) { | |||
err = cdc.UnmarshalBinaryBare(pubKeyBytes, &pubKey) | |||
return | |||
} | |||
func SignatureFromBytes(pubKeyBytes []byte) (pubKey crypto.Signature, err error) { | |||
err = cdc.UnmarshalBinaryBare(pubKeyBytes, &pubKey) | |||
return | |||
} |
@ -1,164 +0,0 @@ | |||
package crypto | |||
import ( | |||
"crypto/subtle" | |||
secp256k1 "github.com/btcsuite/btcd/btcec" | |||
"github.com/tendermint/ed25519" | |||
"github.com/tendermint/ed25519/extra25519" | |||
) | |||
func PrivKeyFromBytes(privKeyBytes []byte) (privKey PrivKey, err error) { | |||
err = cdc.UnmarshalBinaryBare(privKeyBytes, &privKey) | |||
return | |||
} | |||
//---------------------------------------- | |||
type PrivKey interface { | |||
Bytes() []byte | |||
Sign(msg []byte) (Signature, error) | |||
PubKey() PubKey | |||
Equals(PrivKey) bool | |||
} | |||
//------------------------------------- | |||
var _ PrivKey = PrivKeyEd25519{} | |||
// Implements PrivKey | |||
type PrivKeyEd25519 [64]byte | |||
func (privKey PrivKeyEd25519) Bytes() []byte { | |||
return cdc.MustMarshalBinaryBare(privKey) | |||
} | |||
func (privKey PrivKeyEd25519) Sign(msg []byte) (Signature, error) { | |||
privKeyBytes := [64]byte(privKey) | |||
signatureBytes := ed25519.Sign(&privKeyBytes, msg) | |||
return SignatureEd25519(*signatureBytes), nil | |||
} | |||
func (privKey PrivKeyEd25519) PubKey() PubKey { | |||
privKeyBytes := [64]byte(privKey) | |||
pubBytes := *ed25519.MakePublicKey(&privKeyBytes) | |||
return PubKeyEd25519(pubBytes) | |||
} | |||
// Equals - you probably don't need to use this. | |||
// Runs in constant time based on length of the keys. | |||
func (privKey PrivKeyEd25519) Equals(other PrivKey) bool { | |||
if otherEd, ok := other.(PrivKeyEd25519); ok { | |||
return subtle.ConstantTimeCompare(privKey[:], otherEd[:]) == 1 | |||
} else { | |||
return false | |||
} | |||
} | |||
func (privKey PrivKeyEd25519) ToCurve25519() *[32]byte { | |||
keyCurve25519 := new([32]byte) | |||
privKeyBytes := [64]byte(privKey) | |||
extra25519.PrivateKeyToCurve25519(keyCurve25519, &privKeyBytes) | |||
return keyCurve25519 | |||
} | |||
// Deterministically generates new priv-key bytes from key. | |||
func (privKey PrivKeyEd25519) Generate(index int) PrivKeyEd25519 { | |||
bz, err := cdc.MarshalBinaryBare(struct { | |||
PrivKey [64]byte | |||
Index int | |||
}{privKey, index}) | |||
if err != nil { | |||
panic(err) | |||
} | |||
newBytes := Sha256(bz) | |||
newKey := new([64]byte) | |||
copy(newKey[:32], newBytes) | |||
ed25519.MakePublicKey(newKey) | |||
return PrivKeyEd25519(*newKey) | |||
} | |||
func GenPrivKeyEd25519() PrivKeyEd25519 { | |||
privKeyBytes := new([64]byte) | |||
copy(privKeyBytes[:32], CRandBytes(32)) | |||
ed25519.MakePublicKey(privKeyBytes) | |||
return PrivKeyEd25519(*privKeyBytes) | |||
} | |||
// NOTE: secret should be the output of a KDF like bcrypt, | |||
// if it's derived from user input. | |||
func GenPrivKeyEd25519FromSecret(secret []byte) PrivKeyEd25519 { | |||
privKey32 := Sha256(secret) // Not Ripemd160 because we want 32 bytes. | |||
privKeyBytes := new([64]byte) | |||
copy(privKeyBytes[:32], privKey32) | |||
ed25519.MakePublicKey(privKeyBytes) | |||
return PrivKeyEd25519(*privKeyBytes) | |||
} | |||
//------------------------------------- | |||
var _ PrivKey = PrivKeySecp256k1{} | |||
// Implements PrivKey | |||
type PrivKeySecp256k1 [32]byte | |||
func (privKey PrivKeySecp256k1) Bytes() []byte { | |||
return cdc.MustMarshalBinaryBare(privKey) | |||
} | |||
func (privKey PrivKeySecp256k1) Sign(msg []byte) (Signature, error) { | |||
priv__, _ := secp256k1.PrivKeyFromBytes(secp256k1.S256(), privKey[:]) | |||
sig__, err := priv__.Sign(Sha256(msg)) | |||
if err != nil { | |||
return nil, err | |||
} | |||
return SignatureSecp256k1(sig__.Serialize()), nil | |||
} | |||
func (privKey PrivKeySecp256k1) PubKey() PubKey { | |||
_, pub__ := secp256k1.PrivKeyFromBytes(secp256k1.S256(), privKey[:]) | |||
var pub PubKeySecp256k1 | |||
copy(pub[:], pub__.SerializeCompressed()) | |||
return pub | |||
} | |||
// Equals - you probably don't need to use this. | |||
// Runs in constant time based on length of the keys. | |||
func (privKey PrivKeySecp256k1) Equals(other PrivKey) bool { | |||
if otherSecp, ok := other.(PrivKeySecp256k1); ok { | |||
return subtle.ConstantTimeCompare(privKey[:], otherSecp[:]) == 1 | |||
} else { | |||
return false | |||
} | |||
} | |||
/* | |||
// Deterministically generates new priv-key bytes from key. | |||
func (key PrivKeySecp256k1) Generate(index int) PrivKeySecp256k1 { | |||
newBytes := cdc.BinarySha256(struct { | |||
PrivKey [64]byte | |||
Index int | |||
}{key, index}) | |||
var newKey [64]byte | |||
copy(newKey[:], newBytes) | |||
return PrivKeySecp256k1(newKey) | |||
} | |||
*/ | |||
func GenPrivKeySecp256k1() PrivKeySecp256k1 { | |||
privKeyBytes := [32]byte{} | |||
copy(privKeyBytes[:], CRandBytes(32)) | |||
priv, _ := secp256k1.PrivKeyFromBytes(secp256k1.S256(), privKeyBytes[:]) | |||
copy(privKeyBytes[:], priv.Serialize()) | |||
return PrivKeySecp256k1(privKeyBytes) | |||
} | |||
// NOTE: secret should be the output of a KDF like bcrypt, | |||
// if it's derived from user input. | |||
func GenPrivKeySecp256k1FromSecret(secret []byte) PrivKeySecp256k1 { | |||
privKey32 := Sha256(secret) // Not Ripemd160 because we want 32 bytes. | |||
priv, _ := secp256k1.PrivKeyFromBytes(secp256k1.S256(), privKey32) | |||
privKeyBytes := [32]byte{} | |||
copy(privKeyBytes[:], priv.Serialize()) | |||
return PrivKeySecp256k1(privKeyBytes) | |||
} |
@ -1,60 +0,0 @@ | |||
package crypto_test | |||
import ( | |||
"testing" | |||
"github.com/stretchr/testify/assert" | |||
"github.com/tendermint/tendermint/crypto" | |||
) | |||
func TestGeneratePrivKey(t *testing.T) { | |||
testPriv := crypto.GenPrivKeyEd25519() | |||
testGenerate := testPriv.Generate(1) | |||
signBytes := []byte("something to sign") | |||
pub := testGenerate.PubKey() | |||
sig, err := testGenerate.Sign(signBytes) | |||
assert.NoError(t, err) | |||
assert.True(t, pub.VerifyBytes(signBytes, sig)) | |||
} | |||
/* | |||
type BadKey struct { | |||
PrivKeyEd25519 | |||
} | |||
func TestReadPrivKey(t *testing.T) { | |||
assert, require := assert.New(t), require.New(t) | |||
// garbage in, garbage out | |||
garbage := []byte("hjgewugfbiewgofwgewr") | |||
XXX This test wants to register BadKey globally to crypto, | |||
but we don't want to support that. | |||
_, err := PrivKeyFromBytes(garbage) | |||
require.Error(err) | |||
edKey := GenPrivKeyEd25519() | |||
badKey := BadKey{edKey} | |||
cases := []struct { | |||
key PrivKey | |||
valid bool | |||
}{ | |||
{edKey, true}, | |||
{badKey, false}, | |||
} | |||
for i, tc := range cases { | |||
data := tc.key.Bytes() | |||
fmt.Println(">>>", data) | |||
key, err := PrivKeyFromBytes(data) | |||
fmt.Printf("!!! %#v\n", key, err) | |||
if tc.valid { | |||
assert.NoError(err, "%d", i) | |||
assert.Equal(tc.key, key, "%d", i) | |||
} else { | |||
assert.Error(err, "%d: %#v", i, key) | |||
} | |||
} | |||
} | |||
*/ |
@ -1,153 +0,0 @@ | |||
package crypto | |||
import ( | |||
"bytes" | |||
"crypto/sha256" | |||
"fmt" | |||
"golang.org/x/crypto/ripemd160" | |||
secp256k1 "github.com/btcsuite/btcd/btcec" | |||
"github.com/tendermint/ed25519" | |||
"github.com/tendermint/ed25519/extra25519" | |||
cmn "github.com/tendermint/tendermint/libs/common" | |||
"github.com/tendermint/tendermint/crypto/tmhash" | |||
) | |||
// An address is a []byte, but hex-encoded even in JSON. | |||
// []byte leaves us the option to change the address length. | |||
// Use an alias so Unmarshal methods (with ptr receivers) are available too. | |||
type Address = cmn.HexBytes | |||
func PubKeyFromBytes(pubKeyBytes []byte) (pubKey PubKey, err error) { | |||
err = cdc.UnmarshalBinaryBare(pubKeyBytes, &pubKey) | |||
return | |||
} | |||
//---------------------------------------- | |||
type PubKey interface { | |||
Address() Address | |||
Bytes() []byte | |||
VerifyBytes(msg []byte, sig Signature) bool | |||
Equals(PubKey) bool | |||
} | |||
//------------------------------------- | |||
var _ PubKey = PubKeyEd25519{} | |||
const PubKeyEd25519Size = 32 | |||
// Implements PubKeyInner | |||
type PubKeyEd25519 [PubKeyEd25519Size]byte | |||
// Address is the SHA256-20 of the raw pubkey bytes. | |||
func (pubKey PubKeyEd25519) Address() Address { | |||
return Address(tmhash.Sum(pubKey[:])) | |||
} | |||
func (pubKey PubKeyEd25519) Bytes() []byte { | |||
bz, err := cdc.MarshalBinaryBare(pubKey) | |||
if err != nil { | |||
panic(err) | |||
} | |||
return bz | |||
} | |||
func (pubKey PubKeyEd25519) VerifyBytes(msg []byte, sig_ Signature) bool { | |||
// make sure we use the same algorithm to sign | |||
sig, ok := sig_.(SignatureEd25519) | |||
if !ok { | |||
return false | |||
} | |||
pubKeyBytes := [PubKeyEd25519Size]byte(pubKey) | |||
sigBytes := [SignatureEd25519Size]byte(sig) | |||
return ed25519.Verify(&pubKeyBytes, msg, &sigBytes) | |||
} | |||
// For use with golang/crypto/nacl/box | |||
// If error, returns nil. | |||
func (pubKey PubKeyEd25519) ToCurve25519() *[PubKeyEd25519Size]byte { | |||
keyCurve25519, pubKeyBytes := new([PubKeyEd25519Size]byte), [PubKeyEd25519Size]byte(pubKey) | |||
ok := extra25519.PublicKeyToCurve25519(keyCurve25519, &pubKeyBytes) | |||
if !ok { | |||
return nil | |||
} | |||
return keyCurve25519 | |||
} | |||
func (pubKey PubKeyEd25519) String() string { | |||
return fmt.Sprintf("PubKeyEd25519{%X}", pubKey[:]) | |||
} | |||
func (pubKey PubKeyEd25519) Equals(other PubKey) bool { | |||
if otherEd, ok := other.(PubKeyEd25519); ok { | |||
return bytes.Equal(pubKey[:], otherEd[:]) | |||
} else { | |||
return false | |||
} | |||
} | |||
//------------------------------------- | |||
var _ PubKey = PubKeySecp256k1{} | |||
const PubKeySecp256k1Size = 33 | |||
// Implements PubKey. | |||
// Compressed pubkey (just the x-cord), | |||
// prefixed with 0x02 or 0x03, depending on the y-cord. | |||
type PubKeySecp256k1 [PubKeySecp256k1Size]byte | |||
// Implements Bitcoin style addresses: RIPEMD160(SHA256(pubkey)) | |||
func (pubKey PubKeySecp256k1) Address() Address { | |||
hasherSHA256 := sha256.New() | |||
hasherSHA256.Write(pubKey[:]) // does not error | |||
sha := hasherSHA256.Sum(nil) | |||
hasherRIPEMD160 := ripemd160.New() | |||
hasherRIPEMD160.Write(sha) // does not error | |||
return Address(hasherRIPEMD160.Sum(nil)) | |||
} | |||
func (pubKey PubKeySecp256k1) Bytes() []byte { | |||
bz, err := cdc.MarshalBinaryBare(pubKey) | |||
if err != nil { | |||
panic(err) | |||
} | |||
return bz | |||
} | |||
func (pubKey PubKeySecp256k1) VerifyBytes(msg []byte, sig_ Signature) bool { | |||
// and assert same algorithm to sign and verify | |||
sig, ok := sig_.(SignatureSecp256k1) | |||
if !ok { | |||
return false | |||
} | |||
pub__, err := secp256k1.ParsePubKey(pubKey[:], secp256k1.S256()) | |||
if err != nil { | |||
return false | |||
} | |||
sig__, err := secp256k1.ParseDERSignature(sig[:], secp256k1.S256()) | |||
if err != nil { | |||
return false | |||
} | |||
return sig__.Verify(Sha256(msg), pub__) | |||
} | |||
func (pubKey PubKeySecp256k1) String() string { | |||
return fmt.Sprintf("PubKeySecp256k1{%X}", pubKey[:]) | |||
} | |||
func (pubKey PubKeySecp256k1) Equals(other PubKey) bool { | |||
if otherSecp, ok := other.(PubKeySecp256k1); ok { | |||
return bytes.Equal(pubKey[:], otherSecp[:]) | |||
} else { | |||
return false | |||
} | |||
} |
@ -1,50 +0,0 @@ | |||
package crypto | |||
import ( | |||
"encoding/hex" | |||
"testing" | |||
"github.com/btcsuite/btcutil/base58" | |||
"github.com/stretchr/testify/assert" | |||
"github.com/stretchr/testify/require" | |||
) | |||
type keyData struct { | |||
priv string | |||
pub string | |||
addr string | |||
} | |||
var secpDataTable = []keyData{ | |||
{ | |||
priv: "a96e62ed3955e65be32703f12d87b6b5cf26039ecfa948dc5107a495418e5330", | |||
pub: "02950e1cdfcb133d6024109fd489f734eeb4502418e538c28481f22bce276f248c", | |||
addr: "1CKZ9Nx4zgds8tU7nJHotKSDr4a9bYJCa3", | |||
}, | |||
} | |||
func TestPubKeySecp256k1Address(t *testing.T) { | |||
for _, d := range secpDataTable { | |||
privB, _ := hex.DecodeString(d.priv) | |||
pubB, _ := hex.DecodeString(d.pub) | |||
addrBbz, _, _ := base58.CheckDecode(d.addr) | |||
addrB := Address(addrBbz) | |||
var priv PrivKeySecp256k1 | |||
copy(priv[:], privB) | |||
pubKey := priv.PubKey() | |||
pubT, _ := pubKey.(PubKeySecp256k1) | |||
pub := pubT[:] | |||
addr := pubKey.Address() | |||
assert.Equal(t, pub, pubB, "Expected pub keys to match") | |||
assert.Equal(t, addr, addrB, "Expected addresses to match") | |||
} | |||
} | |||
func TestPubKeyInvalidDataProperReturnsEmpty(t *testing.T) { | |||
pk, err := PubKeyFromBytes([]byte("foo")) | |||
require.NotNil(t, err, "expecting a non-nil error") | |||
require.Nil(t, pk, "expecting an empty public key on error") | |||
} |
@ -0,0 +1,198 @@ | |||
package secp256k1 | |||
import ( | |||
"bytes" | |||
"crypto/sha256" | |||
"crypto/subtle" | |||
"fmt" | |||
secp256k1 "github.com/btcsuite/btcd/btcec" | |||
amino "github.com/tendermint/go-amino" | |||
"github.com/tendermint/tendermint/crypto" | |||
"github.com/tendermint/tendermint/libs/common" | |||
"golang.org/x/crypto/ripemd160" | |||
) | |||
//------------------------------------- | |||
const ( | |||
Secp256k1PrivKeyAminoRoute = "tendermint/PrivKeySecp256k1" | |||
Secp256k1PubKeyAminoRoute = "tendermint/PubKeySecp256k1" | |||
Secp256k1SignatureAminoRoute = "tendermint/SignatureSecp256k1" | |||
) | |||
var cdc = amino.NewCodec() | |||
func init() { | |||
cdc.RegisterInterface((*crypto.PubKey)(nil), nil) | |||
cdc.RegisterConcrete(PubKeySecp256k1{}, | |||
Secp256k1PubKeyAminoRoute, nil) | |||
cdc.RegisterInterface((*crypto.PrivKey)(nil), nil) | |||
cdc.RegisterConcrete(PrivKeySecp256k1{}, | |||
Secp256k1PrivKeyAminoRoute, nil) | |||
cdc.RegisterInterface((*crypto.Signature)(nil), nil) | |||
cdc.RegisterConcrete(SignatureSecp256k1{}, | |||
Secp256k1SignatureAminoRoute, nil) | |||
} | |||
//------------------------------------- | |||
var _ crypto.PrivKey = PrivKeySecp256k1{} | |||
// PrivKeySecp256k1 implements PrivKey. | |||
type PrivKeySecp256k1 [32]byte | |||
// Bytes marshalls the private key using amino encoding. | |||
func (privKey PrivKeySecp256k1) Bytes() []byte { | |||
return cdc.MustMarshalBinaryBare(privKey) | |||
} | |||
// Sign creates an ECDSA signature on curve Secp256k1, using SHA256 on the msg. | |||
func (privKey PrivKeySecp256k1) Sign(msg []byte) (crypto.Signature, error) { | |||
priv, _ := secp256k1.PrivKeyFromBytes(secp256k1.S256(), privKey[:]) | |||
sig, err := priv.Sign(crypto.Sha256(msg)) | |||
if err != nil { | |||
return nil, err | |||
} | |||
return SignatureSecp256k1(sig.Serialize()), nil | |||
} | |||
// PubKey performs the point-scalar multiplication from the privKey on the | |||
// generator point to get the pubkey. | |||
func (privKey PrivKeySecp256k1) PubKey() crypto.PubKey { | |||
_, pubkeyObject := secp256k1.PrivKeyFromBytes(secp256k1.S256(), privKey[:]) | |||
var pubkeyBytes PubKeySecp256k1 | |||
copy(pubkeyBytes[:], pubkeyObject.SerializeCompressed()) | |||
return pubkeyBytes | |||
} | |||
// Equals - you probably don't need to use this. | |||
// Runs in constant time based on length of the keys. | |||
func (privKey PrivKeySecp256k1) Equals(other crypto.PrivKey) bool { | |||
if otherSecp, ok := other.(PrivKeySecp256k1); ok { | |||
return subtle.ConstantTimeCompare(privKey[:], otherSecp[:]) == 1 | |||
} | |||
return false | |||
} | |||
// GenPrivKey generates a new ECDSA private key on curve secp256k1 private key. | |||
// It uses OS randomness in conjunction with the current global random seed | |||
// in tendermint/libs/common to generate the private key. | |||
func GenPrivKey() PrivKeySecp256k1 { | |||
privKeyBytes := [32]byte{} | |||
copy(privKeyBytes[:], crypto.CRandBytes(32)) | |||
// crypto.CRandBytes is guaranteed to be 32 bytes long, so it can be | |||
// casted to PrivKeySecp256k1. | |||
return PrivKeySecp256k1(privKeyBytes) | |||
} | |||
// GenPrivKeySecp256k1 hashes the secret with SHA2, and uses | |||
// that 32 byte output to create the private key. | |||
// NOTE: secret should be the output of a KDF like bcrypt, | |||
// if it's derived from user input. | |||
func GenPrivKeySecp256k1(secret []byte) PrivKeySecp256k1 { | |||
privKey32 := sha256.Sum256(secret) | |||
// sha256.Sum256() is guaranteed to be 32 bytes long, so it can be | |||
// casted to PrivKeySecp256k1. | |||
return PrivKeySecp256k1(privKey32) | |||
} | |||
//------------------------------------- | |||
var _ crypto.PubKey = PubKeySecp256k1{} | |||
// PubKeySecp256k1Size is comprised of 32 bytes for one field element | |||
// (the x-coordinate), plus one byte for the parity of the y-coordinate. | |||
const PubKeySecp256k1Size = 33 | |||
// PubKeySecp256k1 implements crypto.PubKey. | |||
// It is the compressed form of the pubkey. The first byte depends is a 0x02 byte | |||
// if the y-coordinate is the lexicographically largest of the two associated with | |||
// the x-coordinate. Otherwise the first byte is a 0x03. | |||
// This prefix is followed with the x-coordinate. | |||
type PubKeySecp256k1 [PubKeySecp256k1Size]byte | |||
// Address returns a Bitcoin style addresses: RIPEMD160(SHA256(pubkey)) | |||
func (pubKey PubKeySecp256k1) Address() crypto.Address { | |||
hasherSHA256 := sha256.New() | |||
hasherSHA256.Write(pubKey[:]) // does not error | |||
sha := hasherSHA256.Sum(nil) | |||
hasherRIPEMD160 := ripemd160.New() | |||
hasherRIPEMD160.Write(sha) // does not error | |||
return crypto.Address(hasherRIPEMD160.Sum(nil)) | |||
} | |||
// Bytes returns the pubkey marshalled with amino encoding. | |||
func (pubKey PubKeySecp256k1) Bytes() []byte { | |||
bz, err := cdc.MarshalBinaryBare(pubKey) | |||
if err != nil { | |||
panic(err) | |||
} | |||
return bz | |||
} | |||
func (pubKey PubKeySecp256k1) VerifyBytes(msg []byte, interfaceSig crypto.Signature) bool { | |||
// and assert same algorithm to sign and verify | |||
sig, ok := interfaceSig.(SignatureSecp256k1) | |||
if !ok { | |||
return false | |||
} | |||
pub, err := secp256k1.ParsePubKey(pubKey[:], secp256k1.S256()) | |||
if err != nil { | |||
return false | |||
} | |||
parsedSig, err := secp256k1.ParseDERSignature(sig[:], secp256k1.S256()) | |||
if err != nil { | |||
return false | |||
} | |||
return parsedSig.Verify(crypto.Sha256(msg), pub) | |||
} | |||
func (pubKey PubKeySecp256k1) String() string { | |||
return fmt.Sprintf("PubKeySecp256k1{%X}", pubKey[:]) | |||
} | |||
func (pubKey PubKeySecp256k1) Equals(other crypto.PubKey) bool { | |||
if otherSecp, ok := other.(PubKeySecp256k1); ok { | |||
return bytes.Equal(pubKey[:], otherSecp[:]) | |||
} | |||
return false | |||
} | |||
//------------------------------------- | |||
var _ crypto.Signature = SignatureSecp256k1{} | |||
// SignatureSecp256k1 implements crypto.Signature | |||
type SignatureSecp256k1 []byte | |||
func (sig SignatureSecp256k1) Bytes() []byte { | |||
bz, err := cdc.MarshalBinaryBare(sig) | |||
if err != nil { | |||
panic(err) | |||
} | |||
return bz | |||
} | |||
func (sig SignatureSecp256k1) IsZero() bool { return len(sig) == 0 } | |||
func (sig SignatureSecp256k1) String() string { | |||
return fmt.Sprintf("/%X.../", common.Fingerprint(sig[:])) | |||
} | |||
func (sig SignatureSecp256k1) Equals(other crypto.Signature) bool { | |||
if otherSecp, ok := other.(SignatureSecp256k1); ok { | |||
return subtle.ConstantTimeCompare(sig[:], otherSecp[:]) == 1 | |||
} else { | |||
return false | |||
} | |||
} | |||
func SignatureSecp256k1FromBytes(data []byte) crypto.Signature { | |||
sig := make(SignatureSecp256k1, len(data)) | |||
copy(sig[:], data) | |||
return sig | |||
} |
@ -0,0 +1,88 @@ | |||
package secp256k1_test | |||
import ( | |||
"encoding/hex" | |||
"testing" | |||
"github.com/btcsuite/btcutil/base58" | |||
"github.com/stretchr/testify/assert" | |||
"github.com/stretchr/testify/require" | |||
"github.com/tendermint/tendermint/crypto" | |||
"github.com/tendermint/tendermint/crypto/secp256k1" | |||
underlyingSecp256k1 "github.com/btcsuite/btcd/btcec" | |||
) | |||
type keyData struct { | |||
priv string | |||
pub string | |||
addr string | |||
} | |||
var secpDataTable = []keyData{ | |||
{ | |||
priv: "a96e62ed3955e65be32703f12d87b6b5cf26039ecfa948dc5107a495418e5330", | |||
pub: "02950e1cdfcb133d6024109fd489f734eeb4502418e538c28481f22bce276f248c", | |||
addr: "1CKZ9Nx4zgds8tU7nJHotKSDr4a9bYJCa3", | |||
}, | |||
} | |||
func TestPubKeySecp256k1Address(t *testing.T) { | |||
for _, d := range secpDataTable { | |||
privB, _ := hex.DecodeString(d.priv) | |||
pubB, _ := hex.DecodeString(d.pub) | |||
addrBbz, _, _ := base58.CheckDecode(d.addr) | |||
addrB := crypto.Address(addrBbz) | |||
var priv secp256k1.PrivKeySecp256k1 | |||
copy(priv[:], privB) | |||
pubKey := priv.PubKey() | |||
pubT, _ := pubKey.(secp256k1.PubKeySecp256k1) | |||
pub := pubT[:] | |||
addr := pubKey.Address() | |||
assert.Equal(t, pub, pubB, "Expected pub keys to match") | |||
assert.Equal(t, addr, addrB, "Expected addresses to match") | |||
} | |||
} | |||
func TestSignAndValidateSecp256k1(t *testing.T) { | |||
privKey := secp256k1.GenPrivKey() | |||
pubKey := privKey.PubKey() | |||
msg := crypto.CRandBytes(128) | |||
sig, err := privKey.Sign(msg) | |||
require.Nil(t, err) | |||
assert.True(t, pubKey.VerifyBytes(msg, sig)) | |||
// Mutate the signature, just one bit. | |||
sigEd := sig.(secp256k1.SignatureSecp256k1) | |||
sigEd[3] ^= byte(0x01) | |||
sig = sigEd | |||
assert.False(t, pubKey.VerifyBytes(msg, sig)) | |||
} | |||
// This test is intended to justify the removal of calls to the underlying library | |||
// in creating the privkey. | |||
func TestSecp256k1LoadPrivkeyAndSerializeIsIdentity(t *testing.T) { | |||
numberOfTests := 256 | |||
for i := 0; i < numberOfTests; i++ { | |||
// Seed the test case with some random bytes | |||
privKeyBytes := [32]byte{} | |||
copy(privKeyBytes[:], crypto.CRandBytes(32)) | |||
// This function creates a private and public key in the underlying libraries format. | |||
// The private key is basically calling new(big.Int).SetBytes(pk), which removes leading zero bytes | |||
priv, _ := underlyingSecp256k1.PrivKeyFromBytes(underlyingSecp256k1.S256(), privKeyBytes[:]) | |||
// this takes the bytes returned by `(big int).Bytes()`, and if the length is less than 32 bytes, | |||
// pads the bytes from the left with zero bytes. Therefore these two functions composed | |||
// result in the identity function on privKeyBytes, hence the following equality check | |||
// always returning true. | |||
serializedBytes := priv.Serialize() | |||
require.Equal(t, privKeyBytes[:], serializedBytes) | |||
} | |||
} |
@ -1,90 +0,0 @@ | |||
package crypto | |||
import ( | |||
"fmt" | |||
"crypto/subtle" | |||
. "github.com/tendermint/tendermint/libs/common" | |||
) | |||
func SignatureFromBytes(pubKeyBytes []byte) (pubKey Signature, err error) { | |||
err = cdc.UnmarshalBinaryBare(pubKeyBytes, &pubKey) | |||
return | |||
} | |||
//---------------------------------------- | |||
type Signature interface { | |||
Bytes() []byte | |||
IsZero() bool | |||
Equals(Signature) bool | |||
} | |||
//------------------------------------- | |||
var _ Signature = SignatureEd25519{} | |||
const SignatureEd25519Size = 64 | |||
// Implements Signature | |||
type SignatureEd25519 [SignatureEd25519Size]byte | |||
func (sig SignatureEd25519) Bytes() []byte { | |||
bz, err := cdc.MarshalBinaryBare(sig) | |||
if err != nil { | |||
panic(err) | |||
} | |||
return bz | |||
} | |||
func (sig SignatureEd25519) IsZero() bool { return len(sig) == 0 } | |||
func (sig SignatureEd25519) String() string { return fmt.Sprintf("/%X.../", Fingerprint(sig[:])) } | |||
func (sig SignatureEd25519) Equals(other Signature) bool { | |||
if otherEd, ok := other.(SignatureEd25519); ok { | |||
return subtle.ConstantTimeCompare(sig[:], otherEd[:]) == 1 | |||
} else { | |||
return false | |||
} | |||
} | |||
func SignatureEd25519FromBytes(data []byte) Signature { | |||
var sig SignatureEd25519 | |||
copy(sig[:], data) | |||
return sig | |||
} | |||
//------------------------------------- | |||
var _ Signature = SignatureSecp256k1{} | |||
// Implements Signature | |||
type SignatureSecp256k1 []byte | |||
func (sig SignatureSecp256k1) Bytes() []byte { | |||
bz, err := cdc.MarshalBinaryBare(sig) | |||
if err != nil { | |||
panic(err) | |||
} | |||
return bz | |||
} | |||
func (sig SignatureSecp256k1) IsZero() bool { return len(sig) == 0 } | |||
func (sig SignatureSecp256k1) String() string { return fmt.Sprintf("/%X.../", Fingerprint(sig[:])) } | |||
func (sig SignatureSecp256k1) Equals(other Signature) bool { | |||
if otherSecp, ok := other.(SignatureSecp256k1); ok { | |||
return subtle.ConstantTimeCompare(sig[:], otherSecp[:]) == 1 | |||
} else { | |||
return false | |||
} | |||
} | |||
func SignatureSecp256k1FromBytes(data []byte) Signature { | |||
sig := make(SignatureSecp256k1, len(data)) | |||
copy(sig[:], data) | |||
return sig | |||
} |
@ -1,46 +0,0 @@ | |||
package crypto | |||
import ( | |||
"testing" | |||
"github.com/stretchr/testify/assert" | |||
"github.com/stretchr/testify/require" | |||
) | |||
func TestSignAndValidateEd25519(t *testing.T) { | |||
privKey := GenPrivKeyEd25519() | |||
pubKey := privKey.PubKey() | |||
msg := CRandBytes(128) | |||
sig, err := privKey.Sign(msg) | |||
require.Nil(t, err) | |||
// Test the signature | |||
assert.True(t, pubKey.VerifyBytes(msg, sig)) | |||
// Mutate the signature, just one bit. | |||
sigEd := sig.(SignatureEd25519) | |||
sigEd[7] ^= byte(0x01) | |||
sig = sigEd | |||
assert.False(t, pubKey.VerifyBytes(msg, sig)) | |||
} | |||
func TestSignAndValidateSecp256k1(t *testing.T) { | |||
privKey := GenPrivKeySecp256k1() | |||
pubKey := privKey.PubKey() | |||
msg := CRandBytes(128) | |||
sig, err := privKey.Sign(msg) | |||
require.Nil(t, err) | |||
assert.True(t, pubKey.VerifyBytes(msg, sig)) | |||
// Mutate the signature, just one bit. | |||
sigEd := sig.(SignatureSecp256k1) | |||
sigEd[3] ^= byte(0x01) | |||
sig = sigEd | |||
assert.False(t, pubKey.VerifyBytes(msg, sig)) | |||
} |
@ -0,0 +1,280 @@ | |||
// Code generated by protoc-gen-gogo. DO NOT EDIT. | |||
// source: libs/common/types.proto | |||
/* | |||
Package common is a generated protocol buffer package. | |||
It is generated from these files: | |||
libs/common/types.proto | |||
It has these top-level messages: | |||
KVPair | |||
KI64Pair | |||
*/ | |||
package common | |||
import testing "testing" | |||
import rand "math/rand" | |||
import time "time" | |||
import proto "github.com/gogo/protobuf/proto" | |||
import jsonpb "github.com/gogo/protobuf/jsonpb" | |||
import golang_proto "github.com/golang/protobuf/proto" | |||
import fmt "fmt" | |||
import math "math" | |||
import _ "github.com/gogo/protobuf/gogoproto" | |||
// Reference imports to suppress errors if they are not otherwise used. | |||
var _ = proto.Marshal | |||
var _ = golang_proto.Marshal | |||
var _ = fmt.Errorf | |||
var _ = math.Inf | |||
func TestKVPairProto(t *testing.T) { | |||
seed := time.Now().UnixNano() | |||
popr := rand.New(rand.NewSource(seed)) | |||
p := NewPopulatedKVPair(popr, false) | |||
dAtA, err := proto.Marshal(p) | |||
if err != nil { | |||
t.Fatalf("seed = %d, err = %v", seed, err) | |||
} | |||
msg := &KVPair{} | |||
if err := proto.Unmarshal(dAtA, msg); err != nil { | |||
t.Fatalf("seed = %d, err = %v", seed, err) | |||
} | |||
littlefuzz := make([]byte, len(dAtA)) | |||
copy(littlefuzz, dAtA) | |||
for i := range dAtA { | |||
dAtA[i] = byte(popr.Intn(256)) | |||
} | |||
if !p.Equal(msg) { | |||
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p) | |||
} | |||
if len(littlefuzz) > 0 { | |||
fuzzamount := 100 | |||
for i := 0; i < fuzzamount; i++ { | |||
littlefuzz[popr.Intn(len(littlefuzz))] = byte(popr.Intn(256)) | |||
littlefuzz = append(littlefuzz, byte(popr.Intn(256))) | |||
} | |||
// shouldn't panic | |||
_ = proto.Unmarshal(littlefuzz, msg) | |||
} | |||
} | |||
func TestKVPairMarshalTo(t *testing.T) { | |||
seed := time.Now().UnixNano() | |||
popr := rand.New(rand.NewSource(seed)) | |||
p := NewPopulatedKVPair(popr, false) | |||
size := p.Size() | |||
dAtA := make([]byte, size) | |||
for i := range dAtA { | |||
dAtA[i] = byte(popr.Intn(256)) | |||
} | |||
_, err := p.MarshalTo(dAtA) | |||
if err != nil { | |||
t.Fatalf("seed = %d, err = %v", seed, err) | |||
} | |||
msg := &KVPair{} | |||
if err := proto.Unmarshal(dAtA, msg); err != nil { | |||
t.Fatalf("seed = %d, err = %v", seed, err) | |||
} | |||
for i := range dAtA { | |||
dAtA[i] = byte(popr.Intn(256)) | |||
} | |||
if !p.Equal(msg) { | |||
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p) | |||
} | |||
} | |||
func TestKI64PairProto(t *testing.T) { | |||
seed := time.Now().UnixNano() | |||
popr := rand.New(rand.NewSource(seed)) | |||
p := NewPopulatedKI64Pair(popr, false) | |||
dAtA, err := proto.Marshal(p) | |||
if err != nil { | |||
t.Fatalf("seed = %d, err = %v", seed, err) | |||
} | |||
msg := &KI64Pair{} | |||
if err := proto.Unmarshal(dAtA, msg); err != nil { | |||
t.Fatalf("seed = %d, err = %v", seed, err) | |||
} | |||
littlefuzz := make([]byte, len(dAtA)) | |||
copy(littlefuzz, dAtA) | |||
for i := range dAtA { | |||
dAtA[i] = byte(popr.Intn(256)) | |||
} | |||
if !p.Equal(msg) { | |||
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p) | |||
} | |||
if len(littlefuzz) > 0 { | |||
fuzzamount := 100 | |||
for i := 0; i < fuzzamount; i++ { | |||
littlefuzz[popr.Intn(len(littlefuzz))] = byte(popr.Intn(256)) | |||
littlefuzz = append(littlefuzz, byte(popr.Intn(256))) | |||
} | |||
// shouldn't panic | |||
_ = proto.Unmarshal(littlefuzz, msg) | |||
} | |||
} | |||
func TestKI64PairMarshalTo(t *testing.T) { | |||
seed := time.Now().UnixNano() | |||
popr := rand.New(rand.NewSource(seed)) | |||
p := NewPopulatedKI64Pair(popr, false) | |||
size := p.Size() | |||
dAtA := make([]byte, size) | |||
for i := range dAtA { | |||
dAtA[i] = byte(popr.Intn(256)) | |||
} | |||
_, err := p.MarshalTo(dAtA) | |||
if err != nil { | |||
t.Fatalf("seed = %d, err = %v", seed, err) | |||
} | |||
msg := &KI64Pair{} | |||
if err := proto.Unmarshal(dAtA, msg); err != nil { | |||
t.Fatalf("seed = %d, err = %v", seed, err) | |||
} | |||
for i := range dAtA { | |||
dAtA[i] = byte(popr.Intn(256)) | |||
} | |||
if !p.Equal(msg) { | |||
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p) | |||
} | |||
} | |||
func TestKVPairJSON(t *testing.T) { | |||
seed := time.Now().UnixNano() | |||
popr := rand.New(rand.NewSource(seed)) | |||
p := NewPopulatedKVPair(popr, true) | |||
marshaler := jsonpb.Marshaler{} | |||
jsondata, err := marshaler.MarshalToString(p) | |||
if err != nil { | |||
t.Fatalf("seed = %d, err = %v", seed, err) | |||
} | |||
msg := &KVPair{} | |||
err = jsonpb.UnmarshalString(jsondata, msg) | |||
if err != nil { | |||
t.Fatalf("seed = %d, err = %v", seed, err) | |||
} | |||
if !p.Equal(msg) { | |||
t.Fatalf("seed = %d, %#v !Json Equal %#v", seed, msg, p) | |||
} | |||
} | |||
func TestKI64PairJSON(t *testing.T) { | |||
seed := time.Now().UnixNano() | |||
popr := rand.New(rand.NewSource(seed)) | |||
p := NewPopulatedKI64Pair(popr, true) | |||
marshaler := jsonpb.Marshaler{} | |||
jsondata, err := marshaler.MarshalToString(p) | |||
if err != nil { | |||
t.Fatalf("seed = %d, err = %v", seed, err) | |||
} | |||
msg := &KI64Pair{} | |||
err = jsonpb.UnmarshalString(jsondata, msg) | |||
if err != nil { | |||
t.Fatalf("seed = %d, err = %v", seed, err) | |||
} | |||
if !p.Equal(msg) { | |||
t.Fatalf("seed = %d, %#v !Json Equal %#v", seed, msg, p) | |||
} | |||
} | |||
func TestKVPairProtoText(t *testing.T) { | |||
seed := time.Now().UnixNano() | |||
popr := rand.New(rand.NewSource(seed)) | |||
p := NewPopulatedKVPair(popr, true) | |||
dAtA := proto.MarshalTextString(p) | |||
msg := &KVPair{} | |||
if err := proto.UnmarshalText(dAtA, msg); err != nil { | |||
t.Fatalf("seed = %d, err = %v", seed, err) | |||
} | |||
if !p.Equal(msg) { | |||
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p) | |||
} | |||
} | |||
func TestKVPairProtoCompactText(t *testing.T) { | |||
seed := time.Now().UnixNano() | |||
popr := rand.New(rand.NewSource(seed)) | |||
p := NewPopulatedKVPair(popr, true) | |||
dAtA := proto.CompactTextString(p) | |||
msg := &KVPair{} | |||
if err := proto.UnmarshalText(dAtA, msg); err != nil { | |||
t.Fatalf("seed = %d, err = %v", seed, err) | |||
} | |||
if !p.Equal(msg) { | |||
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p) | |||
} | |||
} | |||
func TestKI64PairProtoText(t *testing.T) { | |||
seed := time.Now().UnixNano() | |||
popr := rand.New(rand.NewSource(seed)) | |||
p := NewPopulatedKI64Pair(popr, true) | |||
dAtA := proto.MarshalTextString(p) | |||
msg := &KI64Pair{} | |||
if err := proto.UnmarshalText(dAtA, msg); err != nil { | |||
t.Fatalf("seed = %d, err = %v", seed, err) | |||
} | |||
if !p.Equal(msg) { | |||
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p) | |||
} | |||
} | |||
func TestKI64PairProtoCompactText(t *testing.T) { | |||
seed := time.Now().UnixNano() | |||
popr := rand.New(rand.NewSource(seed)) | |||
p := NewPopulatedKI64Pair(popr, true) | |||
dAtA := proto.CompactTextString(p) | |||
msg := &KI64Pair{} | |||
if err := proto.UnmarshalText(dAtA, msg); err != nil { | |||
t.Fatalf("seed = %d, err = %v", seed, err) | |||
} | |||
if !p.Equal(msg) { | |||
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p) | |||
} | |||
} | |||
func TestKVPairSize(t *testing.T) { | |||
seed := time.Now().UnixNano() | |||
popr := rand.New(rand.NewSource(seed)) | |||
p := NewPopulatedKVPair(popr, true) | |||
size2 := proto.Size(p) | |||
dAtA, err := proto.Marshal(p) | |||
if err != nil { | |||
t.Fatalf("seed = %d, err = %v", seed, err) | |||
} | |||
size := p.Size() | |||
if len(dAtA) != size { | |||
t.Errorf("seed = %d, size %v != marshalled size %v", seed, size, len(dAtA)) | |||
} | |||
if size2 != size { | |||
t.Errorf("seed = %d, size %v != before marshal proto.Size %v", seed, size, size2) | |||
} | |||
size3 := proto.Size(p) | |||
if size3 != size { | |||
t.Errorf("seed = %d, size %v != after marshal proto.Size %v", seed, size, size3) | |||
} | |||
} | |||
func TestKI64PairSize(t *testing.T) { | |||
seed := time.Now().UnixNano() | |||
popr := rand.New(rand.NewSource(seed)) | |||
p := NewPopulatedKI64Pair(popr, true) | |||
size2 := proto.Size(p) | |||
dAtA, err := proto.Marshal(p) | |||
if err != nil { | |||
t.Fatalf("seed = %d, err = %v", seed, err) | |||
} | |||
size := p.Size() | |||
if len(dAtA) != size { | |||
t.Errorf("seed = %d, size %v != marshalled size %v", seed, size, len(dAtA)) | |||
} | |||
if size2 != size { | |||
t.Errorf("seed = %d, size %v != before marshal proto.Size %v", seed, size, size2) | |||
} | |||
size3 := proto.Size(p) | |||
if size3 != size { | |||
t.Errorf("seed = %d, size %v != after marshal proto.Size %v", seed, size, size3) | |||
} | |||
} | |||
//These tests are generated by github.com/gogo/protobuf/plugin/testgen |
@ -0,0 +1,39 @@ | |||
package core | |||
import ( | |||
"testing" | |||
"time" | |||
"github.com/tendermint/tendermint/types" | |||
) | |||
func TestGetValidatorsWithTimeout(t *testing.T) { | |||
height, vs := getValidatorsWithTimeout( | |||
testValidatorReceiver{}, | |||
time.Millisecond, | |||
) | |||
if height != -1 { | |||
t.Errorf("expected negative height") | |||
} | |||
if len(vs) != 0 { | |||
t.Errorf("expected no validators") | |||
} | |||
} | |||
type testValidatorReceiver struct{} | |||
func (tr testValidatorReceiver) GetValidators() (int64, []*types.Validator) { | |||
vs := []*types.Validator{} | |||
for i := 0; i < 3; i++ { | |||
v, _ := types.RandValidator(true, 10) | |||
vs = append(vs, v) | |||
} | |||
time.Sleep(time.Millisecond) | |||
return 10, vs | |||
} |
@ -1,3 +0,0 @@ | |||
#! /bin/bash | |||
protoc --go_out=plugins=grpc:. -I $GOPATH/src/ -I . types.proto |
@ -0,0 +1,531 @@ | |||
// Code generated by protoc-gen-gogo. DO NOT EDIT. | |||
// source: rpc/grpc/types.proto | |||
/* | |||
Package core_grpc is a generated protocol buffer package. | |||
It is generated from these files: | |||
rpc/grpc/types.proto | |||
It has these top-level messages: | |||
RequestPing | |||
RequestBroadcastTx | |||
ResponsePing | |||
ResponseBroadcastTx | |||
*/ | |||
package core_grpc | |||
import testing "testing" | |||
import rand "math/rand" | |||
import time "time" | |||
import proto "github.com/gogo/protobuf/proto" | |||
import jsonpb "github.com/gogo/protobuf/jsonpb" | |||
import golang_proto "github.com/golang/protobuf/proto" | |||
import fmt "fmt" | |||
import math "math" | |||
import _ "github.com/gogo/protobuf/gogoproto" | |||
import _ "github.com/tendermint/tendermint/abci/types" | |||
// Reference imports to suppress errors if they are not otherwise used. | |||
var _ = proto.Marshal | |||
var _ = golang_proto.Marshal | |||
var _ = fmt.Errorf | |||
var _ = math.Inf | |||
func TestRequestPingProto(t *testing.T) { | |||
seed := time.Now().UnixNano() | |||
popr := rand.New(rand.NewSource(seed)) | |||
p := NewPopulatedRequestPing(popr, false) | |||
dAtA, err := proto.Marshal(p) | |||
if err != nil { | |||
t.Fatalf("seed = %d, err = %v", seed, err) | |||
} | |||
msg := &RequestPing{} | |||
if err := proto.Unmarshal(dAtA, msg); err != nil { | |||
t.Fatalf("seed = %d, err = %v", seed, err) | |||
} | |||
littlefuzz := make([]byte, len(dAtA)) | |||
copy(littlefuzz, dAtA) | |||
for i := range dAtA { | |||
dAtA[i] = byte(popr.Intn(256)) | |||
} | |||
if !p.Equal(msg) { | |||
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p) | |||
} | |||
if len(littlefuzz) > 0 { | |||
fuzzamount := 100 | |||
for i := 0; i < fuzzamount; i++ { | |||
littlefuzz[popr.Intn(len(littlefuzz))] = byte(popr.Intn(256)) | |||
littlefuzz = append(littlefuzz, byte(popr.Intn(256))) | |||
} | |||
// shouldn't panic | |||
_ = proto.Unmarshal(littlefuzz, msg) | |||
} | |||
} | |||
func TestRequestPingMarshalTo(t *testing.T) { | |||
seed := time.Now().UnixNano() | |||
popr := rand.New(rand.NewSource(seed)) | |||
p := NewPopulatedRequestPing(popr, false) | |||
size := p.Size() | |||
dAtA := make([]byte, size) | |||
for i := range dAtA { | |||
dAtA[i] = byte(popr.Intn(256)) | |||
} | |||
_, err := p.MarshalTo(dAtA) | |||
if err != nil { | |||
t.Fatalf("seed = %d, err = %v", seed, err) | |||
} | |||
msg := &RequestPing{} | |||
if err := proto.Unmarshal(dAtA, msg); err != nil { | |||
t.Fatalf("seed = %d, err = %v", seed, err) | |||
} | |||
for i := range dAtA { | |||
dAtA[i] = byte(popr.Intn(256)) | |||
} | |||
if !p.Equal(msg) { | |||
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p) | |||
} | |||
} | |||
func TestRequestBroadcastTxProto(t *testing.T) { | |||
seed := time.Now().UnixNano() | |||
popr := rand.New(rand.NewSource(seed)) | |||
p := NewPopulatedRequestBroadcastTx(popr, false) | |||
dAtA, err := proto.Marshal(p) | |||
if err != nil { | |||
t.Fatalf("seed = %d, err = %v", seed, err) | |||
} | |||
msg := &RequestBroadcastTx{} | |||
if err := proto.Unmarshal(dAtA, msg); err != nil { | |||
t.Fatalf("seed = %d, err = %v", seed, err) | |||
} | |||
littlefuzz := make([]byte, len(dAtA)) | |||
copy(littlefuzz, dAtA) | |||
for i := range dAtA { | |||
dAtA[i] = byte(popr.Intn(256)) | |||
} | |||
if !p.Equal(msg) { | |||
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p) | |||
} | |||
if len(littlefuzz) > 0 { | |||
fuzzamount := 100 | |||
for i := 0; i < fuzzamount; i++ { | |||
littlefuzz[popr.Intn(len(littlefuzz))] = byte(popr.Intn(256)) | |||
littlefuzz = append(littlefuzz, byte(popr.Intn(256))) | |||
} | |||
// shouldn't panic | |||
_ = proto.Unmarshal(littlefuzz, msg) | |||
} | |||
} | |||
func TestRequestBroadcastTxMarshalTo(t *testing.T) { | |||
seed := time.Now().UnixNano() | |||
popr := rand.New(rand.NewSource(seed)) | |||
p := NewPopulatedRequestBroadcastTx(popr, false) | |||
size := p.Size() | |||
dAtA := make([]byte, size) | |||
for i := range dAtA { | |||
dAtA[i] = byte(popr.Intn(256)) | |||
} | |||
_, err := p.MarshalTo(dAtA) | |||
if err != nil { | |||
t.Fatalf("seed = %d, err = %v", seed, err) | |||
} | |||
msg := &RequestBroadcastTx{} | |||
if err := proto.Unmarshal(dAtA, msg); err != nil { | |||
t.Fatalf("seed = %d, err = %v", seed, err) | |||
} | |||
for i := range dAtA { | |||
dAtA[i] = byte(popr.Intn(256)) | |||
} | |||
if !p.Equal(msg) { | |||
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p) | |||
} | |||
} | |||
func TestResponsePingProto(t *testing.T) { | |||
seed := time.Now().UnixNano() | |||
popr := rand.New(rand.NewSource(seed)) | |||
p := NewPopulatedResponsePing(popr, false) | |||
dAtA, err := proto.Marshal(p) | |||
if err != nil { | |||
t.Fatalf("seed = %d, err = %v", seed, err) | |||
} | |||
msg := &ResponsePing{} | |||
if err := proto.Unmarshal(dAtA, msg); err != nil { | |||
t.Fatalf("seed = %d, err = %v", seed, err) | |||
} | |||
littlefuzz := make([]byte, len(dAtA)) | |||
copy(littlefuzz, dAtA) | |||
for i := range dAtA { | |||
dAtA[i] = byte(popr.Intn(256)) | |||
} | |||
if !p.Equal(msg) { | |||
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p) | |||
} | |||
if len(littlefuzz) > 0 { | |||
fuzzamount := 100 | |||
for i := 0; i < fuzzamount; i++ { | |||
littlefuzz[popr.Intn(len(littlefuzz))] = byte(popr.Intn(256)) | |||
littlefuzz = append(littlefuzz, byte(popr.Intn(256))) | |||
} | |||
// shouldn't panic | |||
_ = proto.Unmarshal(littlefuzz, msg) | |||
} | |||
} | |||
func TestResponsePingMarshalTo(t *testing.T) { | |||
seed := time.Now().UnixNano() | |||
popr := rand.New(rand.NewSource(seed)) | |||
p := NewPopulatedResponsePing(popr, false) | |||
size := p.Size() | |||
dAtA := make([]byte, size) | |||
for i := range dAtA { | |||
dAtA[i] = byte(popr.Intn(256)) | |||
} | |||
_, err := p.MarshalTo(dAtA) | |||
if err != nil { | |||
t.Fatalf("seed = %d, err = %v", seed, err) | |||
} | |||
msg := &ResponsePing{} | |||
if err := proto.Unmarshal(dAtA, msg); err != nil { | |||
t.Fatalf("seed = %d, err = %v", seed, err) | |||
} | |||
for i := range dAtA { | |||
dAtA[i] = byte(popr.Intn(256)) | |||
} | |||
if !p.Equal(msg) { | |||
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p) | |||
} | |||
} | |||
func TestResponseBroadcastTxProto(t *testing.T) { | |||
seed := time.Now().UnixNano() | |||
popr := rand.New(rand.NewSource(seed)) | |||
p := NewPopulatedResponseBroadcastTx(popr, false) | |||
dAtA, err := proto.Marshal(p) | |||
if err != nil { | |||
t.Fatalf("seed = %d, err = %v", seed, err) | |||
} | |||
msg := &ResponseBroadcastTx{} | |||
if err := proto.Unmarshal(dAtA, msg); err != nil { | |||
t.Fatalf("seed = %d, err = %v", seed, err) | |||
} | |||
littlefuzz := make([]byte, len(dAtA)) | |||
copy(littlefuzz, dAtA) | |||
for i := range dAtA { | |||
dAtA[i] = byte(popr.Intn(256)) | |||
} | |||
if !p.Equal(msg) { | |||
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p) | |||
} | |||
if len(littlefuzz) > 0 { | |||
fuzzamount := 100 | |||
for i := 0; i < fuzzamount; i++ { | |||
littlefuzz[popr.Intn(len(littlefuzz))] = byte(popr.Intn(256)) | |||
littlefuzz = append(littlefuzz, byte(popr.Intn(256))) | |||
} | |||
// shouldn't panic | |||
_ = proto.Unmarshal(littlefuzz, msg) | |||
} | |||
} | |||
func TestResponseBroadcastTxMarshalTo(t *testing.T) { | |||
seed := time.Now().UnixNano() | |||
popr := rand.New(rand.NewSource(seed)) | |||
p := NewPopulatedResponseBroadcastTx(popr, false) | |||
size := p.Size() | |||
dAtA := make([]byte, size) | |||
for i := range dAtA { | |||
dAtA[i] = byte(popr.Intn(256)) | |||
} | |||
_, err := p.MarshalTo(dAtA) | |||
if err != nil { | |||
t.Fatalf("seed = %d, err = %v", seed, err) | |||
} | |||
msg := &ResponseBroadcastTx{} | |||
if err := proto.Unmarshal(dAtA, msg); err != nil { | |||
t.Fatalf("seed = %d, err = %v", seed, err) | |||
} | |||
for i := range dAtA { | |||
dAtA[i] = byte(popr.Intn(256)) | |||
} | |||
if !p.Equal(msg) { | |||
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p) | |||
} | |||
} | |||
func TestRequestPingJSON(t *testing.T) { | |||
seed := time.Now().UnixNano() | |||
popr := rand.New(rand.NewSource(seed)) | |||
p := NewPopulatedRequestPing(popr, true) | |||
marshaler := jsonpb.Marshaler{} | |||
jsondata, err := marshaler.MarshalToString(p) | |||
if err != nil { | |||
t.Fatalf("seed = %d, err = %v", seed, err) | |||
} | |||
msg := &RequestPing{} | |||
err = jsonpb.UnmarshalString(jsondata, msg) | |||
if err != nil { | |||
t.Fatalf("seed = %d, err = %v", seed, err) | |||
} | |||
if !p.Equal(msg) { | |||
t.Fatalf("seed = %d, %#v !Json Equal %#v", seed, msg, p) | |||
} | |||
} | |||
func TestRequestBroadcastTxJSON(t *testing.T) { | |||
seed := time.Now().UnixNano() | |||
popr := rand.New(rand.NewSource(seed)) | |||
p := NewPopulatedRequestBroadcastTx(popr, true) | |||
marshaler := jsonpb.Marshaler{} | |||
jsondata, err := marshaler.MarshalToString(p) | |||
if err != nil { | |||
t.Fatalf("seed = %d, err = %v", seed, err) | |||
} | |||
msg := &RequestBroadcastTx{} | |||
err = jsonpb.UnmarshalString(jsondata, msg) | |||
if err != nil { | |||
t.Fatalf("seed = %d, err = %v", seed, err) | |||
} | |||
if !p.Equal(msg) { | |||
t.Fatalf("seed = %d, %#v !Json Equal %#v", seed, msg, p) | |||
} | |||
} | |||
func TestResponsePingJSON(t *testing.T) { | |||
seed := time.Now().UnixNano() | |||
popr := rand.New(rand.NewSource(seed)) | |||
p := NewPopulatedResponsePing(popr, true) | |||
marshaler := jsonpb.Marshaler{} | |||
jsondata, err := marshaler.MarshalToString(p) | |||
if err != nil { | |||
t.Fatalf("seed = %d, err = %v", seed, err) | |||
} | |||
msg := &ResponsePing{} | |||
err = jsonpb.UnmarshalString(jsondata, msg) | |||
if err != nil { | |||
t.Fatalf("seed = %d, err = %v", seed, err) | |||
} | |||
if !p.Equal(msg) { | |||
t.Fatalf("seed = %d, %#v !Json Equal %#v", seed, msg, p) | |||
} | |||
} | |||
func TestResponseBroadcastTxJSON(t *testing.T) { | |||
seed := time.Now().UnixNano() | |||
popr := rand.New(rand.NewSource(seed)) | |||
p := NewPopulatedResponseBroadcastTx(popr, true) | |||
marshaler := jsonpb.Marshaler{} | |||
jsondata, err := marshaler.MarshalToString(p) | |||
if err != nil { | |||
t.Fatalf("seed = %d, err = %v", seed, err) | |||
} | |||
msg := &ResponseBroadcastTx{} | |||
err = jsonpb.UnmarshalString(jsondata, msg) | |||
if err != nil { | |||
t.Fatalf("seed = %d, err = %v", seed, err) | |||
} | |||
if !p.Equal(msg) { | |||
t.Fatalf("seed = %d, %#v !Json Equal %#v", seed, msg, p) | |||
} | |||
} | |||
func TestRequestPingProtoText(t *testing.T) { | |||
seed := time.Now().UnixNano() | |||
popr := rand.New(rand.NewSource(seed)) | |||
p := NewPopulatedRequestPing(popr, true) | |||
dAtA := proto.MarshalTextString(p) | |||
msg := &RequestPing{} | |||
if err := proto.UnmarshalText(dAtA, msg); err != nil { | |||
t.Fatalf("seed = %d, err = %v", seed, err) | |||
} | |||
if !p.Equal(msg) { | |||
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p) | |||
} | |||
} | |||
func TestRequestPingProtoCompactText(t *testing.T) { | |||
seed := time.Now().UnixNano() | |||
popr := rand.New(rand.NewSource(seed)) | |||
p := NewPopulatedRequestPing(popr, true) | |||
dAtA := proto.CompactTextString(p) | |||
msg := &RequestPing{} | |||
if err := proto.UnmarshalText(dAtA, msg); err != nil { | |||
t.Fatalf("seed = %d, err = %v", seed, err) | |||
} | |||
if !p.Equal(msg) { | |||
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p) | |||
} | |||
} | |||
func TestRequestBroadcastTxProtoText(t *testing.T) { | |||
seed := time.Now().UnixNano() | |||
popr := rand.New(rand.NewSource(seed)) | |||
p := NewPopulatedRequestBroadcastTx(popr, true) | |||
dAtA := proto.MarshalTextString(p) | |||
msg := &RequestBroadcastTx{} | |||
if err := proto.UnmarshalText(dAtA, msg); err != nil { | |||
t.Fatalf("seed = %d, err = %v", seed, err) | |||
} | |||
if !p.Equal(msg) { | |||
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p) | |||
} | |||
} | |||
func TestRequestBroadcastTxProtoCompactText(t *testing.T) { | |||
seed := time.Now().UnixNano() | |||
popr := rand.New(rand.NewSource(seed)) | |||
p := NewPopulatedRequestBroadcastTx(popr, true) | |||
dAtA := proto.CompactTextString(p) | |||
msg := &RequestBroadcastTx{} | |||
if err := proto.UnmarshalText(dAtA, msg); err != nil { | |||
t.Fatalf("seed = %d, err = %v", seed, err) | |||
} | |||
if !p.Equal(msg) { | |||
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p) | |||
} | |||
} | |||
func TestResponsePingProtoText(t *testing.T) { | |||
seed := time.Now().UnixNano() | |||
popr := rand.New(rand.NewSource(seed)) | |||
p := NewPopulatedResponsePing(popr, true) | |||
dAtA := proto.MarshalTextString(p) | |||
msg := &ResponsePing{} | |||
if err := proto.UnmarshalText(dAtA, msg); err != nil { | |||
t.Fatalf("seed = %d, err = %v", seed, err) | |||
} | |||
if !p.Equal(msg) { | |||
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p) | |||
} | |||
} | |||
func TestResponsePingProtoCompactText(t *testing.T) { | |||
seed := time.Now().UnixNano() | |||
popr := rand.New(rand.NewSource(seed)) | |||
p := NewPopulatedResponsePing(popr, true) | |||
dAtA := proto.CompactTextString(p) | |||
msg := &ResponsePing{} | |||
if err := proto.UnmarshalText(dAtA, msg); err != nil { | |||
t.Fatalf("seed = %d, err = %v", seed, err) | |||
} | |||
if !p.Equal(msg) { | |||
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p) | |||
} | |||
} | |||
func TestResponseBroadcastTxProtoText(t *testing.T) { | |||
seed := time.Now().UnixNano() | |||
popr := rand.New(rand.NewSource(seed)) | |||
p := NewPopulatedResponseBroadcastTx(popr, true) | |||
dAtA := proto.MarshalTextString(p) | |||
msg := &ResponseBroadcastTx{} | |||
if err := proto.UnmarshalText(dAtA, msg); err != nil { | |||
t.Fatalf("seed = %d, err = %v", seed, err) | |||
} | |||
if !p.Equal(msg) { | |||
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p) | |||
} | |||
} | |||
func TestResponseBroadcastTxProtoCompactText(t *testing.T) { | |||
seed := time.Now().UnixNano() | |||
popr := rand.New(rand.NewSource(seed)) | |||
p := NewPopulatedResponseBroadcastTx(popr, true) | |||
dAtA := proto.CompactTextString(p) | |||
msg := &ResponseBroadcastTx{} | |||
if err := proto.UnmarshalText(dAtA, msg); err != nil { | |||
t.Fatalf("seed = %d, err = %v", seed, err) | |||
} | |||
if !p.Equal(msg) { | |||
t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p) | |||
} | |||
} | |||
func TestRequestPingSize(t *testing.T) { | |||
seed := time.Now().UnixNano() | |||
popr := rand.New(rand.NewSource(seed)) | |||
p := NewPopulatedRequestPing(popr, true) | |||
size2 := proto.Size(p) | |||
dAtA, err := proto.Marshal(p) | |||
if err != nil { | |||
t.Fatalf("seed = %d, err = %v", seed, err) | |||
} | |||
size := p.Size() | |||
if len(dAtA) != size { | |||
t.Errorf("seed = %d, size %v != marshalled size %v", seed, size, len(dAtA)) | |||
} | |||
if size2 != size { | |||
t.Errorf("seed = %d, size %v != before marshal proto.Size %v", seed, size, size2) | |||
} | |||
size3 := proto.Size(p) | |||
if size3 != size { | |||
t.Errorf("seed = %d, size %v != after marshal proto.Size %v", seed, size, size3) | |||
} | |||
} | |||
func TestRequestBroadcastTxSize(t *testing.T) { | |||
seed := time.Now().UnixNano() | |||
popr := rand.New(rand.NewSource(seed)) | |||
p := NewPopulatedRequestBroadcastTx(popr, true) | |||
size2 := proto.Size(p) | |||
dAtA, err := proto.Marshal(p) | |||
if err != nil { | |||
t.Fatalf("seed = %d, err = %v", seed, err) | |||
} | |||
size := p.Size() | |||
if len(dAtA) != size { | |||
t.Errorf("seed = %d, size %v != marshalled size %v", seed, size, len(dAtA)) | |||
} | |||
if size2 != size { | |||
t.Errorf("seed = %d, size %v != before marshal proto.Size %v", seed, size, size2) | |||
} | |||
size3 := proto.Size(p) | |||
if size3 != size { | |||
t.Errorf("seed = %d, size %v != after marshal proto.Size %v", seed, size, size3) | |||
} | |||
} | |||
func TestResponsePingSize(t *testing.T) { | |||
seed := time.Now().UnixNano() | |||
popr := rand.New(rand.NewSource(seed)) | |||
p := NewPopulatedResponsePing(popr, true) | |||
size2 := proto.Size(p) | |||
dAtA, err := proto.Marshal(p) | |||
if err != nil { | |||
t.Fatalf("seed = %d, err = %v", seed, err) | |||
} | |||
size := p.Size() | |||
if len(dAtA) != size { | |||
t.Errorf("seed = %d, size %v != marshalled size %v", seed, size, len(dAtA)) | |||
} | |||
if size2 != size { | |||
t.Errorf("seed = %d, size %v != before marshal proto.Size %v", seed, size, size2) | |||
} | |||
size3 := proto.Size(p) | |||
if size3 != size { | |||
t.Errorf("seed = %d, size %v != after marshal proto.Size %v", seed, size, size3) | |||
} | |||
} | |||
func TestResponseBroadcastTxSize(t *testing.T) { | |||
seed := time.Now().UnixNano() | |||
popr := rand.New(rand.NewSource(seed)) | |||
p := NewPopulatedResponseBroadcastTx(popr, true) | |||
size2 := proto.Size(p) | |||
dAtA, err := proto.Marshal(p) | |||
if err != nil { | |||
t.Fatalf("seed = %d, err = %v", seed, err) | |||
} | |||
size := p.Size() | |||
if len(dAtA) != size { | |||
t.Errorf("seed = %d, size %v != marshalled size %v", seed, size, len(dAtA)) | |||
} | |||
if size2 != size { | |||
t.Errorf("seed = %d, size %v != before marshal proto.Size %v", seed, size, size2) | |||
} | |||
size3 := proto.Size(p) | |||
if size3 != size { | |||
t.Errorf("seed = %d, size %v != after marshal proto.Size %v", seed, size, size3) | |||
} | |||
} | |||
//These tests are generated by github.com/gogo/protobuf/plugin/testgen |