-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathexample-usage.js
More file actions
141 lines (114 loc) · 4.15 KB
/
example-usage.js
File metadata and controls
141 lines (114 loc) · 4.15 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
#!/usr/bin/env node
import { registerAccount } from './hyli-wallet.js';
// Example of using the registration function programmatically
async function exampleUsage() {
console.log("=== Example: Programmatic Account Registration ===\n");
// Configuration
const config = {
username: "testuser",
password: "mypassword123",
inviteCode: "INVITE123",
salt: "customsalt",
enableSessionKey: true
};
console.log("Configuration:", config);
console.log("");
try {
// Register the account
const result = await registerAccount(
config.username,
config.password,
config.inviteCode,
config.salt,
config.enableSessionKey
);
if (result.success) {
console.log("✅ Registration successful!");
console.log("Wallet details:", JSON.stringify(result.wallet, null, 2));
// You can now use the wallet object for other operations
console.log(`\nAccount address: ${result.wallet.address}`);
if (result.wallet.sessionKey) {
console.log(`Session key public: ${result.wallet.sessionKey.publicKey}`);
console.log(`Session key expires: ${new Date(result.wallet.sessionKey.expiration).toISOString()}`);
}
} else {
console.log("❌ Registration failed:", result.error);
}
} catch (error) {
console.error("Error during registration:", error);
}
}
// Example of batch registration
async function batchRegistration() {
console.log("\n=== Example: Batch Registration ===\n");
const users = [
{ username: "alice", password: "alicepass123", inviteCode: "INVITE001" },
{ username: "bob", password: "bobpass123", inviteCode: "INVITE002" },
{ username: "charlie", password: "charliepass123", inviteCode: "INVITE003" }
];
for (const user of users) {
console.log(`Registering user: ${user.username}`);
try {
const result = await registerAccount(
user.username,
user.password,
user.inviteCode,
undefined, // Use random salt
false // No session key
);
if (result.success) {
console.log(`✅ ${user.username} registered successfully`);
} else {
console.log(`❌ ${user.username} failed: ${result.error}`);
}
} catch (error) {
console.log(`❌ ${user.username} error: ${error.message}`);
}
console.log("---");
}
}
// Example of error handling
async function errorHandlingExample() {
console.log("\n=== Example: Error Handling ===\n");
// Test with invalid password
try {
const result = await registerAccount(
"testuser",
"123", // Too short
"INVITE123"
);
console.log("Result:", result);
} catch (error) {
console.log("Caught error:", error.message);
}
// Test with missing parameters
try {
const result = await registerAccount(
"testuser",
"", // Empty password
"INVITE123"
);
console.log("Result:", result);
} catch (error) {
console.log("Caught error:", error.message);
}
}
// Run examples
async function runExamples() {
// Set environment variables for the examples
process.env.NODE_BASE_URL = "http://localhost:4321";
process.env.INDEXER_BASE_URL = "http://localhost:4322";
process.env.WALLET_API_BASE_URL = "http://localhost:4000";
await exampleUsage();
await batchRegistration();
await errorHandlingExample();
console.log("\n=== Examples completed ===");
}
// Run if this file is executed directly
if (import.meta.url === `file://${process.argv[1]}`) {
runExamples().catch(error => {
console.error("Example execution failed:", error);
process.exit(1);
});
}
export { exampleUsage, batchRegistration, errorHandlingExample };