Skip to content

Commit

Permalink
Fix monitor parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
benthecarman committed Sep 19, 2023
1 parent 1f224f7 commit 52e1300
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
8 changes: 6 additions & 2 deletions mutiny-core/src/ldkstorage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -637,8 +637,10 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner, S: MutinyStorage> Persist<Chann
funding_txo.txid.to_hex(),
funding_txo.index
);
// safely convert u64 to u32
let update_id = monitor.get_latest_update_id();
debug_assert!(update_id == utils::get_monitor_version(monitor.encode()));

// safely convert u64 to u32
let version = if update_id >= u32::MAX as u64 {
u32::MAX
} else {
Expand All @@ -663,8 +665,10 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner, S: MutinyStorage> Persist<Chann
funding_txo.txid.to_hex(),
funding_txo.index
);
// safely convert u64 to u32
let update_id = monitor.get_latest_update_id();
debug_assert!(update_id == utils::get_monitor_version(monitor.encode()));

// safely convert u64 to u32
let version = if update_id >= u32::MAX as u64 {
u32::MAX
} else {
Expand Down
8 changes: 8 additions & 0 deletions mutiny-core/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>) -> u64 {
// first two bytes are the version
// next 8 bytes are the version number
u64::from_be_bytes(bytes[2..10].try_into().unwrap())
}
7 changes: 3 additions & 4 deletions mutiny-wasm/src/indexed_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ impl IndexedDbStorage {
match current.get::<Vec<u8>>(&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())?;
Expand Down Expand Up @@ -367,9 +367,8 @@ impl IndexedDbStorage {
// we can get versions from monitors, so we should compare
match current.get::<Vec<u8>>(&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?;
Expand Down

0 comments on commit 52e1300

Please sign in to comment.