Clavem

Verify a Clavem license token offline in Electron

Import the 32-byte public key as a JWK with node:crypto, verify the signature over "sgt1.<payload>" in the main process, then check v, pid, valid, exp and refresh_by. No dependency is needed, and it works in every Electron version that ships Node.

The token format and the checks are in the offline verification reference, and the Electron guide shows a compact version. This page is a version to ship: it names the reason for each refusal, takes the clock as a parameter so tests need no timers, and guards against a clock set back. The online call that refreshes the token is in license key validation in Electron.

The verifier

Node accepts an Ed25519 public key as a JSON Web Key. In that format the key’s x member is exactly the base64url string from your product page, so there is nothing to convert or wrap:

// main/token.js
import { createHash, createPublicKey, verify } from 'node:crypto';

const isBase64Url = (s) => /^[A-Za-z0-9_-]+$/.test(s);

export class TokenVerifier {
  #key;
  #productId;
  #now;

  /** publicKey: 32 raw bytes, base64url, from your product page. Throws when it is not a valid key. */
  constructor(publicKey, productId, now = () => Math.floor(Date.now() / 1000)) {
    this.#key = createPublicKey({ key: { kty: 'OKP', crv: 'Ed25519', x: publicKey.trim() }, format: 'jwk' });
    this.#productId = productId;
    this.#now = now;
  }

  /** The claims when the signature is good, otherwise a string naming the problem. */
  claims(token) {
    const parts = typeof token === 'string' ? token.split('.') : [];
    if (parts.length !== 3 || parts[0] !== 'sgt1') return 'malformed';
    const [prefix, body, sig] = parts;
    if (!isBase64Url(body) || !isBase64Url(sig)) return 'malformed';
    const signature = Buffer.from(sig, 'base64url');
    if (signature.length !== 64) return 'malformed';
    if (!verify(null, Buffer.from(`${prefix}.${body}`), this.#key, signature)) return 'bad-signature';
    try { return JSON.parse(Buffer.from(body, 'base64url').toString('utf8')); } catch { return 'malformed'; }
  }

  /** 'ok' or the reason. highestIat is the largest iat verified before. */
  check(token, highestIat = 0) {
    const c = this.claims(token);
    if (typeof c === 'string') return c;
    const now = this.#now();
    if (c.v !== 1) return 'malformed';
    if (c.pid !== this.#productId) return 'wrong-product';
    if (c.valid !== true) return 'not-valid';
    if (c.exp !== undefined && c.exp <= now) return 'expired';
    if (now < highestIat) return 'clock-set-back';
    if (c.refresh_by <= now) return 'stale';
    return 'ok';
  }
}

Build it once in app.whenReady(), inside a try. A mistyped key makes the constructor throw ERR_CRYPTO_INVALID_JWK, which is a build mistake you want to see at startup, not as a locked app.

Test results

The tokens below were signed with a throwaway key in the documented sgt1 format, and the verifier held its public half. Output from Node, run without Electron:

good                     ok
withExp                  ok
expiredExp               expired
stale                    stale
notValid                 not-valid
wrongProduct             wrong-product
wrongVersion             malformed
signedByOtherKey         bad-signature
tampered                 bad-signature
twoParts                 malformed
fourParts                malformed
wrongPrefix              malformed
garbage                  malformed
realDemoSignedByClavem   bad-signature
highest iat in the future -> clock-set-back
15 days later -> stale

tampered is a good token whose payload was edited to extend exp, with the old signature. realDemoSignedByClavem is the live demo token: real and decodable, but signed by Clavem’s demo key, so a verifier built from any other public key must refuse it. The same token also verified with crypto.subtle.verify in Node’s Web Crypto, so that path works when you need it, for example in a utility process without Node integration.

Check the key and the clock

kh ties a token to the key the customer typed. It is the first 16 hex characters of the SHA-256 of the normalized key. A Clavem-format key is uppercased with I and L read as 1, O as 0 and U as V. Any other key is lowercased:

export function matchesKey(claims, licenseKey) {
  const t = licenseKey.trim();
  const native = /^[0-9A-Za-z]{5}(?:-[0-9A-Za-z]{5}){4}$/.test(t);
  const normalized = native ? t.toUpperCase().replace(/[IL]/g, '1').replace(/O/g, '0').replace(/U/g, 'V') : t.toLowerCase();
  return createHash('sha256').update(normalized).digest('hex').slice(0, 16) === claims.kh;
}

Against the demo token’s kh, it returned true for the key as is, in lowercase with spaces, and with I and O for 1 and 0, and false for a key that differs in its last character.

For the clock, keep the highest iat you have verified inside the same encrypted blob as the license, so no second file is involved:

const saved = await load();                                  // { key, instanceId, token, highestIat? }
const result = verifier.check(saved.token, saved.highestIat ?? 0);
if (result === 'ok') {
  const { iat } = verifier.claims(saved.token);
  if (iat > (saved.highestIat ?? 0)) await save({ ...saved, highestIat: iat });
}

A user who deletes the license file starts over, and nothing offline can prevent that. The guard stops the casual date change.

Traps in Electron and Node

  • Buffer.from(x, 'base64url') does not throw on bad input. It skips characters it does not know. Buffer.from('AB!!CD', 'base64url') returned 3 bytes without an error. That is why the verifier checks the alphabet and the 64-byte signature length itself.
  • The verifier lives in the main process only. Do not import it into the preload script or the renderer.
  • A token holds the buyer’s email in readable form. Keep it out of logs and crash reports.
  • Create the key object once. createPublicKey on every launch is cheap, but on every IPC call it is not.

Where the check lives

The verifier is JavaScript inside your app archive, and by default anyone can unpack and edit an ASAR file. Electron’s ASAR integrity feature validates the archive’s contents at runtime. Its documentation lists support for macOS from Electron 16 and for Windows from Electron 30. Turn it on, along with the matching fuses, if bypass worries you. It raises the effort. It does not make the check unbreakable.

Doing this with Clavem

Each product has a key pair, and every activate and validate response carries a token signed with the private half. The public key is on the product page and in GET /v1/admin/products. See the API reference for the endpoints.

Questions

Do I need a library to verify Ed25519 in Electron?

No. Node's crypto module imports an Ed25519 public key from a JWK and verifies with crypto.verify and a null algorithm. The base64url public key from your product page is already the JWK x value.

Why not verify in the renderer with Web Crypto?

Web Crypto Ed25519 works in current Electron, but a renderer is the wrong place for a license decision, because page scripts and remote content can reach it. Verify in the main process and send the renderer a status.

Can someone edit the check out of my Electron app?

Yes, if they can edit the app code on their machine. ASAR integrity makes that harder on macOS and Windows, but no offline check is tamper-proof. The signature stops forged tokens, and refresh_by stops old ones.

Sources

  1. Electron, ASAR integrity (checked November 2026)
  2. Electron, safeStorage (checked November 2026)