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

Improve encrypt/decrypt flow #125

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 28 additions & 11 deletions src/index.html
Expand Up @@ -258,12 +258,17 @@ <h4 class="card-title">
<button
class="btn btn-primary btn-lg btn-block mb-3"
id="getEncryptionKeyButton"
disabled
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this removed? The intention was to have all of the buttons that rely upon an account being connected to start as disabled until we know whether something is connected or not.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same goes for the "decrypt" button.

>
Get Encryption Key
Get Public Encryption Key
</button>

<p class="info-text alert alert-secondary">
Public Encryption key: <span id="encryptionKeyDisplay"></span>
</p>
<hr />
<h5 class="card-title">
Encrypt
</h5>

<div id="encrypt-message-form">
<input
Expand All @@ -272,33 +277,45 @@ <h4 class="card-title">
placeholder="Message to encrypt"
id="encryptMessageInput"
/>
<input
class="form-control"
type="text"
placeholder="Recipient's Public Encryption Key"
id="publicKeyInput"
/>

<button
class="btn btn-primary btn-lg btn-block mb-3"
id="encryptButton"
disabled
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, this one was meant to be disabled until we have a public encryption key to encrypt with.

>
Encrypt
</button>

<p class="info-text alert alert-secondary">
Ciphertext: <span id="ciphertextDisplay"></span>
</p>
</div>

<hr />
<h5 class="card-title">
Decrypt
</h5>


<input
class="form-control"
type="text"
placeholder="Encrypted Message"
id="encryptedMessageInput"
/>

<button
class="btn btn-primary btn-lg btn-block mb-3"
id="decryptButton"
disabled
>
Decrypt
</button>

<p class="info-text alert alert-secondary">
Encryption key: <span id="encryptionKeyDisplay"></span>
</p>

<p class="info-text text-truncate alert alert-secondary">
Ciphertext: <span id="ciphertextDisplay"></span>
</p>

<p class="info-text alert alert-secondary">
Cleartext: <span id="cleartextDisplay"></span>
Expand Down
27 changes: 9 additions & 18 deletions src/index.js
Expand Up @@ -6,6 +6,7 @@ import {
recoverTypedSignatureLegacy,
recoverTypedSignature,
recoverTypedSignature_v4 as recoverTypedSignatureV4,
decrypt,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is causing the linter to fail

} from 'eth-sig-util';
import { ethers } from 'ethers';
import { toChecksumAddress } from 'ethereumjs-util';
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -511,32 +518,17 @@ const initialize = async () => {
}
};

encryptMessageInput.onkeyup = () => {
if (
!getEncryptionKeyButton.disabled &&
encryptMessageInput.value.length > 0
) {
if (encryptButton.disabled) {
encryptButton.disabled = false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this deleted to allow encrypting an empty message? I'm not sure why that was disallowed to being with 🤔 Does that work?

}
} 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;
}
};

Expand Down Expand Up @@ -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}`;
Expand Down