Skip to content

Commit

Permalink
api getContext builds w3up client from configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
gobengo committed Mar 21, 2024
1 parent 06b0718 commit f1ff837
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 3 deletions.
8 changes: 8 additions & 0 deletions packages/api/src/bindings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { UserOutput, UserOutputKey } from './utils/db-client-types.js'
import { DBClient } from './utils/db-client.js'
import { LinkdexApi } from './utils/linkdex.js'
import { Logging } from './utils/logs.js'
import { Client as W3upClient } from '@web3-storage/w3up-client'

export type RuntimeEnvironmentName = 'test' | 'dev' | 'staging' | 'production'

Expand Down Expand Up @@ -97,6 +98,12 @@ export interface ServiceConfiguration {
/** Slack webhook url */
SLACK_USER_REQUEST_WEBHOOK_URL: string

/** base64 encoded multiformats ed25519 secretKey */
W3_NFTSTORAGE_PRINCIPAL?: string

/** CID (identity codec) of CAR-encoded UCAN DAG */
W3_NFTSTORAGE_PROOF?: string

/** w3up connection URL (e.g. https://up.web3.storage) */
W3UP_URL?: string
}
Expand Down Expand Up @@ -131,6 +138,7 @@ export interface RouteContext {
r2Uploader: Uploader
ucanService: Service
auth?: Auth
w3up?: W3upClient
W3UP_URL?: string
}

Expand Down
1 change: 1 addition & 0 deletions packages/api/src/routes/nfts-upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export async function nftUpload(event, ctx) {
})
}

console.warn('nftUpload ctx.w3up', ctx.w3up)
if (ctx.W3UP_URL) {
const w3upResponse = await fetch(ctx.W3UP_URL)
}
Expand Down
12 changes: 12 additions & 0 deletions packages/api/src/utils/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Logging } from './logs.js'
import pkg from '../../package.json'
import { Service } from 'ucan-storage/service'
import { LinkdexApi } from './linkdex.js'
import { createW3upClientFromConfig } from './w3up.js'

/**
* Obtains a route context object.
Expand Down Expand Up @@ -66,6 +67,16 @@ export async function getContext(event, params) {
const w3upConfig = {
W3UP_URL: config.W3UP_URL,
}

let w3up
if (config.W3UP_URL) {
w3up = await createW3upClientFromConfig({
url: config.W3UP_URL,
principal: config.W3_NFTSTORAGE_PRINCIPAL ?? '',
proof: config.W3_NFTSTORAGE_PROOF ?? '',
})
}

return {
...w3upConfig,
params,
Expand All @@ -75,5 +86,6 @@ export async function getContext(event, params) {
r2Uploader,
log,
ucanService,
w3up,
}
}
40 changes: 37 additions & 3 deletions packages/api/src/utils/w3up.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import { identity } from 'multiformats/hashes/identity'
import { CarReader, CarWriter } from '@ipld/car'
import { importDAG } from '@ucanto/core/delegation'
import * as ucanto from '@ucanto/core'
import * as W3upClient from '@web3-storage/w3up-client'
import { connect } from '@ucanto/client'
import { CAR, HTTP } from '@ucanto/transport'

/**
* @param {object} env
Expand All @@ -24,7 +27,7 @@ export async function getW3upClient({ principal, proof } = {}) {
}

/**
* @param {string} proof
* @param {string} proof - UCAN delegation encoded as a CID
*/
async function parseW3Proof(proof) {
let cid
Expand All @@ -38,12 +41,11 @@ async function parseW3Proof(proof) {
}
throw err
}

if (cid.multihash.code !== identity.code) {
console.error(
`Error: failed to read proof. Must be identity CID. Fetching of remote proof CARs not supported by this command yet`
)
process.exit(1)
throw new Error('expected cid to have identity code')
}
const delegation = await readProofFromBytes(cid.multihash.digest)
return delegation
Expand Down Expand Up @@ -101,3 +103,35 @@ export async function encodeDelegationAsCid(delegation) {
const cid = CID.createV1(ucanto.CAR.code, identity.digest(bytes))
return cid
}

/**
* @param {object} options
* @param {string} options.url
* @param {string} options.principal
* @param {string} options.proof
*/
export async function createW3upClientFromConfig(options) {
const url = new URL(options.url)
const principal = ed25519.parse(options.principal)
const connection = connect({
id: { did: () => 'did:web:web3.storage' },
codec: CAR.outbound,
channel: HTTP.open({
url,
method: 'POST',
}),
})
const store = new StoreMemory()
const w3up = await W3upClient.create({
principal,
store,
serviceConf: {
upload: connection,
access: connection,
filecoin: connection,
},
receiptsEndpoint: new URL('/receipt/', options.url),
})
await w3up.addSpace(await parseW3Proof(options.proof))
return w3up
}

0 comments on commit f1ff837

Please sign in to comment.