license-key-infrastructure
v1.1.1
Published
Reusable RSA-signed license verification for Electron and Node.js apps
Downloads
106
Maintainers
Readme
License Key Infrastructure
Reusable RSA-signed license verification for Electron and Node.js apps. Decouple licensing from your app and use the same infrastructure across multiple projects.
Features
- RSA-SHA256 signature verification
- HWID binding — tie license to a specific machine, or use
hwid: "*"for any machine - Expiry dates — checked against online time (worldtimeapi.org, timeapi.io) to prevent clock-back bypass
- Electron integration — license gate window, IPC handlers, preload script
- CLI tools — generate keys, sign licenses, get machine ID
- TypeScript — full type definitions included
Installation
Choose one of these methods:
# From npm (recommended after publishing)
npm install license-key-infrastructure
# From GitHub
npm install Wisernage/PathofLicense
# Local / monorepo
npm install file:../PathofLicenseFor Electron apps, Electron is a peer dependency (optional — only needed for the /electron module).
Monorepo / Workspaces
If using npm or pnpm workspaces, add to your root package.json:
{
"workspaces": ["PathofLicense", "your-app"]
}Then in your app:
{
"dependencies": {
"license-key-infrastructure": "workspace:*"
}
}Integration Checklist (Electron)
- Install —
npm install license-key-infrastructure - Add public key — Place
license-public.pemin your app (e.g. next tomain.js) - Create gate — Call
createLicenseGateorintegratewith{ app, publicKeyPath, appName } - Register IPC — Call
registerIpcHandlers()(or useintegrate()which does this automatically) - Verify on startup — In
app.whenReady(): verify license, show gate if invalid, quit if user closes - Expose getMachineId (optional) — Add
license:getMachineIdto your main window preload so users can copy their machine ID
Quick Start (Electron)
Option A: One-call integrate()
// main.js
const { app } = require('electron');
const path = require('path');
const { integrate } = require('license-key-infrastructure/electron');
const licenseGate = integrate({
app,
publicKeyPath: path.join(__dirname, 'license-public.pem'),
appName: 'My App',
iconPath: path.join(__dirname, 'icon.ico'),
});
app.whenReady().then(async () => {
if (app.isPackaged) {
let result = await licenseGate.verifyLicense();
while (!result.valid) {
const applied = await licenseGate.showLicenseGate(result);
if (!applied) return app.quit();
result = await licenseGate.verifyLicense();
}
}
// ... create main window, etc.
});Option B: Manual setup with createLicenseGate
// main.js
const { app } = require('electron');
const path = require('path');
const { createLicenseGate } = require('license-key-infrastructure/electron');
const licenseGate = createLicenseGate({
app,
publicKeyPath: path.join(__dirname, 'license-public.pem'),
appName: 'My App',
iconPath: path.join(__dirname, 'icon.ico'),
});
licenseGate.registerIpcHandlers();
app.whenReady().then(async () => {
if (app.isPackaged) {
let result = await licenseGate.verifyLicense();
while (!result.valid) {
const applied = await licenseGate.showLicenseGate(result);
if (!applied) return app.quit();
result = await licenseGate.verifyLicense();
}
}
// ... create main window, etc.
});Preload for main window (optional)
To expose getMachineId to the renderer (e.g. for a "Copy Machine ID" button):
getMachineId: () => ipcRenderer.invoke('license:getMachineId'),The license gate window uses its own preload from the package. No extra setup needed.
Gate UI Customization
Customize the license gate window with optional parameters:
createLicenseGate({
app,
publicKeyPath,
appName: 'My App',
gateTitle: 'Activate Your License', // Override the h2 title
gateStyles: 'h2 { color: #00ff00; }', // Extra CSS
gateHtml: '<html>...</html>', // Full HTML replacement (see below)
});- gateTitle — Override the default
${appName} — License Requiredtitle - gateStyles — Extra CSS appended to the default styles
- gateHtml — Full HTML replacement. Your HTML must use
window.licenseGate.apply(json)andwindow.licenseGate.done(applied). Use{{ERROR_MSG}}and{{TITLE}}as placeholders for dynamic content.
Core API (Node.js, no Electron)
const license = require('license-key-infrastructure');
// Verify license data (object)
const result = await license.verifyLicenseData(licenseData, {
publicKeyPath: '/path/to/license-public.pem',
});
// Verify license file
const result = await license.verifyLicenseFile('/path/to/license.json', {
publicKeyPath: '/path/to/license-public.pem',
});
// Verify and save pasted JSON
const result = await license.saveAndVerifyLicense(
'/path/to/save/license.json',
jsonString,
{ publicKeyPath: '/path/to/license-public.pem' }
);
// Get machine ID
const machineId = license.getMachineId();CLI Tools
Generate keypair (run once)
npx license-generate-keys
# Or with custom paths:
npx license-generate-keys --keys-dir ./my-keys --public-out ./app/license-public.pemSign a license
# License for any machine, expires in 1 month
npx license-sign "*"
# License for specific machine
npx license-sign 79cb54a01f16e0e01c5995a26ec65b7d17561dbd86a68f28c981892877fc93e5
# Custom expiry
npx license-sign "*" 2025-12-31
# No expiry
npx license-sign "*" --no-expiry
# Custom paths
npx license-sign "*" --keys-dir ./my-keys --out ./license.jsonGet machine ID
npx license-get-machine-idLicense File Format
{
"payload": {
"hwid": "*",
"expiry": "2025-12-31"
},
"signature": "base64..."
}hwid: Machine ID or"*"for any machineexpiry: Optional. ISO date (YYYY-MM-DD). Requires internet at startup to verify time.
Integration with Installer (NSIS)
For optional license paste during install, write to %APPDATA%\Your App Name\license.json in your custom NSIS page. The app will validate on launch.
Using in Multiple Projects
- Generate keys once (or per product) — keep the private key secret.
- Bundle
license-public.pemwith each app. - Use
createLicenseGateorintegratewith your app name and paths. - Share the same keypair across apps if you want one license to work for multiple products, or use separate keypairs per product.
Publishing to npm
When ready to publish:
npm login
npm publishConsumers can then use npm install license-key-infrastructure.
