Function hdk::hash::hash_entry
source · [−]pub fn hash_entry<I, E>(input: I) -> Result<HoloHash<Entry>, WasmError>where
Entry: TryFrom<I>,
WasmError: From<E>,
<Entry as TryFrom<I>>::Error == E,
Expand description
Hash anything that implements TryInto<Entry>
.
Hashes are typed in Holochain, e.g. ActionHash
and EntryHash
are different and yield different
bytes for a given value. This ensures correctness and allows type based dispatch in various
areas of the codebase.
Usually you want to hash a value that you want to reference on the DHT with must_get_entry
etc. because
it represents some domain-specific data sourced externally or generated within the wasm.
ActionHash
hashes are always generated by the process of committing something to a local
chain. Every host function that commits an entry returns the new ActionHash
. The ActionHash
can
also be used with must_get_action
etc. to retreive a specific record from the DHT rather than the
oldest live record.
However there is no way to generate an action hash directly from an action from inside wasm.
Record
values (entry+action pairs returned by must_get_action
etc.) contain prehashed action structs
called ActionHashed
, which is composed of a ActionHash
alongside the “raw” Action
value. Generally the pre-hashing is
more efficient than hashing actions ad-hoc as hashing always needs to be done at the database
layer, so we want to re-use that as much as possible.
The action hash can be extracted from the Record as record.action_hashed().as_hash()
.
@todo is there any use-case that can’t be satisfied by the action_hashed
approach?
Anything that is annotated with #hdk_entry_helper implements this so is compatible automatically.
hash_entry
is “dumb” in that it doesn’t check that the entry is defined, committed, on the DHT or
any other validation, it simply generates the hash for the serialized representation of
something in the same way that the DHT would.
It is strongly recommended that you use the hash_entry
function to calculate hashes to avoid
inconsistencies between hashes in the wasm guest and the host.
For example, a lot of the crypto crates in rust compile to wasm so in theory could generate the
hash in the guest, but there is the potential that the serialization logic could be slightly
different, etc.
#[hdk_entry_helper]
struct Foo;
let foo_hash = hash_entry(Foo)?;