Skip to content

Commit

Permalink
wip - address book & transaction record metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
aspect committed Jul 18, 2023
1 parent 90f14c6 commit 2106425
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 7 deletions.
16 changes: 14 additions & 2 deletions wallet/core/src/storage/local/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct Cache {
pub metadata: Collection<AccountId, Metadata>,
pub transaction_records: Collection<TransactionRecordId, TransactionRecord>,
pub transaction_metadata: HashMap<TransactionRecordId, TransactionMetadata>,
pub address_book: Vec<AddressBookEntry>,
}

impl TryFrom<(Wallet, &Secret)> for Cache {
Expand All @@ -33,8 +34,18 @@ impl TryFrom<(Wallet, &Secret)> for Cache {
let transaction_records: Collection<TransactionRecordId, TransactionRecord> = payload.0.transaction_records.try_into()?;
let transaction_metadata: HashMap<TransactionRecordId, TransactionMetadata> =
payload.0.transaction_metadata.into_iter().collect();
let address_book = payload.0.address_book.into_iter().collect();

Ok(Cache { user_hint, prv_key_data, prv_key_data_info, accounts, metadata, transaction_records, transaction_metadata })
Ok(Cache {
user_hint,
prv_key_data,
prv_key_data_info,
accounts,
metadata,
address_book,
transaction_records,
transaction_metadata,
})
}
}

Expand All @@ -48,7 +59,8 @@ impl TryFrom<(&Cache, &Secret)> for Wallet {
let metadata: Vec<Metadata> = (&cache.metadata).try_into()?;
let transaction_records: Vec<TransactionRecord> = (&cache.transaction_records).try_into()?;
let transaction_metadata = cache.transaction_metadata.clone();
let payload = Payload { prv_key_data, accounts, transaction_records, transaction_metadata };
let address_book = cache.address_book.clone();
let payload = Payload { prv_key_data, accounts, address_book, transaction_records, transaction_metadata };
let payload = Decrypted::new(payload).encrypt(secret)?;

Ok(Wallet { payload, metadata, user_hint: cache.user_hint.clone() })
Expand Down
13 changes: 10 additions & 3 deletions wallet/core/src/storage/local/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,11 +360,18 @@ impl AccountStore for LocalStoreInner {
#[async_trait]
impl AddressBookStore for LocalStoreInner {
async fn iter(&self) -> Result<StorageStream<AddressBookEntry>> {
todo!()
Ok(Box::pin(AddressBookEntryStream::new(self.cache.clone())))
}

async fn search(&self, _search: &str) -> Result<Vec<Arc<AddressBookEntry>>> {
todo!()
async fn search(&self, search: &str) -> Result<Vec<Arc<AddressBookEntry>>> {
let matches = self
.cache()
.address_book
.iter()
.filter_map(|entry| if entry.alias.contains(search) { Some(Arc::new(entry.clone())) } else { None })
.collect();

Ok(matches)
}
}

Expand Down
29 changes: 29 additions & 0 deletions wallet/core/src/storage/local/streams.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,35 @@ impl Stream for MetadataStream {
}
}

#[derive(Clone, Debug)]
pub struct AddressBookEntryStream {
inner: StoreStreamInner,
}

impl AddressBookEntryStream {
pub(crate) fn new(cache: Arc<Mutex<Cache>>) -> Self {
Self { inner: StoreStreamInner::new(cache) }
}
}

impl Stream for AddressBookEntryStream {
type Item = Result<Arc<AddressBookEntry>>;

fn poll_next(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let cache = self.inner.cache.clone();
let cache = cache.lock().unwrap();
let vec = &cache.address_book; //transaction_records.vec;

if self.inner.cursor < vec.len() {
let address_book_entry = vec[self.inner.cursor].clone();
self.inner.cursor += 1;
Poll::Ready(Some(Ok(Arc::new(address_book_entry))))
} else {
Poll::Ready(None)
}
}
}

#[derive(Clone, Debug)]
pub struct TransactionRecordStream {
inner: StoreStreamInner,
Expand Down
8 changes: 6 additions & 2 deletions wallet/core/src/storage/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use crate::imports::*;
use crate::result::Result;
use crate::secret::Secret;
use crate::storage::{
Account, Encryptable, KeyCaps, PrvKeyData, PrvKeyDataId, PrvKeyDataPayload, TransactionMetadata, TransactionRecord,
TransactionRecordId,
Account, AddressBookEntry, Encryptable, KeyCaps, PrvKeyData, PrvKeyDataId, PrvKeyDataPayload, TransactionMetadata,
TransactionRecord, TransactionRecordId,
};
use kaspa_bip32::Mnemonic;
use zeroize::{Zeroize, ZeroizeOnDrop};
Expand All @@ -14,6 +14,10 @@ use zeroize::{Zeroize, ZeroizeOnDrop};
pub struct Payload {
pub prv_key_data: Vec<PrvKeyData>,
pub accounts: Vec<Account>,
#[serde(default)]
pub address_book: Vec<AddressBookEntry>,

// ------
pub transaction_records: Vec<TransactionRecord>,
pub transaction_metadata: HashMap<TransactionRecordId, TransactionMetadata>,
}
Expand Down

0 comments on commit 2106425

Please sign in to comment.