Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encryption/Decryption not working with sr25519 keypair #1314

Closed
LaurentTrk opened this issue Dec 12, 2021 · 13 comments · Fixed by #1762
Closed

Encryption/Decryption not working with sr25519 keypair #1314

LaurentTrk opened this issue Dec 12, 2021 · 13 comments · Fixed by #1762
Labels

Comments

@LaurentTrk
Copy link

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
Copy link
Member

jacogr commented Dec 13, 2021

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
Copy link
Author

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
Copy link
Contributor

@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
Copy link
Author

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
Copy link
Contributor

@LaurentTrk @jacogr I have posted a fix.

@polkadot-js-bot
Copy link

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
Copy link

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

@amany9000
Copy link

@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
Copy link
Member

jacogr commented Jan 11, 2023

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
Copy link
Author

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
Copy link

Thank you for the confirmation @jacogr .

@amany9000
Copy link

@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
Copy link

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.

@polkadot-js polkadot-js locked as resolved and limited conversation to collaborators Apr 4, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
6 participants