added support for writing double quotes

in social networ, and in public channel posts and replies.

Improved verify sig function, but its still not used ( and it fails for some specific events with pubkey starting with 00 and a minority of other events with special chars in them)
This commit is contained in:
Vishal 2022-11-19 21:17:39 +05:30
parent 2e207ef054
commit 0365947c3f
3 changed files with 49 additions and 28 deletions

View File

@ -47,6 +47,7 @@ String mySign(String privateKey, String msg) {
* otherwise e and p tags are found for the given event being replied to, if that event data is available
*/
Future<void> sendReplyPostLike(Store node, String replyToId, String replyKind, String content) async {
content = addEscapeChars(content);
String strTags = node.getTagStr(replyToId, exename, true);
if( replyToId.isNotEmpty && strTags == "") { // this returns empty only when the given replyto ID is non-empty, but its not found ( nor is it 64 bytes)
print("${gWarningColor}The given target id was not found and/or is not a valid id. Not sending the event.$gColorEndMarker");
@ -87,6 +88,7 @@ Future<void> sendReplyPostLike(Store node, String replyToId, String replyKind, S
// Sends a public channel message
Future<void> sendPublicChannelMessage(Store node, String channelId, String messageToSend, String replyKind) async {
messageToSend = addEscapeChars(messageToSend);
String strTags = node.getTagStr(channelId, exename);
int createdAt = DateTime.now().millisecondsSinceEpoch ~/1000;
@ -95,6 +97,7 @@ Future<void> sendPublicChannelMessage(Store node, String channelId, String messa
String sig = mySign(userPrivateKey, id);
String toSendMessage = '["EVENT",{"id":"$id","pubkey":"$userPublicKey","created_at":$createdAt,"kind":$replyKind,"tags":[$strTags],"content":"$messageToSend","sig":"$sig"}]';
//printInColor(toSendMessage, gCommentColor);
sendRequest( gListRelayUrls1, toSendMessage);
Future<void> foo() async {
@ -108,6 +111,8 @@ Future<void> sendPublicChannelMessage(Store node, String channelId, String messa
// Sends a public channel message
Future<void> sendPublicChannelReply(Store node, Channel channel, String replyTo, String messageToSend, String replyKind) async {
messageToSend = addEscapeChars(messageToSend);
String strTags = node.getTagStrForChannel(channel, replyTo, exename);
int createdAt = DateTime.now().millisecondsSinceEpoch ~/1000;
@ -126,9 +131,9 @@ Future<void> sendPublicChannelReply(Store node, Channel channel, String replyTo,
}
// send DM
Future<void> sendDirectMessage(Store node, String otherPubkey, String messageToSend) async {
//messageToSend = addEscapeChars(messageToSend); since this get encrypted , it does not need escaping
String otherPubkey02 = "02" + otherPubkey;
String encryptedMessageToSend = myEncrypt(userPrivateKey, otherPubkey02, messageToSend);
@ -810,7 +815,7 @@ Future<void> channelMenuUI(Store node) async {
} else {
// send message to the given room
print("sending message |$messageToSend|");
//print("sending message |$messageToSend|");
await sendPublicChannelMessage(node, fullChannelId, messageToSend, "42");
pageNum = 1; // reset it
@ -992,7 +997,6 @@ Future<void> addUsersToEncryptedChannel(Store node, String fullChannelId, String
Future<void> encryptedChannelMenuUI(Store node) async {
gSpecificDebug = 1;
bool continueChatMenu = true;
bool justShowedChannels = false;
@ -1258,7 +1262,6 @@ Future<void> mainMenuUi(Store node) async {
break;
}
content = addEscapeChars(content);
stdout.write("\nType id of event to reply to (leave blank to make a new post; type x to cancel): ");
String? $replyToVar = stdin.readLineSync();
String replyToId = $replyToVar??"";

View File

@ -64,11 +64,11 @@ String getStrTagsFromJson(dynamic json) {
str += "[";
int j = 0;
for(dynamic e in tag) {
for(dynamic element in tag) {
if( j != 0) {
str += ",";
}
str += "\"${e.toString()}\"";
str += "\"${element.toString()}\"";
j++;
}
str += "]";
@ -78,13 +78,13 @@ String getStrTagsFromJson(dynamic json) {
}
bool verifyEvent(dynamic json) {
return true;
gSpecificDebug = 0;
if(gSpecificDebug > 0) print("----\nIn verify event:");
String createdAt = json['created_at'].toString();
List<dynamic> listTags = json['tags'];
//print(listTags);
String strTags = json['tags'].toString();
strTags = getStrTagsFromJson(json['tags']);
String strTags = getStrTagsFromJson(json['tags']);
//print("strTags = $strTags");
@ -92,19 +92,25 @@ bool verifyEvent(dynamic json) {
String eventPubkey = json['pubkey'];
String strKind = json['kind'].toString();
String content = json['content'];
content = unEscapeChars( content);
String eventSig = json['sig'];
String calculatedId = getShaId(eventPubkey, createdAt.toString(), strKind, strTags, content);
//print("\ncalculated id = $calculatedId actual id = $id");
bool verified = true;//verify( eventPubkey, calculatedId, sig);
if( false) {
String calculatedId = getShaId(eventPubkey, createdAt.toString(), strKind, strTags, content);
bool verified = true;//verify( eventPubkey, calculatedId, eventSig);
if( !verified ) {
//printWarning("wrong sig event sig = $sig event id = $id calculated id = $calculatedId " );
//print("Event: kind = $strKind");
throw Exception();
} else {
//printInColor("verified correct sig", gCommentColor);
if( !verified && !eventPubkey.startsWith("00")) {
if(gSpecificDebug > 0) printWarning("\nwrong sig event\nevent sig = $eventSig\nevent id = $id\ncalculated id = $calculatedId " );
if(gSpecificDebug > 0) print("Event: kind = $strKind\n");
//getShaId(eventPubkey, createdAt.toString(), strKind, strTags, content);
//print("$json");
//throw Exception();
} else {
if(gSpecificDebug > 0) printInColor("\nverified correct sig for event id $id\n", gCommentColor);
}
}
return true;
}
@ -222,7 +228,7 @@ class EventData {
//}
try {
//verifyEvent(json);
verifyEvent(json);
} on Exception catch(e) {
//printWarning("verify gave exception $e");
@ -1387,12 +1393,22 @@ class Contact {
}
String addEscapeChars(String str) {
return str.replaceAll("\"", "\\\"");
String temp = str.replaceAll("\"", "\\\"");
return temp.replaceAll("\n", "\\n");
}
String unEscapeChars(String str) {
//print("in unEscape: |$str|");
String temp = str.replaceAll("\"", "\\\"");
temp = temp.replaceAll("\n", "\\n");
//print("returning |$temp|\n");
return temp;
}
String getShaId(String pubkey, String createdAt, String kind, String strTags, String content) {
String buf = '[0,"$pubkey",$createdAt,$kind,[$strTags],"$content"]';
//print("in getShaId for buf: |$buf|");
if(gSpecificDebug > 0) print("in getShaId for buf: |$buf|");
var bufInBytes = utf8.encode(buf);
var value = sha256.convert(bufInBytes);
return value.toString();

View File

@ -1,7 +1,5 @@
import 'dart:collection';
import 'dart:io';
import 'dart:convert';
import 'package:nostr_console/console_ui.dart';
import 'package:nostr_console/event_ds.dart';
import 'package:nostr_console/relays.dart';
import 'package:nostr_console/settings.dart';
@ -1679,7 +1677,9 @@ class Store {
String getTagStr(String replyToId, String clientName, [bool addAllP = false]) {
clientName = (clientName == "")? "nostr_console": clientName; // in case its empty
if( replyToId.isEmpty) {
return '["client","$clientName"]';
if( gWhetherToSendClientTag)
return '["client","$clientName"]';
return "[]";
}
String strTags = "";
@ -1727,10 +1727,12 @@ class Store {
strTags += '["e","$rootEventId","","root"],';
}
}
strTags += '["e","$latestEventId","$relay","reply"],';
strTags += '["e","$latestEventId","$relay","reply"]';
}
strTags += '["client","$clientName"]' ;
if( gWhetherToSendClientTag)
strTags += ',["client","$clientName"]' ;
return strTags;
}