Skip to content

Commit

Permalink
Integrate VSS into wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
benthecarman committed Jul 8, 2023
1 parent ae11a3b commit f370ffc
Show file tree
Hide file tree
Showing 18 changed files with 366 additions and 232 deletions.
4 changes: 2 additions & 2 deletions mutiny-core/src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ struct CustomClaims {
sub: String,
}

pub(crate) struct MutinyAuthClient {
auth: AuthManager,
pub struct MutinyAuthClient {
pub auth: AuthManager,
lnurl_client: Arc<LnUrlClient>,
url: String,
http_client: Client,
Expand Down
12 changes: 6 additions & 6 deletions mutiny-core/src/gossip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ fn write_gossip_data(
network_graph: &NetworkGraph,
) -> Result<(), MutinyError> {
// Save the last sync timestamp
storage.set_data(GOSSIP_SYNC_TIME_KEY, last_sync_timestamp)?;
storage.set_data(GOSSIP_SYNC_TIME_KEY, last_sync_timestamp, None)?;

// Save the network graph
storage.set_data(NETWORK_GRAPH_KEY, network_graph.encode().to_hex())?;
storage.set_data(NETWORK_GRAPH_KEY, network_graph.encode().to_hex(), None)?;

Ok(())
}
Expand Down Expand Up @@ -362,7 +362,7 @@ pub(crate) fn save_peer_connection_info(
},
};

storage.set_data(key, new_info)?;
storage.set_data(key, new_info, None)?;
Ok(())
}

Expand All @@ -388,7 +388,7 @@ pub(crate) fn set_peer_label(
},
};

storage.set_data(key, new_info)?;
storage.set_data(key, new_info, None)?;
Ok(())
}

Expand All @@ -406,7 +406,7 @@ pub(crate) fn delete_peer_info(
if current.nodes.is_empty() {
storage.delete(&[key])?;
} else {
storage.set_data(key, current)?;
storage.set_data(key, current, None)?;
}
}

