Skip to content

Commit

Permalink
use raw SHA256 for CIDs for #72
Browse files Browse the repository at this point in the history
  • Loading branch information
makew0rld committed Jan 9, 2025
1 parent 4d14324 commit 81ef313
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 50 deletions.
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,9 @@ require (
github.com/fxamacker/cbor/v2 v2.6.0
github.com/go-chi/chi/v5 v5.0.12
github.com/go-chi/jwtauth/v5 v5.3.1
github.com/ipfs/boxo v0.20.0
github.com/ipfs/go-cid v0.4.1
github.com/ipfs/go-datastore v0.6.0
github.com/jackc/pgx/v5 v5.5.5
github.com/joho/godotenv v1.5.1
github.com/lestrrat-go/jwx/v2 v2.0.21
github.com/multiformats/go-multicodec v0.9.0
github.com/openziti/secretstream v0.1.20
github.com/photon-storage/go-ipfs-car v0.0.0-20240530014616-17d95f03173f
github.com/rjeczalik/notify v0.9.3
Expand Down Expand Up @@ -67,10 +63,13 @@ require (
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
github.com/huin/goupnp v1.3.0 // indirect
github.com/ipfs/bbloom v0.0.4 // indirect
github.com/ipfs/boxo v0.20.0 // indirect
github.com/ipfs/go-bitfield v1.1.0 // indirect
github.com/ipfs/go-block-format v0.2.0 // indirect
github.com/ipfs/go-blockservice v0.5.2 // indirect
github.com/ipfs/go-cid v0.4.1 // indirect
github.com/ipfs/go-cidutil v0.1.0 // indirect
github.com/ipfs/go-datastore v0.6.0 // indirect
github.com/ipfs/go-ds-measure v0.2.0 // indirect
github.com/ipfs/go-fs-lock v0.0.7 // indirect
github.com/ipfs/go-ipfs-blockstore v1.3.1 // indirect
Expand Down Expand Up @@ -144,6 +143,7 @@ require (
github.com/multiformats/go-multiaddr-dns v0.3.1 // indirect
github.com/multiformats/go-multiaddr-fmt v0.1.0 // indirect
github.com/multiformats/go-multibase v0.2.0 // indirect
github.com/multiformats/go-multicodec v0.9.0 // indirect
github.com/multiformats/go-multihash v0.2.3 // indirect
github.com/multiformats/go-multistream v0.5.0 // indirect
github.com/multiformats/go-varint v0.0.7 // indirect
Expand Down
8 changes: 0 additions & 8 deletions upload/web3.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,6 @@ func uploadWeb3(space string, cidPaths []string) error {
}
defer util.RemoveCarTmpDatastore() //nolint:errcheck

// Make sure CID hasn't changed
if car.Root().String() != filepath.Base(cidPath) {
return fmt.Errorf(
"CAR CID doesn't match file CID: %s != %s",
car.Root().String(), filepath.Base(cidPath),
)
}

if err := car.Write(tmpF); err != nil {
return fmt.Errorf("error writing temp CAR file: %w", err)
}
Expand Down
50 changes: 14 additions & 36 deletions util/cid.go
Original file line number Diff line number Diff line change
@@ -1,48 +1,26 @@
package util

import (
"crypto/sha256"
"encoding/base32"
"io"
)

"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"
var (
// https://github.com/multiformats/multibase
// The encoding referred to by "base32" or "b".
// "RFC4648 case-insensitive - no padding"
multibaseBase32 = base32.NewEncoding("abcdefghijklmnopqrstuvwxyz234567").WithPadding(base32.NoPadding)
)

// CalculateFileCid gets the CIDv1 for the given data, the same as IPFS kubo would.
// CalculateFileCid gets the CIDv1 for the given data, using raw SHA-256.
// It does not load the whole file into memory.
func CalculateFileCid(fileReader io.Reader) (string, error) {
ds := dsync.MutexWrap(datastore.NewNullDatastore())
bs := blockstore.NewBlockstore(ds)
bs = blockstore.NewIdStore(bs)
bsrv := blockservice.New(bs, offline.Exchange(bs))
dsrv := merkledag.NewDAGService(bsrv)
// Create a UnixFS graph from our file, parameters described here but can be visualized at https://dag.ipfs.tech/
ufsImportParams := uih.DagBuilderParams{
Maxlinks: uih.DefaultLinksPerBlock, // Default max of 174 links per block
RawLeaves: true, // Leave the actual file bytes untouched instead of wrapping them in a dag-pb protobuf wrapper
CidBuilder: cid.V1Builder{ // Use CIDv1 for all links
Codec: uint64(multicodec.Raw),
MhType: uint64(multicodec.Sha2_256), // Use SHA2-256 as the hash function
MhLength: -1, // Use the default hash length for the given hash function (in this case 256 bits)
},
Dagserv: dsrv,
NoCopy: false,
}
ufsBuilder, err := ufsImportParams.New(chunker.NewSizeSplitter(fileReader, chunker.DefaultBlockSize)) // Split the file up into fixed sized 256KiB chunks
if err != nil {
return cid.Undef.String(), err
}
nd, err := balanced.Layout(ufsBuilder) // Arrange the graph with a balanced layout
hasher := sha256.New()
_, err := io.Copy(hasher, fileReader)
if err != nil {
return cid.Undef.String(), err
return "", err
}
return nd.Cid().String(), nil
// The bytes are (in order) CID version, raw multicodec, sha2-256 multihash, 32 byte length hash
return "b" + multibaseBase32.EncodeToString(append([]byte{0x01, 0x55, 0x12, 0x20}, hasher.Sum(nil)...)), nil
}
4 changes: 2 additions & 2 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ func MoveFile(sourcePath, destPath string) error {

// GetCAR returns a CARv1 file created from the provided reader.
// Currently this uses the default IPFS kubo settings under the hood, and so the
// CIDv1 represented by the CAR file should exactly match the CIDv1 from CalculateFileCid
// or IPFS kubo every time.
// CIDv1 represented by the CAR file should exactly match the CIDv1 from
// IPFS kubo every time. It will not match the CID from CalculateFileCid.
//
// Set useDisk to control whether this function holds the read bytes all in memory
// or stores them on the disk temporarily.
Expand Down

0 comments on commit 81ef313

Please sign in to comment.