-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d2be2a4
commit a7f0990
Showing
4 changed files
with
93 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |