Skip to content

Commit c813fac

Browse files
committed
Fix: Error handling
1 parent 35c225c commit c813fac

File tree

10 files changed

+98
-54
lines changed

10 files changed

+98
-54
lines changed

front/bun.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"crypto-js": "^4.2.0",
1313
"elliptic": "^6.6.1",
1414
"hyli": "^0.3.0",
15-
"hyli-wallet": "0.5.18",
15+
"hyli-wallet": "0.5.19",
1616
"react": "^19.2.0",
1717
"react-dom": "^19.2.0",
1818
"react-router-dom": "^7.9.3",
@@ -493,7 +493,7 @@
493493

494494
"hyli-noir": ["[email protected]", "", { "dependencies": { "hyli": "^0.4.1", "noir-jwt": "^0.4.5" }, "peerDependencies": { "@aztec/bb.js": "0.87.9", "@noir-lang/noir_js": "1.0.0-beta.11", "@noir-lang/noir_wasm": "1.0.0-beta.11" } }, "sha512-2aAhpbYvaGzxuWS5Jqcl5QY51FcPg4wrPdbWR3GJBYapiw9So/C40lkmEyEVmEXE+F5cDPNSFs67Hvjnb5Camg=="],
495495

496-
"hyli-wallet": ["[email protected].18", "", { "dependencies": { "@noble/hashes": "^2.0.1", "@types/crypto-js": "^4.2.2", "@types/elliptic": "^6.4.18", "@types/react-router-dom": "^5.3.3", "elliptic": "^6.6.1", "hyli": "^0.4.1", "js-sha3": "^0.9.3", "mipd": "^0.0.7" }, "peerDependencies": { "hyli-noir": "^0.3.0", "react": "^19.1.1", "react-dom": "^19.1.1", "react-router-dom": "^7.9.3" } }, "sha512-6V7jtrho6sYWtDYmTmwuAtG2ei2gRhJ+cEWoiT+FjDjSwuQuNxz5YTO165VhM5xio3ADvQ8+STutEnjhE/HApg=="],
496+
"hyli-wallet": ["[email protected].19", "", { "dependencies": { "@noble/hashes": "^2.0.1", "@types/crypto-js": "^4.2.2", "@types/elliptic": "^6.4.18", "@types/react-router-dom": "^5.3.3", "elliptic": "^6.6.1", "hyli": "^0.4.1", "js-sha3": "^0.9.3", "mipd": "^0.0.7" }, "peerDependencies": { "hyli-noir": "^0.3.0", "react": "^19.1.1", "react-dom": "^19.1.1", "react-router-dom": "^7.9.3" } }, "sha512-NlekniVFBxVgizg5Q6Md1mfnk2N/+vYP+s3d7Dg0A6AP8owK8e2XsBSpzWTaJnZWm/YGO5EGV3qCN+J8/K96vA=="],
497497

498498
"idb-keyval": ["[email protected]", "", {}, "sha512-yjD9nARJ/jb1g+CvD0tlhUHOrJ9Sy0P8T9MF3YaLlHnSRpwPfpTX0XIvpmw3gAJUmEu3FiICLBDPXVwyEvrleg=="],
499499

front/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"crypto-js": "^4.2.0",
1919
"elliptic": "^6.6.1",
2020
"hyli": "^0.3.0",
21-
"hyli-wallet": "0.5.18",
21+
"hyli-wallet": "0.5.19",
2222
"react": "^19.2.0",
2323
"react-dom": "^19.2.0",
2424
"react-router-dom": "^7.9.3",

