From 52e1300230561f3571ae5466e3c08b563bbf6358 Mon Sep 17 00:00:00 2001 From: benthecarman Date: Tue, 19 Sep 2023 01:33:08 -0500 Subject: [PATCH] Fix monitor parsing --- mutiny-core/src/ldkstorage.rs | 8 ++++++-- mutiny-core/src/utils.rs | 8 ++++++++ mutiny-wasm/src/indexed_db.rs | 7 +++---- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/mutiny-core/src/ldkstorage.rs b/mutiny-core/src/ldkstorage.rs index be7c5eea8..b27e6f1fd 100644 --- a/mutiny-core/src/ldkstorage.rs +++ b/mutiny-core/src/ldkstorage.rs @@ -637,8 +637,10 @@ impl Persist= u32::MAX as u64 { u32::MAX } else { @@ -663,8 +665,10 @@ impl Persist= u32::MAX as u64 { u32::MAX } else { diff --git a/mutiny-core/src/utils.rs b/mutiny-core/src/utils.rs index 16f277614..7bb802bf4 100644 --- a/mutiny-core/src/utils.rs +++ b/mutiny-core/src/utils.rs @@ -170,3 +170,11 @@ where wasm_bindgen_futures::spawn_local(future); } } + +/// Returns the version of a channel monitor from a serialized version +/// of a channel monitor. +pub fn get_monitor_version(bytes: Vec) -> u64 { + // first two bytes are the version + // next 8 bytes are the version number + u64::from_be_bytes(bytes[2..10].try_into().unwrap()) +} diff --git a/mutiny-wasm/src/indexed_db.rs b/mutiny-wasm/src/indexed_db.rs index 5294176de..9b04abda0 100644 --- a/mutiny-wasm/src/indexed_db.rs +++ b/mutiny-wasm/src/indexed_db.rs @@ -252,7 +252,7 @@ impl IndexedDbStorage { match current.get::>(&key)? { Some(bytes) => { // check first byte is 1, then take u64 from next 8 bytes - let current_version = u64::from_be_bytes(bytes[2..10].try_into().unwrap()); + let current_version = utils::get_monitor_version(bytes); let obj: Value = LocalStorage::get(&key).unwrap(); let value = decrypt_value(&key, obj, current.password())?; @@ -367,9 +367,8 @@ impl IndexedDbStorage { // we can get versions from monitors, so we should compare match current.get::>(&kv.key)? { Some(bytes) => { - // check first byte is 1, then take u64 from next 8 bytes - let current_version = - u64::from_be_bytes(bytes[2..10].try_into().unwrap()); + let current_version = utils::get_monitor_version(bytes); + // if the current version is less than the version from vss, then we want to use the vss version if current_version < kv.version as u64 { let obj = vss.get_object(&kv.key).await?;