mirror of
https://github.com/by-jp/www.byjp.me.git
synced 2025-08-09 22:16:07 +01:00
This standardises all my markdown files to have no leading newline after the `---` that ends the frontmatter. This makes display on Gemini (in which newlines matter) much more consistent! I can't spot any errors with the regex I used here, but there may be one or two articles that change shape — hopefully I can spot them and fix them!
2.6 KiB
2.6 KiB
title | summary | tags | references | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
IPFS | The InterPlanetary FileSystem, and its adjacent distributed technologies. |
|
|
Generating IPFS hashes
For some reason I often find myself wanting to generate the CID that IPFS would generate in Go, but without having to run an entire node. I found this repo from which I've extracted and tweaked the following code to generate Blake3 CIDs from arbitrary bytes.
package hash
import (
"bytes"
"fmt"
"github.com/ipfs/boxo/blockservice"
blockstore "github.com/ipfs/boxo/blockstore"
chunker "github.com/ipfs/boxo/chunker"
offline "github.com/ipfs/boxo/exchange/offline"
"github.com/ipfs/boxo/ipld/merkledag"
"github.com/ipfs/boxo/ipld/unixfs/importer/balanced"
uih "github.com/ipfs/boxo/ipld/unixfs/importer/helpers"
"github.com/ipfs/go-cid"
"github.com/ipfs/go-datastore"
dsync "github.com/ipfs/go-datastore/sync"
multicodec "github.com/multiformats/go-multicodec"
)
func CID(data []byte) (string, error) {
ds := dsync.MutexWrap(datastore.NewNullDatastore())
bs := blockstore.NewIdStore(blockstore.NewBlockstore(ds))
bsrv := blockservice.New(bs, offline.Exchange(bs))
dsrv := merkledag.NewDAGService(bsrv)
ufsImportParams := uih.DagBuilderParams{
Maxlinks: uih.DefaultLinksPerBlock,
RawLeaves: true,
CidBuilder: cid.V1Builder{
Codec: uint64(multicodec.Raw),
MhType: uint64(multicodec.Blake3),
MhLength: -1,
},
Dagserv: dsrv,
NoCopy: false,
}
r := bytes.NewReader(data)
ufsBuilder, err := ufsImportParams.New(chunker.NewSizeSplitter(r, chunker.DefaultBlockSize))
if err != nil {
return "", fmt.Errorf("unable to chunk the event to create a Blake3 hash: %w", err)
}
nd, err := balanced.Layout(ufsBuilder)
if err != nil {
return "", fmt.Errorf("unable to layout the DAG while hashing the event: %w", err)
}
return nd.Cid().String(), nil
}
This can be done much more simply if you know that your data is never going to exceed 256KiB (which is the default chunker size) with the following. This works cos, without chunking, the data that'd be stored is identical to what you're providing!
import (
"github.com/ipfs/go-cid"
"github.com/multiformats/go-multihash"
)
func CID(data []byte) (string, error) {
hash, err := multihash.Sum(data, multihash.BLAKE3, -1)
if err != nil {
return "", err
}
return cid.NewCidV1(cid.Raw, hash).String(), nil
}