Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update access-api ucanto proxy to not need a signer #390

Merged
merged 2 commits into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions packages/access-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
"license": "(Apache-2.0 OR MIT)",
"dependencies": {
"@ipld/dag-ucan": "^3.2.0",
"@ucanto/core": "^4.1.0",
"@ucanto/interface": "^4.1.0",
"@ucanto/principal": "^4.1.0",
"@ucanto/server": "^4.1.0",
"@ucanto/transport": "^4.1.0",
"@ucanto/core": "^4.2.3",
"@ucanto/interface": "^4.2.3",
"@ucanto/principal": "^4.2.3",
"@ucanto/server": "^4.2.3",
"@ucanto/transport": "^4.2.3",
"@web3-storage/access": "workspace:^",
"@web3-storage/capabilities": "workspace:^",
"@web3-storage/worker-utils": "0.4.3-dev",
Expand All @@ -43,7 +43,7 @@
"@types/mocha": "^10.0.1",
"@types/node": "^18.11.18",
"@types/qrcode": "^1.5.0",
"@ucanto/client": "^4.1.0",
"@ucanto/client": "^4.2.3",
"better-sqlite3": "8.0.1",
"buffer": "^6.0.3",
"dotenv": "^16.0.3",
Expand Down
3 changes: 0 additions & 3 deletions packages/access-api/src/service/upload-api-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { createProxyHandler } from '../ucanto/proxy.js'
* @template {string|number|symbol} M
* @template {Ucanto.ConnectionView<any>} [Connection=Ucanto.ConnectionView<any>]
* @param {object} options
* @param {Ucanto.Signer} options.signer
* @param {Array<M>} options.methods
* @param {{ default: Connection } & Record<Ucanto.UCAN.DID, Connection>} options.connections
*/
Expand Down Expand Up @@ -97,7 +96,6 @@ function getDefaultConnections(options) {
/**
* @template {Ucanto.ConnectionView<any>} [Connection=Ucanto.ConnectionView<any>]
* @param {object} options
* @param {Ucanto.Signer} options.signer
* @param {typeof globalThis.fetch} [options.fetch]
* @param {{ default: Connection, [K: Ucanto.UCAN.DID]: Connection }} [options.connections]
* @param {Record<Ucanto.UCAN.DID, URL>} [options.audienceToUrl]
Expand All @@ -116,7 +114,6 @@ export function createUploadProxy(options) {
/**
* @template {Ucanto.ConnectionView<any>} [Connection=Ucanto.ConnectionView<any>]
* @param {object} options
* @param {Ucanto.Signer} options.signer
* @param {typeof globalThis.fetch} [options.fetch]
* @param {{ default: Connection, [K: Ucanto.UCAN.DID]: Connection }} [options.connections]
* @param {Record<Ucanto.UCAN.DID, URL>} [options.audienceToUrl]
Expand Down
3 changes: 1 addition & 2 deletions packages/access-api/src/ucanto/client-codec.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { Delegation } from '@ucanto/core'
import * as UCAN from '@ipld/dag-ucan'
import { UTF8 } from '@ucanto/transport'

/** @type {import('./types.js').ClientCodec} */
export const clientCodec = {
async encode(invocations, options) {
const headers = new Headers()
const chain = await Delegation.delegate(invocations[0])
const chain = await invocations[0].delegate()

// TODO iterate over proofs and send them too
// for (const ucan of chain.iterate()) {
Expand Down
28 changes: 8 additions & 20 deletions packages/access-api/src/ucanto/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,35 +47,23 @@ function defaultCatchInvocationError(error) {
* @param {object} options
* @param {(error: unknown) => Promise<unknown>} [options.catchInvocationError] - catches any error that comes from invoking the proxy invocation on the connection. If it returns a value, that value will be the proxied invocation result.
* @param {{ default: Connection, [K: Ucanto.UCAN.DID]: Connection }} options.connections
* @param {Ucanto.Signer} options.signer
*/
export function createProxyHandler(options) {
/**
* @template {import('@ucanto/interface').Capability} Capability
* @param {Ucanto.Invocation<Capability>} invocationIn
* @param {Ucanto.Invocation<Capability>} invocation
* @param {Ucanto.InvocationContext} context
* @returns {Promise<Ucanto.Result<any, { error: true }>>}
*/
return async function handleInvocation(invocationIn, context) {
const {
connections,
signer,
catchInvocationError = defaultCatchInvocationError,
} = options
const { audience, capabilities, expiration, notBefore } = invocationIn
const connection = connections[audience.did()] ?? connections.default
const proxyInvocation = Client.invoke({
issuer: signer,
capability: capabilities[0],
audience,
proofs: [invocationIn],
expiration,
notBefore,
})
return async function handleInvocation(invocation, context) {
const { connections, catchInvocationError = defaultCatchInvocationError } =
options
const connection =
connections[invocation.audience.did()] ?? connections.default
try {
const [result] = await Client.execute(
[proxyInvocation],
/** @type {Client.ConnectionView<any>} */ (connection)
[await invocation.delegate()],
connection
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect you to be able to just pass invocation without having to call .delegate()

)
return result
} catch (error) {
Expand Down
2 changes: 0 additions & 2 deletions packages/access-api/test/ucanto-proxy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ describe('ucanto-proxy', () => {
service: {
test: {
succeed: createProxyHandler({
signer: proxyPrincipal,
connections: {
default: Client.connect({
id: upstreamPrincipal,
Expand Down Expand Up @@ -117,7 +116,6 @@ describe('ucanto-proxy', () => {
service: {
test: {
succeed: createProxyHandler({
signer: upstreamPrincipal,
connections: {
default: Client.connect({
id: upstreamPrincipal,
Expand Down
14 changes: 7 additions & 7 deletions packages/access-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@
"@ipld/car": "^5.0.3",
"@ipld/dag-cbor": "^9.0.0",
"@ipld/dag-ucan": "^3.2.0",
"@ucanto/client": "^4.1.0",
"@ucanto/core": "^4.1.0",
"@ucanto/interface": "^4.1.0",
"@ucanto/principal": "^4.1.0",
"@ucanto/transport": "^4.1.0",
"@ucanto/validator": "^4.1.0",
"@ucanto/client": "^4.2.3",
"@ucanto/core": "^4.2.3",
"@ucanto/interface": "^4.2.3",
"@ucanto/principal": "^4.2.3",
"@ucanto/transport": "^4.2.3",
"@ucanto/validator": "^4.2.3",
"@web3-storage/capabilities": "workspace:^",
"bigint-mod-arith": "^3.1.2",
"conf": "10.2.0",
Expand All @@ -91,7 +91,7 @@
"@types/node": "^18.11.18",
"@types/varint": "^6.0.1",
"@types/ws": "^8.5.4",
"@ucanto/server": "^4.1.0",
"@ucanto/server": "^4.2.3",
"assert": "^2.0.0",
"delay": "^5.0.0",
"hd-scripts": "^4.0.0",
Expand Down
10 changes: 5 additions & 5 deletions packages/capabilities/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@
"dist/src/**/*.d.ts.map"
],
"dependencies": {
"@ucanto/core": "^4.1.0",
"@ucanto/interface": "^4.1.0",
"@ucanto/principal": "^4.1.0",
"@ucanto/transport": "^4.1.0",
"@ucanto/validator": "^4.1.0"
"@ucanto/core": "^4.2.3",
"@ucanto/interface": "^4.2.3",
"@ucanto/principal": "^4.2.3",
"@ucanto/transport": "^4.2.3",
"@ucanto/validator": "^4.2.3"
},
"devDependencies": {
"@types/assert": "^1.5.6",
Expand Down
10 changes: 5 additions & 5 deletions packages/upload-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@
"@ipld/car": "^5.0.3",
"@ipld/dag-ucan": "^3.2.0",
"@ipld/unixfs": "^2.0.1",
"@ucanto/client": "^4.1.0",
"@ucanto/interface": "^4.1.0",
"@ucanto/transport": "^4.1.0",
"@ucanto/client": "^4.2.3",
"@ucanto/interface": "^4.2.3",
"@ucanto/transport": "^4.2.3",
"@web3-storage/capabilities": "workspace:^",
"multiformats": "^11.0.1",
"p-queue": "^7.3.0",
Expand All @@ -77,8 +77,8 @@
"devDependencies": {
"@types/assert": "^1.5.6",
"@types/mocha": "^10.0.1",
"@ucanto/principal": "^4.1.0",
"@ucanto/server": "^4.1.0",
"@ucanto/principal": "^4.2.3",
"@ucanto/server": "^4.2.3",
"assert": "^2.0.0",
"blockstore-core": "^3.0.0",
"c8": "^7.12.0",
Expand Down
Loading