Skip to content

Commit

Permalink
fix: decipher bug, add Event.quick (#26)
Browse files Browse the repository at this point in the history
* fix encrypt bug, add Event.quick

* a send and a receive unit test
  • Loading branch information
ntheden authored and ethicnology committed Apr 20, 2023
1 parent 385f211 commit cc429de
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 54 deletions.
16 changes: 15 additions & 1 deletion lib/src/event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ class Event {
throw Exception('invalid input');
}

var tags = (json['tags'] as List<dynamic>)
List<List<String>> tags = (json['tags'] as List<dynamic>)
.map((e) => (e as List<dynamic>).map((e) => e as String).toList())
.toList();

Expand All @@ -271,6 +271,20 @@ 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
3 changes: 1 addition & 2 deletions lib/src/nips/nip_004/crypto.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ class Nip4 {
return outputPlainText;
}

// Decrypt data using self private key
static String decipher(
String privkey,
String pubkey,
Expand Down Expand Up @@ -101,6 +100,6 @@ class Nip4 {
//remove padding
offset += cipherImpl.doFinal(cipherText, offset, finalPlainText, offset);
Uint8List result = finalPlainText.sublist(0, offset);
return Utf8Decoder().convert(result.toList());
return Utf8Decoder().convert(result);
}
}
20 changes: 18 additions & 2 deletions lib/src/nips/nip_004/event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,24 @@ class EncryptedDirectMessage extends Event {
return ciphertext;
}

String getPlaintext(String receiverPrivkey, String senderPubkey) {
return Nip4.decipher(receiverPrivkey, senderPubkey, content);
String getPlaintext(String receiverPrivkey, [String senderPubkey=""]) {
if (senderPubkey.length == 0) {
senderPubkey = pubkey;
}
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}");
}
return plaintext;
}

