chore: initial commit for v0.0.1
DChain single-node blockchain + React Native messenger client. Core: - PBFT consensus with multi-sig validator admission + equivocation slashing - BadgerDB + schema migration scaffold (CurrentSchemaVersion=0) - libp2p gossipsub (tx/v1, blocks/v1, relay/v1, version/v1) - Native Go contracts (username_registry) alongside WASM (wazero) - WebSocket gateway with topic-based fanout + Ed25519-nonce auth - Relay mailbox with NaCl envelope encryption (X25519 + Ed25519) - Prometheus /metrics, per-IP rate limit, body-size cap Deployment: - Single-node compose (deploy/single/) with Caddy TLS + optional Prometheus - 3-node dev compose (docker-compose.yml) with mocked internet topology - 3-validator prod compose (deploy/prod/) for federation - Auto-update from Gitea via /api/update-check + systemd timer - Build-time version injection (ldflags → node --version) - UI / Swagger toggle flags (DCHAIN_DISABLE_UI, DCHAIN_DISABLE_SWAGGER) Client (client-app/): - Expo / React Native / NativeWind - E2E NaCl encryption, typing indicator, contact requests - Auto-discovery of canonical contracts, chain_id aware, WS reconnect on node switch Documentation: - README.md, CHANGELOG.md, CONTEXT.md - deploy/single/README.md with 6 operator scenarios - deploy/UPDATE_STRATEGY.md with 4-layer forward-compat design - docs/contracts/*.md per contract
This commit is contained in:
101
client-app/lib/storage.ts
Normal file
101
client-app/lib/storage.ts
Normal file
@@ -0,0 +1,101 @@
|
||||
/**
|
||||
* Persistent storage for keys and app settings.
|
||||
* On mobile: expo-secure-store for key material, AsyncStorage for settings.
|
||||
* On web: falls back to localStorage (dev only).
|
||||
*/
|
||||
|
||||
import * as SecureStore from 'expo-secure-store';
|
||||
import AsyncStorage from '@react-native-async-storage/async-storage';
|
||||
import type { KeyFile, Contact, NodeSettings } from './types';
|
||||
|
||||
// ─── Keys ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
const KEYFILE_KEY = 'dchain_keyfile';
|
||||
const CONTACTS_KEY = 'dchain_contacts';
|
||||
const SETTINGS_KEY = 'dchain_settings';
|
||||
const CHATS_KEY = 'dchain_chats';
|
||||
|
||||
/** Save the key file in secure storage (encrypted on device). */
|
||||
export async function saveKeyFile(kf: KeyFile): Promise<void> {
|
||||
await SecureStore.setItemAsync(KEYFILE_KEY, JSON.stringify(kf));
|
||||
}
|
||||
|
||||
/** Load key file. Returns null if not set. */
|
||||
export async function loadKeyFile(): Promise<KeyFile | null> {
|
||||
const raw = await SecureStore.getItemAsync(KEYFILE_KEY);
|
||||
if (!raw) return null;
|
||||
return JSON.parse(raw) as KeyFile;
|
||||
}
|
||||
|
||||
/** Delete key file (logout / factory reset). */
|
||||
export async function deleteKeyFile(): Promise<void> {
|
||||
await SecureStore.deleteItemAsync(KEYFILE_KEY);
|
||||
}
|
||||
|
||||
// ─── Node settings ─────────────────────────────────────────────────────────────
|
||||
|
||||
const DEFAULT_SETTINGS: NodeSettings = {
|
||||
nodeUrl: 'http://localhost:8081',
|
||||
contractId: '',
|
||||
};
|
||||
|
||||
export async function loadSettings(): Promise<NodeSettings> {
|
||||
const raw = await AsyncStorage.getItem(SETTINGS_KEY);
|
||||
if (!raw) return DEFAULT_SETTINGS;
|
||||
return { ...DEFAULT_SETTINGS, ...JSON.parse(raw) };
|
||||
}
|
||||
|
||||
export async function saveSettings(s: Partial<NodeSettings>): Promise<void> {
|
||||
const current = await loadSettings();
|
||||
await AsyncStorage.setItem(SETTINGS_KEY, JSON.stringify({ ...current, ...s }));
|
||||
}
|
||||
|
||||
// ─── Contacts ─────────────────────────────────────────────────────────────────
|
||||
|
||||
export async function loadContacts(): Promise<Contact[]> {
|
||||
const raw = await AsyncStorage.getItem(CONTACTS_KEY);
|
||||
if (!raw) return [];
|
||||
return JSON.parse(raw) as Contact[];
|
||||
}
|
||||
|
||||
export async function saveContact(c: Contact): Promise<void> {
|
||||
const contacts = await loadContacts();
|
||||
const idx = contacts.findIndex(x => x.address === c.address);
|
||||
if (idx >= 0) contacts[idx] = c;
|
||||
else contacts.push(c);
|
||||
await AsyncStorage.setItem(CONTACTS_KEY, JSON.stringify(contacts));
|
||||
}
|
||||
|
||||
export async function deleteContact(address: string): Promise<void> {
|
||||
const contacts = await loadContacts();
|
||||
await AsyncStorage.setItem(
|
||||
CONTACTS_KEY,
|
||||
JSON.stringify(contacts.filter(c => c.address !== address)),
|
||||
);
|
||||
}
|
||||
|
||||
// ─── Message cache (per-chat local store) ────────────────────────────────────
|
||||
|
||||
export interface CachedMessage {
|
||||
id: string;
|
||||
from: string;
|
||||
text: string;
|
||||
timestamp: number;
|
||||
mine: boolean;
|
||||
}
|
||||
|
||||
export async function loadMessages(chatId: string): Promise<CachedMessage[]> {
|
||||
const raw = await AsyncStorage.getItem(`${CHATS_KEY}_${chatId}`);
|
||||
if (!raw) return [];
|
||||
return JSON.parse(raw) as CachedMessage[];
|
||||
}
|
||||
|
||||
export async function appendMessage(chatId: string, msg: CachedMessage): Promise<void> {
|
||||
const msgs = await loadMessages(chatId);
|
||||
// Deduplicate by id
|
||||
if (msgs.find(m => m.id === msg.id)) return;
|
||||
msgs.push(msg);
|
||||
// Keep last 500 messages per chat
|
||||
const trimmed = msgs.slice(-500);
|
||||
await AsyncStorage.setItem(`${CHATS_KEY}_${chatId}`, JSON.stringify(trimmed));
|
||||
}
|
||||
Reference in New Issue
Block a user