-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmod.rs
More file actions
202 lines (184 loc) · 6.7 KB
/
mod.rs
File metadata and controls
202 lines (184 loc) · 6.7 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
pub mod key_derivation;
pub mod cpfp_tx;
use std::{fmt, str::FromStr};
use bip39::{Mnemonic, Language};
use secp256k1_zkp::rand::{self, Rng};
use serde::{Serialize, Deserialize};
use crate::{utils::ServerConfig, MercuryError};
#[derive(Debug, Serialize, Deserialize, Clone)]
#[cfg_attr(feature = "bindings", derive(uniffi::Record))]
pub struct Wallet {
pub name: String,
pub mnemonic: String,
pub version: String,
pub state_entity_endpoint: String,
pub electrum_endpoint: String,
pub network: String,
pub blockheight: u32,
pub initlock: u32,
pub interval: u32,
pub tokens: Vec<Token>,
pub activities: Vec<Activity>,
pub coins: Vec<Coin>,
pub settings: Settings,
}
#[allow(non_snake_case)]
#[derive(Debug, Serialize, Deserialize, Clone)]
#[cfg_attr(feature = "bindings", derive(uniffi::Record))]
pub struct Settings {
pub network: String,
pub block_explorerURL: Option<String>,
pub torProxyHost: Option<String>,
pub torProxyPort: Option<String>,
pub torProxyControlPassword: Option<String>,
pub torProxyControlPort: Option<String>,
pub statechainEntityApi: String,
pub torStatechainEntityApi: Option<String>,
pub electrumProtocol: String,
pub electrumHost: String,
pub electrumPort: String,
pub electrumType: String,
pub notifications: bool,
pub tutorials: bool
}
#[derive(Debug, Serialize, Deserialize, Clone)]
#[cfg_attr(feature = "bindings", derive(uniffi::Record))]
pub struct Token {
pub btc_payment_address: String,
pub fee: String,
pub lightning_invoice: String,
pub processor_id: String,
pub token_id: String,
pub confirmed: bool,
pub spent: bool,
pub expiry: String,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
#[cfg_attr(feature = "bindings", derive(uniffi::Record))]
pub struct Activity {
pub utxo: String,
pub amount: u32,
pub action: String,
pub date: String
}
#[derive(Debug, Serialize, Deserialize, Clone)]
#[cfg_attr(feature = "bindings", derive(uniffi::Record))]
pub struct Coin {
pub index: u32,
pub user_privkey: String,
pub user_pubkey: String,
pub auth_privkey: String,
pub auth_pubkey: String,
pub derivation_path: String,
pub fingerprint: String,
/// The coin address is the user_pubkey || auth_pubkey
/// Used to transfer the coin to another wallet
pub address: String,
/// The backup address is the address used in backup transactions
/// The backup address is the p2tr address of the user_pubkey
pub backup_address: String,
pub server_pubkey: Option<String>,
// The aggregated_pubkey is the user_pubkey + server_pubkey
pub aggregated_pubkey: Option<String>,
/// The aggregated address is the P2TR address from aggregated_pubkey
pub aggregated_address: Option<String>,
pub utxo_txid: Option<String>,
pub utxo_vout: Option<u32>,
pub amount: Option<u32>,
pub statechain_id: Option<String>,
pub signed_statechain_id: Option<String>,
pub locktime: Option<u32>,
pub secret_nonce: Option<String>,
pub public_nonce: Option<String>,
pub blinding_factor: Option<String>,
pub server_public_nonce: Option<String>,
pub tx_cpfp: Option<String>,
pub tx_withdraw: Option<String>,
pub withdrawal_address: Option<String>,
pub status: CoinStatus,
pub duplicate_index: u32,
}
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
#[cfg_attr(feature = "bindings", derive(uniffi::Enum))]
#[allow(non_camel_case_types)]
pub enum CoinStatus {
INITIALISED, // address generated but no Tx0 yet
IN_MEMPOOL, // Tx0 in mempool
UNCONFIRMED, // Tx0 is awaiting more confirmations before coin is available to be sent
CONFIRMED, // Tx0 confirmed and coin available to be sent
IN_TRANSFER, // transfer-sender performed, but receiver hasn't completed transfer-receiver
WITHDRAWING, // withdrawal tx signed and broadcast but not yet confirmed
TRANSFERRED, // the coin was transferred
WITHDRAWN, // the coin was withdrawn
DUPLICATED, // the coin was duplicated
}
impl fmt::Display for CoinStatus {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// Match the enum variants
write!(f, "{}", match self {
Self::INITIALISED => "INITIALISED",
Self::IN_MEMPOOL => "IN_MEMPOOL",
Self::UNCONFIRMED => "UNCONFIRMED",
Self::CONFIRMED => "CONFIRMED",
Self::IN_TRANSFER => "IN_TRANSFER",
Self::WITHDRAWING => "WITHDRAWING",
Self::TRANSFERRED => "TRANSFERRED",
Self::WITHDRAWN => "WITHDRAWN",
Self::DUPLICATED => "DUPLICATED",
})
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "bindings", derive(uniffi::Record))]
pub struct CoinStatusParseError;
impl fmt::Display for CoinStatusParseError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "provided string was not a valid CoinStatus")
}
}
impl std::error::Error for CoinStatusParseError {}
impl FromStr for CoinStatus {
type Err = CoinStatusParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"INITIALISED" => Ok(CoinStatus::INITIALISED),
"IN_MEMPOOL" => Ok(CoinStatus::IN_MEMPOOL),
"UNCONFIRMED" => Ok(CoinStatus::UNCONFIRMED),
"CONFIRMED" => Ok(CoinStatus::CONFIRMED),
"IN_TRANSFER" => Ok(CoinStatus::IN_TRANSFER),
"WITHDRAWING" => Ok(CoinStatus::WITHDRAWING),
"TRANSFERRED" => Ok(CoinStatus::TRANSFERRED),
"WITHDRAWN" => Ok(CoinStatus::WITHDRAWN),
"DUPLICATED" => Ok(CoinStatus::DUPLICATED),
_ => Err(CoinStatusParseError {}),
}
}
}
#[derive(Debug, Serialize, Deserialize, Clone)]
#[cfg_attr(feature = "bindings", derive(uniffi::Record))]
pub struct StatechainBackupTxs {
pub statechain_id: String,
pub backup_txs: Vec<BackupTx>
}
#[derive(Debug, Serialize, Deserialize, Clone)]
#[cfg_attr(feature = "bindings", derive(uniffi::Record))]
pub struct BackupTx {
pub tx_n: u32,
pub tx: String,
pub client_public_nonce: String,
pub server_public_nonce: String,
pub client_public_key: String,
pub server_public_key: String,
pub blinding_factor: String,
}
pub fn set_config(wallet: &mut Wallet, config: &ServerConfig) {
wallet.initlock = config.initlock;
wallet.interval = config.interval;
}
#[cfg_attr(feature = "bindings", uniffi::export)]
pub fn generate_mnemonic() -> core::result::Result<String, MercuryError> {
let mut rng = rand::thread_rng();
let entropy = (0..16).map(|_| rng.gen::<u8>()).collect::<Vec<u8>>(); // 16 bytes of entropy for 12 words
let mnemonic = Mnemonic::from_entropy_in(Language::English, &entropy)?;
Ok(mnemonic.to_string())
}