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.

82 lines
2.8 KiB

  1. package cryptoamino
  2. import (
  3. "reflect"
  4. amino "github.com/tendermint/go-amino"
  5. "github.com/tendermint/tendermint/crypto"
  6. "github.com/tendermint/tendermint/crypto/ed25519"
  7. "github.com/tendermint/tendermint/crypto/secp256k1"
  8. "github.com/tendermint/tendermint/crypto/sr25519"
  9. )
  10. var cdc = amino.NewCodec()
  11. // nameTable is used to map public key concrete types back
  12. // to their registered amino names. This should eventually be handled
  13. // by amino. Example usage:
  14. // nameTable[reflect.TypeOf(ed25519.PubKey{})] = ed25519.PubKeyAminoName
  15. var nameTable = make(map[reflect.Type]string, 3)
  16. func init() {
  17. // NOTE: It's important that there be no conflicts here,
  18. // as that would change the canonical representations,
  19. // and therefore change the address.
  20. // TODO: Remove above note when
  21. // https://github.com/tendermint/go-amino/issues/9
  22. // is resolved
  23. RegisterAmino(cdc)
  24. // TODO: Have amino provide a way to go from concrete struct to route directly.
  25. // Its currently a private API
  26. nameTable[reflect.TypeOf(ed25519.PubKey{})] = ed25519.PubKeyAminoName
  27. nameTable[reflect.TypeOf(sr25519.PubKey{})] = sr25519.PubKeyAminoName
  28. nameTable[reflect.TypeOf(secp256k1.PubKey{})] = secp256k1.PubKeyAminoName
  29. }
  30. // PubkeyAminoName returns the amino route of a pubkey
  31. // cdc is currently passed in, as eventually this will not be using
  32. // a package level codec.
  33. func PubkeyAminoName(cdc *amino.Codec, key crypto.PubKey) (string, bool) {
  34. route, found := nameTable[reflect.TypeOf(key)]
  35. return route, found
  36. }
  37. // RegisterAmino registers all crypto related types in the given (amino) codec.
  38. func RegisterAmino(cdc *amino.Codec) {
  39. // These are all written here instead of
  40. cdc.RegisterInterface((*crypto.PubKey)(nil), nil)
  41. cdc.RegisterConcrete(ed25519.PubKey{},
  42. ed25519.PubKeyAminoName, nil)
  43. cdc.RegisterConcrete(sr25519.PubKey{},
  44. sr25519.PubKeyAminoName, nil)
  45. cdc.RegisterConcrete(secp256k1.PubKey{},
  46. secp256k1.PubKeyAminoName, nil)
  47. cdc.RegisterInterface((*crypto.PrivKey)(nil), nil)
  48. cdc.RegisterConcrete(ed25519.PrivKey{},
  49. ed25519.PrivKeyAminoName, nil)
  50. cdc.RegisterConcrete(sr25519.PrivKey{},
  51. sr25519.PrivKeyAminoName, nil)
  52. cdc.RegisterConcrete(secp256k1.PrivKey{},
  53. secp256k1.PrivKeyAminoName, nil)
  54. }
  55. // RegisterKeyType registers an external key type to allow decoding it from bytes
  56. func RegisterKeyType(o interface{}, name string) {
  57. cdc.RegisterConcrete(o, name, nil)
  58. nameTable[reflect.TypeOf(o)] = name
  59. }
  60. // PrivKeyFromBytes unmarshals private key bytes and returns a PrivKey
  61. func PrivKeyFromBytes(privKeyBytes []byte) (privKey crypto.PrivKey, err error) {
  62. err = cdc.UnmarshalBinaryBare(privKeyBytes, &privKey)
  63. return
  64. }
  65. // PubKeyFromBytes unmarshals public key bytes and returns a PubKey
  66. func PubKeyFromBytes(pubKeyBytes []byte) (pubKey crypto.PubKey, err error) {
  67. err = cdc.UnmarshalBinaryBare(pubKeyBytes, &pubKey)
  68. return
  69. }