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.

61 lines
1.5 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. path := DerivationPath{44, 60, 0, 0, 0}
  14. priv, err := NewPrivKeyLedgerSecp256k1(path)
  15. require.Nil(t, err, "%+v", err)
  16. pub := priv.PubKey()
  17. sig := priv.Sign(msg)
  18. valid := pub.VerifyBytes(msg, sig)
  19. assert.True(t, valid)
  20. // now, let's serialize the key and make sure it still works
  21. bs := priv.Bytes()
  22. priv2, err := PrivKeyFromBytes(bs)
  23. require.Nil(t, err, "%+v", err)
  24. // make sure we get the same pubkey when we load from disk
  25. pub2 := priv2.PubKey()
  26. require.Equal(t, pub, pub2)
  27. // signing with the loaded key should match the original pubkey
  28. sig = priv2.Sign(msg)
  29. valid = pub.VerifyBytes(msg, sig)
  30. assert.True(t, valid)
  31. // make sure pubkeys serialize properly as well
  32. bs = pub.Bytes()
  33. bpub, err := PubKeyFromBytes(bs)
  34. require.NoError(t, err)
  35. assert.Equal(t, pub, bpub)
  36. }
  37. // TestRealLedgerErrorHandling calls. These tests assume
  38. // the ledger is not plugged in....
  39. func TestRealLedgerErrorHandling(t *testing.T) {
  40. if os.Getenv("WITH_LEDGER") != "" {
  41. t.Skip("Skipping on WITH_LEDGER as it tests unplugged cases")
  42. }
  43. // first, try to generate a key, must return an error
  44. // (no panic)
  45. path := DerivationPath{44, 60, 0, 0, 0}
  46. _, err := NewPrivKeyLedgerSecp256k1(path)
  47. require.Error(t, err)
  48. }