-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadapter.rs
More file actions
169 lines (150 loc) · 5.49 KB
/
adapter.rs
File metadata and controls
169 lines (150 loc) · 5.49 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
//! Types for converting ETH-like types to Substrate types and vice versa
use alloy_primitives::Address;
use sp_core::{blake2_128, blake2_256, twox_128, twox_256, twox_64};
use subxt::{metadata::types::StorageHasher, utils::AccountId32};
/// Reserved address for generic SCALE-encoded RuntimeCall dispatch
/// When EthereumTransaction.to == GENERIC_CALL_ADDRESS, the transaction.data
/// field contains a SCALE-encoded RuntimeCall that will be decoded and dispatched.
#[allow(dead_code)] // Used in tests and for documentation
pub const GENERIC_CALL_ADDRESS: Address = Address::ZERO;
use subeth_primitives::AddressMapping as PrimitiveAddressMapping;
pub use subeth_primitives::PalletContractMapping;
/// Address mapping logic
pub(crate) struct AddressMapping;
impl AddressMapping {
/// Hash `AccountId20` to get `AccountId32`
pub fn to_ss58(address: Address) -> AccountId32 {
let hash = PrimitiveAddressMapping::to_ss58(address);
AccountId32::from(hash)
}
/// Truncate `AccountId32` to get `AccountId20`
pub fn to_address(account_id: AccountId32) -> Address {
let inner: &[u8; 32] = account_id.as_ref();
PrimitiveAddressMapping::to_address(inner)
}
}
/// Pallet storage read structure
#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
pub struct StorageKey {
/// Storage item name
pub name: String,
/// The rest of the keys, could be multiple if n-map storage
pub keys: Vec<Vec<u8>>,
}
/// Hash the key to get the storage key
pub fn hash_key(key: &[u8], hasher: &StorageHasher) -> Vec<u8> {
match hasher {
StorageHasher::Blake2_128 => blake2_128(key).to_vec(),
StorageHasher::Blake2_256 => blake2_256(key).to_vec(),
StorageHasher::Blake2_128Concat => {
let hash = blake2_128(key);
let mut result = Vec::with_capacity(32 + key.len());
result.extend_from_slice(&hash);
result.extend_from_slice(key);
result
}
StorageHasher::Twox128 => twox_128(key).to_vec(),
StorageHasher::Twox256 => twox_256(key).to_vec(),
StorageHasher::Twox64Concat => {
let hash = twox_64(key);
let mut result = Vec::with_capacity(8 + key.len());
result.extend_from_slice(&hash);
result.extend_from_slice(key);
result
}
StorageHasher::Identity => key.to_vec(),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_storage_key_works() {
let key = StorageKey {
name: "Account".to_string(),
keys: vec![hex::decode(
"5e51610a8f5b5e04bf2f28960aa138df2b7531d0dbea936336ed40a204c92e12",
)
.unwrap()],
};
assert_eq!(
serde_json::to_string(&key).unwrap(),
r#"{"name":"Account","keys":[[94,81,97,10,143,91,94,4,191,47,40,150,10,161,56,223,43,117,49,208,219,234,147,99,54,237,64,162,4,201,46,18]]}"#
);
let key = StorageKey {
name: "ActiveEra".to_string(),
keys: vec![],
};
assert_eq!(
serde_json::to_string(&key).unwrap(),
r#"{"name":"ActiveEra","keys":[]}"#
);
}
#[test]
fn test_hash_and_truncate() {
let address = Address::from([1u8; 20]);
let expected_account_id =
hex::decode("8b304616ddedac8267d0381d53301825902eb056a70fc56b90e84efa492a015b")
.unwrap();
let account_id = AddressMapping::to_ss58(address);
let account_id_raw: &[u8] = account_id.as_ref();
assert_eq!(account_id_raw, expected_account_id.as_slice());
let account_id = AccountId32::from([1u8; 32]);
let new_address = AddressMapping::to_address(account_id);
assert_eq!(address, new_address);
}
#[test]
fn test_pallet_mapping_works() {
let balances = PalletContractMapping::contract_address("Balances");
let system = PalletContractMapping::contract_address("System");
let staking = PalletContractMapping::contract_address("Staking");
let democracy = PalletContractMapping::contract_address("democrac");
let treasury = PalletContractMapping::contract_address("treasury");
assert_eq!(
system,
"0x53797374656d0000000000000000000000000000"
.parse::<Address>()
.unwrap()
);
assert_eq!(
balances,
"0x42616c616e636573000000000000000000000000"
.parse::<Address>()
.unwrap()
);
assert_eq!(
staking,
"0x5374616b696e6700000000000000000000000000"
.parse::<Address>()
.unwrap()
);
assert_eq!(
democracy,
"0x64656d6f63726163000000000000000000000000"
.parse::<Address>()
.unwrap()
);
assert_eq!(
treasury,
"0x7472656173757279000000000000000000000000"
.parse::<Address>()
.unwrap()
);
assert_eq!(
PalletContractMapping::pallet_name(balances).unwrap(),
"Balances"
);
assert_eq!(
PalletContractMapping::pallet_name(staking).unwrap(),
"Staking"
);
assert_eq!(
PalletContractMapping::pallet_name(democracy).unwrap(),
"democrac"
);
assert_eq!(
PalletContractMapping::pallet_name(treasury).unwrap(),
"treasury"
);
}
}