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.

99 lines
2.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 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 := 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. func TestRealLedgerEd25519(t *testing.T) {
  38. if os.Getenv("WITH_LEDGER") == "" {
  39. t.Skip("Set WITH_LEDGER to run code on real ledger")
  40. }
  41. msg := []byte("kuhehfeohg")
  42. path := DerivationPath{44, 60, 0, 0, 0}
  43. priv, err := NewPrivKeyLedgerEd25519(path)
  44. require.Nil(t, err, "%+v", err)
  45. pub := priv.PubKey()
  46. sig := priv.Sign(msg)
  47. valid := pub.VerifyBytes(msg, sig)
  48. assert.True(t, valid)
  49. // now, let's serialize the key and make sure it still works
  50. bs := priv.Bytes()
  51. priv2, err := PrivKeyFromBytes(bs)
  52. require.Nil(t, err, "%+v", err)
  53. // make sure we get the same pubkey when we load from disk
  54. pub2 := priv2.PubKey()
  55. require.Equal(t, pub, pub2)
  56. // signing with the loaded key should match the original pubkey
  57. sig = priv2.Sign(msg)
  58. valid = pub.VerifyBytes(msg, sig)
  59. assert.True(t, valid)
  60. // make sure pubkeys serialize properly as well
  61. bs = pub.Bytes()
  62. bpub, err := PubKeyFromBytes(bs)
  63. require.NoError(t, err)
  64. assert.Equal(t, pub, bpub)
  65. }
  66. // TestRealLedgerErrorHandling calls. These tests assume
  67. // the ledger is not plugged in....
  68. func TestRealLedgerErrorHandling(t *testing.T) {
  69. if os.Getenv("WITH_LEDGER") != "" {
  70. t.Skip("Skipping on WITH_LEDGER as it tests unplugged cases")
  71. }
  72. // first, try to generate a key, must return an error
  73. // (no panic)
  74. path := DerivationPath{44, 60, 0, 0, 0}
  75. _, err := NewPrivKeyLedgerSecp256k1(path)
  76. require.Error(t, err)
  77. }