Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
146 commits
Select commit Hold shift + click to select a range
19d857d
wip: skeleton in place
ntn-x2 Jun 9, 2022
98c24ce
wip: trying to fight with rust-analyzer
ntn-x2 Jun 10, 2022
8704a4d
wip: FFS
ntn-x2 Jun 10, 2022
39691f2
Merge commit '52a47659becda33033a269c7d8152dcf5493d0b9' into aa/publi…
ntn-x2 Jun 13, 2022
323296d
wip: tests for the new pallet
ntn-x2 Jun 13, 2022
b586fb2
test: add unit tests for the new pallet
ntn-x2 Jun 14, 2022
d01497b
chore: fmt
ntn-x2 Jun 14, 2022
e2d56a2
fix: restore toolchain to nightly
ntn-x2 Jun 14, 2022
7bc2e70
core: clippy
ntn-x2 Jun 14, 2022
f98c1e7
wip: minor chores
ntn-x2 Jun 14, 2022
21aeb3a
test: add hcecks for deposit reservations
ntn-x2 Jun 15, 2022
bb4d421
test: add InvalidInput test case
ntn-x2 Jun 15, 2022
c16dd8d
feat: working on parsing the right chain_id information
ntn-x2 Jun 15, 2022
6d07577
feat: add dotsama and solana chains
ntn-x2 Jun 16, 2022
1ca4a9e
feat: generic chain support
ntn-x2 Jun 16, 2022
bc3b0c6
feat: add utility functions for chain IDs
ntn-x2 Jun 16, 2022
ba3fb49
chore: clippy + fmt
ntn-x2 Jun 16, 2022
f9878c9
fix: uncomment remaining test functions
ntn-x2 Jun 16, 2022
18c7326
wip: first version of new pallet
ntn-x2 Jun 16, 2022
eed9719
feat: new pallet for chain IDs
ntn-x2 Jun 16, 2022
918fff0
wip: fixing last issues
ntn-x2 Jun 16, 2022
b9ae971
fix: chain ids now working fine
ntn-x2 Jun 17, 2022
a43f2af
feat: asset id complete (untested)
ntn-x2 Jun 17, 2022
df4f1aa
most of unit tests for asset IDs in
ntn-x2 Jun 17, 2022
cd0748b
test: unit tests passing
ntn-x2 Jun 17, 2022
948832a
wip: almost complete
ntn-x2 Jun 17, 2022
031e851
chore: small improvements
ntn-x2 Jun 17, 2022
f38741f
feat: default implementations of important traits
ntn-x2 Jun 20, 2022
dc49855
chore: few needed adjustments
ntn-x2 Jun 20, 2022
feb4759
test: add test for too long credentials
ntn-x2 Jun 20, 2022
144e325
chore: mashnet-node-runtime compiling
ntn-x2 Jun 20, 2022
5a95626
Merge commit '40c48d5f89aacac2971ddede247561fd9073a218' into aa/publi…
ntn-x2 Jun 30, 2022
05b498d
chore: minor refinements
ntn-x2 Jun 30, 2022
5b2426c
chore: remove asset transfer feature
ntn-x2 Jul 1, 2022
ea03307
chore: add test cases for smart contract addresses without leading
ntn-x2 Jul 4, 2022
5835b00
Merge commit '2b576fd6fd568de9b1ef38670aea547251e34173' into aa/publi…
ntn-x2 Jul 5, 2022
d38cab2
fix: dependencies in mashnet-node compilation
ntn-x2 Jul 5, 2022
2cc096e
chore: re-organize imports
ntn-x2 Jul 5, 2022
6f9c328
chore: last cleanups for chain asset DID stuff
ntn-x2 Jul 5, 2022
d5d42b7
fix: adjust asset DID definition in code
ntn-x2 Jul 5, 2022
0623037
wip: whole project compiling
ntn-x2 Jul 5, 2022
22a4690
wip: benchmarking refactoring
ntn-x2 Jul 6, 2022
6c9999a
chore: refactored the default implementation for AssetDid
ntn-x2 Jul 6, 2022
4a048db
wip: refactor almost completed
ntn-x2 Jul 6, 2022
9e521ed
wip: first benchmark case compiling
ntn-x2 Jul 6, 2022
acc061e
wip: refactoring before review
ntn-x2 Jul 8, 2022
5d0c04f
wip: refactoring pt. 2
ntn-x2 Jul 8, 2022
9317196
wip: refactoring pt. 3
ntn-x2 Jul 8, 2022
3946a45
chore: optimise eip155 chain reference
ntn-x2 Jul 11, 2022
f2f477c
wip: optimizing storage
ntn-x2 Jul 11, 2022
39b9630
wip: tests failing
ntn-x2 Jul 11, 2022
888eabb
chore: completed
ntn-x2 Jul 11, 2022
bdd5990
wip: switch to hex_literal
ntn-x2 Jul 11, 2022
c0c6863
chore: refactor complete
ntn-x2 Jul 11, 2022
334a8d6
chore: final chores for the asset DID crate
ntn-x2 Jul 12, 2022
2cca385
bench: setup for public-credentials pallet complete
ntn-x2 Jul 12, 2022
bedf0cd
wip: implementing the Display trait for the AssetDID type
ntn-x2 Jul 12, 2022
cae0846
test: unit tests for ChainID Display implementation working
ntn-x2 Jul 12, 2022
dc4ad62
test: unit tests completed for asset DID crate
ntn-x2 Jul 12, 2022
3b4d125
feat: whole project compiling after implementing Display
ntn-x2 Jul 12, 2022
80dbba5
chore: fmt
ntn-x2 Jul 12, 2022
f8dc617
chore: update benchmarking script for new pallet
ntn-x2 Jul 12, 2022
ba95ca3
chore: update benchmark scripts and weights
ntn-x2 Jul 12, 2022
e68d0f1
chore: update weights
ntn-x2 Jul 12, 2022
31a7de8
Merge commit 'c8f45771eb3f87c2617f925a7ee088a68fcc4990' into aa/publi…
ntn-x2 Jul 12, 2022
5e12525
fix: correct signature verification weight
ntn-x2 Jul 12, 2022
db9eaa5
chore: address TODO
ntn-x2 Jul 12, 2022
616e745
chore: update test script to use --all-targets and check for test tar…
ntn-x2 Jul 12, 2022
a52e26d
test: storage deposit test for runtimes
ntn-x2 Jul 12, 2022
1e4ccd4
chore: address last TODOs
ntn-x2 Jul 12, 2022
705c093
chore: fmt
ntn-x2 Jul 12, 2022
3bcb397
chore: factor everything out into variables
ntn-x2 Jul 13, 2022
9acde1e
fix: clippy issues
ntn-x2 Jul 13, 2022
964613e
fix: checks for asset DID length bounds
ntn-x2 Jul 13, 2022
30fb527
Merge commit 'd8af5d12531d1e2681c5038490958bfb08536a84' into aa/publi…
ntn-x2 Jul 15, 2022
ff7cebd
chore: make failing clearer for decimal references
ntn-x2 Jul 15, 2022
7189465
chore: factor out bounds checking function
ntn-x2 Jul 15, 2022
3413d2b
fix: max line length limit
ntn-x2 Jul 15, 2022
b4140b7
chore: factor out split function
ntn-x2 Jul 15, 2022
606ac15
chore: right syntax for generics
ntn-x2 Jul 15, 2022
9059733
chore: typo
ntn-x2 Jul 15, 2022
8483968
chore: factor out credential retrieval in public credentials pallet
ntn-x2 Jul 15, 2022
61bfdb1
feat: add public credentials runtime API definition
ntn-x2 Jul 18, 2022
ba6dca4
feat: add public credentials runtime API implementation for standalone
ntn-x2 Jul 18, 2022
bdad321
wip: only need to add the RPC to the node executable
ntn-x2 Jul 18, 2022
ded3efd
wip: add serialization support
ntn-x2 Jul 18, 2022
721ed4d
feat: compiling
ntn-x2 Jul 19, 2022
b6dbe2a
fix: compiling with one generic added
ntn-x2 Jul 19, 2022
7e49dbb
fix: compiling with second argument
ntn-x2 Jul 19, 2022
ef844d5
COMPILING
ntn-x2 Jul 19, 2022
85f0b45
chore: qol improvements
ntn-x2 Jul 20, 2022
91fd513
feat: add key to return value of get_credentials
ntn-x2 Jul 20, 2022
ff83e98
chore: remove unneded serde support
ntn-x2 Jul 20, 2022
0001d8c
deps: remove unnecessary serde deps
ntn-x2 Jul 20, 2022
e2b553e
wip: refactoring
ntn-x2 Jul 20, 2022
026b2d0
almost there
ntn-x2 Jul 20, 2022
378d745
feat: add runtime api implementation for parachain runtimes
ntn-x2 Jul 20, 2022
ddd4cdc
chore: remove InputError type from public-credentials pallet
ntn-x2 Jul 21, 2022
34d4332
fix: change TryFrom requirement for SubjectId
ntn-x2 Jul 21, 2022
cc47d5e
chore: remove unused deps
ntn-x2 Jul 21, 2022
80b98b8
chore: update repo links
ntn-x2 Jul 21, 2022
23103ad
chore: move errors in their own module
ntn-x2 Jul 21, 2022
750b3b9
chore: comments
ntn-x2 Jul 21, 2022
5b7d2a8
chore: split components into its own struct
ntn-x2 Jul 21, 2022
a9a3891
chore: add link to const generics
ntn-x2 Jul 21, 2022
17e1fcc
chore: replace switch with if let
ntn-x2 Jul 21, 2022
74526d7
chore: replace matches! with contains
ntn-x2 Jul 21, 2022
0329cf8
chore: make comment a doc comment
ntn-x2 Jul 21, 2022
ae42b32
chore: revert Eq derive for DidDetails
ntn-x2 Jul 21, 2022
a3d1ed8
chore: attestation namespace not needed
ntn-x2 Jul 21, 2022
68c5851
chore: re-add commented out pallets in bench script
ntn-x2 Jul 21, 2022
fc6c7d1
chore: add some logging to the InvalidFormat error cases
ntn-x2 Jul 22, 2022
a6c6954
chore: move serde import up again
ntn-x2 Jul 22, 2022
e43cff7
chore: replace From<Error> for i32 with repr(i32)
ntn-x2 Jul 22, 2022
e8aaa0a
chore: improve efficienct of Display
ntn-x2 Jul 22, 2022
c580520
fix: changes to public credentials based on feedback (#392)
ntn-x2 Aug 8, 2022
79bade8
merge origin/develop into current branch
ntn-x2 Aug 8, 2022
127f8c1
Fix clone runtime for public credentials RPC
ntn-x2 Aug 8, 2022
a734eb9
Remove unused deps
ntn-x2 Aug 8, 2022
58b218c
Remove most of the unrelated changes
ntn-x2 Aug 8, 2022
89c8ce0
Last chores
ntn-x2 Aug 8, 2022
9265b3a
Remove non_exhaustive from all structs
ntn-x2 Aug 9, 2022
e34a900
Add comments to Cargo.toml
ntn-x2 Aug 10, 2022
7fa209a
Switch ordering in Cargo.toml
ntn-x2 Aug 10, 2022
5b31ced
Switch ordering in Cargo.toml
ntn-x2 Aug 10, 2022
8931f71
Cargo.toml fixes
ntn-x2 Aug 10, 2022
57baf68
Refactor benchmarking helper function
ntn-x2 Aug 10, 2022
295b816
Update public credentials pallet description
ntn-x2 Aug 10, 2022
36ca6a2
Update comments in pallet
ntn-x2 Aug 10, 2022
88a3aaf
Update description of the reclaim_deposit extrinsic
ntn-x2 Aug 10, 2022
98a4531
More comments
ntn-x2 Aug 10, 2022
5144705
Add case for invalid input
ntn-x2 Aug 10, 2022
d054cb1
remove unused variable in benchmarks
ntn-x2 Aug 10, 2022
4acf61c
Merge branch origin/develop into aa/public-credentials-v2
ntn-x2 Aug 10, 2022
7fe38a0
Make comment a doc comment
ntn-x2 Aug 10, 2022
175ed43
fmt
ntn-x2 Aug 12, 2022
6e54dd9
Fix match case
ntn-x2 Sep 19, 2022
29f5a4c
Fix import
ntn-x2 Sep 19, 2022
9ebcd05
Fix single match case
ntn-x2 Sep 19, 2022
2508c30
Fix new line
ntn-x2 Sep 19, 2022
88b034c
Fix access control file
ntn-x2 Sep 19, 2022
bc96327
Fix more matches
ntn-x2 Sep 19, 2022
9e43b3e
Fix proxy type
ntn-x2 Sep 19, 2022
4a310fb
Fmt
ntn-x2 Sep 19, 2022
69fa1c0
Merge commit 'a7b2c43a7c02b4d3a51f895655c37e2a78412abd' into aa/publi…
ntn-x2 Sep 19, 2022
3b1ab07
Fix repo links
ntn-x2 Sep 20, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: new pallet for chain IDs
  • Loading branch information
ntn-x2 committed Jun 16, 2022
commit eed971900bed94ac0a385cc37fb30037bb17bdda
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions utilities/assets/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
authors = ["KILT <[email protected]>"]
description = "Asset DIDs and related structs, suitable for no_std environments."
edition = "2021"
name = "kilt-caip-assets"
repository = "https://github.com/KILTprotocol/mashnet-node"
version = "1.6.2"

[dependencies]
frame-support = {branch = "polkadot-v0.9.23", default-features = false, git = "https://github.com/paritytech/substrate"}
base58 = {version = "0.2.0", default-features = false}

[features]
default = ["std"]

std = [
"frame-support/std",
]
340 changes: 340 additions & 0 deletions utilities/assets/src/chain_id.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,340 @@
// KILT Blockchain – https://botlabs.org
// Copyright (C) 2019-2022 BOTLabs GmbH

// The KILT Blockchain 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.

// The KILT Blockchain 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 this program. If not, see <https://www.gnu.org/licenses/>.

// If you feel like getting in touch with us, you can do so at [email protected]

use base58::FromBase58;
use core::str;

use frame_support::{sp_runtime::traits::CheckedConversion, traits::ConstU32, BoundedVec};

const MINIMUM_NAMESPACE_LENGTH: u32 = 3;
const MAXIMUM_NAMESPACE_LENGTH: u32 = 8;
const MINIMUM_REFERENCE_LENGTH: u32 = 1;
const MAXIMUM_REFERENCE_LENGTH: u32 = 32;

pub enum ChainId {
Eip155(Eip155Reference),
Bip122(GenesisHexHashReference),
Dotsama(GenesisHexHashReference),
Solana(GenesisBase58HashReference),
Generic(GenericChainId),
}

pub enum ChainIdError {
Namespace(NamespaceError),
Reference(ReferenceError),
InvalidFormat,
}

pub enum NamespaceError {
TooLong,
TooShort,
InvalidCharacter,
}

pub enum ReferenceError {
TooLong,
TooShort,
InvalidCharacter,
}

impl TryFrom<&[u8]> for ChainId {
type Error = ChainIdError;

fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
match value {
// "eip155:" chains -> https://github.com/ChainAgnostic/CAIPs/blob/master/CAIPs/caip-3.md
[b'e', b'i', b'p', b'1', b'5', b'5', b':', chain_id @ ..] => {
Eip155Reference::try_from(chain_id).map(Self::Eip155)
}
// "bip122:" chains -> https://github.com/ChainAgnostic/CAIPs/blob/master/CAIPs/caip-4.md
[b'b', b'i', b'p', b'1', b'2', b'2', b':', chain_id @ ..] => {
GenesisHexHashReference::try_from(chain_id).map(Self::Bip122)
}
// "polkadot:" chains -> https://github.com/ChainAgnostic/CAIPs/blob/master/CAIPs/caip-13.md
[b'p', b'o', b'l', b'k', b'a', b'd', b'o', b't', b':', chain_id @ ..] => {
GenesisHexHashReference::try_from(chain_id).map(Self::Dotsama)
}
// "solana:" chains -> https://github.com/ChainAgnostic/CAIPs/blob/master/CAIPs/caip-30.md
[b's', b'o', b'l', b'a', b'n', b'a', b':', chain_id @ ..] => {
GenesisBase58HashReference::try_from(chain_id).map(Self::Solana)
}
// Other chains that are still compatible with the CAIP-2 spec -> https://github.com/ChainAgnostic/CAIPs/blob/master/CAIPs/caip-2.md
chain_id => GenericChainId::try_from(chain_id).map(Self::Generic),
}
}
}

