Skip to content

Commit

Permalink
Addressing nits
Browse files Browse the repository at this point in the history
  • Loading branch information
citizen-stig committed Aug 31, 2023
1 parent 66c5562 commit c56b823
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 43 deletions.
79 changes: 48 additions & 31 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion examples/demo-rollup/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ pub fn get_genesis_config<A: BasicAddress>(
let key_and_address: PrivateKeyAndAddress<DefaultContext> = hex_key
.try_into()
.expect("Failed to parse sequencer private key and address");
assert!(key_and_address.is_matching(), "Inconsistent key data");
assert!(
key_and_address.is_matching_to_default(),
"Inconsistent key data"
);

create_demo_genesis_config(
100000000,
Expand Down
25 changes: 14 additions & 11 deletions module-system/sov-cli/src/wallet_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,9 @@ pub struct PrivateKeyAndAddress<Ctx: sov_modules_api::Context> {
pub address: Ctx::Address,
}

impl<Ctx: sov_modules_api::Context> PartialEq for PrivateKeyAndAddress<Ctx> {
fn eq(&self, other: &Self) -> bool {
self.private_key.pub_key() == other.private_key.pub_key() && self.address == other.address
}
}

impl<Ctx: sov_modules_api::Context> PrivateKeyAndAddress<Ctx> {
/// Returns boolean if address actually matches the private key
pub fn is_matching(&self) -> bool {
/// Returns boolean if the private key matches default address
pub fn is_matching_to_default(&self) -> bool {
self.private_key.to_address::<Ctx::Address>() == self.address
}

Expand All @@ -96,7 +90,8 @@ impl<Ctx: sov_modules_api::Context> PrivateKeyAndAddress<Ctx> {
}

/// A simplified struct representing private key and associated address
/// where the private key is represented as a hex string
/// where the private key is represented as a hex string and address as canonical string
/// TODO: Remove it https://github.com/Sovereign-Labs/sovereign-sdk/issues/766
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct HexPrivateAndAddress {
/// Private key is hex encoded bytes, without leading 0x
Expand Down Expand Up @@ -310,7 +305,11 @@ mod tests {

let decoded: PrivateKeyAndAddress<C> = serde_json::from_str(&json).unwrap();

assert_eq!(private_key_and_address, decoded);
assert_eq!(
private_key_and_address.private_key.pub_key(),
decoded.private_key.pub_key()
);
assert_eq!(private_key_and_address.address, decoded.address);
}

#[test]
Expand All @@ -327,6 +326,10 @@ mod tests {

let converted = PrivateKeyAndAddress::<C>::try_from(hex_private_key_and_address).unwrap();

assert_eq!(private_key_and_address, converted)
assert_eq!(
private_key_and_address.private_key.pub_key(),
converted.private_key.pub_key()
);
assert_eq!(private_key_and_address.address, converted.address);
}
}
2 changes: 2 additions & 0 deletions module-system/sov-modules-api/src/default_signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub mod private_key {
}

impl DefaultPrivateKey {
// This is private method and panics if input slice has incorrect length
fn try_from_keypair(value: &[u8]) -> Result<Self, DefaultPrivateKeyDeserializationError> {
let value: [u8; KEYPAIR_LENGTH] = value
.try_into()
Expand All @@ -48,6 +49,7 @@ pub mod private_key {
Ok(Self { key_pair })
}

// This is private method and panics if input slice has incorrect length
fn try_from_private_key(value: &[u8]) -> Self {
let value: [u8; SECRET_KEY_LENGTH] = value
.try_into()
Expand Down

0 comments on commit c56b823

Please sign in to comment.