Browse Source

Update README with examples; Add signature test

pull/1782/head
Jae Kwon 7 years ago
parent
commit
7fb3f704b3
2 changed files with 29 additions and 4 deletions
  1. +15
    -0
      README.md
  2. +14
    -4
      encode_test.go

+ 15
- 0
README.md View File

@ -4,3 +4,18 @@ go-crypto is the cryptographic package adapted for Tendermint's uses
## Importing it ## Importing it
`import "github.com/tendermint/go-crypto"` `import "github.com/tendermint/go-crypto"`
## Binary encoding
go-crypto `.Bytes()` uses Amino:binary encoding, but Amino:JSON is also supported.
```go
Example Amino:JSON encodings:
crypto.PrivKeyEd25519 - {"type":"954568A3288910","value":"EVkqJO/jIXp3rkASXfh9YnyToYXRXhBr6g9cQVxPFnQBP/5povV4HTjvsy530kybxKHwEi85iU8YL0qQhSYVoQ=="}
crypto.SignatureEd25519 - {"type":"6BF5903DA1DB28","value":"77sQNZOrf7ltExpf7AV1WaYPCHbyRLgjBsoWVzcduuLk+jIGmYk+s5R6Emm29p12HeiNAuhUJgdFGmwkpeGJCA=="}
crypto.PubKeyEd25519 - {"type":"AC26791624DE60","value":"AT/+aaL1eB0477Mud9JMm8Sh8BIvOYlPGC9KkIUmFaE="}
crypto.PrivKeySecp256k1 - {"type":"019E82E1B0F798","value":"zx4Pnh67N+g2V+5vZbQzEyRerX9c4ccNZOVzM9RvJ0Y="}
crypto.SignatureSecp256k1 - {"type":"6D1EA416E1FEE8","value":"MEUCIQCIg5TqS1l7I+MKTrSPIuUN2+4m5tA29dcauqn3NhEJ2wIgICaZ+lgRc5aOTVahU/XoLopXKn8BZcl0bnuYWLvohR8="}
crypto.PubKeySecp256k1 - {"type":"F8CCEAEB5AE980","value":"A8lPKJXcNl5VHt1FK8a244K9EJuS4WX1hFBnwisi0IJx"}
```

+ 14
- 4
encode_test.go View File

@ -18,7 +18,9 @@ func checkAminoBinary(t *testing.T, src byter, dst interface{}, size int) {
// Make sure this is compatible with current (Bytes()) encoding. // Make sure this is compatible with current (Bytes()) encoding.
assert.Equal(t, src.Bytes(), bz, "Amino binary vs Bytes() mismatch") assert.Equal(t, src.Bytes(), bz, "Amino binary vs Bytes() mismatch")
// Make sure we have the expected length. // Make sure we have the expected length.
assert.Equal(t, size, len(bz), "Amino binary size mismatch")
if size != -1 {
assert.Equal(t, size, len(bz), "Amino binary size mismatch")
}
// Unmarshal. // Unmarshal.
err = cdc.UnmarshalBinaryBare(bz, dst) err = cdc.UnmarshalBinaryBare(bz, dst)
require.Nil(t, err, "%+v", err) require.Nil(t, err, "%+v", err)
@ -41,9 +43,8 @@ func checkAminoJSON(t *testing.T, src interface{}, dst interface{}, isNil bool)
func TestKeyEncodings(t *testing.T) { func TestKeyEncodings(t *testing.T) {
cases := []struct { cases := []struct {
privKey PrivKey
// 1 (type byte) + size of byte array
privSize, pubSize int
privKey PrivKey
privSize, pubSize int // binary sizes
}{ }{
{ {
privKey: GenPrivKeyEd25519(), privKey: GenPrivKeyEd25519(),
@ -58,6 +59,7 @@ func TestKeyEncodings(t *testing.T) {
} }
for _, tc := range cases { for _, tc := range cases {
// Check (de/en)codings of PrivKeys. // Check (de/en)codings of PrivKeys.
var priv2, priv3 PrivKey var priv2, priv3 PrivKey
checkAminoBinary(t, tc.privKey, &priv2, tc.privSize) checkAminoBinary(t, tc.privKey, &priv2, tc.privSize)
@ -65,6 +67,14 @@ func TestKeyEncodings(t *testing.T) {
checkAminoJSON(t, tc.privKey, &priv3, false) // TODO also check Prefix bytes. checkAminoJSON(t, tc.privKey, &priv3, false) // TODO also check Prefix bytes.
assert.EqualValues(t, tc.privKey, priv3) assert.EqualValues(t, tc.privKey, priv3)
// Check (de/en)codings of Signatures.
var sig1, sig2, sig3 Signature
sig1 = tc.privKey.Sign([]byte("something"))
checkAminoBinary(t, sig1, &sig2, -1) // Siganture size changes for Secp anyways.
assert.EqualValues(t, sig1, sig2)
checkAminoJSON(t, sig1, &sig3, false) // TODO also check Prefix bytes.
assert.EqualValues(t, sig1, sig3)
// Check (de/en)codings of PubKeys. // Check (de/en)codings of PubKeys.
pubKey := tc.privKey.PubKey() pubKey := tc.privKey.PubKey()
var pub2, pub3 PubKey var pub2, pub3 PubKey


Loading…
Cancel
Save