Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 14 additions & 18 deletions admin-dapp/bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 4 additions & 8 deletions admin-dapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,11 @@
"lodash": "4.18.1"
},
"dependencies": {
"@enbox/agent": "0.8.17",
"@enbox/api": "0.6.55",
"@enbox/auth": "0.6.63",
"@enbox/browser": "0.3.41",
"@enbox/browser": "0.3.56",
"@enbox/common": "0.1.3",
"@enbox/crypto": "0.1.5",
"@enbox/dids": "0.1.5",
"@enbox/dwn-sdk-js": "0.4.10",
"@enbox/protocols": "0.2.82",
"@enbox/crypto": "0.1.6",
"@enbox/dids": "0.1.6",
"@enbox/protocols": "0.2.91",
"clsx": "2.1.1",
"lucide-react": "0.407.0",
"react": "18.3.1",
Expand Down
40 changes: 36 additions & 4 deletions admin-dapp/src/enbox/EnboxProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import React, { createContext, useCallback, useEffect, useRef, useState } from "react";

import { AuthManager, BrowserConnectHandler, Enbox } from "@enbox/browser";
import type { AuthManagerOptions, AuthSession } from "@enbox/browser";
import { BrowserStorage } from "@enbox/auth";
import type { ProviderAuthParams, ProviderAuthResult } from "@enbox/auth";
import { AuthManager, BrowserConnectHandler, BrowserStorage, Enbox, isSessionExpiredError, isSessionInvalidError } from "@enbox/browser";
import type { AuthManagerOptions, AuthSession, ProviderAuthParams, ProviderAuthResult } from "@enbox/browser";

import {
AUTH_DATA_PATH,
Expand Down Expand Up @@ -130,6 +128,8 @@ async function resetScopedAuthStorage() {

export const EnboxProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
const authRef = useRef<AuthManager | null>(null);
const monitorStopRef = useRef<(() => void) | null>(null);
const disconnectRef = useRef<((options?: { clearStorage?: boolean }) => Promise<void>) | null>(null);

const [isConnecting, setIsConnecting] = useState(false);
const [enbox, setEnbox] = useState<Enbox | undefined>();
Expand All @@ -147,6 +147,23 @@ export const EnboxProvider: React.FC<{ children: React.ReactNode }> = ({ childre
setIsDelegateSession(Boolean(session.delegateDid));
setProtocolsInitialized(false);
setProtocolSetupError(undefined);

// Monitor delegate (wallet) sessions only; local sessions never expire on us.
monitorStopRef.current?.();
monitorStopRef.current = null;
const auth = authRef.current;
if (auth && session.delegateDid) {
monitorStopRef.current = auth.startConnectionMonitor({
autoRefresh: { protocols: DAPP_PROTOCOLS },
onError: (err) => {
if (isSessionExpiredError(err) || isSessionInvalidError(err)) {
void disconnectRef.current?.();
} else {
console.warn("[meshd-admin] connection refresh failed; will retry", err);
}
}
});
}
}, []);

useEffect(() => {
Expand Down Expand Up @@ -234,6 +251,8 @@ export const EnboxProvider: React.FC<{ children: React.ReactNode }> = ({ childre

const disconnect = useCallback(async (options?: { clearStorage?: boolean }) => {
const auth = authRef.current;
monitorStopRef.current?.();
monitorStopRef.current = null;
try {
await enbox?.disconnect();
await auth?.disconnect({ clearStorage: options?.clearStorage });
Expand All @@ -251,6 +270,19 @@ export const EnboxProvider: React.FC<{ children: React.ReactNode }> = ({ childre
}
}, [enbox]);

// Keep the monitor's onError handler pointing at the latest disconnect, and
// ensure the monitor is torn down when the provider unmounts.
useEffect(() => {
disconnectRef.current = disconnect;
}, [disconnect]);

useEffect(() => {
return () => {
monitorStopRef.current?.();
monitorStopRef.current = null;
};
}, []);

return (
<EnboxContext.Provider
value={{
Expand Down
18 changes: 4 additions & 14 deletions admin-dapp/src/enbox/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { AuthManagerOptions, Permission, ProtocolRequest, WalletOption } from "@enbox/browser";
import { DEFAULT_WALLETS } from "@enbox/browser";

import meshProtocolDefinitionJson from "../../../protocols/wireguard-mesh.json";

Expand All @@ -10,20 +11,9 @@ const ADMIN_PERMISSIONS = ["read", "write", "delete"] satisfies Permission[];

export const DAPP_NAME = import.meta.env.VITE_ENBOX_DAPP_NAME || "meshd Admin";

export const WALLET_OPTIONS = [
{
name: "Enbox Wallet",
url: import.meta.env.VITE_ENBOX_WALLET_URL || "https://enbox-wallet.pages.dev",
icon: `${import.meta.env.VITE_ENBOX_WALLET_URL || "https://enbox-wallet.pages.dev"}/favicon.ico`,
description: "Your Enbox identity wallet"
},
{
name: "Blue Enbox Wallet",
url: import.meta.env.VITE_ENBOX_BLUE_WALLET_URL || "https://blue-enbox-wallet.pages.dev",
icon: `${import.meta.env.VITE_ENBOX_BLUE_WALLET_URL || "https://blue-enbox-wallet.pages.dev"}/favicon.ico`,
description: "Your Enbox identity wallet"
}
] satisfies WalletOption[];
// Use the SDK's current DEFAULT_WALLETS (Enbox, Prism, Matcha, Onyx) so the
// standardized connect dialog stays current as the SDK's wallet set evolves.
export const WALLET_OPTIONS: WalletOption[] = DEFAULT_WALLETS;

export const DWN_ENDPOINTS = (
import.meta.env.VITE_ENBOX_DWN_ENDPOINTS || "https://dev.aws.dwn.enbox.id,https://enbox-dwn.fly.dev"
Expand Down
3 changes: 1 addition & 2 deletions admin-dapp/src/enbox/protocols.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { normalizeProtocolRequests } from "@enbox/auth";
import type { Enbox } from "@enbox/browser";
import { DwnInterfaceName, DwnMethodName } from "@enbox/dwn-sdk-js";
import { DwnInterfaceName, DwnMethodName, normalizeProtocolRequests } from "@enbox/browser";
import { describe, expect, it, vi } from "vitest";

import rawMeshProtocolDefinition from "../../../protocols/wireguard-mesh.json";
Expand Down
2 changes: 1 addition & 1 deletion admin-dapp/src/meshd/admin.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DwnInterface } from "@enbox/agent";
import { DwnInterface } from "@enbox/browser";
import { Ed25519 } from "@enbox/crypto";
import { describe, expect, it, vi } from "vitest";

Expand Down
2 changes: 1 addition & 1 deletion admin-dapp/src/meshd/admin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DwnInterface } from "@enbox/agent";
import { DwnInterface } from "@enbox/browser";
import { Ed25519 } from "@enbox/crypto";
import { DidJwk } from "@enbox/dids";

Expand Down
2 changes: 1 addition & 1 deletion admin-dapp/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "ESNext",
"moduleResolution": "Node",
"moduleResolution": "Bundler",
"baseUrl": ".",
"paths": {
"@/*": ["src/*"]
Expand Down
Loading