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/>.
|
|
|
|
|
2021-02-09 01:58:03 +01:00
|
|
|
pub mod bip32;
|
|
|
|
pub mod classic;
|
|
|
|
|
2021-01-04 15:18:51 +01:00
|
|
|
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)?;
|
2021-02-09 01:58:03 +01:00
|
|
|
let new_log_n = log_n(system_memory);
|
|
|
|
let new_secret_code = gen_secret_code(member_wallet, secret_code_type, new_log_n)?;
|
2021-01-09 22:03:19 +01:00
|
|
|
|
2021-02-09 01:58:03 +01:00
|
|
|
let new_dewif =
|
|
|
|
dup_crypto::dewif::change_dewif_passphrase(dewif, old_secret_code, &new_secret_code)
|
|
|
|
.map_err(DubpError::DewifReadError)?;
|
|
|
|
|
|
|
|
let pubkey = get_pubkey(currency, &new_dewif, &new_secret_code)?;
|
|
|
|
|
|
|
|
Ok(vec![new_dewif, new_secret_code, pubkey])
|
2021-01-04 15:18:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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-02-09 01:58:03 +01:00
|
|
|
wallet_type: WalletType,
|
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);
|
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)?;
|
2021-02-09 01:58:03 +01:00
|
|
|
|
|
|
|
let (dewif, pubkey) = match wallet_type {
|
|
|
|
WalletType::Ed25519 => {
|
|
|
|
let keypair = dup_crypto::keys::ed25519::bip32::KeyPair::from_seed(seed.clone());
|
|
|
|
let pubkey = keypair.public_key();
|
|
|
|
let dewif = dup_crypto::dewif::write_dewif_v4_content(
|
|
|
|
currency,
|
|
|
|
log_n,
|
|
|
|
&secret_code,
|
|
|
|
&pubkey,
|
|
|
|
seed,
|
|
|
|
);
|
|
|
|
(dewif, pubkey.to_base58())
|
|
|
|
}
|
|
|
|
WalletType::Bip32Ed25519 => {
|
|
|
|
let keypair = KeyPairFromSeed32Generator::generate(seed);
|
|
|
|
let dewif =
|
|
|
|
dup_crypto::dewif::write_dewif_v3_content(currency, &keypair, log_n, &secret_code);
|
|
|
|
(dewif, 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-02-08 18:26:30 +01:00
|
|
|
pub(super) fn get_secret_code_len(
|
2021-02-09 02:12:01 +01:00
|
|
|
dewif: *const raw::c_char,
|
|
|
|
member_wallet: u32,
|
|
|
|
secret_code_type: u32,
|
|
|
|
) -> Result<usize, DubpError> {
|
|
|
|
let dewif = char_ptr_to_str(dewif)?;
|
|
|
|
let member_wallet = member_wallet != 0;
|
|
|
|
let secret_code_type = SecretCodeType::from(secret_code_type);
|
|
|
|
|
|
|
|
let log_n = dup_crypto::dewif::read_dewif_log_n(ExpectedCurrency::Any, dewif)
|
2021-02-08 18:26:30 +01:00
|
|
|
.map_err(DubpError::DewifReadError)?;
|
|
|
|
|
2021-02-09 02:12:01 +01:00
|
|
|
Ok(crate::secret_code::compute_secret_code_len(
|
|
|
|
member_wallet,
|
|
|
|
secret_code_type,
|
|
|
|
log_n,
|
|
|
|
)?)
|
2021-02-08 18:26:30 +01:00
|
|
|
}
|
|
|
|
|
2021-02-09 01:58:03 +01:00
|
|
|
pub(super) fn get_pubkey(
|
|
|
|
currency: Currency,
|
2021-01-09 21:12:52 +01:00
|
|
|
dewif: &str,
|
2021-02-09 01:58:03 +01:00
|
|
|
secret_code: &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-02-09 01:58:03 +01:00
|
|
|
&secret_code.to_ascii_uppercase(),
|
2021-01-04 15:18:51 +01:00
|
|
|
)
|
|
|
|
.map_err(DubpError::DewifReadError)?;
|
2021-02-09 01:58:03 +01:00
|
|
|
if let Some(keypair) = keypairs.next() {
|
|
|
|
Ok(keypair.public_key().to_base58())
|
2021-01-04 15:18:51 +01:00
|
|
|
} 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
|
|
|
|
}
|
|
|
|
}
|