impl ChainId {
pub fn ethereum_mainnet() -> Self {
Self::Eip155(Eip155Reference::from_slice_unchecked(b"1"))
}

pub fn moonriver_eth() -> Self {
// Info taken from https://chainlist.org/
Self::Eip155(Eip155Reference::from_slice_unchecked(b"1285"))
}

pub fn moonbeam_eth() -> Self {
// Info taken from https://chainlist.org/
Self::Eip155(Eip155Reference::from_slice_unchecked(b"1284"))
}

pub fn bitcoin_mainnet() -> Self {
Self::Bip122(GenesisHexHashReference::from_slice_unchecked(
b"000000000019d6689c085ae165831e93",
))
}

pub fn polkadot() -> Self {
Self::Dotsama(GenesisHexHashReference::from_slice_unchecked(
b"91b171bb158e2d3848fa23a9f1c25182",
))
}

pub fn kusama() -> Self {
Self::Dotsama(GenesisHexHashReference::from_slice_unchecked(
b"b0a8d493285c2df73290dfb7e61f870f",
))
}

pub fn kilt_spiritnet() -> Self {
Self::Dotsama(GenesisHexHashReference::from_slice_unchecked(
b"411f057b9107718c9624d6aa4a3f23c1",
))
}

pub fn solana_mainnet() -> Self {
Self::Solana(GenesisBase58HashReference::from_slice_unchecked(
b"4sGjMW1sUnHzSxGspuhpqLDx6wiyjNtZ",
))
}
}

