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.

74 lines
2.2 KiB

  1. package keys
  2. import (
  3. "sort"
  4. crypto "github.com/tendermint/go-crypto"
  5. data "github.com/tendermint/go-wire/data"
  6. )
  7. // Info is the public information about a key
  8. type Info struct {
  9. Name string `json:"name"`
  10. Address data.Bytes `json:"address"`
  11. PubKey crypto.PubKey `json:"pubkey"`
  12. }
  13. func (i *Info) Format() Info {
  14. if !i.PubKey.Empty() {
  15. i.Address = i.PubKey.Address()
  16. }
  17. return *i
  18. }
  19. // Infos is a wrapper to allows alphabetical sorting of the keys
  20. type Infos []Info
  21. func (k Infos) Len() int { return len(k) }
  22. func (k Infos) Less(i, j int) bool { return k[i].Name < k[j].Name }
  23. func (k Infos) Swap(i, j int) { k[i], k[j] = k[j], k[i] }
  24. func (k Infos) Sort() {
  25. if k != nil {
  26. sort.Sort(k)
  27. }
  28. }
  29. // Signable represents any transaction we wish to send to tendermint core
  30. // These methods allow us to sign arbitrary Tx with the KeyStore
  31. type Signable interface {
  32. // SignBytes is the immutable data, which needs to be signed
  33. SignBytes() []byte
  34. // Sign will add a signature and pubkey.
  35. //
  36. // Depending on the Signable, one may be able to call this multiple times for multisig
  37. // Returns error if called with invalid data or too many times
  38. Sign(pubkey crypto.PubKey, sig crypto.Signature) error
  39. // Signers will return the public key(s) that signed if the signature
  40. // is valid, or an error if there is any issue with the signature,
  41. // including if there are no signatures
  42. Signers() ([]crypto.PubKey, error)
  43. // TxBytes returns the transaction data as well as all signatures
  44. // It should return an error if Sign was never called
  45. TxBytes() ([]byte, error)
  46. }
  47. // Signer allows one to use a keystore to sign transactions
  48. type Signer interface {
  49. Sign(name, passphrase string, tx Signable) error
  50. }
  51. // Manager allows simple CRUD on a keystore, as an aid to signing
  52. type Manager interface {
  53. Signer
  54. // Create also returns a seed phrase for cold-storage
  55. Create(name, passphrase, algo string) (Info, string, error)
  56. // Recover takes a seedphrase and loads in the private key
  57. Recover(name, passphrase, seedphrase string) (Info, error)
  58. List() (Infos, error)
  59. Get(name string) (Info, error)
  60. Update(name, oldpass, newpass string) error
  61. Delete(name, passphrase string) error
  62. }