Skip to content

piece

PieceCID utilities for Filecoin Onchain Cloud.

import * as Piece from '@filoz/synapse-core/piece'

// Compute
const piece = await Piece.calculate(bytes)
piece.root    // 32-byte merkle root (for contract calls)
piece.size    // raw bytes
piece.height  // tree height

// Parse / validate
const piece = Piece.from('bafkz...')     // throws on invalid
const piece = Piece.tryFrom(maybeInput)  // null on invalid
Piece.is(value)                          // type guard

// Streaming
const { transform, result } = Piece.transformStream()
await source.pipeThrough(transform).pipeTo(sink)
const piece = await result

Reference: FRC-0069 https://github.com/filecoin-project/FIPs/blob/master/FRCs/frc-0069.md

PieceCID hashing uses the Rust/WASM hasher from @hugomrdias/commp-wasm. The digest and size math are derived from @web3-storage/data-segment. See the package README for full attribution.

Namespace Description
download -
downloadAndValidate -
downloadStream -
resolvePieceUrl -
Class Description
PieceCID -
Interface Description
PieceCIDTransform A pass-through TransformStream that computes the PieceCID of the data flowing through it. The PieceCIDTransform.result promise resolves once the input stream closes.
PieceHasher Imperative incremental hasher. Use for sync code paths or when integrating with libraries that expose chunk-level callbacks.
Type Alias Description
CalculateInput Input types accepted by calculate.
PieceCIDInput Inputs accepted by from and tryFrom.
Variable Description
CODEC_CODE PieceCID codec (raw).
defaultResolvers The default resolvers to use when resolving the piece URL
MULTIHASH_CODE PieceCID multihash code (fr32-sha2-256-trunc254-padded-binary-tree).
MULTIHASH_NAME PieceCID multihash name.
Function Description
calculate Compute the PieceCID of input. Always returns a Promise. For the synchronous bytes case, use hasher directly.
chainResolver Resolve the piece URL from the chain
download Download a piece from a URL.
downloadAndValidate Download data from a URL, validate its PieceCID, and return as Uint8Array
downloadStream Download a piece from a URL as a stream, validating its PieceCID as the bytes flow through.
equals Compare two PieceCID inputs for equality. Invalid inputs return false.
filbeamResolver Resolve the piece URL from the FilBeam CDN
findPieceOnProviders Find the piece on the providers
from Construct a PieceCID from any supported input.
hasher Create a new PieceHasher.
is Type guard for PieceCID instances. Plain CIDs need tryFrom to gain accessors. Cross-realm/bundle safe via PIECE_CID_TAG.
providersResolver Resolve the piece URL from the providers
resolvePieceUrl Resolve the piece URL from the available resolvers
transformStream -
tryFrom Construct a PieceCID from any supported input, or null if the input is null/undefined or fails validation.

Re-exports createPieceUrl


Re-exports createPieceUrlFilBeam


Re-exports createPieceUrlPDP