|
| 1 | +import { encrypt } from '@metamask/eth-sig-util'; |
| 2 | +import { stringifiableToHex } from '../../utils'; |
| 3 | +import globalContext from '../..'; |
| 4 | + |
| 5 | +export function encryptDecryptComponent(parentContainer) { |
| 6 | + parentContainer.insertAdjacentHTML( |
| 7 | + 'beforeend', |
| 8 | + `<div class="row d-flex justify-content-center"> |
| 9 | + <div class="col-xl-4 col-lg-6 col-md-12 col-sm-12 col-12"> |
| 10 | + <div class="card"> |
| 11 | + <div class="card-body"> |
| 12 | + <h4 class="card-title"> |
| 13 | + Encrypt / Decrypt |
| 14 | + </h4> |
| 15 | +
|
| 16 | + <button |
| 17 | + class="btn btn-primary btn-lg btn-block mb-3" |
| 18 | + id="getEncryptionKeyButton" |
| 19 | + disabled |
| 20 | + > |
| 21 | + Get Encryption Key |
| 22 | + </button> |
| 23 | +
|
| 24 | + <hr /> |
| 25 | +
|
| 26 | + <div id="encrypt-message-form"> |
| 27 | + <input |
| 28 | + class="form-control" |
| 29 | + type="text" |
| 30 | + placeholder="Message to encrypt" |
| 31 | + id="encryptMessageInput" |
| 32 | + /> |
| 33 | +
|
| 34 | + <button |
| 35 | + class="btn btn-primary btn-lg btn-block mb-3" |
| 36 | + id="encryptButton" |
| 37 | + disabled |
| 38 | + > |
| 39 | + Encrypt |
| 40 | + </button> |
| 41 | + </div> |
| 42 | +
|
| 43 | + <hr /> |
| 44 | +
|
| 45 | + <button |
| 46 | + class="btn btn-primary btn-lg btn-block mb-3" |
| 47 | + id="decryptButton" |
| 48 | + disabled |
| 49 | + > |
| 50 | + Decrypt |
| 51 | + </button> |
| 52 | +
|
| 53 | + <p class="info-text alert alert-secondary"> |
| 54 | + Encryption key: <span id="encryptionKeyDisplay"></span> |
| 55 | + </p> |
| 56 | +
|
| 57 | + <p class="info-text text-truncate alert alert-secondary"> |
| 58 | + Ciphertext: <span id="ciphertextDisplay"></span> |
| 59 | + </p> |
| 60 | +
|
| 61 | + <p class="info-text alert alert-secondary"> |
| 62 | + Cleartext: <span id="cleartextDisplay"></span> |
| 63 | + </p> |
| 64 | + </div> |
| 65 | + </div> |
| 66 | + </div> |
| 67 | + </div>`, |
| 68 | + ); |
| 69 | + |
| 70 | + const getEncryptionKeyButton = document.getElementById( |
| 71 | + 'getEncryptionKeyButton', |
| 72 | + ); |
| 73 | + const encryptMessageInput = document.getElementById('encryptMessageInput'); |
| 74 | + const encryptButton = document.getElementById('encryptButton'); |
| 75 | + const decryptButton = document.getElementById('decryptButton'); |
| 76 | + const encryptionKeyDisplay = document.getElementById('encryptionKeyDisplay'); |
| 77 | + const ciphertextDisplay = document.getElementById('ciphertextDisplay'); |
| 78 | + const cleartextDisplay = document.getElementById('cleartextDisplay'); |
| 79 | + |
| 80 | + document.addEventListener('globalConnectionChange', function (e) { |
| 81 | + if (e.detail.connected) { |
| 82 | + // MetaMask is connected, enable the button |
| 83 | + getEncryptionKeyButton.disabled = false; |
| 84 | + } |
| 85 | + }); |
| 86 | + |
| 87 | + document.addEventListener('disableAndClear', function () { |
| 88 | + getEncryptionKeyButton.disabled = true; |
| 89 | + encryptMessageInput.disabled = true; |
| 90 | + encryptButton.disabled = true; |
| 91 | + decryptButton.disabled = true; |
| 92 | + encryptionKeyDisplay.innerText = ''; |
| 93 | + encryptMessageInput.value = ''; |
| 94 | + ciphertextDisplay.innerText = ''; |
| 95 | + cleartextDisplay.innerText = ''; |
| 96 | + }); |
| 97 | + |
| 98 | + /** |
| 99 | + * Encrypt / Decrypt |
| 100 | + */ |
| 101 | + |
| 102 | + getEncryptionKeyButton.onclick = async () => { |
| 103 | + try { |
| 104 | + encryptionKeyDisplay.innerText = await globalContext.provider.request({ |
| 105 | + method: 'eth_getEncryptionPublicKey', |
| 106 | + params: [globalContext.accounts[0]], |
| 107 | + }); |
| 108 | + encryptMessageInput.disabled = false; |
| 109 | + } catch (error) { |
| 110 | + encryptionKeyDisplay.innerText = `Error: ${error.message}`; |
| 111 | + encryptMessageInput.disabled = true; |
| 112 | + encryptButton.disabled = true; |
| 113 | + decryptButton.disabled = true; |
| 114 | + } |
| 115 | + }; |
| 116 | + |
| 117 | + encryptMessageInput.onkeyup = () => { |
| 118 | + if ( |
| 119 | + !getEncryptionKeyButton.disabled && |
| 120 | + encryptMessageInput.value.length > 0 |
| 121 | + ) { |
| 122 | + if (encryptButton.disabled) { |
| 123 | + encryptButton.disabled = false; |
| 124 | + } |
| 125 | + } else if (!encryptButton.disabled) { |
| 126 | + encryptButton.disabled = true; |
| 127 | + } |
| 128 | + }; |
| 129 | + |
| 130 | + encryptButton.onclick = () => { |
| 131 | + try { |
| 132 | + ciphertextDisplay.innerText = stringifiableToHex( |
| 133 | + encrypt({ |
| 134 | + publicKey: encryptionKeyDisplay.innerText, |
| 135 | + data: encryptMessageInput.value, |
| 136 | + version: 'x25519-xsalsa20-poly1305', |
| 137 | + }), |
| 138 | + ); |
| 139 | + decryptButton.disabled = false; |
| 140 | + } catch (error) { |
| 141 | + ciphertextDisplay.innerText = `Error: ${error.message}`; |
| 142 | + decryptButton.disabled = true; |
| 143 | + } |
| 144 | + }; |
| 145 | + |
| 146 | + decryptButton.onclick = async () => { |
| 147 | + try { |
| 148 | + cleartextDisplay.innerText = await globalContext.provider.request({ |
| 149 | + method: 'eth_decrypt', |
| 150 | + params: [ciphertextDisplay.innerText, globalContext.accounts[0]], |
| 151 | + }); |
| 152 | + } catch (error) { |
| 153 | + cleartextDisplay.innerText = `Error: ${error.message}`; |
| 154 | + } |
| 155 | + }; |
| 156 | +} |
0 commit comments