diff --git a/lib/src/event.dart b/lib/src/event.dart index 2748f4b..96f7c25 100644 --- a/lib/src/event.dart +++ b/lib/src/event.dart @@ -128,27 +128,26 @@ class Event { ); } - /// Instantiate Event object from the minimum available data + /// Instantiate Event object from the minimum needed data /// /// ```dart ///Event event = Event.from( /// kind: 1, - /// tags: [], /// content: "", /// privkey: /// "5ee1c8000ab28edd64d74a7d951ac2dd559814887b1b9e1ac7c5f89e96125c12", ///); ///``` factory Event.from({ - int createdAt = 0, + int? createdAt, required int kind, - required List> tags, + List> tags = const [], required String content, required String privkey, String? subscriptionId, bool verify = false, }) { - if (createdAt == 0) createdAt = currentUnixTimestampSeconds(); + createdAt ??= currentUnixTimestampSeconds(); final pubkey = bip340.getPublicKey(privkey).toLowerCase(); final id = _processEventId( @@ -271,20 +270,6 @@ class Event { ); } - factory Event.quick( - String content, - String privkey, - ) { - Event event = Event.partial(); - event.kind = 1; - event.content = content; - event.createdAt = currentUnixTimestampSeconds(); - event.pubkey = bip340.getPublicKey(privkey).toLowerCase(); - event.id = event.getEventId(); - event.sig = event.getSignature(privkey); - return event; - } - /// To obtain the event.id, we sha256 the serialized event. /// The serialization is done over the UTF-8 JSON-serialized string (with no white space or line breaks) of the following structure: /// diff --git a/lib/src/nips/nip_004/crypto.dart b/lib/src/nips/nip_004/crypto.dart index f342b87..3dbdbb0 100644 --- a/lib/src/nips/nip_004/crypto.dart +++ b/lib/src/nips/nip_004/crypto.dart @@ -64,9 +64,9 @@ class Nip4 { static String decipher( String privkey, String pubkey, - String ciphertext, [ - String nonce = "", - ]) { + String ciphertext, + String nonce, + ) { Uint8List cipherText = base64.decode(ciphertext); List> byteSecret = gMapByteSecret[pubkey] ?? []; if (byteSecret.isEmpty) { diff --git a/lib/src/nips/nip_004/event.dart b/lib/src/nips/nip_004/event.dart index f437f03..ce46119 100644 --- a/lib/src/nips/nip_004/event.dart +++ b/lib/src/nips/nip_004/event.dart @@ -4,8 +4,6 @@ import 'package:nostr/src/nips/nip_004/crypto.dart'; import 'package:nostr/src/utils.dart'; class EncryptedDirectMessage extends Event { - static Map>> gMapByteSecret = {}; - EncryptedDirectMessage(Event event) : super( event.id, @@ -39,28 +37,24 @@ class EncryptedDirectMessage extends Event { String? get receiverPubkey => findPubkey(); + String get nonce { + List split = content.split("?iv="); + if (split.length != 2) throw Exception("invalid nip4 content"); + return split[1]; + } + String getCiphertext(String senderPrivkey, String receiverPubkey) { - String ciphertext = - Nip4.cipher(senderPrivkey, '02$receiverPubkey', content); - return ciphertext; + return Nip4.cipher(senderPrivkey, '02$receiverPubkey', content); } - String getPlaintext(String receiverPrivkey, [String senderPubkey=""]) { - if (senderPubkey.length == 0) { - senderPubkey = pubkey; - } + String getPlaintext(String receiverPrivkey) { + String ciphertext = content.split("?iv=")[0]; String plaintext = ""; - int ivIndex = content.indexOf("?iv="); - if( ivIndex <= 0) { - print("Invalid content for dm, could not get ivIndex: $content"); - return plaintext; - } - String iv = content.substring(ivIndex + "?iv=".length, content.length); - String ciphertext = content.substring(0, ivIndex); try { - plaintext = Nip4.decipher(receiverPrivkey, "02$senderPubkey", ciphertext, iv); - } catch(e) { - print("Fail to decrypt: ${e}"); + plaintext = + Nip4.decipher(receiverPrivkey, "02$pubkey", ciphertext, nonce); + } catch (e) { + throw Exception("Fail to decipher: $e"); } return plaintext; }