1use super::Location;
20pub use crate::v4::{BodyId, BodyPart};
21use crate::{
22 v4::{Junction as OldJunction, NetworkId as OldNetworkId},
23 VersionedLocation,
24};
25use bounded_collections::{BoundedSlice, BoundedVec, ConstU32};
26use codec::{self, Decode, DecodeWithMemTracking, Encode, MaxEncodedLen};
27use hex_literal::hex;
28use scale_info::TypeInfo;
29use serde::{Deserialize, Serialize};
30
31#[derive(
35 Copy,
36 Clone,
37 Eq,
38 PartialEq,
39 Ord,
40 PartialOrd,
41 Encode,
42 Decode,
43 DecodeWithMemTracking,
44 Debug,
45 TypeInfo,
46 MaxEncodedLen,
47 Serialize,
48 Deserialize,
49)]
50pub enum Junction {
51 Parachain(#[codec(compact)] u32),
55 AccountId32 { network: Option<NetworkId>, id: [u8; 32] },
60 AccountIndex64 {
65 network: Option<NetworkId>,
66 #[codec(compact)]
67 index: u64,
68 },
69 AccountKey20 { network: Option<NetworkId>, key: [u8; 20] },
74 PalletInstance(u8),
78 GeneralIndex(#[codec(compact)] u128),
84 GeneralKey { length: u8, data: [u8; 32] },
93 OnlyChild,
97 Plurality { id: BodyId, part: BodyPart },
102 GlobalConsensus(NetworkId),
105}
106
107pub const WESTEND_GENESIS_HASH: [u8; 32] =
109 hex!["e143f23803ac50e8f6f8e62695d1ce9e4e1d68aa36c1cd2cfd15340213f3423e"];
110
111pub const ROCOCO_GENESIS_HASH: [u8; 32] =
113 hex!["6408de7737c59c238890533af25896a2c20608d8b380bb01029acb392781063e"];
114
115pub const DUMMY_GENESIS_HASH: [u8; 32] = [0; 32];
117
118#[derive(
123 Copy,
124 Clone,
125 Eq,
126 PartialEq,
127 Ord,
128 PartialOrd,
129 Encode,
130 Decode,
131 DecodeWithMemTracking,
132 Debug,
133 TypeInfo,
134 MaxEncodedLen,
135 Serialize,
136 Deserialize,
137)]
138pub enum NetworkId {
139 ByGenesis([u8; 32]),
141 ByFork { block_number: u64, block_hash: [u8; 32] },
143 Polkadot,
145 Kusama,
147 #[codec(index = 7)]
149 Ethereum {
150 #[codec(compact)]
152 chain_id: u64,
153 },
154 #[codec(index = 8)]
156 BitcoinCore,
157 #[codec(index = 9)]
159 BitcoinCash,
160 #[codec(index = 10)]
162 PolkadotBulletin,
163}
164
165impl From<OldNetworkId> for Option<NetworkId> {
166 fn from(old: OldNetworkId) -> Self {
167 Some(NetworkId::from(old))
168 }
169}
170
171impl From<OldNetworkId> for NetworkId {
172 fn from(old: OldNetworkId) -> Self {
173 use OldNetworkId::*;
174 match old {
175 ByGenesis(hash) => Self::ByGenesis(hash),
176 ByFork { block_number, block_hash } => Self::ByFork { block_number, block_hash },
177 Polkadot => Self::Polkadot,
178 Kusama => Self::Kusama,
179 Westend => Self::ByGenesis(WESTEND_GENESIS_HASH),
180 Rococo => Self::ByGenesis(ROCOCO_GENESIS_HASH),
181 Wococo => Self::ByGenesis(DUMMY_GENESIS_HASH),
182 Ethereum { chain_id } => Self::Ethereum { chain_id },
183 BitcoinCore => Self::BitcoinCore,
184 BitcoinCash => Self::BitcoinCash,
185 PolkadotBulletin => Self::PolkadotBulletin,
186 }
187 }
188}
189
190impl From<NetworkId> for Junction {
191 fn from(n: NetworkId) -> Self {
192 Self::GlobalConsensus(n)
193 }
194}
195
196impl From<[u8; 32]> for Junction {
197 fn from(id: [u8; 32]) -> Self {
198 Self::AccountId32 { network: None, id }
199 }
200}
201
202impl From<BoundedVec<u8, ConstU32<32>>> for Junction {
203 fn from(key: BoundedVec<u8, ConstU32<32>>) -> Self {
204 key.as_bounded_slice().into()
205 }
206}
207
208impl<'a> From<BoundedSlice<'a, u8, ConstU32<32>>> for Junction {
209 fn from(key: BoundedSlice<'a, u8, ConstU32<32>>) -> Self {
210 let mut data = [0u8; 32];
211 data[..key.len()].copy_from_slice(&key[..]);
212 Self::GeneralKey { length: key.len() as u8, data }
213 }
214}
215
216impl<'a> TryFrom<&'a Junction> for BoundedSlice<'a, u8, ConstU32<32>> {
217 type Error = ();
218 fn try_from(key: &'a Junction) -> Result<Self, ()> {
219 match key {
220 Junction::GeneralKey { length, data } => {
221 BoundedSlice::try_from(&data[..data.len().min(*length as usize)]).map_err(|_| ())
222 },
223 _ => Err(()),
224 }
225 }
226}
227
228impl From<[u8; 20]> for Junction {
229 fn from(key: [u8; 20]) -> Self {
230 Self::AccountKey20 { network: None, key }
231 }
232}
233
234impl From<u64> for Junction {
235 fn from(index: u64) -> Self {
236 Self::AccountIndex64 { network: None, index }
237 }
238}
239
240impl From<u128> for Junction {
241 fn from(id: u128) -> Self {
242 Self::GeneralIndex(id)
243 }
244}
245
246impl TryFrom<OldJunction> for Junction {
247 type Error = ();
248 fn try_from(value: OldJunction) -> Result<Self, ()> {
249 use OldJunction::*;
250 Ok(match value {
251 Parachain(id) => Self::Parachain(id),
252 AccountId32 { network: maybe_network, id } => {
253 Self::AccountId32 { network: maybe_network.map(|network| network.into()), id }
254 },
255 AccountIndex64 { network: maybe_network, index } => {
256 Self::AccountIndex64 { network: maybe_network.map(|network| network.into()), index }
257 },
258 AccountKey20 { network: maybe_network, key } => {
259 Self::AccountKey20 { network: maybe_network.map(|network| network.into()), key }
260 },
261 PalletInstance(index) => Self::PalletInstance(index),
262 GeneralIndex(id) => Self::GeneralIndex(id),
263 GeneralKey { length, data } => Self::GeneralKey { length, data },
264 OnlyChild => Self::OnlyChild,
265 Plurality { id, part } => Self::Plurality { id, part },
266 GlobalConsensus(network) => Self::GlobalConsensus(network.into()),
267 })
268 }
269}
270
271impl Junction {
272 pub fn into_location(self) -> Location {
276 Location::new(0, [self])
277 }
278
279 pub fn into_exterior(self, n: u8) -> Location {
284 Location::new(n, [self])
285 }
286
287 pub fn into_versioned(self) -> VersionedLocation {
291 self.into_location().into_versioned()
292 }
293
294 pub fn remove_network_id(&mut self) {
296 use Junction::*;
297 match self {
298 AccountId32 { ref mut network, .. } |
299 AccountIndex64 { ref mut network, .. } |
300 AccountKey20 { ref mut network, .. } => *network = None,
301 _ => {},
302 }
303 }
304}
305
306#[cfg(test)]
307mod tests {
308 use super::*;
309 use alloc::vec;
310
311 #[test]
312 fn junction_round_trip_works() {
313 let j = Junction::GeneralKey { length: 32, data: [1u8; 32] };
314 let k = Junction::try_from(OldJunction::try_from(j).unwrap()).unwrap();
315 assert_eq!(j, k);
316
317 let j = OldJunction::GeneralKey { length: 32, data: [1u8; 32] };
318 let k = OldJunction::try_from(Junction::try_from(j).unwrap()).unwrap();
319 assert_eq!(j, k);
320
321 let j = Junction::from(BoundedVec::try_from(vec![1u8, 2, 3, 4]).unwrap());
322 let k = Junction::try_from(OldJunction::try_from(j).unwrap()).unwrap();
323 assert_eq!(j, k);
324 let s: BoundedSlice<_, _> = (&k).try_into().unwrap();
325 assert_eq!(s, &[1u8, 2, 3, 4][..]);
326
327 let j = OldJunction::GeneralKey { length: 32, data: [1u8; 32] };
328 let k = OldJunction::try_from(Junction::try_from(j).unwrap()).unwrap();
329 assert_eq!(j, k);
330 }
331}