use super::Location;
pub use crate::v3::{BodyId, BodyPart};
use crate::{
v3::{Junction as OldJunction, NetworkId as OldNetworkId},
v5::{Junction as NewJunction, NetworkId as NewNetworkId},
VersionedLocation,
};
use bounded_collections::{BoundedSlice, BoundedVec, ConstU32};
use codec::{self, Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
use serde::{Deserialize, Serialize};
#[derive(
Copy,
Clone,
Eq,
PartialEq,
Ord,
PartialOrd,
Encode,
Decode,
Debug,
TypeInfo,
MaxEncodedLen,
Serialize,
Deserialize,
)]
pub enum Junction {
Parachain(#[codec(compact)] u32),
AccountId32 { network: Option<NetworkId>, id: [u8; 32] },
AccountIndex64 {
network: Option<NetworkId>,
#[codec(compact)]
index: u64,
},
AccountKey20 { network: Option<NetworkId>, key: [u8; 20] },
PalletInstance(u8),
GeneralIndex(#[codec(compact)] u128),
GeneralKey { length: u8, data: [u8; 32] },
OnlyChild,
Plurality { id: BodyId, part: BodyPart },
GlobalConsensus(NetworkId),
}
impl From<NewNetworkId> for Option<NetworkId> {
fn from(new: NewNetworkId) -> Self {
Some(NetworkId::from(new))
}
}
impl From<NewNetworkId> for NetworkId {
fn from(new: NewNetworkId) -> Self {
use NewNetworkId::*;
match new {
ByGenesis(hash) => Self::ByGenesis(hash),
ByFork { block_number, block_hash } => Self::ByFork { block_number, block_hash },
Polkadot => Self::Polkadot,
Kusama => Self::Kusama,
Ethereum { chain_id } => Self::Ethereum { chain_id },
BitcoinCore => Self::BitcoinCore,
BitcoinCash => Self::BitcoinCash,
PolkadotBulletin => Self::PolkadotBulletin,
}
}
}
#[derive(
Copy,
Clone,
Eq,
PartialEq,
Ord,
PartialOrd,
Encode,
Decode,
Debug,
TypeInfo,
MaxEncodedLen,
Serialize,
Deserialize,
)]
pub enum NetworkId {
ByGenesis([u8; 32]),
ByFork { block_number: u64, block_hash: [u8; 32] },
Polkadot,
Kusama,
Westend,
Rococo,
Wococo,
Ethereum {
#[codec(compact)]
chain_id: u64,
},
BitcoinCore,
BitcoinCash,
PolkadotBulletin,
}
impl From<OldNetworkId> for Option<NetworkId> {
fn from(old: OldNetworkId) -> Self {
Some(NetworkId::from(old))
}
}
impl From<OldNetworkId> for NetworkId {
fn from(old: OldNetworkId) -> Self {
use OldNetworkId::*;
match old {
ByGenesis(hash) => Self::ByGenesis(hash),
ByFork { block_number, block_hash } => Self::ByFork { block_number, block_hash },
Polkadot => Self::Polkadot,
Kusama => Self::Kusama,
Westend => Self::Westend,
Rococo => Self::Rococo,
Wococo => Self::Wococo,
Ethereum { chain_id } => Self::Ethereum { chain_id },
BitcoinCore => Self::BitcoinCore,
BitcoinCash => Self::BitcoinCash,
PolkadotBulletin => Self::PolkadotBulletin,
}
}
}
impl From<NetworkId> for Junction {
fn from(n: NetworkId) -> Self {
Self::GlobalConsensus(n)
}
}
impl From<[u8; 32]> for Junction {
fn from(id: [u8; 32]) -> Self {
Self::AccountId32 { network: None, id }
}
}
impl From<BoundedVec<u8, ConstU32<32>>> for Junction {
fn from(key: BoundedVec<u8, ConstU32<32>>) -> Self {
key.as_bounded_slice().into()
}
}
impl<'a> From<BoundedSlice<'a, u8, ConstU32<32>>> for Junction {
fn from(key: BoundedSlice<'a, u8, ConstU32<32>>) -> Self {
let mut data = [0u8; 32];
data[..key.len()].copy_from_slice(&key[..]);
Self::GeneralKey { length: key.len() as u8, data }
}
}
impl<'a> TryFrom<&'a Junction> for BoundedSlice<'a, u8, ConstU32<32>> {
type Error = ();
fn try_from(key: &'a Junction) -> Result<Self, ()> {
match key {
Junction::GeneralKey { length, data } =>
BoundedSlice::try_from(&data[..data.len().min(*length as usize)]).map_err(|_| ()),
_ => Err(()),
}
}
}
impl From<[u8; 20]> for Junction {
fn from(key: [u8; 20]) -> Self {
Self::AccountKey20 { network: None, key }
}
}
impl From<u64> for Junction {
fn from(index: u64) -> Self {
Self::AccountIndex64 { network: None, index }
}
}
impl From<u128> for Junction {
fn from(id: u128) -> Self {
Self::GeneralIndex(id)
}
}
impl TryFrom<OldJunction> for Junction {
type Error = ();
fn try_from(value: OldJunction) -> Result<Self, ()> {
use OldJunction::*;
Ok(match value {
Parachain(id) => Self::Parachain(id),
AccountId32 { network: maybe_network, id } =>
Self::AccountId32 { network: maybe_network.map(|network| network.into()), id },
AccountIndex64 { network: maybe_network, index } =>
Self::AccountIndex64 { network: maybe_network.map(|network| network.into()), index },
AccountKey20 { network: maybe_network, key } =>
Self::AccountKey20 { network: maybe_network.map(|network| network.into()), key },
PalletInstance(index) => Self::PalletInstance(index),
GeneralIndex(id) => Self::GeneralIndex(id),
GeneralKey { length, data } => Self::GeneralKey { length, data },
OnlyChild => Self::OnlyChild,
Plurality { id, part } => Self::Plurality { id, part },
GlobalConsensus(network) => Self::GlobalConsensus(network.into()),
})
}
}
impl TryFrom<NewJunction> for Junction {
type Error = ();
fn try_from(value: NewJunction) -> Result<Self, Self::Error> {
use NewJunction::*;
Ok(match value {
Parachain(id) => Self::Parachain(id),
AccountId32 { network: maybe_network, id } =>
Self::AccountId32 { network: maybe_network.map(|network| network.into()), id },
AccountIndex64 { network: maybe_network, index } =>
Self::AccountIndex64 { network: maybe_network.map(|network| network.into()), index },
AccountKey20 { network: maybe_network, key } =>
Self::AccountKey20 { network: maybe_network.map(|network| network.into()), key },
PalletInstance(index) => Self::PalletInstance(index),
GeneralIndex(id) => Self::GeneralIndex(id),
GeneralKey { length, data } => Self::GeneralKey { length, data },
OnlyChild => Self::OnlyChild,
Plurality { id, part } => Self::Plurality { id, part },
GlobalConsensus(network) => Self::GlobalConsensus(network.into()),
})
}
}
impl Junction {
pub fn into_location(self) -> Location {
Location::new(0, [self])
}
pub fn into_exterior(self, n: u8) -> Location {
Location::new(n, [self])
}
pub fn into_versioned(self) -> VersionedLocation {
self.into_location().into_versioned()
}
pub fn remove_network_id(&mut self) {
use Junction::*;
match self {
AccountId32 { ref mut network, .. } |
AccountIndex64 { ref mut network, .. } |
AccountKey20 { ref mut network, .. } => *network = None,
_ => {},
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use alloc::vec;
#[test]
fn junction_round_trip_works() {
let j = Junction::GeneralKey { length: 32, data: [1u8; 32] };
let k = Junction::try_from(OldJunction::try_from(j).unwrap()).unwrap();
assert_eq!(j, k);
let j = OldJunction::GeneralKey { length: 32, data: [1u8; 32] };
let k = OldJunction::try_from(Junction::try_from(j).unwrap()).unwrap();
assert_eq!(j, k);
let j = Junction::from(BoundedVec::try_from(vec![1u8, 2, 3, 4]).unwrap());
let k = Junction::try_from(OldJunction::try_from(j).unwrap()).unwrap();
assert_eq!(j, k);
let s: BoundedSlice<_, _> = (&k).try_into().unwrap();
assert_eq!(s, &[1u8, 2, 3, 4][..]);
let j = OldJunction::GeneralKey { length: 32, data: [1u8; 32] };
let k = OldJunction::try_from(Junction::try_from(j).unwrap()).unwrap();
assert_eq!(j, k);
}
}