Examples

json‑seal can be used anywhere you need a portable, tamper‑evident representation of JSON data. Below are small, focused examples that illustrate common patterns.

Sealing a configuration file

Useful for local apps, CLI tools, or distributed systems that need to ensure configuration integrity.

import { generateKeyPair, signPayload } from "json-seal";

const { publicKey, privateKey } = await generateKeyPair();

const prefs = {
  language: "en-GB",
  timezone: "Europe/London",
  notifications: {
    email: true,
    push: false
  }
};

const sealed = await signPayload(prefs, privateKey, publicKey);
// write `sealed` to disk

Verifying a received document

Any environment with WebCrypto can verify a sealed envelope using the embedded public key.

import { verifyBackup } from "json-seal";

// `sealed` loaded from disk or received over the network
const result = await verifyBackup(sealed);

if (result.valid) {
  console.log("Authentic:", result.payload);
} else {
  console.error("Verification failed");
}

Sealing data before syncing

Ideal for offline‑first apps where data may be stored locally and synced later. Each record carries its own authenticity.

const record = {
  id: "task-123",
  text: "Buy milk",
  completed: false
};

const sealedRecord = await signPayload(record, privateKey, publicKey);
// sync `sealedRecord` to the server

Embedding sealed data in a backup

Backups remain verifiable anywhere they are restored, thanks to the embedded public key.

const backup = {
  timestamp: Date.now(),
  items: [
    await signPayload({ note: "hello" }, privateKey, publicKey),
    await signPayload({ note: "world" }, privateKey, publicKey)
  ]
};

// store `backup` anywhere

Using canonicalization directly

Useful when you need deterministic JSON output for hashing or comparison. json‑seal implements the full RFC 8785 JSON Canonicalization Scheme.

import { canonicalize } from "json-seal";

const text = canonicalize({ b: 2, a: 1 });
// {"a":1,"b":2}

← Back