Skip to content

Commit dbefb26

Browse files
committed
fix: currency contesxt
1 parent 6d1ee0c commit dbefb26

3 files changed

Lines changed: 91 additions & 44 deletions

File tree

infrastructure/signature-validator/src/index.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,20 @@ import axios from "axios";
22
import * as jose from "jose";
33

44
// Dynamic import for base58btc to handle ESM module in CommonJS context
5+
// Using Function constructor to preserve dynamic import() and avoid TypeScript transformation
56
let base58btcCache: { decode: (input: string) => Uint8Array } | null = null;
67

7-
async function getBase58btc() {
8+
async function getBase58btc(): Promise<{ decode: (input: string) => Uint8Array }> {
89
if (!base58btcCache) {
9-
const base58Module = await import("multiformats/bases/base58");
10-
base58btcCache = base58Module.base58btc;
10+
// Use Function constructor to preserve dynamic import() syntax
11+
// This prevents TypeScript from transforming it to require() in CommonJS output
12+
// The string is evaluated at runtime, preserving the ESM import behavior
13+
const dynamicImport = new Function("specifier", "return import(specifier)") as (specifier: string) => Promise<any>;
14+
const base58Module = await dynamicImport("multiformats/bases/base58");
15+
if (!base58Module?.base58btc) {
16+
throw new Error("Failed to load base58btc from multiformats");
17+
}
18+
base58btcCache = base58Module.base58btc as { decode: (input: string) => Uint8Array };
1119
}
1220
return base58btcCache;
1321
}

platforms/eCurrency/client/src/pages/currency-detail.tsx

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,6 @@ export default function CurrencyDetail() {
3737
return null;
3838
});
3939

40-
// Set default to user account if no context is set and user is available
41-
useEffect(() => {
42-
if (user && !accountContext) {
43-
const defaultContext = { type: "user" as const, id: user.id };
44-
setAccountContext(defaultContext);
45-
localStorage.setItem("ecurrency_account_context", JSON.stringify(defaultContext));
46-
}
47-
}, [user, accountContext]);
48-
49-
// Save account context to localStorage whenever it changes
50-
const handleAccountContextChange = (context: { type: "user" | "group"; id: string } | null) => {
51-
// If null is passed, default to user account
52-
const finalContext = context || (user ? { type: "user" as const, id: user.id } : null);
53-
setAccountContext(finalContext);
54-
if (finalContext) {
55-
localStorage.setItem("ecurrency_account_context", JSON.stringify(finalContext));
56-
} else {
57-
localStorage.removeItem("ecurrency_account_context");
58-
}
59-
};
60-
6140
const currencyId = params?.currencyId;
6241

6342
const { data: currency } = useQuery({
@@ -104,6 +83,51 @@ export default function CurrencyDetail() {
10483
},
10584
});
10685

