Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add custom hasher example to readme #645

Merged
merged 2 commits into from
Oct 7, 2024
Merged
Changes from 1 commit
Commits
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
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,41 @@ console.log(await d.get(retrievedObject.link))
// { hello: 'world' }
```

## 🔒 Custom Hasher

A [hasher](https:/multiformats/js-multiformats?tab=readme-ov-file#multihash-hashers) is used to determine the immutable address of the content (aka the [**CID**](https:/multiformats/cid?tab=readme-ov-file#what-is-it)) being imported into helia. The default hasher used by the methods above is [sha2-256 multihash](https:/multiformats/js-multiformats?tab=readme-ov-file#multihash-hashers-1), but others can be provided with [AddOptions](https://helia.io/interfaces/_helia_dag_cbor.AddOptions.html). This is useful for applications that require hashers with specific properties; so in most cases keeping the default is recommended.

> **Changing the hasher will cause a different CID to be returned for the same content! In other words: the same content imported with different hashers is treated like unique content with a unique address.**

```js
import { createHelia } from 'helia'
import { dagCbor } from '@helia/dag-cbor'
import { sha512 as hasher } from 'multiformats/hashes/sha2'

const helia = await createHelia()
const d = dagCbor(helia)

const object1 = { hello: 'world' }

const myImmutableAddress1 = await d.add(object1)
const myImmutableAddress2 = await d.add(object1, { hasher })

/** The same objects with different CIDs are treated as different objects */

console.log(myImmutableAddress1)
// CID(bafyreidykglsfhoixmivffc5uwhcgshx4j465xwqntbmu43nb2dzqwfvae)
console.log(myImmutableAddress2)
// CID(bafyrgqhai26anf3i7pips7q22coa4sz2fr4gk4q4sqdtymvvjyginfzaqewveaeqdh524nsktaq43j65v22xxrybrtertmcfxufdam3da3hbk)

const retrievedObject1 = await d.get(myImmutableAddress1)
const retrievedObject2 = await d.get(myImmutableAddress2)
achingbrain marked this conversation as resolved.
Show resolved Hide resolved

console.log(retrievedObject1)
// { hello: 'world' }
console.log(retrievedObject2)
// { hello: 'world' }
```

# 🐾 Next steps

Check out the [helia-examples](https:/ipfs-examples/helia-examples)
Expand Down
Loading