Skip to content
This repository was archived by the owner on Jun 11, 2022. It is now read-only.
Merged
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
39 changes: 33 additions & 6 deletions cardano/src/redeem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,9 @@ impl fmt::Display for PublicKey {
}
}

pub const PRIVATEKEY_SIZE: usize = 64;
pub const PRIVATEKEY_SIZE: usize = 32;

#[derive(Clone)]
pub struct PrivateKey([u8; PRIVATEKEY_SIZE]);
impl fmt::Debug for PrivateKey {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Expand Down Expand Up @@ -145,17 +146,18 @@ impl PrivateKey {
Self::from_slice(&bytes)
}

pub fn generate(seed: &[u8]) -> Self {
let (sk, _) = ed25519::keypair(seed);
Self::from_bytes(sk)
pub fn generate(seed: &[u8]) -> Result<Self> {
Self::from_slice(seed)
}

pub fn public(&self) -> PublicKey {
PublicKey::from_bytes(ed25519::to_public(&self.0))
let (_, pk) = ed25519::keypair(&self.0);
PublicKey::from_bytes(pk)
}

pub fn sign(&self, bytes: &[u8]) -> Signature {
Signature::from_bytes(ed25519::signature(bytes, &self.0))
let (sk, _) = ed25519::keypair(&self.0);
Signature::from_bytes(ed25519::signature(bytes, &sk))
}
}

Expand Down Expand Up @@ -497,3 +499,28 @@ impl<'de> serde::Deserialize<'de> for Signature {
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use quickcheck::{Arbitrary, Gen};

impl Arbitrary for PrivateKey {
fn arbitrary<G: Gen>(g: &mut G) -> Self {
let mut seed = [0u8; PRIVATEKEY_SIZE];
for byte in seed.iter_mut() {
*byte = u8::arbitrary(g);
}
PrivateKey::from_bytes(seed)
}
}

quickcheck! {
fn redeem_signature(stuff: (PrivateKey, Vec<u8>)) -> bool {
let (private_key, data) = stuff;
let public_key = private_key.public();
let signature = private_key.sign(&data);
public_key.verify(&signature, &data)
}
}
}