86+
// Validate account context after user and groups are loaded
87+
useEffect(() => {
88+
if (!user) return;
89+
90+
const adminGroups = groups?.filter((g: any) => g.isAdmin) || [];
91+
92+
// If no context is set, default to user account
93+
if (!accountContext) {
94+
const defaultContext = { type: "user" as const, id: user.id };
95+
setAccountContext(defaultContext);
96+
localStorage.setItem("ecurrency_account_context", JSON.stringify(defaultContext));
97+
return;
98+
}
99+
100+
// Validate the saved context
101+
let isValid = false;
102+
103+
if (accountContext.type === "user") {
104+
// User context must match current user ID
105+
isValid = accountContext.id === user.id;
106+
} else if (accountContext.type === "group") {
107+
// Group context must be in admin groups list
108+
isValid = adminGroups.some((g: any) => g.id === accountContext.id);
109+
}
110+
111+
// If invalid, reset to user account
112+
if (!isValid) {
113+
const defaultContext = { type: "user" as const, id: user.id };
114+
setAccountContext(defaultContext);
115+
localStorage.setItem("ecurrency_account_context", JSON.stringify(defaultContext));
116+
}
117+
}, [user, groups, accountContext]);
118+
119+
// Save account context to localStorage whenever it changes
120+
const handleAccountContextChange = (context: { type: "user" | "group"; id: string } | null) => {
121+
// If null is passed, default to user account
122+
const finalContext = context || (user ? { type: "user" as const, id: user.id } : null);
123+
setAccountContext(finalContext);
124+
if (finalContext) {
125+
localStorage.setItem("ecurrency_account_context", JSON.stringify(finalContext));
126+
} else {
127+
localStorage.removeItem("ecurrency_account_context");
128+
}
129+
};
130+
107131
const { data: totalSupplyData, isLoading: totalSupplyLoading } = useQuery({
108132
queryKey: ["totalSupply", currencyId],
109133
queryFn: async () => {

platforms/eCurrency/client/src/pages/dashboard.tsx

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,46 @@ export default function Dashboard() {
3939
return null;
4040
});
4141

42-
// Set default to user account if no context is set and user is available
42+
const { data: groups } = useQuery({
43+
queryKey: ["userGroups"],
44+
queryFn: async () => {
45+
const response = await apiClient.get("/api/groups/my");
46+
return response.data;
47+
},
48+
});
49+
50+
// Validate account context after user and groups are loaded
4351
useEffect(() => {
44-
if (user && !accountContext) {
52+
if (!user) return;
53+
54+
const adminGroups = groups?.filter((g: any) => g.isAdmin) || [];
55+
56+
// If no context is set, default to user account
57+
if (!accountContext) {
58+
const defaultContext = { type: "user" as const, id: user.id };
59+
setAccountContext(defaultContext);
60+
localStorage.setItem("ecurrency_account_context", JSON.stringify(defaultContext));
61+
return;
62+
}
63+
64+
// Validate the saved context
65+
let isValid = false;
66+
67+
if (accountContext.type === "user") {
68+
// User context must match current user ID
69+
isValid = accountContext.id === user.id;
70+
} else if (accountContext.type === "group") {
71+
// Group context must be in admin groups list
72+
isValid = adminGroups.some((g: any) => g.id === accountContext.id);
73+
}
74+
75+
// If invalid, reset to user account
76+
if (!isValid) {
4577
const defaultContext = { type: "user" as const, id: user.id };
4678
setAccountContext(defaultContext);
4779
localStorage.setItem("ecurrency_account_context", JSON.stringify(defaultContext));
4880
}
49-
}, [user, accountContext]);
81+
}, [user, groups, accountContext]);
5082

5183
// Save account context to localStorage whenever it changes
5284
const handleAccountContextChange = (context: { type: "user" | "group"; id: string } | null) => {
@@ -60,15 +92,6 @@ export default function Dashboard() {
6092
}
6193
};
6294

63-
// Ensure accountContext is set to user account if user loads and context is null
64-
useEffect(() => {
65-
if (user && !accountContext) {
66-
const defaultContext = { type: "user" as const, id: user.id };
67-
setAccountContext(defaultContext);
68-
localStorage.setItem("ecurrency_account_context", JSON.stringify(defaultContext));
69-
}
70-
}, [user, accountContext]);
71-
7295
const { data: balances, isLoading: balancesLoading } = useQuery({
7396
queryKey: ["balances", accountContext],
7497
queryFn: async () => {
@@ -80,14 +103,6 @@ export default function Dashboard() {
80103
},
81104
});
82105

83-
const { data: groups } = useQuery({
84-
queryKey: ["userGroups"],
85-
queryFn: async () => {
86-
const response = await apiClient.get("/api/groups/my");
87-
return response.data;
88-
},
89-
});
90-
91106
const { data: transactions, isLoading: transactionsLoading } = useQuery({
92107
queryKey: ["history", accountContext, transactionOffset],
93108
queryFn: async () => {

0 commit comments

Comments
 (0)