Skip to content

Commit

Permalink
accounts_import api impl
Browse files Browse the repository at this point in the history
  • Loading branch information
surinder83singh committed Oct 10, 2024
1 parent 65a6967 commit 9610a3f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 15 deletions.
9 changes: 7 additions & 2 deletions wallet/core/src/api/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
10 changes: 9 additions & 1 deletion wallet/core/src/api/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,15 @@ pub trait WalletApi: Send + Sync + AnySync {
request: AccountsEnsureDefaultRequest,
) -> Result<AccountsEnsureDefaultResponse>;

// TODO
/// Wrapper around [`accounts_import_call()`](Self::accounts_import_call)
async fn accounts_import(
self: Arc<Self>,
wallet_secret: Secret,
account_create_args: AccountCreateArgs,
) -> Result<AccountDescriptor> {
Ok(self.accounts_import_call(AccountsImportRequest { wallet_secret, account_create_args }).await?.account_descriptor)
}

async fn accounts_import_call(self: Arc<Self>, request: AccountsImportRequest) -> Result<AccountsImportResponse>;

/// Get an [`AccountDescriptor`] for a specific account id.
Expand Down
16 changes: 13 additions & 3 deletions wallet/core/src/wallet/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,9 +343,19 @@ impl WalletApi for super::Wallet {
Ok(AccountsEnsureDefaultResponse { account_descriptor })
}

async fn accounts_import_call(self: Arc<Self>, _request: AccountsImportRequest) -> Result<AccountsImportResponse> {
// TODO handle account imports
return Err(Error::NotImplemented);
async fn accounts_import_call(self: Arc<Self>, request: AccountsImportRequest) -> Result<AccountsImportResponse> {
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<Self>, request: AccountsGetRequest) -> Result<AccountsGetResponse> {
Expand Down
8 changes: 0 additions & 8 deletions wallet/core/src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion wallet/keys/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ pub use crate::publickey::*;
pub use crate::secret::*;
pub use crate::types::*;
pub use crate::xprv::*;
pub use crate::xpub::*;
pub use crate::xpub::*;

0 comments on commit 9610a3f

Please sign in to comment.