11use anyhow:: anyhow;
2- use gloo_storage:: { LocalStorage , Storage } ;
32use gloo_utils:: format:: JsValueSerdeExt ;
43use lightning:: util:: logger:: Logger ;
54use lightning:: { log_debug, log_error} ;
@@ -204,26 +203,6 @@ impl IndexedDbStorage {
204203 map. set ( key, json) ?;
205204 }
206205
207- // get the local storage data, this should take priority if it is being used
208- log_debug ! ( logger, "Reading from local storage" ) ;
209- let local_storage = LocalStorage :: raw ( ) ;
210- let length = LocalStorage :: length ( ) ;
211- for index in 0 ..length {
212- let key_opt: Option < String > = local_storage. key ( index) . unwrap ( ) ;
213-
214- if let Some ( key) = key_opt {
215- // only add to the map if it is a key we expect
216- // this is to prevent any unexpected data from being added to the map
217- // from either malicious 3rd party or a previous version of the wallet
218- if write_to_local_storage ( & key) {
219- // compare versions between local storage and indexed db storage
220- if let Some ( ( key, value) ) = Self :: handle_local_storage_key ( key, & map, logger) ? {
221- map. set_data ( key, value, None ) ?;
222- }
223- }
224- }
225- }
226-
227206 match vss {
228207 None => {
229208 let final_map = map. memory . read ( ) . unwrap ( ) ;
@@ -248,76 +227,6 @@ impl IndexedDbStorage {
248227 }
249228 }
250229
251- fn handle_local_storage_key (
252- key : String ,
253- current : & MemoryStorage ,
254- logger : & MutinyLogger ,
255- ) -> Result < Option < ( String , Value ) > , MutinyError > {
256- if key. starts_with ( MONITORS_PREFIX_KEY ) {
257- // we can get versions from monitors, so we should compare
258- match current. get :: < Vec < u8 > > ( & key) ? {
259- Some ( bytes) => {
260- // check first byte is 1, then take u64 from next 8 bytes
261- let current_version = utils:: get_monitor_version ( bytes) ;
262-
263- let obj: Value = LocalStorage :: get ( & key) . unwrap ( ) ;
264- let value = decrypt_value ( & key, obj, current. password ( ) ) ?;
265- if let Ok ( local_bytes) = serde_json:: from_value :: < Vec < u8 > > ( value. clone ( ) ) {
266- let local_version = utils:: get_monitor_version ( local_bytes) ;
267-
268- // if the current version is less than the version from local storage
269- // then we want to use the local storage version
270- if current_version < local_version {
271- log_debug ! (
272- logger,
273- "Using local storage key {key} with version {}" ,
274- local_version
275- ) ;
276- return Ok ( Some ( ( key, value) ) ) ;
277- }
278- }
279- }
280- None => {
281- let value: Value = LocalStorage :: get ( & key) . unwrap ( ) ;
282- return Ok ( Some ( ( key, value) ) ) ;
283- }
284- }
285- } else if key. starts_with ( CHANNEL_MANAGER_KEY ) {
286- // we can get versions from channel manager, so we should compare
287- match current. get_data :: < VersionedValue > ( & key) {
288- Ok ( Some ( local) ) => {
289- let obj: Value = LocalStorage :: get ( & key) . unwrap ( ) ;
290- let value = decrypt_value ( & key, obj, current. password ( ) ) ?;
291-
292- // if the current version is less than the version from local storage
293- // then we want to use the local storage version
294- if let Ok ( v) = serde_json:: from_value :: < VersionedValue > ( value. clone ( ) ) {
295- if v. version > local. version {
296- log_debug ! (
297- logger,
298- "Using local storage key {key} with version {}" ,
299- v. version
300- ) ;
301- return Ok ( Some ( ( key, value) ) ) ;
302- }
303- }
304- }
305- Ok ( None ) => {
306- let obj: Value = LocalStorage :: get ( & key) . unwrap ( ) ;
307- let value = decrypt_value ( & key, obj, current. password ( ) ) ?;
308- if serde_json:: from_value :: < VersionedValue > ( value. clone ( ) ) . is_ok ( ) {
309- return Ok ( Some ( ( key, value) ) ) ;
310- }
311- }
312- Err ( _) => return Err ( MutinyError :: IncorrectPassword ) ,
313- }
314- }
315-
316- log_debug ! ( logger, "Skipping local storage key {key}" ) ;
317-
318- Ok ( None )
319- }
320-
321230 async fn handle_vss_key (
322231 kv : KeyVersion ,
323232 vss : & MutinyVssClient ,
@@ -482,18 +391,6 @@ fn used_once(key: &str) -> bool {
482391 }
483392}
484393
485- /// To help prevent force closes we save to local storage as well as indexed db.
486- /// This is because indexed db is not always reliable.
487- ///
488- /// We need to do this for the channel manager and channel monitors.
489- fn write_to_local_storage ( key : & str ) -> bool {
490- match key {
491- str if str. starts_with ( CHANNEL_MANAGER_KEY ) => true ,
492- str if str. starts_with ( MONITORS_PREFIX_KEY ) => true ,
493- _ => false ,
494- }
495- }
496-
497394impl MutinyStorage for IndexedDbStorage {
498395 fn password ( & self ) -> Option < & str > {
499396 self . password . as_deref ( )
@@ -526,15 +423,6 @@ impl MutinyStorage for IndexedDbStorage {
526423 } ;
527424 } ) ;
528425
529- // Some values we want to write to local storage as well as indexed db
530- if write_to_local_storage ( & key) {
531- LocalStorage :: set ( & key, & data) . map_err ( |e| {
532- MutinyError :: write_err ( MutinyStorageError :: Other ( anyhow ! (
533- "Failed to write to local storage: {e}"
534- ) ) )
535- } ) ?;
536- }
537-
538426 // some values only are read once, so we don't need to write them to memory,
539427 // just need them in indexed db for next time
540428 if !used_once ( key. as_ref ( ) ) {
@@ -559,15 +447,6 @@ impl MutinyStorage for IndexedDbStorage {
559447
560448 Self :: save_to_indexed_db ( & self . indexed_db , & key, & data) . await ?;
561449
562- // Some values we want to write to local storage as well as indexed db
563- if write_to_local_storage ( & key) {
564- LocalStorage :: set ( & key, & data) . map_err ( |e| {
565- MutinyError :: write_err ( MutinyStorageError :: Other ( anyhow ! (
566- "Failed to write to local storage: {e}"
567- ) ) )
568- } ) ?;
569- }
570-
571450 // some values only are read once, so we don't need to write them to memory,
572451 // just need them in indexed db for next time
573452 if !used_once ( key. as_ref ( ) ) {
@@ -633,11 +512,6 @@ impl MutinyStorage for IndexedDbStorage {
633512 . map_err ( |e| MutinyError :: write_err ( e. into ( ) ) ) ?;
634513
635514 for key in keys {
636- // Some values we want to write to local storage as well as indexed db
637- // we should delete them from local storage as well
638- if write_to_local_storage ( & key) {
639- LocalStorage :: delete ( & key)
640- }
641515 map. remove ( & key) ;
642516 }
643517
@@ -755,30 +629,24 @@ impl MutinyStorage for IndexedDbStorage {
755629 . await
756630 . map_err ( |e| MutinyError :: write_err ( anyhow ! ( "Failed clear indexed db: {e}" ) . into ( ) ) ) ?;
757631
758- // We use some localstorage right now for ensuring channel data
759- LocalStorage :: clear ( ) ;
760-
761632 Ok ( ( ) )
762633 }
763634}
764635
765636#[ cfg( test) ]
766637mod tests {
767638 use super :: * ;
768- use crate :: indexed_db:: { IndexedDbStorage , WALLET_OBJECT_STORE_NAME } ;
639+ use crate :: indexed_db:: IndexedDbStorage ;
769640 use crate :: utils:: sleep;
770641 use crate :: utils:: test:: log;
771642 use bip39:: Mnemonic ;
772643 use bitcoin:: hashes:: hex:: ToHex ;
773- use gloo_storage:: { LocalStorage , Storage } ;
774644 use mutiny_core:: storage:: MutinyStorage ;
775- use mutiny_core:: test_utils:: { MANAGER_BYTES , MONITOR_VERSION_HIGHER , MONITOR_VERSION_LOWER } ;
645+ use mutiny_core:: test_utils:: MANAGER_BYTES ;
776646 use mutiny_core:: { encrypt:: encryption_key_from_pass, logging:: MutinyLogger } ;
777- use rexie:: TransactionMode ;
778647 use serde_json:: json;
779648 use std:: str:: FromStr ;
780649 use std:: sync:: Arc ;
781- use wasm_bindgen:: JsValue ;
782650 use wasm_bindgen_test:: { wasm_bindgen_test as test, wasm_bindgen_test_configure} ;
783651
784652 wasm_bindgen_test_configure ! ( run_in_browser) ;
@@ -944,85 +812,6 @@ mod tests {
944812 IndexedDbStorage :: clear ( ) . await . unwrap ( ) ;
945813 }
946814
947- async fn compare_local_storage_versions (
948- test_name : & str ,
949- local_storage : Vec < u8 > ,
950- indexed_db : Vec < u8 > ,
951- ) -> Vec < u8 > {
952- let key = format ! ( "{MONITORS_PREFIX_KEY}test_{test_name}" ) ;
953- // set in local storage
954- LocalStorage :: set ( & key, local_storage) . unwrap ( ) ;
955- // set in indexed db
956- let rexie = IndexedDbStorage :: build_indexed_db_database ( ) . await . unwrap ( ) ;
957- let tx = rexie
958- . transaction ( & [ WALLET_OBJECT_STORE_NAME ] , TransactionMode :: ReadWrite )
959- . unwrap ( ) ;
960- let store = tx. store ( WALLET_OBJECT_STORE_NAME ) . unwrap ( ) ;
961- store
962- . put (
963- & JsValue :: from_serde ( & indexed_db) . unwrap ( ) ,
964- Some ( & JsValue :: from ( & key) ) ,
965- )
966- . await
967- . unwrap ( ) ;
968-
969- tx. done ( ) . await . unwrap ( ) ;
970-
971- let logger = Arc :: new ( MutinyLogger :: default ( ) ) ;
972- let storage = IndexedDbStorage :: new ( None , None , None , logger)
973- . await
974- . unwrap ( ) ;
975-
976- let bytes: Vec < u8 > = storage. get ( & key) . unwrap ( ) . unwrap ( ) ;
977-
978- // clear the storage to clean up
979- IndexedDbStorage :: clear ( ) . await . unwrap ( ) ;
980-
981- bytes
982- }
983-
984- #[ test]
985- async fn test_local_storage_version_0_indexed_db_version_max ( ) {
986- let test_name = "test_local_storage_version_0_indexed_db_version_max" ;
987- log ! ( "{test_name}" ) ;
988-
989- let bytes = compare_local_storage_versions (
990- test_name,
991- MONITOR_VERSION_LOWER . to_vec ( ) ,
992- MONITOR_VERSION_HIGHER . to_vec ( ) ,
993- )
994- . await ;
995- assert_eq ! ( bytes, MONITOR_VERSION_HIGHER ) ;
996- }
997-
998- #[ test]
999- async fn test_local_storage_version_max_indexed_db_version_0 ( ) {
1000- let test_name = "test_local_storage_version_max_indexed_db_version_0" ;
1001- log ! ( "{test_name}" ) ;
1002-
1003- let bytes = compare_local_storage_versions (
1004- test_name,
1005- MONITOR_VERSION_HIGHER . to_vec ( ) ,
1006- MONITOR_VERSION_LOWER . to_vec ( ) ,
1007- )
1008- . await ;
1009- assert_eq ! ( bytes, MONITOR_VERSION_HIGHER ) ;
1010- }
1011-
1012- #[ test]
1013- async fn test_local_storage_version_max_indexed_db_version_max ( ) {
1014- let test_name = "test_local_storage_version_max_indexed_db_version_max" ;
1015- log ! ( "{test_name}" ) ;
1016-
1017- let bytes = compare_local_storage_versions (
1018- test_name,
1019- MONITOR_VERSION_HIGHER . to_vec ( ) ,
1020- MONITOR_VERSION_HIGHER . to_vec ( ) ,
1021- )
1022- . await ;
1023- assert_eq ! ( bytes, MONITOR_VERSION_HIGHER ) ;
1024- }
1025-
1026815 #[ test]
1027816 async fn test_correct_incorrect_password_error ( ) {
1028817 let test_name = "test_correct_incorrect_password_error" ;
0 commit comments