Skip to content

Commit

Permalink
feat(suite): nostr address request/reply
Browse files Browse the repository at this point in the history
  • Loading branch information
martykan committed Dec 12, 2024
1 parent 3579668 commit eb27ce5
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 42 deletions.
1 change: 1 addition & 0 deletions packages/connect-nostr/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ export class NostrClient extends PeerToPeerCommunicationClient<PeerToPeerCommuni
[
{
'#p': [nip19.decode(recipientPubkey).data.toString()],
since: Math.floor(Date.now() / 1000),
},
],
{
Expand Down
48 changes: 23 additions & 25 deletions packages/suite/src/actions/suite/nostrActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,31 +100,29 @@ export const dispose = () => (_dispatch: Dispatch, _getState: GetState) => {
return nostrClient?.dispose();
};

export const setIdentity =
(params: Parameters<(typeof nostrClient)['setIdentity']>[0]) =>
async (dispatch: Dispatch, _getState: GetState) => {
const { selectedDevice } = _getState().device;

const response = await TrezorConnect.nostrGetPublicKey({
path: "m/44'/1237'/0'/0/0",
device: selectedDevice,
});
export const setIdentity = () => async (dispatch: Dispatch, _getState: GetState) => {
const { selectedDevice } = _getState().device;

console.log('response', response);
if (!response.success) {
return;
}
const response = await TrezorConnect.nostrGetPublicKey({
path: "m/44'/1237'/0'/0/0",
device: selectedDevice,
});

nostrClient?.setIdentity({
type: 'signer',
npubStr: response.payload.pubkey,
signer: (event: any) =>
TrezorConnect.nostrSignEvent({
path: "m/44'/1237'/0'/0/0",
device: selectedDevice,
...event,
}),
});
console.log('response', response);
if (!response.success) {
return;
}

nostrClient?.setIdentity({
type: 'signer',
npubStr: response.payload.pubkey,
signer: (event: any) =>
TrezorConnect.nostrSignEvent({
path: "m/44'/1237'/0'/0/0",
device: selectedDevice,
...event,
}),
});

console.log('nostrClient', nostrClient);
};
console.log('nostrClient', nostrClient);
};
12 changes: 8 additions & 4 deletions packages/suite/src/views/contacts/ContactList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Button, Card, Dropdown, Table, Text } from '@trezor/components';
import { spacings } from '@trezor/theme';

const ContactItem = ({
contact: { address, label, signature },
contact: { address, label, receiveAddresses },
remove,
}: {
contact: Contact;
Expand All @@ -21,7 +21,11 @@ const ContactItem = ({
</Table.Cell>
<Table.Cell>
<Text variant="tertiary" typographyStyle="hint" overflowWrap="anywhere">
{signature}
{receiveAddresses?.map(ra => (
<a href={'bitcoin:' + ra.address} key={ra.address}>
{ra.address}
</a>
))}
</Text>
</Table.Cell>
<Table.Cell align="right">
Expand Down Expand Up @@ -61,8 +65,8 @@ export const ContactList = ({
<Table.Header>
<Table.Row>
<Table.Cell>Label</Table.Cell>
<Table.Cell>Address</Table.Cell>
<Table.Cell>Signature</Table.Cell>
<Table.Cell>Npub</Table.Cell>
<Table.Cell>Receive address</Table.Cell>
<Table.Cell />
</Table.Row>
</Table.Header>
Expand Down
27 changes: 26 additions & 1 deletion packages/suite/src/views/contacts/useAddContact.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import TrezorConnect from '@trezor/connect';
import { selectDevice } from '@suite-common/wallet-core';
import { contactsActions, getDeviceState } from '@suite-common/contacts';

import * as nostrActions from 'src/actions/suite/nostrActions';

import { useDispatch, useSelector } from '../../hooks/suite';

export const useAddContact = (onCloseModal: () => void, label: string, address: string) => {
Expand Down Expand Up @@ -34,8 +36,31 @@ export const useAddContact = (onCloseModal: () => void, label: string, address:

const { signature } = response.payload;

const contact = { address, label, signature, deviceState };
const newAddress = await dispatch(
nostrActions.request({
kind: 9898,
tags: [['p', address]],
content: JSON.stringify({
type: 'address_request',
}),
}),
);
console.log('newAddress', newAddress);

const contact = {
address,
label,
signature,
deviceState,
receiveAddresses: [
{
address: newAddress.response.address,
signature: newAddress.response.signature,
},
],
};
dispatch(contactsActions.addContact(contact));

onCloseModal();

return { error: null };
Expand Down
29 changes: 17 additions & 12 deletions packages/suite/src/views/settings/SettingsDebug/Nostr.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from 'react';
import { useEffect, useState } from 'react';

import TrezorConnect from '@trezor/connect';
import { Input, Button } from '@trezor/components';
Expand All @@ -18,11 +18,11 @@ export const Nostr = () => {
const { keys, status: clientStatus, event: events, relayUrl, type } = nostr;
const { nsec: Nsec, npub: myNpub } = keys;

console.log('status', clientStatus);
/*console.log('status', clientStatus);
console.log('events, ', events);
console.log('type', type);
console.log('Nsec', Nsec);
console.log('myNpub', myNpub);
console.log('myNpub', myNpub);*/
const unusedAddress = useSelector(
state => state.wallet.accounts[0]?.addresses?.unused[0].address,
);
Expand All @@ -34,7 +34,6 @@ export const Nostr = () => {
};

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

Expand Down Expand Up @@ -72,17 +71,23 @@ export const Nostr = () => {
// dispatch()
};

const handleAddressRequestResponse = (content: any) => {
console.log('handleAddressRequestResponse', content);
const handleAddressRequestResponse = async () => {
console.log('handleAddressRequestResponse', events);

const addressResponse = await TrezorConnect.getAddress({
coin: 'test',
path: "m/44'/1'/0'/0/0",
showOnTrezor: false,
});

dispatch(
nostrActions.send({
kind: 9898,
tags: [['p', events.pubkey]],
content: JSON.stringify({
type: 'address_response',
request_id: content.id,
payload: unusedAddress,
request_id: events.content.id,
payload: addressResponse.payload,
}),
}),
);
Expand Down Expand Up @@ -123,16 +128,17 @@ export const Nostr = () => {
if (content.type === 'address_response') {
return (
<SectionItem>
<TextColumn title="Address response" description={content.payload} />
<TextColumn
title="Address response"
description={JSON.stringify(content.payload)}
/>
</SectionItem>
);
}

return null;
};

console.log(events);

return (
<>
<SectionItem>
Expand All @@ -156,7 +162,6 @@ export const Nostr = () => {
<ActionColumn>
<Button
onClick={() => {
// @ts-ignore
dispatch(nostrActions.newIdentity());
}}
>
Expand Down
4 changes: 4 additions & 0 deletions suite-common/contacts/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ export type Contact = {
label: string;
signature: string;
deviceState: string;
receiveAddresses: {
address: string;
signature: string;
}[];
};

0 comments on commit eb27ce5

Please sign in to comment.