Skip to content

Commit bddba73

Browse files
committed
break up import ufvk and create account
1 parent 6f7fc6c commit bddba73

File tree

1 file changed

+14
-34
lines changed

1 file changed

+14
-34
lines changed

src/wallet.rs

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -123,43 +123,25 @@ where
123123
let usk = usk_from_seed_str(seed_phrase, account_index, &self.network)?;
124124
let ufvk = usk.to_unified_full_viewing_key();
125125

126-
let birthday = match birthday_height {
127-
Some(height) => height,
128-
None => {
129-
let chain_tip: u32 = self
130-
.client
131-
.get_latest_block(service::ChainSpec::default())
132-
.await?
133-
.into_inner()
134-
.height
135-
.try_into()
136-
.expect("block heights must fit into u32");
137-
chain_tip - 100
138-
}
139-
};
140-
// Construct an `AccountBirthday` for the account's birthday.
141-
let birthday = {
142-
// Fetch the tree state corresponding to the last block prior to the wallet's
143-
// birthday height. NOTE: THIS APPROACH LEAKS THE BIRTHDAY TO THE SERVER!
144-
let request = service::BlockId {
145-
height: (birthday - 1).into(),
146-
..Default::default()
147-
};
148-
let treestate = self.client.get_tree_state(request).await?.into_inner();
149-
AccountBirthday::from_treestate(treestate, None).map_err(|_| Error::BirthdayError)?
150-
};
151-
152-
let _account = self
153-
.db
154-
.import_account_ufvk(&ufvk, &birthday, AccountPurpose::Spending)?;
155-
// TOOD: Make this public on account Ok(account.account_id().to_string())
156-
Ok("0".to_string())
126+
self.import_account_ufvk(&ufvk, birthday_height, AccountPurpose::Spending)
127+
.await
157128
}
158129

159130
pub async fn import_ufvk(
160131
&mut self,
161132
ufvk: &UnifiedFullViewingKey,
162133
birthday_height: Option<u32>,
134+
) -> Result<String, Error> {
135+
self.import_account_ufvk(ufvk, birthday_height, AccountPurpose::ViewOnly)
136+
.await
137+
}
138+
139+
/// Helper method for importing an account directly from a Ufvk or from seed.
140+
async fn import_account_ufvk(
141+
&mut self,
142+
ufvk: &UnifiedFullViewingKey,
143+
birthday_height: Option<u32>,
144+
purpose: AccountPurpose,
163145
) -> Result<String, Error> {
164146
let birthday = match birthday_height {
165147
Some(height) => height,
@@ -187,9 +169,7 @@ where
187169
AccountBirthday::from_treestate(treestate, None).map_err(|_| Error::BirthdayError)?
188170
};
189171

190-
let _account = self
191-
.db
192-
.import_account_ufvk(ufvk, &birthday, AccountPurpose::ViewOnly)?;
172+
let _account = self.db.import_account_ufvk(ufvk, &birthday, purpose)?;
193173

194174
Ok("0".to_string())
195175
}

0 commit comments

Comments
 (0)