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.

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