Skip to content

Commit

Permalink
wallet storage data versioning
Browse files Browse the repository at this point in the history
  • Loading branch information
aspect committed Sep 13, 2023
1 parent ca31934 commit 15737c6
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 44 deletions.
17 changes: 17 additions & 0 deletions kaspad/src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,29 @@ impl Runtime {
/// This function will automatically create a [`Runtime`]
/// instance with the supplied [`Args`] and then
/// call [`create_core_with_runtime`].
///
/// Usage semantics:
/// `let (core, rpc_core_service) = create_core(args);`
///
/// The instance of the [`RpcCoreService`] needs to be released
/// (dropped) before the `Core` is shut down.
///
pub fn create_core(args: Args) -> (Arc<Core>, Arc<RpcCoreService>) {
let rt = Runtime::from_args(&args);
create_core_with_runtime(&rt, &args)
}

/// Create [`Core`] instance with supplied [`Args`] and [`Runtime`].
///
/// Usage semantics:
/// ```
/// let Runtime = Runtime::from_args(&args); // or create your own
/// let (core, rpc_core_service) = create_core(&runtime, &args);
/// ```
///
/// The instance of the [`RpcCoreService`] needs to be released
/// (dropped) before the `Core` is shut down.
///
pub fn create_core_with_runtime(runtime: &Runtime, args: &Args) -> (Arc<Core>, Arc<RpcCoreService>) {
let network = args.network();

Expand Down
4 changes: 2 additions & 2 deletions wallet/core/src/runtime/account/variants/bip32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl Bip32 {
let id = AccountId::from_bip32(&prv_key_data_id, &data);
let inner = Arc::new(Inner::new(wallet, id, Some(settings)));

let storage::account::Bip32 { account_index, xpub_keys, ecdsa } = data;
let storage::account::Bip32 { account_index, xpub_keys, ecdsa, .. } = data;

let address_derivation_indexes = meta.and_then(|meta| meta.address_derivation_indexes()).unwrap_or_default();

Expand Down Expand Up @@ -73,7 +73,7 @@ impl Account for Bip32 {

fn as_storable(&self) -> Result<storage::account::Account> {
let settings = self.context().settings.clone().unwrap_or_default();
let bip32 = storage::Bip32 { account_index: self.account_index, xpub_keys: self.xpub_keys.clone(), ecdsa: self.ecdsa };
let bip32 = storage::Bip32::new(self.account_index, self.xpub_keys.clone(), self.ecdsa);
let account = storage::Account::new(*self.id(), Some(self.prv_key_data_id), settings, storage::AccountData::Bip32(bip32));
Ok(account)
}
Expand Down
4 changes: 2 additions & 2 deletions wallet/core/src/runtime/account/variants/keypair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl Keypair {
let id = AccountId::from_keypair(&prv_key_data_id, &data);
let inner = Arc::new(Inner::new(wallet, id, Some(settings)));

let storage::account::Keypair { public_key, ecdsa } = data;
let storage::account::Keypair { public_key, ecdsa, .. } = data;
Ok(Self { inner, prv_key_data_id, public_key, ecdsa })
}
}
Expand Down Expand Up @@ -59,7 +59,7 @@ impl Account for Keypair {

fn as_storable(&self) -> Result<storage::account::Account> {
let settings = self.context().settings.clone().unwrap_or_default();
let keypair = storage::Keypair { public_key: self.public_key, ecdsa: self.ecdsa };
let keypair = storage::Keypair::new(self.public_key, self.ecdsa);
let account = storage::Account::new(*self.id(), Some(self.prv_key_data_id), settings, storage::AccountData::Keypair(keypair));
Ok(account)
}
Expand Down
4 changes: 2 additions & 2 deletions wallet/core/src/runtime/account/variants/legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl Legacy {
let id = AccountId::from_legacy(&prv_key_data_id, &data);
let inner = Arc::new(Inner::new(wallet, id, Some(settings)));

let storage::account::Legacy { xpub_keys } = data;
let storage::account::Legacy { xpub_keys, .. } = data;

let address_derivation_indexes = meta.and_then(|meta| meta.address_derivation_indexes()).unwrap_or_default();

Expand Down Expand Up @@ -65,7 +65,7 @@ impl Account for Legacy {
fn as_storable(&self) -> Result<storage::account::Account> {
let settings = self.context().settings.clone().unwrap_or_default();

let legacy = storage::Legacy { xpub_keys: self.xpub_keys.clone() };
let legacy = storage::Legacy::new(self.xpub_keys.clone());

let account = storage::Account::new(*self.id(), Some(self.prv_key_data_id), settings, storage::AccountData::Legacy(legacy));

Expand Down
16 changes: 8 additions & 8 deletions wallet/core/src/runtime/account/variants/multisig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl MultiSig {
let id = AccountId::from_multisig(&prv_key_data_id, &data);
let inner = Arc::new(Inner::new(wallet, id, Some(settings)));

let storage::account::MultiSig { account_index, xpub_keys, cosigner_index, minimum_signatures, ecdsa } = data;
let storage::account::MultiSig { account_index, xpub_keys, cosigner_index, minimum_signatures, ecdsa, .. } = data;

let address_derivation_indexes = meta.and_then(|meta| meta.address_derivation_indexes()).unwrap_or_default();

Expand Down Expand Up @@ -80,13 +80,13 @@ impl Account for MultiSig {
fn as_storable(&self) -> Result<storage::account::Account> {
let settings = self.context().settings.clone().unwrap_or_default();

let multisig = storage::MultiSig {
account_index: self.account_index,
xpub_keys: self.xpub_keys.clone(),
ecdsa: self.ecdsa,
cosigner_index: self.cosigner_index,
minimum_signatures: self.minimum_signatures,
};
let multisig = storage::MultiSig::new(
self.account_index,
self.xpub_keys.clone(),
self.cosigner_index,
self.minimum_signatures,
self.ecdsa,
);

let account =
storage::Account::new(*self.id(), Some(self.prv_key_data_id), settings, storage::AccountData::MultiSig(multisig));
Expand Down
12 changes: 6 additions & 6 deletions wallet/core/src/runtime/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ impl Wallet {
let xpub_prefix = kaspa_bip32::Prefix::XPUB;
let xpub_keys = Arc::new(vec![xpub_key.to_string(Some(xpub_prefix))]);

let bip32 = storage::Bip32 { account_index, xpub_keys, ecdsa: false };
let bip32 = storage::Bip32::new(account_index, xpub_keys, false);

let settings = storage::Settings { is_visible: false, name: None, title: None };
let account: Arc<dyn Account> = Arc::new(runtime::Bip32::try_new(self, prv_key_data.id, settings, bip32, None).await?);
Expand Down Expand Up @@ -516,7 +516,7 @@ impl Wallet {
prv_key_data.create_xpub(account_args.payment_secret.as_ref(), account_args.account_kind, account_index).await?;
let xpub_keys = Arc::new(vec![xpub_key.to_string(Some(xpub_prefix))]);

let bip32 = storage::Bip32 { account_index, xpub_keys, ecdsa: false };
let bip32 = storage::Bip32::new(account_index, xpub_keys, false);

let settings = storage::Settings { is_visible: false, name: None, title: None };
let account: Arc<dyn Account> = Arc::new(runtime::Bip32::try_new(self, prv_key_data.id, settings, bip32, None).await?);
Expand Down Expand Up @@ -696,7 +696,7 @@ impl Wallet {

// TODO: xpub_keys
let xpub_keys = Arc::new(vec![]);
let data = storage::Legacy { xpub_keys };
let data = storage::Legacy::new(xpub_keys);
let settings = storage::Settings::default();
let account = Arc::new(runtime::account::Legacy::try_new(self, prv_key_data.id, settings, data, None).await?);

Expand Down Expand Up @@ -743,7 +743,7 @@ impl Wallet {
let ecdsa = false;
// ---

let data = storage::Bip32 { xpub_keys, account_index, ecdsa };
let data = storage::Bip32::new(account_index, xpub_keys, ecdsa);
let settings = storage::Settings::default();
Arc::new(runtime::account::Bip32::try_new(self, prv_key_data.id, settings, data, None).await?)
// account
Expand All @@ -753,7 +753,7 @@ impl Wallet {
let xpub_keys = Arc::new(vec![]);
// ---

let data = storage::Legacy { xpub_keys };
let data = storage::Legacy::new(xpub_keys);
let settings = storage::Settings::default();
Arc::new(runtime::account::Legacy::try_new(self, prv_key_data.id, settings, data, None).await?)
}
Expand All @@ -765,7 +765,7 @@ impl Wallet {
let minimum_signatures = 1;
// ---

let data = storage::MultiSig { xpub_keys, account_index, ecdsa, cosigner_index, minimum_signatures };
let data = storage::MultiSig::new(account_index, xpub_keys, cosigner_index, minimum_signatures, ecdsa);
let settings = storage::Settings::default();
Arc::new(runtime::account::MultiSig::try_new(self, prv_key_data.id, settings, data, None).await?)
}
Expand Down
78 changes: 58 additions & 20 deletions wallet/core/src/storage/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,92 @@ pub struct Settings {
pub title: Option<String>,
}

const LEGACY_ACCOUNT_VERSION: u16 = 0;
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub struct Legacy {
#[serde(default)]
pub version: u16,

pub xpub_keys: Arc<Vec<String>>,
}

impl Legacy {
pub fn new(xpub_keys: Arc<Vec<String>>) -> Self {
Self { version: LEGACY_ACCOUNT_VERSION, xpub_keys }
}
}

const BIP32_ACCOUNT_VERSION: u16 = 0;
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub struct Bip32 {
#[serde(default)]
pub version: u16,

pub account_index: u64,
pub xpub_keys: Arc<Vec<String>>,
pub ecdsa: bool,
}

impl Bip32 {
pub fn new(account_index: u64, xpub_keys: Arc<Vec<String>>, ecdsa: bool) -> Self {
Self { version: BIP32_ACCOUNT_VERSION, account_index, xpub_keys, ecdsa }
}
}

const MULTISIG_ACCOUNT_VERSION: u16 = 0;
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub struct MultiSig {
#[serde(default)]
pub version: u16,

pub account_index: u64,
pub xpub_keys: Arc<Vec<String>>,
pub cosigner_index: u8,
pub minimum_signatures: u16,
pub ecdsa: bool,
}

impl MultiSig {
pub fn new(account_index: u64, xpub_keys: Arc<Vec<String>>, cosigner_index: u8, minimum_signatures: u16, ecdsa: bool) -> Self {
Self { version: MULTISIG_ACCOUNT_VERSION, account_index, xpub_keys, cosigner_index, minimum_signatures, ecdsa }
}
}

const KEYPAIR_ACCOUNT_VERSION: u16 = 0;
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub struct Keypair {
#[serde(default)]
pub version: u16,

pub public_key: PublicKey,
pub ecdsa: bool,
}

impl Keypair {
pub fn new(public_key: PublicKey, ecdsa: bool) -> Self {
Self { version: KEYPAIR_ACCOUNT_VERSION, public_key, ecdsa }
}
}

const HARDWARE_ACCOUNT_VERSION: u16 = 0;
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub struct Hardware {}
pub struct Hardware {
#[serde(default)]
pub version: u16,

pub descriptor: String,
}

impl Hardware {
pub fn new(descriptor: &str) -> Self {
Self { version: HARDWARE_ACCOUNT_VERSION, descriptor: descriptor.to_string() }
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
Expand All @@ -70,8 +122,12 @@ impl AccountData {
}
}

const ACCOUNT_VERSION: u16 = 0;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Account {
#[serde(default)]
pub version: u16,

pub id: AccountId,
pub prv_key_data_id: Option<PrvKeyDataId>,
pub settings: Settings,
Expand All @@ -80,28 +136,10 @@ pub struct Account {

impl Account {
pub fn new(id: AccountId, prv_key_data_id: Option<PrvKeyDataId>, settings: Settings, data: AccountData) -> Self {
Self { id, prv_key_data_id, settings, data }
Self { version: ACCOUNT_VERSION, id, prv_key_data_id, settings, data }
}

pub fn data(&self) -> &AccountData {
&self.data
}
}

// #[derive(Debug, Clone, Serialize, Deserialize)]
// pub struct AccountV0_0_0 {
// pub id: AccountId,
// pub prv_key_data_id: PrvKeyDataId,
// #[serde(skip_serializing_if = "Option::is_none")]
// pub name: Option<String>,
// #[serde(skip_serializing_if = "Option::is_none")]
// pub title: Option<String>,
// pub is_visible: bool,
// pub account_kind: AccountKind,
// pub account_index: u64,
// pub pub_key_data: PubKeyData,
// pub prv_key_data_id: PrvKeyDataId,
// pub minimum_signatures: u16,
// pub cosigner_index: u32,
// pub ecdsa: bool,
// }
2 changes: 1 addition & 1 deletion wallet/core/src/storage/local/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl TryFrom<(&Cache, &Secret)> for Wallet {
let accounts: Vec<Account> = (&cache.accounts).try_into()?;
let metadata: Vec<Metadata> = (&cache.metadata).try_into()?;
let address_book = cache.address_book.clone();
let payload = Payload { prv_key_data, accounts, address_book };
let payload = Payload::new(prv_key_data, accounts, address_book);
let payload = Decrypted::new(payload).encrypt(secret)?;

Ok(Wallet { version: WALLET_VERSION, payload, metadata, user_hint: cache.user_hint.clone() })
Expand Down
11 changes: 11 additions & 0 deletions wallet/core/src/storage/local/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,24 @@ use crate::storage::{Account, AddressBookEntry, PrvKeyData, PrvKeyDataId};
use kaspa_bip32::Mnemonic;
use zeroize::{Zeroize, ZeroizeOnDrop};

pub const PAYLOAD_VERSION: [u16; 3] = [1, 0, 0];

#[derive(Default, Debug, Clone, Serialize, Deserialize)]
pub struct Payload {
#[serde(default)]
pub version: [u16; 3],

pub prv_key_data: Vec<PrvKeyData>,
pub accounts: Vec<Account>,
pub address_book: Vec<AddressBookEntry>,
}

impl Payload {
pub fn new(prv_key_data: Vec<PrvKeyData>, accounts: Vec<Account>, address_book: Vec<AddressBookEntry>) -> Self {
Self { version: PAYLOAD_VERSION, prv_key_data, accounts, address_book }
}
}

impl ZeroizeOnDrop for Payload {}

impl Zeroize for Payload {
Expand Down
3 changes: 2 additions & 1 deletion wallet/core/src/storage/local/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ use crate::storage::{Decrypted, Encrypted, Hint, Metadata, PrvKeyData, PrvKeyDat
use serde_json::{from_str, from_value, Value};
use workflow_store::fs;

pub const WALLET_VERSION: [u16; 3] = [0, 0, 1];
pub const WALLET_VERSION: [u16; 3] = [1, 0, 0];

#[derive(Clone, Serialize, Deserialize)]
pub struct Wallet {
#[serde(default)]
pub version: [u16; 3],

#[serde(skip_serializing_if = "Option::is_none")]
pub user_hint: Option<Hint>,
pub payload: Encrypted,
Expand Down
4 changes: 2 additions & 2 deletions wallet/core/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ mod tests {
payload.prv_key_data.push(prv_key_data2.clone());

let settings = Settings { name: Some("Wallet-A".to_string()), title: Some("Wallet A".to_string()), is_visible: false };
let bip32 = Bip32 { account_index: 0, xpub_keys: pub_key_data1.clone(), ecdsa: false };
let bip32 = Bip32::new(0, pub_key_data1.clone(), false);
let id = AccountId::from_bip32(&prv_key_data1.id, &bip32);
let account1 = Account::new(id, Some(prv_key_data1.id), settings, AccountData::Bip32(bip32));
payload.accounts.push(account1);

let settings = Settings { name: Some("Wallet-B".to_string()), title: Some("Wallet B".to_string()), is_visible: false };
let bip32 = Bip32 { account_index: 0, xpub_keys: pub_key_data2.clone(), ecdsa: false };
let bip32 = Bip32::new(0, pub_key_data2.clone(), false);
let id = AccountId::from_bip32(&prv_key_data2.id, &bip32);
let account2 = Account::new(id, Some(prv_key_data2.id), settings, AccountData::Bip32(bip32));
payload.accounts.push(account2);
Expand Down

0 comments on commit 15737c6

Please sign in to comment.