sp_idn_traits/
lib.rs

1/*
2 * Copyright 2025 by Ideal Labs, LLC
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//! # IDN Traits
18//!
19//! Core traits for the Ideal Network (IDN) ecosystem.
20//!
21//! This crate provides fundamental interfaces for handling randomness pulses.
22//!
23//! ## Modules
24//!
25//! * [`pulse`] - Traits and types for randomness pulses handling and distribution
26//!
27//! ## Overview
28//!
29//! The IDN traits define the foundational interfaces that allow different
30//! components of the system to interact in a standardized way. These traits
31//! enable a modular architecture where randomness sources, dispatchers, and
32//! consumers can all operate together seamlessly.
33
34#![cfg_attr(not(feature = "std"), no_std)]
35
36pub mod pulse;
37
38use frame_support::pallet_prelude::Encode;
39use sp_core::H256;
40use sp_io::hashing::blake2_256;
41
42/// Trait for hashing with a salt.
43pub trait Hashable {
44	fn hash(&self, salt: &[u8]) -> H256;
45}
46impl<T> Hashable for T
47where
48	T: Encode,
49{
50	fn hash(&self, salt: &[u8]) -> H256 {
51		let id_tuple = (self, salt);
52		// Encode the tuple using SCALE codec.
53		let encoded = id_tuple.encode();
54		// Hash the encoded bytes using blake2_256.
55		H256::from_slice(&blake2_256(&encoded))
56	}
57}