From a1b65098b4970e33800b8e433a86529fe1e4692b Mon Sep 17 00:00:00 2001 From: Tomas Martykan Date: Thu, 12 Dec 2024 14:31:00 +0100 Subject: [PATCH] feat(suite): make contacts work with nostr sign --- .../src/views/contacts/AddNewContactModal.tsx | 2 +- packages/suite/src/views/contacts/index.tsx | 9 ++--- .../src/views/contacts/useAddContact.tsx | 12 ++++--- .../contacts/src/utils/findContact.ts | 34 +++++++++---------- .../src/send/sendFormBitcoinThunks.ts | 7 ++-- 5 files changed, 29 insertions(+), 35 deletions(-) diff --git a/packages/suite/src/views/contacts/AddNewContactModal.tsx b/packages/suite/src/views/contacts/AddNewContactModal.tsx index e14246cca6c..d02ffa014de 100644 --- a/packages/suite/src/views/contacts/AddNewContactModal.tsx +++ b/packages/suite/src/views/contacts/AddNewContactModal.tsx @@ -38,7 +38,7 @@ export const AddNewContactModal = ({ onClose }: AddNewContactModalProps) => { } > - No device selected or device unacquired + {errorMessage} ); diff --git a/packages/suite/src/views/contacts/index.tsx b/packages/suite/src/views/contacts/index.tsx index 16b6ec4cb26..1425a473ff0 100644 --- a/packages/suite/src/views/contacts/index.tsx +++ b/packages/suite/src/views/contacts/index.tsx @@ -47,16 +47,13 @@ const GetMyPubkeyButton = () => { const getMyPubkey = async () => { if (!device) return; - const response = await TrezorConnect.getAddress({ + const response = await TrezorConnect.nostrGetPublicKey({ device, - path: "m/44'/1'/0'/0/0", - coin: 'test', useEmptyPassphrase: device.useEmptyPassphrase, - showOnTrezor: false, - scriptType: 'SPENDADDRESS', + path: "m/44'/1237'/0'/0/0", }); if (response.success) { - alert(`Your identity pubkey: ${response.payload.address}`); + alert(`Your identity pubkey: ${response.payload.pubkey}`); } else { alert(`Failed to get pubkey: ${response.payload.error}`); } diff --git a/packages/suite/src/views/contacts/useAddContact.tsx b/packages/suite/src/views/contacts/useAddContact.tsx index 6306ea95d7b..e5b434acdc6 100644 --- a/packages/suite/src/views/contacts/useAddContact.tsx +++ b/packages/suite/src/views/contacts/useAddContact.tsx @@ -16,14 +16,16 @@ export const useAddContact = (onCloseModal: () => void, label: string, address: return { error: 'Missing data' }; } - const message = `${label}/${address}`; + const content = `${label}/${address}`; - const response = await TrezorConnect.signMessage({ + const response = await TrezorConnect.nostrSignEvent({ device, - path: "m/44'/1'/0'/0/0", - coin: 'test', - message, useEmptyPassphrase: device.useEmptyPassphrase, + path: "m/44'/1237'/0'/0/0", + created_at: 0, + kind: 27922, + tags: [], + content, }); if (!response.success) { diff --git a/suite-common/contacts/src/utils/findContact.ts b/suite-common/contacts/src/utils/findContact.ts index 25cf8dad248..646d04dc622 100644 --- a/suite-common/contacts/src/utils/findContact.ts +++ b/suite-common/contacts/src/utils/findContact.ts @@ -8,25 +8,23 @@ export const findContactBySignedMessage = ( address: string, signature: string, ) => { - for (const contact of contacts) { - try { - // same logic as on fw side - const messageDigest = sha256(address); - let recId = Buffer.from(signature.slice(0, 2), 'hex')[0]; - recId -= 27; - recId &= 3; + let recover; + try { + // same logic as on fw side + const messageDigest = sha256(address); + let recId = Buffer.from(signature.slice(0, 2), 'hex')[0]; + recId -= 27; + recId &= 3; - const recover = secp256k1.Signature.fromCompact(Buffer.from(signature.slice(2), 'hex')) - .addRecoveryBit(recId) - .recoverPublicKey(messageDigest) - .toHex(); + recover = secp256k1.Signature.fromCompact(Buffer.from(signature.slice(2), 'hex')) + .addRecoveryBit(recId) + .recoverPublicKey(messageDigest) + .toHex(); + } catch (error) { + console.error(error); - if (recover.slice(2) === contact.address) { - return contact; - } - } catch (error) { - console.error(error); - continue; - } + return; } + + return contacts.find(c => c.address === recover.slice(2)); }; diff --git a/suite-common/wallet-core/src/send/sendFormBitcoinThunks.ts b/suite-common/wallet-core/src/send/sendFormBitcoinThunks.ts index 46f521e458a..cbf8ad33ce6 100644 --- a/suite-common/wallet-core/src/send/sendFormBitcoinThunks.ts +++ b/suite-common/wallet-core/src/send/sendFormBitcoinThunks.ts @@ -351,18 +351,16 @@ export const signBitcoinSendFormTransactionThunk = createThunk< if (output.address) { const contactSignature = formState.contactSignatures?.[output.address]; if (contactSignature) { - //console.log('contactSignature', contactSignature); const contact = findContactBySignedMessage( contacts, output.address, contactSignature, ); - //console.log('contact', contact); if (contact) { return { ...output, label: contact.label, - label_sig: Buffer.from(contact.signature, 'base64').toString('hex'), + label_sig: contact.signature, label_pk: contact.address, address_pk_sig: contactSignature, }; @@ -370,12 +368,11 @@ export const signBitcoinSendFormTransactionThunk = createThunk< } const contact = contacts.find(c => c.address === output.address); - //console.log('direct contact', contact); if (contact) { return { ...output, label: contact.label, - label_sig: Buffer.from(contact.signature, 'base64').toString('hex'), + label_sig: contact.signature, }; } }