msgLoop works

This commit is contained in:
open-trade
2022-01-20 18:02:20 +08:00
parent 70c213a60a
commit b8ff266d76
4 changed files with 129 additions and 30 deletions

View File

@@ -8,13 +8,19 @@ export function setConn(conn) {
}
export function getConn() {
return windows.currentConnection;
return window.currentConnection;
}
export async function startConn(id) {
export function close() {
getConn()?.close();
setConn(undefined);
}
export function newConn() {
window.currentConnection?.close();
const conn = new Connection();
setConn(conn);
await conn.start('124931507');
return conn;
}
let sodium;
@@ -49,12 +55,22 @@ export function seal(unsigned, theirPk, ourSk) {
return sodium.crypto_box_easy(unsigned, nonce, theirPk, ourSk);
}
function makeOnce(value) {
var byteArray = Array(24).fill(0);
for (var index = 0; index < byteArray.length && value > 0; index++) {
var byte = value & 0xff;
byteArray[index] = byte;
value = (value - byte) / 256;
}
return Uint8Array.from(byteArray);
};
export function encrypt(unsigned, nonce, key) {
return sodium.crypto_secretbox_easy(unsigned, nonce, key);
return sodium.crypto_secretbox_easy(unsigned, makeOnce(nonce), key);
}
export function decrypt(signed, nonce, key) {
return sodium.crypto_secretbox_open_easy(signed, nonce, key);
}
window.startConn = startConn;
return sodium.crypto_secretbox_open_easy(signed, makeOnce(nonce), key);
}