2018-06-28 17:54:47 -07:00
|
|
|
package crypto
|
|
|
|
|
|
|
|
import (
|
2018-07-13 19:17:53 -07:00
|
|
|
"fmt"
|
2018-06-28 17:54:47 -07:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2018-07-25 13:43:37 -07:00
|
|
|
"github.com/tendermint/tendermint/crypto/encoding/amino"
|
2018-06-28 17:54:47 -07:00
|
|
|
)
|
|
|
|
|
2018-07-13 19:17:53 -07:00
|
|
|
var ledgerEnabledEnv = "TEST_WITH_LEDGER"
|
2018-06-28 17:54:47 -07:00
|
|
|
|
2018-07-13 19:17:53 -07:00
|
|
|
func TestRealLedgerSecp256k1(t *testing.T) {
|
|
|
|
if os.Getenv(ledgerEnabledEnv) == "" {
|
|
|
|
t.Skip(fmt.Sprintf("Set '%s' to run code on a real ledger", ledgerEnabledEnv))
|
2018-06-28 17:54:47 -07:00
|
|
|
}
|
2018-07-13 19:17:53 -07:00
|
|
|
msg := []byte("{\"account_number\":\"3\",\"chain_id\":\"1234\",\"fee\":{\"amount\":[{\"amount\":\"150\",\"denom\":\"atom\"}],\"gas\":\"5000\"},\"memo\":\"memo\",\"msgs\":[[\"%s\"]],\"sequence\":\"6\"}")
|
2018-06-28 17:54:47 -07:00
|
|
|
path := DerivationPath{44, 60, 0, 0, 0}
|
|
|
|
|
|
|
|
priv, err := NewPrivKeyLedgerSecp256k1(path)
|
2018-07-13 19:17:53 -07:00
|
|
|
require.Nil(t, err, "%s", err)
|
|
|
|
|
2018-06-28 17:54:47 -07:00
|
|
|
pub := priv.PubKey()
|
|
|
|
sig, err := priv.Sign(msg)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
valid := pub.VerifyBytes(msg, sig)
|
2018-06-29 18:10:15 -07:00
|
|
|
require.True(t, valid)
|
2018-06-28 17:54:47 -07:00
|
|
|
|
2018-07-13 19:17:53 -07:00
|
|
|
// now, let's serialize the public key and make sure it still works
|
|
|
|
bs := priv.PubKey().Bytes()
|
2018-07-25 13:43:37 -07:00
|
|
|
pub2, err := cryptoAmino.PubKeyFromBytes(bs)
|
2018-06-28 17:54:47 -07:00
|
|
|
require.Nil(t, err, "%+v", err)
|
|
|
|
|
|
|
|
// make sure we get the same pubkey when we load from disk
|
|
|
|
require.Equal(t, pub, pub2)
|
|
|
|
|
|
|
|
// signing with the loaded key should match the original pubkey
|
2018-07-13 19:17:53 -07:00
|
|
|
sig, err = priv.Sign(msg)
|
2018-06-28 17:54:47 -07:00
|
|
|
require.Nil(t, err)
|
|
|
|
valid = pub.VerifyBytes(msg, sig)
|
2018-06-29 18:10:15 -07:00
|
|
|
require.True(t, valid)
|
2018-06-28 17:54:47 -07:00
|
|
|
|
|
|
|
// make sure pubkeys serialize properly as well
|
|
|
|
bs = pub.Bytes()
|
2018-07-25 13:43:37 -07:00
|
|
|
bpub, err := cryptoAmino.PubKeyFromBytes(bs)
|
2018-06-28 17:54:47 -07:00
|
|
|
require.NoError(t, err)
|
2018-06-29 18:10:15 -07:00
|
|
|
require.Equal(t, pub, bpub)
|
2018-06-28 17:54:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestRealLedgerErrorHandling calls. These tests assume
|
|
|
|
// the ledger is not plugged in....
|
|
|
|
func TestRealLedgerErrorHandling(t *testing.T) {
|
2018-07-13 19:17:53 -07:00
|
|
|
if os.Getenv(ledgerEnabledEnv) != "" {
|
|
|
|
t.Skip(fmt.Sprintf("Unset '%s' to run code as if without a real Ledger", ledgerEnabledEnv))
|
2018-06-28 17:54:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// first, try to generate a key, must return an error
|
|
|
|
// (no panic)
|
|
|
|
path := DerivationPath{44, 60, 0, 0, 0}
|
|
|
|
_, err := NewPrivKeyLedgerSecp256k1(path)
|
|
|
|
require.Error(t, err)
|
|
|
|
}
|