pub struct Eip155Reference(BoundedVec<u8, ConstU32<MAXIMUM_REFERENCE_LENGTH>>);

impl Eip155Reference {
#[allow(dead_code)]
pub(crate) fn from_slice_unchecked(slice: &[u8]) -> Self {
Self(slice.to_vec().try_into().unwrap())
}
}

impl TryFrom<&[u8]> for Eip155Reference {
type Error = ChainIdError;

fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
let input_length = value
.len()
.checked_into::<u32>()
.ok_or(ChainIdError::Reference(ReferenceError::TooLong))?;
if input_length < MINIMUM_REFERENCE_LENGTH {
Err(ChainIdError::Reference(ReferenceError::TooShort))
} else if input_length > MAXIMUM_REFERENCE_LENGTH {
Err(ChainIdError::Reference(ReferenceError::TooLong))
} else {
value.iter().try_for_each(|c| {
if !(b'0'..=b'9').contains(c) {
Err(ChainIdError::Reference(ReferenceError::InvalidCharacter))
} else {
Ok(())
}
})?;
// Unchecked since we already checked for length
Ok(Self::from_slice_unchecked(value))
}
}
}

pub struct GenesisHexHashReference(BoundedVec<u8, ConstU32<MAXIMUM_REFERENCE_LENGTH>>);

