Skip to content

Commit

Permalink
feat(suite): make contacts work with nostr sign
Browse files Browse the repository at this point in the history
  • Loading branch information
martykan committed Dec 12, 2024
1 parent b7da7fa commit a1b6509
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 35 deletions.
2 changes: 1 addition & 1 deletion packages/suite/src/views/contacts/AddNewContactModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const AddNewContactModal = ({ onClose }: AddNewContactModalProps) => {
</NewModal.Button>
}
>
No device selected or device unacquired
{errorMessage}
</NewModal>
);

Expand Down
9 changes: 3 additions & 6 deletions packages/suite/src/views/contacts/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}
Expand Down
12 changes: 7 additions & 5 deletions packages/suite/src/views/contacts/useAddContact.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
34 changes: 16 additions & 18 deletions suite-common/contacts/src/utils/findContact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
};
7 changes: 2 additions & 5 deletions suite-common/wallet-core/src/send/sendFormBitcoinThunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,31 +351,28 @@ 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,
};
}
}

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,
};
}
}
Expand Down

0 comments on commit a1b6509

Please sign in to comment.