Skip to content
Snippets Groups Projects
Commit d6bb963c authored by Cédric Moreau's avatar Cédric Moreau
Browse files

refact: public and secret copy

parent 4d0bb385
No related branches found
No related tags found
No related merge requests found
...@@ -8,10 +8,13 @@ use scrypt::password_hash::{Output, PasswordHasher, SaltString}; ...@@ -8,10 +8,13 @@ use scrypt::password_hash::{Output, PasswordHasher, SaltString};
use crate::crypto::duniter_signature::DuniterSignature; use crate::crypto::duniter_signature::DuniterSignature;
const PUBLIC_KEY_LEN: usize = 32;
const SECRET_KEY_LEN: usize = 64;
/// The main functional class for handle Duniter crypto. /// The main functional class for handle Duniter crypto.
pub struct DuniterKey { pub struct DuniterKey {
pub public: [u8; 32], pub public: [u8; PUBLIC_KEY_LEN],
pub secret: [u8; 64], pub secret: [u8; SECRET_KEY_LEN],
} }
/// How to display a duniter keyring: "pub_base58:sec_base58" format. /// How to display a duniter keyring: "pub_base58:sec_base58" format.
...@@ -32,18 +35,10 @@ impl DuniterKey { ...@@ -32,18 +35,10 @@ impl DuniterKey {
pub fn from_base58(public: &str, secret: &str) -> Result<DuniterKey, Box<dyn Error>> { pub fn from_base58(public: &str, secret: &str) -> Result<DuniterKey, Box<dyn Error>> {
let public_vec = bs58::decode(public).into_vec().unwrap(); let public_vec = bs58::decode(public).into_vec().unwrap();
let secret_vec = bs58::decode(secret).into_vec().unwrap(); let secret_vec = bs58::decode(secret).into_vec().unwrap();
let mut public = [0u8; 32]; let mut public = [0u8; PUBLIC_KEY_LEN];
for (i, v) in public_vec.iter().enumerate() { let mut secret = [0u8; SECRET_KEY_LEN];
if i < 32 { for i in 0..PUBLIC_KEY_LEN { public[i] = public_vec[i] }
public[i] = *v; for i in 0..SECRET_KEY_LEN { secret[i] = secret_vec[i] }
}
}
let mut secret = [0u8; 64];
for (i, v) in secret_vec.iter().enumerate() {
if i < 64 {
secret[i] = *v;
}
}
Ok(DuniterKey { public, secret }) Ok(DuniterKey { public, secret })
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment