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
use ethsign::{KeyFile, Protected, SecretKey, Signature};
use once_cell::sync::Lazy;
use web3::signing::keccak256;
use crate::{Adapter, LockedState, UnlockedState};
pub use {
client::{ChainTransport, Ethereum, Options},
error::Error,
};
pub type UnlockedAdapter = Adapter<client::Ethereum<UnlockedWallet>, UnlockedState>;
pub type LockedAdapter = Adapter<client::Ethereum<LockedWallet>, LockedState>;
pub type LockedClient = Ethereum<LockedWallet>;
pub type UnlockedClient = Ethereum<UnlockedWallet>;
mod channel;
mod client;
mod error;
pub mod ewt;
#[cfg(any(test, feature = "test-util"))]
#[cfg_attr(docsrs, doc(cfg(feature = "test-util")))]
pub mod test_util;
pub static OUTPACE_ABI: Lazy<&'static [u8]> =
Lazy::new(|| include_bytes!("../../lib/protocol-eth/abi/OUTPACE.json"));
pub static ERC20_ABI: Lazy<&'static [u8]> = Lazy::new(|| {
include_str!("../../lib/protocol-eth/abi/ERC20.json")
.trim_end_matches('\n')
.as_bytes()
});
pub static SWEEPER_ABI: Lazy<&'static [u8]> =
Lazy::new(|| include_bytes!("../../lib/protocol-eth/abi/Sweeper.json"));
pub static IDENTITY_ABI: Lazy<&'static [u8]> =
Lazy::new(|| include_bytes!("../../lib/protocol-eth/abi/Identity5.2.json"));
fn to_ethereum_signed(message: &[u8]) -> [u8; 32] {
let eth = "\x19Ethereum Signed Message:\n";
let message_length = message.len();
let mut bytes = format!("{}{}", eth, message_length).into_bytes();
bytes.extend(message);
keccak256(&bytes)
}
pub trait Electrum {
fn to_electrum(&self) -> [u8; 65];
fn from_electrum(data: &[u8]) -> Option<Self>
where
Self: Sized;
}
impl Electrum for Signature {
fn to_electrum(&self) -> [u8; 65] {
let mut electrum_array = [0_u8; 65];
electrum_array[0..32].copy_from_slice(&self.r);
electrum_array[32..64].copy_from_slice(&self.s);
electrum_array[64] = self.v + 27;
electrum_array
}
fn from_electrum(sig: &[u8]) -> Option<Self> {
if sig.len() != 65 || sig[64] < 27 {
return None;
}
let mut r = [0u8; 32];
r.copy_from_slice(&sig[0..32]);
let mut s = [0u8; 32];
s.copy_from_slice(&sig[32..64]);
let v = sig[64] - 27;
Some(Signature { v, r, s })
}
}
#[derive(Debug, Clone)]
pub struct UnlockedWallet {
wallet: SecretKey,
}
#[derive(Debug, Clone)]
pub enum LockedWallet {
KeyStore {
keystore: KeyFile,
password: Protected,
},
PrivateKey(String),
}
pub trait WalletState: Send + Sync {}
impl WalletState for UnlockedWallet {}
impl WalletState for LockedWallet {}