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
use codec::{Decode, Encode};
use serde::{Deserialize, Serialize};
use sp_core::crypto::{AccountId32 as Sp_AccountId32, Ss58Codec};
use subxt::utils::Static;
#[derive(
Hash,
Clone,
Eq,
PartialEq,
Ord,
PartialOrd,
Encode,
Decode,
Debug,
scale_encode::EncodeAsType,
scale_decode::DecodeAsType,
)]
pub struct AccountId32(pub Static<Sp_AccountId32>);
impl AccountId32 {
pub fn new(value: [u8; 32]) -> Self {
AccountId32(Static(value.into()))
}
pub fn to_sp_core_account_id(&self) -> Sp_AccountId32 {
let account_id = self.0.clone();
(*account_id).clone().into()
}
}
impl std::fmt::Display for AccountId32 {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let wp_accountid = self.clone();
let sp_accountid = wp_accountid.0;
let ss58_checks = sp_accountid.to_ss58check();
write!(f, "{}", ss58_checks)
}
}
impl std::str::FromStr for AccountId32 {
type Err = sp_core::crypto::PublicError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let account_id = Sp_AccountId32::from_ss58check(s)?;
Ok(AccountId32(Static(account_id)))
}
}
impl Serialize for AccountId32 {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(self.clone().0.to_ss58check().as_str())
}
}
impl<'de> Deserialize<'de> for AccountId32 {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let account_id = Sp_AccountId32::from_ss58check(&String::deserialize(deserializer)?)
.map_err(|e| serde::de::Error::custom(format!("{e:?}")))?;
Ok(AccountId32(Static(account_id)))
}
}
impl From<sp_runtime::AccountId32> for AccountId32 {
fn from(value: sp_runtime::AccountId32) -> Self {
AccountId32(Static(value.into()))
}
}
impl From<sp_core::sr25519::Public> for AccountId32 {
fn from(value: sp_core::sr25519::Public) -> Self {
let account_id: Sp_AccountId32 = value.into();
account_id.into()
}
}
impl From<sp_core::ed25519::Public> for AccountId32 {
fn from(value: sp_core::ed25519::Public) -> Self {
let account_id: Sp_AccountId32 = value.into();
account_id.into()
}
}
impl From<sp_keyring::Sr25519Keyring> for AccountId32 {
fn from(account: sp_keyring::Sr25519Keyring) -> Self {
let account = account.to_account_id();
account.into()
}
}
impl Default for AccountId32 {
fn default() -> Self {
AccountId32(Static([0; 32].into()))
}
}
impl From<[u8; 32]> for AccountId32 {
fn from(x: [u8; 32]) -> Self {
AccountId32(Static(x.into()))
}
}