Wire the Blossom MediaTransport into publishing: shareable lots carry their varietyId, publishLots uploads the lot's cover photo and puts the returned URL on the offer (best-effort — a hosting failure still publishes the offer, just without a photo). Add a configurable media server (Comunes default, empty to opt out) to settings and the market advanced config.
97 lines
3.5 KiB
Dart
97 lines
3.5 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 [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.
|
|
///
|
|
/// [mediaServerUrl] (a Blossom endpoint) enables offer photos: when set, the
|
|
/// session exposes a [MediaTransport] to host images. It rides HTTP, not the
|
|
/// relay pool, so it's independent of relay reachability.
|
|
Future<SocialSession> openSession(
|
|
List<String> relayUrls, {
|
|
String? mediaServerUrl,
|
|
}) async {
|
|
final pool = await RelayPool.connect(relayUrls, identity: identity);
|
|
return SocialSession(pool, mediaServerUrl: mediaServerUrl);
|
|
}
|
|
}
|
|
|
|
/// One relay channel with the three transports on top — the
|
|
/// "one channel, three interfaces" shape, at the app layer. Media rides its own
|
|
/// HTTP endpoint (Blossom), so it's a fourth transport but not on the channel.
|
|
class SocialSession {
|
|
SocialSession(this._channel, {String? mediaServerUrl})
|
|
: offers = NostrOfferTransport(_channel),
|
|
messages = NostrMessageTransport(_channel),
|
|
trust = NostrTrustTransport(_channel),
|
|
profile = NostrProfileTransport(_channel),
|
|
media = (mediaServerUrl != null && mediaServerUrl.trim().isNotEmpty)
|
|
? BlossomMediaTransport(
|
|
Uri.parse(mediaServerUrl.trim()),
|
|
_channel.privateKeyHex,
|
|
)
|
|
: null;
|
|
|
|
final NostrChannel _channel;
|
|
|
|
final OfferTransport offers;
|
|
final MessageTransport messages;
|
|
final TrustTransport trust;
|
|
final ProfileTransport profile;
|
|
|
|
/// Hosts offer photos, or null when no media server is configured (offers then
|
|
/// publish without images).
|
|
final MediaTransport? media;
|
|
|
|
Future<void> close() async {
|
|
await media?.close();
|
|
await _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;
|
|
}
|