Package detail

@ipld/dag-ucan

ipld22.9kApache-2.0 OR MIT3.4.5

UCAN codec for IPLD

UCAN, IPLD, JWT, multicodec

readme

@ipld/dag-ucan

UCAN

An implementation of UCANs in IPLD via Advanced Data Layout (ADL), designed for use with multiformats.

mascot

Overview

This library provides an ADL for representing UCANs natively in IPLD. It implements UCAN IPLD specification and uses DAG-CBOR as a primary encoding, which is hash consistent and more compact than a secondary RAW JWT encoding. Every UCAN in either encoding can be formatted into a valid JWT string and consumed by other spec compliant UCAN implementations. However UCANs issued by other libraries may end up in represented in secondory RAW JWT encoding, that is because whitespaces and key order in JWT affects signatures and there for can't be represented accurately in CBOR. When parsing UCANs library will use CBOR representation and fallback to RAW JWT, which allows interop with all existing tokens in the wild.

API

import * as UCAN from "@ipld/dag-ucan"

UCAN.parse(jwt: string): UCAN.View

Parses UCAN formatted as JWT string into a representatino that can be encoded, formatted and queried.

const ucan = UCAN.parse(jwt)
ucan.issuer.did() // did:key:z6Mkk89bC3JrVqKie71YEcc5M1SMVxuCgNx6zLZ8SYJsxALi

UCAN.format(ucan: UCAN.UCAN): string

Formats UCAN into a JWT string.

UCAN.format(UCAN.parse(jwt)) === jwt // true

UCAN.encode(ucan: UCAN.UCAN): Uint8Array

Encodes UCAN into a binary representation.

UCAN.encode(UCAN.parse(jwt)) // Uint8Array(679)

UCAN.decode(bytes: Uint8Array): UCAN.UCAN

Decodes UCAN from binary representation into object representation.

UCAN.decode(UCAN.encode(ucan))

UCAN.issue(options: UCAN.UCANOptions): Promise<UCAN.UCAN>

Issues a signed UCAN.

Please note that no capability or time bound validation takes place.

const ucan = await UCAN.issue({
  issuer: alice,
  audience: bob,
  capabilities: [
    {
      can: "fs/read",
      with: `storage://${alice.did()}/public/photos/`,
    },
    {
      can: "pin/add",
      with: alice.did(),
    },
  ],
})

Embedding Proofs

While not recommended, it is possible to inline proofs inside a single UCAN using CIDs with identity multihash:

import { identity } from "multiformats/hashes/identity"
const proof = await UCAN.issue({
  issuer: alice,
  audience: bob,
  capabilities: [{ can: "store/add", with: alice.did() }],
})

const delegation = await UCAN.issue({
  issuer: bob,
  audience: mallory,
  capabilities: proof.capabilities,
  proofs: [await UCAN.link(proof, { hasher: identity })],
})

License

Licensed under either of

Contribute

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.

changelog

Changelog

3.4.5 (2025-01-21)

Bug Fixes

  • upgrade nodejs version and de-restrict ci action (cc8e4ae)

3.4.4 (2025-01-21)

Bug Fixes

3.4.3 (2025-01-21)

Bug Fixes

  • add license and contribute sections to README (#103) (fe23d7f)

3.4.2 (2025-01-21)

Bug Fixes

3.4.1 (2025-01-21)

Bug Fixes

3.4.0 (2023-09-01)

Features

3.3.2 (2023-03-30)

Bug Fixes

3.3.1 (2023-03-30)

Bug Fixes

3.3.0 (2023-03-29)

Features

  • implement signature.verify (#82) (5d79a6b)
  • verify method on signature type (3140637)

3.2.0 (2023-01-17)

Features

  • update multiformats and other deps (#77) (6c07bbe)

3.1.0 (2022-12-14)

Features

3.0.1 (2022-12-02)

Miscellaneous Chores

3.0.0 (2022-12-01)

⚠ BREAKING CHANGES

  • simplify principal type (#67)

Features

2.0.1 (2022-11-03)

Bug Fixes

2.0.0 (2022-10-15)

⚠ BREAKING CHANGES

  • make view compatible with model (#63)

Features

1.0.0 (2022-10-13)

Features

Miscellaneous Chores

4.0.0-beta (2022-09-26)

⚠ BREAKING CHANGES

  • 0.9 spec (#57)

Features

3.0.0-beta (2022-09-12)

⚠ BREAKING CHANGES

  • add support for multiple caps in the types (#53)

Features

  • add support for multiple caps in the types (#53) (2cdae6e)

Bug Fixes

  • default for block caps and change to pnpm (#55) (d2638c3)

2.0.0-beta (2022-09-09)

⚠ BREAKING CHANGES

  • fix types for exported entrypoints and simplify others (#50)

Features

  • fix types for exported entrypoints and simplify others (#50) (f7ce660)

1.7.0-beta (2022-06-15)

Features

  • align CID/Link API with multiformats PR (#47) (4c4a16d)

1.6.0-beta (2022-06-14)

Features

1.5.0-beta (2022-05-13)

Features

Bug Fixes

1.4.1-beta (2022-05-09)

Bug Fixes

1.4.0-beta (2022-05-09)

Features

1.3.0-beta (2022-05-05)

Features

Bug Fixes

  • make verifySignature funciton usable from other things (#29) (55c22cd)

1.2.4-beta (2022-05-03)

Bug Fixes

1.2.3-beta (2022-04-21)

Bug Fixes

1.2.2-beta (2022-04-21)

Bug Fixes

1.2.1-beta (2022-04-21)

Bug Fixes

1.2.0-beta (2022-04-21)

Features

1.1.1-beta (2022-04-15)

Bug Fixes

1.1.0-beta (2022-04-13)

Features

Bug Fixes

  • add 'name' field per BlockCodec interface (00ebfbe)

1.0.0-beta (2022-04-11)

Features

  • implement cbor based representation (#4) (f8d116e)
  • initial implementation (#2) (2bc5820)
  • replace type with code discriminant (#7) (6c02e18)

Miscellaneous Chores