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.

41 lines
1.1 KiB

  1. package encoding
  2. import (
  3. "fmt"
  4. "github.com/tendermint/tendermint/crypto"
  5. "github.com/tendermint/tendermint/crypto/ed25519"
  6. pc "github.com/tendermint/tendermint/proto/tendermint/crypto"
  7. )
  8. // PubKeyToProto takes crypto.PubKey and transforms it to a protobuf Pubkey
  9. func PubKeyToProto(k crypto.PubKey) (pc.PublicKey, error) {
  10. var kp pc.PublicKey
  11. switch k := k.(type) {
  12. case ed25519.PubKey:
  13. kp = pc.PublicKey{
  14. Sum: &pc.PublicKey_Ed25519{
  15. Ed25519: k,
  16. },
  17. }
  18. default:
  19. return kp, fmt.Errorf("toproto: key type %v is not supported", k)
  20. }
  21. return kp, nil
  22. }
  23. // PubKeyFromProto takes a protobuf Pubkey and transforms it to a crypto.Pubkey
  24. func PubKeyFromProto(k pc.PublicKey) (crypto.PubKey, error) {
  25. switch k := k.Sum.(type) {
  26. case *pc.PublicKey_Ed25519:
  27. if len(k.Ed25519) != ed25519.PubKeySize {
  28. return nil, fmt.Errorf("invalid size for PubKeyEd25519. Got %d, expected %d",
  29. len(k.Ed25519), ed25519.PubKeySize)
  30. }
  31. pk := make(ed25519.PubKey, ed25519.PubKeySize)
  32. copy(pk, k.Ed25519)
  33. return pk, nil
  34. default:
  35. return nil, fmt.Errorf("fromproto: key type %v is not supported", k)
  36. }
  37. }