Skip to content

Commit

Permalink
refactor: PR #26 wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ethicnology committed Apr 20, 2023
1 parent cc429de commit 933a384
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 41 deletions.
23 changes: 4 additions & 19 deletions lib/src/event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<String>> tags,
List<List<String>> 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(
Expand Down Expand Up @@ -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:
///
Expand Down
6 changes: 3 additions & 3 deletions lib/src/nips/nip_004/crypto.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<int>> byteSecret = gMapByteSecret[pubkey] ?? [];
if (byteSecret.isEmpty) {
Expand Down
32 changes: 13 additions & 19 deletions lib/src/nips/nip_004/event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, List<List<int>>> gMapByteSecret = {};

EncryptedDirectMessage(Event event)
: super(
event.id,
Expand Down Expand Up @@ -39,28 +37,24 @@ class EncryptedDirectMessage extends Event {

String? get receiverPubkey => findPubkey();

String get nonce {
List<String> 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;
}
Expand Down

0 comments on commit 933a384

Please sign in to comment.