-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Andrew Plaza
committed
Sep 1, 2021
1 parent
6c11e05
commit 03be6ea
Showing
13 changed files
with
149 additions
and
146 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// Copyright 2019-2021 Parity Technologies (UK) Ltd. | ||
// This file is part of substrate-desub. | ||
// | ||
// substrate-desub is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// substrate-desub is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with substrate-desub. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
//! Copy of this: https://substrate.dev/rustdocs/latest/pallet_identity/enum.Data.html | ||
//! For the purpose of Decoding the Data field. | ||
//! Because of the specificity/complexity of decoding: | ||
//! - `BoundedVec` includes private tuple fields with no getters so Serde `remote` does not work | ||
//! - `Data` has a special way of encoding/decoding | ||
//! Data impl is copied over and must be maintaned against substrate master. | ||
|
||
use codec::{Decode, Encode}; | ||
use serde::{Serialize, Deserialize}; | ||
use std::iter::once; | ||
|
||
|
||
/// Either underlying data blob if it is at most 32 bytes, or a hash of it. If the data is greater | ||
/// than 32-bytes then it will be truncated when encoding. | ||
/// | ||
/// Can also be `None`. | ||
#[derive(Clone, Eq, PartialEq, Debug, Serialize, Deserialize)] | ||
pub enum Data { | ||
/// No data here. | ||
None, | ||
/// The data is stored directly. | ||
Raw(Vec<u8>), | ||
/// Only the Blake2 hash of the data is stored. The preimage of the hash may be retrieved | ||
/// through some hash-lookup service. | ||
BlakeTwo256([u8; 32]), | ||
/// Only the SHA2-256 hash of the data is stored. The preimage of the hash may be retrieved | ||
/// through some hash-lookup service. | ||
Sha256([u8; 32]), | ||
/// Only the Keccak-256 hash of the data is stored. The preimage of the hash may be retrieved | ||
/// through some hash-lookup service. | ||
Keccak256([u8; 32]), | ||
/// Only the SHA3-256 hash of the data is stored. The preimage of the hash may be retrieved | ||
/// through some hash-lookup service. | ||
ShaThree256([u8; 32]), | ||
} | ||
|
||
impl Decode for Data { | ||
fn decode<I: codec::Input>(input: &mut I) -> Result<Self, codec::Error> { | ||
let b = input.read_byte()?; | ||
Ok(match b { | ||
0 => Data::None, | ||
n @ 1 ..= 33 => { | ||
let mut r = vec![0u8; n as usize - 1]; | ||
input.read(&mut r[..])?; | ||
Data::Raw(r) | ||
} | ||
34 => Data::BlakeTwo256(<[u8; 32]>::decode(input)?), | ||
35 => Data::Sha256(<[u8; 32]>::decode(input)?), | ||
36 => Data::Keccak256(<[u8; 32]>::decode(input)?), | ||
37 => Data::ShaThree256(<[u8; 32]>::decode(input)?), | ||
_ => return Err(codec::Error::from("invalid leading byte")), | ||
}) | ||
} | ||
} | ||
|
||
impl Encode for Data { | ||
fn encode(&self) -> Vec<u8> { | ||
match self { | ||
Data::None => vec![0u8; 1], | ||
Data::Raw(ref x) => { | ||
let l = x.len().min(32); | ||
let mut r = vec![l as u8 + 1; l + 1]; | ||
r[1..].copy_from_slice(&x[..l as usize]); | ||
r | ||
} | ||
Data::BlakeTwo256(ref h) => once(34u8).chain(h.iter().cloned()).collect(), | ||
Data::Sha256(ref h) => once(35u8).chain(h.iter().cloned()).collect(), | ||
Data::Keccak256(ref h) => once(36u8).chain(h.iter().cloned()).collect(), | ||
Data::ShaThree256(ref h) => once(37u8).chain(h.iter().cloned()).collect(), | ||
} | ||
} | ||
} | ||
impl codec::EncodeLike for Data {} | ||
|
||
impl Default for Data { | ||
fn default() -> Self { | ||
Self::None | ||
} | ||
} | ||
|
Oops, something went wrong.