diff --git a/lib/nostr.dart b/lib/nostr.dart index ece862d..038fb49 100644 --- a/lib/nostr.dart +++ b/lib/nostr.dart @@ -4,6 +4,7 @@ library nostr; export 'src/event.dart'; +export 'src/eose.dart'; export 'src/keychain.dart'; export 'src/request.dart'; export 'src/filter.dart'; diff --git a/lib/src/eose.dart b/lib/src/eose.dart new file mode 100644 index 0000000..93ffafe --- /dev/null +++ b/lib/src/eose.dart @@ -0,0 +1,26 @@ +import 'dart:convert'; + +/// Indicates "end of stored events" +/// +/// this class is mostly for getting subscription id +class Eose { + /// default constructor + Eose(this.subscriptionId); + + /// subscription_id is a random string that should be used to represent a subscription. + final String subscriptionId; + + /// Serialize to nostr close message + /// - ["EOSE", subscription_id] + String serialize() { + return jsonEncode(["EOSE", subscriptionId]); + } + + /// Deserialize a nostr close message + /// - ["CLOSE", subscription_id] + factory Eose.deserialize(input) { + if (input is! List) throw 'Invalid type for EOSE message'; + if (input.length != 2) throw 'Invalid length for EOSE message'; + return Eose(input[1]); + } +} diff --git a/lib/src/message.dart b/lib/src/message.dart index 3eb26ea..96c6846 100644 --- a/lib/src/message.dart +++ b/lib/src/message.dart @@ -32,6 +32,9 @@ class Message { case MessageType.close: message = Close.deserialize(data); break; + case MessageType.eose: + message = Eose.deserialize(data); + break; default: message = jsonEncode(data.sublist(1)); break; diff --git a/test/eose_test.dart b/test/eose_test.dart new file mode 100644 index 0000000..9c67b6d --- /dev/null +++ b/test/eose_test.dart @@ -0,0 +1,26 @@ +import 'package:nostr/nostr.dart'; +import 'package:test/test.dart'; + +void main() { + group('Eose', () { + test('Constructor', () { + String subscriptionId = generate64RandomHexChars(); + var eose = Eose(subscriptionId); + expect(eose.subscriptionId, subscriptionId); + }); + + test('Eose.serialize', () { + String subscriptionId = generate64RandomHexChars(); + String serialized = '["EOSE","$subscriptionId"]'; + var eose = Eose(subscriptionId); + expect(eose.serialize(), serialized); + }); + + test('Eose.deserialize', () { + String subscriptionId = generate64RandomHexChars(); + var serialized = ["EOSE", subscriptionId]; + var eose = Eose.deserialize(serialized); + expect(eose.subscriptionId, subscriptionId); + }); + }); +} diff --git a/test/message_test.dart b/test/message_test.dart index 6c2929f..701b533 100644 --- a/test/message_test.dart +++ b/test/message_test.dart @@ -35,6 +35,7 @@ void main() { String payload = '["EOSE", "random"]'; var msg = Message.deserialize(payload); expect(msg.type, "EOSE"); + expect(msg.message.subscriptionId, "random"); }); test('OK', () {