/
lib.rs
69 lines (51 loc) · 2.14 KB
/
lib.rs
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
mod wallet;
pub use wallet::Wallet;
use async_trait::async_trait;
use fuel_tx::Transaction;
use fuel_types::Address;
use fuels_core::signature::Signature;
use std::error::Error;
/// A wallet instantiated with a locally stored private key
pub type LocalWallet = Wallet;
/// Trait for signing transactions and messages
///
/// Implement this trait to support different signing modes, e.g. Ledger, hosted etc.
#[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;
/// Signs the hash of the provided message
async fn sign_message<S: Send + Sync + AsRef<[u8]>>(
&self,
message: S,
) -> Result<Signature, Self::Error>;
/// Signs the transaction
async fn sign_transaction(&self, message: &Transaction) -> Result<Signature, Self::Error>;
/// Returns the signer's Fuel Address
fn address(&self) -> Address;
}
#[cfg(test)]
mod tests {
use std::str::FromStr;
use rand::{rngs::StdRng, RngCore, SeedableRng};
use secp256k1::SecretKey;
use super::*;
#[tokio::test]
async fn sign_and_verify() {
let mut rng = StdRng::seed_from_u64(2322u64);
let mut secret_seed = [0u8; 32];
rng.fill_bytes(&mut secret_seed);
let secret =
SecretKey::from_slice(&secret_seed).expect("Failed to generate random secret!");
let wallet = LocalWallet::new_from_private_key(secret).unwrap();
let message = "my message";
let signature = wallet.sign_message(message.as_bytes()).await.unwrap();
// Check if signature is what we expect it to be
assert_eq!(signature.compact, Signature::from_str("0x8eeb238db1adea4152644f1cd827b552dfa9ab3f4939718bb45ca476d167c6512a656f4d4c7356bfb9561b14448c230c6e7e4bd781df5ee9e5999faa6495163d").unwrap().compact);
// Recover address that signed the message
let recovered_address = signature.recover(message).unwrap();
assert_eq!(wallet.address, recovered_address);
// Verify signature
signature.verify(message, recovered_address).unwrap();
}
}