impl GenesisHexHashReference {
#[allow(dead_code)]
pub(crate) fn from_slice_unchecked(slice: &[u8]) -> Self {
Self(slice.to_vec().try_into().unwrap())
}
}

impl TryFrom<&[u8]> for GenesisHexHashReference {
type Error = ChainIdError;

fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
let input_length = value
.len()
.checked_into::<u32>()
.ok_or(ChainIdError::Reference(ReferenceError::TooLong))?;
if input_length < MINIMUM_REFERENCE_LENGTH {
Err(ChainIdError::Reference(ReferenceError::TooShort))
} else if input_length > MAXIMUM_REFERENCE_LENGTH {
Err(ChainIdError::Reference(ReferenceError::TooLong))
} else if input_length % 2 != 0 {
// Hex encoding can only have 2x characters
Err(ChainIdError::InvalidFormat)
} else {
value.iter().try_for_each(|c| {
if !matches!(c, b'0'..=b'9' | b'a'..=b'f') {
Err(ChainIdError::Reference(ReferenceError::InvalidCharacter))
} else {
Ok(())
}
})?;
// Unchecked since we already checked for length
Ok(Self::from_slice_unchecked(value))
}
}
}

pub struct GenesisBase58HashReference(BoundedVec<u8, ConstU32<MAXIMUM_REFERENCE_LENGTH>>);

