polkadot-js / common

Utilities and base libraries for use across polkadot-js for Polkadot and Substrate. Includes base libraries, crypto helpers and cross-environment helpers.
Apache License 2.0
253 stars 145 forks source link

Encryption/Decryption not working with sr25519 keypair #1314

Closed LaurentTrk closed 1 year ago

LaurentTrk commented 2 years ago

Hi all ! This issue is the continuation of the closed #1124 about the support of sr25519 keypair for the Encryption/Decryption feature.

After several tests, I cannot get it work, as the ed2curve.convertPublicKey used to convert the public key does not work for sr25519 public key.

The Ristretto decoding of the public key used in Schnorrkel is not the same as in ed25519.

I managed to make encryption/decryption between 2 sr22519 keypairs using Ristretto255.js, but I can't do it between one ed25519 and one sr25519.

Could anyone help me ?

Thanks !

jacogr commented 2 years ago

The encyption/decryption support was contributed, so have really not tested it apart from the available supplied tests. I have not seen it out in the wild, so if the feedback is it doesn't work, it will need to be removed.

LaurentTrk commented 2 years ago

Thanks @jacogr for your reply !
I don't know what to think about it...

This issue is closely related to this feature on the extension(which will be obsolete if this one does not work), and I think it was the plan of @RoyTimes when he first worked on this encryption feature.

RoyTimes commented 2 years ago

@LaurentTrk @jacogr Let me look into it. I thought such case is covered with the original tests? I can change the code so that it appends the 32 bytes Curve25519 public key of the sender to the beginning of the message.

LaurentTrk commented 2 years ago

Thanks @RoyTimes !
Yes, sr25519 tests seems covered, but it appears that coming from an ed25519 and convert it to sr255159 (the way it's done in the tests) works, whereas using a native sr25519 does not.

The test code I am using:

const keyring = new Keyring();

const senderPair = keyring.createFromUri(mnemonicGenerate(), { name: 'first pair' }, 'sr25519');
const receiverPair = keyring.createFromUri(mnemonicGenerate(), { name: 'second pair' }, 'sr25519');

const message = stringToU8a('This is a test.');
const encryptedMessage = senderPair.encryptMessage(message, receiverPair.publicKey);
const decryptedMessage = receiverPair.decryptMessage(encryptedMessage, senderPair.publicKey);

const isMatch = u8aToString(message) === u8aToString(decryptedMessage);

// Verify that the decrypted message matches the original message
console.log(`Does the decrypted message match the original message? ${isMatch}`);

Under the hood, the conversion of the sr25519 public key with the ed2curve.convertPublicKey() function fails (make sense as the ed2curve is designed to handle ed25519 keypairs.

Thanks again for your help.

RoyTimes commented 2 years ago

@LaurentTrk @jacogr I have posted a fix.

polkadot-js-bot commented 2 years ago

This issue has been open for 21 days with no activity and is not labelled as an enhancement. It will be closed in 7 days.

defliction commented 2 years ago

Thanks - ran into issues with this today, looking forward to this fix.

amany9000 commented 1 year ago

@jacogr @LaurentTrk I'm still facing this issue with sr25519, for some keys encryptMessage() works, for others it errors with Unable to convert publicKey to ed25519 from convertPublicKeyToCurve25519().

jacogr commented 1 year ago

This functionality will probably be removed completely. There is no sane way of doing this at all. (Where “sane” refers to no having to share other keys out of band)

LaurentTrk commented 1 year ago

Hi @amany9000 As @jacogr mentionned, this will not be fixed in the Polkadot JS library, nor the extension.

If you plan to use this feature from a wallet, I am aware that the Talisman Wallet team has added support for encryption/decryption recently (from the version v1.10.0).

amany9000 commented 1 year ago

Thank you for the confirmation @jacogr .

amany9000 commented 1 year ago

@LaurentTrk That's pretty interesting but I couldn't find any implementation or documentation on talisman-society. Could you please point me to any of those?

polkadot-js-bot commented 1 year ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue if you think you have a related problem or query.