Skip to content

Commit bd4ba8c

Browse files
committed
apply feedback from Sam
1 parent 566ec47 commit bd4ba8c

File tree

5 files changed

+204
-198
lines changed

5 files changed

+204
-198
lines changed

adapter/src/ethereum.rs

Lines changed: 2 additions & 175 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ use web3::{
3232
Web3,
3333
};
3434

35+
mod error;
36+
3537
lazy_static! {
3638
static ref ADEXCORE_ABI: &'static [u8] =
3739
include_bytes!("../../lib/protocol-eth/abi/AdExCore.json");
@@ -431,181 +433,6 @@ pub fn ewt_verify(
431433
Ok(verified_payload)
432434
}
433435

434-
mod error {
435-
use primitives::adapter::{AdapterErrorKind, Error as AdapterError};
436-
use primitives::ChannelId;
437-
use std::fmt;
438-
439-
#[derive(Debug)]
440-
pub enum Error {
441-
Keystore(KeystoreError),
442-
WalletUnlock(ethstore::Error),
443-
Web3(web3::Error),
444-
RelayerClient(reqwest::Error),
445-
/// When the ChannelId that we get from hashing the EthereumChannel with the contract address
446-
/// does not align with the provided Channel
447-
InvalidChannelId {
448-
expected: ChannelId,
449-
actual: ChannelId,
450-
},
451-
ChannelInactive(ChannelId),
452-
/// Signing of the message failed
453-
SignMessage(EwtSigningError),
454-
VerifyMessage(EwtVerifyError),
455-
ContractInitialization(ethabi::Error),
456-
ContractQuerying(web3::contract::Error),
457-
/// Error occurred during verification of Signature and/or StateRoot and/or Address
458-
VerifyAddress(VerifyError),
459-
}
460-
461-
impl std::error::Error for Error {}
462-
463-
impl AdapterErrorKind for Error {}
464-
465-
impl fmt::Display for Error {
466-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
467-
use Error::*;
468-
469-
match self {
470-
Keystore(err) => write!(f, "Keystore: {}", err),
471-
WalletUnlock(err) => write!(f, "Wallet unlocking: {}", err),
472-
Web3(err) => write!(f, "Web3: {}", err),
473-
RelayerClient(err) => write!(f, "Relayer client: {}", err),
474-
InvalidChannelId { expected, actual} => write!(f, "The hashed EthereumChannel.id ({}) is not the same as the Channel.id ({}) that was provided", expected, actual),
475-
ChannelInactive(channel_id) => write!(f, "Channel ({}) is not Active on the ethereum network", channel_id),
476-
SignMessage(err) => write!(f, "Signing message: {}", err),
477-
VerifyMessage(err) => write!(f, "Verifying message: {}", err),
478-
ContractInitialization(err) => write!(f, "Contract initialization: {}", err),
479-
ContractQuerying(err) => write!(f, "Contract querying: {}", err),
480-
VerifyAddress(err) => write!(f, "Verifying address: {}", err)
481-
}
482-
}
483-
}
484-
485-
#[derive(Debug)]
486-
/// Error returned on `eth_adapter.verify()` when the combination of
487-
/// (signer, state_root, signature) **doesn't align**.
488-
pub enum VerifyError {
489-
PublicKeyRecovery(parity_crypto::publickey::Error),
490-
StateRootDecoding(hex::FromHexError),
491-
SignatureDecoding(hex::FromHexError),
492-
SignatureNotPrefixed,
493-
}
494-
495-
impl fmt::Display for VerifyError {
496-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
497-
use VerifyError::*;
498-
499-
match self {
500-
PublicKeyRecovery(err) => {
501-
write!(f, "Recovering the public key from the signature: {}", err)
502-
}
503-
StateRootDecoding(err) => write!(f, "Decoding the hex of the state root: {}", err),
504-
SignatureDecoding(err) => write!(f, "Decoding the hex of the signature: {}", err),
505-
SignatureNotPrefixed => write!(f, "Signature is not prefixed with `0x`"),
506-
}
507-
}
508-
}
509-
510-
impl From<VerifyError> for AdapterError<Error> {
511-
fn from(err: VerifyError) -> Self {
512-
AdapterError::Adapter(Error::VerifyAddress(err))
513-
}
514-
}
515-
516-
#[derive(Debug)]
517-
pub enum KeystoreError {
518-
/// `address` key is missing from the keystore file
519-
AddressMissing,
520-
/// The `address` key in the keystore file is not a valid `ValidatorId`
521-
AddressInvalid(primitives::DomainError),
522-
/// reading the keystore file failed
523-
ReadingFile(std::io::Error),
524-
/// Deserializing the keystore file failed
525-
Deserialization(serde_json::Error),
526-
}
527-
528-
impl std::error::Error for KeystoreError {
529-
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
530-
use KeystoreError::*;
531-
match self {
532-
AddressMissing => None,
533-
AddressInvalid(err) => Some(err),
534-
ReadingFile(err) => Some(err),
535-
Deserialization(err) => Some(err),
536-
}
537-
}
538-
}
539-
540-
impl fmt::Display for KeystoreError {
541-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
542-
use KeystoreError::*;
543-
544-
match self {
545-
AddressMissing => write!(f, "\"address\" key missing in keystore file"),
546-
AddressInvalid(err) => write!(f, "\"address\" is invalid: {}", err),
547-
ReadingFile(err) => write!(f, "Reading keystore file: {}", err),
548-
Deserialization(err) => write!(f, "Deserializing keystore file: {}", err),
549-
}
550-
}
551-
}
552-
553-
impl From<KeystoreError> for AdapterError<Error> {
554-
fn from(err: KeystoreError) -> Self {
555-
AdapterError::Adapter(Error::Keystore(err))
556-
}
557-
}
558-
559-
#[derive(Debug)]
560-
pub enum EwtSigningError {
561-
HeaderSerialization(serde_json::Error),
562-
PayloadSerialization(serde_json::Error),
563-
SigningMessage(ethstore::Error),
564-
DecodingHexSignature(hex::FromHexError),
565-
}
566-
567-
impl fmt::Display for EwtSigningError {
568-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
569-
use EwtSigningError::*;
570-
571-
match self {
572-
HeaderSerialization(err) => write!(f, "Header serialization: {}", err),
573-
PayloadSerialization(err) => write!(f, "Payload serialization: {}", err),
574-
SigningMessage(err) => write!(f, "Signing message: {}", err),
575-
DecodingHexSignature(err) => write!(f, "Decoding hex of Signature: {}", err),
576-
}
577-
}
578-
}
579-
impl From<EwtSigningError> for AdapterError<Error> {
580-
fn from(err: EwtSigningError) -> Self {
581-
AdapterError::Adapter(Error::SignMessage(err))
582-
}
583-
}
584-
585-
#[derive(Debug)]
586-
pub enum EwtVerifyError {
587-
AddressRecovery(parity_crypto::publickey::Error),
588-
SignatureDecoding(base64::DecodeError),
589-
PayloadDecoding(base64::DecodeError),
590-
PayloadDeserialization(serde_json::Error),
591-
PayloadUtf8(std::string::FromUtf8Error),
592-
}
593-
594-
impl fmt::Display for EwtVerifyError {
595-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
596-
use EwtVerifyError::*;
597-
598-
match self {
599-
AddressRecovery(err) => write!(f, "Address recovery: {}", err),
600-
SignatureDecoding(err) => write!(f, "Signature decoding: {}", err),
601-
PayloadDecoding(err) => write!(f, "Payload decoding: {}", err),
602-
PayloadDeserialization(err) => write!(f, "Payload deserialization: {}", err),
603-
PayloadUtf8(err) => write!(f, "Payload is not a valid utf8 string: {}", err),
604-
}
605-
}
606-
}
607-
}
608-
609436
#[cfg(test)]
610437
mod test {
611438
use super::*;

adapter/src/ethereum/error.rs

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
use primitives::adapter::{AdapterErrorKind, Error as AdapterError};
2+
use primitives::ChannelId;
3+
use std::fmt;
4+
5+
#[derive(Debug)]
6+
pub enum Error {
7+
Keystore(KeystoreError),
8+
WalletUnlock(ethstore::Error),
9+
Web3(web3::Error),
10+
RelayerClient(reqwest::Error),
11+
/// When the ChannelId that we get from hashing the EthereumChannel with the contract address
12+
/// does not align with the provided Channel
13+
InvalidChannelId {
14+
expected: ChannelId,
15+
actual: ChannelId,
16+
},
17+
ChannelInactive(ChannelId),
18+
/// Signing of the message failed
19+
SignMessage(EwtSigningError),
20+
VerifyMessage(EwtVerifyError),
21+
ContractInitialization(ethabi::Error),
22+
ContractQuerying(web3::contract::Error),
23+
/// Error occurred during verification of Signature and/or StateRoot and/or Address
24+
VerifyAddress(VerifyError),
25+
}
26+
27+
impl std::error::Error for Error {}
28+
29+
impl AdapterErrorKind for Error {}
30+
31+
impl fmt::Display for Error {
32+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33+
use Error::*;
34+
35+
match self {
36+
Keystore(err) => write!(f, "Keystore: {}", err),
37+
WalletUnlock(err) => write!(f, "Wallet unlocking: {}", err),
38+
Web3(err) => write!(f, "Web3: {}", err),
39+
RelayerClient(err) => write!(f, "Relayer client: {}", err),
40+
InvalidChannelId { expected, actual} => write!(f, "The hashed EthereumChannel.id ({}) is not the same as the Channel.id ({}) that was provided", expected, actual),
41+
ChannelInactive(channel_id) => write!(f, "Channel ({}) is not Active on the ethereum network", channel_id),
42+
SignMessage(err) => write!(f, "Signing message: {}", err),
43+
VerifyMessage(err) => write!(f, "Verifying message: {}", err),
44+
ContractInitialization(err) => write!(f, "Contract initialization: {}", err),
45+
ContractQuerying(err) => write!(f, "Contract querying: {}", err),
46+
VerifyAddress(err) => write!(f, "Verifying address: {}", err)
47+
}
48+
}
49+
}
50+
51+
#[derive(Debug)]
52+
/// Error returned on `eth_adapter.verify()` when the combination of
53+
/// (signer, state_root, signature) **doesn't align**.
54+
pub enum VerifyError {
55+
PublicKeyRecovery(parity_crypto::publickey::Error),
56+
StateRootDecoding(hex::FromHexError),
57+
SignatureDecoding(hex::FromHexError),
58+
SignatureNotPrefixed,
59+
}
60+
61+
impl fmt::Display for VerifyError {
62+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63+
use VerifyError::*;
64+
65+
match self {
66+
PublicKeyRecovery(err) => {
67+
write!(f, "Recovering the public key from the signature: {}", err)
68+
}
69+
StateRootDecoding(err) => write!(f, "Decoding the hex of the state root: {}", err),
70+
SignatureDecoding(err) => write!(f, "Decoding the hex of the signature: {}", err),
71+
SignatureNotPrefixed => write!(f, "Signature is not prefixed with `0x`"),
72+
}
73+
}
74+
}
75+
76+
impl From<VerifyError> for AdapterError<Error> {
77+
fn from(err: VerifyError) -> Self {
78+
AdapterError::Adapter(Error::VerifyAddress(err))
79+
}
80+
}
81+
82+
#[derive(Debug)]
83+
pub enum KeystoreError {
84+
/// `address` key is missing from the keystore file
85+
AddressMissing,
86+
/// The `address` key in the keystore file is not a valid `ValidatorId`
87+
AddressInvalid(primitives::DomainError),
88+
/// reading the keystore file failed
89+
ReadingFile(std::io::Error),
90+
/// Deserializing the keystore file failed
91+
Deserialization(serde_json::Error),
92+
}
93+
94+
impl std::error::Error for KeystoreError {
95+
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
96+
use KeystoreError::*;
97+
match self {
98+
AddressMissing => None,
99+
AddressInvalid(err) => Some(err),
100+
ReadingFile(err) => Some(err),
101+
Deserialization(err) => Some(err),
102+
}
103+
}
104+
}
105+
106+
impl fmt::Display for KeystoreError {
107+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
108+
use KeystoreError::*;
109+
110+
match self {
111+
AddressMissing => write!(f, "\"address\" key missing in keystore file"),
112+
AddressInvalid(err) => write!(f, "\"address\" is invalid: {}", err),
113+
ReadingFile(err) => write!(f, "Reading keystore file: {}", err),
114+
Deserialization(err) => write!(f, "Deserializing keystore file: {}", err),
115+
}
116+
}
117+
}
118+
119+
impl From<KeystoreError> for AdapterError<Error> {
120+
fn from(err: KeystoreError) -> Self {
121+
AdapterError::Adapter(Error::Keystore(err))
122+
}
123+
}
124+
125+
#[derive(Debug)]
126+
pub enum EwtSigningError {
127+
HeaderSerialization(serde_json::Error),
128+
PayloadSerialization(serde_json::Error),
129+
SigningMessage(ethstore::Error),
130+
DecodingHexSignature(hex::FromHexError),
131+
}
132+
133+
impl fmt::Display for EwtSigningError {
134+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
135+
use EwtSigningError::*;
136+
137+
match self {
138+
HeaderSerialization(err) => write!(f, "Header serialization: {}", err),
139+
PayloadSerialization(err) => write!(f, "Payload serialization: {}", err),
140+
SigningMessage(err) => write!(f, "Signing message: {}", err),
141+
DecodingHexSignature(err) => write!(f, "Decoding hex of Signature: {}", err),
142+
}
143+
}
144+
}
145+
impl From<EwtSigningError> for AdapterError<Error> {
146+
fn from(err: EwtSigningError) -> Self {
147+
AdapterError::Adapter(Error::SignMessage(err))
148+
}
149+
}
150+
151+
#[derive(Debug)]
152+
pub enum EwtVerifyError {
153+
AddressRecovery(parity_crypto::publickey::Error),
154+
SignatureDecoding(base64::DecodeError),
155+
PayloadDecoding(base64::DecodeError),
156+
PayloadDeserialization(serde_json::Error),
157+
PayloadUtf8(std::string::FromUtf8Error),
158+
}
159+
160+
impl fmt::Display for EwtVerifyError {
161+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
162+
use EwtVerifyError::*;
163+
164+
match self {
165+
AddressRecovery(err) => write!(f, "Address recovery: {}", err),
166+
SignatureDecoding(err) => write!(f, "Signature decoding: {}", err),
167+
PayloadDecoding(err) => write!(f, "Payload decoding: {}", err),
168+
PayloadDeserialization(err) => write!(f, "Payload deserialization: {}", err),
169+
PayloadUtf8(err) => write!(f, "Payload is not a valid utf8 string: {}", err),
170+
}
171+
}
172+
}

0 commit comments

Comments
 (0)