You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.4 KiB

  1. package crypto
  2. import (
  3. "os"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/stretchr/testify/require"
  7. )
  8. func TestRealLedger(t *testing.T) {
  9. if os.Getenv("WITH_LEDGER") == "" {
  10. t.Skip("Set WITH_LEDGER to run code on real ledger")
  11. }
  12. msg := []byte("kuhehfeohg")
  13. priv, err := NewPrivKeyLedgerSecp256k1()
  14. require.Nil(t, err, "%+v", err)
  15. pub := priv.PubKey()
  16. sig := priv.Sign(msg)
  17. valid := pub.VerifyBytes(msg, sig)
  18. assert.True(t, valid)
  19. // now, let's serialize the key and make sure it still works
  20. bs := priv.Bytes()
  21. priv2, err := PrivKeyFromBytes(bs)
  22. require.Nil(t, err, "%+v", err)
  23. // make sure we get the same pubkey when we load from disk
  24. pub2 := priv2.PubKey()
  25. require.Equal(t, pub, pub2)
  26. // signing with the loaded key should match the original pubkey
  27. sig = priv2.Sign(msg)
  28. valid = pub.VerifyBytes(msg, sig)
  29. assert.True(t, valid)
  30. // make sure pubkeys serialize properly as well
  31. bs = pub.Bytes()
  32. bpub, err := PubKeyFromBytes(bs)
  33. require.NoError(t, err)
  34. assert.Equal(t, pub, bpub)
  35. }
  36. // TestRealLedgerErrorHandling calls. These tests assume
  37. // the ledger is not plugged in....
  38. func TestRealLedgerErrorHandling(t *testing.T) {
  39. if os.Getenv("WITH_LEDGER") != "" {
  40. t.Skip("Skipping on WITH_LEDGER as it tests unplugged cases")
  41. }
  42. // first, try to generate a key, must return an error
  43. // (no panic)
  44. _, err := NewPrivKeyLedgerSecp256k1()
  45. require.Error(t, err)
  46. }