You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
2.5 KiB
63 lines
2.5 KiB
// The recipes of the interoperability of the locator, TypeScript to Go:
|
|
// sealed locators with their envelopes, which this library writes from a
|
|
// seed (seeded.ts). scripts/locator-ts-samples.mjs writes their files,
|
|
// scripts/locator-go-verdicts.go opens them with Go and writes
|
|
// testing/locator-interop.json, and locator.interop.test.ts writes them
|
|
// again and compares them with what Go read.
|
|
|
|
import { newEnvelope, seal } from '../envelope.ts';
|
|
import { type Locator, marshalLocator } from '../locator.ts';
|
|
import { quicknet } from '../profile.ts';
|
|
import { pattern, sha256Hex } from './locator.ts';
|
|
import { seededFill } from './seeded.ts';
|
|
|
|
/** A recipe: a .dkc of dkc_length bytes in an envelope whose locator, with its addresses, is sealed for round. */
|
|
export interface Recipe {
|
|
readonly name: string;
|
|
readonly seed: string;
|
|
readonly round: number;
|
|
readonly dkc_length: number;
|
|
readonly addresses: readonly { readonly uri: string; readonly offset: number }[];
|
|
}
|
|
|
|
/** The recipes, in their order. */
|
|
export function interopRecipes(): Recipe[] {
|
|
const out: Recipe[] = [];
|
|
for (const [round, n, addresses] of [
|
|
[1000, 0, 1],
|
|
[1001, 100, 2],
|
|
[1004, 70000, 8],
|
|
[2000, 5000, 3],
|
|
[1000, 65536, 5],
|
|
[1001, 1 << 20, 1],
|
|
// More than 256 chunks: the counter of the nonce carries.
|
|
[2000, (1 << 24) + 1, 2],
|
|
] as const) {
|
|
out.push({
|
|
name: `a locator of round ${round}, a .dkc of ${n} bytes`,
|
|
seed: `datekeys-ts interop locator ${round} ${n}`,
|
|
round,
|
|
dkc_length: n,
|
|
addresses: Array.from({ length: addresses }, (_, i) =>
|
|
i % 2 === 0 ? { uri: `https://example.com/${round}/${i}`, offset: i * 1000 } : { uri: `ipfs://bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi/${i}`, offset: 0 },
|
|
),
|
|
});
|
|
}
|
|
return out;
|
|
}
|
|
|
|
/** The files of a recipe, by the suffix of their name, and what Go must find in them. */
|
|
export function interopFiles(r: Recipe): Record<string, Uint8Array> {
|
|
const random = seededFill(r.seed);
|
|
const dkc = pattern(r.dkc_length);
|
|
const e = newEnvelope(dkc, random);
|
|
const loc: Locator = { ...e.locator, addresses: r.addresses.map((a) => ({ ...a })) };
|
|
const sealed = seal(quicknet(), r.round, loc, random);
|
|
return { sealed, rest: e.rest, plaintext: marshalLocator(loc), dkc };
|
|
}
|
|
|
|
/** The SHA-256 of each file, as the vectors write them. */
|
|
export function interopDigests(files: Record<string, Uint8Array>): Record<string, string> {
|
|
return Object.fromEntries(Object.entries(files).map(([k, b]) => [k, sha256Hex(b)]));
|
|
}
|