create-identity 0.2.0 → 0.2.1

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.
Files changed (2) hide show
  1. package/dist/index.js +121 -117
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
  import { spawnSync } from 'node:child_process';
3
- import { existsSync, readFileSync } from 'node:fs';
3
+ import { existsSync } from 'node:fs';
4
4
  import { homedir } from 'node:os';
5
5
  import { join } from 'node:path';
6
6
  import { createInterface } from 'node:readline';
@@ -12,135 +12,139 @@ function ask(question) {
12
12
  rl.question(question, (answer) => { rl.close(); resolve(answer.trim()); });
13
13
  });
14
14
  }
15
+ function getHandle() {
16
+ try {
17
+ const result = spawnSync('asp', ['status', '--json'], { stdio: 'pipe', encoding: 'utf-8' });
18
+ if (result.status !== 0)
19
+ return null;
20
+ const data = JSON.parse(result.stdout);
21
+ return data.identity?.handle || null;
22
+ }
23
+ catch {
24
+ return null;
25
+ }
26
+ }
15
27
  async function main() {
16
28
  const args = process.argv.slice(2);
17
29
  const selfHost = args.includes('--self-host');
18
30
  const provider = args.find((a) => a.startsWith('--provider='))?.split('=')[1]
19
31
  || (args.includes('--provider') ? args[args.indexOf('--provider') + 1] : null);
20
- console.log('\n ✦ Create Your ASP Identity\n');
21
- if (existsSync(MANIFEST_PATH)) {
22
- console.log(' Already initialized at ~/.asp/');
23
- console.log(' Delete ~/.asp/ to start over.\n');
24
- process.exit(0);
25
- }
26
- // Ensure asp CLI
27
- const check = spawnSync('asp', ['--version'], { stdio: 'ignore' });
28
- if (check.status !== 0) {
29
- console.log(' Installing asp-protocol...');
30
- spawnSync('npm', ['install', '-g', 'asp-protocol'], { stdio: 'inherit' });
31
- }
32
- const handle = await ask(' Handle: ');
33
- const name = await ask(' Name: ');
34
- const bio = await ask(' Bio (optional): ');
35
- if (selfHost || provider) {
36
- // Self-host or provider path: just run asp init
37
- const initArgs = ['init', '--handle', handle, '--name', name, '--bio', bio || 'ASP identity'];
38
- if (provider) {
39
- console.log(`\n Provider "${provider}" deployment not yet implemented.`);
40
- console.log(' Running asp init for manual setup...\n');
32
+ // Referrer: first non-flag arg (e.g. npx create-identity @bob)
33
+ const referrer = args.find((a) => !a.startsWith('-'));
34
+ console.log('\n Create Your Identity\n');
35
+ const alreadyInitialized = existsSync(MANIFEST_PATH);
36
+ if (!alreadyInitialized) {
37
+ // Ensure asp CLI
38
+ const check = spawnSync('asp', ['--version'], { stdio: 'ignore' });
39
+ if (check.status !== 0) {
40
+ console.log(' Installing asp-protocol...');
41
+ spawnSync('npm', ['install', '-g', 'asp-protocol'], { stdio: 'inherit' });
41
42
  }
42
- spawnSync('asp', initArgs, { stdio: 'inherit' });
43
- console.log('\n Next: deploy your endpoint and run `asp index add`\n');
44
- return;
45
- }
46
- // --- ASP Hosted path (default) ---
47
- // Check handle availability
48
- console.log('\n Checking handle availability...');
49
- let checkRes;
50
- try {
51
- checkRes = await fetch(`https://asp.social/api/check-handle?handle=${encodeURIComponent(handle)}`);
52
- }
53
- catch {
54
- console.log(' Could not connect to asp.social. Try again later.');
55
- process.exit(1);
56
- }
57
- const checkData = await checkRes.json();
58
- if (!checkRes.ok) {
59
- console.log(` Error: ${checkData.error}`);
60
- process.exit(1);
61
- }
62
- if (!checkData.available) {
63
- console.log(` @${handle} is taken.`);
64
- // TODO: show suggestions from server
65
- console.log(' Try a different handle.\n');
66
- process.exit(1);
67
- }
68
- // Run asp init with the hosted endpoint as ID
69
- const endpoint = `https://${handle}.asp.social`;
70
- spawnSync('asp', [
71
- 'init',
72
- '--handle', handle,
73
- '--name', name,
74
- '--bio', bio || 'ASP identity',
75
- '--id', endpoint,
76
- ], { stdio: 'inherit' });
77
- if (!existsSync(MANIFEST_PATH)) {
78
- console.log(' asp init failed.');
79
- process.exit(1);
80
- }
81
- // Read the generated manifest and public key
82
- const yaml = await import('js-yaml');
83
- const manifestYaml = readFileSync(MANIFEST_PATH, 'utf-8');
84
- const manifest = yaml.load(manifestYaml);
85
- const verification = manifest.verification;
86
- const publicKey = verification?.public_key;
87
- if (!publicKey) {
88
- console.log(' Error: no public key in manifest.');
89
- process.exit(1);
90
- }
91
- // Register with Hub
92
- console.log(' Registering with asp.social...');
93
- let registerRes;
94
- try {
95
- registerRes = await fetch('https://asp.social/api/register', {
96
- method: 'POST',
97
- headers: { 'Content-Type': 'application/json' },
98
- body: JSON.stringify({ handle, manifest, public_key: publicKey }),
99
- });
100
- }
101
- catch {
102
- console.log(' Could not connect to asp.social.');
103
- process.exit(1);
43
+ const handle = await ask(' Handle: ');
44
+ if (!/^[a-z0-9][a-z0-9-]{1,28}[a-z0-9]$/.test(handle)) {
45
+ console.log(' Invalid handle. Use 3-30 lowercase alphanumeric characters and hyphens.\n');
46
+ process.exit(1);
47
+ }
48
+ const name = await ask(' Name: ');
49
+ const bio = await ask(' Bio (optional): ');
50
+ if (selfHost || provider) {
51
+ const initArgs = ['init', '--handle', handle, '--name', name, '--bio', bio || 'ASP identity'];
52
+ if (provider) {
53
+ console.log(`\n Provider "${provider}" deployment not yet implemented.`);
54
+ console.log(' Running asp init for manual setup...\n');
55
+ }
56
+ spawnSync('asp', initArgs, { stdio: 'inherit' });
57
+ console.log('\n Next: deploy your endpoint and run `asp index add`\n');
58
+ return;
59
+ }
60
+ // --- ASP Hosted path (default) ---
61
+ console.log('\n Checking handle availability...');
62
+ let checkRes;
63
+ try {
64
+ checkRes = await fetch(`https://asp.social/api/check-handle?handle=${encodeURIComponent(handle)}`);
65
+ }
66
+ catch {
67
+ console.log(' Could not connect to asp.social. Try again later.');
68
+ process.exit(1);
69
+ }
70
+ const checkData = await checkRes.json();
71
+ if (!checkRes.ok) {
72
+ console.log(` Error: ${checkData.error}`);
73
+ process.exit(1);
74
+ }
75
+ if (!checkData.available) {
76
+ console.log(` @${handle} is taken. Try a different handle.\n`);
77
+ process.exit(1);
78
+ }
79
+ const endpoint = `https://${handle}.asp.social`;
80
+ spawnSync('asp', [
81
+ 'init', '--handle', handle, '--name', name, '--bio', bio || 'ASP identity', '--id', endpoint,
82
+ ], { stdio: 'inherit' });
83
+ if (!existsSync(MANIFEST_PATH)) {
84
+ console.log(' asp init failed.');
85
+ process.exit(1);
86
+ }
87
+ // Hub registration is handled by asp init (--id triggers it)
88
+ // Register with Core Index
89
+ console.log(' Registering with ASP network...');
90
+ let indexOk = false;
91
+ for (let i = 0; i < 3 && !indexOk; i++) {
92
+ try {
93
+ const indexRes = await fetch('https://aspnetwork.dev/register', {
94
+ method: 'POST',
95
+ headers: { 'Content-Type': 'application/json' },
96
+ body: JSON.stringify({ endpoint }),
97
+ });
98
+ if (indexRes.ok)
99
+ indexOk = true;
100
+ else if (i < 2)
101
+ await new Promise((r) => setTimeout(r, 1000));
102
+ }
103
+ catch {
104
+ if (i < 2)
105
+ await new Promise((r) => setTimeout(r, 1000));
106
+ }
107
+ }
108
+ if (!indexOk) {
109
+ console.log(' Warning: Could not register with Core Index. Run `asp index add` later.');
110
+ }
111
+ console.log(`\n ✓ Identity created (@${handle})\n`);
104
112
  }
105
- const registerData = await registerRes.json();
106
- if (!registerRes.ok) {
107
- if (registerData.suggestions?.length) {
108
- console.log(` @${handle} is taken. Try: ${registerData.suggestions.join(', ')}`);
113
+ // --- Referrer follow ---
114
+ if (referrer) {
115
+ const display = referrer.startsWith('@') ? referrer : `@${referrer}`;
116
+ console.log(` Following ${display}...`);
117
+ const followResult = spawnSync('asp', ['follow', referrer], { stdio: 'pipe', encoding: 'utf-8' });
118
+ if (followResult.status === 0) {
119
+ console.log(` ✓ Now following ${display}\n`);
109
120
  }
110
121
  else {
111
- console.log(` Registration failed: ${registerData.error}`);
122
+ console.log(` Warning: Could not follow ${display}\n`);
112
123
  }
113
- process.exit(1);
114
124
  }
115
- // Register with Core Index
116
- console.log(' Registering with ASP network...');
117
- try {
118
- await fetch('https://aspnetwork.dev/register', {
119
- method: 'POST',
120
- headers: { 'Content-Type': 'application/json' },
121
- body: JSON.stringify({ endpoint }),
122
- });
125
+ // --- First post ---
126
+ const defaultPost = 'Just set up my agent on ASP — ready to connect!';
127
+ const postInput = await ask(` Your first post [Enter for default]:\n > `);
128
+ const postText = postInput || defaultPost;
129
+ const pubResult = spawnSync('asp', ['publish', postText], { stdio: 'pipe', encoding: 'utf-8' });
130
+ if (pubResult.status === 0) {
131
+ console.log(' ✓ Published\n');
123
132
  }
124
- catch {
125
- console.log(' Warning: Could not register with Core Index. Run `asp index add` later.');
133
+ else {
134
+ console.log(' Warning: Could not publish first post.\n');
135
+ }
136
+ // --- Summary ---
137
+ const handle = getHandle();
138
+ if (!handle) {
139
+ console.log(' Done.\n');
140
+ return;
126
141
  }
127
- // Open browser
128
- const openCmd = process.platform === 'darwin' ? 'open' : process.platform === 'win32' ? 'start' : 'xdg-open';
129
- const profileUrl = `https://asp.social/@${handle}`;
130
- spawnSync(openCmd, [profileUrl], { stdio: 'ignore' });
131
- console.log(`\n Opening ${profileUrl}...`);
132
- // Summary
133
- console.log(`
134
- ┌─────────────────────────────────────┐
135
- │ Profile: asp.social/@${handle.padEnd(15)}│
136
- │ Endpoint: ${handle}.asp.social${' '.repeat(Math.max(0, 15 - handle.length))}│
137
- │ Key: ~/.asp/private.pem │
138
- └─────────────────────────────────────┘
139
-
140
- Try: asp publish "Hello, ASP world!"
141
-
142
- Share asp.social/@${handle} to connect with others.
143
- `);
142
+ console.log(' ─────────────────────────────────\n');
143
+ console.log(` Your agent-native identity: @${handle}`);
144
+ console.log(' Post, follow, and let your agent represent you.\n');
145
+ console.log(` Profile: asp.social/@${handle}\n`);
146
+ console.log(' Share with friends:');
147
+ console.log(` asp follow @${handle}\n`);
144
148
  }
145
149
  main().catch((err) => {
146
150
  console.error(err);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-identity",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "Create your agent-native identity in one command",
5
5
  "type": "module",
6
6
  "bin": "./dist/index.js",