mirror of
https://github.com/block/buzz.git
synced 2026-10-28 13:01:10 +00:00
This PR implements MVP, iOS-only,
[NIP-PL](8d2d0ff5ad/docs/nips/NIP-PL.md)-compliant
push notifications.
A relay with `BUZZ_PUSH_ENABLED` will send a push notification for any
message that appears in the in-app Notifications tab.
## Enrollment flow
The first time the client first connects to a relay with
`BUZZ_PUSH_ENABLED`:
```mermaid
sequenceDiagram
autonumber
participant App as Buzz iOS app
participant iOS
participant Relay as Buzz relay
participant Attest as Apple App Attest
participant Gateway as Push gateway
App->>Relay: Fetch NIP-11 push capability
Relay-->>App: Push profile, current relay public key, and limits
par
App->>iOS: Request notification permission
iOS-->>App: Permission result
and
App->>iOS: Register for remote notifications
iOS-->>App: Device token
end
App->>Gateway: Request installation challenge
Gateway-->>App: Single-use challenge
App->>Attest: Attest installation transcript
Attest-->>App: Attestation proof
App->>Gateway: Enroll device token and proof
Gateway-->>App: Installation handle
App->>Gateway: Request delegation challenge
Gateway-->>App: Single-use challenge
App->>Attest: Assert relay-key delegation
Attest-->>App: Assertion
App->>Gateway: Create delegation
Gateway-->>App: Opaque endpoint grant
App->>Relay: Publish encrypted push lease and filters
Relay-->>App: Lease acknowledged
```
## Push-time flow
When a notification-eligible event is received by the relay:
```mermaid
%%{init: {
"sequence": {
"actorMargin": 20,
"width": 110,
"messageMargin": 18,
"diagramMarginX": 8,
"wrap": true
}
}}%%
sequenceDiagram
autonumber
participant Relay as Buzz relay
participant Gateway as Push gateway
participant APNs as Apple Push<br/>Notification service
participant iOS
participant NSE as Notification service<br/>extension
Relay->>Gateway: POST /v1/deliveries/apns<br/>opaque endpoint grant, request ID, expiry, NIP-98 authorization
Gateway->>APNs: POST /3/device/{device-token}<br/>topic, request ID, expiry, constant mutable-content payload
APNs-->>Gateway: 200 OK: request accepted
Gateway-->>Relay: 200 OK: accepted status
APNs-->>iOS: Notification: constant reconnect alert<br/>mutable-content = 1
iOS->>NSE: Invoke extension<br/>original notification content
NSE->>Relay: POST /query: subscription filters, limit 10<br/>NIP-98 authorization
Relay-->>NSE: 200 OK: signed Nostr events<br/>kinds 9, 40002, 45001, or 45003
NSE->>iOS: Complete notification: title, body, subtitle<br/>thread ID, exact-message target
```
relay → push gateway → APNs -> NSE -> Notification Center
## Known limitations
The APNs wake payload is intentionally constant and opaque: it contains
no originating community or message identifier, in keeping with the
implemented NIP-PL privacy design.
The Notification Service Extension must therefore reconnect to the relay
and resolve eligible messages after each wake. Around overlapping wakes,
timing boundaries, or resolution windows, notification presentation may
occasionally omit an expected message or display a message more than
once.
This best-effort behavior is deliberately accepted for the current
implementation and will be measured during the internal rollout to
determine whether the user experience is acceptable before any broader
deployment; the implementation does not claim exactly-once presentation.
## Validation
Live end-to-end hardware validation used an internal remotely hosted
development relay and push gateway, the APNs sandbox, and a physical
iPhone 12 mini:
- A second real Buzz client published a uniquely marked message through
the hosted relay.
- The relay matched the message and sent the constant opaque wake
through the hosted gateway. The gateway made an actual APNs request; no
`simctl push` or simulated notification was used.
- The iPhone received the notification on its lock screen. The
Notification Service Extension reconnected to the relay, fetched the
event, verified its ID and signature, and replaced the placeholder
content with the real notification title and body.
- After the app populated its shared presentation cache, a final marked
notification visibly showed the sender display name, sender avatar, and
hashtag-prefixed channel name.
- Tapping a lock-screen notification opened Buzz and exercised the
notification-response path and navigated to the corresponding message.
Final validation with a dogfood-signed artifact and production App
Attest/APNs configuration remains a release step.
## Independent pre-reviews
- **First pass:**
[Carl](buzz://message?channel=18882f4c-289f-41db-942f-81f6f8066da1&id=74ab9a93bb227f3e762568f1cf9fee66d7495b0edc3918735ff787238b9cc585)
found missing transient retries, executor-key rotation suppression,
duplicate installation renewal, and an unauthenticated challenge write
amplifier. These were resolved by [retry-safe
bootstrap](https://github.com/block/buzz/pull/6269/changes/12c66ea62)
and [authenticated renewal plus a cross-replica
quota](https://github.com/block/buzz/pull/6269/changes/8e5ece0bd).
[sol-max](buzz://message?channel=ad83385f-8e9e-4461-9a35-c1bf2e208532&id=d26d53daa4684669e2ed354638241f13f36c3a97027fe8b4dd738aff09038962)
found delegation generation burning and an edited applied migration,
resolved by [exact-generation
revocation](https://github.com/block/buzz/pull/6269/changes/c26d2159d)
and a [forward-only
migration](https://github.com/block/buzz/pull/6269/changes/956c1d099).
[k3-max](buzz://message?channel=5e46055d-a766-4065-ae25-05d1e4aaa6b2&id=d43139138a0b15f806cbdbeeedd8f69d992cadf2e805876db6fdde6a34c7eda1)
found no blockers.
- **Exact-head re-review:**
[Carl](buzz://message?channel=18882f4c-289f-41db-942f-81f6f8066da1&id=a897721673459301b0cf26e8b85a1478d7ebbb56a4621f93d774c98d395b8f68),
[sol-max](buzz://message?channel=ad83385f-8e9e-4461-9a35-c1bf2e208532&id=fb2159f709ec68f74f7b21459acd76da0e8a7c5c0f3d469f99826b0cc2380849),
and
[k3-max](buzz://message?channel=5e46055d-a766-4065-ae25-05d1e4aaa6b2&id=2ce2842910435f562e9d9cc718595848f281b122c94605e523a4b964254b8bfb)
independently returned **NO BLOCKERS** at `7eb3a650b`; k3-max also
revalidated every remediation and the endpoint-specific App Attest
enrollment bound.
---------
Signed-off-by: Tom Brow <tomb@squareup.com>
Signed-off-by: Tom Brow <tomb@block.xyz>
Co-authored-by: Tom Brow <tomb@squareup.com>
Co-authored-by: Codex <noreply@openai.com>
Co-authored-by: Jordan Mecom <jm@squareup.com>
659 lines
20 KiB
Dart
659 lines
20 KiB
Dart
import 'dart:convert';
|
|
import 'dart:math';
|
|
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:nostr/nostr.dart' as nostr;
|
|
|
|
import '../crypto/nip44.dart';
|
|
import '../relay/nostr_models.dart';
|
|
import '../relay/signed_event_relay.dart';
|
|
import 'push_bridge.dart';
|
|
import 'push_subscription.dart';
|
|
|
|
const buzzPushLeaseKind = 30350;
|
|
const buzzDevPushAppProfile = 'buzz-ios-dogfood';
|
|
const buzzPushTransport = 'apns';
|
|
const _maxSafeJsonInteger = 9007199254740991;
|
|
const _maxLeaseLifetimeSeconds = 2592000;
|
|
const _lowercaseHex64Pattern = r'^[0-9a-f]{64}$';
|
|
const _installationIdPattern = r'^[0-9a-f]{32}$';
|
|
|
|
class BuzzPushLeaseDescriptor {
|
|
final String origin;
|
|
final String executorKeyId;
|
|
final String executorPubkey;
|
|
final String transport;
|
|
final int maxLeaseTtlSeconds;
|
|
final int maxContentLength;
|
|
final int maxPlaintextLength;
|
|
final int maxEndpointLength;
|
|
final int maxStringLength;
|
|
|
|
const BuzzPushLeaseDescriptor({
|
|
required this.origin,
|
|
required this.executorKeyId,
|
|
required this.executorPubkey,
|
|
required this.transport,
|
|
required this.maxLeaseTtlSeconds,
|
|
required this.maxContentLength,
|
|
required this.maxPlaintextLength,
|
|
required this.maxEndpointLength,
|
|
required this.maxStringLength,
|
|
});
|
|
|
|
factory BuzzPushLeaseDescriptor.fromRelayInformation(
|
|
Map<String, dynamic> information,
|
|
) {
|
|
_requireExactKeys(
|
|
information,
|
|
required: const {},
|
|
allowed: const {
|
|
'name',
|
|
'description',
|
|
'pubkey',
|
|
'contact',
|
|
'supported_nips',
|
|
'supported_extensions',
|
|
'software',
|
|
'version',
|
|
'limitation',
|
|
'retention',
|
|
'relay_countries',
|
|
'language_tags',
|
|
'tags',
|
|
'posting_policy',
|
|
'payments_url',
|
|
'fees',
|
|
'icon',
|
|
'self',
|
|
'pairing_relay_url',
|
|
'push',
|
|
},
|
|
name: 'NIP-11 document',
|
|
);
|
|
final extensions = _stringList(
|
|
information['supported_extensions'],
|
|
name: 'supported_extensions',
|
|
);
|
|
if (!extensions.contains('nip-pl')) {
|
|
throw const FormatException('NIP-11 does not advertise nip-pl');
|
|
}
|
|
|
|
final push = _stringMap(information['push'], name: 'push');
|
|
_requireExactKeys(
|
|
push,
|
|
required: const {
|
|
'origin',
|
|
'keys',
|
|
'app_profiles',
|
|
'push_kinds',
|
|
'h_grammar',
|
|
'class_support',
|
|
'limitation',
|
|
},
|
|
allowed: const {
|
|
'origin',
|
|
'keys',
|
|
'app_profiles',
|
|
'push_kinds',
|
|
'h_grammar',
|
|
'class_support',
|
|
'limitation',
|
|
},
|
|
name: 'push descriptor',
|
|
);
|
|
|
|
final origin = _canonicalOrigin(push['origin']);
|
|
final keys = _mapList(push['keys'], name: 'push.keys');
|
|
final keyIds = <String>{};
|
|
final currentKeys = <Map<String, dynamic>>[];
|
|
for (final key in keys) {
|
|
_requireExactKeys(
|
|
key,
|
|
required: const {'id', 'pubkey'},
|
|
allowed: const {'id', 'pubkey', 'current', 'retiring'},
|
|
name: 'push key',
|
|
);
|
|
final id = _nonEmptyString(key['id'], name: 'push key id');
|
|
if (!keyIds.add(id)) {
|
|
throw FormatException('Duplicate push key id: $id');
|
|
}
|
|
_lowercaseHex64(key['pubkey'], name: 'push key pubkey');
|
|
final current = key['current'];
|
|
final retiring = key['retiring'];
|
|
if (current != null && current is! bool) {
|
|
throw const FormatException('push key current must be a boolean');
|
|
}
|
|
if (retiring != null && retiring is! bool) {
|
|
throw const FormatException('push key retiring must be a boolean');
|
|
}
|
|
if (current == true) currentKeys.add(key);
|
|
}
|
|
if (currentKeys.length != 1) {
|
|
throw const FormatException(
|
|
'push descriptor must contain exactly one current key',
|
|
);
|
|
}
|
|
final currentKey = currentKeys.single;
|
|
|
|
final profiles = _mapList(push['app_profiles'], name: 'app_profiles');
|
|
final profileIds = <String>{};
|
|
String? transport;
|
|
for (final profile in profiles) {
|
|
_requireExactKeys(
|
|
profile,
|
|
required: const {'id', 'transport'},
|
|
allowed: const {'id', 'transport'},
|
|
name: 'app profile',
|
|
);
|
|
final id = _nonEmptyString(profile['id'], name: 'app profile id');
|
|
if (!profileIds.add(id)) {
|
|
throw FormatException('Duplicate app profile id: $id');
|
|
}
|
|
final candidate = _nonEmptyString(
|
|
profile['transport'],
|
|
name: 'app profile transport',
|
|
);
|
|
if (id == buzzDevPushAppProfile) transport = candidate;
|
|
}
|
|
if (transport != buzzPushTransport) {
|
|
throw const FormatException(
|
|
'NIP-11 does not advertise the dogfood APNs profile',
|
|
);
|
|
}
|
|
|
|
final pushKinds = _intList(push['push_kinds'], name: 'push_kinds');
|
|
if (!buzzPushEligibleKinds.every(pushKinds.contains)) {
|
|
throw const FormatException(
|
|
'NIP-11 does not advertise every Buzz message kind for push',
|
|
);
|
|
}
|
|
final hGrammar = _nonEmptyString(push['h_grammar'], name: 'h_grammar');
|
|
if (hGrammar != 'uuid-v4-lowercase') {
|
|
throw const FormatException('Unsupported push h_grammar');
|
|
}
|
|
|
|
final classSupport = _stringMap(
|
|
push['class_support'],
|
|
name: 'class_support',
|
|
);
|
|
final supportedClasses = _stringList(
|
|
classSupport[buzzPushTransport],
|
|
name: 'class_support.apns',
|
|
);
|
|
const knownClasses = {'default'};
|
|
if (supportedClasses.any((value) => !knownClasses.contains(value))) {
|
|
throw const FormatException('class_support contains an unknown class');
|
|
}
|
|
if (!supportedClasses.contains('default')) {
|
|
throw const FormatException('APNs does not support the default class');
|
|
}
|
|
|
|
final limitation = _stringMap(push['limitation'], name: 'limitation');
|
|
_requireExactKeys(
|
|
limitation,
|
|
required: const {
|
|
'max_lease_ttl',
|
|
'max_leases_per_pubkey',
|
|
'max_subscriptions_per_lease',
|
|
'max_kinds',
|
|
'max_authors',
|
|
'max_h',
|
|
'max_tag_values',
|
|
'max_ignore',
|
|
'max_content_len',
|
|
'max_plaintext_len',
|
|
'max_endpoint_len',
|
|
'max_string_len',
|
|
},
|
|
allowed: const {
|
|
'max_lease_ttl',
|
|
'max_leases_per_pubkey',
|
|
'max_subscriptions_per_lease',
|
|
'max_kinds',
|
|
'max_authors',
|
|
'max_h',
|
|
'max_tag_values',
|
|
'max_ignore',
|
|
'max_content_len',
|
|
'max_plaintext_len',
|
|
'max_endpoint_len',
|
|
'max_string_len',
|
|
},
|
|
name: 'push limitation',
|
|
);
|
|
for (final entry in limitation.entries) {
|
|
_positiveInt(entry.value, name: 'limitation.${entry.key}');
|
|
}
|
|
final maxStringLength = limitation['max_string_len'] as int;
|
|
_checkStringLength(origin, maxStringLength, name: 'origin');
|
|
_checkStringLength(
|
|
currentKey['id'] as String,
|
|
maxStringLength,
|
|
name: 'push key id',
|
|
);
|
|
final maxLeaseTtl = limitation['max_lease_ttl'] as int;
|
|
if (maxLeaseTtl > _maxLeaseLifetimeSeconds) {
|
|
throw const FormatException('max_lease_ttl exceeds the NIP-PL v1 limit');
|
|
}
|
|
|
|
return BuzzPushLeaseDescriptor(
|
|
origin: origin,
|
|
executorKeyId: currentKey['id'] as String,
|
|
executorPubkey: currentKey['pubkey'] as String,
|
|
transport: transport!,
|
|
maxLeaseTtlSeconds: maxLeaseTtl,
|
|
maxContentLength: limitation['max_content_len'] as int,
|
|
maxPlaintextLength: limitation['max_plaintext_len'] as int,
|
|
maxEndpointLength: limitation['max_endpoint_len'] as int,
|
|
maxStringLength: maxStringLength,
|
|
);
|
|
}
|
|
}
|
|
|
|
Future<BuzzPushLeaseDescriptor> fetchBuzzPushLeaseDescriptor(
|
|
String relayBaseUrl, {
|
|
http.Client? client,
|
|
Duration timeout = const Duration(seconds: 8),
|
|
}) async {
|
|
final uri = Uri.tryParse(relayBaseUrl);
|
|
if (uri == null ||
|
|
!const {'http', 'https'}.contains(uri.scheme) ||
|
|
uri.host.isEmpty) {
|
|
throw FormatException('Invalid relay HTTP URL: $relayBaseUrl');
|
|
}
|
|
final requestUri = uri.resolve('/');
|
|
final ownedClient = client ?? http.Client();
|
|
try {
|
|
final response = await ownedClient
|
|
.get(requestUri, headers: const {'Accept': 'application/nostr+json'})
|
|
.timeout(timeout);
|
|
if (response.statusCode != 200) {
|
|
throw StateError(
|
|
'NIP-11 request failed with HTTP ${response.statusCode}: ${response.body}',
|
|
);
|
|
}
|
|
final decoded = jsonDecode(response.body);
|
|
if (decoded is! Map<String, dynamic>) {
|
|
throw const FormatException('NIP-11 response must be a JSON object');
|
|
}
|
|
return BuzzPushLeaseDescriptor.fromRelayInformation(decoded);
|
|
} finally {
|
|
if (client == null) ownedClient.close();
|
|
}
|
|
}
|
|
|
|
class BuzzPushLeasePublication {
|
|
final String eventId;
|
|
final int expiration;
|
|
final String plaintext;
|
|
|
|
const BuzzPushLeasePublication({
|
|
required this.eventId,
|
|
required this.expiration,
|
|
required this.plaintext,
|
|
});
|
|
}
|
|
|
|
typedef BuzzPushLeaseSubmit =
|
|
Future<NostrEvent> Function({
|
|
required int kind,
|
|
required String content,
|
|
required List<List<String>> tags,
|
|
int? createdAt,
|
|
});
|
|
|
|
Future<BuzzPushLeasePublication> publishBuzzDevPushLease({
|
|
required BuzzPushEndpointGrant grant,
|
|
String? leaseInstallationId,
|
|
int? leaseGeneration,
|
|
required BuzzPushLeaseDescriptor descriptor,
|
|
required String nsec,
|
|
required String memberPubkey,
|
|
required List<BuzzPushSubscription> subscriptions,
|
|
required BuzzPushLeaseSubmit submit,
|
|
DateTime Function() now = DateTime.now,
|
|
}) async {
|
|
_validateGrant(grant, descriptor);
|
|
final effectiveLeaseInstallationId =
|
|
leaseInstallationId ?? grant.installationId;
|
|
if (!RegExp(_installationIdPattern).hasMatch(effectiveLeaseInstallationId)) {
|
|
throw const FormatException(
|
|
'Lease installation id must be 16 random bytes encoded as lowercase hex',
|
|
);
|
|
}
|
|
final effectiveLeaseGeneration = leaseGeneration ?? grant.generation;
|
|
if (effectiveLeaseGeneration <= 0 ||
|
|
effectiveLeaseGeneration > _maxSafeJsonInteger) {
|
|
throw const FormatException('Lease generation is invalid');
|
|
}
|
|
final normalizedMemberPubkey = _lowercaseHex64(
|
|
memberPubkey,
|
|
name: 'member pubkey',
|
|
);
|
|
final decoded = nostr.Nip19.decode(payload: nsec);
|
|
if (decoded.prefix != nostr.Nip19Prefix.nsec || decoded.data.length != 64) {
|
|
throw const FormatException('Signing key must be a 32-byte nsec');
|
|
}
|
|
final signingPubkey = nostr.Keys(decoded.data).public;
|
|
if (signingPubkey != normalizedMemberPubkey) {
|
|
throw const FormatException(
|
|
'Authenticated signing key does not match the lease member pubkey',
|
|
);
|
|
}
|
|
|
|
final nowSeconds = now().millisecondsSinceEpoch ~/ 1000;
|
|
final expiration = min(
|
|
grant.expiresAt,
|
|
nowSeconds + descriptor.maxLeaseTtlSeconds,
|
|
);
|
|
if (expiration <= nowSeconds) {
|
|
throw const FormatException('Endpoint grant is already expired');
|
|
}
|
|
|
|
final plaintextMap = <String, dynamic>{
|
|
'v': 1,
|
|
'origin': descriptor.origin,
|
|
'app_profile': grant.appProfile,
|
|
'transport': descriptor.transport,
|
|
'endpoint': grant.endpointGrant,
|
|
'generation': effectiveLeaseGeneration,
|
|
'active': true,
|
|
'subscriptions': [
|
|
for (final subscription in subscriptions) subscription.toJson(),
|
|
],
|
|
};
|
|
final plaintext = jsonEncode(plaintextMap);
|
|
if (utf8.encode(plaintext).length > descriptor.maxPlaintextLength) {
|
|
throw const FormatException('lease plaintext exceeds the advertised limit');
|
|
}
|
|
final conversationKey = getConversationKey(
|
|
decoded.data,
|
|
descriptor.executorPubkey,
|
|
);
|
|
final content = nip44Encrypt(conversationKey, plaintext);
|
|
if (utf8.encode(content).length > descriptor.maxContentLength) {
|
|
throw const FormatException(
|
|
'lease ciphertext exceeds the advertised limit',
|
|
);
|
|
}
|
|
final acknowledged = await submit(
|
|
kind: buzzPushLeaseKind,
|
|
content: content,
|
|
tags: [
|
|
['d', effectiveLeaseInstallationId],
|
|
['expiration', '$expiration'],
|
|
['exec', descriptor.executorKeyId],
|
|
],
|
|
createdAt: nowSeconds,
|
|
);
|
|
if (acknowledged.id.isEmpty) {
|
|
throw StateError('Relay returned an empty event id for the push lease');
|
|
}
|
|
return BuzzPushLeasePublication(
|
|
eventId: acknowledged.id,
|
|
expiration: expiration,
|
|
plaintext: plaintext,
|
|
);
|
|
}
|
|
|
|
Future<BuzzPushLeasePublication> publishBuzzDevPushLeaseThroughRelay({
|
|
required BuzzPushEndpointGrant grant,
|
|
String? leaseInstallationId,
|
|
int? leaseGeneration,
|
|
required BuzzPushLeaseDescriptor descriptor,
|
|
required String nsec,
|
|
required String memberPubkey,
|
|
required List<BuzzPushSubscription> subscriptions,
|
|
required SignedEventRelay relay,
|
|
DateTime Function() now = DateTime.now,
|
|
}) => publishBuzzDevPushLease(
|
|
grant: grant,
|
|
leaseInstallationId: leaseInstallationId,
|
|
leaseGeneration: leaseGeneration,
|
|
descriptor: descriptor,
|
|
nsec: nsec,
|
|
memberPubkey: memberPubkey,
|
|
subscriptions: subscriptions,
|
|
submit: relay.submit,
|
|
now: now,
|
|
);
|
|
|
|
/// Publishes the minimal higher-generation inactive lease used when a
|
|
/// community is removed from this device. Gateway delegation is intentionally
|
|
/// untouched because it is scoped to the installation and relay key, not the
|
|
/// community.
|
|
Future<BuzzPushLeasePublication> publishBuzzPushLeaseTombstone({
|
|
required BuzzPushLeaseDescriptor descriptor,
|
|
required String installationId,
|
|
required int generation,
|
|
required String nsec,
|
|
required String memberPubkey,
|
|
required BuzzPushLeaseSubmit submit,
|
|
DateTime Function() now = DateTime.now,
|
|
}) async {
|
|
if (!RegExp(_installationIdPattern).hasMatch(installationId)) {
|
|
throw const FormatException(
|
|
'Installation id must be 16 random bytes encoded as lowercase hex',
|
|
);
|
|
}
|
|
if (generation <= 0 || generation > _maxSafeJsonInteger) {
|
|
throw const FormatException('Tombstone generation is invalid');
|
|
}
|
|
final normalizedMemberPubkey = _lowercaseHex64(
|
|
memberPubkey,
|
|
name: 'member pubkey',
|
|
);
|
|
final decoded = nostr.Nip19.decode(payload: nsec);
|
|
if (decoded.prefix != nostr.Nip19Prefix.nsec || decoded.data.length != 64) {
|
|
throw const FormatException('Signing key must be a 32-byte nsec');
|
|
}
|
|
if (nostr.Keys(decoded.data).public != normalizedMemberPubkey) {
|
|
throw const FormatException(
|
|
'Authenticated signing key does not match the lease member pubkey',
|
|
);
|
|
}
|
|
|
|
final nowSeconds = now().millisecondsSinceEpoch ~/ 1000;
|
|
final expiration = nowSeconds + descriptor.maxLeaseTtlSeconds;
|
|
final plaintextMap = <String, dynamic>{
|
|
'v': 1,
|
|
'origin': descriptor.origin,
|
|
'generation': generation,
|
|
'active': false,
|
|
};
|
|
final plaintext = jsonEncode(plaintextMap);
|
|
if (utf8.encode(plaintext).length > descriptor.maxPlaintextLength) {
|
|
throw const FormatException('lease plaintext exceeds the advertised limit');
|
|
}
|
|
final content = nip44Encrypt(
|
|
getConversationKey(decoded.data, descriptor.executorPubkey),
|
|
plaintext,
|
|
);
|
|
if (utf8.encode(content).length > descriptor.maxContentLength) {
|
|
throw const FormatException(
|
|
'lease ciphertext exceeds the advertised limit',
|
|
);
|
|
}
|
|
final acknowledged = await submit(
|
|
kind: buzzPushLeaseKind,
|
|
content: content,
|
|
tags: [
|
|
['d', installationId],
|
|
['expiration', '$expiration'],
|
|
['exec', descriptor.executorKeyId],
|
|
],
|
|
createdAt: nowSeconds,
|
|
);
|
|
if (acknowledged.id.isEmpty) {
|
|
throw StateError('Relay returned an empty event id for the push tombstone');
|
|
}
|
|
return BuzzPushLeasePublication(
|
|
eventId: acknowledged.id,
|
|
expiration: expiration,
|
|
plaintext: plaintext,
|
|
);
|
|
}
|
|
|
|
Future<BuzzPushLeasePublication> publishBuzzPushLeaseTombstoneThroughRelay({
|
|
required BuzzPushLeaseDescriptor descriptor,
|
|
required String installationId,
|
|
required int generation,
|
|
required String nsec,
|
|
required String memberPubkey,
|
|
required SignedEventRelay relay,
|
|
DateTime Function() now = DateTime.now,
|
|
}) => publishBuzzPushLeaseTombstone(
|
|
descriptor: descriptor,
|
|
installationId: installationId,
|
|
generation: generation,
|
|
nsec: nsec,
|
|
memberPubkey: memberPubkey,
|
|
submit: relay.submit,
|
|
now: now,
|
|
);
|
|
|
|
void _validateGrant(
|
|
BuzzPushEndpointGrant grant,
|
|
BuzzPushLeaseDescriptor descriptor,
|
|
) {
|
|
if (utf8.encode(grant.installationId).length > 64 ||
|
|
!RegExp(_installationIdPattern).hasMatch(grant.installationId)) {
|
|
throw const FormatException(
|
|
'Installation id must be 16 random bytes encoded as lowercase hex',
|
|
);
|
|
}
|
|
if (grant.relayPubkey != descriptor.executorPubkey) {
|
|
throw const FormatException(
|
|
'Stored endpoint grant is delegated to a different relay key',
|
|
);
|
|
}
|
|
if (grant.appProfile != buzzDevPushAppProfile) {
|
|
throw const FormatException('Endpoint grant is not for buzz-ios-dogfood');
|
|
}
|
|
if (grant.endpointGrant.isEmpty ||
|
|
utf8.encode(grant.endpointGrant).length > descriptor.maxEndpointLength) {
|
|
throw const FormatException(
|
|
'Endpoint grant violates the advertised endpoint limit',
|
|
);
|
|
}
|
|
if (grant.endpointEpoch <= 0) {
|
|
throw const FormatException('Endpoint grant epoch is invalid');
|
|
}
|
|
if (grant.generation <= 0 || grant.generation > _maxSafeJsonInteger) {
|
|
throw const FormatException('Endpoint grant generation is invalid');
|
|
}
|
|
}
|
|
|
|
String _canonicalOrigin(Object? value) {
|
|
final origin = _nonEmptyString(value, name: 'origin');
|
|
final uri = Uri.tryParse(origin);
|
|
if (uri == null ||
|
|
!const {'ws', 'wss'}.contains(uri.scheme) ||
|
|
uri.host.isEmpty ||
|
|
uri.userInfo.isNotEmpty ||
|
|
uri.path.isNotEmpty ||
|
|
uri.hasQuery ||
|
|
uri.hasFragment ||
|
|
'${uri.scheme}://${uri.authority}' != origin) {
|
|
throw FormatException('Invalid canonical push origin: $origin');
|
|
}
|
|
return origin;
|
|
}
|
|
|
|
void _checkStringLength(String value, int maximum, {required String name}) {
|
|
if (maximum <= 0 || utf8.encode(value).length > maximum) {
|
|
throw FormatException('$name exceeds its advertised byte limit');
|
|
}
|
|
}
|
|
|
|
String _lowercaseHex64(Object? value, {required String name}) {
|
|
final text = _nonEmptyString(value, name: name);
|
|
if (!RegExp(_lowercaseHex64Pattern).hasMatch(text)) {
|
|
throw FormatException('$name must be exactly 64 lowercase hex characters');
|
|
}
|
|
return text;
|
|
}
|
|
|
|
String _nonEmptyString(Object? value, {required String name}) {
|
|
if (value is! String || value.isEmpty) {
|
|
throw FormatException('$name must be a non-empty string');
|
|
}
|
|
return value;
|
|
}
|
|
|
|
int _positiveInt(Object? value, {required String name}) {
|
|
if (value is! int || value <= 0) {
|
|
throw FormatException('$name must be a positive integer');
|
|
}
|
|
return value;
|
|
}
|
|
|
|
Map<String, dynamic> _stringMap(Object? value, {required String name}) {
|
|
if (value is! Map<String, dynamic>) {
|
|
throw FormatException('$name must be an object');
|
|
}
|
|
return value;
|
|
}
|
|
|
|
List<Map<String, dynamic>> _mapList(Object? value, {required String name}) {
|
|
if (value is! List || value.isEmpty) {
|
|
throw FormatException('$name must be a non-empty array');
|
|
}
|
|
return [
|
|
for (final item in value)
|
|
if (item is Map<String, dynamic>)
|
|
item
|
|
else
|
|
throw FormatException('$name entries must be objects'),
|
|
];
|
|
}
|
|
|
|
List<String> _stringList(Object? value, {required String name}) {
|
|
if (value is! List || value.isEmpty) {
|
|
throw FormatException('$name must be a non-empty string array');
|
|
}
|
|
return [
|
|
for (final item in value)
|
|
if (item is String && item.isNotEmpty)
|
|
item
|
|
else
|
|
throw FormatException('$name entries must be non-empty strings'),
|
|
];
|
|
}
|
|
|
|
List<int> _intList(
|
|
Object? value, {
|
|
required String name,
|
|
bool allowEmpty = false,
|
|
}) {
|
|
if (value is! List || (!allowEmpty && value.isEmpty)) {
|
|
throw FormatException(
|
|
'$name must be ${allowEmpty ? 'an' : 'a non-empty'} integer array',
|
|
);
|
|
}
|
|
return [
|
|
for (final item in value)
|
|
if (item is int && item >= 0)
|
|
item
|
|
else
|
|
throw FormatException('$name entries must be non-negative integers'),
|
|
];
|
|
}
|
|
|
|
void _requireExactKeys(
|
|
Map<String, dynamic> value, {
|
|
required Set<String> required,
|
|
required Set<String> allowed,
|
|
required String name,
|
|
}) {
|
|
final missing = required.difference(value.keys.toSet());
|
|
if (missing.isNotEmpty) {
|
|
throw FormatException('$name is missing ${missing.join(', ')}');
|
|
}
|
|
final unexpected = value.keys.toSet().difference(allowed);
|
|
if (unexpected.isNotEmpty) {
|
|
throw FormatException('$name contains unknown field ${unexpected.first}');
|
|
}
|
|
}
|