Skip to content

Commit

Permalink
feat: Implement Deref and DerefMut for Signed
Browse files Browse the repository at this point in the history
  • Loading branch information
netrome committed May 7, 2024
1 parent 8ad91f0 commit e5a704c
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions signer/src/ecdsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,20 @@ impl<T: wsts::net::Signable> Signed<T> {
}
}

impl<T> std::ops::Deref for Signed<T> {
type Target = T;

fn deref(&self) -> &Self::Target {
&self.inner
}
}

impl<T> std::ops::DerefMut for Signed<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
}
}

/// Helper trait to provide the ability to construct a `Signed<T>`.
pub trait SignECDSA: Sized {
fn sign_ecdsa(self, private_key: &Scalar) -> Result<Signed<Self>, Error>;
Expand Down Expand Up @@ -139,11 +153,31 @@ mod tests {
assert!(!signed_msg.verify());
}

#[test]
fn signed_should_deref_to_the_underlying_type() {
let msg = SignableStr("I'm Batman");
let bruce_wayne_private_key = Scalar::from(1337);

let signed_msg = msg
.sign_ecdsa(&bruce_wayne_private_key)
.expect("Failed to sign message");

assert_eq!(signed_msg.len(), 10);
}

struct SignableStr(&'static str);

impl wsts::net::Signable for SignableStr {
fn hash(&self, hasher: &mut sha2::Sha256) {
hasher.update(self.0)
}
}

impl std::ops::Deref for SignableStr {
type Target = &'static str;

fn deref(&self) -> &Self::Target {
&self.0
}
}
}

0 comments on commit e5a704c

Please sign in to comment.