From 8147bddfed910f2dbfdb0aaace9e8e5953fdb47a Mon Sep 17 00:00:00 2001 From: surinder singh Date: Thu, 10 Oct 2024 23:15:47 +0530 Subject: [PATCH] accounts_import api impl (#107) --- wallet/core/src/api/message.rs | 9 +++++++-- wallet/core/src/api/traits.rs | 10 +++++++++- wallet/core/src/wallet/api.rs | 16 +++++++++++++--- wallet/core/src/wallet/mod.rs | 8 -------- wallet/keys/src/prelude.rs | 2 +- 5 files changed, 30 insertions(+), 15 deletions(-) diff --git a/wallet/core/src/api/message.rs b/wallet/core/src/api/message.rs index 9756e51f5..0fe5ce951 100644 --- a/wallet/core/src/api/message.rs +++ b/wallet/core/src/api/message.rs @@ -401,11 +401,16 @@ pub struct AccountsEnsureDefaultResponse { // TODO #[derive(Clone, Debug, Serialize, Deserialize, BorshSerialize, BorshDeserialize)] #[serde(rename_all = "camelCase")] -pub struct AccountsImportRequest {} +pub struct AccountsImportRequest { + pub wallet_secret: Secret, + pub account_create_args: AccountCreateArgs, +} #[derive(Clone, Debug, Serialize, Deserialize, BorshSerialize, BorshDeserialize)] #[serde(rename_all = "camelCase")] -pub struct AccountsImportResponse {} +pub struct AccountsImportResponse { + pub account_descriptor: AccountDescriptor, +} #[derive(Clone, Debug, Serialize, Deserialize, BorshSerialize, BorshDeserialize)] #[serde(rename_all = "camelCase")] diff --git a/wallet/core/src/api/traits.rs b/wallet/core/src/api/traits.rs index 357665e77..74b11906c 100644 --- a/wallet/core/src/api/traits.rs +++ b/wallet/core/src/api/traits.rs @@ -375,7 +375,15 @@ pub trait WalletApi: Send + Sync + AnySync { request: AccountsEnsureDefaultRequest, ) -> Result; - // TODO + /// Wrapper around [`accounts_import_call()`](Self::accounts_import_call) + async fn accounts_import( + self: Arc, + wallet_secret: Secret, + account_create_args: AccountCreateArgs, + ) -> Result { + Ok(self.accounts_import_call(AccountsImportRequest { wallet_secret, account_create_args }).await?.account_descriptor) + } + async fn accounts_import_call(self: Arc, request: AccountsImportRequest) -> Result; /// Get an [`AccountDescriptor`] for a specific account id. diff --git a/wallet/core/src/wallet/api.rs b/wallet/core/src/wallet/api.rs index 7dda53e74..acb5869e0 100644 --- a/wallet/core/src/wallet/api.rs +++ b/wallet/core/src/wallet/api.rs @@ -343,9 +343,19 @@ impl WalletApi for super::Wallet { Ok(AccountsEnsureDefaultResponse { account_descriptor }) } - async fn accounts_import_call(self: Arc, _request: AccountsImportRequest) -> Result { - // TODO handle account imports - return Err(Error::NotImplemented); + async fn accounts_import_call(self: Arc, request: AccountsImportRequest) -> Result { + let AccountsImportRequest { wallet_secret, account_create_args } = request; + + let guard = self.guard(); + let guard = guard.lock().await; + + let account = self.create_account(&wallet_secret, account_create_args, true, &guard).await?; + account.clone().scan(Some(100), Some(5000)).await?; + let account_descriptor = account.descriptor()?; + self.store().as_account_store()?.store_single(&account.to_storage()?, account.metadata()?.as_ref()).await?; + self.store().commit(&wallet_secret).await?; + + Ok(AccountsImportResponse { account_descriptor }) } async fn accounts_get_call(self: Arc, request: AccountsGetRequest) -> Result { diff --git a/wallet/core/src/wallet/mod.rs b/wallet/core/src/wallet/mod.rs index d7c9b6c76..06cb76434 100644 --- a/wallet/core/src/wallet/mod.rs +++ b/wallet/core/src/wallet/mod.rs @@ -1474,7 +1474,6 @@ impl Wallet { let legacy_account = account.clone().as_legacy_account()?; legacy_account.create_private_context(wallet_secret, payment_secret, None).await?; - // account.clone().initialize_private_data(wallet_secret, payment_secret, None).await?; if self.is_connected() { if let Some(notifier) = notifier { @@ -1483,13 +1482,6 @@ impl Wallet { account.clone().scan(Some(100), Some(5000)).await?; } - // let derivation = account.clone().as_derivation_capable()?.derivation(); - // let m = derivation.receive_address_manager(); - // m.get_range(0..(m.index() + CACHE_ADDRESS_OFFSET))?; - // let m = derivation.change_address_manager(); - // m.get_range(0..(m.index() + CACHE_ADDRESS_OFFSET))?; - // account.clone().clear_private_data().await?; - legacy_account.clear_private_context().await?; Ok(account) diff --git a/wallet/keys/src/prelude.rs b/wallet/keys/src/prelude.rs index 12c0e39e2..5d3af82dd 100644 --- a/wallet/keys/src/prelude.rs +++ b/wallet/keys/src/prelude.rs @@ -11,4 +11,4 @@ pub use crate::publickey::*; pub use crate::secret::*; pub use crate::types::*; pub use crate::xprv::*; -pub use crate::xpub::*; \ No newline at end of file +pub use crate::xpub::*;