diff --git a/example/index.tsx b/example/index.tsx index 5aaf5b2..2a79dcd 100644 --- a/example/index.tsx +++ b/example/index.tsx @@ -1,25 +1,53 @@ -import { getPublicKey } from 'nostr-tools'; import * as React from 'react'; -import { useEffect } from 'react'; +import { useEffect, useState } from 'react'; import * as ReactDOM from 'react-dom'; -import { Connect, ConnectMessageType, GetPublicKeyResponse, Session } from '../src'; -import { prepareResponse } from '../src/event'; +import { NostrRPC } from '../src/request'; +class Server extends NostrRPC { + async ping(): Promise { + return 'pong'; + } +} +const server = new Server({ + secretKey: + 'ed779ff047f99c95f732b22c9f8f842afb870c740aab591776ebc7b64e83cf6c', +}); +const client = new NostrRPC({ + secretKey: + '5acff99d1ad3e1706360d213fd69203312d9b5e91a2d5f2e06100cc6f686e5b3', +}); const App = () => { + const [response, setResponse] = useState(''); + useEffect(() => { (async () => { - + await server.listen(); })(); }, []); + const makeCall = async () => { + const result = await client.call({ + target: server.self.pubkey, + request: { method: 'ping' }, + }); + setResponse(result); + } + + return (
-

Check you console

+

Nostr Connect Playground

+

Server pubkey: {server.self.pubkey}

+

Client pubkey: {client.self.pubkey}

+
+ +
+

{response}

) } diff --git a/src/request.ts b/src/request.ts index 530e1bf..c04c6f3 100644 --- a/src/request.ts +++ b/src/request.ts @@ -59,26 +59,27 @@ export class NostrRPC { // send request via relay await new Promise((resolve, reject) => { const pub = this.relay.publish(event); - pub.on('failed', reject); - pub.on('seen', resolve); + pub.on('failed', (reason: any) => { + reject(reason); + }); + pub.on('seen', () => { + resolve(); + }); }); - console.log(`request: nostr id: ${event.id}`, { id, method, params }); - + // TODO: reject after a timeout + // waiting for response from remote return new Promise((resolve, reject) => { - // waiting for response from remote const queries = [ { kinds: [4], authors: [target], '#p': [this.self.pubkey], + since: event.created_at - 1, }, ]; - // TODO: reject after a timeout + let sub = this.relay.sub(queries); - - console.log('subscribing to', JSON.stringify(queries)); - sub.on('event', async (event: Event) => { let payload; try { @@ -95,11 +96,13 @@ export class NostrRPC { // ignore all the events that are not NostrRPCResponse events if (!isValidResponse(payload)) return; - console.log(`response: nostr id: ${event.id}`, payload); // ignore all the events that are not for this request if (payload.id !== id) return; + // unsubscribe from the stream + sub.unsub(); + // if the response is an error, reject the promise if (payload.error) { reject(payload.error); @@ -162,14 +165,9 @@ export class NostrRPC { body ); - console.log('response to be sent', responseEvent); // send response via relay - const pub = this.relay.publish(responseEvent); - pub.on('failed', console.error); - }); - - sub.on('eose', () => { - sub.unsub(); + this.relay.publish(responseEvent); + // TODO: handle errors when event is not seen }); return sub;