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
use crate::error::Error; use crate::service::{Service, ServiceParseError}; use crate::{AbstractClient, Identity}; use core::fmt::{self, Debug}; use sp_core::crypto::{Pair, PublicError, SecretStringError, Ss58Codec}; use std::str::FromStr; use substrate_subxt::{sp_core, system::System, Runtime}; use thiserror::Error; #[derive(Clone)] pub struct Suri<P: Pair>(pub P::Seed); impl<P: Pair> Debug for Suri<P> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "*****") } } #[derive(Debug, Error)] #[error("Invalid suri encoded key pair: {0:?}")] pub struct InvalidSuri(SecretStringError); impl<P: Pair> FromStr for Suri<P> { type Err = InvalidSuri; fn from_str(string: &str) -> Result<Self, Self::Err> { let (_, seed) = P::from_string_with_seed(string, None).map_err(InvalidSuri)?; Ok(Self(seed.unwrap())) } } #[derive(Clone, Debug, Eq, PartialEq)] pub struct Ss58<T: System>(pub T::AccountId); #[derive(Debug, Error)] #[error("Invalid ss58 encoded public key: {0:?}")] pub struct InvalidSs58(PublicError); impl<T: System> FromStr for Ss58<T> where T::AccountId: Ss58Codec, { type Err = InvalidSs58; fn from_str(string: &str) -> Result<Self, Self::Err> { Ok(Self( <T::AccountId as Ss58Codec>::from_string(string).map_err(InvalidSs58)?, )) } } pub async fn resolve<T: Runtime + Identity, P: Pair>( client: &dyn AbstractClient<T, P>, identifier: Option<Identifier<T>>, ) -> Result<T::Uid, Error> { let identifier = if let Some(identifier) = identifier { identifier } else { Identifier::Account(client.signer().await?.account_id().clone()) }; let uid = match identifier { Identifier::Uid(uid) => uid, Identifier::Account(account_id) => client .fetch_uid(&account_id) .await? .ok_or(Error::NoAccount)?, Identifier::Service(service) => client.resolve(&service).await?, }; Ok(uid) } #[derive(Clone, Debug, Eq, PartialEq)] pub enum Identifier<T: Identity> { Uid(T::Uid), Account(T::AccountId), Service(Service), } impl<T: Identity> core::fmt::Display for Identifier<T> { fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { match self { Self::Uid(uid) => write!(f, "{}", uid), Self::Account(account_id) => write!(f, "{}", account_id.to_string()), Self::Service(service) => write!(f, "{}", service), } } } impl<T: Identity> FromStr for Identifier<T> where <T as System>::AccountId: Ss58Codec, { type Err = ServiceParseError; fn from_str(string: &str) -> core::result::Result<Self, Self::Err> { if let Ok(uid) = T::Uid::from_str(string) { Ok(Self::Uid(uid)) } else if let Ok(Ss58(account_id)) = Ss58::<T>::from_str(string) { Ok(Self::Account(account_id)) } else { Ok(Self::Service(Service::from_str(string)?)) } } } #[cfg(test)] mod tests { use core::str::FromStr; use test_client::identity::{Identifier, Service, ServiceParseError}; use test_client::mock::AccountKeyring; use test_client::Runtime; #[test] fn parse_identifer() { assert_eq!( Identifier::from_str("dvc94ch@github"), Ok(Identifier::<Runtime>::Service(Service::Github( "dvc94ch".into() ))) ); assert_eq!( Identifier::<Runtime>::from_str("dvc94ch@twitter"), Err(ServiceParseError::Unknown("twitter".into())) ); assert_eq!( Identifier::<Runtime>::from_str("@dvc94ch"), Err(ServiceParseError::Invalid) ); let alice = AccountKeyring::Alice.to_account_id(); assert_eq!( Identifier::<Runtime>::from_str(&alice.to_string()), Ok(Identifier::Account(alice)) ); } }