Skip to content

Commit

Permalink
update access-api ucanto proxy to not need a signer and not sign prox…
Browse files Browse the repository at this point in the history
…yInvocation. using features coming in ucanto 4.2.0
  • Loading branch information
gobengo committed Jan 23, 2023
1 parent ba31138 commit 787d282
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 27 deletions.
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
)
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

0 comments on commit 787d282

Please sign in to comment.