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.

124 lines
3.6 KiB

  1. package crypto
  2. import (
  3. "fmt"
  4. secp256k1 "github.com/btcsuite/btcd/btcec"
  5. ledger "github.com/zondax/ledger-goclient"
  6. )
  7. func pubkeyLedgerSecp256k1(device *ledger.Ledger, path DerivationPath) (pub PubKey, err error) {
  8. key, err := device.GetPublicKeySECP256K1(path)
  9. if err != nil {
  10. return nil, fmt.Errorf("error fetching public key: %v", err)
  11. }
  12. var p PubKeySecp256k1
  13. // Reserialize in the 33-byte compressed format
  14. cmp, err := secp256k1.ParsePubKey(key[:], secp256k1.S256())
  15. copy(p[:], cmp.SerializeCompressed())
  16. pub = p
  17. return
  18. }
  19. func signLedgerSecp256k1(device *ledger.Ledger, path DerivationPath, msg []byte) (sig Signature, err error) {
  20. bsig, err := device.SignSECP256K1(path, msg)
  21. if err != nil {
  22. return sig, err
  23. }
  24. sig = SignatureSecp256k1FromBytes(bsig)
  25. return
  26. }
  27. // PrivKeyLedgerSecp256k1 implements PrivKey, calling the ledger nano
  28. // we cache the PubKey from the first call to use it later
  29. type PrivKeyLedgerSecp256k1 struct {
  30. // PubKey should be private, but we want to encode it via go-amino
  31. // so we can view the address later, even without having the ledger
  32. // attached
  33. CachedPubKey PubKey
  34. Path DerivationPath
  35. }
  36. // NewPrivKeyLedgerSecp256k1 will generate a new key and store the
  37. // public key for later use.
  38. func NewPrivKeyLedgerSecp256k1(path DerivationPath) (PrivKey, error) {
  39. var pk PrivKeyLedgerSecp256k1
  40. pk.Path = path
  41. // cache the pubkey for later use
  42. pubKey, err := pk.getPubKey()
  43. if err != nil {
  44. return nil, err
  45. }
  46. pk.CachedPubKey = pubKey
  47. return &pk, err
  48. }
  49. // ValidateKey allows us to verify the sanity of a key
  50. // after loading it from disk
  51. func (pk PrivKeyLedgerSecp256k1) ValidateKey() error {
  52. // getPubKey will return an error if the ledger is not
  53. pub, err := pk.getPubKey()
  54. if err != nil {
  55. return err
  56. }
  57. // verify this matches cached address
  58. if !pub.Equals(pk.CachedPubKey) {
  59. return fmt.Errorf("cached key does not match retrieved key")
  60. }
  61. return nil
  62. }
  63. // AssertIsPrivKeyInner fulfils PrivKey Interface
  64. func (pk *PrivKeyLedgerSecp256k1) AssertIsPrivKeyInner() {}
  65. // Bytes fulfils PrivKey Interface - but it stores the cached pubkey so we can verify
  66. // the same key when we reconnect to a ledger
  67. func (pk PrivKeyLedgerSecp256k1) Bytes() []byte {
  68. return cdc.MustMarshalBinaryBare(pk)
  69. }
  70. // Sign calls the ledger and stores the PubKey for future use
  71. //
  72. // Communication is checked on NewPrivKeyLedger and PrivKeyFromBytes,
  73. // returning an error, so this should only trigger if the privkey is held
  74. // in memory for a while before use.
  75. func (pk PrivKeyLedgerSecp256k1) Sign(msg []byte) (Signature, error) {
  76. dev, err := getLedger()
  77. if err != nil {
  78. return nil, err
  79. }
  80. sig, err := signLedgerSecp256k1(dev, pk.Path, msg)
  81. if err != nil {
  82. return nil, err
  83. }
  84. return sig, nil
  85. }
  86. // PubKey returns the stored PubKey
  87. func (pk PrivKeyLedgerSecp256k1) PubKey() PubKey {
  88. return pk.CachedPubKey
  89. }
  90. // getPubKey reads the pubkey the ledger itself
  91. // since this involves IO, it may return an error, which is not exposed
  92. // in the PubKey interface, so this function allows better error handling
  93. func (pk PrivKeyLedgerSecp256k1) getPubKey() (key PubKey, err error) {
  94. dev, err := getLedger()
  95. if err != nil {
  96. return key, fmt.Errorf("cannot connect to Ledger device - error: %v", err)
  97. }
  98. key, err = pubkeyLedgerSecp256k1(dev, pk.Path)
  99. if err != nil {
  100. return key, fmt.Errorf("please open Cosmos app on the Ledger device - error: %v", err)
  101. }
  102. return key, err
  103. }
  104. // Equals fulfils PrivKey Interface - makes sure both keys refer to the
  105. // same
  106. func (pk PrivKeyLedgerSecp256k1) Equals(other PrivKey) bool {
  107. if ledger, ok := other.(*PrivKeyLedgerSecp256k1); ok {
  108. return pk.CachedPubKey.Equals(ledger.CachedPubKey)
  109. }
  110. return false
  111. }