2021-01-04 15:18:51 +01:00
|
|
|
// Copyright (C) 2020 Éloïs SANCHEZ.
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as
|
|
|
|
// published by the Free Software Foundation, either version 3 of the
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
use crate::*;
|
|
|
|
|
2021-01-09 23:39:04 +01:00
|
|
|
pub(super) fn change_secret_code(
|
2021-01-09 21:12:52 +01:00
|
|
|
currency: &str,
|
|
|
|
dewif: &str,
|
2021-01-09 23:39:04 +01:00
|
|
|
old_secret_code: &str,
|
2021-01-09 22:03:19 +01:00
|
|
|
member_wallet: bool,
|
2021-01-09 23:39:04 +01:00
|
|
|
secret_code_type: SecretCodeType,
|
2021-01-10 15:45:55 +01:00
|
|
|
system_memory: i64,
|
2021-01-09 22:03:19 +01:00
|
|
|
) -> Result<Vec<String>, DubpError> {
|
2021-01-04 15:18:51 +01:00
|
|
|
let currency = parse_currency(currency)?;
|
|
|
|
let mut keypairs = dup_crypto::dewif::read_dewif_file_content(
|
|
|
|
ExpectedCurrency::Specific(currency),
|
|
|
|
dewif,
|
2021-01-09 23:39:04 +01:00
|
|
|
old_secret_code,
|
2021-01-04 15:18:51 +01:00
|
|
|
)
|
|
|
|
.map_err(DubpError::DewifReadError)?;
|
|
|
|
if let Some(KeyPairEnum::Ed25519(keypair)) = keypairs.next() {
|
2021-01-10 15:45:55 +01:00
|
|
|
let log_n = log_n(system_memory);
|
|
|
|
let new_secret_code = gen_secret_code(member_wallet, secret_code_type, log_n)?;
|
2021-01-09 22:03:19 +01:00
|
|
|
|
2021-01-10 15:45:55 +01:00
|
|
|
let dewif =
|
|
|
|
dup_crypto::dewif::write_dewif_v3_content(currency, &keypair, log_n, &new_secret_code);
|
2021-01-09 22:03:19 +01:00
|
|
|
let pubkey = keypair.public_key().to_base58();
|
2021-01-09 23:39:04 +01:00
|
|
|
Ok(vec![dewif, new_secret_code, pubkey])
|
2021-01-04 15:18:51 +01:00
|
|
|
} else {
|
|
|
|
Err(DubpError::DewifReadError(DewifReadError::CorruptedContent))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn gen_dewif(
|
2021-01-09 21:12:52 +01:00
|
|
|
currency: &str,
|
2021-01-09 23:39:04 +01:00
|
|
|
language: Language,
|
2021-01-09 21:12:52 +01:00
|
|
|
mnemonic: &str,
|
2021-01-09 22:03:19 +01:00
|
|
|
member_wallet: bool,
|
2021-01-09 23:39:04 +01:00
|
|
|
secret_code_type: SecretCodeType,
|
2021-01-10 15:45:55 +01:00
|
|
|
system_memory: i64,
|
2021-01-09 22:03:19 +01:00
|
|
|
) -> Result<Vec<String>, DubpError> {
|
2021-01-04 15:18:51 +01:00
|
|
|
let currency = parse_currency(currency)?;
|
2021-01-09 23:39:04 +01:00
|
|
|
let mnemonic =
|
|
|
|
Mnemonic::from_phrase(mnemonic, language).map_err(|_| DubpError::WrongLanguage)?;
|
2021-01-04 15:18:51 +01:00
|
|
|
let seed = dup_crypto::mnemonic::mnemonic_to_seed(&mnemonic);
|
|
|
|
let keypair = KeyPairFromSeed32Generator::generate(seed);
|
2021-01-09 22:03:19 +01:00
|
|
|
|
2021-01-10 15:45:55 +01:00
|
|
|
let log_n = log_n(system_memory);
|
|
|
|
let secret_code = gen_secret_code(member_wallet, secret_code_type, log_n)?;
|
|
|
|
let dewif = dup_crypto::dewif::write_dewif_v3_content(currency, &keypair, log_n, &secret_code);
|
2021-01-09 22:03:19 +01:00
|
|
|
let pubkey = keypair.public_key().to_base58();
|
2021-01-09 23:39:04 +01:00
|
|
|
Ok(vec![dewif, secret_code, pubkey])
|
2021-01-04 15:18:51 +01:00
|
|
|
}
|
|
|
|
|
2021-01-09 22:03:19 +01:00
|
|
|
pub(super) fn get_pubkey(currency: Currency, dewif: &str, pin: &str) -> Result<String, DubpError> {
|
2021-01-04 15:18:51 +01:00
|
|
|
let mut keypairs = dup_crypto::dewif::read_dewif_file_content(
|
|
|
|
ExpectedCurrency::Specific(currency),
|
|
|
|
dewif,
|
2021-01-08 01:49:54 +01:00
|
|
|
&pin.to_ascii_uppercase(),
|
2021-01-04 15:18:51 +01:00
|
|
|
)
|
|
|
|
.map_err(DubpError::DewifReadError)?;
|
|
|
|
if let Some(KeyPairEnum::Ed25519(keypair)) = keypairs.next() {
|
|
|
|
Ok(keypair.public_key().to_base58())
|
|
|
|
} else {
|
|
|
|
Err(DubpError::DewifReadError(DewifReadError::CorruptedContent))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-09 21:12:52 +01:00
|
|
|
pub(super) fn sign(currency: &str, dewif: &str, pin: &str, msg: &str) -> Result<String, DubpError> {
|
2021-01-04 15:18:51 +01:00
|
|
|
let currency = parse_currency(currency)?;
|
|
|
|
let mut keypairs = dup_crypto::dewif::read_dewif_file_content(
|
|
|
|
ExpectedCurrency::Specific(currency),
|
|
|
|
dewif,
|
2021-01-08 01:49:54 +01:00
|
|
|
&pin.to_ascii_uppercase(),
|
2021-01-04 15:18:51 +01:00
|
|
|
)
|
|
|
|
.map_err(DubpError::DewifReadError)?;
|
|
|
|
if let Some(KeyPairEnum::Ed25519(keypair)) = keypairs.next() {
|
|
|
|
Ok(keypair.generate_signator().sign(msg.as_bytes()).to_base64())
|
|
|
|
} else {
|
|
|
|
Err(DubpError::DewifReadError(DewifReadError::CorruptedContent))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn sign_several(
|
2021-01-09 21:12:52 +01:00
|
|
|
currency: &str,
|
|
|
|
dewif: &str,
|
|
|
|
pin: &str,
|
|
|
|
msgs: &[&str],
|
2021-01-04 15:18:51 +01:00
|
|
|
) -> Result<Vec<String>, DubpError> {
|
|
|
|
let currency = parse_currency(currency)?;
|
|
|
|
let mut keypairs = dup_crypto::dewif::read_dewif_file_content(
|
|
|
|
ExpectedCurrency::Specific(currency),
|
|
|
|
dewif,
|
2021-01-08 01:49:54 +01:00
|
|
|
&pin.to_ascii_uppercase(),
|
2021-01-04 15:18:51 +01:00
|
|
|
)
|
|
|
|
.map_err(DubpError::DewifReadError)?;
|
|
|
|
if let Some(KeyPairEnum::Ed25519(keypair)) = keypairs.next() {
|
|
|
|
let signator = keypair.generate_signator();
|
|
|
|
Ok(msgs
|
2021-01-04 17:08:43 +01:00
|
|
|
.iter()
|
2021-01-04 15:18:51 +01:00
|
|
|
.map(|msg| signator.sign(msg.as_bytes()).to_base64())
|
|
|
|
.collect())
|
|
|
|
} else {
|
|
|
|
Err(DubpError::DewifReadError(DewifReadError::CorruptedContent))
|
|
|
|
}
|
|
|
|
}
|
2021-01-10 15:45:55 +01:00
|
|
|
|
2021-02-06 19:52:42 +01:00
|
|
|
pub(crate) fn log_n(system_memory: i64) -> u8 {
|
2021-01-10 15:45:55 +01:00
|
|
|
if system_memory > 3_000_000_000 {
|
|
|
|
15
|
|
|
|
} else {
|
|
|
|
12
|
|
|
|
}
|
|
|
|
}
|