-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcli.js
More file actions
executable file
Β·193 lines (161 loc) Β· 6.38 KB
/
cli.js
File metadata and controls
executable file
Β·193 lines (161 loc) Β· 6.38 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/env node
require('dotenv').config()
const { initializeDatabase } = require('./src/db/database')
const { domainService } = require('./src/services/domains')
const { userService } = require('./src/services/users')
// Parse command line arguments
function parseArgs() {
const args = process.argv.slice(2)
const options = {}
for (let i = 0; i < args.length; i++) {
const arg = args[i]
if (arg === '--help' || arg === '-h') {
showHelp()
process.exit(0)
}
if (arg.startsWith('--')) {
const key = arg.slice(2)
const value = args[i + 1]
if (!value || value.startsWith('--')) {
console.error(`Error: Missing value for --${key}`)
process.exit(1)
}
options[key] = value
i++ // Skip the value in next iteration
} else {
console.error(`Error: Unknown argument: ${arg}`)
console.error('Use --help for usage information')
process.exit(1)
}
}
return options
}
function showHelp() {
console.log(`
UMA User CLI - Add users manually to the system
USAGE:
node cli.js [options]
OPTIONS:
--domain <domain> Domain name (e.g., example.com) [required]
--username <username> Username for the UMA address [required]
--spark-key <key> Spark public key for Lightning payments [required]
--evm-address <address> EVM address for blockchain payments [required]
--display-name <name> Optional display name for the user
--help, -h Show this help message
EXAMPLES:
# Add a user with minimal required fields
node cli.js --domain example.com --username alice --spark-key abc123... --evm-address 0x1234...
# Add a user with display name
node cli.js --domain example.com --username bob --spark-key def456... --evm-address 0x5678... --display-name "Bob Smith"
NOTES:
- The domain will be created automatically if it doesn't exist
- Spark public key is required for Lightning Network payments
- EVM address is required for blockchain settlements (Polygon, Ethereum, etc.)
- Username must be unique within the domain and follow format: lowercase letters, numbers, underscores, hyphens (1-64 chars)
`)
}
async function main() {
try {
const options = parseArgs()
// Validate required arguments
const required = ['domain', 'username', 'spark-key', 'evm-address']
const missing = required.filter(key => !options[key])
if (missing.length > 0) {
console.error('Error: Missing required arguments:', missing.join(', '))
console.error('Use --help for usage information')
process.exit(1)
}
const {
domain: domainName,
username,
'spark-key': sparkPublicKey,
'evm-address': evmAddress,
'display-name': displayName
} = options
console.log('π UMA User CLI')
console.log('ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ')
console.log(`π§ Domain: ${domainName}`)
console.log(`π€ Username: ${username}`)
console.log(`β‘ Spark Key: ${sparkPublicKey.substring(0, 20)}...`)
console.log(`π EVM Address: ${evmAddress}`)
if (displayName) {
console.log(`π Display Name: ${displayName}`)
}
console.log('')
// Initialize database
console.log('π Initializing database...')
await initializeDatabase()
console.log('β
Database initialized')
console.log('')
// Check if domain exists, create if not
console.log(`π Checking domain: ${domainName}`)
let domain = await domainService.getDomainByName(domainName)
if (!domain) {
console.log(`π Creating domain: ${domainName}`)
const domainResult = await domainService.createDomain({
domain: domainName,
ownerEmail: `admin@${domainName}`,
isDefault: false
})
domain = domainResult.domain
console.log(`β
Domain created with ID: ${domain._id}`)
} else {
console.log(`β
Domain exists with ID: ${domain._id}`)
}
console.log('')
// Check if user already exists
console.log(`π Checking if user exists: ${username}@${domainName}`)
const existingUser = await userService.getUserByUsernameAndDomain(username, domain._id)
if (existingUser) {
console.error(`β Error: User "${username}" already exists in domain "${domainName}"`)
process.exit(1)
}
// Create the user
console.log(`π€ Creating user: ${username}@${domainName}`)
const userResult = await userService.createUser({
username,
domainId: domain._id,
displayName: displayName || username,
sparkPublicKey,
addresses: {
polygon: evmAddress, // Use polygon as the primary EVM address
ethereum: evmAddress // Also set ethereum to the same address for now
}
})
// Get the complete user with addresses
const completeUser = await userService.enrichUserWithAddresses(userResult)
console.log('β
User created successfully!')
console.log('')
console.log('π User Details:')
console.log(` ID: ${completeUser._id}`)
console.log(` UMA Address: ${username}@${domainName}`)
console.log(` Display Name: ${completeUser.display_name}`)
console.log(` Spark Public Key: ${completeUser.spark_public_key ? 'β
Set' : 'β Not set'}`)
console.log(` EVM Addresses: ${Object.keys(completeUser.addresses || {}).length} configured`)
console.log(` Created: ${completeUser.created_at}`)
console.log('')
console.log('π User is now ready to receive UMA payments!')
console.log(` Test with: curl "http://localhost:3000/.well-known/lnurlp/${username}"`)
} catch (error) {
console.error('β Error:', error.message)
if (error.message.includes('Invalid username format')) {
console.error('')
console.error('π‘ Username format: lowercase letters, numbers, underscores, and hyphens only (1-64 characters)')
}
process.exit(1)
}
}
// Handle uncaught errors
process.on('uncaughtException', (error) => {
console.error('β Uncaught Exception:', error.message)
process.exit(1)
})
process.on('unhandledRejection', (reason, promise) => {
console.error('β Unhandled Rejection:', reason)
process.exit(1)
})
// Run the CLI
if (require.main === module) {
main()
}
module.exports = { main, parseArgs, showHelp }