refactor: switch to use exchangeCommunicationKeys and setLoginKey

This commit is contained in:
Derrick Hammer 2023-07-28 21:28:53 -04:00
parent c513404d96
commit 0706a61817
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 6 additions and 31 deletions

View File

@ -12,6 +12,10 @@ import browser from "webextension-polyfill";
import { bytesToHex, hexToBytes, randomBytes } from "@lumeweb/libweb"; import { bytesToHex, hexToBytes, randomBytes } from "@lumeweb/libweb";
import { secretbox } from "@noble/ciphers/salsa"; import { secretbox } from "@noble/ciphers/salsa";
import "./App.scss"; import "./App.scss";
import {
exchangeCommunicationKeys,
setLoginKey,
} from "../../../shared/keys.js";
const BIP44_PATH = "m/44'/1627'/0'/0'/0'"; const BIP44_PATH = "m/44'/1627'/0'/0'/0'";
@ -123,37 +127,8 @@ export default function App() {
const seed = await bip39.mnemonicToSeed(wordSeed); const seed = await bip39.mnemonicToSeed(wordSeed);
const key = HDKey.fromMasterSeed(seed).derive(BIP44_PATH); const key = HDKey.fromMasterSeed(seed).derive(BIP44_PATH);
let pubKey; await exchangeCommunicationKeys();
let privKey = x25519.utils.randomPrivateKey(); await setLoginKey(key.privateKey);
try {
pubKey = await browser.runtime.sendMessage({
method: "exchangeCommunicationKeys",
data: bytesToHex(x25519.getPublicKey(privKey)),
});
} catch (e) {
alert(`Failed to login: ${e.message}`);
return;
}
if (!pubKey) {
alert(`Failed to login: could not get communication key`);
return;
}
pubKey = hexToBytes(pubKey);
const secret = x25519.getSharedSecret(privKey, pubKey);
const nonce = randomBytes(24);
const box = secretbox(secret, nonce);
const ciphertext = box.seal(key.privateKey);
await browser.runtime.sendMessage({
method: "setLoginKey",
data: {
data: bytesToHex(ciphertext),
nonce: bytesToHex(nonce),
},
});
window.setTimeout(() => { window.setTimeout(() => {
window.location.href = "/dashboard.html"; window.location.href = "/dashboard.html";