Skip to content

Commit 6b4abac

Browse files
committed
primitives - ToETHChecksum trait
1 parent 19b1d22 commit 6b4abac

File tree

5 files changed

+31
-14
lines changed

5 files changed

+31
-14
lines changed

adapter/src/dummy.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use futures::future::{BoxFuture, FutureExt};
22
use primitives::adapter::{Adapter, AdapterError, AdapterResult, DummyAdapterOptions, Session};
33
use primitives::channel_validator::ChannelValidator;
44
use primitives::config::Config;
5-
use primitives::{Channel, ValidatorId};
5+
use primitives::{Channel, ToETHChecksum, ValidatorId};
66
use std::collections::HashMap;
77

88
#[derive(Debug, Clone)]
@@ -43,7 +43,7 @@ impl Adapter for DummyAdapter {
4343
let signature = format!(
4444
"Dummy adapter signature for {} by {}",
4545
state_root,
46-
self.whoami().to_hex_checksummed_string()
46+
self.whoami().to_checksum()
4747
);
4848
Ok(signature)
4949
}
@@ -57,7 +57,7 @@ impl Adapter for DummyAdapter {
5757
// select the `identity` and compare it to the signer
5858
// for empty string this will return array with 1 element - an empty string `[""]`
5959
let is_same = match signature.rsplit(' ').take(1).next() {
60-
Some(from) => from == signer.to_hex_checksummed_string(),
60+
Some(from) => from == signer.to_checksum(),
6161
None => false,
6262
};
6363

adapter/src/ethereum.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use primitives::{
1313
adapter::{Adapter, AdapterError, AdapterResult, KeystoreOptions, Session},
1414
channel_validator::ChannelValidator,
1515
config::Config,
16-
Channel, ValidatorId,
16+
Channel, ToETHChecksum, ValidatorId,
1717
};
1818
use serde::{Deserialize, Serialize};
1919
use serde_hex::{SerHexOpt, StrictPfx};
@@ -211,7 +211,7 @@ impl Adapter for EthereumAdapter {
211211
let verified = ewt_verify(header_encoded, payload_encoded, token_encoded)
212212
.map_err(|e| map_error(&e.to_string()))?;
213213

214-
if self.whoami().to_hex_checksummed_string() != verified.payload.id {
214+
if self.whoami().to_checksum() != verified.payload.id {
215215
return Err(AdapterError::Configuration(
216216
"token payload.id !== whoami(): token was not intended for us".to_string(),
217217
));
@@ -265,10 +265,10 @@ impl Adapter for EthereumAdapter {
265265

266266
let era = Utc::now().timestamp_millis() as f64 / 60000.0;
267267
let payload = Payload {
268-
id: validator.to_hex_checksummed_string(),
268+
id: validator.to_checksum(),
269269
era: era.floor() as i64,
270270
identity: None,
271-
address: self.whoami().to_hex_checksummed_string(),
271+
address: self.whoami().to_checksum(),
272272
};
273273

274274
ewt_sign(&wallet, &self.keystore_pwd, &payload)
@@ -460,7 +460,7 @@ mod test {
460460
let payload = Payload {
461461
id: "awesomeValidator".into(),
462462
era: 100_000,
463-
address: eth_adapter.whoami().to_hex_checksummed_string(),
463+
address: eth_adapter.whoami().to_checksum(),
464464
identity: None,
465465
};
466466
let wallet = eth_adapter.wallet.clone();
@@ -669,7 +669,7 @@ mod test {
669669
.expect("failed to deserialize address");
670670

671671
let payload = Payload {
672-
id: eth_adapter.whoami().to_hex_checksummed_string(),
672+
id: eth_adapter.whoami().to_checksum(),
673673
era: 100_000,
674674
address: format!("{:?}", leader_account),
675675
identity: Some(identity),

primitives/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,11 @@ impl error::Error for DomainError {
5555
None
5656
}
5757
}
58+
59+
/// Trait that creates a String which is `0x` prefixed and encodes the bytes by `eth_checksum`
60+
pub trait ToETHChecksum: AsRef<[u8]> {
61+
fn to_checksum(&self) -> String {
62+
// checksum replaces `0x` prefix and adds one itself
63+
eth_checksum::checksum(&hex::encode(self.as_ref()))
64+
}
65+
}

primitives/src/validator.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize};
33
use serde_hex::{SerHex, StrictPfx};
44
use std::fmt;
55

6-
use crate::{BalancesMap, BigNum, DomainError};
6+
use crate::{BalancesMap, BigNum, DomainError, ToETHChecksum};
77
use std::convert::TryFrom;
88

99
#[derive(Debug)]
@@ -23,15 +23,24 @@ impl ValidatorId {
2323
&self.0
2424
}
2525

26+
/// To Hex non-`0x` prefixed string without **Checksum**ing the string
2627
pub fn to_hex_non_prefix_string(&self) -> String {
2728
hex::encode(self.0)
2829
}
2930

31+
/// To Hex `0x` prefixed string **without** __Checksum__ing the string
32+
pub fn to_hex_prefix_string(&self) -> String {
33+
format!("0x{}", self.to_hex_non_prefix_string())
34+
}
35+
36+
// To Hex `0x` prefixed string **with** **Checksum**ing the string
3037
pub fn to_hex_checksummed_string(&self) -> String {
3138
eth_checksum::checksum(&format!("0x{}", self.to_hex_non_prefix_string()))
3239
}
3340
}
3441

42+
impl ToETHChecksum for ValidatorId {}
43+
3544
impl From<&[u8; 20]> for ValidatorId {
3645
fn from(bytes: &[u8; 20]) -> Self {
3746
Self(*bytes)
@@ -79,7 +88,7 @@ impl TryFrom<&String> for ValidatorId {
7988

8089
impl fmt::Display for ValidatorId {
8190
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
82-
write!(f, "{}", format!("0x{}", self.to_hex_non_prefix_string()))
91+
write!(f, "{}", self.to_checksum())
8392
}
8493
}
8594

validator_worker/src/sentry_interface.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use primitives::sentry::{
99
ValidatorMessageResponse,
1010
};
1111
use primitives::validator::MessageTypes;
12-
use primitives::{Channel, Config, ValidatorDesc, ValidatorId};
12+
use primitives::{Channel, Config, ToETHChecksum, ValidatorDesc, ValidatorId};
1313
use reqwest::{Client, Response};
1414
use std::collections::HashMap;
1515
use std::time::Duration;
@@ -95,7 +95,7 @@ impl<T: Adapter + 'static> SentryApi<T> {
9595
let url = format!(
9696
"{}/validator-messages/{}/{}?limit=1",
9797
self.validator_url,
98-
from.to_hex_checksummed_string(),
98+
from.to_checksum(),
9999
message_type
100100
);
101101
let result = self
@@ -220,7 +220,7 @@ async fn fetch_page(
220220

221221
let query = [
222222
format!("page={}", page),
223-
format!("validator={}", validator.to_hex_checksummed_string()),
223+
format!("validator={}", validator.to_checksum()),
224224
]
225225
.join("&");
226226

0 commit comments

Comments
 (0)