Skip to content

Commit c4a97b4

Browse files
committed
sdk-rust: Make load() return Option
Not strictly required, but allows downstream consumers to detect that they need to call `signup()` if `load()` returns `None`
1 parent c2a776c commit c4a97b4

File tree

5 files changed

+196
-70
lines changed

5 files changed

+196
-70
lines changed

app-rs/src/app.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ impl App {
9393
let wallet_user = WalletUser::from_seed(rng, root_seed);
9494

9595
let user_db_config =
96-
WalletUserDbConfig::new(wallet_user.user_pk, env_db_config.clone());
96+
WalletUserDbConfig::new(env_db_config.clone(), wallet_user.user_pk);
9797

9898
// Create fresh wallet
9999
let credentials = CredentialsRef::from(root_seed);
@@ -173,23 +173,37 @@ impl App {
173173
// If there's nothing in the secret store, this must be a fresh install;
174174
// we can just return here.
175175
let root_seed = match maybe_root_seed {
176-
None => return Ok(None),
177176
Some(s) => s,
177+
None => return Ok(None),
178178
};
179179

180180
let wallet_user = WalletUser::from_seed(rng, &root_seed);
181181
let user_db_config =
182-
WalletUserDbConfig::new(wallet_user.user_pk, env_db_config.clone());
182+
WalletUserDbConfig::new(env_db_config.clone(), wallet_user.user_pk);
183183

184-
// Load existing wallet
184+
// Load the wallet.
185185
let credentials = CredentialsRef::from(&root_seed);
186-
let wallet = LexeWallet::load(
186+
let maybe_wallet = LexeWallet::load(
187187
rng,
188188
env_config.clone(),
189189
credentials,
190190
env_db_config.lexe_data_dir().clone(),
191191
)
192192
.context("Failed to build LexeWallet")?;
193+
let wallet = match maybe_wallet {
194+
Some(w) => w,
195+
// This is a weird case: our seed exists but our local data doesn't.
196+
// Maybe the user manually deleted their local data? In any case,
197+
// we don't want to send the user back to the signup / restore
198+
// flows, so just create a fresh wallet instance.
199+
None => LexeWallet::fresh(
200+
rng,
201+
env_config.clone(),
202+
credentials,
203+
env_db_config.lexe_data_dir().clone(),
204+
)
205+
.context("Failed to build fresh LexeWallet")?,
206+
};
193207
let user_config = wallet.user_config().clone();
194208

195209
let app_db =
@@ -222,7 +236,7 @@ impl App {
222236
) -> anyhow::Result<Self> {
223237
let wallet_user = WalletUser::from_seed(rng, root_seed);
224238
let user_db_config =
225-
WalletUserDbConfig::new(wallet_user.user_pk, env_db_config.clone());
239+
WalletUserDbConfig::new(env_db_config.clone(), wallet_user.user_pk);
226240

227241
// Init a fresh LexeWallet
228242
let credentials = CredentialsRef::from(root_seed);

node-client/src/credentials.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub enum Credentials {
3535
}
3636

3737
/// Borrowed credentials required to connect to a user node via mTLS.
38+
#[derive(Copy, Clone)]
3839
pub enum CredentialsRef<'a> {
3940
/// Using a [`RootSeed`]. Ex: app.
4041
RootSeed(&'a RootSeed),

sdk-rust/src/config.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use std::{borrow::Cow, fmt, path::PathBuf, sync::LazyLock};
22

3+
use anyhow::Context;
34
use common::{api::user::UserPk, env::DeployEnv, ln::network::LxNetwork};
5+
use node_client::credentials::CredentialsRef;
46

57
use crate::unstable::provision;
68

@@ -229,7 +231,7 @@ impl WalletEnvDbConfig {
229231
impl WalletUserDbConfig {
230232
/// Construct a new [`WalletUserDbConfig`] from the environment database
231233
/// config and user public key.
232-
pub fn new(user_pk: UserPk, env_db_config: WalletEnvDbConfig) -> Self {
234+
pub fn new(env_db_config: WalletEnvDbConfig, user_pk: UserPk) -> Self {
233235
let user_db_dir = env_db_config.env_db_dir.join(user_pk.to_string());
234236
Self {
235237
env_db_config,
@@ -238,6 +240,20 @@ impl WalletUserDbConfig {
238240
}
239241
}
240242

243+
/// Construct a new [`WalletUserDbConfig`] from credentials and the
244+
/// environment database config.
245+
pub fn from_credentials(
246+
credentials: CredentialsRef<'_>,
247+
env_db_config: WalletEnvDbConfig,
248+
) -> anyhow::Result<Self> {
249+
// Is `Some(_)` if the credentials were created by `node-v0.8.11+`.
250+
let user_pk = credentials.user_pk().context(
251+
"Client credentials are out of date. \
252+
Please create a new one from within the Lexe wallet app.",
253+
)?;
254+
Ok(Self::new(env_db_config, user_pk))
255+
}
256+
241257
/// The environment-level database configuration.
242258
pub fn env_db_config(&self) -> &WalletEnvDbConfig {
243259
&self.env_db_config

sdk-rust/src/unstable/wallet_db.rs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,14 @@ impl WalletDb<DiskFs> {
6666
})
6767
}
6868

69-
/// Load an existing [`WalletDb`] (or create a new one if none exists).
70-
pub fn load(user_db_config: WalletUserDbConfig) -> anyhow::Result<Self> {
69+
/// Load an existing [`WalletDb`]. Returns [`None`] if no local data exists.
70+
pub fn load(
71+
user_db_config: WalletUserDbConfig,
72+
) -> anyhow::Result<Option<Self>> {
73+
if !user_db_config.user_db_dir().exists() {
74+
return Ok(None);
75+
}
76+
7177
let payments_ffs =
7278
DiskFs::create_dir_all(user_db_config.payments_db_dir())
7379
.context("Could not create payments ffs")?;
@@ -100,7 +106,7 @@ impl WalletDb<DiskFs> {
100106
let provision_history = ProvisionHistory::read_from_ffs(&provision_ffs)
101107
.context("Could not read provision history")?;
102108

103-
// Log the latest provisioned enclave
109+
// Log the latest provisioned enclave as of this load.
104110
match provision_history.provisioned.last() {
105111
Some(latest) => info!(
106112
version = %latest.version,
@@ -111,13 +117,29 @@ impl WalletDb<DiskFs> {
111117
None => info!("Empty provision history"),
112118
}
113119

114-
Ok(Self {
120+
Ok(Some(Self {
115121
user_db_config,
116122
payments_db,
117123
payment_sync_lock: tokio::sync::Mutex::new(()),
118124
provision_ffs,
119125
provision_history: Arc::new(Mutex::new(provision_history)),
120-
})
126+
}))
127+
}
128+
129+
/// Load an existing [`WalletDb`], or create a fresh one if none exists.
130+
pub fn load_or_fresh(
131+
user_db_config: WalletUserDbConfig,
132+
) -> anyhow::Result<Self> {
133+
let maybe_db = Self::load(user_db_config.clone())
134+
.context("Failed to load wallet db")?;
135+
136+
let db = match maybe_db {
137+
Some(d) => d,
138+
None => Self::fresh(user_db_config)
139+
.context("Failed to create fresh wallet db")?,
140+
};
141+
142+
Ok(db)
121143
}
122144

123145
/// Get the user database configuration.

0 commit comments

Comments
 (0)