Skip to content

Commit

Permalink
Event signing dialog shows user pubkey
Browse files Browse the repository at this point in the history
  • Loading branch information
tvolk131 committed May 17, 2024
1 parent 1498612 commit 5d1296b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
10 changes: 6 additions & 4 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ use nip_55::KeyManager;
use nostr_sdk::key::SecretKey;
use nostr_sdk::nips::nip46;
use nostr_sdk::secp256k1::{Keypair, Secp256k1};
use nostr_sdk::PublicKey;
use nostr_sdk::{EventId, FromBech32};
use nostr_sdk::{EventId, FromBech32, PublicKey, ToBech32};
use std::collections::HashMap;
use std::sync::Arc;
use tauri::Manager;
Expand Down Expand Up @@ -120,7 +119,7 @@ impl Nip46RequestApprover for KeystacheRequestApprover {
) -> Nip46RequestApproval {
// TODO: IMPORTANT!!! Currently we ignore all but the first request. We should handle all requests.
// TODO: We should use `_user_pubkey` and pass it to the frontend.
let (request, _user_pubkey) = match requests.into_iter().next() {
let (request, user_pubkey) = match requests.into_iter().next() {
Some(request) => request,
None => return Nip46RequestApproval::Reject,
};
Expand Down Expand Up @@ -150,7 +149,10 @@ impl Nip46RequestApprover for KeystacheRequestApprover {

if self
.app_handle
.emit_all("sign_event_request", event.clone())
.emit_all(
"sign_event_request",
(event, user_pubkey.to_bech32().unwrap()),
)
.is_err()
{
return Nip46RequestApproval::Reject;
Expand Down
14 changes: 9 additions & 5 deletions src/pages/HomePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { type PayInvoiceResponse, type UnsignedNostrEvent } from "~/types";
const HomePage = () => {
const [open, setOpen] = useState(false);
const [event, setEvent] = useState<UnsignedNostrEvent | undefined>(undefined);
const [userPubkey, setUserPubkey] = useState<string | undefined>(undefined);
const [invoice, setInvoice] = useState<string | undefined>(undefined);
const [pubkey, setPubkey] = useState<string | undefined | null>(undefined);
const resolveRejectRef = useRef<{
Expand Down Expand Up @@ -50,15 +51,14 @@ const HomePage = () => {
}, []);

useEffect(() => {
const handleEvent = (event: UnsignedNostrEvent): Promise<boolean> => {
return handleSignEventRequests((event, userPubkey): Promise<boolean> => {
setEvent(event);
setUserPubkey(userPubkey);
setOpen(true);
return new Promise((resolve, reject) => {
resolveRejectRef.current = { resolve, reject };
});
};

return handleSignEventRequests(handleEvent);
});
}, []);

useEffect(() => {
Expand Down Expand Up @@ -107,13 +107,17 @@ const HomePage = () => {
</div>

<Dialog open={open} onOpenChange={setOpen}>
<DialogContent className="h-[20rem] max-w-[22rem]">
<DialogContent className="max-h-[30rem] max-w-[22rem]">
<DialogHeader>
<DialogTitle>Sign Event?</DialogTitle>
</DialogHeader>
<div className="overflow-auto bg-muted">
<pre>{JSON.stringify(event, null, 2) ?? ""}</pre>
</div>
As User
<div className="overflow-auto bg-muted">
{userPubkey}
</div>
<DialogFooter>
<div className="flex justify-end gap-x-4">
<Button onClick={handleAccept}>Accept</Button>
Expand Down
8 changes: 4 additions & 4 deletions src/tauriCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,18 @@ export const setNsec = async (nsec: string): Promise<void> => {
}

type SignEventRequestHandler = (
event: UnsignedNostrEvent,
event: UnsignedNostrEvent, userPubkey: string
) => Promise<boolean> | boolean;

listen("sign_event_request", async (event: Event<UnsignedNostrEvent>) => {
listen("sign_event_request", async (event: Event<[UnsignedNostrEvent, string]>) => {
let isApproved = false;
for (const handler of Object.values(signEventRequestHandlers)) {
isApproved = await handler(event.payload);
isApproved = await handler(...event.payload);
if (isApproved) {
break;
}
}
respondToSignEventRequest(event.payload.id, isApproved);
respondToSignEventRequest(event.payload[0].id, isApproved);
})
.then((unlisten) => {
// When vite reloads, a new event listener is created, so we need to unlisten to the old one.
Expand Down

0 comments on commit 5d1296b

Please sign in to comment.