Skip to content

Commit

Permalink
update instructions for new hash function
Browse files Browse the repository at this point in the history
  • Loading branch information
shawntabrizi committed Aug 4, 2024
1 parent f09b535 commit bdd2dc0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
12 changes: 7 additions & 5 deletions steps/27/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8> = 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. 🤔


Expand Down
7 changes: 4 additions & 3 deletions steps/27/src/impls.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use super::*;
use frame::prelude::*;
/* 🚧 TODO 🚧: Import `frame::primtives::BlakeTwo256`. */
/* 🚧 TODO 🚧: Import `frame::traits::Hash`. */

impl<T: Config> Pallet<T> {
/* 🚧 TODO 🚧: Create a function `gen_dna` which returns a `[u8; 32]`.
Expand All @@ -8,9 +10,8 @@ impl<T: Config> Pallet<T> {
- `block_number`
- `extrinsic_index`
- `CountForKitties::<T>::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 {
Expand Down

0 comments on commit bdd2dc0

Please sign in to comment.