diff --git a/native/native_rust_library/src/lib.rs b/native/native_rust_library/src/lib.rs --- a/native/native_rust_library/src/lib.rs +++ b/native/native_rust_library/src/lib.rs @@ -23,10 +23,12 @@ use tokio::runtime::{Builder, Runtime}; use tonic::{Request, Status}; use tracing::instrument; +use wallet_registration::register_wallet_user; mod argon2_tools; mod backup; mod constants; +mod wallet_registration; use argon2_tools::compute_backup_key_str; @@ -84,6 +86,21 @@ promise_id: u32, ); + #[cxx_name = "identityRegisterWalletUser"] + fn register_wallet_user( + siwe_message: String, + siwe_signature: String, + key_payload: String, + key_payload_signature: String, + content_prekey: String, + content_prekey_signature: String, + notif_prekey: String, + notif_prekey_signature: String, + content_one_time_keys: Vec, + notif_one_time_keys: Vec, + promise_id: u32, + ); + #[cxx_name = "identityLogInWalletUser"] fn log_in_wallet_user( siwe_message: String, diff --git a/native/native_rust_library/src/wallet_registration.rs b/native/native_rust_library/src/wallet_registration.rs new file mode 100644 --- /dev/null +++ b/native/native_rust_library/src/wallet_registration.rs @@ -0,0 +1,88 @@ +use crate::{ + handle_string_result_as_callback, Error, UserIDAndDeviceAccessToken, + WalletUserInfo, CODE_VERSION, DEVICE_TYPE, IDENTITY_SOCKET_ADDR, RUNTIME, +}; +use grpc_clients::identity::{ + get_unauthenticated_client, + protos::unauth::{ + DeviceKeyUpload, IdentityKeyInfo, Prekey, WalletAuthRequest, + }, +}; +use tracing::instrument; + +#[instrument] +pub fn register_wallet_user( + siwe_message: String, + siwe_signature: String, + key_payload: String, + key_payload_signature: String, + content_prekey: String, + content_prekey_signature: String, + notif_prekey: String, + notif_prekey_signature: String, + content_one_time_keys: Vec, + notif_one_time_keys: Vec, + promise_id: u32, +) { + RUNTIME.spawn(async move { + let wallet_user_info = WalletUserInfo { + siwe_message, + siwe_signature, + key_payload, + key_payload_signature, + content_prekey, + content_prekey_signature, + notif_prekey, + notif_prekey_signature, + content_one_time_keys, + notif_one_time_keys, + }; + let result = register_wallet_user_helper(wallet_user_info).await; + handle_string_result_as_callback(result, promise_id); + }); +} + +async fn register_wallet_user_helper( + wallet_user_info: WalletUserInfo, +) -> Result { + let registration_request = WalletAuthRequest { + siwe_message: wallet_user_info.siwe_message, + siwe_signature: wallet_user_info.siwe_signature, + device_key_upload: Some(DeviceKeyUpload { + device_key_info: Some(IdentityKeyInfo { + payload: wallet_user_info.key_payload, + payload_signature: wallet_user_info.key_payload_signature, + social_proof: None, // The SIWE message and signature are the social proof + }), + content_upload: Some(Prekey { + prekey: wallet_user_info.content_prekey, + prekey_signature: wallet_user_info.content_prekey_signature, + }), + notif_upload: Some(Prekey { + prekey: wallet_user_info.notif_prekey, + prekey_signature: wallet_user_info.notif_prekey_signature, + }), + one_time_content_prekeys: wallet_user_info.content_one_time_keys, + one_time_notif_prekeys: wallet_user_info.notif_one_time_keys, + device_type: DEVICE_TYPE.into(), + }), + }; + + let mut identity_client = get_unauthenticated_client( + IDENTITY_SOCKET_ADDR, + CODE_VERSION, + DEVICE_TYPE.as_str_name().to_lowercase(), + ) + .await?; + + let registration_response = identity_client + .register_wallet_user(registration_request) + .await? + .into_inner(); + + let user_id_and_access_token = UserIDAndDeviceAccessToken { + user_id: registration_response.user_id, + access_token: registration_response.access_token, + }; + Ok(serde_json::to_string(&user_id_and_access_token)?) +}