Expand All @@ -426,7 +426,7 @@ pub(crate) fn save_ln_peer_info(

// if the new info is different than the current info, we should to save it
if !current.is_some_and(|c| c == new_info) {
storage.set_data(key, new_info)?;
storage.set_data(key, new_info, None)?;
}

Ok(())
Expand Down
14 changes: 7 additions & 7 deletions mutiny-core/src/keymanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,12 @@ pub fn generate_seed(num_words: u8) -> Result<Mnemonic, MutinyError> {
// key secret will be derived from `m/0'`.
pub(crate) fn create_keys_manager<S: MutinyStorage>(
wallet: Arc<OnChainWallet<S>>,
mnemonic: &Mnemonic,
xprivkey: ExtendedPrivKey,
child_index: u32,
logger: Arc<MutinyLogger>,
) -> Result<PhantomKeysManager<S>, MutinyError> {
let context = Secp256k1::new();

let seed = mnemonic.to_seed("");
let xprivkey = ExtendedPrivKey::new_master(wallet.network, &seed)?;
let shared_key = xprivkey.derive_priv(
&context,
&DerivationPath::from(vec![ChildNumber::from_hardened_idx(0)?]),
Expand Down Expand Up @@ -250,6 +248,7 @@ mod tests {
use crate::onchain::OnChainWallet;
use crate::storage::MemoryStorage;
use bip39::Mnemonic;
use bitcoin::util::bip32::ExtendedPrivKey;
use bitcoin::Network;
use esplora_client::Builder;
use std::str::FromStr;
Expand Down Expand Up @@ -277,10 +276,11 @@ mod tests {
logger.clone(),
));
let stop = Arc::new(AtomicBool::new(false));
let xpriv = ExtendedPrivKey::new_master(Network::Testnet, &mnemonic.to_seed("")).unwrap();

let wallet = Arc::new(
OnChainWallet::new(
&mnemonic,
xpriv,
db,
Network::Testnet,
esplora,
Expand All @@ -291,21 +291,21 @@ mod tests {
.unwrap(),
);

let km = create_keys_manager(wallet.clone(), &mnemonic, 1, logger.clone()).unwrap();
let km = create_keys_manager(wallet.clone(), xpriv, 1, logger.clone()).unwrap();
let pubkey = pubkey_from_keys_manager(&km);
assert_eq!(
"02cae09cf2c8842ace44068a5bf3117a494ebbf69a99e79712483c36f97cdb7b54",
pubkey.to_string()
);

let km = create_keys_manager(wallet.clone(), &mnemonic, 2, logger.clone()).unwrap();
let km = create_keys_manager(wallet.clone(), xpriv, 2, logger.clone()).unwrap();
let second_pubkey = pubkey_from_keys_manager(&km);
assert_eq!(
"03fcc9eaaf0b84946ea7935e3bc4f2b498893c2f53e5d2994d6877d149601ce553",
second_pubkey.to_string()
);

let km = create_keys_manager(wallet, &mnemonic, 2, logger).unwrap();
let km = create_keys_manager(wallet, xpriv, 2, logger).unwrap();
let second_pubkey_again = pubkey_from_keys_manager(&km);

assert_eq!(second_pubkey, second_pubkey_again);
Expand Down
42 changes: 22 additions & 20 deletions mutiny-core/src/labels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ impl<S: MutinyStorage> LabelStorage for S {
// update the labels map
let mut address_labels = self.get_address_labels()?;
address_labels.insert(address.to_string(), labels.clone());
self.set_data(ADDRESS_LABELS_MAP_KEY, address_labels)?;
self.set_data(ADDRESS_LABELS_MAP_KEY, address_labels, None)?;

// update the label items
let now = crate::utils::now().as_secs();
Expand All @@ -138,7 +138,7 @@ impl<S: MutinyStorage> LabelStorage for S {
// Update the last used timestamp
label_item.last_used_time = now;

self.set_data(key, label_item)?;
self.set_data(key, label_item, None)?;
}
None => {
// Create a new label item
Expand All @@ -147,7 +147,7 @@ impl<S: MutinyStorage> LabelStorage for S {
invoices: vec![],
last_used_time: now,
};
self.set_data(key, label_item)?;
self.set_data(key, label_item, None)?;
}
}
}
Expand All @@ -159,7 +159,7 @@ impl<S: MutinyStorage> LabelStorage for S {
// update the labels map
let mut invoice_labels = self.get_invoice_labels()?;
invoice_labels.insert(invoice.clone(), labels.clone());
self.set_data(INVOICE_LABELS_MAP_KEY, invoice_labels)?;
self.set_data(INVOICE_LABELS_MAP_KEY, invoice_labels, None)?;

// update the label items
let now = crate::utils::now().as_secs();
Expand All @@ -176,7 +176,7 @@ impl<S: MutinyStorage> LabelStorage for S {
// Update the last used timestamp
label_item.last_used_time = now;

self.set_data(key, label_item)?;
self.set_data(key, label_item, None)?;
}
None => {
// Create a new label item
Expand All @@ -185,7 +185,7 @@ impl<S: MutinyStorage> LabelStorage for S {
invoices: vec![invoice.to_string()],
last_used_time: now,
};
self.set_data(key, label_item)?;
self.set_data(key, label_item, None)?;
}
}
}
Expand Down Expand Up @@ -224,7 +224,7 @@ impl<S: MutinyStorage> LabelStorage for S {
// convert label into a uuid for uniqueness
let id = Uuid::new_v4().to_string();
// create label item
self.set_data(get_label_item_key(&id), current)?;
self.set_data(get_label_item_key(&id), current, None)?;

// replace label in address_labels with new uuid
let addr_labels = self.get_address_labels()?;
Expand All @@ -241,7 +241,7 @@ impl<S: MutinyStorage> LabelStorage for S {
updated.insert(addr, new_labels);
}
}
self.set_data(ADDRESS_LABELS_MAP_KEY, updated)?;
self.set_data(ADDRESS_LABELS_MAP_KEY, updated, None)?;

// replace label in invoice_labels with new uuid
let invoice_labels = self.get_invoice_labels()?;
Expand All @@ -258,11 +258,11 @@ impl<S: MutinyStorage> LabelStorage for S {
updated.insert(inv, new_labels);
}
}
self.set_data(INVOICE_LABELS_MAP_KEY, updated)?;
self.set_data(INVOICE_LABELS_MAP_KEY, updated, None)?;

// create the contact
let key = get_contact_key(&id);
self.set_data(key, contact)?;
self.set_data(key, contact, None)?;

// delete old label item
self.delete(&[get_label_item_key(&label)])?;
Expand All @@ -276,28 +276,28 @@ impl<S: MutinyStorage> LabelStorage for S {
// generate a uuid, this will be the "label" that we use to store the contact
let id = Uuid::new_v4().to_string();
let key = get_contact_key(&id);
self.set_data(key, contact)?;
self.set_data(key, contact, None)?;

let key = get_label_item_key(&id);
let label_item = LabelItem {
last_used_time: crate::utils::now().as_secs(),
..Default::default()
};
self.set_data(key, label_item)?;
self.set_data(key, label_item, None)?;
Ok(id)
}

fn archive_contact(&self, id: impl AsRef<str>) -> Result<(), MutinyError> {
let contact = self.get_contact(&id)?;
if let Some(mut contact) = contact {
contact.archived = Some(true);
self.set(get_contact_key(&id), contact)?;
self.set(get_contact_key(&id), contact, None)?;
}
Ok(())
}

fn edit_contact(&self, id: impl AsRef<str>, contact: Contact) -> Result<(), MutinyError> {
self.set(get_contact_key(&id), contact)
self.set(get_contact_key(&id), contact, None)
}

fn get_tag_items(&self) -> Result<Vec<TagItem>, MutinyError> {
Expand Down Expand Up @@ -509,7 +509,7 @@ mod tests {
let storage = MemoryStorage::default();
let labels_map = create_test_address_labels_map();
storage
.set_data(ADDRESS_LABELS_MAP_KEY, labels_map.clone())
.set_data(ADDRESS_LABELS_MAP_KEY, labels_map.clone(), None)
.unwrap();

let result = storage.get_address_labels();
Expand All @@ -524,7 +524,7 @@ mod tests {
let storage = MemoryStorage::default();
let labels_map = create_test_invoice_labels_map();
storage
.set_data(INVOICE_LABELS_MAP_KEY, labels_map.clone())
.set_data(INVOICE_LABELS_MAP_KEY, labels_map.clone(), None)
.unwrap();

let result = storage.get_invoice_labels();
Expand All @@ -541,7 +541,7 @@ mod tests {
let labels = create_test_labels();
for (label, label_item) in labels.clone() {
storage
.set_data(get_label_item_key(label), label_item)
.set_data(get_label_item_key(label), label_item, None)
.unwrap();
}

Expand All @@ -566,7 +566,7 @@ mod tests {
let labels = create_test_labels();
for (label, label_item) in labels.clone() {
storage
.set_data(get_label_item_key(label), label_item)
.set_data(get_label_item_key(label), label_item, None)
.unwrap();
}

Expand Down Expand Up @@ -618,7 +618,9 @@ mod tests {

let contacts = create_test_contacts();
for (id, contact) in contacts.clone() {
storage.set_data(get_contact_key(id), contact).unwrap();
storage
.set_data(get_contact_key(id), contact, None)
.unwrap();
}

let result = storage.get_contacts().unwrap();
Expand Down Expand Up @@ -796,7 +798,7 @@ mod tests {
let labels = create_test_labels();
for (label, label_item) in labels {
storage
.set_data(get_label_item_key(label.clone()), label_item.clone())
.set_data(get_label_item_key(label.clone()), label_item.clone(), None)
.unwrap();
expected_tag_items.push(TagItem::Label((label, label_item)));
}
Expand Down
Loading

0 comments on commit f370ffc

Please sign in to comment.