referrerpolicy=no-referrer-when-downgrade

sp_runtime/
multiaddress.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! MultiAddress type is a wrapper for multiple downstream account formats.
19
20use alloc::vec::Vec;
21use codec::{Decode, DecodeWithMemTracking, Encode};
22
23/// A multi-format address wrapper for on-chain accounts.
24#[derive(
25	Encode, Decode, DecodeWithMemTracking, PartialEq, Eq, Clone, Debug, scale_info::TypeInfo,
26)]
27#[cfg_attr(feature = "std", derive(Hash))]
28pub enum MultiAddress<AccountId, AccountIndex> {
29	/// It's an account ID (pubkey).
30	Id(AccountId),
31	/// It's an account index.
32	Index(#[codec(compact)] AccountIndex),
33	/// It's some arbitrary raw bytes.
34	Raw(Vec<u8>),
35	/// It's a 32 byte representation.
36	Address32([u8; 32]),
37	/// It's a 20 byte representation.
38	Address20([u8; 20]),
39}
40
41#[cfg(feature = "std")]
42impl<AccountId, AccountIndex> std::fmt::Display for MultiAddress<AccountId, AccountIndex>
43where
44	AccountId: std::fmt::Debug,
45	AccountIndex: std::fmt::Debug,
46{
47	fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
48		use sp_core::hexdisplay::HexDisplay;
49		match self {
50			Self::Raw(inner) => write!(f, "MultiAddress::Raw({})", HexDisplay::from(inner)),
51			Self::Address32(inner) => {
52				write!(f, "MultiAddress::Address32({})", HexDisplay::from(inner))
53			},
54			Self::Address20(inner) => {
55				write!(f, "MultiAddress::Address20({})", HexDisplay::from(inner))
56			},
57			_ => write!(f, "{:?}", self),
58		}
59	}
60}
61
62impl<AccountId, AccountIndex> From<AccountId> for MultiAddress<AccountId, AccountIndex> {
63	fn from(a: AccountId) -> Self {
64		Self::Id(a)
65	}
66}