Browse Source

Add tests for pub/priv key validation

pull/1782/head
Ethan Frey 7 years ago
parent
commit
7c5a10a7d4
3 changed files with 40 additions and 2 deletions
  1. +38
    -0
      nano/keys_test.go
  2. +1
    -1
      nano/sign.go
  3. +1
    -1
      nano/sign_test.go

+ 38
- 0
nano/keys_test.go View File

@ -0,0 +1,38 @@
package nano
import (
"encoding/hex"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestLedgerKeys(t *testing.T) {
assert, require := assert.New(t), require.New(t)
// cryptoCase from sign_test
cases := []struct {
msg, pubkey, sig string
valid bool
}{
{
msg: "F00D",
pubkey: "8E8754F012C2FDB492183D41437FD837CB81D8BBE731924E2E0DAF43FD3F2C93",
sig: "787DC03E9E4EE05983E30BAE0DEFB8DB0671DBC2F5874AC93F8D8CA4018F7A42D6F9A9BCEADB422AC8E27CEE9CA205A0B88D22CD686F0A43EB806E8190A3C400",
valid: true,
},
}
for i, tc := range cases {
bmsg, err := hex.DecodeString(tc.msg)
require.NoError(err, "%d", i)
priv := NewMockKey(tc.msg, tc.pubkey, tc.sig)
pub := priv.PubKey()
sig := priv.Sign(bmsg)
valid := pub.VerifyBytes(bmsg, sig)
assert.Equal(tc.valid, valid, "%d", i)
}
}

+ 1
- 1
nano/sign.go View File

@ -61,7 +61,7 @@ func parseDigest(resp []byte) (key, sig []byte, err error) {
return key, sig, nil
}
func parseKey(data []byte) (key crypto.PubKey, err error) {
func parseEdKey(data []byte) (key crypto.PubKey, err error) {
ed := crypto.PubKeyEd25519{}
if len(data) < len(ed) {
return key, errors.Errorf("Key length too short: %d", len(data))


+ 1
- 1
nano/sign_test.go View File

@ -86,7 +86,7 @@ func TestCryptoConvert(t *testing.T) {
msg, key, sig, err := toBytes(tc)
require.Nil(err, "%d: %+v", i, err)
pk, err := parseKey(key)
pk, err := parseEdKey(key)
require.Nil(err, "%d: %+v", i, err)
psig, err := parseSig(sig)
require.Nil(err, "%d: %+v", i, err)


Loading…
Cancel
Save