-
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.
chore: use w3up client final version (#190)
Needs to wire a server to serve a receipt agent message so that we can finish the upload blob flow, given this uses a server also for storing data
- Loading branch information
1 parent
4961f4a
commit 82f3d76
Showing
9 changed files
with
179 additions
and
21 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,61 @@ | ||
import { CarWriter } from '@ipld/car' | ||
import * as CAR from '@ucanto/transport/car' | ||
import { CID } from 'multiformats/cid' | ||
import * as raw from 'multiformats/codecs/raw' | ||
import { sha256 } from 'multiformats/hashes/sha2' | ||
|
||
/** @param {number} size */ | ||
export async function randomBytes(size) { | ||
const bytes = new Uint8Array(size) | ||
while (size) { | ||
const chunk = new Uint8Array(Math.min(size, 65_536)) | ||
if (!globalThis.crypto) { | ||
try { | ||
const { webcrypto } = await import('node:crypto') | ||
webcrypto.getRandomValues(chunk) | ||
} catch (err) { | ||
throw new Error( | ||
'unknown environment - no global crypto and not Node.js', | ||
{ cause: err } | ||
) | ||
} | ||
} else { | ||
crypto.getRandomValues(chunk) | ||
} | ||
size -= chunk.length | ||
bytes.set(chunk, size) | ||
} | ||
return bytes | ||
} | ||
|
||
/** @param {number} size */ | ||
export async function randomCAR(size) { | ||
const bytes = await randomBytes(size) | ||
return toCAR(bytes) | ||
} | ||
|
||
/** @param {Uint8Array} bytes */ | ||
export async function toBlock(bytes) { | ||
const hash = await sha256.digest(bytes) | ||
const cid = CID.createV1(raw.code, hash) | ||
return { cid, bytes } | ||
} | ||
|
||
/** | ||
* @param {Uint8Array} bytes | ||
*/ | ||
export async function toCAR(bytes) { | ||
const block = await toBlock(bytes) | ||
const { writer, out } = CarWriter.create(block.cid) | ||
writer.put(block) | ||
writer.close() | ||
|
||
const chunks = [] | ||
for await (const chunk of out) { | ||
chunks.push(chunk) | ||
} | ||
const blob = new Blob(chunks) | ||
const cid = await CAR.codec.link(new Uint8Array(await blob.arrayBuffer())) | ||
|
||
return Object.assign(blob, { cid, roots: [block.cid] }) | ||
} |
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 | ||
} |