Repository navigation
Expand file tree
/
Copy pathcomplete-setup.js
More file actions
138 lines (122 loc) Β· 4.73 KB
/
Copy pathcomplete-setup.js
File metadata and controls
138 lines (122 loc) Β· 4.73 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
/**
* Complete profile setup when register-manual.js succeeded but fname registration failed.
* Registers a valid fname (max 16 chars, lowercase alphanumeric + hyphens) and saves credentials.
*/
require('dotenv').config();
const fs = require('fs');
const { registerFname } = require('./farcaster-lib/src');
// Same key parsing as other scripts
let raw = (process.env.CUSTODY_PRIVATE_KEY || process.env.PRIVATE_KEY || '').trim();
let PRIVATE_KEY = null;
if (/^0x[a-fA-F0-9]{64}$/.test(raw)) {
PRIVATE_KEY = raw;
} else if (raw) {
const parts = raw.split(/CUSTODY_PRIVATE_KEY=|PRIVATE_KEY=/i).map((s) => s.trim());
PRIVATE_KEY = parts.find((s) => /^0x[a-fA-F0-9]{64}$/.test(s)) || raw.match(/0x[a-fA-F0-9]{64}/)?.[0];
}
function generateValidFname(archetype) {
const suffix = Math.floor(1000 + Math.random() * 9000).toString();
const name = `${archetype}-${suffix}`;
return name.slice(0, 16);
}
async function completeSetup() {
console.log('π§ Completing profile setup...\n');
if (!PRIVATE_KEY) {
console.error('β CUSTODY_PRIVATE_KEY not found in .env');
process.exit(1);
}
// Load from credentials.json (from register-manual partial save) or use env / defaults
let fid = process.env.FID ? parseInt(process.env.FID, 10) : null;
let signerPrivateKey = process.env.SIGNER_PRIVATE_KEY || null;
let archetype = process.env.ARCHETYPE || 'explorer';
if (fs.existsSync('credentials.json')) {
try {
const creds = JSON.parse(fs.readFileSync('credentials.json', 'utf8'));
if (creds.fid) fid = parseInt(creds.fid, 10);
if (creds.signerPrivateKey) signerPrivateKey = creds.signerPrivateKey;
if (creds.archetype) archetype = creds.archetype;
} catch (e) {
console.warn('Could not load credentials.json:', e.message);
}
}
if (!fid || !signerPrivateKey) {
console.error('β Need FID and SIGNER_PRIVATE_KEY. Set in .env or ensure credentials.json has them.');
process.exit(1);
}
const username = generateValidFname(archetype);
console.log('π Attempting username:', username);
console.log(' Length:', username.length, '(max 16)');
console.log(' Valid:', /^[a-z0-9][a-z0-9-]{0,15}$/.test(username));
try {
console.log('\nπ― Registering username...');
await registerFname({
privateKey: PRIVATE_KEY,
signerPrivateKey,
fid,
fname: username
});
console.log('β
Username registered!');
const credentials = {
fid: fid.toString(),
custodyPrivateKey: PRIVATE_KEY,
signerPrivateKey,
username,
archetype,
createdAt: new Date().toISOString()
};
fs.writeFileSync('credentials.json', JSON.stringify(credentials, null, 2));
const envBlock = `
FID=${fid}
SIGNER_PRIVATE_KEY=${signerPrivateKey}
USERNAME=${username}
ARCHETYPE=${archetype}
HUB_URL=hub-grpc.pinata.cloud
FC_NETWORK=MAINNET
`;
const envPath = '.env';
const envContent = fs.existsSync(envPath) ? fs.readFileSync(envPath, 'utf8') : '';
if (!envContent.includes('USERNAME=') || !envContent.includes(`FID=${fid}`)) {
fs.appendFileSync(envPath, envBlock);
}
console.log('\nπΎ Credentials saved!');
console.log('ββββββββββββββββββββββββββββββββββ');
console.log(`β
Profile: https://warpcast.com/${username}`);
console.log('\nπ Your agent is ready:');
console.log(` node run.js ${archetype}`);
console.log('ββββββββββββββββββββββββββββββββββ\n');
if (fs.existsSync('setup-progress.json')) {
fs.unlinkSync('setup-progress.json');
}
} catch (error) {
if (error.message.includes('already') || error.message.includes('taken') || error.message.includes('claimed')) {
console.log('\nπ‘ Username taken, trying another...');
const newUsername = generateValidFname(archetype);
try {
await registerFname({
privateKey: PRIVATE_KEY,
signerPrivateKey,
fid,
fname: newUsername
});
console.log('β
Username registered:', newUsername);
const credentials = {
fid: fid.toString(),
custodyPrivateKey: PRIVATE_KEY,
signerPrivateKey,
username: newUsername,
archetype,
createdAt: new Date().toISOString()
};
fs.writeFileSync('credentials.json', JSON.stringify(credentials, null, 2));
console.log(`\nβ
Profile: https://warpcast.com/${newUsername}`);
console.log(`\nπ Run: node run.js ${archetype}`);
return;
} catch (retryError) {
console.error('Retry failed:', retryError.message);
}
}
console.error('\nβ Error:', error.message);
process.exit(1);
}
}
completeSetup();