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
extern crate core;
pub mod provider;
pub mod wallet;
#[doc(no_inline)]
pub use fuel_crypto;
use async_trait::async_trait;
use fuel_crypto::Signature;
use fuels_core::tx::{field, Cacheable, UniqueIdentifier};
use fuels_types::bech32::Bech32Address;
use std::error::Error;
pub use wallet::{Wallet, WalletUnlocked};
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
pub trait Signer: std::fmt::Debug + Send + Sync {
type Error: Error + Send + Sync;
async fn sign_message<S: Send + Sync + AsRef<[u8]>>(
&self,
message: S,
) -> Result<Signature, Self::Error>;
async fn sign_transaction<Tx: Cacheable + UniqueIdentifier + field::Witnesses + Send>(
&self,
message: &mut Tx,
) -> Result<Signature, Self::Error>;
fn address(&self) -> &Bech32Address;
}
#[cfg(test)]
#[cfg(feature = "test-helpers")]
mod tests {
use fuel_crypto::{Message, SecretKey};
use fuels_core::constants::BASE_ASSET_ID;
use fuels_core::{
parameters::TxParameters,
tx::{
field::Maturity, Address, AssetId, Bytes32, Chargeable, Input, Output, Transaction,
TxPointer, UtxoId,
},
};
use fuels_test_helpers::{setup_single_asset_coins, setup_test_client};
use rand::{rngs::StdRng, RngCore, SeedableRng};
use std::str::FromStr;
use crate::provider::Provider;
use crate::wallet::WalletUnlocked;
use super::*;
#[tokio::test]
async fn sign_and_verify() -> Result<(), Box<dyn Error>> {
let mut rng = StdRng::seed_from_u64(2322u64);
let mut secret_seed = [0u8; 32];
rng.fill_bytes(&mut secret_seed);
let secret = unsafe { SecretKey::from_bytes_unchecked(secret_seed) };
let wallet = WalletUnlocked::new_from_private_key(secret, None);
let message = "my message";
let signature = wallet.sign_message(message).await?;
assert_eq!(signature, Signature::from_str("0x8eeb238db1adea4152644f1cd827b552dfa9ab3f4939718bb45ca476d167c6512a656f4d4c7356bfb9561b14448c230c6e7e4bd781df5ee9e5999faa6495163d")?);
let message = Message::new(message);
let recovered_address = signature.recover(&message)?;
assert_eq!(wallet.address().hash(), recovered_address.hash());
signature.verify(&recovered_address, &message)?;
Ok(())
}
#[tokio::test]
async fn sign_tx_and_verify() -> Result<(), Box<dyn Error>> {
let secret = SecretKey::from_str(
"5f70feeff1f229e4a95e1056e8b4d80d0b24b565674860cc213bdb07127ce1b1",
)?;
let wallet = WalletUnlocked::new_from_private_key(secret, None);
let input_coin = Input::coin_signed(
UtxoId::new(Bytes32::zeroed(), 0),
Address::from_str(
"0xf1e92c42b90934aa6372e30bc568a326f6e66a1a0288595e6e3fbd392a4f3e6e",
)?,
10000000,
AssetId::from([0u8; 32]),
TxPointer::default(),
0,
0,
);
let output_coin = Output::coin(
Address::from_str(
"0xc7862855b418ba8f58878db434b21053a61a2025209889cc115989e8040ff077",
)?,
1,
AssetId::from([0u8; 32]),
);
let mut tx = Transaction::script(
0,
1000000,
0,
hex::decode("24400000")?,
vec![],
vec![input_coin],
vec![output_coin],
vec![],
);
let signature = wallet.sign_transaction(&mut tx).await?;
let message = unsafe { Message::from_bytes_unchecked(*tx.id()) };
assert_eq!(signature, Signature::from_str("34482a581d1fe01ba84900581f5321a8b7d4ec65c3e7ca0de318ff8fcf45eb2c793c4b99e96400673e24b81b7aa47f042cad658f05a84e2f96f365eb0ce5a511")?);
let recovered_address = signature.recover(&message)?;
assert_eq!(wallet.address().hash(), recovered_address.hash());
signature.verify(&recovered_address, &message)?;
Ok(())
}
#[tokio::test]
async fn send_transfer_transactions() -> Result<(), fuels_types::errors::Error> {
let mut wallet_1 = WalletUnlocked::new_random(None);
let mut wallet_2 = WalletUnlocked::new_random(None).lock();
let mut coins_1 = setup_single_asset_coins(wallet_1.address(), BASE_ASSET_ID, 1, 1000000);
let coins_2 = setup_single_asset_coins(wallet_2.address(), BASE_ASSET_ID, 1, 1000000);
coins_1.extend(coins_2);
let (client, _) = setup_test_client(coins_1, vec![], None, None, None).await;
let provider = Provider::new(client);
wallet_1.set_provider(provider.clone());
wallet_2.set_provider(provider);
let wallet_1_initial_coins = wallet_1.get_coins(BASE_ASSET_ID).await?;
let wallet_2_initial_coins = wallet_2.get_coins(BASE_ASSET_ID).await?;
assert_eq!(wallet_1_initial_coins.len(), 1);
assert_eq!(wallet_2_initial_coins.len(), 1);
let gas_price = 1;
let gas_limit = 500_000;
let maturity = 0;
let tx_params = TxParameters {
gas_price,
gas_limit,
maturity,
};
let (tx_id, _receipts) = wallet_1
.transfer(wallet_2.address(), 1, Default::default(), tx_params)
.await?;
let res = wallet_1
.get_provider()?
.get_transaction_by_id(&tx_id)
.await?
.unwrap();
let script = res.transaction.as_script().cloned().unwrap();
assert_eq!(script.limit(), gas_limit);
assert_eq!(script.price(), gas_price);
assert_eq!(*script.maturity(), maturity);
let wallet_1_spendable_resources =
wallet_1.get_spendable_resources(BASE_ASSET_ID, 0).await?;
let wallet_1_all_coins = wallet_1.get_coins(BASE_ASSET_ID).await?;
let wallet_2_all_coins = wallet_2.get_coins(BASE_ASSET_ID).await?;
assert_eq!(wallet_1_spendable_resources.len(), 0);
assert_eq!(wallet_1_all_coins.len(), 1);
assert_eq!(wallet_2_all_coins.len(), 2);
let response = wallet_1
.transfer(
wallet_2.address(),
2000000,
Default::default(),
TxParameters::default(),
)
.await;
assert!(response.is_err());
let wallet_2_coins = wallet_2.get_coins(BASE_ASSET_ID).await?;
assert_eq!(wallet_2_coins.len(), 2); Ok(())
}
#[tokio::test]
async fn transfer_coins_with_change() -> Result<(), fuels_types::errors::Error> {
let mut wallet_1 = WalletUnlocked::new_random(None);
let mut wallet_2 = WalletUnlocked::new_random(None).lock();
let mut coins_1 = setup_single_asset_coins(wallet_1.address(), BASE_ASSET_ID, 1, 5);
let coins_2 = setup_single_asset_coins(wallet_2.address(), BASE_ASSET_ID, 1, 5);
coins_1.extend(coins_2);
let (client, _) = setup_test_client(coins_1, vec![], None, None, None).await;
let provider = Provider::new(client);
wallet_1.set_provider(provider.clone());
wallet_2.set_provider(provider);
let wallet_1_initial_coins = wallet_1.get_coins(BASE_ASSET_ID).await?;
let wallet_2_initial_coins = wallet_2.get_coins(BASE_ASSET_ID).await?;
assert_eq!(wallet_1_initial_coins.len(), 1);
assert_eq!(wallet_2_initial_coins.len(), 1);
let _receipts = wallet_1
.transfer(
wallet_2.address(),
2,
Default::default(),
TxParameters::default(),
)
.await?;
let wallet_1_final_coins = wallet_1.get_coins(BASE_ASSET_ID).await?;
let resulting_amount = wallet_1_final_coins.first().unwrap();
assert_eq!(resulting_amount.amount, 3);
let wallet_2_final_coins = wallet_2.get_coins(BASE_ASSET_ID).await?;
assert_eq!(wallet_2_final_coins.len(), 2);
let total_amount: u64 = wallet_2_final_coins.iter().map(|c| c.amount).sum();
assert_eq!(total_amount, 7);
Ok(())
}
}