1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//!
//! Structs used as various arguments for internal wallet operations.
//!

use crate::imports::*;
use crate::secret::Secret;
use crate::storage::interface::CreateArgs;
use crate::storage::{Hint, PrvKeyDataId};
use borsh::{BorshDeserialize, BorshSchema, BorshSerialize};
use zeroize::Zeroize;

#[derive(Clone, Debug, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
#[serde(rename_all = "camelCase")]
pub struct WalletCreateArgs {
    pub title: Option<String>,
    pub filename: Option<String>,
    pub encryption_kind: EncryptionKind,
    pub user_hint: Option<Hint>,
    pub overwrite_wallet_storage: bool,
}

impl WalletCreateArgs {
    pub fn new(
        title: Option<String>,
        filename: Option<String>,
        encryption_kind: EncryptionKind,
        user_hint: Option<Hint>,
        overwrite_wallet_storage: bool,
    ) -> Self {
        Self { title, filename, encryption_kind, user_hint, overwrite_wallet_storage }
    }
}

impl From<WalletCreateArgs> for CreateArgs {
    fn from(args: WalletCreateArgs) -> Self {
        CreateArgs::new(args.title, args.filename, args.encryption_kind, args.user_hint, args.overwrite_wallet_storage)
    }
}

#[derive(Default, Clone, Debug, Serialize, Deserialize, BorshSerialize, BorshDeserialize, BorshSchema)]
pub struct WalletOpenArgs {
    /// Return account descriptors
    pub account_descriptors: bool,
    /// Enable support for legacy accounts
    pub legacy_accounts: bool,
}

impl WalletOpenArgs {
    pub fn default_with_legacy_accounts() -> Self {
        Self { legacy_accounts: true, ..Default::default() }
    }

    pub fn load_account_descriptors(&self) -> bool {
        self.account_descriptors || self.legacy_accounts
    }

    pub fn is_legacy_only(&self) -> bool {
        self.legacy_accounts && !self.account_descriptors
    }
}

#[derive(Clone, Debug, Serialize, Deserialize, BorshSerialize, BorshDeserialize, BorshSchema)]
pub struct PrvKeyDataCreateArgs {
    pub name: Option<String>,
    pub payment_secret: Option<Secret>,
    pub mnemonic: String,
}

impl PrvKeyDataCreateArgs {
    pub fn new(name: Option<String>, payment_secret: Option<Secret>, mnemonic: String) -> Self {
        Self { name, payment_secret, mnemonic }
    }
}

impl Zeroize for PrvKeyDataCreateArgs {
    fn zeroize(&mut self) {
        self.mnemonic.zeroize();
    }
}

#[derive(Clone, Debug, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
pub struct AccountCreateArgsBip32 {
    pub account_name: Option<String>,
    pub account_index: Option<u64>,
}

impl AccountCreateArgsBip32 {
    pub fn new(account_name: Option<String>, account_index: Option<u64>) -> Self {
        Self { account_name, account_index }
    }
}

#[derive(Clone, Debug, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
pub struct PrvKeyDataArgs {
    pub prv_key_data_id: PrvKeyDataId,
    pub payment_secret: Option<Secret>,
}

impl PrvKeyDataArgs {
    pub fn new(prv_key_data_id: PrvKeyDataId, payment_secret: Option<Secret>) -> Self {
        Self { prv_key_data_id, payment_secret }
    }
}

#[derive(Clone, Debug, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
pub enum AccountCreateArgs {
    Bip32 {
        prv_key_data_args: PrvKeyDataArgs,
        account_args: AccountCreateArgsBip32,
    },
    Legacy {
        prv_key_data_id: PrvKeyDataId,
        account_name: Option<String>,
    },
    Multisig {
        prv_key_data_args: Vec<PrvKeyDataArgs>,
        additional_xpub_keys: Vec<String>,
        name: Option<String>,
        minimum_signatures: u16,
    },
}

impl AccountCreateArgs {
    pub fn new_bip32(
        prv_key_data_id: PrvKeyDataId,
        payment_secret: Option<Secret>,
        account_name: Option<String>,
        account_index: Option<u64>,
    ) -> Self {
        let prv_key_data_args = PrvKeyDataArgs { prv_key_data_id, payment_secret };
        let account_args = AccountCreateArgsBip32 { account_name, account_index };
        AccountCreateArgs::Bip32 { prv_key_data_args, account_args }
    }

    pub fn new_legacy(prv_key_data_id: PrvKeyDataId, account_name: Option<String>) -> Self {
        AccountCreateArgs::Legacy { prv_key_data_id, account_name }
    }

    pub fn new_multisig(
        prv_key_data_args: Vec<PrvKeyDataArgs>,
        additional_xpub_keys: Vec<String>,
        name: Option<String>,
        minimum_signatures: u16,
    ) -> Self {
        AccountCreateArgs::Multisig { prv_key_data_args, additional_xpub_keys, name, minimum_signatures }
    }
}