From aec073a609e266411815b021db2dd510bd4c72b3 Mon Sep 17 00:00:00 2001 From: ethicnology Date: Fri, 27 Jan 2023 23:03:07 +0200 Subject: [PATCH] refactor: json_annotation --- lib/src/event.dart | 40 ++++++++++++++++++++-------------------- lib/src/event.g.dart | 40 ++++++++++++++++++++++++++++++++++++++++ pubspec.yaml | 4 ++++ 3 files changed, 64 insertions(+), 20 deletions(-) create mode 100644 lib/src/event.g.dart diff --git a/lib/src/event.dart b/lib/src/event.dart index 8cedbfb..4fa7e7b 100644 --- a/lib/src/event.dart +++ b/lib/src/event.dart @@ -3,6 +3,9 @@ import 'package:convert/convert.dart'; import 'package:crypto/crypto.dart'; import 'package:bip340/bip340.dart' as bip340; import 'package:nostr/src/utils.dart'; +import 'package:json_annotation/json_annotation.dart'; + +part 'event.g.dart'; /// The only object type that exists is the event, which has the following format on the wire: /// @@ -16,6 +19,7 @@ import 'package:nostr/src/utils.dart'; /// ], /// - "content": "arbitrary string", /// - "sig": "64-bytes signature of the sha256 hash of the serialized event data, which is the same as the 'id' field" +@JsonSerializable() class Event { /// 32-bytes hex-encoded sha256 of the the serialized event data (hex) late String id; @@ -24,6 +28,9 @@ class Event { late String pubkey; /// unix timestamp in seconds + @JsonKey( + name: 'created_at', + ) late int createdAt; /// - 0: set_metadata: the content is set to a stringified JSON object {name: , about: , picture: } describing the user who created the event. A relay may delete past set_metadata events once it gets a new one for the same pubkey. @@ -43,6 +50,7 @@ class Event { late String sig; /// subscription_id is a random string that should be used to represent a subscription. + @JsonKey(includeIfNull: false, toJson: null) String? subscriptionId; /// Default constructor @@ -82,18 +90,12 @@ class Event { } /// Deserialize an event from a JSON - Event.fromJson(Map json, {this.subscriptionId}) { - id = json['id']; - pubkey = json['pubkey']; - createdAt = json['created_at']; - kind = json['kind']; - tags = json['tags'].cast>(); - content = json['content']; - sig = json['sig']; - } + factory Event.fromJson(Map json) => _$EventFromJson(json); /// Serialize an event in JSON - Map toJson() => { + Map toJson() => _$EventToJson(this); + + Map toJson2() => { 'id': id, 'pubkey': pubkey, 'created_at': createdAt, @@ -117,23 +119,17 @@ class Event { /// Deserialize a nostr event message /// - ["EVENT", event JSON as defined above] /// - ["EVENT", subscription_id, event JSON as defined above] - Event.deserialize(input) { + factory Event.deserialize(input) { Map json = {}; if (input.length == 2) { json = input[1] as Map; } else if (input.length == 3) { json = input[2] as Map; - subscriptionId = input[1]; + json['subscriptionId'] = input[1]; } else { throw Exception('invalid input'); } - id = json['id']; - pubkey = json['pubkey']; - createdAt = json['created_at']; - kind = json['kind']; - tags = json['tags'].cast>(); - content = json['content']; - sig = json['sig']; + return _$EventFromJson(json); } /// To obtain the event.id, we sha256 the serialized event. @@ -148,10 +144,14 @@ class Event { /// ///] String getEventId() { + // FIXME since json_annotation getEventId generates sometimes wrong ID + print('$id $pubkey $createdAt $kind $tags $content $sig'); List data = [0, pubkey.toLowerCase(), createdAt, kind, tags, content]; String serializedEvent = json.encode(data); List hash = sha256.convert(utf8.encode(serializedEvent)).bytes; - return hex.encode(hash); + var tmp = hex.encode(hash); + print(tmp); + return tmp; } /// Each user has a keypair. Signatures, public key, and encodings are done according to the Schnorr signatures standard for the curve secp256k1 diff --git a/lib/src/event.g.dart b/lib/src/event.g.dart new file mode 100644 index 0000000..687536b --- /dev/null +++ b/lib/src/event.g.dart @@ -0,0 +1,40 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'event.dart'; + +// ************************************************************************** +// JsonSerializableGenerator +// ************************************************************************** + +Event _$EventFromJson(Map json) => Event( + json['id'] as String, + json['pubkey'] as String, + json['created_at'] as int, + json['kind'] as int, + (json['tags'] as List) + .map((e) => (e as List).map((e) => e as String).toList()) + .toList(), + json['content'] as String, + json['sig'] as String, + )..subscriptionId = json['subscriptionId'] as String?; + +Map _$EventToJson(Event instance) { + final val = { + 'id': instance.id, + 'pubkey': instance.pubkey, + 'created_at': instance.createdAt, + 'kind': instance.kind, + 'tags': instance.tags, + 'content': instance.content, + 'sig': instance.sig, + }; + + void writeNotNull(String key, dynamic value) { + if (value != null) { + val[key] = value; + } + } + + writeNotNull('subscriptionId', instance.subscriptionId); + return val; +} diff --git a/pubspec.yaml b/pubspec.yaml index 336e09b..45ee50e 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -9,7 +9,11 @@ environment: dev_dependencies: lints: ^2.0.0 test: ^1.16.0 + build_runner: ^2.3.3 + json_serializable: ^6.6.0 + dependencies: bip340: ^0.0.4 convert: ^3.1.1 crypto: ^3.0.2 + json_annotation: ^4.8.0