### Install multiformats via npm Source: https://multiformats.github.io/js-multiformats Install the multiformats library using npm for use in your project. ```bash $ npm i multiformats ``` -------------------------------- ### Use Multibase Encoders and Decoders Source: https://multiformats.github.io/js-multiformats/modules/index.html Examples of serializing CIDs to strings and parsing them back using multibase. ```javascript import { base64 } from "multiformats/bases/base64" cid.toString(base64.encoder) //> 'mAYAEEiCTojlxqRTl6svwqNJRVM2jCcPBxy+7mRTUfGDzy2gViA' ``` ```javascript CID.parse('mAYAEEiCTojlxqRTl6svwqNJRVM2jCcPBxy+7mRTUfGDzy2gViA', base64.decoder) //> CID(bagaaierasords4njcts6vs7qvdjfcvgnume4hqohf65zsfguprqphs3icwea) ``` ```javascript cid.toString(base64) CID.parse(cid.toString(base64), base64) ``` ```javascript const v1 = CID.parse('bagaaierasords4njcts6vs7qvdjfcvgnume4hqohf65zsfguprqphs3icwea') v1.toString() //> 'bagaaierasords4njcts6vs7qvdjfcvgnume4hqohf65zsfguprqphs3icwea' const v0 = CID.parse('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n') v0.toString() //> 'QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n' v0.toV1().toString() //> 'bafybeihdwdcefgh4dqkjv67uzcmw7ojee6xedzdetojuzjevtenxquvyku' ``` -------------------------------- ### Define a custom JSON Multicodec Source: https://multiformats.github.io/js-multiformats Provides an example implementation of a JSON block codec conforming to the `BlockCodec` interface. It includes `name`, `code`, `encode`, and `decode` properties for JSON serialization and deserialization. ```javascript export const { name, code, encode, decode } = { name: 'json', code: 0x0200, encode: json => new TextEncoder().encode(JSON.stringify(json)), decode: bytes => JSON.parse(new TextDecoder().decode(bytes)) } ``` -------------------------------- ### Function walk Source: https://multiformats.github.io/js-multiformats/functions/traversal.walk.html The walk function allows for recursive traversal of a data structure starting from a given CID. It utilizes a provided 'load' function to fetch blocks and a 'seen' set to prevent infinite loops. ```APIDOC ## Function walk ### Description Traverses a data structure starting from a given CID, using a provided load function and a set to track visited CIDs. ### Method N/A (This is a function signature, not an HTTP endpoint) ### Endpoint N/A ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example N/A ### Response #### Success Response (200) N/A (This function returns void) #### Response Example N/A ### Parameters #### `__namedParameters` - **cid** (CID) - Required - The starting CID for the traversal. - **seen** (Set) - Optional - A set to keep track of visited CIDs to prevent cycles. - **load** (function) - Required - A function that takes a CID and returns a Promise resolving to a BlockView or null. This function is used to load blocks during traversal. - **cid** (CID) - The CID of the block to load. - **Returns** (Promise | null>) - A Promise that resolves to the loaded block view or null if the block is not found. ``` -------------------------------- ### Function: from Source: https://multiformats.github.io/js-multiformats/functions/hashes_hasher.from.html Initializes a new Hasher instance using the provided HasherInit configuration. ```APIDOC ## Function: from ### Description Creates a new Hasher instance based on the provided initialization parameters. ### Parameters - **__namedParameters** (HasherInit) - Required - The configuration object containing the name and code for the hasher. ### Returns - **Hasher** - Returns a configured Hasher instance. ``` -------------------------------- ### from Function Source: https://multiformats.github.io/js-multiformats/modules/hashes_hasher.html Creates a Hasher instance from a given name and options. ```APIDOC ## Function from ### Description Creates a Hasher instance. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **name** (string): The name of the hash algorithm. - **options** (HasherInit): Initialization options for the hasher. ### Returns - **Hasher**: An instance of the Hasher class. ``` -------------------------------- ### Create a CID with JSON codec and SHA256 hash Source: https://multiformats.github.io/js-multiformats Demonstrates creating a CID by encoding JSON data, hashing it with SHA256, and then creating the CID object. Requires importing CID, JSON codec, and SHA256 hasher. ```javascript import { CID } from 'multiformats/cid' import * as json from 'multiformats/codecs/json' import { sha256 } from 'multiformats/hashes/sha2' const bytes = json.encode({ hello: 'world' }) const hash = await sha256.digest(bytes) const cid = CID.create(1, json.code, hash) //> CID(bagaaierasords4njcts6vs7qvdjfcvgnume4hqohf65zsfguprqphs3icwea) ``` -------------------------------- ### Create a custom SHA256 Multihash Hasher Source: https://multiformats.github.io/js-multiformats Demonstrates creating a custom SHA256 multihash hasher using the `hasher.from` utility. This involves defining the hasher's `name`, `code`, and `encode` function, which uses Node.js crypto for hashing. ```javascript import * as hasher from 'multiformats/hashes/hasher' const sha256 = hasher.from({ // As per multiformats table // https://github.com/multiformats/multicodec/blob/master/table.csv#L9 name: 'sha2-256', code: 0x12, encode: (input) => new Uint8Array(crypto.createHash('sha256').update(input).digest()) }) const hash = await sha256.digest(json.encode({ hello: 'world' })) CID.create(1, json.code, hash) //> CID(bagaaierasords4njcts6vs7qvdjfcvgnume4hqohf65zsfguprqphs3icwea) ``` -------------------------------- ### HasherInit Interface Source: https://multiformats.github.io/js-multiformats/modules/hashes_hasher.html Initialization options for a Hasher. ```APIDOC ## Interface HasherInit ### Description Initialization options for a Hasher. ### Properties - **name** (string): The name of the hash algorithm. - **options** (DigestOptions): Options for the digest. ``` -------------------------------- ### Basics Module Source: https://multiformats.github.io/js-multiformats/modules/basics.html Information about the 'basics' module within the js-multiformats library. ```APIDOC ## Module basics ### Variables - **bases** - **codecs** - **hashes** ### References - **bytes** → bytes - **CID** → CID - **digest** → hashes/digest - **hasher** → hashes/hasher - **varint** → varint ``` -------------------------------- ### CID Constructor Source: https://multiformats.github.io/js-multiformats/classes/cid.CID.html Constructs a new CID instance with the specified version, code, multihash, and bytes. ```APIDOC ## Constructors ### constructor * new CID< Data = unknown, Format extends number = number, Alg extends number = number, Version extends Version = Version, >( version: Version, code: Format, multihash: MultihashDigest, bytes: Uint8Array, ): CID #### Type Parameters * Data = unknown * Format extends number = number * Alg extends number = number * Version extends Version = Version #### Parameters * version: Version Version of the CID * code: Format Code of the codec content is encoded in, see https://github.com/multiformats/multicodec/blob/master/table.csv * multihash: MultihashDigest (Multi)hash of the of the content. * bytes: Uint8Array #### Returns CID ``` -------------------------------- ### Function create Source: https://multiformats.github.io/js-multiformats/functions/link.create.html Creates a simplified version of a CIDv1 link. ```APIDOC ## Function create ### Description Creates a simplified version of a CIDv1 link. ### Parameters - **code** (Code extends number) - Required - Content encoding format code. - **digest** (MultihashDigest) - Required - Multihash of the content. ### Returns - **Link** - A link object representing the CIDv1. ``` -------------------------------- ### Hasher Class Constructor Source: https://multiformats.github.io/js-multiformats/classes/hashes_hasher.Hasher.html Initializes a new Hasher instance with a name, code, and encoding function. ```APIDOC ## Constructor ### Description Creates a new Hasher instance representing a hashing algorithm. ### Parameters - **name** (Name) - Required - The name of the multihash. - **code** (Code) - Required - The code of the multihash. - **encode** (Function) - Required - Function that takes a Uint8Array and returns a digest. - **minDigestLength** (number) - Optional - Minimum length of the digest. - **maxDigestLength** (number) - Optional - Maximum length of the digest. ``` -------------------------------- ### Function createLegacy Source: https://multiformats.github.io/js-multiformats/functions/link.createLegacy.html Creates a legacy CIDv0 link from a provided MultihashDigest. ```APIDOC ## createLegacy ### Description Simplified version of the create function used to generate a CIDv0 LegacyLink. ### Parameters - **digest** (MultihashDigest<18>) - Required - The multihash digest to convert into a legacy link. ### Returns - **LegacyLink** - The resulting legacy CIDv0 link object. ``` -------------------------------- ### Create and decode IPLD Blocks Source: https://multiformats.github.io/js-multiformats Shows how to encode a JavaScript object into an IPLD block using a specified codec and hasher, and then decode it back. Supports verification of the hash on decode if the CID is known. ```javascript import * as Block from 'multiformats/block' import * as codec from '@ipld/dag-cbor' import { sha256 as hasher } from 'multiformats/hashes/sha2' const value = { hello: 'world' } // encode a block let block = await Block.encode({ value, codec, hasher }) block.value // { hello: 'world' } block.bytes // Uint8Array block.cid // CID() w/ sha2-256 hash address and dag-cbor codec // you can also decode blocks from their binary state block = await Block.decode({ bytes: block.bytes, codec, hasher }) // if you have the cid you can also verify the hash on decode block = await Block.create({ bytes: block.bytes, cid: block.cid, codec, hasher }) ``` -------------------------------- ### Function create Source: https://multiformats.github.io/js-multiformats/functions/block.create.html The 'create' function is used to create a block with specified data, codec, hashing algorithm, and CID version. ```APIDOC ## Function create ### Description Creates a block with the given data, codec, hashing algorithm, and CID version. ### Method N/A (This is a function, not an HTTP endpoint) ### Endpoint N/A ### Parameters #### __namedParameters: CreateInput - **T** (Logical type) - The logical type of the data encoded in the block. - **Code** (number) - The multicodec code corresponding to the codec used to encode the block. - **Alg** (number) - The multicodec code corresponding to the hashing algorithm used in CID creation. - **V** (Version) - The CID version. ### Returns Promise> - A promise that resolves to a BlockView object containing the created block. ### Type Parameters - **T**: Logical type of the data encoded in the block. - **Code** (extends number): Multicodec code corresponding to codec used to encode the block. - **Alg** (extends number): Multicodec code corresponding to the hashing algorithm used in CID creation. - **V** (extends Version): CID version. ``` -------------------------------- ### Function createUnsafe Source: https://multiformats.github.io/js-multiformats/functions/block.createUnsafe.html Creates a BlockView from provided input parameters including codec, hashing algorithm, and CID version. ```APIDOC ## Function createUnsafe ### Description Creates a BlockView instance using the specified codec, hashing algorithm, and CID version. This is an unsafe operation intended for specific block construction scenarios. ### Parameters - **__namedParameters** (CreateUnsafeInput) - Required - An object containing the configuration for the block, including the data, codec code, hashing algorithm code, and CID version. ### Returns - **BlockView** - A view of the block constructed with the provided parameters. ``` -------------------------------- ### CID toString and parse with Base64 codec Source: https://multiformats.github.io/js-multiformats Demonstrates using a multibase codec (base64) for both serializing a CID to a string and parsing it back. Codecs provide both encoder and decoder functionality. ```javascript cid.toString(base64) CID.parse(cid.toString(base64), base64) ``` -------------------------------- ### HasherInit Interface Source: https://multiformats.github.io/js-multiformats/interfaces/hashes_hasher.HasherInit.html Defines the structure for initializing a hasher with its configuration and encoding capabilities. ```APIDOC ## Interface HasherInit ### Description Represents the initialization configuration for a hasher, including its name, code, and optional length constraints. ### Type Parameters * `Name` extends `string` - The name of the hash algorithm. * `Code` extends `number` - The numerical code associated with the hash algorithm. ### Properties #### `code` - **code** (Code) - The numerical code for the hash algorithm. #### `Optional` maxDigestLength - **maxDigestLength** (number) - The maximum length a hash is allowed to be truncated to in bytes. If not specified, it will be inferred from the length of the digest. #### `Optional` minDigestLength - **minDigestLength** (number) - The minimum length a hash is allowed to be truncated to in bytes. Defaults to 20. #### `name` - **name** (Name) - The name of the hash algorithm. ### Methods #### encode - **encode**(input: Uint8Array): Await> - Encodes the input byte array using the hasher. ### Request Example ```json { "name": "sha2-256", "code": 32, "maxDigestLength": 32, "minDigestLength": 32 } ``` ### Response Example ```json { "digest": "" } ``` ``` -------------------------------- ### ByteView Interface Source: https://multiformats.github.io/js-multiformats/interfaces/block_interface.ByteView.html Defines the structure and methods of the ByteView interface. ```APIDOC ## Interface ByteView A byte-encoded representation of some type of `Data`. A `ByteView` is essentially a `Uint8Array` that's been "tagged" with a `Data` type parameter indicating the type of encoded data. For example, a `ByteView<{ hello: "world" }>` is a `Uint8Array` containing a binary representation of a `{hello: "world"}`. ### Type Parameters * Data ### Hierarchy * Uint8Array * Phantom * ByteView ### Indexable * [index: number]: number ### Properties #### `Optional` [Marker] `"[Marker]"?: Data` #### `Readonly` [toStringTag] `"[toStringTag]": "Uint8Array"` #### `Readonly` buffer `buffer: ArrayBufferLike` The ArrayBuffer instance referenced by the array. #### `Readonly` byteLength `byteLength: number` The length in bytes of the array. #### `Readonly` byteOffset `byteOffset: number` The offset in bytes of the array. #### `Readonly` BYTES_PER_ELEMENT `BYTES_PER_ELEMENT: number` The size in bytes of each element in the array. #### `Readonly` length `length: number` The length of the array. ### Methods #### `[iterator]` `"[iterator]"(): ArrayIterator` #### at `at(index: number): number | undefined` #### copyWithin `copyWithin(target: number, start: number, end?: number): this` #### entries `entries(): ArrayIterator<[number, number]>` #### every `every( predicate: (value: number, index: number, array: this) => unknown, thisArg?: any, ): boolean` #### fill `fill(value: number, start?: number, end?: number): this` #### filter `filter( predicate: (value: number, index: number, array: this) => any, thisArg?: any, ): Uint8Array` #### find `find( predicate: (value: number, index: number, obj: this) => boolean, thisArg?: any, ): number | undefined` #### findIndex `findIndex( predicate: (value: number, index: number, obj: this) => boolean, thisArg?: any, ): number` #### findLast `findLast( predicate: (value: number, index: number, array: this) => value is S, thisArg?: any, ): S | undefined` #### findLast `findLast( predicate: (value: number, index: number, array: this) => unknown, thisArg?: any, ): number | undefined` #### findLastIndex `findLastIndex( predicate: (value: number, index: number, array: this) => unknown, thisArg?: any, ): number` #### forEach `forEach( callbackfn: (value: number, index: number, array: this) => void, thisArg?: any, ): void` #### includes `includes(searchElement: number, fromIndex?: number): boolean` #### indexOf `indexOf(searchElement: number, fromIndex?: number): number` #### join `join(separator?: string): string` #### keys `keys(): ArrayIterator` #### lastIndexOf `lastIndexOf(searchElement: number, fromIndex?: number): number` #### map `map( callbackfn: (value: number, index: number, array: this) => number, thisArg?: any, ): Uint8Array` #### reduce `reduce( callbackfn: ( previousValue: number, currentValue: number, currentIndex: number, array: this, ) => number, ): number` #### reduce `reduce( callbackfn: ( previousValue: number, currentValue: number, currentIndex: number, array: this, ) => number, initialValue: number, ): number` #### reduce `reduce( callbackfn: ( previousValue: U, currentValue: number, currentIndex: number, array: this, ) => U, initialValue: U, ): U` #### reduceRight `reduceRight( callbackfn: ( previousValue: number, currentValue: number, currentIndex: number, array: this, ) => number, ): number` #### reduceRight `reduceRight( callbackfn: ( previousValue: number, currentValue: number, currentIndex: number, array: this, ) => number, initialValue: number, ): number` #### reduceRight `reduceRight( callbackfn: ( previousValue: U, currentValue: number, currentIndex: number, array: this, ) => U, initialValue: U, ): U` #### reverse `reverse(): this` #### set `set(array: ArrayLike, offset?: number): void` #### slice `slice(start?: number, end?: number): Uint8Array` #### some `some( predicate: (value: number, index: number, array: this) => unknown, thisArg?: any, ): boolean` #### sort `sort(compareFn?: (a: number, b: number) => number): this` #### subarray `subarray(begin?: number, end?: number): Uint8Array` #### toLocaleString `toLocaleString(): string` #### toLocaleString `toLocaleString( locales: string | string[], options?: NumberFormatOptions, ): string` #### toReversed `toReversed(): Uint8Array` #### toSorted `toSorted( compareFn?: (a: number, b: number) => number, ): Uint8Array` #### toString `toString(): string` #### valueOf `valueOf(): this` #### values `values(): ArrayIterator` #### with `with(index: number, value: number): Uint8Array` ``` -------------------------------- ### CID Static Methods Source: https://multiformats.github.io/js-multiformats/classes/cid.CID.html Static methods for creating and validating CID instances, including utility functions for type checking. ```APIDOC ### `Static`asCID * asCID< Data, Format extends number, Alg extends number, Version extends Version, U, >( input: U | Link, ): CID | null Takes any input `value` and returns a `CID` instance if it was a `CID` otherwise returns `null`. If `value` is instanceof `CID` it will return value back. If `value` is not instance of this CID class, but is compatible CID it will return new instance of this `CID` class. Otherwise returns null. This allows two different incompatible versions of CID library to co-exist and interop as long as binary interface is compatible. #### Type Parameters * Data * Format extends number * Alg extends number * Version extends Version * U #### Parameters * input: U | Link #### Returns CID | null ### `Static`create * create( version: Version, code: Format, digest: MultihashDigest, ): CID #### Type Parameters * Data * Format extends number * Alg extends number * Version extends Version #### Parameters * version: Version Version of the CID * code: Format Code of the codec content is encoded in, see https://github.com/multiformats/multicodec/blob/master/table.csv * digest: MultihashDigest (Multi)hash of the of the content. #### Returns CID ### `Static`createV0 * createV0(digest: MultihashDigest<18>): CID Simplified version of `create` for CIDv0. #### Type Parameters * T = unknown #### Parameters * digest: MultihashDigest<18> #### Returns CID ### `Static`createV1 * createV1( code: Code, digest: MultihashDigest, ): CID Simplified version of `create` for CIDv1. ``` -------------------------------- ### join Source: https://multiformats.github.io/js-multiformats/interfaces/block_interface.ByteView.html Adds all the elements of an array separated by the specified separator string. ```APIDOC ## join ### Description Adds all the elements of an array separated by the specified separator string. ### Method join ### Parameters #### Path Parameters - **separator** (string) - Optional - A string used to separate one element of an array from the next in the resulting String. If omitted, the array elements are separated with a comma. ### Returns string - A string that represents the elements of the array joined into one string. ### Example ```javascript const array = ["a", "b", "c"]; console.log(array.join("-")); // Output: "a-b-c" console.log(array.join()); // Output: "a,b,c" ``` ``` -------------------------------- ### BlockView Method Signatures Source: https://multiformats.github.io/js-multiformats/interfaces/block_interface.BlockView.html Method definitions for retrieving data, links, and tree paths from a BlockView. ```typescript get(path: string): BlockCursorView ``` ```typescript links(): Iterable<[string, CID]> ``` ```typescript tree(): Iterable ``` -------------------------------- ### forEach Source: https://multiformats.github.io/js-multiformats/interfaces/block_interface.ByteView.html Executes a provided function once for each array element. ```APIDOC ## forEach ### Description Performs the specified action for each element in an array. ### Method forEach ### Parameters #### Path Parameters - **callbackfn** (function) - Required - A function that accepts up to three arguments. forEach calls the callbackfn function one time for each element in the array. - **thisArg** (any) - Optional - An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value. ### Returns void ### Example ```javascript const array = [1, 2, 3]; array.forEach(element => { console.log(element); }); // Output: // 1 // 2 // 3 ``` ``` -------------------------------- ### Digest Class Source: https://multiformats.github.io/js-multiformats/classes/hashes_digest.Digest.html Documentation for the Digest class, its constructor, and properties. ```APIDOC ## Class Digest Represents a multihash digest which carries information about the hashing algorithm and an actual hash digest. #### Type Parameters * Code extends number * Size extends number #### Implements * MultihashDigest ### Constructors #### constructor * new Digest( code: Code, size: Size, digest: Uint8Array, bytes: Uint8Array, ): Digest Creates a multihash digest. ##### Type Parameters * Code extends number * Size extends number ##### Parameters * `code`: Code * `size`: Size * `digest`: Uint8Array * `bytes`: Uint8Array ##### Returns Digest ## Properties ### `Readonly` bytes bytes: Uint8Array Binary representation of this multihash digest. ### `Readonly` code code: Code Code of the multihash ### `Readonly` digest digest: Uint8Array Raw digest (without a hashing algorithm info) ### `Readonly` size size: Size byte length of the `this.digest` ``` -------------------------------- ### varint Namespace Functions Source: https://multiformats.github.io/js-multiformats/modules/index.varint.html Overview of the available functions for handling variable-length integers. ```APIDOC ## varint Namespace ### Functions - **decode**: Decodes a variable-length integer from a byte source. - **encodeTo**: Encodes a number into a variable-length integer and writes it to a target buffer. - **encodingLength**: Calculates the number of bytes required to encode a given integer as a varint. ``` -------------------------------- ### Base32pad Codec Source: https://multiformats.github.io/js-multiformats/variables/bases_base32.base32pad.html Details the 'base32pad' codec, a variant of base32 encoding. ```APIDOC ## Variable base32pad`Const` ### Description Represents the 'base32pad' codec, which is a specific implementation within the multiformats library. ### Method N/A (Constant Declaration) ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response N/A ### Code Example ```typescript base32pad: Codec<"base32pad", "c"> = ... ``` ``` -------------------------------- ### Block Class Constructor Source: https://multiformats.github.io/js-multiformats/classes/block.Block.html Instantiates a new Block object with provided bytes, CID, and value. ```APIDOC ## new Block(__namedParameters: { bytes: ByteView; cid: CID; value: T }): Block ### Description Creates a new Block instance. ### Parameters - **__namedParameters** (object) - Required - An object containing: - **bytes** (ByteView) - The byte representation of the block's data. - **cid** (CID) - The Content Identifier for the block. - **value** (T) - The decoded value of the block. ### Returns - **Block** - A new Block instance. ``` -------------------------------- ### Load multiformats via script tag Source: https://multiformats.github.io/js-multiformats Load the multiformats library directly into your HTML using a script tag. Exports will be available globally as `Multiformats`. ```html ``` -------------------------------- ### CID default base encoding and parsing Source: https://multiformats.github.io/js-multiformats Shows how CID objects use default base encodings (like base32 and base58btc) for serialization and parsing without explicitly providing base encoders/decoders. Includes conversion between CID versions. ```javascript const v1 = CID.parse('bagaaierasords4njcts6vs7qvdjfcvgnume4hqohf65zsfguprqphs3icwea') v1.toString() //> 'bagaaierasords4njcts6vs7qvdjfcvgnume4hqohf65zsfguprqphs3icwea' const v0 = CID.parse('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n') v0.toString() //> 'QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n' v0.toV1().toString() //> 'bafybeihdwdcefgh4dqkjv67uzcmw7ojee6xedzdetojuzjevtenxquvyku' ``` -------------------------------- ### CID Class and Functions Source: https://multiformats.github.io/js-multiformats/modules/cid.html Overview of the CID class and its associated functions for creating, formatting, and serializing CIDs. ```APIDOC ## Module cid ### Classes #### Class: CID Represents a Content Identifier (CID). ### Functions #### Function: format Formats a CID into a string representation. #### Function: fromJSON Creates a CID object from a JSON representation. #### Function: toJSON Converts a CID object into its JSON representation. ``` -------------------------------- ### Traverse DAG with walk() Source: https://multiformats.github.io/js-multiformats Use `walk()` to traverse a DAG in depth-first order, calling a loader function for each block. The loader fetches blocks and can be used to collect CIDs in traversal order. Ensure blocks are fetched correctly by the loader. ```javascript import { walk } from 'multiformats/traversal' import * as Block from 'multiformats/block' import * as codec from 'multiformats/codecs/json' import { sha256 as hasher } from 'multiformats/hashes/sha2' // build a DAG (a single block for this simple example) const value = { hello: 'world' } const block = await Block.encode({ value, codec, hasher }) const { cid } = block console.log(cid) //> CID(bagaaierasords4njcts6vs7qvdjfcvgnume4hqohf65zsfguprqphs3icwea) // create a loader function that also collects CIDs of blocks in // their traversal order const load = (cid, blocks) => async (cid) => { // fetch a block using its cid // e.g.: const block = await fetchBlockByCID(cid) blocks.push(cid) return block } // collect blocks in this DAG starting from the root `cid` const blocks = [] await walk({ cid, load: load(cid, blocks) }) console.log(blocks) //> [CID(bagaaierasords4njcts6vs7qvdjfcvgnume4hqohf65zsfguprqphs3icwea)] ``` -------------------------------- ### Function create Source: https://multiformats.github.io/js-multiformats/functions/hashes_digest.create.html Creates a multihash digest using a specified code and digest byte array. ```APIDOC ## create ### Description Creates a multihash digest. ### Parameters - **code** (number) - Required - The multihash code. - **digest** (Uint8Array) - Required - The digest byte array. ### Returns - **Digest** - A multihash digest object. ``` -------------------------------- ### Method: decode Source: https://multiformats.github.io/js-multiformats/interfaces/bases_interface.MultibaseDecoder.html Decodes a multibase string into a Uint8Array. The input must include a valid multibase prefix. ```APIDOC ## decode ### Description Decodes a multibase string (which must have a multibase prefix added) into a Uint8Array. If the provided prefix does not match, an exception is thrown. ### Parameters - **multibase** (Multibase) - Required - The multibase string to be decoded. ### Returns - **Uint8Array** - The decoded byte array. ``` -------------------------------- ### Base32hexpad Codec Source: https://multiformats.github.io/js-multiformats/variables/bases_base32.base32hexpad.html Details on the base32hexpad codec, including its type and usage. ```APIDOC ## Variable base32hexpad`Const` ### Description Represents the base32hexpad codec, a constant value within the multiformats library. ### Method N/A ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response #### Success Response (200) - **base32hexpad** (Codec<"base32hexpad", "t">) - The base32hexpad codec object. #### Response Example ```json { "base32hexpad": "Codec<\"base32hexpad\", \"t\">" } ``` ``` -------------------------------- ### Module bases/base256emoji Source: https://multiformats.github.io/js-multiformats/modules/bases_base256emoji.html Documentation for the base256emoji module within the multiformats library. ```APIDOC ## Module bases/base256emoji ### Description This module provides the base256emoji encoding implementation for the multiformats library. ### Variables - **base256emoji** - The base256emoji encoder/decoder instance. ``` -------------------------------- ### Static inspectBytes Source: https://multiformats.github.io/js-multiformats/classes/cid.CID.html Inspects the initial bytes of a CID to determine its properties like codec, version, and digest size. ```APIDOC ## Static inspectBytes ### Description Inspect the initial bytes of a CID to determine its properties. It is recommended that at least 10 bytes be made available in the initialBytes argument. ### Parameters #### Request Body - **initialBytes** (ByteView>) - Required - The initial bytes of the CID. ### Response #### Success Response (200) - **codec** (C) - The codec code. - **digestSize** (number) - The size of the digest. - **multihashCode** (A) - The multihash algorithm code. - **multihashSize** (number) - The size of the multihash. - **size** (number) - The total size. - **version** (V) - The CID version. ``` -------------------------------- ### CID Methods Source: https://multiformats.github.io/js-multiformats/classes/cid.CID.html Contains methods for comparing CIDs, converting them to different formats, and serializing them. ```APIDOC ## Methods ### equals * equals(other: unknown): other is CID #### Parameters * other: unknown #### Returns other is CID ### link * link(): this #### Returns this ### toJSON * toJSON(): LinkJSON> #### Returns LinkJSON> ### toString * toString(base?: MultibaseEncoder): string Returns a string representation of an object. #### Parameters * `Optional`base: MultibaseEncoder #### Returns string ### toV0 * toV0(): CID #### Returns CID ### toV1 * toV1(): CID #### Returns CID ``` -------------------------------- ### Base64pad Codec Source: https://multiformats.github.io/js-multiformats/variables/bases_base64.base64pad.html Information about the base64pad codec, a variable codec for base64 encoding. ```APIDOC ## Variable base64pad`Const` ### Description Represents the base64pad codec, which is a variable codec used for base64 encoding. ### Type `Codec<"base64pad", "M">` ``` -------------------------------- ### set Source: https://multiformats.github.io/js-multiformats/interfaces/block_interface.ByteView.html Sets a value or an array of values. ```APIDOC ## set ### Description Sets a value or an array of values. ### Parameters - **array** (ArrayLike) - Required - A typed or untyped array of values to set. - **offset** (number) - Optional - The index in the current array at which the values are to be written. ### Returns - **void** ``` -------------------------------- ### Link Interface Methods Source: https://multiformats.github.io/js-multiformats/interfaces/link_interface.Link.html Details the methods available on the Link interface, such as equals, link, toString, and toV1. ```typescript equals(other: unknown): other is Link ``` ```typescript link(): Link ``` ```typescript toString( base?: MultibaseEncoder, ): ToString, Prefix> ``` ```typescript toV1(): Link ``` -------------------------------- ### base58btc Codec Source: https://multiformats.github.io/js-multiformats/variables/bases_base58.base58btc.html Details regarding the base58btc constant codec implementation. ```APIDOC ## base58btc ### Description The base58btc constant is a Codec implementation for the 'base58btc' format, identified by the prefix 'z'. ### Type Codec<"base58btc", "z"> ``` -------------------------------- ### base32hexpadupper Codec Source: https://multiformats.github.io/js-multiformats/variables/bases_base32.base32hexpadupper.html Details regarding the base32hexpadupper constant codec implementation. ```APIDOC ## base32hexpadupper ### Description A constant representing the base32hexpadupper codec implementation within the multiformats library. ### Type Codec<"base32hexpadupper", "T"> ``` -------------------------------- ### Block Methods Source: https://multiformats.github.io/js-multiformats/classes/block.Block.html Provides methods for interacting with the Block's content and structure. ```APIDOC ## Block Methods ### get * get(path?: string): BlockCursorView ### Description Retrieves a cursor view for a given path within the block's content. ### Parameters - **path** (string) - Optional - The path to navigate within the block. Defaults to '/'. ### Returns - **BlockCursorView** - A cursor view for the specified path. ### links * links(): Iterable<[string, CID]> ### Description Returns an iterable of links (name and CID) found within the block. ### Returns - **Iterable<[string, CID]>** - An iterable of tuples, where each tuple contains the link name and its corresponding CID. ### tree * tree(): Iterable ### Description Returns an iterable of all paths (tree structure) within the block. ### Returns - **Iterable** - An iterable of strings representing the paths within the block. ``` -------------------------------- ### bases/base36 Module Source: https://multiformats.github.io/js-multiformats/modules/bases_base36.html Provides access to the base36 and base36upper encoding variables. ```APIDOC ## Module: bases/base36 ### Description This module provides the base36 and base36upper encoding implementations for the multiformats library. ### Variables - **base36** - The base36 encoding implementation. - **base36upper** - The base36upper encoding implementation. ``` -------------------------------- ### Interface Link Source: https://multiformats.github.io/js-multiformats/interfaces/link_interface.Link.html Defines the structure and methods for an IPLD link object. ```APIDOC ## Interface Link ### Description Represents an IPLD link to specific data of type T, providing access to the underlying CID components and conversion utilities. ### Properties - **[Marker]?** (Data) - Optional marker for the linked data type. - **byteLength** (number) - Readonly length of the link bytes. - **byteOffset** (number) - Readonly offset of the link bytes. - **bytes** (ByteView) - Readonly byte representation of the link. - **code** (Format) - Readonly multicodec code for the codec. - **multihash** (MultihashDigest) - Readonly multihash digest of the CID. - **version** (V) - Readonly CID version. ### Methods - **equals(other: unknown)**: Checks if the link is equal to another object. - **link()**: Returns the link instance. - **toString(base?)**: Returns a string representation of the link, optionally using a specific multibase encoder. - **toV1()**: Converts the link to CID version 1. ``` -------------------------------- ### Interface DigestOptions Source: https://multiformats.github.io/js-multiformats/interfaces/hashes_hasher.DigestOptions.html Configuration options for digest operations, including optional truncation settings. ```APIDOC ## Interface DigestOptions ### Description Configuration options for digest operations. Allows for the truncation of the returned digest to a specific number of bytes. ### Properties - **truncate** (number) - Optional - Truncate the returned digest to this number of bytes. This may cause the digest method to throw/reject if the value is greater than the digest length or below a threshold where the risk of hash collisions is significant. ``` -------------------------------- ### LegacyLink Methods Source: https://multiformats.github.io/js-multiformats/interfaces/link_interface.LegacyLink.html Details the methods available on the LegacyLink interface for comparison, conversion, and string representation. ```typescript equals(other: unknown): other is Link ``` ```typescript link(): Link ``` ```typescript toString( base?: MultibaseEncoder, ): ToString, Prefix> ``` ```typescript toV1(): Link ``` -------------------------------- ### Static parse Source: https://multiformats.github.io/js-multiformats/classes/cid.CID.html Parses a string representation of a CID into a CID instance. ```APIDOC ## Static parse ### Description Takes a CID in a string representation and creates an instance. If base decoder is not provided, it will use a default from the configuration. ### Parameters #### Request Body - **source** (ToString, Prefix>) - Required - The string representation of the CID. - **base** (MultibaseDecoder) - Optional - The multibase decoder to use. ### Response #### Success Response (200) - **CID** (CID) - The parsed CID instance. ``` -------------------------------- ### codecs/raw Module Functions Source: https://multiformats.github.io/js-multiformats/modules/codecs_raw.html Overview of the encode and decode functions available in the raw codec module. ```APIDOC ## Functions ### encode Encodes data into the raw format. ### decode Decodes data from the raw format. ``` -------------------------------- ### Codecs Interface Documentation Source: https://multiformats.github.io/js-multiformats/modules/codecs_interface.html This section provides documentation for the core interfaces related to codecs in the js-multiformats library. ```APIDOC ## Codecs Interface This module defines the interfaces for codecs used in the js-multiformats library. ### Interfaces #### BlockCodec Represents a codec that can encode and decode blocks of data. ### BlockDecoder Represents a decoder for blocks of data. ### BlockEncoder Represents an encoder for blocks of data. ### References - **ArrayBufferView**: Refers to ArrayBufferView. - **ByteView**: Refers to ByteView. ``` -------------------------------- ### Function toString Source: https://multiformats.github.io/js-multiformats/functions/bytes.toString.html Converts a Uint8Array into a string representation. ```APIDOC ## toString ### Description Converts a Uint8Array into a string representation. ### Parameters - **b** (Uint8Array) - Required - The byte array to convert. ### Returns - **string** - The string representation of the input byte array. ``` -------------------------------- ### base32padupper Codec Source: https://multiformats.github.io/js-multiformats/variables/bases_base32.base32padupper.html Details regarding the base32padupper constant which implements the Codec interface. ```APIDOC ## base32padupper ### Description The `base32padupper` constant is a codec implementation for the 'base32padupper' encoding scheme. ### Type `Codec<"base32padupper", "C">` ``` -------------------------------- ### Static decodeFirst Source: https://multiformats.github.io/js-multiformats/classes/cid.CID.html Decodes a CID from the beginning of a byte array, returning the CID and the remaining bytes. ```APIDOC ## Static decodeFirst ### Description Decodes a CID from its binary representation at the beginning of a byte array. Returns an array with the first element containing the CID and the second element containing the remainder of the original byte array. ### Parameters #### Request Body - **bytes** (ByteView>) - Required - The byte array containing a CID at the start. ### Response #### Success Response (200) - **result** (Array) - [CID, Uint8Array] ``` -------------------------------- ### codecs/json Module Functions Source: https://multiformats.github.io/js-multiformats/modules/codecs_json.html Overview of the available functions for JSON encoding and decoding. ```APIDOC ## Functions ### decode Decodes a JSON-encoded input into a JavaScript object or value. ### encode Encodes a JavaScript object or value into a JSON-formatted string or buffer. ``` -------------------------------- ### Link Interface Type Parameters Source: https://multiformats.github.io/js-multiformats/interfaces/link_interface.Link.html Lists the type parameters for the Link interface, including Data, Format, Alg, and V (CID version). ```typescript * Data extends unknown = unknown * Format extends number = number * Alg extends number = number * V extends Version = 1 ``` -------------------------------- ### bases/base16 Module Source: https://multiformats.github.io/js-multiformats/modules/bases_base16.html Provides access to the base16 and base16upper encoding variables. ```APIDOC ## Module: bases/base16 ### Description This module exports base16 encoding implementations for use within the multiformats ecosystem. ### Variables - **base16** - The standard base16 encoding implementation. - **base16upper** - The uppercase variant of the base16 encoding implementation. ``` -------------------------------- ### with Source: https://multiformats.github.io/js-multiformats/interfaces/block_interface.ByteView.html Returns a copy of the array with a value replaced at a specific index. ```APIDOC ## with ### Description Copies the array and inserts the given number at the provided index. ### Parameters #### Query Parameters - **index** (number) - Required - The index of the value to overwrite. - **value** (number) - Required - The value to insert into the copied array. ### Response #### Success Response (200) - **result** (Uint8Array) - A copy of the original array with the inserted value. ```