impl GenesisBase58HashReference {
#[allow(dead_code)]
pub(crate) fn from_slice_unchecked(slice: &[u8]) -> Self {
Self(slice.to_vec().try_into().unwrap())
}
}

impl TryFrom<&[u8]> for GenesisBase58HashReference {
type Error = ChainIdError;

fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
let input_length = value
.len()
.checked_into::<u32>()
.ok_or(ChainIdError::Reference(ReferenceError::TooLong))?;
if input_length < MINIMUM_REFERENCE_LENGTH {
Err(ChainIdError::Reference(ReferenceError::TooShort))
} else if input_length > MAXIMUM_REFERENCE_LENGTH {
Err(ChainIdError::Reference(ReferenceError::TooLong))
} else {
let decoded_string =
str::from_utf8(value).map_err(|_| ChainIdError::Reference(ReferenceError::InvalidCharacter))?;
// Check for proper base58 encoding
decoded_string
.from_base58()
.map_err(|_| ChainIdError::Reference(ReferenceError::InvalidCharacter))?;
// Unchecked since we already checked for length
Ok(Self::from_slice_unchecked(value))
}
}
}

pub struct GenericChainId {
pub namespace: ChainNamespace,
pub reference: ChainReference,
}

impl GenericChainId {
#[allow(dead_code)]
fn from_raw_unchecked(namespace: &[u8], reference: &[u8]) -> Self {
Self {
namespace: ChainNamespace::from_slice_unchecked(namespace),
reference: ChainReference::from_slice_unchecked(reference),
}
}
}

