Skip to content

Commit 27a165b

Browse files
committed
fix: updates
1 parent 60b0998 commit 27a165b

File tree

4 files changed

+57
-27
lines changed

4 files changed

+57
-27
lines changed

packages/auth-server-api/src/config.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,10 @@ function parseSupportedChains(): Chain[] {
114114

115115
// Keep discovering chains until CHAIN_N_ID doesn't exist
116116
while (process.env[`CHAIN_${chainIndex}_ID`]) {
117-
const chainIdStr = process.env[`CHAIN_${chainIndex}_ID`];
117+
const chainIdStr = process.env[`CHAIN_${chainIndex}_ID`]!;
118118
const rpcUrl = process.env[`CHAIN_${chainIndex}_RPC_URL`];
119119
const decimalsStr = process.env[`CHAIN_${chainIndex}_BASE_TOKEN_DECIMALS`];
120120

121-
// Validate required fields
122-
if (!chainIdStr) {
123-
console.error(`CHAIN_${chainIndex}_ID is required but not provided`);
124-
process.exit(1);
125-
}
126121
if (!rpcUrl) {
127122
console.error(`CHAIN_${chainIndex}_RPC_URL is required but not provided`);
128123
process.exit(1);
@@ -205,7 +200,15 @@ const prividiumConfig: PrividiumConfig = {
205200
adminPrivateKey: env.PRIVIDIUM_ADMIN_PRIVATE_KEY || "",
206201
templateKey: env.PRIVIDIUM_TEMPLATE_KEY || "",
207202
ssoAuthServerBaseUrl: env.SSO_AUTH_SERVER_BASE_URL || "",
208-
domain: env.SSO_AUTH_SERVER_BASE_URL ? new URL(env.SSO_AUTH_SERVER_BASE_URL).host : "",
203+
domain: (() => {
204+
if (!env.SSO_AUTH_SERVER_BASE_URL) return "";
205+
try {
206+
return new URL(env.SSO_AUTH_SERVER_BASE_URL).host;
207+
} catch {
208+
console.error(`SSO_AUTH_SERVER_BASE_URL is not a valid URL: ${env.SSO_AUTH_SERVER_BASE_URL}`);
209+
process.exit(1);
210+
}
211+
})(),
209212
};
210213

211214
// Rate limiting configuration

packages/auth-server-api/src/handlers/deploy-account.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ export const deployAccountHandler = async (req: Request, res: Response): Promise
3939
let adminSdk: PrividiumSiweChain | undefined;
4040
if (prividiumConfig.enabled && req.prividiumUser) {
4141
try {
42-
const adminAuth = getAdminAuthService(prividiumConfig, chain);
43-
await adminAuth.initialize();
44-
adminSdk = adminAuth.getSdkInstance();
42+
adminSdk = getAdminAuthService().getSdkInstance();
4543
} catch (error) {
4644
console.error("Admin authentication failed:", error);
4745
res.status(500).json({
@@ -53,7 +51,7 @@ export const deployAccountHandler = async (req: Request, res: Response): Promise
5351

5452
// Create transport - use SDK transport if enabled, otherwise direct RPC
5553
const transport = adminSdk
56-
? adminSdk.transport // SDK transport with auto-auth!
54+
? adminSdk.transport // SDK provides authenticated transport
5755
: http(env.RPC_URL);
5856

5957
// Create clients

packages/auth-server-api/src/index.ts

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,31 @@ import type { Hex } from "viem";
22
import { privateKeyToAccount } from "viem/accounts";
33

44
import { allowlist, app } from "./app.js";
5-
import { env } from "./config.js";
6-
7-
// Start server
8-
const port = parseInt(env.PORT, 10);
9-
app.listen(port, () => {
10-
console.log(`Auth Server API listening on port ${port}`);
11-
console.log(`CORS origins: ${allowlist.join(", ")}`);
12-
console.log(`Deployer address: ${privateKeyToAccount(env.DEPLOYER_PRIVATE_KEY as Hex).address}`);
13-
console.log(`RPC URL: ${env.RPC_URL}`);
5+
import { env, prividiumConfig, SUPPORTED_CHAINS } from "./config.js";
6+
import { initAdminAuthService } from "./services/prividium/admin-auth.js";
7+
8+
async function start() {
9+
// Initialize Prividium admin auth at startup if enabled
10+
if (prividiumConfig.enabled) {
11+
const chain = SUPPORTED_CHAINS[0];
12+
if (!chain) {
13+
console.error("Prividium mode requires at least one configured chain");
14+
process.exit(1);
15+
}
16+
await initAdminAuthService(prividiumConfig, chain);
17+
}
18+
19+
// Start server
20+
const port = parseInt(env.PORT, 10);
21+
app.listen(port, () => {
22+
console.log(`Auth Server API listening on port ${port}`);
23+
console.log(`CORS origins: ${allowlist.join(", ")}`);
24+
console.log(`Deployer address: ${privateKeyToAccount(env.DEPLOYER_PRIVATE_KEY as Hex).address}`);
25+
console.log(`RPC URL: ${env.RPC_URL}`);
26+
});
27+
}
28+
29+
start().catch((error) => {
30+
console.error("Failed to start server:", error);
31+
process.exit(1);
1432
});

packages/auth-server-api/src/services/prividium/admin-auth.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ import type { PrividiumConfig } from "../../config.js";
1010
*/
1111
export class AdminAuthService {
1212
private sdkInstance: PrividiumSiweChain;
13+
readonly chainId: number;
1314

14-
constructor(private config: PrividiumConfig, chain: Chain) {
15+
constructor(config: PrividiumConfig, chain: Chain) {
16+
this.chainId = chain.id;
1517
const account = privateKeyToAccount(config.adminPrivateKey as Hex);
1618

1719
// Initialize SDK - it handles everything
@@ -35,8 +37,7 @@ export class AdminAuthService {
3537
}
3638

3739
/**
38-
* Ensures admin is authenticated.
39-
* Call this once at startup.
40+
* Authenticates admin with Prividium. Call once at startup.
4041
*/
4142
async initialize(): Promise<void> {
4243
if (!this.sdkInstance.isAuthorized()) {
@@ -56,15 +57,25 @@ export class AdminAuthService {
5657
}
5758
}
5859

59-
// Singleton instance
60+
// Singleton instance, initialized at startup via initAdminAuthService()
6061
let adminAuthServiceInstance: AdminAuthService | null = null;
6162

6263
/**
63-
* Gets or creates the singleton AdminAuthService instance.
64+
* Initializes the singleton AdminAuthService and authenticates with Prividium.
65+
* Must be called once at startup before handling requests.
6466
*/
65-
export function getAdminAuthService(config: PrividiumConfig, chain: Chain): AdminAuthService {
67+
export async function initAdminAuthService(config: PrividiumConfig, chain: Chain): Promise<void> {
68+
adminAuthServiceInstance = new AdminAuthService(config, chain);
69+
await adminAuthServiceInstance.initialize();
70+
}
71+
72+
/**
73+
* Gets the initialized AdminAuthService singleton.
74+
* Throws if called before initAdminAuthService().
75+
*/
76+
export function getAdminAuthService(): AdminAuthService {
6677
if (!adminAuthServiceInstance) {
67-
adminAuthServiceInstance = new AdminAuthService(config, chain);
78+
throw new Error("AdminAuthService not initialized. Call initAdminAuthService() at startup first.");
6879
}
6980
return adminAuthServiceInstance;
7081
}

0 commit comments

Comments
 (0)