Commit f413f826 authored by Éloïs's avatar Éloïs

[feat] crypto: seed: add base58

parent 55547d2c
Pipeline #4598 failed with stages
in 17 seconds
......@@ -14,6 +14,7 @@ crate-type = ["cdylib", "rlib"]
default = ["console_error_panic_hook"]
[dependencies]
base58 = "0.1.*"
cfg-if = "0.1.2"
dup-crypto = "0.6.0"
dubp-documents = "0.12.0"
......
use base58::ToBase58;
use dup_crypto::bases::*;
use dup_crypto::hashs::*;
use dup_crypto::keys::*;
......@@ -35,8 +36,8 @@ pub fn generate_ed25519_pubkey(salt: &str, password: &str) -> String {
#[wasm_bindgen]
pub fn generate_seed_from_salted_password(salt: &str, password: &str, base: usize) -> String {
if base != 16 {
return "Error : invalid param base : accepted values are : 16.".to_owned();
if base != 16 && base != 58 {
return "Error : invalid param base : accepted values are : 16, 58.".to_owned();
}
let seed = ed25519::KeyPairFromSaltedPasswordGenerator::with_default_parameters()
......@@ -44,6 +45,7 @@ pub fn generate_seed_from_salted_password(salt: &str, password: &str, base: usiz
match base {
16 => Hash(seed).to_hex(),
58 => seed.to_base58(),
_ => unreachable!(),
}
}
......
......@@ -30,16 +30,27 @@ fn test_generate_ed25519_keypair() {
#[wasm_bindgen_test]
fn test_generate_ed25519_keypair_from_seed() {
let seed = generate_seed_from_salted_password(
let seed_b16 = generate_seed_from_salted_password(
"JhxtHB7UcsDbA9wMSyMKXUzBZUQvqVyB32KwzS9SWoLkjrUhHV",
"JhxtHB7UcsDbA9wMSyMKXUzBZUQvqVyB32KwzS9SWoLkjrUhHV_",
16,
);
let pubkey = generate_ed25519_pubkey_from_seed(&seed, 16);
let seed_b58 = generate_seed_from_salted_password(
"JhxtHB7UcsDbA9wMSyMKXUzBZUQvqVyB32KwzS9SWoLkjrUhHV",
"JhxtHB7UcsDbA9wMSyMKXUzBZUQvqVyB32KwzS9SWoLkjrUhHV_",
58,
);
let pubkey_from_seed_b16 = generate_ed25519_pubkey_from_seed(&seed_b16, 16);
let pubkey_from_seed_b58 = generate_ed25519_pubkey_from_seed(&seed_b58, 58);
assert_eq!(
pubkey.as_str(),
pubkey_from_seed_b16,
pubkey_from_seed_b58
);
assert_eq!(
pubkey_from_seed_b58.as_str(),
"7iMV3b6j2hSj5WtrfchfvxivS9swN3opDgxudeHq64fb"
);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment