Skip to content

Commit

Permalink
wip - scaffolding for Address Book and Transaction Record Metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
aspect committed Jul 17, 2023
1 parent 3bc63aa commit cc30675
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 4 deletions.
3 changes: 3 additions & 0 deletions wallet/core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ pub enum Error {

#[error("{0}")]
ToValue(String),

#[error("The feature is not supported")]
NotImplemented,
}

impl From<Aborted> for Error {
Expand Down
8 changes: 8 additions & 0 deletions wallet/core/src/storage/address.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use crate::imports::*;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AddressBookEntry {
pub alias: String,
pub title: String,
pub address: Address,
}
12 changes: 12 additions & 0 deletions wallet/core/src/storage/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ pub trait AccountStore: Send + Sync {
async fn remove(&self, id: &[&AccountId]) -> Result<()>;
}

#[async_trait]
pub trait AddressBookStore: Send + Sync {
async fn iter(&self) -> Result<StorageStream<AddressBookEntry>> {
Err(Error::NotImplemented)
}
async fn search(&self, _search: &str) -> Result<Vec<Arc<AddressBookEntry>>> {
Err(Error::NotImplemented)
}
}

#[async_trait]
pub trait MetadataStore: Send + Sync {
async fn iter(&self, prv_key_data_id_filter: Option<PrvKeyDataId>) -> Result<StorageStream<Metadata>>;
Expand All @@ -79,6 +89,7 @@ pub trait TransactionRecordStore: Send + Sync {
async fn load(&self, id: &[TransactionRecordId]) -> Result<Vec<Arc<TransactionRecord>>>;
async fn store(&self, data: &[&TransactionRecord]) -> Result<()>;
async fn remove(&self, id: &[&TransactionRecordId]) -> Result<()>;
async fn store_transaction_metadata(&self, id: TransactionRecordId, metadata: TransactionMetadata) -> Result<()>;
}

pub struct CreateArgs {
Expand Down Expand Up @@ -144,6 +155,7 @@ pub trait Interface: Send + Sync + AnySync {

fn as_prv_key_data_store(&self) -> Result<Arc<dyn PrvKeyDataStore>>;
fn as_account_store(&self) -> Result<Arc<dyn AccountStore>>;
fn as_address_book_store(&self) -> Result<Arc<dyn AddressBookStore>>;
fn as_metadata_store(&self) -> Result<Arc<dyn MetadataStore>>;
fn as_transaction_record_store(&self) -> Result<Arc<dyn TransactionRecordStore>>;
}
Expand Down
8 changes: 6 additions & 2 deletions wallet/core/src/storage/local/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub struct Cache {
pub accounts: Collection<AccountId, Account>,
pub metadata: Collection<AccountId, Metadata>,
pub transaction_records: Collection<TransactionRecordId, TransactionRecord>,
pub transaction_metadata: HashMap<TransactionRecordId, TransactionMetadata>,
}

impl TryFrom<(Wallet, &Secret)> for Cache {
Expand All @@ -30,8 +31,10 @@ impl TryFrom<(Wallet, &Secret)> for Cache {
let metadata: Collection<AccountId, Metadata> = wallet.metadata.try_into()?;
let user_hint = wallet.user_hint;
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();

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

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

Ok(Wallet { payload, metadata, user_hint: cache.user_hint.clone() })
Expand Down
20 changes: 20 additions & 0 deletions wallet/core/src/storage/local/interface.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::imports::*;
use crate::result::Result;
use crate::storage::interface::AddressBookStore;
use crate::storage::interface::CreateArgs;
use crate::storage::interface::OpenArgs;
use crate::storage::interface::StorageStream;
Expand Down Expand Up @@ -173,6 +174,10 @@ impl Interface for LocalStore {
Ok(self.inner()?)
}

fn as_address_book_store(&self) -> Result<Arc<dyn AddressBookStore>> {
Ok(self.inner()?)
}

fn as_metadata_store(&self) -> Result<Arc<dyn MetadataStore>> {
Ok(self.inner()?)
}
Expand Down Expand Up @@ -352,6 +357,17 @@ impl AccountStore for LocalStoreInner {
}
}

#[async_trait]
impl AddressBookStore for LocalStoreInner {
async fn iter(&self) -> Result<StorageStream<AddressBookEntry>> {
todo!()
}

async fn search(&self, _search: &str) -> Result<Vec<Arc<AddressBookEntry>>> {
todo!()
}
}

#[async_trait]
impl MetadataStore for LocalStoreInner {
async fn iter(&self, prv_key_data_id_filter: Option<PrvKeyDataId>) -> Result<StorageStream<Metadata>> {
Expand Down Expand Up @@ -384,4 +400,8 @@ impl TransactionRecordStore for LocalStoreInner {
self.set_modified(true);
Ok(())
}

async fn store_transaction_metadata(&self, _id: TransactionRecordId, _metadata: TransactionMetadata) -> Result<()> {
Ok(())
}
}
4 changes: 3 additions & 1 deletion wallet/core/src/storage/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub use crate::encryption::{Decrypted, Encryptable, Encrypted};

pub mod account;
pub mod address;
pub mod hint;
pub mod id;
pub mod interface;
Expand All @@ -12,13 +13,14 @@ pub mod transaction;
pub mod wasm;

pub use account::Account;
pub use address::AddressBookEntry;
pub use hint::Hint;
pub use id::IdT;
pub use interface::{AccessContextT, AccountStore, Interface, MetadataStore, PrvKeyDataStore, TransactionRecordStore};
pub use keydata::{KeyCaps, PrvKeyData, PrvKeyDataId, PrvKeyDataInfo, PrvKeyDataMap, PrvKeyDataPayload, PubKeyData, PubKeyDataId};
pub use metadata::Metadata;
pub use payload::Payload;
pub use transaction::{TransactionRecord, TransactionRecordId, TransactionType};
pub use transaction::{TransactionMetadata, TransactionRecord, TransactionRecordId, TransactionType};

pub use crate::runtime::{AccountId, AccountKind};

Expand Down
8 changes: 7 additions & 1 deletion wallet/core/src/storage/payload.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
use std::collections::HashMap;

use crate::imports::*;
use crate::result::Result;
use crate::secret::Secret;
use crate::storage::{Account, Encryptable, KeyCaps, PrvKeyData, PrvKeyDataId, PrvKeyDataPayload, TransactionRecord};
use crate::storage::{
Account, Encryptable, KeyCaps, PrvKeyData, PrvKeyDataId, PrvKeyDataPayload, TransactionMetadata, TransactionRecord,
TransactionRecordId,
};
use kaspa_bip32::Mnemonic;
use zeroize::{Zeroize, ZeroizeOnDrop};

Expand All @@ -10,6 +15,7 @@ pub struct Payload {
pub prv_key_data: Vec<PrvKeyData>,
pub accounts: Vec<Account>,
pub transaction_records: Vec<TransactionRecord>,
pub transaction_metadata: HashMap<TransactionRecordId, TransactionMetadata>,
}

impl ZeroizeOnDrop for Payload {}
Expand Down
6 changes: 6 additions & 0 deletions wallet/core/src/storage/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ impl std::fmt::Display for TransactionRecordId {
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TransactionMetadata {
pub id: TransactionRecordId,
pub metadata: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TransactionRecord {
pub id: TransactionRecordId,
Expand Down

0 comments on commit cc30675

Please sign in to comment.