@@ -123,43 +123,25 @@ where
123
123
let usk = usk_from_seed_str ( seed_phrase, account_index, & self . network ) ?;
124
124
let ufvk = usk. to_unified_full_viewing_key ( ) ;
125
125
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
157
128
}
158
129
159
130
pub async fn import_ufvk (
160
131
& mut self ,
161
132
ufvk : & UnifiedFullViewingKey ,
162
133
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 ,
163
145
) -> Result < String , Error > {
164
146
let birthday = match birthday_height {
165
147
Some ( height) => height,
@@ -187,9 +169,7 @@ where
187
169
AccountBirthday :: from_treestate ( treestate, None ) . map_err ( |_| Error :: BirthdayError ) ?
188
170
} ;
189
171
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) ?;
193
173
194
174
Ok ( "0" . to_string ( ) )
195
175
}
0 commit comments