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.

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