From 498de4d5b1261643a54bf163aff05b0563251e73 Mon Sep 17 00:00:00 2001 From: Martin Varmuza Date: Wed, 11 Dec 2024 14:48:00 +0100 Subject: [PATCH] fixup! fixup! feat(suite): [wip] nostr barebones --- packages/connect-nostr/src/index.ts | 77 +++++++---- .../views/settings/SettingsDebug/Nostr.tsx | 128 ++++++++++++------ 2 files changed, 141 insertions(+), 64 deletions(-) diff --git a/packages/connect-nostr/src/index.ts b/packages/connect-nostr/src/index.ts index 16b60a1cff2..1787116efb6 100644 --- a/packages/connect-nostr/src/index.ts +++ b/packages/connect-nostr/src/index.ts @@ -1,20 +1,16 @@ -import { - getPublicKey, - finalizeEvent, - verifyEvent, - Event, - generateSecretKey, -} from 'nostr-tools/pure'; -import { Relay } from 'nostr-tools/relay'; +import { getPublicKey, finalizeEvent, Event, generateSecretKey } from 'nostr-tools/pure'; +import { Relay, Subscription } from 'nostr-tools/relay'; import * as nip19 from 'nostr-tools/nip19'; import { useWebSocketImplementation } from 'nostr-tools/pool'; import WebSocket from 'ws'; -import { PeerToPeerCommunicationClient, PeerToPeerCommunicationClientEvents } from './abstract'; -useWebSocketImplementation(WebSocket); +import { createDeferredManager } from '@trezor/utils'; +import { PeerToPeerCommunicationClient, PeerToPeerCommunicationClientEvents } from './abstract'; export type { Event } from 'nostr-tools/pure'; +useWebSocketImplementation(WebSocket); + export class NostrClient extends PeerToPeerCommunicationClient { sk?: Uint8Array; pk?: string; @@ -23,10 +19,15 @@ export class NostrClient extends PeerToPeerCommunicationClient nip19.decode(k).data), limit: 1, }, ], { onevent: event => { this.emit('event', event); + + const resp = JSON.parse(event.content); + console.log('on event parsed', resp); + const { request_id, ...data } = resp; + + if (typeof request_id !== 'undefined') { + this.messages.resolve(Number(request_id), data); + } this.events.push(event); }, - // oneose() { - // console.log('=====oneose===='); - // subscription.close(); - // }, + oneose() { + // "End of Stored Events". + console.log('=====all stored events processed===='); + }, }, ); } diff --git a/packages/suite/src/views/settings/SettingsDebug/Nostr.tsx b/packages/suite/src/views/settings/SettingsDebug/Nostr.tsx index 6658b4df9e7..cda89c3ebdb 100644 --- a/packages/suite/src/views/settings/SettingsDebug/Nostr.tsx +++ b/packages/suite/src/views/settings/SettingsDebug/Nostr.tsx @@ -15,9 +15,14 @@ export const Nostr = () => { const [client, setClient] = useState(null); const [events, setEvents] = useState(); const [clientStatus, setClientStatus] = useState('disconnected'); + const [addressRequestState, setAddressRequestState] = useState<'none' | 'pending' | 'success'>( + 'none', + ); + const unusedAddress = useSelector( state => state.wallet.accounts[0]?.addresses?.unused[0].address, ); + const handleChange = (event: any) => { setNsec(event.target.value); }; @@ -32,7 +37,9 @@ export const Nostr = () => { relayUrl: 'wss://relay.primal.net', }); setClient(nostrClient); - setMyNpub(nostrClient.pk); + if (nostrClient.npub) { + setMyNpub(nostrClient.npub); + } nostrClient.on('event', message => { console.log('message', message); @@ -61,7 +68,7 @@ export const Nostr = () => { const handleDisconnectClick = async () => { client?.dispose(); }; - const handleSendPaymentRequest = () => { + const handleSendPaymentData = () => { client?.send({ content: JSON.stringify({ type: 'payment_request', @@ -71,50 +78,68 @@ export const Nostr = () => { }; const handleSendAddressRequest = () => { - client?.send({ - content: JSON.stringify({ - type: 'address_request', - }), - }); + setAddressRequestState('pending'); + client + ?.request({ + content: JSON.stringify({ + type: 'address_request', + }), + }) + .then(result => { + console.log('result', result); + setAddressRequestState('success'); + }); }; - const handleAddressRequestResponse = () => { + const handleAddressRequestResponse = (content: any) => { + console.log('handleAddressRequestResponse', content); + client?.send({ content: JSON.stringify({ type: 'address_response', + request_id: content.id, payload: unusedAddress, }), }); }; - const PeerRequest = ({ created_at, pubkey, content }: any) => { + const PeerRequest = ({ pubkey, content }: any) => { if (content.type === 'payment_request') { return ( -
-

timestamp: {created_at}

-

from {pubkey}

-

+ + + {content.payload} -

-
+ + ); } if (content.type === 'address_request') { return ( -
-

timestamp: {created_at}

-

from {pubkey}

- -
+ + + + + + + ); } if (content.type === 'address_response') { return ( -
-

yay, we received address: {content.payload}

-
+ + + ); } @@ -144,7 +169,7 @@ export const Nostr = () => { onClick={() => { client?.newIdentity(); setNsec(client?.nsecStr!); - setMyNpub(client?.pk!); + setMyNpub(client?.npub!); console.log('client', client); }} > @@ -168,7 +193,7 @@ export const Nostr = () => { - +
@@ -183,7 +208,7 @@ export const Nostr = () => {
- + { - {peerNpub && clientStatus === 'connected' && ( - <> - - - - - + + + + + + + + + + + + + + + {clientStatus === 'connected' && ( + <> - - {events && } + + {events && } )}