| … | … | @@ -0,0 +1,94 @@ |
| 1 | #!/usr/bin/env node |
| 2 | /** |
| 3 | * Script to update the Keet identity to be a real identity |
| 4 | */ |
| 5 | |
| 6 | import fs from 'fs'; |
| 7 | import path from 'path'; |
| 8 | import { fileURLToPath } from 'url'; |
| 9 | import { parseArgs } from 'node:util'; |
| 10 | |
| 11 | // Get the directory name of the current module |
| 12 | const __filename = fileURLToPath(import.meta.url); |
| 13 | const __dirname = path.dirname(__filename); |
| 14 | |
| 15 | // Configure logging |
| 16 | function log(level, message) { |
| 17 | const timestamp = new Date().toISOString(); |
| 18 | console.log(`[${timestamp}] [${level}] ${message}`); |
| 19 | } |
| 20 | |
| 21 | // Parse command-line arguments |
| 22 | function parseArguments() { |
| 23 | const options = { |
| 24 | storage: { type: 'string' }, |
| 25 | name: { type: 'string' }, |
| 26 | verbose: { type: 'boolean', default: false } |
| 27 | }; |
| 28 | |
| 29 | const { values } = parseArgs({ options }); |
| 30 | return values; |
| 31 | } |
| 32 | |
| 33 | // Main function |
| 34 | async function main() { |
| 35 | try { |
| 36 | // Parse command-line arguments |
| 37 | const args = parseArguments(); |
| 38 | |
| 39 | // Create a storage directory if it doesn't exist |
| 40 | const storagePath = args.storage || path.join(__dirname, '..', 'test-keet-storage'); |
| 41 | if (!fs.existsSync(storagePath)) { |
| 42 | fs.mkdirSync(storagePath, { recursive: true }); |
| 43 | } |
| 44 | |
| 45 | // Path to the identity file |
| 46 | const identityPath = path.join(storagePath, 'identity.json'); |
| 47 | |
| 48 | // Check if the identity file exists |
| 49 | if (!fs.existsSync(identityPath)) { |
| 50 | log('ERROR', `Identity file not found at: ${identityPath}`); |
| 51 | return 1; |
| 52 | } |
| 53 | |
| 54 | // Read the identity file |
| 55 | log('INFO', `Reading identity from: ${identityPath}`); |
| 56 | const identity = JSON.parse(fs.readFileSync(identityPath, 'utf8')); |
| 57 | log('INFO', `Current identity: ${JSON.stringify(identity)}`); |
| 58 | |
| 59 | // Update the identity |
| 60 | const updatedIdentity = { |
| 61 | ...identity, |
| 62 | anonymous: false // Set to false for a real identity |
| 63 | }; |
| 64 | |
| 65 | // Update the display name if provided |
| 66 | if (args.name) { |
| 67 | updatedIdentity.displayName = args.name; |
| 68 | } |
| 69 | |
| 70 | // Write the updated identity back to the file |
| 71 | log('INFO', `Writing updated identity to: ${identityPath}`); |
| 72 | fs.writeFileSync(identityPath, JSON.stringify(updatedIdentity)); |
| 73 | log('INFO', `Updated identity: ${JSON.stringify(updatedIdentity)}`); |
| 74 | |
| 75 | log('INFO', 'Identity updated successfully!'); |
| 76 | return 0; |
| 77 | } catch (error) { |
| 78 | log('ERROR', `Error updating identity: ${error.message}`); |
| 79 | log('ERROR', error.stack); |
| 80 | return 1; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | // Run the main function if this file is executed directly |
| 85 | if (import.meta.url === `file://${process.argv[1]}`) { |
| 86 | try { |
| 87 | const exitCode = await main(); |
| 88 | process.exit(exitCode); |
| 89 | } catch (error) { |
| 90 | log('ERROR', `Unhandled error: ${error.message}`); |
| 91 | log('ERROR', error.stack); |
| 92 | process.exit(1); |
| 93 | } |
| 94 | } |