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.

71 lines
2.5 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/multisig"
  8. "github.com/tendermint/tendermint/crypto/secp256k1"
  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.PubKeyEd25519{})] = 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.PubKeyEd25519{})] = ed25519.PubKeyAminoName
  27. nameTable[reflect.TypeOf(secp256k1.PubKeySecp256k1{})] = secp256k1.PubKeyAminoName
  28. nameTable[reflect.TypeOf(multisig.PubKeyMultisigThreshold{})] = multisig.PubKeyMultisigThresholdAminoRoute
  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.PubKeyEd25519{},
  42. ed25519.PubKeyAminoName, nil)
  43. cdc.RegisterConcrete(secp256k1.PubKeySecp256k1{},
  44. secp256k1.PubKeyAminoName, nil)
  45. cdc.RegisterConcrete(multisig.PubKeyMultisigThreshold{},
  46. multisig.PubKeyMultisigThresholdAminoRoute, nil)
  47. cdc.RegisterInterface((*crypto.PrivKey)(nil), nil)
  48. cdc.RegisterConcrete(ed25519.PrivKeyEd25519{},
  49. ed25519.PrivKeyAminoName, nil)
  50. cdc.RegisterConcrete(secp256k1.PrivKeySecp256k1{},
  51. secp256k1.PrivKeyAminoName, nil)
  52. }
  53. func PrivKeyFromBytes(privKeyBytes []byte) (privKey crypto.PrivKey, err error) {
  54. err = cdc.UnmarshalBinaryBare(privKeyBytes, &privKey)
  55. return
  56. }
  57. func PubKeyFromBytes(pubKeyBytes []byte) (pubKey crypto.PubKey, err error) {
  58. err = cdc.UnmarshalBinaryBare(pubKeyBytes, &pubKey)
  59. return
  60. }