Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement drop for private keys #2

Merged
merged 1 commit into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 29 additions & 14 deletions src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,20 @@ use ed25519_dalek::{SigningKey, Signer, VerifyingKey, Verifier};
pub struct PublicKey([u8;32]);

impl PublicKey {
pub fn as_array(&self) -> [u8;32] {
self.0
}

pub fn verify_hash(&self, hash: &[u8;32], signature: Signature) -> bool {
let pk = VerifyingKey::from_bytes(&self.0).unwrap();
pk.verify(hash, &signature.into()).is_ok()
}
}

impl AsRef<[u8]> for PublicKey {
fn as_ref(&self) -> &[u8] {
&self.0
}
}

/// An ed25519 private key that can be used to sign a hash
#[derive(Debug, PartialEq, Clone, Copy)]
#[derive(Debug, PartialEq, Clone)]
pub struct PrivateKey([u8;64]);

impl PrivateKey {
Expand All @@ -31,10 +33,6 @@ impl PrivateKey {
PrivateKey(sk.to_keypair_bytes())
}

pub fn as_array(&self) -> [u8;64] {
self.0
}

pub fn public_key(&self) -> PublicKey {
PublicKey(self.0[32..].try_into().unwrap())
}
Expand All @@ -45,12 +43,27 @@ impl PrivateKey {
}
}

impl AsRef<[u8]> for PrivateKey {
fn as_ref(&self) -> &[u8] {
&self.0
}
}

impl Into<UnlockKey> for PrivateKey {
fn into(self) -> UnlockKey {
UnlockKey::new(Algorithm::ED25519, self.public_key())
}
}

impl Drop for PrivateKey {
fn drop(&mut self) {
// Zero out the private key
for byte in self.0.iter_mut() {
*byte = 0;
}
}
}

/// An address that can be used to receive UTXOs
#[derive(Debug, PartialEq, Clone, Copy)]
pub struct Address([u8;32]);
Expand All @@ -60,10 +73,6 @@ impl Address {
Address(addr)
}

pub fn as_array(&self) -> [u8;32] {
self.0
}

pub fn parse_string(s: &str) -> Result<Self, HexParseError> {
let s = match s.split_once(":"){
Some((_prefix, suffix)) => suffix,
Expand Down Expand Up @@ -92,6 +101,12 @@ impl Address {
}
}

impl AsRef<[u8]> for Address {
fn as_ref(&self) -> &[u8] {
&self.0
}
}

impl fmt::Display for Address {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut buf = [0u8;32+6];
Expand Down Expand Up @@ -193,7 +208,7 @@ impl SiaEncodable for UnlockKey {
fn encode(&self, buf: &mut Vec<u8>) {
self.algorithm.encode(buf);
buf.extend_from_slice(&(32 as u64).to_le_bytes());
buf.extend_from_slice(&self.public_key.as_array());
buf.extend_from_slice(self.public_key.as_ref());
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/seed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ mod tests {
let seed = Seed::from_mnemonic(PHRASE).unwrap();
for (i, expected) in test_addresses {
let pk = seed.private_key(i);
assert_eq!(pk.as_array(), expected.as_slice(), "index {}", i);
assert_eq!(pk.as_ref(), expected, "index {}", i);
}
}

Expand All @@ -143,7 +143,7 @@ mod tests {
let seed = Seed::from_mnemonic(PHRASE).unwrap();
for (i, expected) in test_addresses {
let pk = seed.private_key(i).public_key();
assert_eq!(pk.as_array(), expected.as_slice()[32..], "index {}", i);
assert_eq!(pk.as_ref(), expected[32..].as_ref(), "index {}", i);
}
}

Expand Down