hyli-wallet/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "hyli-wallet",
3-
"version": "0.5.18",
3+
"version": "0.5.19",
44
"type": "module",
55
"license": "MIT",
66
"repository": {

hyli-wallet/src/hooks/useWallet.tsx

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -208,12 +208,57 @@ export const WalletProvider: React.FC<React.PropsWithChildren<WalletProviderProp
208208
(onError ?? internalOnError)?.(error);
209209
throw error;
210210
}
211-
let result = await authProvider.login({
212-
credentials,
213-
onWalletEvent: onWalletEvent ?? internalOnWalletEvent,
214-
onError: onError ?? internalOnError,
215-
registerSessionKey: getRegSessKey(extraParams?.registerSessionKey),
216-
});
211+
const rawUsername = credentials.username?.trim();
212+
const indexer = (() => {
213+
try {
214+
return IndexerService.getInstance();
215+
} catch {
216+
return null;
217+
}
218+
})();
219+
if (rawUsername && indexer) {
220+
const accountsToCheck = new Set<string>([rawUsername]);
221+
if (!rawUsername.includes("@")) {
222+
accountsToCheck.add(`${rawUsername}@${walletContractName}`);
223+
}
224+
let accountExists = false;
225+
for (const accountId of accountsToCheck) {
226+
try {
227+
await indexer.getAccountInfo(accountId);
228+
accountExists = true;
229+
break;
230+
} catch {
231+
// Ignore errors from the indexer; they indicate the account was not found.
232+
}
233+
}
234+
if (!accountExists) {
235+
const error = new Error(`Account with username "${rawUsername}" does not exist.`);
236+
(onError ?? internalOnError)?.(error);
237+
return undefined;
238+
}
239+
}
240+
241+
let result: AuthResult;
242+
try {
243+
result = await authProvider.login({
244+
credentials,
245+
onWalletEvent: onWalletEvent ?? internalOnWalletEvent,
246+
onError: onError ?? internalOnError,
247+
registerSessionKey: getRegSessKey(extraParams?.registerSessionKey),
248+
});
249+
} catch (error) {
250+
(onError ?? internalOnError)?.(error as Error);
251+
result = {
252+
success: false,
253+
error: "Unknown error",
254+
};
255+
}
256+
if (!result.success) {
257+
const error = new Error(result.error || "Registration failed");
258+
(onError ?? internalOnError)?.(error);
259+
return undefined;
260+
}
261+
217262
setWallet(result.wallet ?? null);
218263
if (result.wallet)
219264
(onWalletEvent ?? internalOnWalletEvent)?.({

hyli-wallet/src/providers/EthereumWalletAuthProvider.ts

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
initializeEthereumProviders,
2525
} from "./ethereumProviders";
2626
import { EIP1193Provider } from "mipd";
27+
import { createPath } from "react-router-dom";
2728

2829
export interface EthereumWalletAuthCredentials extends AuthCredentials {
2930
inviteCode?: string;
@@ -240,11 +241,17 @@ export class EthereumWalletAuthProvider implements AuthProvider<EthereumWalletAu
240241
indexerService: IndexerService,
241242
onError?: WalletErrorCallback
242243
) {
243-
const accountInfo = await indexerService.getAccountInfo(username);
244-
if (accountInfo) {
245-
const error = `Account with username "${username}" already exists.`;
246-
onError?.(new Error(error));
247-
throw new Error(error);
244+
try {
245+
const accountInfo = await indexerService.getAccountInfo(username);
246+
if (accountInfo) {
247+
const error = `Account with username "${username}" already exists.`;
248+
onError?.(new Error(error));
249+
throw new Error(error);
250+
}
251+
} catch (error: any) {
252+
// Any failure from the indexer means the account is not registered yet.
253+
// We ignore 404 and network errors to allow registration to proceed.
254+
return;
248255
}
249256

250257
if (!inviteCode) {
@@ -266,12 +273,6 @@ export class EthereumWalletAuthProvider implements AuthProvider<EthereumWalletAu
266273

267274
const identity = `${username}@${walletContractName}`;
268275

269-
onWalletEvent?.({
270-
account: identity,
271-
type: "checking_password",
272-
message: "Requesting Ethereum wallet signature…",
273-
});
274-
275276
const indexerService = IndexerService.getInstance();
276277
const accountInfo = await indexerService.getAccountInfo(username);
277278
if (!("Ethereum" in accountInfo.auth_method)) {
@@ -281,30 +282,28 @@ export class EthereumWalletAuthProvider implements AuthProvider<EthereumWalletAu
281282
const storedAddress = this.normalizeEthereumAddress(
282283
`${accountInfo.auth_method.Ethereum.address ?? ""}`,
283284
);
285+
286+
const ethereum = this.getEthereum(credentials.providerId);
287+
const ethAddr = await this.getPrimaryAccount(ethereum);
288+
289+
const walletAddress = this.normalizeEthereumAddress(ethAddr[0]);
290+
291+
if (walletAddress !== storedAddress) {
292+
// onError?.(new Error("Ethereum account does not match registered wallet address"));
293+
return { success: false, error: "Ethereum account does not match registered wallet address" };
294+
}
295+
284296
const nonce = Date.now();
285297
const message = this.buildSigningMessage(identity, nonce);
286298

287-
let ethAddr: string[];
299+
// let ethAddr: string[];
288300
let signature: string;
289301
try {
290302
const result = await this.signWithEthereumWallet(message, credentials.providerId);
291-
ethAddr = result.ethAddr;
292303
signature = result.signature;
293304
} catch (error: any) {
294-
// Handle wallet specific errors
295-
if (error.code === 4001) {
296-
return { success: false, error: "Wallet signature request was rejected by user" };
297-
}
298-
if (error.message.includes("User rejected") || error.message.includes("User denied")) {
299-
return { success: false, error: "Wallet signature request was rejected by user" };
300-
}
301-
throw error; // Re-throw other errors
302-
}
303-
304-
const walletAddress = this.normalizeEthereumAddress(ethAddr[0]);
305-
306-
if (walletAddress !== storedAddress) {
307-
return { success: false, error: "Ethereum account does not match registered wallet address" };
305+
// onError?.(new Error("Wallet signature request was rejected by user: " + (error.message || error)));
306+
return { success: false, error: "Wallet signature request was rejected by user" };
308307
}
309308

310309
const digest = this.buildEthereumMessageDigest(message);
@@ -444,14 +443,7 @@ export class EthereumWalletAuthProvider implements AuthProvider<EthereumWalletAu
444443
ethAddr = result.ethAddr;
445444
signature = result.signature;
446445
} catch (error: any) {
447-
// Handle wallet specific errors
448-
if (error.code === 4001) {
449-
return { success: false, error: "Wallet signature request was rejected by user" };
450-
}
451-
if (error.message.includes("User rejected") || error.message.includes("User denied")) {
452-
return { success: false, error: "Wallet signature request was rejected by user" };
453-
}
454-
throw error; // Re-throw other errors
446+
return { success: false, error: "Wallet signature request was rejected by user" };
455447
}
456448

457449
const walletAddress = this.normalizeEthereumAddress(ethAddr[0]);
@@ -461,7 +453,7 @@ export class EthereumWalletAuthProvider implements AuthProvider<EthereumWalletAu
461453
this.buildSecp256k1SignatureComponents(digest, signature);
462454

463455
if (recoveredAddress !== walletAddress) {
464-
throw new Error("Recovered public key does not match wallet address");
456+
return { success: false, error: "Recovered public key does not match wallet address" };
465457
}
466458
let inviteCodeBlob;
467459
try {

hyli-wallet/src/utils/errorMessages.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,13 @@ export const getAuthErrorMessage = (error: unknown): ErrorDetails => {
130130
};
131131
}
132132

133+
if (errorString) {
134+
return {
135+
userMessage: errorString,
136+
technicalMessage: errorString,
137+
};
138+
}
139+
133140
// Generic errors
134141
return {
135142
userMessage: "Something went wrong. Please try again.",

hyli-wallet/vue/bun.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"": {
55
"name": "hyli-wallet-vue",
66
"dependencies": {
7-
"hyli-wallet": "^0.5.18",
7+
"hyli-wallet": "^0.5.19",
88
},
99
"devDependencies": {
1010
"@vitejs/plugin-vue": "^6.0.1",
@@ -220,7 +220,7 @@
220220

221221
"hyli-noir": ["[email protected]", "", { "dependencies": { "hyli": "^0.4.1", "noir-jwt": "^0.4.5" }, "peerDependencies": { "@aztec/bb.js": "0.87.9", "@noir-lang/noir_js": "1.0.0-beta.11", "@noir-lang/noir_wasm": "1.0.0-beta.11" } }, "sha512-2aAhpbYvaGzxuWS5Jqcl5QY51FcPg4wrPdbWR3GJBYapiw9So/C40lkmEyEVmEXE+F5cDPNSFs67Hvjnb5Camg=="],
222222

223-
"hyli-wallet": ["[email protected].18", "", { "dependencies": { "@noble/hashes": "^2.0.1", "@types/crypto-js": "^4.2.2", "@types/elliptic": "^6.4.18", "@types/react-router-dom": "^5.3.3", "elliptic": "^6.6.1", "hyli": "^0.4.1", "js-sha3": "^0.9.3", "mipd": "^0.0.7" }, "peerDependencies": { "hyli-noir": "^0.3.0", "react": "^19.1.1", "react-dom": "^19.1.1", "react-router-dom": "^7.9.3" } }, "sha512-6V7jtrho6sYWtDYmTmwuAtG2ei2gRhJ+cEWoiT+FjDjSwuQuNxz5YTO165VhM5xio3ADvQ8+STutEnjhE/HApg=="],
223+
"hyli-wallet": ["[email protected].19", "", { "dependencies": { "@noble/hashes": "^2.0.1", "@types/crypto-js": "^4.2.2", "@types/elliptic": "^6.4.18", "@types/react-router-dom": "^5.3.3", "elliptic": "^6.6.1", "hyli": "^0.4.1", "js-sha3": "^0.9.3", "mipd": "^0.0.7" }, "peerDependencies": { "hyli-noir": "^0.3.0", "react": "^19.1.1", "react-dom": "^19.1.1", "react-router-dom": "^7.9.3" } }, "sha512-NlekniVFBxVgizg5Q6Md1mfnk2N/+vYP+s3d7Dg0A6AP8owK8e2XsBSpzWTaJnZWm/YGO5EGV3qCN+J8/K96vA=="],
224224

225225
"idb-keyval": ["[email protected]", "", {}, "sha512-yjD9nARJ/jb1g+CvD0tlhUHOrJ9Sy0P8T9MF3YaLlHnSRpwPfpTX0XIvpmw3gAJUmEu3FiICLBDPXVwyEvrleg=="],
226226

hyli-wallet/vue/demo/bun.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"": {
55
"name": "demo",
66
"dependencies": {
7-
"hyli-wallet-vue": "^0.1.18",
7+
"hyli-wallet-vue": "^0.1.19",
88
"vite-plugin-node-polyfills": "^0.24.0",
99
"vue": "^3.5.21",
1010
},
@@ -297,9 +297,9 @@
297297

298298
"hyli-noir": ["[email protected]", "", { "dependencies": { "hyli": "^0.4.1", "noir-jwt": "^0.4.5" }, "peerDependencies": { "@aztec/bb.js": "0.87.9", "@noir-lang/noir_js": "1.0.0-beta.11", "@noir-lang/noir_wasm": "1.0.0-beta.11" } }, "sha512-2aAhpbYvaGzxuWS5Jqcl5QY51FcPg4wrPdbWR3GJBYapiw9So/C40lkmEyEVmEXE+F5cDPNSFs67Hvjnb5Camg=="],
299299

300-
"hyli-wallet": ["[email protected].18", "", { "dependencies": { "@noble/hashes": "^2.0.1", "@types/crypto-js": "^4.2.2", "@types/elliptic": "^6.4.18", "@types/react-router-dom": "^5.3.3", "elliptic": "^6.6.1", "hyli": "^0.4.1", "js-sha3": "^0.9.3", "mipd": "^0.0.7" }, "peerDependencies": { "hyli-noir": "^0.3.0", "react": "^19.1.1", "react-dom": "^19.1.1", "react-router-dom": "^7.9.3" } }, "sha512-6V7jtrho6sYWtDYmTmwuAtG2ei2gRhJ+cEWoiT+FjDjSwuQuNxz5YTO165VhM5xio3ADvQ8+STutEnjhE/HApg=="],
300+
"hyli-wallet": ["[email protected].19", "", { "dependencies": { "@noble/hashes": "^2.0.1", "@types/crypto-js": "^4.2.2", "@types/elliptic": "^6.4.18", "@types/react-router-dom": "^5.3.3", "elliptic": "^6.6.1", "hyli": "^0.4.1", "js-sha3": "^0.9.3", "mipd": "^0.0.7" }, "peerDependencies": { "hyli-noir": "^0.3.0", "react": "^19.1.1", "react-dom": "^19.1.1", "react-router-dom": "^7.9.3" } }, "sha512-NlekniVFBxVgizg5Q6Md1mfnk2N/+vYP+s3d7Dg0A6AP8owK8e2XsBSpzWTaJnZWm/YGO5EGV3qCN+J8/K96vA=="],
301301

302-
"hyli-wallet-vue": ["[email protected].18", "", { "dependencies": { "hyli-wallet": "^0.5.18" }, "peerDependencies": { "vue": "^3.5.22" } }, "sha512-neY88ABy480NvNgltFno6uv+wzx/iJVQ6/gDskvIHhX9MZP+MbApXgSeYGz43zUXanjfOLa7SNjog8GNIh2bMQ=="],
302+
"hyli-wallet-vue": ["[email protected].19", "", { "dependencies": { "hyli-wallet": "^0.5.19" }, "peerDependencies": { "vue": "^3.5.22" } }, "sha512-8YWV4iiXxcF1xcgnJcgROB6Z08nSWBGnd81x7Z64EL130p7tSno5SwFUFw6cZzLOwchD9cPD2M1bEO9p4H0zzA=="],
303303

304304
"idb-keyval": ["[email protected]", "", {}, "sha512-yjD9nARJ/jb1g+CvD0tlhUHOrJ9Sy0P8T9MF3YaLlHnSRpwPfpTX0XIvpmw3gAJUmEu3FiICLBDPXVwyEvrleg=="],
305305

hyli-wallet/vue/demo/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
},
1111
"dependencies": {
1212
"vue": "^3.5.21",
13-
"hyli-wallet-vue": "^0.1.18",
13+
"hyli-wallet-vue": "^0.1.19",
1414
"vite-plugin-node-polyfills": "^0.24.0"
1515
},
1616
"devDependencies": {

hyli-wallet/vue/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "hyli-wallet-vue",
3-
"version": "0.1.18",
3+
"version": "0.1.19",
44
"type": "module",
55
"main": "./dist/hyli-wallet.cjs.js",
66
"module": "./dist/hyli-wallet.es.js",
@@ -26,7 +26,7 @@
2626
"vue": "^3.5.22"
2727
},
2828
"dependencies": {
29-
"hyli-wallet": "^0.5.18"
29+
"hyli-wallet": "^0.5.19"
3030
},
3131
"devDependencies": {
3232
"@vitejs/plugin-vue": "^6.0.1",

0 commit comments

Comments
 (0)