@ipld/dag-ucan
Version:
UCAN codec for IPLD
119 lines (81 loc) • 3.58 kB
Markdown

An implementation of [UCAN][]s in [IPLD][] via [Advanced Data Layout (ADL)](ADL), designed for use with [multiformats][].
![mascot][]
## Overview
This library provides an [ADL][] for representing [UCAN]s 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 [UCAN][]s 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
```ts
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.
```ts
const ucan = UCAN.parse(jwt)
ucan.issuer.did() // did:key:z6Mkk89bC3JrVqKie71YEcc5M1SMVxuCgNx6zLZ8SYJsxALi
```
Formats UCAN into a JWT string.
```ts
UCAN.format(UCAN.parse(jwt)) === jwt // true
```
Encodes UCAN into a binary representation.
```ts
UCAN.encode(UCAN.parse(jwt)) // Uint8Array(679)
```
Decodes UCAN from binary representation into object representation.
```ts
UCAN.decode(UCAN.encode(ucan))
```
Issues a signed UCAN.
> Please note that no capability or time bound validation takes place.
```ts
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(),
},
],
})
```
While not recommended, it is possible to inline proofs inside a single UCAN using CIDs with identity
multihash:
```ts
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 })],
})
```
Licensed under either of
- Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / <http://www.apache.org/licenses/LICENSE-2.0>)
- MIT ([LICENSE-MIT](LICENSE-MIT) / <http://opensource.org/licenses/MIT>)
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.
[]: https://ipld.io/
[]: https://github.com/ucan-wg/spec/
[]: https://github.com/ucan-wg/ucan-ipld/
[]: https://ipld.io/docs/schemas/using/authoring-guide/
[]: https://ipld.io/docs/codecs/known/dag-cbor/
[]: https://github.com/multiformats/js-multiformats
[]: https://ipld.io/docs/advanced-data-layouts/
[]:https://bafybeiap2x7s5hjxdghbpfzd7kkc6l5vqgbwnj4tjbcivnfjfcobwuqo44.ipfs.w3s.link/UCAN%20IPLD%20Mascot.png