From b7d53100c528e8c722ad276a0cf424292f77838c Mon Sep 17 00:00:00 2001 From: ice-orion <102020833+ice-orion@users.noreply.github.com> Date: Thu, 5 Dec 2024 10:49:15 +0300 Subject: [PATCH] chore!: make EventSigner sign method async --- example/nostr_dart_example.dart | 2 +- example/relay_connection_custom_settings.dart | 2 +- lib/src/crypto/key_store.dart | 4 +++- lib/src/model/event_message.dart | 7 ++++--- test/event_message_test.dart | 4 ++-- 5 files changed, 11 insertions(+), 8 deletions(-) diff --git a/example/nostr_dart_example.dart b/example/nostr_dart_example.dart index 7d83ee9..b016db9 100644 --- a/example/nostr_dart_example.dart +++ b/example/nostr_dart_example.dart @@ -33,7 +33,7 @@ void main() async { final KeyStore keyStore = KeyStore.generate(); - final EventMessage event = EventMessage.fromData( + final EventMessage event = await EventMessage.fromData( signer: keyStore, kind: 0, content: '{"name":"test"}', diff --git a/example/relay_connection_custom_settings.dart b/example/relay_connection_custom_settings.dart index e94d216..fdb3f97 100644 --- a/example/relay_connection_custom_settings.dart +++ b/example/relay_connection_custom_settings.dart @@ -17,7 +17,7 @@ void main() async { final KeyStore keyStore = KeyStore.generate(); - final EventMessage event = EventMessage.fromData( + final EventMessage event = await EventMessage.fromData( signer: keyStore, kind: 0, content: '{"name":"test"}', diff --git a/lib/src/crypto/key_store.dart b/lib/src/crypto/key_store.dart index 3dfad14..a519b2e 100644 --- a/lib/src/crypto/key_store.dart +++ b/lib/src/crypto/key_store.dart @@ -1,10 +1,12 @@ +import 'dart:async'; + import 'package:nostr_dart/src/crypto/utils.dart'; mixin EventSigner { String get publicKey; - String sign({required String message}); + FutureOr sign({required String message}); } diff --git a/lib/src/model/event_message.dart b/lib/src/model/event_message.dart index f48bd8a..1398b5e 100644 --- a/lib/src/model/event_message.dart +++ b/lib/src/model/event_message.dart @@ -1,3 +1,4 @@ +import 'dart:async'; import 'dart:convert'; import 'package:nostr_dart/nostr_dart.dart'; @@ -96,13 +97,13 @@ class EventMessage extends RelayMessage { this.subscriptionId, }); - factory EventMessage.fromData({ + static FutureOr fromData({ required EventSigner signer, required int kind, required String content, List> tags = const [], DateTime? createdAt, - }) { + }) async { createdAt ??= DateTime.now(); final String eventId = calculateEventId( @@ -120,7 +121,7 @@ class EventMessage extends RelayMessage { kind: kind, tags: tags, content: content, - sig: signer.sign(message: eventId), + sig: await signer.sign(message: eventId), ); } diff --git a/test/event_message_test.dart b/test/event_message_test.dart index d0c69a2..011933f 100644 --- a/test/event_message_test.dart +++ b/test/event_message_test.dart @@ -52,9 +52,9 @@ void main() { ); }); - test('might be instantiated with fromData constructor', () { + test('might be instantiated with fromData constructor', () async { final KeyStore keyStore = KeyStore.fromPrivate(privateKey); - final EventMessage message = EventMessage.fromData( + final EventMessage message = await EventMessage.fromData( signer: keyStore, kind: kind, content: content,