tane/apps/app_seeds/lib/services/social_service.dart
vjrj 70905a0578 feat(block2): wire the social identity into app_seeds (DI, no UI)
Third slice: bridge Block 1's root seed to the Block 2 transport foundation.

- SocialService: derives the secp256k1 Nostr identity from the SAME root seed
  the recovery QR backs up (no extra backup), exposes npub/pubkey, and opens a
  SocialSession = one shared connection carrying all three transports.
- injector.dart: registers SocialService in get_it. Local-first — derivation is
  cheap + offline; no relay is contacted at startup. Replaces the old
  "nodeId slice of the seed is the social pubkey" placeholder; keeps the CRDT
  author id distinct (unifying it would rewrite authorship — a later decision).
- Test: deterministic identity from the seed hex, matches commons_core, rejects
  malformed hex. 5 tests green; app_seeds analyzes clean (0 errors).
2026-07-10 02:44:37 +02:00

74 lines
2.6 KiB
Dart

import 'dart:typed_data';
import 'package:commons_core/commons_core.dart';
/// The app-side entry point to the (Block 2) social layer.
///
/// Holds the user's Nostr identity — deterministically derived from the same
/// root seed the recovery QR backs up (so it needs no extra backup) — and opens
/// social sessions on demand. Local-first: constructing this is cheap and
/// offline; nothing connects to a relay until [openSession] is called, so the
/// app runs fully without network and the social layer only enriches.
class SocialService {
SocialService({required this.identity, List<String> relays = const []})
: relays = List.unmodifiable(relays);
/// The derived Nostr identity (secp256k1). Same key across reinstalls that
/// restore the same seed.
final NostrIdentity identity;
/// Community relay URLs (may be empty — discovery/publishing degrade to
/// nothing when offline or unconfigured).
final List<String> relays;
/// Shareable public identity (`npub…`) and its hex form.
String get npub => identity.npub;
String get publicKeyHex => identity.publicKeyHex;
/// Derives the identity from the stored root-seed hex (32-byte, 64 chars).
static Future<SocialService> fromRootSeedHex(
String rootSeedHex, {
List<String> relays = const [],
}) async {
final identity = await NostrKeyDerivation.deriveFromSeed(
_hexToBytes(rootSeedHex),
);
return SocialService(identity: identity, relays: relays);
}
/// Opens a session against [relayUrl]: one shared connection carrying all
/// three transports (offers, messaging, trust). Caller closes it.
Future<SocialSession> openSession(String relayUrl) async {
final connection =
await NostrConnection.connect(relayUrl, identity: identity);
return SocialSession(connection);
}
}
/// One live relay connection with the three transports on top — the
/// "one connection, three interfaces" shape, at the app layer.
class SocialSession {
SocialSession(this._connection)
: offers = NostrOfferTransport(_connection),
messages = NostrMessageTransport(_connection),
trust = NostrTrustTransport(_connection);
final NostrConnection _connection;
final OfferTransport offers;
final MessageTransport messages;
final TrustTransport trust;
Future<void> close() => _connection.close();
}
Uint8List _hexToBytes(String hex) {
if (hex.length.isOdd) {
throw ArgumentError('hex string must have an even length');
}
final out = Uint8List(hex.length ~/ 2);
for (var i = 0; i < out.length; i++) {
out[i] = int.parse(hex.substring(i * 2, i * 2 + 2), radix: 16);
}
return out;
}