-
Notifications
You must be signed in to change notification settings - Fork 116
feat(wallets): add get_wallet_names rpc
#2202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
9857170
1de62ac
f4162b8
704305d
1a7c003
874b294
bea5479
e1dbb67
aaf9d19
de645bd
4e46c35
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,23 +2,24 @@ use common::HttpStatusCode; | |
| use crypto::{decrypt_mnemonic, encrypt_mnemonic, generate_mnemonic, CryptoCtx, CryptoInitError, EncryptedData, | ||
| MnemonicError}; | ||
| use http::StatusCode; | ||
| use itertools::Itertools; | ||
| use mm2_core::mm_ctx::MmArc; | ||
| use mm2_err_handle::prelude::*; | ||
| use serde::de::DeserializeOwned; | ||
| use serde_json::{self as json}; | ||
| use serde_json::{self as json, Value as Json}; | ||
|
|
||
| cfg_wasm32! { | ||
| use crate::lp_wallet::mnemonics_wasm_db::{WalletsDb, WalletsDBError}; | ||
| use mm2_core::mm_ctx::from_ctx; | ||
| use mm2_db::indexed_db::{ConstructibleDb, DbLocked, InitDbResult}; | ||
| use mnemonics_wasm_db::{read_encrypted_passphrase_if_available, save_encrypted_passphrase}; | ||
| use mnemonics_wasm_db::{read_all_wallet_names, read_encrypted_passphrase_if_available, save_encrypted_passphrase}; | ||
| use std::sync::Arc; | ||
|
|
||
| type WalletsDbLocked<'a> = DbLocked<'a, WalletsDb>; | ||
| } | ||
|
|
||
| cfg_native! { | ||
| use mnemonics_storage::{read_encrypted_passphrase_if_available, save_encrypted_passphrase, WalletsStorageError}; | ||
| use mnemonics_storage::{read_all_wallet_names, read_encrypted_passphrase_if_available, save_encrypted_passphrase, WalletsStorageError}; | ||
| } | ||
|
|
||
| #[cfg(not(target_arch = "wasm32"))] mod mnemonics_storage; | ||
|
|
@@ -499,3 +500,53 @@ pub async fn get_mnemonic_rpc(ctx: MmArc, req: GetMnemonicRequest) -> MmResult<G | |
| }, | ||
| } | ||
| } | ||
|
|
||
| /// The response to `get_wallet_names_rpc`, returns all created wallet names and the currently activated wallet name. | ||
| #[derive(Serialize)] | ||
| pub struct GetWalletNamesResponse { | ||
| wallet_names: Vec<String>, | ||
| activated_wallet: Option<String>, | ||
| } | ||
|
|
||
| #[derive(Debug, Display, Serialize, SerializeErrorType)] | ||
| #[serde(tag = "error_type", content = "error_data")] | ||
| pub enum GetWalletsError { | ||
| #[display(fmt = "Wallets storage error: {}", _0)] | ||
| WalletsStorageError(String), | ||
| #[display(fmt = "Internal error: {}", _0)] | ||
| Internal(String), | ||
| } | ||
|
|
||
| impl HttpStatusCode for GetWalletsError { | ||
| fn status_code(&self) -> StatusCode { | ||
| match self { | ||
| GetWalletsError::WalletsStorageError(_) | GetWalletsError::Internal(_) => StatusCode::INTERNAL_SERVER_ERROR, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(not(target_arch = "wasm32"))] | ||
| impl From<WalletsStorageError> for GetWalletsError { | ||
| fn from(e: WalletsStorageError) -> Self { GetWalletsError::WalletsStorageError(e.to_string()) } | ||
| } | ||
|
|
||
| #[cfg(target_arch = "wasm32")] | ||
| impl From<WalletsDBError> for GetWalletsError { | ||
| fn from(e: WalletsDBError) -> Self { GetWalletsError::WalletsStorageError(e.to_string()) } | ||
| } | ||
|
|
||
| /// Retrieves all created wallets and the currently activated wallet. | ||
| pub async fn get_wallet_names_rpc(ctx: MmArc, _req: Json) -> MmResult<GetWalletNamesResponse, GetWalletsError> { | ||
| // We want to return wallet names in the same order for both native and wasm32 targets. | ||
| let wallets = read_all_wallet_names(&ctx).await?.sorted().collect(); | ||
| // Note: `ok_or` is used here on `Constructible<Option<String>>` to handle the case where the wallet name is not set. | ||
| // `wallet_name` can be `None` in the case of no-login mode. | ||
| let activated_wallet = ctx.wallet_name.ok_or(GetWalletsError::Internal( | ||
| "`wallet_name` not initialized yet!".to_string(), | ||
| ))?; | ||
|
Comment on lines
+544
to
+546
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you explain please, how you get Ok result in non-login mode with null No-Login Mode Response from pr info
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, exactly There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just ran into the same issue :) Maybe we should make it more explicit with some helpful comments? (Unresolving the topic to make it visible - feel free to re-resolve the topic once you see it)
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added comment here 704305d |
||
|
|
||
| Ok(GetWalletNamesResponse { | ||
| wallet_names: wallets, | ||
| activated_wallet: activated_wallet.clone(), | ||
| }) | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.