Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store encrypted wallet keychain to VSS #67

Merged
merged 2 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 19 additions & 35 deletions mutiny-core/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{event::HTLCStatus, MutinyInvoice};
use crate::{labels::LabelStorage, TransactionDetails};
use async_trait::async_trait;
use bdk_chain::Merge;
use bdk_wallet::ChangeSet;
pub use bdk_wallet::ChangeSet;
use bip39::Mnemonic;
use bitcoin::hashes::Hash;
use bitcoin::Txid;
Expand Down Expand Up @@ -72,6 +72,7 @@ impl From<DelayedKeyValueItem> for VssKeyValueItem {
fn needs_encryption(key: &str) -> bool {
match key {
MNEMONIC_KEY => true,
KEYCHAIN_STORE_KEY => true,
str if str.starts_with(CHANNEL_MANAGER_KEY) => true,
_ => false,
}
Expand Down Expand Up @@ -520,18 +521,30 @@ pub trait MutinyStorage: Clone + Sized + Send + Sync + 'static {
return Ok(());
}

match self.get_data::<ChangeSet>(KEYCHAIN_STORE_KEY)? {
let version = now().as_secs() as u32;
let value = match self.read_changes()? {
Some(mut keychain_store) => {
keychain_store.merge(changeset.clone());
self.write_data(KEYCHAIN_STORE_KEY.to_string(), keychain_store, None)
let value = serde_json::to_value(keychain_store)?;
VersionedValue { value, version }
}
None => self.write_data(KEYCHAIN_STORE_KEY.to_string(), changeset, None),
}
None => {
let value = serde_json::to_value(changeset)?;
VersionedValue { value, version }
}
};
self.write_data(KEYCHAIN_STORE_KEY.to_string(), value, Some(version))
}

/// Read Wallet changeset
fn read_changes(&self) -> Result<Option<ChangeSet>, MutinyError> {
self.get_data(KEYCHAIN_STORE_KEY)
match self.get_data::<VersionedValue>(KEYCHAIN_STORE_KEY)? {
Some(versioned) => {
let changeset = serde_json::from_value(versioned.value)?;
Ok(Some(changeset))
}
None => Ok(None),
}
}

/// Spawn background task to run db tasks
Expand Down Expand Up @@ -1023,35 +1036,6 @@ pub(crate) fn list_payment_info<S: MutinyStorage>(
#[derive(Clone)]
pub struct OnChainStorage<S: MutinyStorage>(pub(crate) S);

// impl<K, S: MutinyStorage> PersistBackend<K> for OnChainStorage<S>
// where
// K: Default + Clone + Append + serde::Serialize + serde::de::DeserializeOwned,
// {
// type WriteError = MutinyError;
// type LoadError = MutinyError;

// fn write_changes(&mut self, changeset: &K) -> Result<(), Self::WriteError> {
// if changeset.is_empty() {
// return Ok(());
// }

// match self.0.get_data::<K>(KEYCHAIN_STORE_KEY)? {
// Some(mut keychain_store) => {
// keychain_store.append(changeset.clone());
// self.0
// .set_data(KEYCHAIN_STORE_KEY.to_string(), keychain_store, None)
// }
// None => self
// .0
// .set_data(KEYCHAIN_STORE_KEY.to_string(), changeset, None),
// }
// }

// fn load_from_persistence(&mut self) -> Result<Option<K>, Self::LoadError> {
// self.0.get_data(KEYCHAIN_STORE_KEY)
// }
// }

pub(crate) fn get_payment_hash_from_key<'a>(key: &'a str, prefix: &str) -> &'a str {
key.trim_start_matches(prefix)
.splitn(2, '_') // To support the old format that had `_{node_id}` at the end
Expand Down
14 changes: 14 additions & 0 deletions mutiny-wasm/src/indexed_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,20 @@ impl IndexedDbStorage {
let obj = vss.get_object(&kv.key).await?;
return Ok(Some((kv.key, obj.value)));
}
KEYCHAIN_STORE_KEY => match current.get_data::<VersionedValue>(&kv.key)? {
EthanYuan marked this conversation as resolved.
Show resolved Hide resolved
Some(local) => {
if local.version < kv.version {
let obj = vss.get_object(&kv.key).await?;
if serde_json::from_value::<ChangeSet>(obj.value.clone()).is_ok() {
return Ok(Some((kv.key, obj.value)));
}
}
}
None => {
let obj = vss.get_object(&kv.key).await?;
return Ok(Some((kv.key, obj.value)));
}
},
key => {
if key.starts_with(MONITORS_PREFIX_KEY) {
// we can get versions from monitors, so we should compare
Expand Down
Loading