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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
use std::{fmt, ops, path::Path};
use async_trait::async_trait;
use elliptic_curve::rand_core;
use eth_keystore::KeystoreError;
use fuel_crypto::{Message, PublicKey, SecretKey, Signature};
use fuel_tx::{AssetId, Witness};
use fuels_types::{
bech32::{Bech32Address, FUEL_BECH32_HRP},
constants::BASE_ASSET_ID,
errors::{Error, Result},
input::Input,
transaction::Transaction,
transaction_builders::TransactionBuilder,
};
use rand::{CryptoRng, Rng};
use thiserror::Error;
use crate::{
accounts_utils::{adjust_inputs, adjust_outputs, calculate_base_amount_with_fee},
provider::Provider,
Account, AccountError, AccountResult, Signer, ViewOnlyAccount,
};
pub const DEFAULT_DERIVATION_PATH_PREFIX: &str = "m/44'/1179993420'";
#[derive(Error, Debug)]
pub enum WalletError {
#[error(transparent)]
Hex(#[from] hex::FromHexError),
#[error("Failed to parse slice")]
Parsing(#[from] std::array::TryFromSliceError),
#[error(transparent)]
KeystoreError(#[from] KeystoreError),
#[error(transparent)]
FuelCrypto(#[from] fuel_crypto::Error),
}
impl From<WalletError> for Error {
fn from(e: WalletError) -> Self {
Error::WalletError(e.to_string())
}
}
type WalletResult<T> = std::result::Result<T, WalletError>;
#[derive(Clone)]
pub struct Wallet {
pub(crate) address: Bech32Address,
provider: Option<Provider>,
}
#[derive(Clone, Debug)]
pub struct WalletUnlocked {
wallet: Wallet,
pub(crate) private_key: SecretKey,
}
impl Wallet {
pub fn from_address(address: Bech32Address, provider: Option<Provider>) -> Self {
Self { address, provider }
}
pub fn provider(&self) -> Option<&Provider> {
self.provider.as_ref()
}
pub fn set_provider(&mut self, provider: Provider) -> &mut Self {
self.provider = Some(provider);
self
}
pub fn address(&self) -> &Bech32Address {
&self.address
}
pub fn unlock(self, private_key: SecretKey) -> WalletUnlocked {
WalletUnlocked {
wallet: self,
private_key,
}
}
}
impl ViewOnlyAccount for Wallet {
fn address(&self) -> &Bech32Address {
self.address()
}
fn try_provider(&self) -> AccountResult<&Provider> {
self.provider.as_ref().ok_or(AccountError::no_provider())
}
fn set_provider(&mut self, provider: Provider) -> &mut Wallet {
self.set_provider(provider)
}
}
impl WalletUnlocked {
pub fn lock(self) -> Wallet {
self.wallet
}
pub fn set_provider(&mut self, provider: Provider) -> &mut Wallet {
self.wallet.set_provider(provider)
}
pub fn new_random(provider: Option<Provider>) -> Self {
let mut rng = rand::thread_rng();
let private_key = SecretKey::random(&mut rng);
Self::new_from_private_key(private_key, provider)
}
pub fn new_from_private_key(private_key: SecretKey, provider: Option<Provider>) -> Self {
let public = PublicKey::from(&private_key);
let hashed = public.hash();
let address = Bech32Address::new(FUEL_BECH32_HRP, hashed);
Wallet::from_address(address, provider).unlock(private_key)
}
pub fn new_from_mnemonic_phrase(
phrase: &str,
provider: Option<Provider>,
) -> WalletResult<Self> {
let path = format!("{DEFAULT_DERIVATION_PATH_PREFIX}/0'/0/0");
Self::new_from_mnemonic_phrase_with_path(phrase, provider, &path)
}
pub fn new_from_mnemonic_phrase_with_path(
phrase: &str,
provider: Option<Provider>,
path: &str,
) -> WalletResult<Self> {
let secret_key = SecretKey::new_from_mnemonic_phrase_with_path(phrase, path)?;
Ok(Self::new_from_private_key(secret_key, provider))
}
pub fn new_from_keystore<P, R, S>(
dir: P,
rng: &mut R,
password: S,
provider: Option<Provider>,
) -> WalletResult<(Self, String)>
where
P: AsRef<Path>,
R: Rng + CryptoRng + rand_core::CryptoRng,
S: AsRef<[u8]>,
{
let (secret, uuid) = eth_keystore::new(dir, rng, password)?;
let secret_key = unsafe { SecretKey::from_slice_unchecked(&secret) };
let wallet = Self::new_from_private_key(secret_key, provider);
Ok((wallet, uuid))
}
pub fn encrypt<P, S>(&self, dir: P, password: S) -> WalletResult<String>
where
P: AsRef<Path>,
S: AsRef<[u8]>,
{
let mut rng = rand::thread_rng();
Ok(eth_keystore::encrypt_key(
dir,
&mut rng,
*self.private_key,
password,
)?)
}
pub fn load_keystore<P, S>(
keypath: P,
password: S,
provider: Option<Provider>,
) -> WalletResult<Self>
where
P: AsRef<Path>,
S: AsRef<[u8]>,
{
let secret = eth_keystore::decrypt_key(keypath, password)?;
let secret_key = unsafe { SecretKey::from_slice_unchecked(&secret) };
Ok(Self::new_from_private_key(secret_key, provider))
}
}
impl ViewOnlyAccount for WalletUnlocked {
fn address(&self) -> &Bech32Address {
self.wallet.address()
}
fn try_provider(&self) -> AccountResult<&Provider> {
self.provider.as_ref().ok_or(AccountError::no_provider())
}
fn set_provider(&mut self, provider: Provider) -> &mut Self {
self.wallet.set_provider(provider);
self
}
}
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
impl Account for WalletUnlocked {
async fn get_asset_inputs_for_amount(
&self,
asset_id: AssetId,
amount: u64,
witness_index: Option<u8>,
) -> Result<Vec<Input>> {
Ok(self
.get_spendable_resources(asset_id, amount)
.await?
.into_iter()
.map(|resource| Input::resource_signed(resource, witness_index.unwrap_or_default()))
.collect::<Vec<Input>>())
}
async fn add_fee_resources<Tb: TransactionBuilder>(
&self,
mut tb: Tb,
previous_base_amount: u64,
witness_index: Option<u8>,
) -> Result<Tb::TxType> {
let consensus_parameters = self
.try_provider()?
.chain_info()
.await?
.consensus_parameters;
tb = tb.set_consensus_parameters(consensus_parameters);
let new_base_amount =
calculate_base_amount_with_fee(&tb, &consensus_parameters, previous_base_amount);
let new_base_inputs = self
.get_asset_inputs_for_amount(BASE_ASSET_ID, new_base_amount, witness_index)
.await?;
adjust_inputs(&mut tb, new_base_inputs);
adjust_outputs(&mut tb, self.address(), new_base_amount);
let mut tx = tb.build()?;
self.sign_transaction(&mut tx)?;
Ok(tx)
}
}
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl Signer for WalletUnlocked {
type Error = WalletError;
async fn sign_message<S: Send + Sync + AsRef<[u8]>>(
&self,
message: S,
) -> WalletResult<Signature> {
let message = Message::new(message);
let sig = Signature::sign(&self.private_key, &message);
Ok(sig)
}
fn sign_transaction(&self, tx: &mut impl Transaction) -> WalletResult<Signature> {
let id = tx.id();
let message = unsafe { Message::from_bytes_unchecked(*id) };
let sig = Signature::sign(&self.private_key, &message);
let witness = vec![Witness::from(sig.as_ref())];
let witnesses: &mut Vec<Witness> = tx.witnesses_mut();
match witnesses.len() {
0 => *witnesses = witness,
_ => {
witnesses.extend(witness);
}
}
Ok(sig)
}
}
impl fmt::Debug for Wallet {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Wallet")
.field("address", &self.address)
.finish()
}
}
impl ops::Deref for WalletUnlocked {
type Target = Wallet;
fn deref(&self) -> &Self::Target {
&self.wallet
}
}
pub fn generate_mnemonic_phrase<R: Rng>(rng: &mut R, count: usize) -> WalletResult<String> {
Ok(fuel_crypto::FuelMnemonic::generate_mnemonic_phrase(
rng, count,
)?)
}
#[cfg(test)]
mod tests {
use tempfile::tempdir;
use super::*;
#[tokio::test]
async fn encrypted_json_keystore() -> Result<()> {
let dir = tempdir()?;
let mut rng = rand::thread_rng();
let (wallet, uuid) = WalletUnlocked::new_from_keystore(&dir, &mut rng, "password", None)?;
let message = "Hello there!";
let signature = wallet.sign_message(message).await?;
let path = Path::new(dir.path()).join(uuid);
let recovered_wallet = WalletUnlocked::load_keystore(path.clone(), "password", None)?;
let signature2 = recovered_wallet.sign_message(message).await?;
assert_eq!(signature, signature2);
assert!(std::fs::remove_file(&path).is_ok());
Ok(())
}
#[tokio::test]
async fn mnemonic_generation() -> Result<()> {
let mnemonic = generate_mnemonic_phrase(&mut rand::thread_rng(), 12)?;
let _wallet = WalletUnlocked::new_from_mnemonic_phrase(&mnemonic, None)?;
Ok(())
}
#[tokio::test]
async fn wallet_from_mnemonic_phrase() -> Result<()> {
let phrase =
"oblige salon price punch saddle immune slogan rare snap desert retire surprise";
let wallet =
WalletUnlocked::new_from_mnemonic_phrase_with_path(phrase, None, "m/44'/60'/0'/0/0")?;
let expected_plain_address =
"df9d0e6c6c5f5da6e82e5e1a77974af6642bdb450a10c43f0c6910a212600185";
let expected_address = "fuel1m7wsumrvtaw6d6pwtcd809627ejzhk69pggvg0cvdyg2yynqqxzseuzply";
assert_eq!(wallet.address().hash().to_string(), expected_plain_address);
assert_eq!(wallet.address().to_string(), expected_address);
let wallet2 =
WalletUnlocked::new_from_mnemonic_phrase_with_path(phrase, None, "m/44'/60'/1'/0/0")?;
let expected_second_plain_address =
"261191b0164a24fd0fd51566ec5e5b0b9ba8fb2d42dc9cf7dbbd6f23d2742759";
let expected_second_address =
"fuel1ycgervqkfgj06r74z4nwchjmpwd637edgtwfea7mh4hj85n5yavszjk4cc";
assert_eq!(
wallet2.address().hash().to_string(),
expected_second_plain_address
);
assert_eq!(wallet2.address().to_string(), expected_second_address);
Ok(())
}
#[tokio::test]
async fn encrypt_and_store_wallet_from_mnemonic() -> Result<()> {
let dir = tempdir()?;
let phrase =
"oblige salon price punch saddle immune slogan rare snap desert retire surprise";
let wallet =
WalletUnlocked::new_from_mnemonic_phrase_with_path(phrase, None, "m/44'/60'/0'/0/0")?;
let uuid = wallet.encrypt(&dir, "password")?;
let path = Path::new(dir.path()).join(uuid);
let recovered_wallet = WalletUnlocked::load_keystore(&path, "password", None)?;
assert_eq!(wallet.address(), recovered_wallet.address());
assert!(std::fs::remove_file(&path).is_ok());
Ok(())
}
}