-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: use w3up client final version #190
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
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' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
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 | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uses https://github.com/w3s-project/w3up/blob/695af8b7313a5edc75235cc578d333577d991e92/packages/w3up-client/src/base.js#L31 that is set in test context