Skip to content

Commit

Permalink
feat(suite): nostr subscribe to replies
Browse files Browse the repository at this point in the history
  • Loading branch information
martykan committed Dec 12, 2024
1 parent 9a0bce0 commit 3579668
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 19 deletions.
26 changes: 15 additions & 11 deletions packages/connect-nostr/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useWebSocketImplementation } from 'nostr-tools/pool';
import WebSocket from 'ws';

Check failure on line 5 in packages/connect-nostr/src/index.ts

View workflow job for this annotation

GitHub Actions / Linting and formatting

'ws' should be listed in the project's dependencies. Run 'npm i -S ws' to add it

import { createDeferredManager } from '@trezor/utils';

import { PeerToPeerCommunicationClient, PeerToPeerCommunicationClientEvents } from './abstract';

export type { Event } from 'nostr-tools/pure';
Expand Down Expand Up @@ -72,6 +73,7 @@ export class NostrClient extends PeerToPeerCommunicationClient<PeerToPeerCommuni

newIdentity() {
this.sk = generateSecretKey();

return this.setIdentity({
type: 'hot-keys',
nsecStr: nip19.nsecEncode(this.sk),
Expand Down Expand Up @@ -109,16 +111,20 @@ export class NostrClient extends PeerToPeerCommunicationClient<PeerToPeerCommuni
console.log(`connected to ${this.relay.url}`);

Check failure on line 111 in packages/connect-nostr/src/index.ts

View workflow job for this annotation

GitHub Actions / Linting and formatting

Unexpected console statement
}

buildMessage({ content }: { content: string }) {
buildMessage({ kind = 1, content, tags = [] }: { kind: number; content: string; tags: any[] }) {
if (!this.nsec && !this.signer) {
// return { success: false, error: 'no identity' };
throw new Error('no identity');
}

const eventTemplate = {
kind: 1,
kind,
created_at: Math.floor(Date.now() / 1000),
tags: [],
tags: tags.map(tag =>
tag.map((value: any) =>
value.startsWith('npub') ? nip19.decode(value).data.toString() : value,
),
),
content,
};

Expand All @@ -129,20 +135,20 @@ export class NostrClient extends PeerToPeerCommunicationClient<PeerToPeerCommuni
}
}

async send({ content }: { content: string }) {
const signedEvent = this.buildMessage({ content });
async send({ ...params }: { kind: number; content: string; tags: any[] }) {
const signedEvent = this.buildMessage({ ...params });

await this.relay.publish(signedEvent);

return { success: true as const };
}

async request({ content }: { content: string }) {
async request({ content, ...params }: { kind: number; content: string; tags: any[] }) {
const { promiseId, promise } = this.messages.create();

const json = JSON.parse(content);
json.id = promiseId.toString();
const signedEvent = this.buildMessage({ content: JSON.stringify(json) });
const signedEvent = this.buildMessage({ ...params, content: JSON.stringify(json) });

await this.relay.publish(signedEvent);

Expand All @@ -151,16 +157,14 @@ export class NostrClient extends PeerToPeerCommunicationClient<PeerToPeerCommuni
return { success: true as const, response: promise };
}

subscribe({ pubKeys }: { pubKeys: nip19.NPub[] }) {
subscribe({ recipientPubkey }: { recipientPubkey: string }) {
if (this.subscription) {
this.subscription.close();
}
this.subscription = this.relay.subscribe(
[
{
kinds: [1],
authors: pubKeys.map(k => nip19.decode(k).data),
limit: 1,
'#p': [nip19.decode(recipientPubkey).data.toString()],
},
],
{
Expand Down
18 changes: 11 additions & 7 deletions packages/suite/src/actions/suite/nostrActions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import TrezorConnect from '@trezor/connect';
import { Dispatch, GetState } from 'src/types/suite';
import { NostrClient } from '@trezor/connect-nostr';

Check failure on line 2 in packages/suite/src/actions/suite/nostrActions.ts

View workflow job for this annotation

GitHub Actions / Linting and formatting

'@trezor/connect-nostr' should be listed in the project's dependencies. Run 'npm i -S @trezor/connect-nostr' to add it

import { Dispatch, GetState } from 'src/types/suite';
import { NOSTR } from 'src/actions/suite/constants';

export type NostrAction =
Expand Down Expand Up @@ -76,17 +77,20 @@ export const init = () => (dispatch: Dispatch, _getState: GetState) => {
nostrClient.connect();
};

export const subscribe = (pubkey: string) => (_dispatch: Dispatch, _getState: GetState) => {
nostrClient?.subscribe({ pubKeys: [pubkey] });
export const subscribe = () => (_dispatch: Dispatch, getState: GetState) => {
const { npub } = getState().nostr.keys;
nostrClient?.subscribe({ recipientPubkey: npub });
};

export const send = (content: any) => (_dispatch: Dispatch, _getState: GetState) => {
nostrClient?.send(content);
};

export const request = (content: any) => (_dispatch: Dispatch, _getState: GetState) => {
return nostrClient?.request(content);
};
export const request =
(params: { kind: number; content: string; tags: any[] }) =>
(_dispatch: Dispatch, _getState: GetState) => {
return nostrClient?.request(params);
};

export const newIdentity = () => (_dispatch: Dispatch, _getState: GetState) => {
return nostrClient?.newIdentity();
Expand All @@ -99,7 +103,7 @@ export const dispose = () => (_dispatch: Dispatch, _getState: GetState) => {
export const setIdentity =
(params: Parameters<(typeof nostrClient)['setIdentity']>[0]) =>
async (dispatch: Dispatch, _getState: GetState) => {
const selectedDevice = _getState().device.selectedDevice;
const { selectedDevice } = _getState().device;

const response = await TrezorConnect.nostrGetPublicKey({
path: "m/44'/1237'/0'/0/0",
Expand Down
6 changes: 5 additions & 1 deletion packages/suite/src/views/settings/SettingsDebug/Nostr.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const Nostr = () => {

const handlePeerNpubClick = () => {
if (!peerNpub) return;
dispatch(nostrActions.subscribe(peerNpub));
dispatch(nostrActions.subscribe());
};

const handleDisconnectClick = async () => {
Expand All @@ -57,6 +57,8 @@ export const Nostr = () => {
setAddressRequestState('pending');
await dispatch(
nostrActions.request({
kind: 9898,
tags: [['p', peerNpub]],
content: JSON.stringify({
type: 'address_request',
}),
Expand All @@ -75,6 +77,8 @@ export const Nostr = () => {

dispatch(
nostrActions.send({
kind: 9898,
tags: [['p', events.pubkey]],
content: JSON.stringify({
type: 'address_response',
request_id: content.id,
Expand Down

0 comments on commit 3579668

Please sign in to comment.