From 7ff26e1b2f532283435d77feaee1a0581b2f7ec1 Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Fri, 3 Sep 2021 18:09:21 -0700 Subject: [PATCH] Improve encrypt/decrypt flow Made encrypt/decrypt a "fully functional" flow, so the test dapp can actually be used as an encrypted messenger. Fixes #124 --- src/index.html | 39 ++++++++++++++++++++++++++++----------- src/index.js | 27 +++++++++------------------ 2 files changed, 37 insertions(+), 29 deletions(-) diff --git a/src/index.html b/src/index.html index 8c9299e9..538cae78 100644 --- a/src/index.html +++ b/src/index.html @@ -258,12 +258,17 @@

+

+ Public Encryption key: +


+

+ Encrypt +
placeholder="Message to encrypt" id="encryptMessageInput" /> + + +

+ Ciphertext: +


+
+ Decrypt +
+ + + -

- Encryption key: -

- -

- Ciphertext: -

Cleartext: diff --git a/src/index.js b/src/index.js index 3a84d839..ea4331de 100644 --- a/src/index.js +++ b/src/index.js @@ -6,6 +6,7 @@ import { recoverTypedSignatureLegacy, recoverTypedSignature, recoverTypedSignature_v4 as recoverTypedSignatureV4, + decrypt, } from 'eth-sig-util'; import { ethers } from 'ethers'; import { toChecksumAddress } from 'ethereumjs-util'; @@ -69,6 +70,8 @@ const getEncryptionKeyButton = document.getElementById( 'getEncryptionKeyButton', ); const encryptMessageInput = document.getElementById('encryptMessageInput'); +const publicKeyInput = document.getElementById('publicKeyInput'); +const encryptedMessageInput = document.getElementById('encryptedMessageInput'); const encryptButton = document.getElementById('encryptButton'); const decryptButton = document.getElementById('decryptButton'); const encryptionKeyDisplay = document.getElementById('encryptionKeyDisplay'); @@ -204,17 +207,21 @@ const initialize = async () => { createToken.disabled = false; personalSign.disabled = false; signTypedData.disabled = false; - getEncryptionKeyButton.disabled = false; ethSign.disabled = false; personalSign.disabled = false; signTypedData.disabled = false; signTypedDataV3.disabled = false; signTypedDataV4.disabled = false; + getEncryptionKeyButton.disabled = false; + encryptButton.disabled = false; + decryptButton.disabled = false; } if (isMetaMaskInstalled()) { addEthereumChain.disabled = false; switchEthereumChain.disabled = false; + encryptMessageInput.disabled = false; + encryptedMessageInput.disabled = false; } else { onboardButton.innerText = 'Click here to install MetaMask!'; onboardButton.onclick = onClickInstall; @@ -511,32 +518,17 @@ const initialize = async () => { } }; - encryptMessageInput.onkeyup = () => { - if ( - !getEncryptionKeyButton.disabled && - encryptMessageInput.value.length > 0 - ) { - if (encryptButton.disabled) { - encryptButton.disabled = false; - } - } else if (!encryptButton.disabled) { - encryptButton.disabled = true; - } - }; - encryptButton.onclick = () => { try { ciphertextDisplay.innerText = stringifiableToHex( encrypt( - encryptionKeyDisplay.innerText, + publicKeyInput.value, { data: encryptMessageInput.value }, 'x25519-xsalsa20-poly1305', ), ); - decryptButton.disabled = false; } catch (error) { ciphertextDisplay.innerText = `Error: ${error.message}`; - decryptButton.disabled = true; } }; @@ -585,7 +577,6 @@ const initialize = async () => { params: [msg, from, 'Example password'], }); personalSignResult.innerHTML = sign; - personalSignVerify.disabled = false; } catch (err) { console.error(err); personalSign.innerHTML = `Error: ${err.message}`;