72 lines
1.9 KiB
TypeScript
72 lines
1.9 KiB
TypeScript
/**
|
|
* Shared helpers for the drive-template test suite. Builds canonical
|
|
* sample templates, signs them, and exposes injectable clocks +
|
|
* deterministic Ed25519 keypairs so individual tests stay focused on
|
|
* the requirement they verify.
|
|
*/
|
|
|
|
import {
|
|
getPublicKey,
|
|
randomPrivateKey,
|
|
} from "@suaveplan/crypto-curve/ed25519";
|
|
import { toHex } from "@suaveplan/crypto-utils";
|
|
import { signTemplate } from "../src/codec/sign.js";
|
|
import type {
|
|
DriveTemplate,
|
|
FolderTemplate,
|
|
PerFilePolicy,
|
|
} from "../src/types/template.types.js";
|
|
|
|
export interface SampleKeys {
|
|
readonly secret: Uint8Array;
|
|
readonly publicHex: string;
|
|
}
|
|
|
|
export function makeKeys(): SampleKeys {
|
|
const secret = randomPrivateKey();
|
|
const publicHex = toHex(getPublicKey(secret));
|
|
return { secret, publicHex };
|
|
}
|
|
|
|
export function makeNonce(value = 7): Uint8Array {
|
|
return new Uint8Array(16).fill(value);
|
|
}
|
|
|
|
export const DEFAULT_POLICY: PerFilePolicy = {
|
|
autoDownload: false,
|
|
meshAllowed: true,
|
|
cdnAllowed: true,
|
|
retention: "lru",
|
|
};
|
|
|
|
export function emptyRoot(): FolderTemplate {
|
|
return { name: "/", children: [] };
|
|
}
|
|
|
|
export interface BuildOptions {
|
|
readonly issuer: string;
|
|
readonly nonce?: Uint8Array;
|
|
readonly issuedAt?: number;
|
|
readonly expiresAt?: number;
|
|
readonly rootFolder?: FolderTemplate;
|
|
}
|
|
|
|
export function buildTemplate(
|
|
options: BuildOptions
|
|
): Omit<DriveTemplate, "serverSignature"> {
|
|
return {
|
|
schemaVersion: 1,
|
|
driveId: "drive-1",
|
|
rootFolder: options.rootFolder ?? emptyRoot(),
|
|
policy: { defaultPolicy: DEFAULT_POLICY },
|
|
issuer: options.issuer,
|
|
issuedAt: options.issuedAt ?? Date.now() - 1_000,
|
|
expiresAt: options.expiresAt ?? Date.now() + 24 * 60 * 60 * 1000,
|
|
nonce: options.nonce ?? makeNonce(),
|
|
};
|
|
}
|
|
|
|
export function signed(options: BuildOptions, keys: SampleKeys): DriveTemplate {
|
|
return signTemplate(buildTemplate(options), keys.secret);
|
|
}
|