#!/usr/bin/env node// Create a fresh NEAR key pair in a private file. Prints the public key only.// usage: keygen.mjs <file> [account] (account defaults to the implicit id)import { writeFileSync, existsSync, chmodSync } from 'node:fs';import { getPublicKeyAsync, utils } from '../web/vendor/ed25519.js';import { b58encode, concat, hex } from '../web/lib/near.js';const [file, account] = process.argv.slice(2);if (!file) throw new Error('usage: keygen.mjs <file> [account]');if (existsSync(file)) throw new Error('refusing to overwrite an existing key file');const seed = crypto.getRandomValues(new Uint8Array(32));const pub = await getPublicKeyAsync(seed);const record = { account_id: account || hex(pub), public_key: 'ed25519:' + b58encode(pub), private_key: 'ed25519:' + b58encode(concat(seed, pub)),};writeFileSync(file, JSON.stringify(record, null, 2) + '\n', { mode: 0o600 });chmodSync(file, 0o600);console.log(record.account_id, record.public_key);