#!/usr/bin/env node
/**
* Script to update the Keet identity to be a real identity
*/
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { parseArgs } from 'node:util';
// Get the directory name of the current module
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Configure logging
function log(level, message) {
const timestamp = new Date().toISOString();
console.log(`[${timestamp}] [${level}] ${message}`);
}
// Parse command-line arguments
function parseArguments() {
const options = {
storage: { type: 'string' },
name: { type: 'string' },
verbose: { type: 'boolean', default: false }
};
const { values } = parseArgs({ options });
return values;
}
// Main function
async function main() {
try {
// Parse command-line arguments
const args = parseArguments();
// Create a storage directory if it doesn't exist
const storagePath = args.storage || path.join(__dirname, '..', 'test-keet-storage');
if (!fs.existsSync(storagePath)) {
fs.mkdirSync(storagePath, { recursive: true });
}
// Path to the identity file
const identityPath = path.join(storagePath, 'identity.json');
// Check if the identity file exists
if (!fs.existsSync(identityPath)) {
log('ERROR', `Identity file not found at: ${identityPath}`);
return 1;
}
// Read the identity file
log('INFO', `Reading identity from: ${identityPath}`);
const identity = JSON.parse(fs.readFileSync(identityPath, 'utf8'));
log('INFO', `Current identity: ${JSON.stringify(identity)}`);
// Update the identity
const updatedIdentity = {
...identity,
anonymous: false // Set to false for a real identity
};
// Update the display name if provided
if (args.name) {
updatedIdentity.displayName = args.name;
}
// Write the updated identity back to the file
log('INFO', `Writing updated identity to: ${identityPath}`);
fs.writeFileSync(identityPath, JSON.stringify(updatedIdentity));
log('INFO', `Updated identity: ${JSON.stringify(updatedIdentity)}`);
log('INFO', 'Identity updated successfully!');
return 0;
} catch (error) {
log('ERROR', `Error updating identity: ${error.message}`);
log('ERROR', error.stack);
return 1;
}
}
// Run the main function if this file is executed directly
if (import.meta.url === `file://${process.argv[1]}`) {
try {
const exitCode = await main();
process.exit(exitCode);
} catch (error) {
log('ERROR', `Unhandled error: ${error.message}`);
log('ERROR', error.stack);
process.exit(1);
}
}