String? findPubkey() {
Expand Down
94 changes: 45 additions & 49 deletions test/nips/nip_004_test.dart
Original file line number Diff line number Diff line change
@@ -1,59 +1,55 @@
import 'package:nostr/nostr.dart';
import 'package:test/test.dart';

String bobPubkey =
"2d38a56c4303bc722370c50c86fc8dd3327f06a8fe59b3ff3d670738d71dd1e1";
String alicePubkey =
"0f76c800a7ea76b83a3ae87de94c6046b98311bda8885cedd8420885b50de181";
String bobPrivkey =
"826ef0e93c1278bd89945377fadb6b6b51d9eedf74ecdb64a96f1897bb670be8";
String alicePrivkey =
"773dc29ff81f7680eeca5d530f528e8c572979b46abc8bfd1586b73a6a98ab4d";

void main() {
group('EncryptedDirectMessage', () {
// test('EncryptedDirectMessage.newEvent', () {
// // DM from bob to alice
// String bobPubKey =
// "2d38a56c4303bc722370c50c86fc8dd3327f06a8fe59b3ff3d670738d71dd1e1";
// String alicePubKey =
// "0f76c800a7ea76b83a3ae87de94c6046b98311bda8885cedd8420885b50de181";
// String bobPrivKey =
// "826ef0e93c1278bd89945377fadb6b6b51d9eedf74ecdb64a96f1897bb670be8";
// String plaintext = "vi veri universum vivus vici";
// List<List<String>> tags = [
// ["p", alicePubKey]
// ];
test('EncryptedDirectMessage.quick', () {
// DM from bob to alice
String plaintext = "vi veri universum vivus vici";
List<List<String>> tags = [
['p', alicePubkey]
];

// EncryptedDirectMessage event =
// EncryptedDirectMessage.newEvent(alicePubKey, plaintext, bobPrivKey);
EncryptedDirectMessage event =
EncryptedDirectMessage.quick(bobPrivkey, alicePubkey, plaintext);

// expect(event.peerPubkey, alicePubKey);
// expect(event.plaintext, plaintext);
// expect(event.pubkey, bobPubKey);
// expect(event.kind, 4);
// expect(event.tags, tags);
// expect(event.subscriptionId, null);
// });
// });
// test('Encrypted DM Receive', () {
// String bobPubKey =
// "2d38a56c4303bc722370c50c86fc8dd3327f06a8fe59b3ff3d670738d71dd1e1";
// String alicePubKey =
// "0f76c800a7ea76b83a3ae87de94c6046b98311bda8885cedd8420885b50de181";
// String bobPrivKey =
// "826ef0e93c1278bd89945377fadb6b6b51d9eedf74ecdb64a96f1897bb670be8";
// String json =
// '["EVENT", "181555e0fec2139d27ac80a5a46801415394e61d67be7f626631760dc5997bc0", {"id": "2739cccdc3fa943ad447378e234ef1325a76f023a169b483b6fe8cab47a793e1", "pubkey": "0f76c800a7ea76b83a3ae87de94c6046b98311bda8885cedd8420885b50de181", "created_at": 1680475069, "kind": 4, "tags": [["p", "2d38a56c4303bc722370c50c86fc8dd3327f06a8fe59b3ff3d670738d71dd1e1"]], "content": "hH1HlQWY3dz7IzJlgnEgW1WNtA0KlvGgo/OC4mep/R4I6PMqJuvZ35j4OFHkMvqb?iv=jbkXPH2esn5DIP3BodpsTQ==", "sig": "ba2d80d8be4612e8438447d38373e9b014b28172d49b7b6afeff462bc25bd4edc69c6e2c145dde052de54f47f4e36d74151fad3d08d21e3cad9563edc64adcca"}]';
expect(event.receiverPubkey, alicePubkey);
expect(event.getPlaintext(alicePrivkey), plaintext);
expect(event.pubkey, bobPubkey);
expect(event.kind, 4);
expect(event.tags, tags);
expect(event.subscriptionId, null);
});

// // String tempPrivateKey = userPrivateKey; // from settings.dart
// // userPrivateKey = bobPrivKey;
// // userPrivateKey = tempPrivateKey;
test('EncryptedDirectMessage Receive', () {
String receivedEvent =
'["EVENT", "181555e0fec2139d27ac80a5a46801415394e61d67be7f626631760dc5997bc0", {"id": "2739cccdc3fa943ad447378e234ef1325a76f023a169b483b6fe8cab47a793e1", "pubkey": "0f76c800a7ea76b83a3ae87de94c6046b98311bda8885cedd8420885b50de181", "created_at": 1680475069, "kind": 4, "tags": [["p", "2d38a56c4303bc722370c50c86fc8dd3327f06a8fe59b3ff3d670738d71dd1e1"]], "content": "hH1HlQWY3dz7IzJlgnEgW1WNtA0KlvGgo/OC4mep/R4I6PMqJuvZ35j4OFHkMvqb?iv=jbkXPH2esn5DIP3BodpsTQ==", "sig": "ba2d80d8be4612e8438447d38373e9b014b28172d49b7b6afeff462bc25bd4edc69c6e2c145dde052de54f47f4e36d74151fad3d08d21e3cad9563edc64adcca"}]';

// Message m = Message.deserialize(json);
// Event event = m.message;
// expect(event.id,
// "2739cccdc3fa943ad447378e234ef1325a76f023a169b483b6fe8cab47a793e1");
// expect(event.pubkey, alicePubKey);
// expect(event.createdAt, 1680475069);
// expect(event.kind, 4);
// expect(event.tags, [
// ["p", bobPubKey]
// ]);
// expect(event.content, "Secret message from alice to bob!");
// expect(event.sig,
// "ba2d80d8be4612e8438447d38373e9b014b28172d49b7b6afeff462bc25bd4edc69c6e2c145dde052de54f47f4e36d74151fad3d08d21e3cad9563edc64adcca");
// expect(event.subscriptionId,
// "181555e0fec2139d27ac80a5a46801415394e61d67be7f626631760dc5997bc0");
Message m = Message.deserialize(receivedEvent);
Event event = m.message;
expect(event.id,
"2739cccdc3fa943ad447378e234ef1325a76f023a169b483b6fe8cab47a793e1");
expect(event.pubkey, alicePubkey);
expect(event.createdAt, 1680475069);
expect(event.kind, 4);
expect(event.tags, [
["p", bobPubkey]
]);
String content = (event as EncryptedDirectMessage).getPlaintext(bobPrivkey);
expect(content, "Secret message from alice to bob!");
expect(event.sig,
"ba2d80d8be4612e8438447d38373e9b014b28172d49b7b6afeff462bc25bd4edc69c6e2c145dde052de54f47f4e36d74151fad3d08d21e3cad9563edc64adcca");
expect(event.subscriptionId,
"181555e0fec2139d27ac80a5a46801415394e61d67be7f626631760dc5997bc0");
});
});
}

0 comments on commit cc429de

Please sign in to comment.