mirror of
https://github.com/Qortal/qortal-ui.git
synced 2025-03-31 01:35:54 +00:00
added two events for encryption
This commit is contained in:
parent
595d16af86
commit
64ded72e8e
@ -36,3 +36,9 @@ export const ADD_LIST_ITEMS = 'ADD_LIST_ITEMS'
|
|||||||
|
|
||||||
// DELETE_LIST_ITEM
|
// DELETE_LIST_ITEM
|
||||||
export const DELETE_LIST_ITEM = 'DELETE_LIST_ITEM'
|
export const DELETE_LIST_ITEM = 'DELETE_LIST_ITEM'
|
||||||
|
|
||||||
|
// ENCRYPT_DATA
|
||||||
|
export const ENCRYPT_DATA = 'ENCRYPT_DATA'
|
||||||
|
|
||||||
|
// DECRYPT_DATA
|
||||||
|
export const DECRYPT_DATA = 'DECRYPT_DATA'
|
@ -8,11 +8,11 @@ import {
|
|||||||
translateUnsafeHTML,
|
translateUnsafeHTML,
|
||||||
registerTranslateConfig,
|
registerTranslateConfig,
|
||||||
} from 'lit-translate';
|
} from 'lit-translate';
|
||||||
import * as actions from '../../components/qdn-action-types';
|
|
||||||
registerTranslateConfig({
|
registerTranslateConfig({
|
||||||
loader: (lang) => fetch(`/language/${lang}.json`).then((res) => res.json()),
|
loader: (lang) => fetch(`/language/${lang}.json`).then((res) => res.json()),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
import * as actions from '../../components/qdn-action-types';
|
||||||
import '@material/mwc-button';
|
import '@material/mwc-button';
|
||||||
import '@material/mwc-icon';
|
import '@material/mwc-icon';
|
||||||
import '@material/mwc-checkbox'
|
import '@material/mwc-checkbox'
|
||||||
@ -21,6 +21,8 @@ import WebWorkerChat from 'web-worker:./computePowWorker.src.js';
|
|||||||
import { publishData } from '../../../utils/publish-image.js';
|
import { publishData } from '../../../utils/publish-image.js';
|
||||||
import { Loader } from '../../../utils/loader.js';
|
import { Loader } from '../../../utils/loader.js';
|
||||||
import { QORT_DECIMALS } from 'qortal-ui-crypto/api/constants';
|
import { QORT_DECIMALS } from 'qortal-ui-crypto/api/constants';
|
||||||
|
import nacl from '../../../../../qortal-ui-crypto/api/deps/nacl-fast.js'
|
||||||
|
import ed2curve from '../../../../../qortal-ui-crypto/api/deps/nacl-fast.js'
|
||||||
const parentEpml = new Epml({ type: 'WINDOW', source: window.parent });
|
const parentEpml = new Epml({ type: 'WINDOW', source: window.parent });
|
||||||
|
|
||||||
class WebBrowser extends LitElement {
|
class WebBrowser extends LitElement {
|
||||||
@ -558,6 +560,121 @@ class WebBrowser extends LitElement {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
case actions.ENCRYPT_DATA: {
|
||||||
|
const requiredFields = ['Uint8ArrayData', 'destinationPublicKey'];
|
||||||
|
const missingFields = [];
|
||||||
|
|
||||||
|
requiredFields.forEach((field) => {
|
||||||
|
if (!data[field]) {
|
||||||
|
missingFields.push(field);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (missingFields.length > 0) {
|
||||||
|
const missingFieldsString = missingFields.join(', ');
|
||||||
|
const errorMsg = `Missing fields: ${missingFieldsString}`
|
||||||
|
let data = {};
|
||||||
|
data['error'] = errorMsg;
|
||||||
|
response = JSON.stringify(data);
|
||||||
|
break
|
||||||
|
}
|
||||||
|
const { Uint8ArrayData, destinationPublicKey } = data
|
||||||
|
|
||||||
|
if (!(Uint8ArrayData instanceof Uint8Array)) {
|
||||||
|
data['error'] = "The Uint8ArrayData you've submitted is invalid";
|
||||||
|
response = JSON.stringify(data);
|
||||||
|
break
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const privateKey = window.parent.reduxStore.getState().app.selectedAddress.keyPair.privateKey
|
||||||
|
if (!privateKey) {
|
||||||
|
data['error'] = "Unable to retrieve keys"
|
||||||
|
response = JSON.stringify(data);
|
||||||
|
break
|
||||||
|
}
|
||||||
|
const convertedPrivateKey = ed2curve.convertSecretKey(privateKey)
|
||||||
|
const convertedPublicKey = ed2curve.convertPublicKey(destinationPublicKey)
|
||||||
|
const sharedSecret = new Uint8Array(32)
|
||||||
|
nacl.lowlevel.crypto_scalarmult(sharedSecret, convertedPrivateKey, convertedPublicKey)
|
||||||
|
|
||||||
|
const chatEncryptionSeed = new Sha256().process(sharedSecret).finish().result
|
||||||
|
|
||||||
|
const nonce = new Uint8Array(24);
|
||||||
|
window.crypto.getRandomValues(nonce);
|
||||||
|
const encryptedData = nacl.secretbox(Uint8ArrayData, nonce, chatEncryptionSeed)
|
||||||
|
const combinedData = new Uint8Array(nonce.length + encryptedData.length);
|
||||||
|
combinedData.set(nonce);
|
||||||
|
combinedData.set(encryptedData, nonce.length);
|
||||||
|
|
||||||
|
|
||||||
|
let data = {};
|
||||||
|
data['encryptedData'] = combinedData
|
||||||
|
data['destinationPublicKey'] = destinationPublicKey
|
||||||
|
response = JSON.stringify(data);
|
||||||
|
break;
|
||||||
|
} catch (error) {
|
||||||
|
const data = {};
|
||||||
|
const errorMsg = error.message || "Error in encrypting data"
|
||||||
|
data['error'] = errorMsg;
|
||||||
|
response = JSON.stringify(data);
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case actions.DECRYPT_DATA: {
|
||||||
|
const requiredFields = ['encryptedData'];
|
||||||
|
const missingFields = [];
|
||||||
|
|
||||||
|
requiredFields.forEach((field) => {
|
||||||
|
if (!data[field]) {
|
||||||
|
missingFields.push(field);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (missingFields.length > 0) {
|
||||||
|
const missingFieldsString = missingFields.join(', ');
|
||||||
|
const errorMsg = `Missing fields: ${missingFieldsString}`
|
||||||
|
let data = {};
|
||||||
|
data['error'] = errorMsg;
|
||||||
|
response = JSON.stringify(data);
|
||||||
|
break
|
||||||
|
}
|
||||||
|
const { encryptedData } = data
|
||||||
|
|
||||||
|
|
||||||
|
try {
|
||||||
|
const combinedData = encryptedData
|
||||||
|
const nonce = combinedData.slice(0, 24);
|
||||||
|
const _encryptedData = combinedData.slice(24);
|
||||||
|
|
||||||
|
const privateKey = window.parent.reduxStore.getState().app.selectedAddress.keyPair.privateKey
|
||||||
|
const publicKey = window.parent.reduxStore.getState().app.selectedAddress.keyPair.publicKey
|
||||||
|
|
||||||
|
if (!privateKey || !publicKey) {
|
||||||
|
data['error'] = "Unable to retrieve keys"
|
||||||
|
response = JSON.stringify(data);
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
const convertedPrivateKey = ed2curve.convertSecretKey(privateKey)
|
||||||
|
const convertedPublicKey = ed2curve.convertPublicKey(publicKey)
|
||||||
|
const sharedSecret = new Uint8Array(32);
|
||||||
|
nacl.lowlevel.crypto_scalarmult(sharedSecret, convertedPrivateKey, convertedPublicKey)
|
||||||
|
|
||||||
|
const _chatEncryptionSeed = new Sha256().process(sharedSecret).finish().result
|
||||||
|
const _decryptedData = nacl.secretbox.open(_encryptedData, nonce, _chatEncryptionSeed)
|
||||||
|
|
||||||
|
let data = {};
|
||||||
|
data['decryptedData'] = _decryptedData
|
||||||
|
response = JSON.stringify(data);
|
||||||
|
break;
|
||||||
|
} catch (error) {
|
||||||
|
const data = {};
|
||||||
|
const errorMsg = error.message || "Error in decrypting data"
|
||||||
|
data['error'] = errorMsg;
|
||||||
|
response = JSON.stringify(data);
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
case actions.GET_LIST_ITEMS: {
|
case actions.GET_LIST_ITEMS: {
|
||||||
const requiredFields = ['list_name'];
|
const requiredFields = ['list_name'];
|
||||||
const missingFields = [];
|
const missingFields = [];
|
||||||
|
Loading…
x
Reference in New Issue
Block a user