Skip to content

Commit

Permalink
feat!: update dag-ucan, types and names (#90)
Browse files Browse the repository at this point in the history
* chore: format

* feat!: update dag-ucan, types and names

* chore: feedback

* chore: more

* chore: cov 100

* fix: address remaining inconsistencies

* fix: remaning issues

* fix: regressions in link impl

* fix: regression is asLink

* change agent to principal

* agent to principal

* Update packages/interface/src/lib.ts

Co-authored-by: Irakli Gozalishvili <[email protected]>

Co-authored-by: Irakli Gozalishvili <[email protected]>
  • Loading branch information
hugomrdias and Gozala authored Sep 14, 2022
1 parent 66a22ee commit cd792c9
Show file tree
Hide file tree
Showing 62 changed files with 860 additions and 1,359 deletions.
2 changes: 1 addition & 1 deletion .github/release-please-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"plugins": ["node-workspace"],
"bootstrap-sha": "1f995064ec6e5953118c2dd1065ee6be959f25b9",
"packages": {
"packages/authority": {},
"packages/principal": {},
"packages/client": {},
"packages/core": {},
"packages/interface": {},
Expand Down
10 changes: 9 additions & 1 deletion .github/release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
{"packages/authority":"0.5.0","packages/client":"0.6.0","packages/core":"0.6.0","packages/interface":"0.7.0","packages/server":"0.7.0","packages/transport":"0.7.0","packages/validator":"0.6.0"}
{
"packages/principal": "0.5.0",
"packages/client": "0.6.0",
"packages/core": "0.6.0",
"packages/interface": "0.7.0",
"packages/server": "0.7.0",
"packages/transport": "0.7.0",
"packages/validator": "0.6.0"
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: authority
name: principal

on:
workflow_dispatch:
Expand All @@ -7,14 +7,14 @@ on:
- main
paths:
- 'packages/interface/**'
- 'packages/authority/**'
- 'packages/principal/**'
pull_request:
branches:
- main
paths:
- 'packages/interface/**'
- 'packages/authority/**'
- '.github/workflows/authority.yml'
- 'packages/principal/**'
- '.github/workflows/principal.yml'
jobs:
check:
name: Typecheck
Expand All @@ -24,7 +24,7 @@ jobs:
node-version:
- 16
project:
- authority
- principal
steps:
- uses: actions/checkout@v2

Expand Down Expand Up @@ -58,7 +58,7 @@ jobs:
- windows-latest
- macos-latest
project:
- authority
- principal

steps:
- uses: actions/checkout@v2
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
- name: Test
run: pnpm run --if-present test:node
if: ${{ steps.release-please.outputs.releases_created }}

- name: Build
run: pnpm run --if-present build
if: ${{ steps.release-please.outputs.releases_created }}
Expand Down
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"typescript.tsdk": "node_modules/typescript/lib"
}
64 changes: 32 additions & 32 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ The very first thing we want to do is define set of capabilities our service wil
Lets define `file/link` capability, where resources are identified via `file:` URLs and MAY contain `link` to be mapped to a given path.

```ts
import { capability, URI, Link, Failure } from "@ucanto/server"
import { capability, URI, Link, Failure } from '@ucanto/server'

const Add = capability({
can: "file/link",
with: URI.match({ protocol: "file:" }),
can: 'file/link',
with: URI.match({ protocol: 'file:' }),
caveats: { link: Link },
derives: (claimed, delegated) =>
// Can be derived if claimed capability path is contained in the delegated
Expand All @@ -44,7 +44,7 @@ const Add = capability({
new Failure(`Notebook ${claimed.uri} is not included in ${delegaed.uri}`),
})

const ensureTrailingDelimiter = uri => (uri.endsWith("/") ? uri : `${uri}/`)
const ensureTrailingDelimiter = uri => (uri.endsWith('/') ? uri : `${uri}/`)
```

> Please note that library gurantees that both `claimed` and `delegated` capabilty will have `{can: "file/link", with: string, uri: URL, caveats: { link?: CID }}`
Expand All @@ -57,7 +57,7 @@ const ensureTrailingDelimiter = uri => (uri.endsWith("/") ? uri : `${uri}/`)
Now that we have a `file/link` capability we can define a service providing it:

```ts
import { provide, Failure, MalformedCapability } from "@ucanto/server"
import { provide, Failure, MalformedCapability } from '@ucanto/server'

const service = (context: { store: Map<string, string> }) => {
const add = provide(Add, ({ capability, invocation }) => {
Expand Down Expand Up @@ -91,13 +91,13 @@ Library comes with several transport layer codecs you can pick from, but you can
import * as Server from "@ucanto/server"
import * as CAR from "@ucanto/transport/car"
import * as CBOR from "@ucanto/transport/cbor"
import { SigningAuthority } from "@ucanto/authority"
import { SigningPrincipal } from "@ucanto/principal"
import * as HTTP from "node:http"
import * as Buffer from "node:buffer"

export const server = (context { store = new Map() } : { store: Map<string, string> }) =>
Server.create({
id: await SigningAuthority.derive(process.env.SERVICE_SECRET),
id: await SigningPrincipal.derive(process.env.SERVICE_SECRET),
service: service(context),
decoder: CAR,
encoder: CBOR,
Expand Down Expand Up @@ -145,31 +145,31 @@ Client can be used to issue and execute UCAN invocations. Here is an example of
invoking `file/link` capability we've defined earlier

```ts
import * as Client from "@ucanto/client"
import { SigningAuthority, Authority } from "@ucanto/authority"
import { CID } from "multiformats"
import * as Client from '@ucanto/client'
import { SigningPrincipal, Principal } from '@ucanto/principal'
import { CID } from 'multiformats'

// Service will have a well known DID
const service = Authority.parse(process.env.SERVICE_ID)
const service = Principal.parse(process.env.SERVICE_ID)
// Client keypair
const issuer = SigningAuthority.parse(process.env.MY_KEPAIR)
const issuer = SigningPrincipal.parse(process.env.MY_KEPAIR)

const demo1 = async connection => {
const me = await Client.invoke({
issuer: alice,
audience: service,
capability: {
can: "file/link",
can: 'file/link',
with: `file://${issuer.did()}/me/about`,
link: CID.parse(process.env.ME_CID),
},
})

const result = await me.execute(connection)
if (result.error) {
console.error("oops", result)
console.error('oops', result)
} else {
console.log("file got linked", result.link.toString())
console.log('file got linked', result.link.toString())
}
}
```
Expand All @@ -191,9 +191,9 @@ const connection = Client.connect({
In practice you probably would want client/server communication to happen across the wire, or at least across processes. You can bring your own transport channel, or choose an existing one. For example:

```ts
import * as HTTP from "@ucanto/transport/http"
import * as CAR from "@ucanto/transport/car"
import * as CBOR from "@ucanto/transport/cbor"
import * as HTTP from '@ucanto/transport/http'
import * as CAR from '@ucanto/transport/car'
import * as CBOR from '@ucanto/transport/cbor'

const connection = Client.connect({
encoder: Transport.CAR, // encode as CAR because server decodes from car
Expand All @@ -210,22 +210,22 @@ const connection = Client.connect({
The library supports batch invocations and takes care of all the nitty gritty details when it comes to UCAN delegation chains, specifically taking chains apart to encode as blocks in CAR and putting them back together into a chain on the other side. All you need to do is provide a delegation in the proofs:

```ts
import { SigningAuthority, Authority } from "@ucanto/authority"
import * as Client from "@ucanto/client"
import { CID } from "multiformats"
import { SigningPrincipal, Principal } from '@ucanto/principal'
import * as Client from '@ucanto/client'
import { CID } from 'multiformats'

const service = Authority.parse(process.env.SERVICE_DID)
const alice = SigningAuthority.parse(process.env.ALICE_KEYPAIR)
const bob = SigningAuthority.parse(process.env.BOB_KEYPAIR)
const service = Principal.parse(process.env.SERVICE_DID)
const alice = SigningPrincipal.parse(process.env.ALICE_KEYPAIR)
const bob = SigningPrincipal.parse(process.env.BOB_KEYPAIR)

const demo2 = async connection => {
// Alice delegates capability to mutate FS under bob's namespace
const proof = await Client.delegate({
issuer: alice,
audience: bob.authority,
audience: bob.principal,
capabilities: [
{
can: "file/link",
can: 'file/link',
with: `file://${alice.did()}/friends/${bob.did()}/`,
},
],
Expand All @@ -235,7 +235,7 @@ const demo2 = async connection => {
issuer: bob,
audience: service,
capability: {
can: "file/link",
can: 'file/link',
with: `file://${alice.did()}/friends/${bob.did()}/about`,
link: CID.parse(process.env.BOB_CID),
},
Expand All @@ -245,7 +245,7 @@ const demo2 = async connection => {
issuer: bob,
audience: service,
capability: {
can: "file/link",
can: 'file/link',
with: `file://${alice.did()}/friends/${MALLORY_DID}/about`,
link: CID.parse(process.env.MALLORY_CID),
},
Expand All @@ -257,15 +257,15 @@ const demo2 = async connection => {
])

if (bobResult.error) {
console.error("oops", r1)
console.error('oops', r1)
} else {
console.log("about bob is linked", r1)
console.log('about bob is linked', r1)
}

if (malloryResult.error) {
console.log("oops", r2)
console.log('oops', r2)
} else {
console.log("about mallory is linked", r2)
console.log('about mallory is linked', r2)
}
}
```
Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"packages/client",
"packages/server",
"packages/transport",
"packages/authority",
"packages/principal",
"packages/validator"
],
"scripts": {
Expand All @@ -21,14 +21,15 @@
"test:web": "pnpm -r run --if-present test:web"
},
"devDependencies": {
"mocha": "^9.2.2",
"mocha": "^10.0.0",
"prettier": "2.7.1",
"typescript": "4.7.4"
"typescript": "4.8.3"
},
"prettier": {
"trailingComma": "es5",
"tabWidth": 2,
"semi": false,
"singleQuote": true
"singleQuote": true,
"arrowParens": "avoid"
}
}
3 changes: 0 additions & 3 deletions packages/authority/src/lib.js

This file was deleted.

12 changes: 6 additions & 6 deletions packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,24 @@
},
"dependencies": {
"@ucanto/interface": "^0.7.0",
"multiformats": "^9.6.4"
"multiformats": "^9.8.1"
},
"devDependencies": {
"@types/chai": "^4.3.0",
"@types/chai": "^4.3.3",
"@types/chai-subset": "^1.3.3",
"@types/mocha": "^9.1.0",
"@ucanto/authority": "^0.5.0",
"@ucanto/principal": "^0.5.0",
"@ucanto/core": "^0.6.0",
"@ucanto/transport": "^0.7.0",
"@web-std/fetch": "^4.1.0",
"@web-std/file": "^3.0.2",
"c8": "^7.11.0",
"chai": "^4.3.6",
"chai-subset": "^1.6.0",
"mocha": "^9.2.2",
"mocha": "^10.0.0",
"nyc": "^15.1.0",
"playwright-test": "^7.3.0",
"typescript": "^4.7.4"
"playwright-test": "^8.1.1",
"typescript": "^4.8.3"
},
"type": "module",
"main": "src/lib.js",
Expand Down
6 changes: 3 additions & 3 deletions packages/client/src/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import { sha256 } from 'multiformats/hashes/sha2'
/**
* Creates a connection to a service.
*
* @template T
* @template {Record<string, any>} T
* @param {API.ConnectionOptions<T>} options
* @returns {API.ConnectionView<T>}
*/
export const connect = (options) => new Connection(options)

/**
* @template T
* @template {Record<string, any>} T
* @implements {API.ConnectionView<T>}
*/
class Connection {
Expand All @@ -38,7 +38,7 @@ class Connection {

/**
* @template {API.Capability} C
* @template T
* @template {Record<string, any>} T
* @template {API.Tuple<API.ServiceInvocation<C, T>>} I
* @param {API.Connection<T>} connection
* @param {I} invocations
Expand Down
6 changes: 3 additions & 3 deletions packages/client/test/client.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import fetch from '@web-std/fetch'
test('encode inovocation', async () => {
/** @type {Client.ConnectionView<Service.Service>} */
const connection = Client.connect({
id: w3.authority,
id: w3.principal,
channel: HTTP.open({ url: new URL('about:blank'), fetch }),
encoder: CAR,
decoder: CBOR,
Expand Down Expand Up @@ -62,7 +62,7 @@ test('encode delegated invocation', async () => {

/** @type {Client.ConnectionView<Service.Service>} */
const connection = Client.connect({
id: w3.authority,
id: w3.principal,
channel: HTTP.open({ url: new URL('about:blank'), fetch }),
encoder: CAR,
decoder: CBOR,
Expand Down Expand Up @@ -140,7 +140,7 @@ test('encode delegated invocation', async () => {
const service = Service.create()
/** @type {Client.ConnectionView<Service.Service>} */
const connection = Client.connect({
id: w3.authority,
id: w3.principal,
channel: HTTP.open({
url: new URL('about:blank'),
fetch: async (url, input) => {
Expand Down
10 changes: 5 additions & 5 deletions packages/client/test/fixtures.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { SigningAuthority } from '@ucanto/authority'
import { SigningPrincipal } from '@ucanto/principal'

/** did:key:z6Mkqa4oY9Z5Pf5tUcjLHLUsDjKwMC95HGXdE1j22jkbhz6r */
export const alice = SigningAuthority.parse(
export const alice = SigningPrincipal.parse(
'MgCZT5vOnYZoVAeyjnzuJIVY9J4LNtJ+f8Js0cTPuKUpFne0BVEDJjEu6quFIU8yp91/TY/+MYK8GvlKoTDnqOCovCVM='
)
/** did:key:z6MkffDZCkCTWreg8868fG1FGFogcJj5X6PY93pPcWDn9bob */
export const bob = SigningAuthority.parse(
export const bob = SigningPrincipal.parse(
'MgCYbj5AJfVvdrjkjNCxB3iAUwx7RQHVQ7H1sKyHy46Iose0BEevXgL1V73PD9snOCIoONgb+yQ9sycYchQC8kygR4qY='
)
/** did:key:z6MktafZTREjJkvV5mfJxcLpNBoVPwDLhTuMg9ng7dY4zMAL */
export const mallory = SigningAuthority.parse(
export const mallory = SigningPrincipal.parse(
'MgCYtH0AvYxiQwBG6+ZXcwlXywq9tI50G2mCAUJbwrrahkO0B0elFYkl3Ulf3Q3A/EvcVY0utb4etiSE8e6pi4H0FEmU='
)

export const service = SigningAuthority.parse(
export const service = SigningPrincipal.parse(
'MgCYKXoHVy7Vk4/QjcEGi+MCqjntUiasxXJ8uJKY0qh11e+0Bs8WsdqGK7xothgrDzzWD0ME7ynPjz2okXDh8537lId8='
)
Loading

0 comments on commit cd792c9

Please sign in to comment.