Question: How to correctly persist client sign in data to local storage on a web client #1062
-
Hello, I'm attempting to create a nostr client in Rust. So I wanted to ask, could anybody recommend the best way to serialize the Client sign in data to local storage? My attempt so far have been to create a giant struct as follows: #[derive(Serialize, Deserialize, Clone, Debug, Default)]
#[serde(tag = "kind")]
pub enum SignerState {
Nip07,
Nip46 {
remote_pubkey: String,
relays: Vec<String>,
app_secret_key: String,
bunker_uri: String,
},
LocalSecret {
secret_key: String,
},
PublicOnly {
public_key: String,
},
#[default]
None,
} P.S. Thank you for this amazing set of libraries! |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 5 replies
-
I see that in the
The |
Beta Was this translation helpful? Give feedback.
-
Thanks for the quick response! pub async fn reinit_client_from_signer(&mut self) -> Result<(), Box<dyn Error>> {
match &self.signer {
SignerState::LocalSecret { secret_key } => {
let keys = Keys::parse(secret_key).unwrap();
self.client.set_signer(keys.clone()).await;
self.public_key = keys.public_key().to_hex();
}
SignerState::Nip46 {
remote_pubkey,
app_secret_key,
bunker_uri,
relays,
} => {
trace!("Restoring NIP-46 signer from cache");
let app_keys = Keys::parse(app_secret_key).unwrap();
let nc = nostr_connect::client::NostrConnect::new(
nostr_sdk::nips::nip46::NostrConnectURI::parse(bunker_uri).unwrap(),
app_keys,
Duration::from_secs(5),
None,
)
.unwrap();
self.client.set_signer(nc).await;
self.public_key = remote_pubkey.clone();
let _ = self.client.add_relay("My relay").await;
self.client.connect().await;
}
SignerState::Nip07 | SignerState::PublicOnly { .. } | SignerState::None => {
trace!("no local signer to restore");
}
}
Ok(())
}
} This function is called after fetching from local storage: pub fn fetch_from_cache(&mut self) {
let cached = window()
.local_storage()
.ok()
.flatten()
.and_then(|storage| {
storage
.get_item(ACCOUNT_STORAGE_KEY)
.ok()
.flatten()
.and_then(|value| serde_json::from_str::<Self>(&value).ok())
})
.unwrap_or_default();
self.public_key = cached.public_key;
self.signer = cached.signer;
} |
Beta Was this translation helpful? Give feedback.
-
By the way I am using nsec.app to test this functionality. |
Beta Was this translation helpful? Give feedback.
-
It occurred to me that my problem may have to do with how I am handling the Client on the leptos side of things (the signal is hard to manipulate through asynchronous functions). |
Beta Was this translation helpful? Give feedback.
The relays in the bunker URI are added automatically to the
NostrConnect
crate. They are not added to theClient
, since are not used there. Also the connection/bootstrap happens automatically for theNostrConnect
client, on the first interaction with it.