impl TryFrom<&[u8]> for GenericChainId {
type Error = ChainIdError;

fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
let input_length = value.len().checked_into::<u32>().ok_or(ChainIdError::InvalidFormat)?;
if input_length > MINIMUM_NAMESPACE_LENGTH + MAXIMUM_NAMESPACE_LENGTH + 1 {
return Err(ChainIdError::InvalidFormat);
}

let mut components = value.split(|c| *c == b':');

let namespace = components
.next()
.ok_or(ChainIdError::InvalidFormat)
.and_then(ChainNamespace::try_from)?;
let reference = components
.next()
.ok_or(ChainIdError::InvalidFormat)
.and_then(ChainReference::try_from)?;

Ok(Self { namespace, reference })
}
}

pub struct ChainNamespace(BoundedVec<u8, ConstU32<MAXIMUM_NAMESPACE_LENGTH>>);

impl ChainNamespace {
fn from_slice_unchecked(value: &[u8]) -> Self {
Self(value.to_vec().try_into().unwrap())
}
}

impl TryFrom<&[u8]> for ChainNamespace {
type Error = ChainIdError;

fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
let input_length = value
.len()
.checked_into::<u32>()
.ok_or(ChainIdError::Namespace(NamespaceError::TooLong))?;
if input_length < MINIMUM_NAMESPACE_LENGTH {
Err(ChainIdError::Namespace(NamespaceError::TooShort))
} else if input_length > MAXIMUM_NAMESPACE_LENGTH {
Err(ChainIdError::Namespace(NamespaceError::TooLong))
} else {
value.iter().try_for_each(|c| {
if !matches!(c, b'-' | b'a'..=b'z' | b'0'..=b'9') {
Err(ChainIdError::Namespace(NamespaceError::InvalidCharacter))
} else {
Ok(())
}
})?;
// Unchecked since we already checked for length
Ok(Self::from_slice_unchecked(value))
}
}
}

pub struct ChainReference(BoundedVec<u8, ConstU32<MAXIMUM_REFERENCE_LENGTH>>);

impl ChainReference {
fn from_slice_unchecked(value: &[u8]) -> Self {
Self(value.to_vec().try_into().unwrap())
}
}

impl TryFrom<&[u8]> for ChainReference {
type Error = ChainIdError;

fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
let input_length = value
.len()
.checked_into::<u32>()
.ok_or(ChainIdError::Reference(ReferenceError::TooLong))?;
if input_length < MINIMUM_REFERENCE_LENGTH {
Err(ChainIdError::Reference(ReferenceError::TooShort))
} else if input_length > MAXIMUM_REFERENCE_LENGTH {
Err(ChainIdError::Reference(ReferenceError::TooLong))
} else {
value.iter().try_for_each(|c| {
if !matches!(c, b'-' | b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9') {
Err(ChainIdError::Reference(ReferenceError::InvalidCharacter))
} else {
Ok(())
}
})?;
// Unchecked since we already checked for length
Ok(Self::from_slice_unchecked(value))
}
}
}
21 changes: 21 additions & 0 deletions utilities/assets/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// KILT Blockchain – https://botlabs.org
// Copyright (C) 2019-2022 BOTLabs GmbH

// The KILT Blockchain 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.

// The KILT Blockchain 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 this program. If not, see <https://www.gnu.org/licenses/>.

// If you feel like getting in touch with us, you can do so at [email protected]

#![cfg_attr(not(feature = "std"), no_std)]

pub mod chain_id;