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.

56 lines
1.3 KiB

  1. package cryptostore
  2. import (
  3. "github.com/pkg/errors"
  4. crypto "github.com/tendermint/go-crypto"
  5. )
  6. var (
  7. // GenEd25519 produces Ed25519 private keys
  8. GenEd25519 Generator = GenFunc(genEd25519)
  9. // GenSecp256k1 produces Secp256k1 private keys
  10. GenSecp256k1 Generator = GenFunc(genSecp256)
  11. )
  12. // Generator determines the type of private key the keystore creates
  13. type Generator interface {
  14. Generate(secret []byte) crypto.PrivKey
  15. }
  16. // GenFunc is a helper to transform a function into a Generator
  17. type GenFunc func(secret []byte) crypto.PrivKey
  18. func (f GenFunc) Generate(secret []byte) crypto.PrivKey {
  19. return f(secret)
  20. }
  21. func genEd25519(secret []byte) crypto.PrivKey {
  22. return crypto.GenPrivKeyEd25519FromSecret(secret).Wrap()
  23. }
  24. func genSecp256(secret []byte) crypto.PrivKey {
  25. return crypto.GenPrivKeySecp256k1FromSecret(secret).Wrap()
  26. }
  27. func getGenerator(algo string) (Generator, error) {
  28. switch algo {
  29. case crypto.NameEd25519:
  30. return GenEd25519, nil
  31. case crypto.NameSecp256k1:
  32. return GenSecp256k1, nil
  33. default:
  34. return nil, errors.Errorf("Cannot generate keys for algorithm: %s", algo)
  35. }
  36. }
  37. func getGeneratorByType(typ byte) (Generator, error) {
  38. switch typ {
  39. case crypto.TypeEd25519:
  40. return GenEd25519, nil
  41. case crypto.TypeSecp256k1:
  42. return GenSecp256k1, nil
  43. default:
  44. return nil, errors.Errorf("Cannot generate keys for algorithm: %X", typ)
  45. }
  46. }