-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
149 lines (123 loc) · 4.28 KB
/
cli.js
File metadata and controls
149 lines (123 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env bare
// cli.js
// Command-line interface for ARONIA (Bare-compatible)
import crypto from 'hypercore-crypto';
import { AroniaNode } from './src/index.js';
const args = process.argv.slice(2);
const command = args[0];
function printUsage() {
console.log(`
ARONIA - Realtime P2P Agent Communication
Usage:
bare cli.js <command> [options]
Commands:
start Start an ARONIA node
identity Show your public key
send <pubkey> <message> Send a message to a peer
peers List connected peers
introduce <to> <target> Introduce a peer to another
trust <pubkey> Auto-accept introductions from peer
help Show this help
Examples:
bare cli.js start --topic my-swarm
bare cli.js send b8e1c4f2... '{"method":"ping"}'
bare cli.js introduce <peer-pubkey> <new-peer-pubkey>
`);
}
async function main() {
switch (command) {
case 'start': {
const topic = getArg('--topic') || 'aronia-default';
const keyPair = crypto.keyPair();
console.log(`
╔════════════════════════════════════════╗
║ ARONIA NODE STARTED ║
╚════════════════════════════════════════╝
Public Key: ${keyPair.publicKey.toString('hex')}
Topic: ${topic}
Your agent is now discoverable on the DHT.
Other agents can connect using your public key.
`);
const node = new AroniaNode({
keyPair,
topic,
whitelist: new Set(), // Start with empty whitelist
});
// Event handlers
node.on('peer:connected', (info) => {
console.log(`\n[+] Peer connected: ${info.pubkey.slice(0, 16)}...`);
console.log(` Agent: ${info.capabilities.agent} v${info.capabilities.version}`);
});
node.on('peer:disconnected', (pubkey) => {
console.log(`\n[-] Peer disconnected: ${pubkey.slice(0, 16)}...`);
});
node.on('peer:rejected', (pubkey, reason) => {
console.log(`\n[!] Peer rejected: ${pubkey.slice(0, 16)}... (${reason})`);
});
node.on('introduction:received', (intro) => {
console.log(`\n[?] Introduction received for: ${intro.alias}`);
console.log(` Pubkey: ${intro.pubkey.slice(0, 16)}...`);
console.log(` From: ${intro.introducerPubkey.slice(0, 16)}...`);
console.log(` Use 'bare cli.js accept-introduction ${intro.pubkey}' to accept`);
});
node.on('introduction:accepted', (pubkey, introducer) => {
console.log(`\n[✓] Introduction accepted: ${pubkey.slice(0, 16)}...`);
console.log(` Introduced by: ${introducer.slice(0, 16)}...`);
});
// Graceful shutdown
process.on('SIGINT', async () => {
console.log('\n\nShutting down...');
await node.stop();
process.exit(0);
});
// Keep running
await new Promise(() => {});
break;
}
case 'identity': {
const keyPair = crypto.keyPair();
console.log('Public Key:', keyPair.publicKey.toString('hex'));
break;
}
case 'send': {
console.log('Send command requires a running node.');
console.log("Use 'bare cli.js start' to start a node first.");
break;
}
case 'peers': {
console.log('Peers command requires a running node.');
console.log("Use 'bare cli.js start' to start a node first.");
break;
}
case 'introduce': {
console.log('Introduce command requires a running node.');
console.log("Use 'bare cli.js start' to start a node first.");
break;
}
case 'trust': {
const pubkey = args[1];
if (!pubkey) {
console.error('Error: Missing pubkey');
console.log('Usage: bare cli.js trust <pubkey>');
process.exit(1);
}
console.log(`Trust configuration for ${pubkey.slice(0, 16)}...`);
console.log('(Requires running node)');
break;
}
case 'help':
default:
printUsage();
}
}
function getArg(name) {
const index = args.indexOf(name);
if (index !== -1 && index + 1 < args.length) {
return args[index + 1];
}
return undefined;
}
main().catch((err) => {
console.error('Error:', err);
process.exit(1);
});