Skip to content

Commit

Permalink
fix: add receipt endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Jun 4, 2024
1 parent d2be2a4 commit a7f0990
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 58 deletions.
11 changes: 6 additions & 5 deletions can.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ import {

/**
* @param {string} [blobPath]
* @param {import('@web3-storage/w3up-client/types.js').RequestOptions} [options]
*/
export async function blobAdd(blobPath, options) {
export async function blobAdd(blobPath) {
const client = await getClient()

const spinner = ora('Reading data').start()
Expand All @@ -38,9 +37,11 @@ export async function blobAdd(blobPath, options) {
}

spinner.start('Storing')
const digest = await client.capability.blob.add(blob, options)
const cid = Link.create(raw.code, digest)
spinner.stopAndPersist({ symbol: '⁂', text: `Stored ${base58btc.encode(digest.bytes)} (${cid})` })
const { multihash } = await client.capability.blob.add(blob, {
receiptsEndpoint: client._receiptsEndpoint.toString()
})
const cid = Link.create(raw.code, multihash)
spinner.stopAndPersist({ symbol: '⁂', text: `Stored ${base58btc.encode(multihash.bytes)} (${cid})` })
}

/**
Expand Down
9 changes: 7 additions & 2 deletions test/helpers/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
import { createEnv } from './env.js'
import { Signer } from '@ucanto/principal/ed25519'
import { createServer as createHTTPServer } from './http-server.js'
import { createReceiptsServer } from './receipt-http-server.js'
import http from 'node:http'
import { StoreConf } from '@web3-storage/w3up-client/stores/conf'
import * as FS from 'node:fs/promises'
Expand Down Expand Up @@ -49,6 +50,7 @@ export const provisionSpace = async (context, { space, account, provider }) => {
* @typedef {import('@web3-storage/w3up-client/types').StoreAddSuccess} StoreAddSuccess
* @typedef {UcantoServerTestContext & {
* server: import('./http-server').TestingServer['server']
* receiptsServer: import('./receipt-http-server.js').TestingServer['server']
* router: import('./http-server').Router
* env: { alice: Record<string, string>, bob: Record<string, string> }
* serverURL: URL
Expand All @@ -61,24 +63,26 @@ export const setup = async () => {
const { server, serverURL, router } = await createHTTPServer({
'/': context.connection.channel.request.bind(context.connection.channel),
})
const { server: receiptsServer, serverURL: receiptsServerUrl } = await createReceiptsServer()

return Object.assign(context, {
server,
serverURL,
receiptsServer,
router,
serverRouter: router,
env: {
alice: createEnv({
storeName: `w3cli-test-alice-${context.service.did()}`,
servicePrincipal: context.service,
serviceURL: serverURL,
receiptsEndpoint: new URL('receipt', serverURL),
receiptsEndpoint: new URL('receipt', receiptsServerUrl),
}),
bob: createEnv({
storeName: `w3cli-test-bob-${context.service.did()}`,
servicePrincipal: context.service,
serviceURL: serverURL,
receiptsEndpoint: new URL('receipt', serverURL),
receiptsEndpoint: new URL('receipt', receiptsServerUrl),
}),
},
})
Expand All @@ -90,6 +94,7 @@ export const setup = async () => {
export const teardown = async (context) => {
await cleanupContext(context)
context.server.close()
context.receiptsServer.close()

const stores = [
context.env.alice.W3_STORE_NAME,
Expand Down
52 changes: 1 addition & 51 deletions test/helpers/http-server.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import http from 'http'
import { once } from 'events'

import { parseLink } from '@ucanto/server'
import * as Signer from '@ucanto/principal/ed25519'
import { Receipt, Message } from '@ucanto/core'
import * as CAR from '@ucanto/transport/car'
import { Assert } from '@web3-storage/content-claims/capability'
import { randomCAR } from './random.js'

/**
* @typedef {import('@ucanto/interface').HTTPRequest<any>} HTTPRequest
* @typedef {import('@ucanto/server').HTTPResponse<any>} HTTPResponse
Expand All @@ -30,14 +23,7 @@ export async function createServer(router) {
* @param {http.ServerResponse} response
*/
const listener = async (request, response) => {
if (request.url?.includes('receipt')) {
const taskCid = request.url?.split('/')[1] ?? ''
const body = await generateReceipt(taskCid)
response.writeHead(200)
response.end(body)
return undefined
}

console.log('http server', request?.url)
const chunks = []
for await (const chunk of request) {
chunks.push(chunk)
Expand Down Expand Up @@ -74,39 +60,3 @@ export async function createServer(router) {
serverURL: new URL(`http://127.0.0.1:${server.address().port}`),
}
}

/**
* @param {string} taskCid
*/
const generateReceipt = async (taskCid) => {
const issuer = await Signer.generate()
const content = (await randomCAR(128)).cid
const locationClaim = await Assert.location.delegate({
issuer,
audience: issuer,
with: issuer.toDIDKey(),
nb: {
content,
location: ['http://localhost'],
},
expiration: Infinity,
})

const receipt = await Receipt.issue({
issuer,
fx: {
fork: [locationClaim],
},
ran: parseLink(taskCid),
result: {
ok: {
site: locationClaim.link(),
},
},
})

const message = await Message.build({
receipts: [receipt],
})
return CAR.request.encode(message).body
}
79 changes: 79 additions & 0 deletions test/helpers/receipt-http-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import http from 'http'
import { once } from 'events'

import { parseLink } from '@ucanto/server'
import * as Signer from '@ucanto/principal/ed25519'
import { Receipt, Message } from '@ucanto/core'
import * as CAR from '@ucanto/transport/car'
import { Assert } from '@web3-storage/content-claims/capability'
import { randomCAR } from './random.js'

/**
* @typedef {{
* server: http.Server
* serverURL: URL
* }} TestingServer
*/

/**
* @returns {Promise<TestingServer>}
*/
export async function createReceiptsServer() {
/**
* @param {http.IncomingMessage} request
* @param {http.ServerResponse} response
*/
const listener = async (request, response) => {
const taskCid = request.url?.split('/')[1] ?? ''
const body = await generateReceipt(taskCid)
response.writeHead(200)
response.end(body)
return undefined
}

const server = http.createServer(listener).listen()

await once(server, 'listening')

return {
server,
// @ts-expect-error
serverURL: new URL(`http://127.0.0.1:${server.address().port}`),
}
}

/**
* @param {string} taskCid
*/
const generateReceipt = async (taskCid) => {
const issuer = await Signer.generate()
const content = (await randomCAR(128)).cid
const locationClaim = await Assert.location.delegate({
issuer,
audience: issuer,
with: issuer.toDIDKey(),
nb: {
content,
location: ['http://localhost'],
},
expiration: Infinity,
})

const receipt = await Receipt.issue({
issuer,
fx: {
fork: [locationClaim],
},
ran: parseLink(taskCid),
result: {
ok: {
site: locationClaim.link(),
},
},
})

const message = await Message.build({
receipts: [receipt],
})
return CAR.request.encode(message).body
}

0 comments on commit a7f0990

Please sign in to comment.