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
// Lower-level primitives
Piece.fr32.expand(rawBytes)
Piece.merkle.computeNode(left, right)

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

Core primitives (FR32, merkle, streaming hasher, size math) are derived from @web3-storage/data-segment. See the package README for full attribution.

NamespaceDescription
download-
downloadAndValidate-
fr32-
merkle-
resolvePieceUrl-
ClassDescription
PieceCID-
InterfaceDescription
PieceCIDTransformA pass-through TransformStream that computes the PieceCID of the data flowing through it. The PieceCIDTransform.result promise resolves once the input stream closes.
PieceHasherImperative incremental hasher. Use for sync code paths or when integrating with libraries that expose chunk-level callbacks.
Type AliasDescription
CalculateInputInput types accepted by calculate.
PieceCIDInputInputs accepted by from and tryFrom.
VariableDescription
BYTES_PER_QUADSource bytes per FR32 quad (127).
CODEC_CODEPieceCID codec (raw).
defaultResolversThe default resolvers to use when resolving the piece URL
MAX_HEIGHTMaximum tree height (255).
MAX_SIZEMaximum payload size that a single PieceCID can represent (limited by the 1-byte tree height field).
MIN_SIZEMinimum payload size for which PieceCID is defined (smaller inputs are zero-padded up to this floor).
MULTIHASH_CODEPieceCID multihash code (fr32-sha2-256-trunc254-padded-binary-tree).
MULTIHASH_NAMEPieceCID multihash name.
FunctionDescription
calculateCompute the PieceCID of input. Always returns a Promise. For the synchronous bytes case, use hasher directly.
chainResolverResolve the piece URL from the chain
downloadDownload a piece from a URL.
downloadAndValidateDownload data from a URL, validate its PieceCID, and return as Uint8Array
equalsCompare two PieceCID inputs for equality. Invalid inputs return false.
filbeamResolverResolve the piece URL from the FilBeam CDN
findPieceOnProvidersFind the piece on the providers
fromConstruct a PieceCID from any supported input.
hasherCreate a new PieceHasher.
heightForTree height that a raw payload of rawSize will produce.
isType guard for PieceCID instances. Plain CIDs need tryFrom to gain accessors. Cross-realm/bundle safe via PIECE_CID_TAG.
paddedSizeAtHeightPadded piece size in bytes for a given tree height.
paddedSizeForPadded piece size in bytes that a raw payload of rawSize will produce.
providersResolverResolve the piece URL from the providers
resolvePieceUrlResolve the piece URL from the available resolvers
transformStream-
tryFromConstruct a PieceCID from any supported input, or null if the input is null/undefined or fails validation.

Re-exports createPieceUrl


Re-exports createPieceUrlPDP