feat(mobile): Nostr identity — Amber NIP-55 deep link + nsec fallback
Some checks failed
CI / Typecheck & Lint (pull_request) Failing after 0s
Some checks failed
CI / Typecheck & Lint (pull_request) Failing after 0s
Implements mobile Nostr identity management per issue #29. Android — NIP-55 Amber integration: - Opens com.greenart7c3.nostrsigner via `nostrsigner:` URI scheme to retrieve the user's public key without exposing it to the app. - Listens for the `mobile://nostr-callback` deep link response and stores the resulting npub in Expo SecureStore. - Falls back to Play Store install prompt when Amber is not installed. iOS / manual fallback: - NostrConnectModal accepts an nsec1 paste-in, validates bech32, derives the pubkey via nostr-tools getPublicKey, and stores the key only in Expo SecureStore — never in AsyncStorage, Redux, or logs. Both platforms: - Truncated npub and signer type (Amber / nsec) shown in Settings. - "Disconnect Nostr" wipes all keys from SecureStore and resets state. - Identity persists across restarts via SecureStore. Supporting changes: - NostrContext: new React context for identity lifecycle. - NostrConnectModal: platform-aware bottom-sheet modal for connect flow. - TimmyContext: added apiBaseUrl/setApiBaseUrl/isConnected; URL persisted in AsyncStorage and restored on mount; circular dep broken via refs. - constants/colors: added field, textInverted, destructive, link colours. - constants/storage-keys: added SERVER_URL_KEY. - app.json: added Android intent filter for mobile://nostr-callback. - package.json: added nostr-tools and expo-secure-store dependencies. Fixes #29 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -8,6 +8,9 @@ import React, {
|
||||
useState,
|
||||
} from "react";
|
||||
import { AppState, Platform } from "react-native";
|
||||
import AsyncStorage from "@react-native-async-storage/async-storage";
|
||||
|
||||
import { SERVER_URL_KEY } from "@/constants/storage-keys";
|
||||
|
||||
export type TimmyMood = "idle" | "thinking" | "working" | "speaking";
|
||||
|
||||
@@ -22,33 +25,42 @@ export type WsEvent = {
|
||||
count?: number;
|
||||
};
|
||||
|
||||
export type ConnectionStatus = "connecting" | "connected" | "disconnected" | "reconnecting" | "error";
|
||||
export type ConnectionStatus =
|
||||
| "connecting"
|
||||
| "connected"
|
||||
| "disconnected"
|
||||
| "reconnecting"
|
||||
| "error";
|
||||
|
||||
type TimmyContextValue = {
|
||||
timmyMood: TimmyMood;
|
||||
connectionStatus: ConnectionStatus;
|
||||
/** True when the WebSocket is fully open */
|
||||
isConnected: boolean;
|
||||
recentEvents: WsEvent[];
|
||||
send: (msg: object) => void;
|
||||
sendVisitorMessage: (text: string) => void;
|
||||
visitorId: string;
|
||||
/** Current API / WebSocket base domain */
|
||||
apiBaseUrl: string;
|
||||
/** Persist a new base URL and reconnect the WebSocket */
|
||||
setApiBaseUrl: (url: string) => void;
|
||||
};
|
||||
|
||||
const TimmyContext = createContext<TimmyContextValue | null>(null);
|
||||
|
||||
const MAX_EVENTS = 100;
|
||||
const BASE_URL = process.env.EXPO_PUBLIC_DOMAIN ?? "";
|
||||
const ENV_DOMAIN = process.env["EXPO_PUBLIC_DOMAIN"] ?? "";
|
||||
const VISITOR_ID =
|
||||
Date.now().toString() + Math.random().toString(36).substr(2, 9);
|
||||
|
||||
function getWsUrl(): string {
|
||||
let domain = BASE_URL;
|
||||
if (!domain) {
|
||||
domain = "localhost:8080";
|
||||
}
|
||||
domain = domain.replace(/^https?:\/\//, "");
|
||||
domain = domain.replace(/\/$/, "");
|
||||
const proto = domain.startsWith("localhost") ? "ws" : "wss";
|
||||
return `${proto}://${domain}/api/ws`;
|
||||
function buildWsUrl(domain: string): string {
|
||||
let d = domain.trim();
|
||||
if (!d) d = "localhost:8080";
|
||||
d = d.replace(/^https?:\/\//, "");
|
||||
d = d.replace(/\/$/, "");
|
||||
const proto = d.startsWith("localhost") ? "ws" : "wss";
|
||||
return `${proto}://${d}/api/ws`;
|
||||
}
|
||||
|
||||
function deriveMood(agentStates: Record<string, string>): TimmyMood {
|
||||
@@ -63,10 +75,12 @@ function deriveMood(agentStates: Record<string, string>): TimmyMood {
|
||||
}
|
||||
|
||||
export function TimmyProvider({ children }: { children: React.ReactNode }) {
|
||||
const [apiBaseUrl, setApiBaseUrlState] = useState(ENV_DOMAIN);
|
||||
const [timmyMood, setTimmyMood] = useState<TimmyMood>("idle");
|
||||
const [connectionStatus, setConnectionStatus] =
|
||||
useState<ConnectionStatus>("connecting");
|
||||
const [recentEvents, setRecentEvents] = useState<WsEvent[]>([]);
|
||||
|
||||
const wsRef = useRef<WebSocket | null>(null);
|
||||
const retryTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
||||
const retryCountRef = useRef(0);
|
||||
@@ -77,6 +91,32 @@ export function TimmyProvider({ children }: { children: React.ReactNode }) {
|
||||
delta: "idle",
|
||||
});
|
||||
const speakingTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
||||
// Stable ref so WebSocket callbacks always read the current URL
|
||||
const apiBaseUrlRef = useRef(apiBaseUrl);
|
||||
// Stable refs to break the connectWs ↔ scheduleRetry circular dependency
|
||||
const connectWsRef = useRef<() => void>(() => {});
|
||||
const scheduleRetryRef = useRef<() => void>(() => {});
|
||||
|
||||
// ── Load persisted URL on mount ────────────────────────────────────────
|
||||
|
||||
useEffect(() => {
|
||||
AsyncStorage.getItem(SERVER_URL_KEY)
|
||||
.then((stored) => {
|
||||
if (stored) {
|
||||
setApiBaseUrlState(stored);
|
||||
apiBaseUrlRef.current = stored;
|
||||
}
|
||||
})
|
||||
.catch(() => {});
|
||||
}, []);
|
||||
|
||||
const setApiBaseUrl = useCallback((url: string) => {
|
||||
setApiBaseUrlState(url);
|
||||
apiBaseUrlRef.current = url;
|
||||
AsyncStorage.setItem(SERVER_URL_KEY, url).catch(() => {});
|
||||
}, []);
|
||||
|
||||
// ── WebSocket helpers ──────────────────────────────────────────────────
|
||||
|
||||
const addEvent = useCallback((evt: Omit<WsEvent, "id" | "timestamp">) => {
|
||||
const entry: WsEvent = {
|
||||
@@ -94,14 +134,14 @@ export function TimmyProvider({ children }: { children: React.ReactNode }) {
|
||||
wsRef.current.close();
|
||||
wsRef.current = null;
|
||||
}
|
||||
const url = getWsUrl();
|
||||
const url = buildWsUrl(apiBaseUrlRef.current);
|
||||
setConnectionStatus("connecting");
|
||||
let ws: WebSocket;
|
||||
try {
|
||||
ws = new WebSocket(url);
|
||||
} catch {
|
||||
setConnectionStatus("error");
|
||||
scheduleRetry();
|
||||
scheduleRetryRef.current();
|
||||
return;
|
||||
}
|
||||
wsRef.current = ws;
|
||||
@@ -134,10 +174,7 @@ export function TimmyProvider({ children }: { children: React.ReactNode }) {
|
||||
|
||||
if (type === "world_state") {
|
||||
const states = (msg.agentStates as Record<string, string>) ?? {};
|
||||
agentStatesRef.current = {
|
||||
...agentStatesRef.current,
|
||||
...states,
|
||||
};
|
||||
agentStatesRef.current = { ...agentStatesRef.current, ...states };
|
||||
setTimmyMood(deriveMood(agentStatesRef.current));
|
||||
return;
|
||||
}
|
||||
@@ -187,7 +224,7 @@ export function TimmyProvider({ children }: { children: React.ReactNode }) {
|
||||
|
||||
ws.onclose = () => {
|
||||
setConnectionStatus("disconnected");
|
||||
scheduleRetry();
|
||||
scheduleRetryRef.current();
|
||||
};
|
||||
|
||||
ws.onerror = () => {
|
||||
@@ -200,9 +237,15 @@ export function TimmyProvider({ children }: { children: React.ReactNode }) {
|
||||
const delay = Math.min(1000 * Math.pow(2, retryCountRef.current), 30000);
|
||||
retryCountRef.current += 1;
|
||||
retryTimerRef.current = setTimeout(() => {
|
||||
connectWs();
|
||||
connectWsRef.current();
|
||||
}, delay);
|
||||
}, [connectWs]);
|
||||
}, []);
|
||||
|
||||
// Keep the stable refs current after every render
|
||||
connectWsRef.current = connectWs;
|
||||
scheduleRetryRef.current = scheduleRetry;
|
||||
|
||||
// ── Initial connect ────────────────────────────────────────────────────
|
||||
|
||||
useEffect(() => {
|
||||
connectWs();
|
||||
@@ -216,7 +259,19 @@ export function TimmyProvider({ children }: { children: React.ReactNode }) {
|
||||
};
|
||||
}, [connectWs]);
|
||||
|
||||
// AppState-aware WebSocket reconnect on foreground
|
||||
// Reconnect when apiBaseUrl changes (skip the very first render)
|
||||
const isFirstRenderRef = useRef(true);
|
||||
useEffect(() => {
|
||||
if (isFirstRenderRef.current) {
|
||||
isFirstRenderRef.current = false;
|
||||
return;
|
||||
}
|
||||
retryCountRef.current = 0;
|
||||
connectWs();
|
||||
}, [apiBaseUrl, connectWs]);
|
||||
|
||||
// ── AppState-aware reconnect on foreground ─────────────────────────────
|
||||
|
||||
useEffect(() => {
|
||||
if (Platform.OS === "web") return;
|
||||
|
||||
@@ -229,20 +284,17 @@ export function TimmyProvider({ children }: { children: React.ReactNode }) {
|
||||
const isNowActive = nextAppState === "active";
|
||||
|
||||
if (wasBackground && isNowActive) {
|
||||
// App returned to foreground — check if WS is still alive
|
||||
const ws = wsRef.current;
|
||||
if (!ws || ws.readyState !== WebSocket.OPEN) {
|
||||
// Cancel any pending retry so we don't create duplicates
|
||||
if (retryTimerRef.current) {
|
||||
clearTimeout(retryTimerRef.current);
|
||||
retryTimerRef.current = null;
|
||||
}
|
||||
retryCountRef.current = 0;
|
||||
setConnectionStatus("reconnecting");
|
||||
connectWs();
|
||||
connectWsRef.current();
|
||||
}
|
||||
} else if (nextAppState === "background") {
|
||||
// Proactively close the WS to avoid OS killing it mid-frame
|
||||
if (retryTimerRef.current) {
|
||||
clearTimeout(retryTimerRef.current);
|
||||
retryTimerRef.current = null;
|
||||
@@ -262,7 +314,9 @@ export function TimmyProvider({ children }: { children: React.ReactNode }) {
|
||||
return () => {
|
||||
subscription.remove();
|
||||
};
|
||||
}, [connectWs]);
|
||||
}, []);
|
||||
|
||||
// ── Outbound messages ──────────────────────────────────────────────────
|
||||
|
||||
const send = useCallback((msg: object) => {
|
||||
if (wsRef.current && wsRef.current.readyState === WebSocket.OPEN) {
|
||||
@@ -270,28 +324,42 @@ export function TimmyProvider({ children }: { children: React.ReactNode }) {
|
||||
}
|
||||
}, []);
|
||||
|
||||
const sendVisitorMessage = useCallback(
|
||||
(text: string) => {
|
||||
if (wsRef.current && wsRef.current.readyState === WebSocket.OPEN) {
|
||||
wsRef.current.send(
|
||||
JSON.stringify({ type: "visitor_message", visitorId: VISITOR_ID, text })
|
||||
);
|
||||
setTimmyMood("thinking");
|
||||
}
|
||||
},
|
||||
[]
|
||||
);
|
||||
const sendVisitorMessage = useCallback((text: string) => {
|
||||
if (wsRef.current && wsRef.current.readyState === WebSocket.OPEN) {
|
||||
wsRef.current.send(
|
||||
JSON.stringify({
|
||||
type: "visitor_message",
|
||||
visitorId: VISITOR_ID,
|
||||
text,
|
||||
})
|
||||
);
|
||||
setTimmyMood("thinking");
|
||||
}
|
||||
}, []);
|
||||
|
||||
const value = useMemo(
|
||||
// ── Context value ──────────────────────────────────────────────────────
|
||||
|
||||
const value = useMemo<TimmyContextValue>(
|
||||
() => ({
|
||||
timmyMood,
|
||||
connectionStatus,
|
||||
isConnected: connectionStatus === "connected",
|
||||
recentEvents,
|
||||
send,
|
||||
sendVisitorMessage,
|
||||
visitorId: VISITOR_ID,
|
||||
apiBaseUrl,
|
||||
setApiBaseUrl,
|
||||
}),
|
||||
[
|
||||
timmyMood,
|
||||
connectionStatus,
|
||||
recentEvents,
|
||||
send,
|
||||
sendVisitorMessage,
|
||||
visitorId: VISITOR_ID,
|
||||
}),
|
||||
[timmyMood, connectionStatus, recentEvents, send, sendVisitorMessage]
|
||||
apiBaseUrl,
|
||||
setApiBaseUrl,
|
||||
]
|
||||
);
|
||||
|
||||
return (
|
||||
|
||||
Reference in New Issue
Block a user