@private.me/xbind 1.2.17 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +97 -7
- package/dist-standalone/_deps/crypto/base64.js +198 -86
- package/dist-standalone/_deps/shared/cjs/errors.js +232 -539
- package/dist-standalone/_deps/shared/cjs/index.js +88 -442
- package/dist-standalone/_deps/shared/cjs/types.js +61 -374
- package/dist-standalone/_deps/shared/errors.d.ts +7 -1
- package/dist-standalone/_deps/shared/errors.d.ts.map +1 -1
- package/dist-standalone/_deps/shared/errors.js +231 -161
- package/dist-standalone/_deps/shared/errors.js.map +1 -1
- package/dist-standalone/_deps/shared/index.js +54 -55
- package/dist-standalone/_deps/shared/types.js +60 -58
- package/dist-standalone/_deps/ux-helpers/cjs/errors.js +1 -1
- package/dist-standalone/_deps/ux-helpers/cjs/pagination.js +1 -1
- package/dist-standalone/_deps/ux-helpers/cjs/progress.js +1 -1
- package/dist-standalone/_deps/ux-helpers/cjs/search.js +1 -1
- package/dist-standalone/_deps/ux-helpers/errors.js +1 -1
- package/dist-standalone/_deps/ux-helpers/pagination.js +1 -1
- package/dist-standalone/_deps/ux-helpers/progress.js +1 -1
- package/dist-standalone/_deps/ux-helpers/search.js +1 -1
- package/dist-standalone/_deps/xregistry/cjs/discovery.js +1 -1
- package/dist-standalone/_deps/xregistry/cjs/errors.js +1 -1
- package/dist-standalone/_deps/xregistry/cjs/index.js +1 -1
- package/dist-standalone/_deps/xregistry/cjs/registry.js +1 -1
- package/dist-standalone/_deps/xregistry/cjs/schema.js +1 -1
- package/dist-standalone/_deps/xregistry/cjs/types.js +1 -1
- package/dist-standalone/_deps/xregistry/discovery.js +1 -1
- package/dist-standalone/_deps/xregistry/errors.js +1 -1
- package/dist-standalone/_deps/xregistry/index.js +1 -1
- package/dist-standalone/_deps/xregistry/registry.js +1 -1
- package/dist-standalone/_deps/xregistry/schema.js +1 -1
- package/dist-standalone/_deps/xregistry/types.js +1 -1
- package/dist-standalone/cli/setup.d.ts +52 -0
- package/dist-standalone/cli/setup.js +515 -0
- package/dist-standalone/cli/types.d.ts +79 -0
- package/dist-standalone/cli/types.js +27 -0
- package/dist-standalone/cli/xbind.d.ts +20 -0
- package/dist-standalone/cli/xbind.js +149 -0
- package/package.json +2 -2
- package/share1.dat +0 -0
|
@@ -0,0 +1,515 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/* eslint-disable no-console */
|
|
3
|
+
/**
|
|
4
|
+
* @module cli/setup
|
|
5
|
+
* xBind setup command - creates deployment identity.
|
|
6
|
+
*
|
|
7
|
+
* Flow:
|
|
8
|
+
* 1. Check if identity exists (~/.xbind/identity.json)
|
|
9
|
+
* 2. Prompt for service name and email
|
|
10
|
+
* 3. Generate DID (did:key format, Ed25519)
|
|
11
|
+
* 4. Generate DeploymentID (DEP-YYYYMM-XXXXXXXXXX)
|
|
12
|
+
* 5. Create account on server (POST /auth/signup)
|
|
13
|
+
* 6. Send email verification code (6-digit)
|
|
14
|
+
* 7. Prompt user to enter code
|
|
15
|
+
* 8. Verify code on server
|
|
16
|
+
* 9. Store identity locally
|
|
17
|
+
* 10. Display success message
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```bash
|
|
21
|
+
* npx xbind setup --name my-service
|
|
22
|
+
* # → Prompts for email
|
|
23
|
+
* # → Generates DID + DeploymentID
|
|
24
|
+
* # → Creates server account
|
|
25
|
+
* # → Sends verification email
|
|
26
|
+
* # → Prompts for 6-digit code
|
|
27
|
+
* # → Saves to ~/.xbind/identity.json
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
import { parseArgs } from 'node:util';
|
|
31
|
+
import * as fs from 'node:fs';
|
|
32
|
+
import * as path from 'node:path';
|
|
33
|
+
import * as os from 'node:os';
|
|
34
|
+
import * as readline from 'node:readline/promises';
|
|
35
|
+
import { stdin as input, stdout as output } from 'node:process';
|
|
36
|
+
import { randomBytes } from 'node:crypto';
|
|
37
|
+
import { ExitCode, Colors } from './types.js';
|
|
38
|
+
/**
|
|
39
|
+
* Get identity storage path.
|
|
40
|
+
*/
|
|
41
|
+
function getIdentityPath() {
|
|
42
|
+
return path.join(os.homedir(), '.xbind', 'identity.json');
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Ensure .xbind directory exists.
|
|
46
|
+
*/
|
|
47
|
+
async function ensureXBindDir() {
|
|
48
|
+
const xbindDir = path.join(os.homedir(), '.xbind');
|
|
49
|
+
try {
|
|
50
|
+
await fs.promises.mkdir(xbindDir, { recursive: true, mode: 0o700 });
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
const err = error;
|
|
54
|
+
throw new Error(`Cannot create .xbind directory: ${err.message}`);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Check if identity exists.
|
|
59
|
+
*/
|
|
60
|
+
async function identityExists() {
|
|
61
|
+
try {
|
|
62
|
+
await fs.promises.access(getIdentityPath(), fs.constants.F_OK);
|
|
63
|
+
return true;
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Read existing identity.
|
|
71
|
+
*/
|
|
72
|
+
async function readIdentity() {
|
|
73
|
+
try {
|
|
74
|
+
const data = await fs.promises.readFile(getIdentityPath(), 'utf-8');
|
|
75
|
+
return JSON.parse(data);
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Write identity to disk.
|
|
83
|
+
*/
|
|
84
|
+
async function writeIdentity(identity) {
|
|
85
|
+
await ensureXBindDir();
|
|
86
|
+
await fs.promises.writeFile(getIdentityPath(), JSON.stringify(identity, null, 2), { mode: 0o600 });
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Spinner for progress indication.
|
|
90
|
+
*/
|
|
91
|
+
class Spinner {
|
|
92
|
+
frames = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
|
|
93
|
+
currentFrame = 0;
|
|
94
|
+
intervalId;
|
|
95
|
+
message;
|
|
96
|
+
useColors;
|
|
97
|
+
constructor(message, useColors = true) {
|
|
98
|
+
this.message = message;
|
|
99
|
+
this.useColors = useColors && process.stdout.isTTY;
|
|
100
|
+
}
|
|
101
|
+
start() {
|
|
102
|
+
this.intervalId = setInterval(() => {
|
|
103
|
+
const frame = this.frames[this.currentFrame];
|
|
104
|
+
process.stdout.write(`\r${frame} ${this.message}`);
|
|
105
|
+
this.currentFrame = (this.currentFrame + 1) % this.frames.length;
|
|
106
|
+
}, 80);
|
|
107
|
+
}
|
|
108
|
+
succeed(message) {
|
|
109
|
+
this.stop();
|
|
110
|
+
const check = this.useColors ? `${Colors.GREEN}✅${Colors.RESET}` : '✅';
|
|
111
|
+
process.stdout.write(`\r${check} ${message ?? this.message}\n`);
|
|
112
|
+
}
|
|
113
|
+
fail(message) {
|
|
114
|
+
this.stop();
|
|
115
|
+
const cross = this.useColors ? `${Colors.RED}❌${Colors.RESET}` : '❌';
|
|
116
|
+
process.stdout.write(`\r${cross} ${message ?? this.message}\n`);
|
|
117
|
+
}
|
|
118
|
+
stop() {
|
|
119
|
+
if (this.intervalId) {
|
|
120
|
+
clearInterval(this.intervalId);
|
|
121
|
+
this.intervalId = undefined;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Prompt user for input.
|
|
127
|
+
*/
|
|
128
|
+
async function prompt(question, defaultValue) {
|
|
129
|
+
const rl = readline.createInterface({ input, output });
|
|
130
|
+
const answer = await rl.question(defaultValue ? `${question} (${defaultValue}): ` : `${question}: `);
|
|
131
|
+
rl.close();
|
|
132
|
+
return answer.trim() || defaultValue || '';
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Validate email format.
|
|
136
|
+
*/
|
|
137
|
+
function isValidEmail(email) {
|
|
138
|
+
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Validate service name.
|
|
142
|
+
*/
|
|
143
|
+
function isValidServiceName(name) {
|
|
144
|
+
return /^[a-zA-Z][a-zA-Z0-9-]{2,63}$/.test(name);
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Generate DID (did:key format with Ed25519).
|
|
148
|
+
* Uses xBind identity module's generateIdentity.
|
|
149
|
+
*/
|
|
150
|
+
async function generateDID() {
|
|
151
|
+
// Import identity module
|
|
152
|
+
const { generateIdentity } = await import('../identity.js');
|
|
153
|
+
const identityResult = await generateIdentity();
|
|
154
|
+
if (!identityResult.ok) {
|
|
155
|
+
throw new Error('Failed to generate identity');
|
|
156
|
+
}
|
|
157
|
+
const identity = identityResult.value;
|
|
158
|
+
// Export keys to raw bytes then convert to base64 for storage
|
|
159
|
+
const publicKeyRaw = new Uint8Array(await crypto.subtle.exportKey('raw', identity.publicKey));
|
|
160
|
+
const privateKeyJwk = await crypto.subtle.exportKey('jwk', identity.privateKey);
|
|
161
|
+
// For Ed25519, the private key in JWK format contains 'd' which is the 32-byte seed
|
|
162
|
+
if (!privateKeyJwk.d) {
|
|
163
|
+
throw new Error('Failed to export private key');
|
|
164
|
+
}
|
|
165
|
+
const publicKeyBase64 = Buffer.from(publicKeyRaw).toString('base64');
|
|
166
|
+
const privateKeyBase64 = privateKeyJwk.d; // Already base64url encoded
|
|
167
|
+
return {
|
|
168
|
+
did: identity.did,
|
|
169
|
+
publicKey: publicKeyBase64,
|
|
170
|
+
privateKey: privateKeyBase64,
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Generate DeploymentID (DEP-YYYYMM-XXXXXXXXXX format).
|
|
175
|
+
*/
|
|
176
|
+
function generateDeploymentID() {
|
|
177
|
+
const now = new Date();
|
|
178
|
+
const year = now.getUTCFullYear();
|
|
179
|
+
const month = String(now.getUTCMonth() + 1).padStart(2, '0');
|
|
180
|
+
// 10 hex digits for collision resistance (40 bits entropy)
|
|
181
|
+
const randomHex = randomBytes(5).toString('hex').toUpperCase();
|
|
182
|
+
return `DEP-${year}${month}-${randomHex}`;
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Create account on server.
|
|
186
|
+
*/
|
|
187
|
+
async function createServerAccount(did, deploymentId, email, apiUrl) {
|
|
188
|
+
const url = `${apiUrl}/auth/signup`;
|
|
189
|
+
try {
|
|
190
|
+
const response = await fetch(url, {
|
|
191
|
+
method: 'POST',
|
|
192
|
+
headers: { 'Content-Type': 'application/json' },
|
|
193
|
+
body: JSON.stringify({ did, deploymentId, email, method: 'code' }),
|
|
194
|
+
});
|
|
195
|
+
if (!response.ok) {
|
|
196
|
+
const error = await response.json().catch(() => ({
|
|
197
|
+
code: 'UNKNOWN',
|
|
198
|
+
message: `Server returned ${response.status}`,
|
|
199
|
+
}));
|
|
200
|
+
return { success: false, error };
|
|
201
|
+
}
|
|
202
|
+
const data = await response.json();
|
|
203
|
+
return { success: true, account: data.account };
|
|
204
|
+
}
|
|
205
|
+
catch (error) {
|
|
206
|
+
return {
|
|
207
|
+
success: false,
|
|
208
|
+
error: {
|
|
209
|
+
code: 'NETWORK_ERROR',
|
|
210
|
+
message: error instanceof Error ? error.message : 'Network request failed',
|
|
211
|
+
hint: 'Check that the server is running and accessible',
|
|
212
|
+
},
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Send email verification code.
|
|
218
|
+
*/
|
|
219
|
+
async function sendVerificationCode(email, apiUrl) {
|
|
220
|
+
const url = `${apiUrl}/auth/send-verification-code`;
|
|
221
|
+
try {
|
|
222
|
+
const response = await fetch(url, {
|
|
223
|
+
method: 'POST',
|
|
224
|
+
headers: { 'Content-Type': 'application/json' },
|
|
225
|
+
body: JSON.stringify({ email }),
|
|
226
|
+
});
|
|
227
|
+
if (!response.ok) {
|
|
228
|
+
const error = await response.json().catch(() => ({ message: `HTTP ${response.status}` }));
|
|
229
|
+
return { success: false, error: error.message };
|
|
230
|
+
}
|
|
231
|
+
return { success: true };
|
|
232
|
+
}
|
|
233
|
+
catch (error) {
|
|
234
|
+
return {
|
|
235
|
+
success: false,
|
|
236
|
+
error: error instanceof Error ? error.message : 'Network error',
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Verify email code on server.
|
|
242
|
+
*/
|
|
243
|
+
async function verifyEmailCode(email, code, apiUrl) {
|
|
244
|
+
const url = `${apiUrl}/auth/verify-email-code`;
|
|
245
|
+
try {
|
|
246
|
+
const response = await fetch(url, {
|
|
247
|
+
method: 'POST',
|
|
248
|
+
headers: { 'Content-Type': 'application/json' },
|
|
249
|
+
body: JSON.stringify({ email, code }),
|
|
250
|
+
});
|
|
251
|
+
if (!response.ok) {
|
|
252
|
+
const error = await response.json().catch(() => ({ message: `HTTP ${response.status}` }));
|
|
253
|
+
return { success: false, error: error.message };
|
|
254
|
+
}
|
|
255
|
+
return { success: true };
|
|
256
|
+
}
|
|
257
|
+
catch (error) {
|
|
258
|
+
return {
|
|
259
|
+
success: false,
|
|
260
|
+
error: error instanceof Error ? error.message : 'Network error',
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* Output success message (human-readable).
|
|
266
|
+
*/
|
|
267
|
+
function outputSuccess(identity, useColors) {
|
|
268
|
+
const green = useColors ? Colors.GREEN : '';
|
|
269
|
+
const gray = useColors ? Colors.GRAY : '';
|
|
270
|
+
const blue = useColors ? Colors.BLUE : '';
|
|
271
|
+
const reset = useColors ? Colors.RESET : '';
|
|
272
|
+
console.log(`${green}✅ Identity created successfully${reset}\n`);
|
|
273
|
+
console.log(`Service Name: ${identity.name}`);
|
|
274
|
+
console.log(`DID: ${green}${identity.did}${reset}`);
|
|
275
|
+
console.log(`DeploymentID: ${identity.deploymentId}`);
|
|
276
|
+
console.log(`Email: ${identity.email} ${identity.emailVerified ? green + '(verified)' + reset : gray + '(unverified)' + reset}`);
|
|
277
|
+
console.log(`Storage: ${gray}${getIdentityPath()}${reset}\n`);
|
|
278
|
+
console.log(`${blue}ℹ️ Next steps:${reset}`);
|
|
279
|
+
console.log(` 1. Connect to a service: xbind connect <service-name>`);
|
|
280
|
+
console.log(` 2. Generate an invite: xbind invite <service-name>`);
|
|
281
|
+
console.log(` 3. Check connection status: xbind status`);
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Output success message (JSON).
|
|
285
|
+
*/
|
|
286
|
+
function outputSuccessJSON(identity) {
|
|
287
|
+
console.log(JSON.stringify({
|
|
288
|
+
status: 'initialized',
|
|
289
|
+
did: identity.did,
|
|
290
|
+
deploymentId: identity.deploymentId,
|
|
291
|
+
name: identity.name,
|
|
292
|
+
email: identity.email,
|
|
293
|
+
emailVerified: identity.emailVerified,
|
|
294
|
+
storagePath: getIdentityPath(),
|
|
295
|
+
createdAt: identity.createdAt,
|
|
296
|
+
}));
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Output error message.
|
|
300
|
+
*/
|
|
301
|
+
function outputError(message, details, hint, useColors = true) {
|
|
302
|
+
const red = useColors ? Colors.RED : '';
|
|
303
|
+
const gray = useColors ? Colors.GRAY : '';
|
|
304
|
+
const blue = useColors ? Colors.BLUE : '';
|
|
305
|
+
const reset = useColors ? Colors.RESET : '';
|
|
306
|
+
console.error(`${red}❌ Error: ${message}${reset}`);
|
|
307
|
+
if (details) {
|
|
308
|
+
console.error(`\n${details}`);
|
|
309
|
+
}
|
|
310
|
+
if (hint) {
|
|
311
|
+
console.error(`\n${blue}ℹ️ ${hint}${reset}`);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* Main setup command.
|
|
316
|
+
*/
|
|
317
|
+
export async function setupCommand(options = {}) {
|
|
318
|
+
const useColors = !options['no-color'] && process.stdout.isTTY;
|
|
319
|
+
const apiUrl = options['api-url'] || process.env.XBIND_API_URL || 'http://localhost:3001';
|
|
320
|
+
// Step 1: Check if identity exists
|
|
321
|
+
if (!options.force && (await identityExists())) {
|
|
322
|
+
const existing = await readIdentity();
|
|
323
|
+
outputError('Identity already exists', `An xBind identity is already configured for this device.\n\nStorage: ${getIdentityPath()}\nDID: ${existing?.did || 'unknown'}`, 'To create a new identity, use:\n xbind setup --force', useColors);
|
|
324
|
+
process.exit(ExitCode.USER_ERROR);
|
|
325
|
+
}
|
|
326
|
+
// Step 2: Prompt for service name
|
|
327
|
+
let serviceName = options.name;
|
|
328
|
+
if (!serviceName) {
|
|
329
|
+
const defaultName = `xbind-${Date.now()}`;
|
|
330
|
+
serviceName = await prompt('Service name', defaultName);
|
|
331
|
+
}
|
|
332
|
+
if (!isValidServiceName(serviceName)) {
|
|
333
|
+
outputError('Invalid service name', `Service names must:\n • Start with a letter\n • Contain only letters, numbers, hyphens\n • Be 3-64 characters long\n\nExample: billing-service, api-v2, payments-prod`, undefined, useColors);
|
|
334
|
+
process.exit(ExitCode.USER_ERROR);
|
|
335
|
+
}
|
|
336
|
+
// Step 3: Prompt for email
|
|
337
|
+
let email = options.email;
|
|
338
|
+
if (!email) {
|
|
339
|
+
email = await prompt('Email address');
|
|
340
|
+
}
|
|
341
|
+
if (!isValidEmail(email)) {
|
|
342
|
+
outputError('Invalid email format', 'Email must be a valid email address.\n\nExample: user@example.com', undefined, useColors);
|
|
343
|
+
process.exit(ExitCode.USER_ERROR);
|
|
344
|
+
}
|
|
345
|
+
// Step 4: Generate DID
|
|
346
|
+
const spinner1 = new Spinner('Generating DID...', useColors);
|
|
347
|
+
if (!options.json)
|
|
348
|
+
spinner1.start();
|
|
349
|
+
let didInfo;
|
|
350
|
+
try {
|
|
351
|
+
didInfo = await generateDID();
|
|
352
|
+
if (!options.json)
|
|
353
|
+
spinner1.succeed('DID generated');
|
|
354
|
+
}
|
|
355
|
+
catch (error) {
|
|
356
|
+
if (!options.json)
|
|
357
|
+
spinner1.fail('Failed to generate DID');
|
|
358
|
+
outputError('DID generation failed', error instanceof Error ? error.message : 'Unknown error', 'Ensure crypto dependencies are installed', useColors);
|
|
359
|
+
process.exit(ExitCode.SYSTEM_ERROR);
|
|
360
|
+
}
|
|
361
|
+
// Step 5: Generate DeploymentID
|
|
362
|
+
const deploymentId = generateDeploymentID();
|
|
363
|
+
// Step 6: Create server account
|
|
364
|
+
const spinner2 = new Spinner('Creating account on server...', useColors);
|
|
365
|
+
if (!options.json)
|
|
366
|
+
spinner2.start();
|
|
367
|
+
const accountResult = await createServerAccount(didInfo.did, deploymentId, email, apiUrl);
|
|
368
|
+
if (!accountResult.success) {
|
|
369
|
+
if (!options.json)
|
|
370
|
+
spinner2.fail('Failed to create account');
|
|
371
|
+
outputError(accountResult.error?.code || 'Account creation failed', accountResult.error?.message, accountResult.error?.hint || `Check server at ${apiUrl}`, useColors);
|
|
372
|
+
process.exit(ExitCode.SYSTEM_ERROR);
|
|
373
|
+
}
|
|
374
|
+
if (!options.json)
|
|
375
|
+
spinner2.succeed('Account created on server');
|
|
376
|
+
// Step 7: Send verification email
|
|
377
|
+
const spinner3 = new Spinner('Sending verification email...', useColors);
|
|
378
|
+
if (!options.json)
|
|
379
|
+
spinner3.start();
|
|
380
|
+
const sendResult = await sendVerificationCode(email, apiUrl);
|
|
381
|
+
if (!sendResult.success) {
|
|
382
|
+
if (!options.json)
|
|
383
|
+
spinner3.fail('Failed to send verification email');
|
|
384
|
+
outputError('Email verification failed', sendResult.error, 'You can verify later using: xbind verify-email', useColors);
|
|
385
|
+
// Continue anyway - email verification not required for basic setup
|
|
386
|
+
}
|
|
387
|
+
else {
|
|
388
|
+
if (!options.json)
|
|
389
|
+
spinner3.succeed('Verification email sent');
|
|
390
|
+
// Step 8: Prompt for verification code
|
|
391
|
+
if (!options.json) {
|
|
392
|
+
const cyan = useColors ? Colors.CYAN : '';
|
|
393
|
+
const reset = useColors ? Colors.RESET : '';
|
|
394
|
+
console.log(`\n${cyan}→ Check your email for a 6-digit verification code${reset}`);
|
|
395
|
+
}
|
|
396
|
+
const code = await prompt('Enter verification code (or press Enter to skip)');
|
|
397
|
+
if (code) {
|
|
398
|
+
const spinner4 = new Spinner('Verifying code...', useColors);
|
|
399
|
+
if (!options.json)
|
|
400
|
+
spinner4.start();
|
|
401
|
+
const verifyResult = await verifyEmailCode(email, code, apiUrl);
|
|
402
|
+
if (!verifyResult.success) {
|
|
403
|
+
if (!options.json)
|
|
404
|
+
spinner4.fail('Verification failed');
|
|
405
|
+
outputError('Invalid verification code', verifyResult.error, 'You can verify later using: xbind verify-email', useColors);
|
|
406
|
+
// Continue anyway - save identity with emailVerified: false
|
|
407
|
+
}
|
|
408
|
+
else {
|
|
409
|
+
if (!options.json)
|
|
410
|
+
spinner4.succeed('Email verified');
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
// Step 9: Save identity locally
|
|
415
|
+
const identity = {
|
|
416
|
+
did: didInfo.did,
|
|
417
|
+
deploymentId,
|
|
418
|
+
name: serviceName,
|
|
419
|
+
email,
|
|
420
|
+
emailVerified: accountResult.account?.emailVerified || false,
|
|
421
|
+
publicKey: didInfo.publicKey,
|
|
422
|
+
privateKey: didInfo.privateKey,
|
|
423
|
+
createdAt: new Date().toISOString(),
|
|
424
|
+
updatedAt: new Date().toISOString(),
|
|
425
|
+
};
|
|
426
|
+
try {
|
|
427
|
+
await writeIdentity(identity);
|
|
428
|
+
}
|
|
429
|
+
catch (error) {
|
|
430
|
+
outputError('Failed to save identity', error instanceof Error ? error.message : 'Unknown error', `Check permissions on ${getIdentityPath()}`, useColors);
|
|
431
|
+
process.exit(ExitCode.SYSTEM_ERROR);
|
|
432
|
+
}
|
|
433
|
+
// Step 10: Output success
|
|
434
|
+
if (options.json) {
|
|
435
|
+
outputSuccessJSON(identity);
|
|
436
|
+
}
|
|
437
|
+
else {
|
|
438
|
+
console.log(''); // blank line
|
|
439
|
+
outputSuccess(identity, useColors);
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
/**
|
|
443
|
+
* CLI entry point.
|
|
444
|
+
*/
|
|
445
|
+
export async function main(argv = process.argv.slice(2)) {
|
|
446
|
+
const { values } = parseArgs({
|
|
447
|
+
args: argv,
|
|
448
|
+
options: {
|
|
449
|
+
name: { type: 'string', short: 'n' },
|
|
450
|
+
email: { type: 'string', short: 'e' },
|
|
451
|
+
pair: { type: 'boolean', short: 'p' },
|
|
452
|
+
force: { type: 'boolean', short: 'f' },
|
|
453
|
+
'api-url': { type: 'string' },
|
|
454
|
+
json: { type: 'boolean' },
|
|
455
|
+
'no-color': { type: 'boolean' },
|
|
456
|
+
debug: { type: 'boolean' },
|
|
457
|
+
help: { type: 'boolean', short: 'h' },
|
|
458
|
+
},
|
|
459
|
+
});
|
|
460
|
+
if (values.help) {
|
|
461
|
+
console.log(`
|
|
462
|
+
xBind Setup - Create Deployment Identity
|
|
463
|
+
|
|
464
|
+
Usage:
|
|
465
|
+
xbind setup [options]
|
|
466
|
+
|
|
467
|
+
Options:
|
|
468
|
+
-n, --name <string> Service name (default: xbind-<timestamp>)
|
|
469
|
+
-e, --email <email> Email address for account
|
|
470
|
+
-p, --pair Display QR code for mobile pairing
|
|
471
|
+
-f, --force Overwrite existing identity
|
|
472
|
+
--api-url <url> Server API URL (default: http://localhost:3001)
|
|
473
|
+
--json Output JSON only (no human-readable text)
|
|
474
|
+
--no-color Disable colored output
|
|
475
|
+
--debug Show detailed error information
|
|
476
|
+
-h, --help Show this help message
|
|
477
|
+
|
|
478
|
+
Examples:
|
|
479
|
+
xbind setup --name billing-service
|
|
480
|
+
xbind setup --name my-app --email user@example.com
|
|
481
|
+
xbind setup --force
|
|
482
|
+
`.trim());
|
|
483
|
+
return;
|
|
484
|
+
}
|
|
485
|
+
try {
|
|
486
|
+
await setupCommand({
|
|
487
|
+
name: values.name,
|
|
488
|
+
email: values.email,
|
|
489
|
+
pair: values.pair,
|
|
490
|
+
force: values.force,
|
|
491
|
+
'api-url': values['api-url'],
|
|
492
|
+
json: values.json,
|
|
493
|
+
'no-color': values['no-color'],
|
|
494
|
+
debug: values.debug,
|
|
495
|
+
});
|
|
496
|
+
}
|
|
497
|
+
catch (error) {
|
|
498
|
+
const useColors = !values['no-color'] && process.stderr.isTTY;
|
|
499
|
+
const red = useColors ? Colors.RED : '';
|
|
500
|
+
const reset = useColors ? Colors.RESET : '';
|
|
501
|
+
console.error(`${red}❌ Fatal error: ${error instanceof Error ? error.message : String(error)}${reset}`);
|
|
502
|
+
if (values.debug && error instanceof Error && error.stack) {
|
|
503
|
+
console.error(error.stack);
|
|
504
|
+
}
|
|
505
|
+
process.exit(ExitCode.SYSTEM_ERROR);
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
// Auto-run if executed directly
|
|
509
|
+
const isDirectRun = process.argv[1]?.endsWith('setup.ts') || process.argv[1]?.endsWith('setup.js');
|
|
510
|
+
if (isDirectRun) {
|
|
511
|
+
main().catch((error) => {
|
|
512
|
+
console.error('Fatal:', error instanceof Error ? error.message : String(error));
|
|
513
|
+
process.exitCode = ExitCode.SYSTEM_ERROR;
|
|
514
|
+
});
|
|
515
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module cli/types
|
|
3
|
+
* Shared types for xBind CLI commands.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Common CLI options across all commands.
|
|
7
|
+
*/
|
|
8
|
+
export interface GlobalOptions {
|
|
9
|
+
/** Output JSON only (no human-readable text) */
|
|
10
|
+
json?: boolean;
|
|
11
|
+
/** Disable colored output */
|
|
12
|
+
'no-color'?: boolean;
|
|
13
|
+
/** Show detailed error information */
|
|
14
|
+
debug?: boolean;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Local identity stored in ~/.xbind/identity.json
|
|
18
|
+
*/
|
|
19
|
+
export interface LocalIdentity {
|
|
20
|
+
/** DID (did:key format) */
|
|
21
|
+
did: string;
|
|
22
|
+
/** DeploymentID (DEP-YYYYMM-XXXXXXXXXX format) */
|
|
23
|
+
deploymentId: string;
|
|
24
|
+
/** Service/device name */
|
|
25
|
+
name: string;
|
|
26
|
+
/** Email address (verified or unverified) */
|
|
27
|
+
email: string;
|
|
28
|
+
/** Email verification status */
|
|
29
|
+
emailVerified: boolean;
|
|
30
|
+
/** Private key (Ed25519, base64-encoded) */
|
|
31
|
+
privateKey: string;
|
|
32
|
+
/** Public key (Ed25519, base64-encoded) */
|
|
33
|
+
publicKey: string;
|
|
34
|
+
/** Creation timestamp */
|
|
35
|
+
createdAt: string;
|
|
36
|
+
/** Last updated timestamp */
|
|
37
|
+
updatedAt: string;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Server account response from /auth/signup
|
|
41
|
+
*/
|
|
42
|
+
export interface ServerAccountResponse {
|
|
43
|
+
success: boolean;
|
|
44
|
+
account?: {
|
|
45
|
+
did: string;
|
|
46
|
+
deploymentId: string;
|
|
47
|
+
email: string;
|
|
48
|
+
emailVerified: boolean;
|
|
49
|
+
tier: 'free' | 'pro' | 'vip';
|
|
50
|
+
};
|
|
51
|
+
error?: {
|
|
52
|
+
code: string;
|
|
53
|
+
message: string;
|
|
54
|
+
hint?: string;
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Exit codes for CLI commands.
|
|
59
|
+
*/
|
|
60
|
+
export declare const ExitCode: {
|
|
61
|
+
readonly SUCCESS: 0;
|
|
62
|
+
readonly USER_ERROR: 1;
|
|
63
|
+
readonly SYSTEM_ERROR: 2;
|
|
64
|
+
readonly AUTH_ERROR: 3;
|
|
65
|
+
readonly BILLING_ERROR: 4;
|
|
66
|
+
readonly USER_CANCELED: 130;
|
|
67
|
+
};
|
|
68
|
+
/**
|
|
69
|
+
* ANSI color codes (only used if TTY detected).
|
|
70
|
+
*/
|
|
71
|
+
export declare const Colors: {
|
|
72
|
+
readonly RED: "\u001B[31m";
|
|
73
|
+
readonly GREEN: "\u001B[32m";
|
|
74
|
+
readonly YELLOW: "\u001B[33m";
|
|
75
|
+
readonly BLUE: "\u001B[34m";
|
|
76
|
+
readonly CYAN: "\u001B[36m";
|
|
77
|
+
readonly GRAY: "\u001B[90m";
|
|
78
|
+
readonly RESET: "\u001B[0m";
|
|
79
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module cli/types
|
|
3
|
+
* Shared types for xBind CLI commands.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Exit codes for CLI commands.
|
|
7
|
+
*/
|
|
8
|
+
export const ExitCode = {
|
|
9
|
+
SUCCESS: 0,
|
|
10
|
+
USER_ERROR: 1,
|
|
11
|
+
SYSTEM_ERROR: 2,
|
|
12
|
+
AUTH_ERROR: 3,
|
|
13
|
+
BILLING_ERROR: 4,
|
|
14
|
+
USER_CANCELED: 130,
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* ANSI color codes (only used if TTY detected).
|
|
18
|
+
*/
|
|
19
|
+
export const Colors = {
|
|
20
|
+
RED: '\x1b[31m',
|
|
21
|
+
GREEN: '\x1b[32m',
|
|
22
|
+
YELLOW: '\x1b[33m',
|
|
23
|
+
BLUE: '\x1b[34m',
|
|
24
|
+
CYAN: '\x1b[36m',
|
|
25
|
+
GRAY: '\x1b[90m',
|
|
26
|
+
RESET: '\x1b[0m',
|
|
27
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* @module cli/xbind
|
|
4
|
+
* Main xBind CLI router - dispatches to subcommands.
|
|
5
|
+
*
|
|
6
|
+
* Supports 12 commands (v2.0.0 spec):
|
|
7
|
+
* 1. setup - Create deployment identity
|
|
8
|
+
* 2. connect - Connect to a service (TODO)
|
|
9
|
+
* 3. invite - Generate invite link (TODO)
|
|
10
|
+
* 4. list - List connections (TODO)
|
|
11
|
+
* 5. status - Display connection status (TODO)
|
|
12
|
+
* 6. migrate - Scan for API keys (TODO)
|
|
13
|
+
* 7. trial - Activate free trial (TODO)
|
|
14
|
+
* 8. deploy - Deploy algorithms (TODO)
|
|
15
|
+
* 9. server - Start relay server (TODO)
|
|
16
|
+
* 10. revoke - Revoke connection (TODO)
|
|
17
|
+
* 11. backup - Export/import identity (TODO)
|
|
18
|
+
* 12. debug - Diagnostics (TODO)
|
|
19
|
+
*/
|
|
20
|
+
export {};
|