1// Copyright (C) Use Ink (UK) Ltd.
2// This file is part of cargo-contract.
3//
4// cargo-contract is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// cargo-contract is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with cargo-contract. If not, see <http://www.gnu.org/licenses/>.
1617use scale::{
18 Decode,
19 Encode,
20};
21use serde::{
22 Deserialize,
23 Serialize,
24};
2526/// A 32-byte cryptographic identifier. This is a simplified version of Substrate's
27/// `sp_core::crypto::AccountId32`.
28///
29/// It replaces the usage of this type from the `sp-core` crate directly. Direct
30/// dependencies on substrate crates can cause linking issues if the dependant
31/// package itself has substrate dependencies which depend on an incompatible version
32/// of `wasmtime`.
33///
34/// This is the only type that was used from `sp-core`, and it is unlikely to change,
35/// so it made sense to make a copy and reduce the dependency burden.
36///
37/// # Note
38///
39/// This has been copied from `subxt::utils::AccountId32`, with some modifications:
40///
41/// - Custom [`scale_info::TypeInfo`] implementation to match original substrate type.
42/// - Made `to_ss58check` public.
43/// - Implemented `TryFrom<&'a [u8]>`.
44///
45/// We can consider modifying the type upstream if appropriate.
46#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode, Debug)]
47pub struct AccountId32(pub [u8; 32]);
4849/// Custom `TypeInfo` impl with path matching original `sp_core::crypto::AccountId32`
50impl scale_info::TypeInfo for AccountId32 {
51type Identity = Self;
5253fn type_info() -> scale_info::Type {
54 scale_info::Type::builder()
55 .path(::scale_info::Path::new("AccountId32", "sp_core::crypto"))
56 .composite(
57 scale_info::build::Fields::unnamed()
58 .field(|f| f.ty::<[u8; 32]>().type_name("[u8; 32]").docs(&[])),
59 )
60 }
61}
6263impl AsRef<[u8]> for AccountId32 {
64fn as_ref(&self) -> &[u8] {
65&self.0[..]
66 }
67}
6869impl AsRef<[u8; 32]> for AccountId32 {
70fn as_ref(&self) -> &[u8; 32] {
71&self.0
72}
73}
7475impl From<[u8; 32]> for AccountId32 {
76fn from(x: [u8; 32]) -> Self {
77 AccountId32(x)
78 }
79}
8081impl<'a> TryFrom<&'a [u8]> for AccountId32 {
82type Error = ();
83fn try_from(x: &'a [u8]) -> Result<AccountId32, ()> {
84if x.len() == 32 {
85let mut data = [0; 32];
86 data.copy_from_slice(x);
87Ok(AccountId32(data))
88 } else {
89Err(())
90 }
91 }
92}
9394impl AccountId32 {
95// Return the ss58-check string for this key. Adapted from `sp_core::crypto`. We need
96 // this to serialize our account appropriately but otherwise don't care.
97pub fn to_ss58check(&self) -> String {
98// For serializing to a string to obtain the account nonce, we use the default
99 // substrate prefix (since we have no way to otherwise pick one). It
100 // doesn't really matter, since when it's deserialized back in
101 // system_accountNextIndex, we ignore this (so long as it's valid).
102const SUBSTRATE_SS58_PREFIX: u8 = 42;
103// prefix <= 63 just take up one byte at the start:
104let mut v = vec![SUBSTRATE_SS58_PREFIX];
105// then push the account ID bytes.
106v.extend(self.0);
107// then push a 2 byte checksum of what we have so far.
108let r = ss58hash(&v);
109 v.extend(&r[0..2]);
110// then encode to base58.
111use base58::ToBase58;
112 v.to_base58()
113 }
114115// This isn't strictly needed, but to give our AccountId32 a little more usefulness,
116 // we also implement the logic needed to decode an AccountId32 from an SS58
117 // encoded string. This is exposed via a `FromStr` impl.
118fn from_ss58check(s: &str) -> Result<Self, FromSs58Error> {
119const CHECKSUM_LEN: usize = 2;
120let body_len = 32;
121122use base58::FromBase58;
123let data = s.from_base58().map_err(|_| FromSs58Error::BadBase58)?;
124if data.len() < 2 {
125return Err(FromSs58Error::BadLength)
126 }
127let prefix_len = match data[0] {
1280..=63 => 1,
12964..=127 => 2,
130_ => return Err(FromSs58Error::InvalidPrefix),
131 };
132if data.len() != prefix_len + body_len + CHECKSUM_LEN {
133return Err(FromSs58Error::BadLength)
134 }
135let hash = ss58hash(&data[0..body_len + prefix_len]);
136let checksum = &hash[0..CHECKSUM_LEN];
137if data[body_len + prefix_len..body_len + prefix_len + CHECKSUM_LEN] != *checksum
138 {
139// Invalid checksum.
140return Err(FromSs58Error::InvalidChecksum)
141 }
142143let result = data[prefix_len..body_len + prefix_len]
144 .try_into()
145 .map_err(|_| FromSs58Error::BadLength)?;
146Ok(AccountId32(result))
147 }
148}
149150/// An error obtained from trying to interpret an SS58 encoded string into an AccountId32
151#[derive(thiserror::Error, Clone, Copy, Eq, PartialEq, Debug)]
152#[allow(missing_docs)]
153pub enum FromSs58Error {
154#[error("Base 58 requirement is violated")]
155BadBase58,
156#[error("Length is bad")]
157BadLength,
158#[error("Invalid checksum")]
159InvalidChecksum,
160#[error("Invalid SS58 prefix byte.")]
161InvalidPrefix,
162}
163164// We do this just to get a checksum to help verify the validity of the address in
165// to_ss58check
166fn ss58hash(data: &[u8]) -> Vec<u8> {
167use blake2::{
168 Blake2b512,
169 Digest,
170 };
171const PREFIX: &[u8] = b"SS58PRE";
172let mut ctx = Blake2b512::new();
173 ctx.update(PREFIX);
174 ctx.update(data);
175 ctx.finalize().to_vec()
176}
177178impl Serialize for AccountId32 {
179fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
180where
181S: serde::Serializer,
182 {
183 serializer.serialize_str(&self.to_ss58check())
184 }
185}
186187impl<'de> Deserialize<'de> for AccountId32 {
188fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
189where
190D: serde::Deserializer<'de>,
191 {
192 AccountId32::from_ss58check(&String::deserialize(deserializer)?)
193 .map_err(|e| serde::de::Error::custom(format!("{e:?}")))
194 }
195}
196197impl std::fmt::Display for AccountId32 {
198fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
199write!(f, "{}", self.to_ss58check())
200 }
201}
202203impl std::str::FromStr for AccountId32 {
204type Err = FromSs58Error;
205fn from_str(s: &str) -> Result<Self, Self::Err> {
206 AccountId32::from_ss58check(s)
207 }
208}
209210#[cfg(test)]
211mod test {
212use super::*;
213214use sp_core::crypto::Ss58Codec;
215use sp_keyring::AccountKeyring;
216217#[test]
218fn ss58_is_compatible_with_substrate_impl() {
219let keyrings = vec![
220 AccountKeyring::Alice,
221 AccountKeyring::Bob,
222 AccountKeyring::Charlie,
223 ];
224225for keyring in keyrings {
226let substrate_account = keyring.to_account_id();
227// Avoid "From" impl hidden behind "substrate-compat" feature so that this
228 // test can work either way:
229let local_account = AccountId32(substrate_account.clone().into());
230231// Both should encode to ss58 the same way:
232let substrate_ss58 = substrate_account.to_ss58check();
233assert_eq!(substrate_ss58, local_account.to_ss58check());
234235// Both should decode from ss58 back to the same:
236assert_eq!(
237 sp_core::crypto::AccountId32::from_ss58check(&substrate_ss58).unwrap(),
238 substrate_account
239 );
240assert_eq!(
241 AccountId32::from_ss58check(&substrate_ss58).unwrap(),
242 local_account
243 );
244 }
245 }
246}