From bdd2dc04c89375e197c8b79a92b15a00b73fb5a2 Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Sat, 3 Aug 2024 20:25:45 -0400 Subject: [PATCH] update instructions for new hash function --- steps/27/README.md | 12 +++++++----- steps/27/src/impls.rs | 7 ++++--- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/steps/27/README.md b/steps/27/README.md index 03239e02..cf64a574 100644 --- a/steps/27/README.md +++ b/steps/27/README.md @@ -29,17 +29,19 @@ If we combine all of these things together, we can ensure that every kitty we mi ## Hash -Obviously our uniqueness inputs are not super useful as is. But we can convert these inputs into a unique set of bytes with fixed length using a Hash function. +Obviously our uniqueness inputs are not super useful as is. But we can convert these inputs into a unique set of bytes with fixed length using a Hash function like [`frame::primitives::BlakeTwo256`](https://docs.rs/polkadot-sdk-frame/0.6.0/polkadot_sdk_frame/primitives/struct.BlakeTwo256.html). ```rust // Collect our unique inputs into a single object. let unique_payload = (item1, item2, item3); -// Encode that object into a vector of bytes. -let encoded_payload: Vec = unique_payload.encode(); -// Hash those bytes to create a fixed-size hash. -let hash: [u8; 32] = frame_support::Hashable::blake2_256(&encoded_payload) +// To use the `hash_of` API, we need to bring the `Hash` trait into scope. +use frame::traits::Hash; +// Hash that object to get a unique identifier. +let hash: [u8; 32] = BlakeTwo256::hash_of(&unique_payload).into(). ``` +The `hash_of` API comes from the `Hash` trait and takes any `encode`-able object, and returns a `H256`, which is a 256-bit hash. As you can see in the code above, it is easy to convert that to a `[u8; 32]` by just calling `.into()`, since these two types are equivalent. + Another nice thing about using a hash is you get some sense of pseudo-randomness between the input and output. This means that two kitties which are minted right after one another could have totally different DNA, which could be useful if you want to associate unique attributes to the different parts of their DNA. 🤔 diff --git a/steps/27/src/impls.rs b/steps/27/src/impls.rs index 0dd01597..0913dbd8 100644 --- a/steps/27/src/impls.rs +++ b/steps/27/src/impls.rs @@ -1,5 +1,7 @@ use super::*; use frame::prelude::*; +/* 🚧 TODO 🚧: Import `frame::primtives::BlakeTwo256`. */ +/* 🚧 TODO 🚧: Import `frame::traits::Hash`. */ impl Pallet { /* 🚧 TODO 🚧: Create a function `gen_dna` which returns a `[u8; 32]`. @@ -8,9 +10,8 @@ impl Pallet { - `block_number` - `extrinsic_index` - `CountForKitties::::get()` - - `encode()` that payload to a byte array named `encoded_payload`. - - Use `frame_support::Hashable` to perform a `blake2_256` hash on the encoded payload. - - Return the 32 byte hash. + - Use `BlakeTwo256` to calculate the `hash_of` the unique payload. + - Return the hash as a `[u8; 32]`. */ pub fn mint(owner: T::AccountId, dna: [u8; 32]) -> DispatchResult {