/key/src/error.rs

https://github.com/CodeChain-io/codechain · Rust · 95 lines · 74 code · 6 blank · 15 comment · 6 complexity · 89edbcf1baa4750265e3206a31ff4256 MD5 · raw file

  1. // Copyright 2018 Kodebox, Inc.
  2. // This file is part of CodeChain.
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as
  6. // published by the Free Software Foundation, either version 3 of the
  7. // License, or (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. use crate::NetworkId;
  17. use bech32::Error as Bech32Error;
  18. use secp256k1::Error as SecpError;
  19. use std::fmt;
  20. #[derive(Debug, PartialEq)]
  21. pub enum Error {
  22. InvalidPublic,
  23. InvalidSecret,
  24. InvalidMessage,
  25. InvalidSignature,
  26. InvalidNetworkId(NetworkId),
  27. InvalidPlatformAddressVersion(u8),
  28. InvalidChecksum,
  29. InvalidPrivate,
  30. InvalidAddress,
  31. FailedKeyGeneration,
  32. Bech32MissingSeparator,
  33. Bech32InvalidChecksum,
  34. Bech32InvalidLength,
  35. Bech32InvalidChar(u8),
  36. Bech32InvalidData(u8),
  37. Bech32MixedCase,
  38. Bech32UnknownHRP,
  39. Custom(String),
  40. }
  41. impl fmt::Display for Error {
  42. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  43. let msg = match *self {
  44. Error::InvalidPublic => "Invalid Public".into(),
  45. Error::InvalidSecret => "Invalid Secret".into(),
  46. Error::InvalidMessage => "Invalid Message".into(),
  47. Error::InvalidSignature => "Invalid Signature".into(),
  48. Error::InvalidNetworkId(network_id) => format!("{} is an invalid network id", network_id),
  49. Error::InvalidPlatformAddressVersion(version) => {
  50. format!("{} is an invalid platform address version", version)
  51. }
  52. Error::InvalidChecksum => "Invalid Checksum".into(),
  53. Error::InvalidPrivate => "Invalid Private".into(),
  54. Error::InvalidAddress => "Invalid Address".into(),
  55. Error::FailedKeyGeneration => "Key generation failed".into(),
  56. Error::Bech32MissingSeparator => "Missing human-readable separator".into(),
  57. Error::Bech32InvalidChecksum => "Invalid checksum".into(),
  58. Error::Bech32InvalidLength => "Invalid Length".into(),
  59. Error::Bech32InvalidChar(_) => "Invalid character".into(),
  60. Error::Bech32InvalidData(_) => "Invalid data point".into(),
  61. Error::Bech32MixedCase => "Mixed-case strings not allowed".into(),
  62. Error::Bech32UnknownHRP => "Unknown human-readable part".into(),
  63. Error::Custom(ref s) => s.clone(),
  64. };
  65. msg.fmt(f)
  66. }
  67. }
  68. impl From<SecpError> for Error {
  69. fn from(e: SecpError) -> Self {
  70. match e {
  71. SecpError::InvalidPublicKey => Error::InvalidPublic,
  72. SecpError::InvalidSecretKey => Error::InvalidSecret,
  73. SecpError::InvalidMessage => Error::InvalidMessage,
  74. _ => Error::InvalidSignature,
  75. }
  76. }
  77. }
  78. impl From<Bech32Error> for Error {
  79. fn from(e: Bech32Error) -> Self {
  80. match e {
  81. Bech32Error::MissingSeparator => Error::Bech32MissingSeparator,
  82. Bech32Error::InvalidChecksum => Error::Bech32InvalidChecksum,
  83. Bech32Error::InvalidLength => Error::Bech32InvalidLength,
  84. Bech32Error::InvalidChar(ch) => Error::Bech32InvalidChar(ch),
  85. Bech32Error::InvalidData(data) => Error::Bech32InvalidData(data),
  86. Bech32Error::MixedCase => Error::Bech32MixedCase,
  87. }
  88. }
  89. }