Skip to content

Commit

Permalink
signing of events using web NIP-07 extension
Browse files Browse the repository at this point in the history
  • Loading branch information
Fmar committed Sep 13, 2023
1 parent 321d3c2 commit 48cf841
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 14 deletions.
3 changes: 1 addition & 2 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@
- ~~login with npub~~
- ~~login with extension on web~~
- ~~text color on light theme login screen YANA + input is dark~~

- signEvent
- ~~signEvent~~

- STOP followers/zaps events from blocking the relay, when navigate away from that profile
- get notifications in background for all accounts
Expand Down
4 changes: 4 additions & 0 deletions lib/js/js_helper_mobile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ Future<String> getPublicKeyAsync() {
Future<Event> signEventAsync(Event event) {
return Future.value(null);
}

Future<String> signSchnorrAsync(String msg) {
return Future.value(null);
}
9 changes: 9 additions & 0 deletions lib/js/js_helper_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,19 @@ external dynamic getPublicKey();
@JS()
external dynamic signEvent(Event event);


// This function will open new popup window for given URL.
@JS()
external dynamic signSchnorr(String msg);

Future<String> getPublicKeyAsync() async {
return await promiseToFuture(await getPublicKey());
}

Future<Event> signEventAsync(Event event) async {
return await promiseToFuture(await signEvent(event));
}

Future<String> signSchnorrAsync(String msg) async {
return await promiseToFuture(await signSchnorr(msg));
}
31 changes: 19 additions & 12 deletions lib/nostr/nostr.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/foundation.dart';
import 'package:yana/nostr/relay_pool.dart';
Expand Down Expand Up @@ -100,28 +102,33 @@ class Nostr {
}

Event? sendEvent(Event event) {
if (StringUtil.isBlank(_privateKey)) {
// TODO to show Notice
throw StateError("Private key is missing. Message can't be signed.");
}
signEvent(event);
var result = _pool.send(["EVENT", event.toJson()]);
if (result) {
return event;
}
// if (StringUtil.isBlank(_privateKey)) {
// // TODO to show Notice
// throw StateError("Private key is missing. Message can't be signed.");
// }
signEvent(event).then((signedEvent) {
var result = _pool.send(["EVENT", signedEvent.toJson()]);
if (result) {
return signedEvent;
}
});
return null;
}

void signEvent(Event event) async {
Future<Event> signEvent(Event event) async {
if (StringUtil.isNotBlank(_privateKey)) {
event.sign(_privateKey!);
return event;
} else {
var signedEvent = await js.signEventAsync(event);
BotToast.showText(text: signedEvent.toString());
// Event signedEvent = await js.signEventAsync(event);
String sig = await js.signSchnorrAsync(event.id);
event.sig = sig;
Event signedEvent = event;
if (kDebugMode) {
BotToast.showText(text: signedEvent.toString());
print("SIGNED EVENT: " + signedEvent.toString());
}
return signedEvent;
}
}

Expand Down
9 changes: 9 additions & 0 deletions web/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,17 @@ async function getPublicKey() {
}

async function signEvent(event) {
console.log("SIGNING EVENT:"+event);
if (window.nostr) {
return await window.nostr.signEvent(event);
}
return "";
}

async function signSchnorr(msg) {
console.log("SIGNING MSG: "+msg);
if (window.nostr) {
return await window.nostr.signSchnorr(msg);
}
return "";
}

0 comments on commit 48cf841

Please sign in to comment.