The app-layer driver that turns local intent into a signed proposal, counter-signs or declines incoming ones, and reconciles every move into the local ledger. Cryptography and wire format stay in commons_core; this owns persistence and orchestration. - propose(): builds + self-signs a PlantarePledge, records the local row (remoteState=proposed, my stub) and sends it. Direction maps to debtor/creditor: iReturn = I received and owe, owedToMe = I gave. - accept(): counter-signs the exact in-hand proposal (verified) and sends the doubly-signed copy back, closing the row. - decline(): notifies the proposer and marks the row declined. - ingest(): verifies stubs before storing — a bad-signature proposal or a half-signed "accept" is dropped; proposals not addressed to me are ignored. Pending proposals held in memory keyed by pledge id (relay redelivers on reconnect), so accept signs the verified payload. Wired into DI + Bootstrap alongside the inbox/sync listeners and torn down/rebuilt on identity switch. SocialSession now exposes the transport. Tests: full propose→accept close, decline, and the three drop paths with a records-only transport + real repos. services/data green.
137 lines
4.9 KiB
Dart
137 lines
4.9 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.
|
|
/// The app-data namespace this identity's devices sync their inventory under.
|
|
/// Lives here (app layer), not in `commons_core`, which stays seed-agnostic.
|
|
const kInventorySyncNamespace = 'org.comunes.tane/inventory';
|
|
|
|
class SocialService {
|
|
SocialService({
|
|
required this.identity,
|
|
this.account = 0,
|
|
this.rootSeedHex = '',
|
|
this.deviceId = '',
|
|
List<String> relays = const [],
|
|
}) : relays = List.unmodifiable(relays);
|
|
|
|
/// This install's device id (for device-to-device sync). Empty in tests /
|
|
/// when sync isn't wired.
|
|
final String deviceId;
|
|
|
|
/// The derived Nostr identity (secp256k1). Same key across reinstalls that
|
|
/// restore the same seed (for a given [account]).
|
|
final NostrIdentity identity;
|
|
|
|
/// Which pseudonymous identity of the root seed this is (0 = the original).
|
|
/// Switching account changes the social identity while the backup stays the
|
|
/// single root seed. See [NostrKeyDerivation.deriveFromSeed].
|
|
final int account;
|
|
|
|
/// The root-seed hex, kept so the switcher can preview OTHER accounts'
|
|
/// identities without re-reading the keystore. Empty when unknown (tests).
|
|
final String rootSeedHex;
|
|
|
|
/// 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) for
|
|
/// the given [account] (0 = the original identity).
|
|
static Future<SocialService> fromRootSeedHex(
|
|
String rootSeedHex, {
|
|
int account = 0,
|
|
String deviceId = '',
|
|
List<String> relays = const [],
|
|
}) async {
|
|
final identity = await NostrKeyDerivation.deriveFromSeed(
|
|
_hexToBytes(rootSeedHex),
|
|
account: account,
|
|
);
|
|
return SocialService(
|
|
identity: identity,
|
|
account: account,
|
|
rootSeedHex: rootSeedHex,
|
|
deviceId: deviceId,
|
|
relays: relays,
|
|
);
|
|
}
|
|
|
|
/// The `npub` for another [account] of the same root seed, so the switcher can
|
|
/// show identities before committing to one. Returns null if the seed is
|
|
/// unknown (e.g. in tests constructed without it).
|
|
Future<String?> npubForAccount(int account) async {
|
|
if (rootSeedHex.isEmpty) return null;
|
|
final id = await NostrKeyDerivation.deriveFromSeed(
|
|
_hexToBytes(rootSeedHex),
|
|
account: account,
|
|
);
|
|
return id.npub;
|
|
}
|
|
|
|
/// Opens a session against [relayUrls]: one fault-tolerant pool carrying all
|
|
/// three transports (offers, messaging, trust). Relays that are down are
|
|
/// skipped; throws only when none are reachable. Caller closes it.
|
|
Future<SocialSession> openSession(List<String> relayUrls) async {
|
|
final pool = await RelayPool.connect(relayUrls, identity: identity);
|
|
return SocialSession(pool, deviceId: deviceId);
|
|
}
|
|
}
|
|
|
|
/// One relay channel with the transports on top — the
|
|
/// "one channel, N interfaces" shape, at the app layer.
|
|
class SocialSession {
|
|
SocialSession(this._channel, {String deviceId = ''})
|
|
: offers = NostrOfferTransport(_channel),
|
|
messages = NostrMessageTransport(_channel),
|
|
trust = NostrTrustTransport(_channel),
|
|
ratings = NostrRatingTransport(_channel),
|
|
reports = NostrReportTransport(_channel),
|
|
profile = NostrProfileTransport(_channel),
|
|
plantares = NostrPlantareTransport(_channel),
|
|
sync = NostrSyncTransport(
|
|
_channel,
|
|
namespace: kInventorySyncNamespace,
|
|
deviceId: deviceId,
|
|
);
|
|
|
|
final NostrChannel _channel;
|
|
|
|
final OfferTransport offers;
|
|
final MessageTransport messages;
|
|
final TrustTransport trust;
|
|
final RatingTransport ratings;
|
|
final ReportTransport reports;
|
|
final ProfileTransport profile;
|
|
|
|
/// Bilateral signed Plantaré handshake (propose/accept/decline), private
|
|
/// end-to-end over the shared connection.
|
|
final PlantareTransport plantares;
|
|
|
|
/// Device-to-device inventory sync for THIS identity's own devices.
|
|
final SyncTransport sync;
|
|
|
|
Future<void> close() => _channel.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;
|
|
}
|