From b784415c57af7d6d484e6369e3316c2110b31cb1 Mon Sep 17 00:00:00 2001 From: link2xt Date: Sat, 29 Jul 2023 18:05:05 +0000 Subject: [PATCH] refactor: move dc_preconfigure_keypair() implementation into `deltachat` crate This allows to hide `DcKey` trait from public API. --- deltachat-ffi/src/lib.rs | 25 ++++++++----------------- src/key.rs | 25 ++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 18 deletions(-) diff --git a/deltachat-ffi/src/lib.rs b/deltachat-ffi/src/lib.rs index bee0167d8..58d238fd8 100644 --- a/deltachat-ffi/src/lib.rs +++ b/deltachat-ffi/src/lib.rs @@ -29,10 +29,9 @@ use deltachat::contact::{Contact, ContactId, Origin}; use deltachat::context::Context; use deltachat::ephemeral::Timer as EphemeralTimer; use deltachat::imex::BackupProvider; -use deltachat::key::DcKey; +use deltachat::key::preconfigure_keypair; use deltachat::message::MsgId; use deltachat::net::read_url_blob; -use deltachat::pgp::KeyPair; use deltachat::qr_code_generator::{generate_backup_qr, get_securejoin_qr_svg}; use deltachat::reaction::{get_msg_reactions, send_reaction, Reactions}; use deltachat::stock_str::StockMessage; @@ -796,21 +795,13 @@ pub unsafe extern "C" fn dc_preconfigure_keypair( return 0; } let ctx = &*context; - block_on(async move { - let addr = tools::EmailAddress::new(&to_string_lossy(addr))?; - let public = key::SignedPublicKey::from_asc(&to_string_lossy(public_data))?.0; - let secret = key::SignedSecretKey::from_asc(&to_string_lossy(secret_data))?.0; - let keypair = KeyPair { - addr, - public, - secret, - }; - key::store_self_keypair(ctx, &keypair, key::KeyPairUse::Default).await?; - Ok::<_, anyhow::Error>(1) - }) - .context("Failed to save keypair") - .log_err(ctx) - .unwrap_or(0) + let addr = to_string_lossy(addr); + let public_data = to_string_lossy(public_data); + let secret_data = to_string_lossy(secret_data); + block_on(preconfigure_keypair(ctx, &addr, &public_data, &secret_data)) + .context("Failed to save keypair") + .log_err(ctx) + .is_ok() as libc::c_int } #[no_mangle] diff --git a/src/key.rs b/src/key.rs index 9e2076bc6..75dc5342f 100644 --- a/src/key.rs +++ b/src/key.rs @@ -24,7 +24,7 @@ use crate::tools::{time, EmailAddress}; /// This trait is implemented for rPGP's [SignedPublicKey] and /// [SignedSecretKey] types and makes working with them a little /// easier in the deltachat world. -pub trait DcKey: Serialize + Deserializable + KeyTrait + Clone { +pub(crate) trait DcKey: Serialize + Deserializable + KeyTrait + Clone { /// Create a key from some bytes. fn from_slice(bytes: &[u8]) -> Result { Ok(::from_bytes(Cursor::new(bytes))?) @@ -307,6 +307,29 @@ pub async fn store_self_keypair( Ok(()) } +/// Saves a keypair as the default keys. +/// +/// This API is used for testing purposes +/// to avoid generating the key in tests. +/// Use import/export APIs instead. +pub async fn preconfigure_keypair( + context: &Context, + addr: &str, + public_data: &str, + secret_data: &str, +) -> Result<()> { + let addr = EmailAddress::new(addr)?; + let public = SignedPublicKey::from_asc(public_data)?.0; + let secret = SignedSecretKey::from_asc(secret_data)?.0; + let keypair = KeyPair { + addr, + public, + secret, + }; + store_self_keypair(context, &keypair, KeyPairUse::Default).await?; + Ok(()) +} + /// A key fingerprint #[derive(Clone, Eq, PartialEq, Hash, serde::Serialize, serde::Deserialize)] pub struct Fingerprint(Vec);