Gathering detailed insights and metrics for multiformats
Gathering detailed insights and metrics for multiformats
Gathering detailed insights and metrics for multiformats
Gathering detailed insights and metrics for multiformats
Multiformats interface (multihash, multicodec, multibase and CID)
npm install multiformats
99.8
Supply Chain
100
Quality
89.1
Maintenance
100
Vulnerability
87.6
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
233 Stars
373 Commits
54 Forks
19 Watching
33 Branches
52 Contributors
Updated on 27 Nov 2024
TypeScript (100%)
Cumulative downloads
Total Downloads
Last day
14.2%
198,989
Compared to previous day
Last week
12.7%
998,235
Compared to previous week
Last month
6.2%
3,897,960
Compared to previous month
Last year
76.6%
41,992,241
Compared to previous year
Interface for multihash, multicodec, multibase and CID
This library defines common interfaces and low level building blocks for various interrelated multiformat technologies (multicodec, multihash, multibase, and CID). They can be used to implement custom base encoders / decoders / codecs, codec encoders /decoders and multihash hashers that comply to the interface that layers above assume.
This library provides implementations for most basics and many others can be found in linked repositories.
1import { CID } from 'multiformats/cid' 2import * as json from 'multiformats/codecs/json' 3import { sha256 } from 'multiformats/hashes/sha2' 4 5const bytes = json.encode({ hello: 'world' }) 6 7const hash = await sha256.digest(bytes) 8const cid = CID.create(1, json.code, hash) 9//> CID(bagaaierasords4njcts6vs7qvdjfcvgnume4hqohf65zsfguprqphs3icwea)
1import * as Block from 'multiformats/block' 2import * as codec from '@ipld/dag-cbor' 3import { sha256 as hasher } from 'multiformats/hashes/sha2' 4 5const value = { hello: 'world' } 6 7// encode a block 8let block = await Block.encode({ value, codec, hasher }) 9 10block.value // { hello: 'world' } 11block.bytes // Uint8Array 12block.cid // CID() w/ sha2-256 hash address and dag-cbor codec 13 14// you can also decode blocks from their binary state 15block = await Block.decode({ bytes: block.bytes, codec, hasher }) 16 17// if you have the cid you can also verify the hash on decode 18block = await Block.create({ bytes: block.bytes, cid: block.cid, codec, hasher })
CIDs can be serialized to string representation using multibase encoders that implement MultibaseEncoder
interface. This library provides quite a few implementations that can be imported:
1import { base64 } from "multiformats/bases/base64"
2cid.toString(base64.encoder)
3//> 'mAYAEEiCTojlxqRTl6svwqNJRVM2jCcPBxy+7mRTUfGDzy2gViA'
Parsing CID string serialized CIDs requires multibase decoder that implements MultibaseDecoder
interface. This library provides a decoder for every encoder it provides:
1CID.parse('mAYAEEiCTojlxqRTl6svwqNJRVM2jCcPBxy+7mRTUfGDzy2gViA', base64.decoder) 2//> CID(bagaaierasords4njcts6vs7qvdjfcvgnume4hqohf65zsfguprqphs3icwea)
Dual of multibase encoder & decoder is defined as multibase codec and it exposes
them as encoder
and decoder
properties. For added convenience codecs also
implement MultibaseEncoder
and MultibaseDecoder
interfaces so they could be
used as either or both:
1cid.toString(base64)
2CID.parse(cid.toString(base64), base64)
Note: CID implementation comes bundled with base32
and base58btc
multibase codecs so that CIDs can be base serialized to (version specific)
default base encoding and parsed without having to supply base encoders/decoders:
1const v1 = CID.parse('bagaaierasords4njcts6vs7qvdjfcvgnume4hqohf65zsfguprqphs3icwea') 2v1.toString() 3//> 'bagaaierasords4njcts6vs7qvdjfcvgnume4hqohf65zsfguprqphs3icwea' 4 5const v0 = CID.parse('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n') 6v0.toString() 7//> 'QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n' 8v0.toV1().toString() 9//> 'bafybeihdwdcefgh4dqkjv67uzcmw7ojee6xedzdetojuzjevtenxquvyku'
This library defines BlockEncoder
, BlockDecoder
and BlockCodec
interfaces.
Codec implementations should conform to the BlockCodec
interface which implements both BlockEncoder
and BlockDecoder
.
Here is an example implementation of JSON BlockCodec
.
1export const { name, code, encode, decode } = { 2 name: 'json', 3 code: 0x0200, 4 encode: json => new TextEncoder().encode(JSON.stringify(json)), 5 decode: bytes => JSON.parse(new TextDecoder().decode(bytes)) 6}
This library defines MultihashHasher
and MultihashDigest
interfaces and convinient function for implementing them:
1import * as hasher from 'multiformats/hashes/hasher' 2 3const sha256 = hasher.from({ 4 // As per multiformats table 5 // https://github.com/multiformats/multicodec/blob/master/table.csv#L9 6 name: 'sha2-256', 7 code: 0x12, 8 9 encode: (input) => new Uint8Array(crypto.createHash('sha256').update(input).digest()) 10}) 11 12const hash = await sha256.digest(json.encode({ hello: 'world' })) 13CID.create(1, json.code, hash) 14 15//> CID(bagaaierasords4njcts6vs7qvdjfcvgnume4hqohf65zsfguprqphs3icwea)
This library contains higher-order functions for traversing graphs of data easily.
walk()
walks through the links in each block of a DAG calling a user-supplied loader function for each one, in depth-first order with no duplicate block visits. The loader should return a Block
object and can be used to inspect and collect block ordering for a full DAG walk. The loader should throw
on error, and return null
if a block should be skipped by walk()
.
1import { walk } from 'multiformats/traversal' 2import * as Block from 'multiformats/block' 3import * as codec from 'multiformats/codecs/json' 4import { sha256 as hasher } from 'multiformats/hashes/sha2' 5 6// build a DAG (a single block for this simple example) 7const value = { hello: 'world' } 8const block = await Block.encode({ value, codec, hasher }) 9const { cid } = block 10console.log(cid) 11//> CID(bagaaierasords4njcts6vs7qvdjfcvgnume4hqohf65zsfguprqphs3icwea) 12 13// create a loader function that also collects CIDs of blocks in 14// their traversal order 15const load = (cid, blocks) => async (cid) => { 16 // fetch a block using its cid 17 // e.g.: const block = await fetchBlockByCID(cid) 18 blocks.push(cid) 19 return block 20} 21 22// collect blocks in this DAG starting from the root `cid` 23const blocks = [] 24await walk({ cid, load: load(cid, blocks) }) 25 26console.log(blocks) 27//> [CID(bagaaierasords4njcts6vs7qvdjfcvgnume4hqohf65zsfguprqphs3icwea)]
blockcodec-to-ipld-format
converts a multiformats BlockCodec
into an
interface-ipld-format
for use with the ipld
package. This can help bridge IPLD codecs implemented using the structure and interfaces defined here to existing code that assumes, or requires interface-ipld-format
. This bridge also includes the relevant TypeScript definitions.
By default, no base encodings (other than base32 & base58btc), hash functions,
or codec implementations are exposed by multiformats
, you need to
import the ones you need yourself.
bases | import | repo |
---|---|---|
base16 | multiformats/bases/base16 | multiformats/js-multiformats |
base32 , base32pad , base32hex , base32hexpad , base32z | multiformats/bases/base32 | multiformats/js-multiformats |
base64 , base64pad , base64url , base64urlpad | multiformats/bases/base64 | multiformats/js-multiformats |
base58btc , base58flick4 | multiformats/bases/base58 | multiformats/js-multiformats |
Other (less useful) bases implemented in multiformats/js-multiformats include: base2
, base8
, base10
, base36
and base256emoji
.
hashes | import | repo |
---|---|---|
sha2-256 , sha2-512 | multiformats/hashes/sha2 | multiformats/js-multiformats |
sha3-224 , sha3-256 , sha3-384 ,sha3-512 , shake-128 , shake-256 , keccak-224 , keccak-256 , keccak-384 , keccak-512 | @multiformats/sha3 | multiformats/js-sha3 |
identity | multiformats/hashes/identity | multiformats/js-multiformats |
murmur3-128 , murmur3-32 | @multiformats/murmur3 | multiformats/js-murmur3 |
blake2b-* , blake2s-* | @multiformats/blake2 | multiformats/js-blake2 |
codec | import | repo |
---|---|---|
raw | multiformats/codecs/raw | multiformats/js-multiformats |
json | multiformats/codecs/json | multiformats/js-multiformats |
dag-cbor | @ipld/dag-cbor | ipld/js-dag-cbor |
dag-json | @ipld/dag-json | ipld/js-dag-json |
dag-pb | @ipld/dag-pb | ipld/js-dag-pb |
dag-jose | dag-jose | ceramicnetwork/js-dag-jose |
1$ npm i multiformats
<script>
tagLoading this module through a script tag will make its exports available as Multiformats
in the global namespace.
1<script src="https://unpkg.com/multiformats/dist/index.min.js"></script>
Licensed under either of
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
10 commit(s) and 1 issue activity found in the last 90 days -- score normalized to 9
Reason
license file detected
Details
Reason
Found 5/22 approved changesets -- score normalized to 2
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
project is not fuzzed
Details
Reason
security policy file not detected
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More