1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
pub use ed25519_dalek::{self, Keypair, PublicKey, SecretKey};
use rand::rngs::OsRng;

use crate::{ed25519::private::Ed25519PrivateKey, ed25519::public::Ed25519PublicKey};

#[derive(Debug)]
pub struct Ed25519Keypair {
    pub raw: ed25519_dalek::Keypair,
}

impl Ed25519Keypair {
    pub fn get_private_key(&self) -> Ed25519PrivateKey {
        Ed25519PrivateKey {
            raw: SecretKey::from_bytes(&self.raw.secret.to_bytes()).unwrap(),
        }
    }

    pub fn get_public_key(&self) -> Ed25519PublicKey {
        Ed25519PublicKey {
            raw: PublicKey::from_bytes(&self.raw.public.to_bytes()).unwrap(),
        }
    }
}

impl Ed25519Keypair {
    pub fn new() -> Self {
        // a.k.a Cryptographically secure pseudo-random number generator.
        let mut csprng = OsRng {};
        Ed25519Keypair {
            raw: ed25519_dalek::Keypair::generate(&mut csprng),
        }
    }

    pub fn construct_from_private_key(private_key: &Ed25519PrivateKey) -> Self {
        let secret_key: &SecretKey = &private_key.raw;
        let keypair: Keypair = construct_from_secret_key(&secret_key);

        Ed25519Keypair { raw: keypair }
    }
}

impl Drop for Ed25519Keypair {
    fn drop(&mut self) {
        if cfg!(feature = "drop-log-enable") {
            println!("{:?} is being deallocated", self);
        }
    }
}

pub fn create_keypair() -> Keypair {
    // a.k.a Cryptographically secure pseudo-random number generator.
    let mut csprng: OsRng = OsRng {};
    ed25519_dalek::Keypair::generate(&mut csprng)
}

pub fn construct_from_secret_key(private_key: &SecretKey) -> Keypair {
    let sk: SecretKey = SecretKey::from_bytes(&(private_key.to_bytes())).unwrap();
    let pk: PublicKey = (&sk).into();

    Keypair {
        public: pk,
        secret: sk,
    }
}

#[no_mangle]
#[cfg(target_os = "ios")]
pub extern "C" fn c_ed25519_keypair_new() -> *mut Ed25519Keypair {
    let keypair = Ed25519Keypair::new();
    Box::into_raw(Box::new(keypair))
}

#[no_mangle]
#[cfg(target_os = "ios")]
pub unsafe extern "C" fn c_ed25519_keypair_destroy(keypair: *mut Ed25519Keypair) {
    let _ = Box::from_raw(keypair);
}

#[no_mangle]
#[cfg(target_os = "ios")]
pub unsafe extern "C" fn c_ed25519_keypair_get_private_key(
    keypair: *mut Ed25519Keypair,
) -> *mut Ed25519PrivateKey {
    let keypair = &mut *keypair;
    let private_key = keypair.get_private_key();
    Box::into_raw(Box::new(private_key))
}

#[no_mangle]
#[cfg(target_os = "ios")]
pub unsafe extern "C" fn c_ed25519_keypair_get_public_key(
    keypair: *mut Ed25519Keypair,
) -> *mut Ed25519PublicKey {
    let keypair = &mut *keypair;
    let public_key = keypair.get_public_key();
    Box::into_raw(Box::new(public_key))
}

#[no_mangle]
#[cfg(target_os = "ios")]
pub extern "C" fn c_ed25519_keypair_construct_from_private_key(
    private_key: &Ed25519PrivateKey,
) -> *mut Ed25519Keypair {
    let keypair = Ed25519Keypair::construct_from_private_key(&private_key);
    Box::into_raw(Box::new(keypair))
}

#[cfg(test)]
mod tests {
    use crate::ed25519;

    #[test]
    fn it_creates_a_keypair() {
        let keypair = ed25519::keypair::create_keypair();

        let serialized_private_key = ed25519::private::serialize_private_key(&(keypair.secret));
        println!("{}", serialized_private_key);

        let serialized_public_key = ed25519::public::serialize_public_key(&(keypair.public));
        println!("{}", serialized_public_key);

        let deserialized_private_key =
            ed25519::private::deserialize_private_key(&serialized_private_key);
        assert_eq!(deserialized_private_key.is_ok(), true);
        assert_eq!(
            keypair.secret.to_bytes(),
            deserialized_private_key.unwrap().to_bytes()
        );

        let deserialized_public_key =
            ed25519::public::deserialize_public_key(&serialized_public_key);
        assert_eq!(deserialized_public_key.is_ok(), true);
        assert_eq!(
            keypair.public.to_bytes(),
            deserialized_public_key.unwrap().to_bytes()
        );
    }
}