The offline verification reference has the token format and a short JavaScript version that uses crypto.subtle directly. This page is the extension version. It uses a bundled library, so it does not depend on the customer’s Chrome version, and it reports why a token was refused. The call that fetches the token is in license key validation in a Chrome extension. The code was run in Node against test tokens. It was not loaded into Chrome.
npm install @noble/ed25519
The verifier
verifyAsync computes SHA-512 through crypto.subtle, which a service worker has. It throws on a key or signature of the wrong length, so the code checks both first:
// background/token.js
import * as ed from '@noble/ed25519';
const ALPHABET = /^[A-Za-z0-9_-]+$/;
const b64url = (s) =>
ALPHABET.test(s) ? Uint8Array.from(atob(s.replaceAll('-', '+').replaceAll('_', '/')), (c) => c.charCodeAt(0)) : null;
/** Resolves to the claims object, or to 'malformed' or 'bad-signature'. */
export async function claims(token, publicKey) {
const parts = typeof token === 'string' ? token.split('.') : [];
if (parts.length !== 3 || parts[0] !== 'sgt1') return 'malformed';
const signature = b64url(parts[2]);
const payload = b64url(parts[1]);
const key = b64url(publicKey.trim());
if (!signature || signature.length !== 64 || !payload || !key || key.length !== 32) return 'malformed';
const message = new TextEncoder().encode(`${parts[0]}.${parts[1]}`);
if (!(await ed.verifyAsync(signature, message, key))) return 'bad-signature';
try {
const parsed = JSON.parse(new TextDecoder().decode(payload));
return parsed && typeof parsed === 'object' ? parsed : 'malformed';
} catch {
return 'malformed';
}
}
/** 'ok' or the reason. highestIat is the largest iat verified before. now is in seconds. */
export async function check(token, { publicKey, productId, highestIat = 0, now = Date.now() / 1000 }) {
const c = await claims(token, publicKey);
if (typeof c === 'string') return c;
if (c.v !== 1) return 'malformed';
if (c.pid !== 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';
}
Only 'ok' should turn the paid features on. 'stale' means validate online once. The ALPHABET test matters, because atob is lenient: it skips ASCII whitespace, which would let a token with a line break in it decode.
Bundle it
Manifest V3 requires that all code run by the extension is in the package, so a <script src> to a CDN or an import from a URL is not allowed. Bundle with esbuild, and point the manifest at the output:
esbuild background.js --bundle --format=esm --outfile=dist/background.js
The test below ran the unbundled modules in Node. Run your bundle once in Chrome’s service worker console and verify a token before you publish.
Test results
Tokens signed with a throwaway key in the documented sgt1 format, and a verifier holding its public half. Output from Node 26 with @noble/ed25519 3.2:
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
short public key -> malformed
tampered is a good token whose payload was edited to extend exp, with the old signature. realDemoSignedByClavem is the live demo token. It decodes, but Clavem signed it with its own key, so a verifier with any other public key has to refuse it. noble also offers { zip215: false } to apply RFC 8032’s stricter rules. Signatures from Clavem verify either way, so the stricter option costs nothing.
The clock guard and the token
Keep the highest iat you have verified in chrome.storage.local and pass it as highestIat. Raise it only after a check returned 'ok':
const { highestIat = 0, license } = await chrome.storage.local.get(['highestIat', 'license']);
const result = await check(license.token, { publicKey: PUBLIC_KEY, productId: PRODUCT_ID, highestIat });
if (result === 'ok') {
const { iat } = await claims(license.token, PUBLIC_KEY);
if (iat > highestIat) await chrome.storage.local.set({ highestIat: iat });
}
A user can clear the extension’s storage and start over, and nothing offline can prevent that. The guard stops the casual date change. A token is signed, not secret, so it needs integrity more than hiding. chrome.storage.local is fine. Do not put it in chrome.storage.sync: Chrome’s documentation says that area syncs to every browser the user is signed in to, and the token names one activation. The payload includes the buyer’s email address in readable form, so keep tokens out of console.log and error reports.
Traps in extensions
- The service worker is not a page. There is no
window. UseglobalThis,atobandcrypto.subtle, which a worker has. - The check is client code. Anyone who unpacks the extension can edit it. The signature stops forged tokens and
refresh_bystops old ones. It does not stop a patched extension. - Do not verify in a content script. Page scripts share the page’s environment. Ask the service worker, which holds the key and the token.
- Mind the version. Pin the
@noble/ed25519version inpackage.json. A major release can change the function names.
Doing this with Clavem
Each product has its own key pair. The public half is on the product page and in GET /v1/admin/products, and every activate and validate response carries a token signed with the private half. See the API reference for the endpoints.
Questions
Can I use the browser's WebCrypto for Ed25519 in an extension?
MDN documents Ed25519 for SubtleCrypto verify and importKey, but support depends on the browser version your customers run. A bundled noble-ed25519 works wherever crypto.subtle can compute SHA-512, so you do not need to track browser versions.
Why can't I load the crypto library from a CDN?
Manifest V3 requires all extension code to be packaged with the extension. Remotely hosted code is not allowed, so bundle the library with esbuild or rollup and ship it in the package.
What happens if the verification throws?
noble-ed25519 throws a TypeError when the key or the signature has the wrong length. Check both lengths first, as the code does, so a malformed token returns a reason and does not crash the service worker.