Riskless/static/js/modules/interface/packet.js

188 lines
4.5 KiB
JavaScript
Raw Normal View History

import { ID } from "./main.js";
export class Packet {
static _createBase(name) {
return {
type: name,
id: window.crypto.randomUUID(),
2023-03-07 15:43:47 +00:00
timestamp: Date.now(),
author: ID,
};
}
2023-03-04 14:19:26 +00:00
static _sign(data) {
// compute hash of data
let hash = CryptoJS.SHA3(JSON.stringify(data));
let res = 0n;
for (let word32 of hash.words) {
// remove any sign
let bi = BigInt.asUintN(32, BigInt(word32));
res = (res << 32n) | bi;
}
// sign hash
let sig = window.rsa.privKey.decrypt(res);
// return new packet
return {
2023-03-04 14:19:26 +00:00
sig: "0x" + sig.toString(16),
payload: data,
};
}
2023-03-13 14:52:14 +00:00
static createAnnounce(rsaKeyPair, paillierKeyPair) {
2023-03-04 14:19:26 +00:00
return this._sign({
...this._createBase("ANNOUNCE"),
rsaPubKey: rsaKeyPair.pubKey.toJSON(),
2023-03-13 14:52:14 +00:00
paillierPubKey: paillierKeyPair.pubKey.toJSON(),
2023-03-04 14:19:26 +00:00
});
}
static createDisconnect() {
2023-03-04 14:19:26 +00:00
return this._sign(this._createBase("DISCONNECT"));
}
static createKeepAlive() {
2023-03-04 14:19:26 +00:00
return this._sign(this._createBase("KEEPALIVE"));
}
static createSetReady(nowReady) {
2023-03-04 14:19:26 +00:00
return this._sign({
...this._createBase("ACT"),
action: "READY",
ready: nowReady,
2023-03-04 14:19:26 +00:00
});
}
2023-03-05 17:19:37 +00:00
static createRandomCyphertext(sessionId, range, cipherText) {
return this._sign({
...this._createBase("RANDOM"),
session: sessionId,
range: range,
stage: "CIPHERTEXT",
cipherText: cipherText,
});
}
static createRandomKey(sessionId, key) {
return this._sign({
...this._createBase("RANDOM"),
session: sessionId,
stage: "DECRYPT",
cipherKey: key,
});
}
static createBarrierSignal() {
2023-03-04 14:19:26 +00:00
return this._sign(this._createBase("BARRIER"));
}
2023-02-06 11:04:37 +00:00
2023-03-17 10:42:11 +00:00
static createRegionClaim(region, text) {
2023-03-05 17:19:37 +00:00
return this._sign({
2023-02-10 15:47:21 +00:00
...this._createBase("ACT"),
region: region,
2023-03-17 10:42:11 +00:00
cipherText: text,
2023-03-05 17:19:37 +00:00
});
2023-02-10 15:47:21 +00:00
}
2023-04-18 19:29:39 +00:00
static createReinforce(regionCipherTexts, verification) {
2023-03-13 14:52:14 +00:00
return this._sign({
...this._createBase("ACT"),
regions: regionCipherTexts,
2023-04-18 19:29:39 +00:00
verification: verification,
2023-03-13 14:52:14 +00:00
});
}
2023-02-17 12:46:21 +00:00
static createAction(action, startRegion, endRegion, amount) {
2023-03-04 14:19:26 +00:00
return this._sign({
2023-02-10 15:47:21 +00:00
...this._createBase("ACT"),
2023-02-17 12:46:21 +00:00
startRegion: startRegion,
endRegion: endRegion,
strength: amount,
action: action,
2023-03-04 14:19:26 +00:00
});
2023-02-06 11:04:37 +00:00
}
2023-02-16 09:38:03 +00:00
2023-04-27 11:52:02 +00:00
static createFortify(fortify) {
2023-04-24 13:20:44 +00:00
return this._sign({
...this._createBase("ACT"),
2023-04-27 11:52:02 +00:00
action: "FORTIFY",
2023-04-24 13:20:44 +00:00
fortify: fortify,
});
}
2023-02-18 15:12:06 +00:00
static createDefense(amount) {
2023-03-04 14:19:26 +00:00
return this._sign({
2023-02-18 15:12:06 +00:00
...this._createBase("ACT"),
action: "DEFENSE",
amount: amount,
2023-03-04 14:19:26 +00:00
});
2023-02-18 15:12:06 +00:00
}
2023-02-16 09:38:03 +00:00
static createEndTurn() {
2023-03-04 14:19:26 +00:00
return this._sign({
2023-02-16 09:38:03 +00:00
...this._createBase("ACT"),
action: "END",
2023-03-04 14:19:26 +00:00
});
2023-02-16 09:38:03 +00:00
}
2023-04-10 10:19:11 +00:00
static createProofRequest(region) {
return this._sign({
...this._createBase("PROOF"),
stage: "REQUEST",
region: region,
});
}
static createProofConjecture(region, plainText, a) {
return this._sign({
...this._createBase("PROOF"),
stage: "CONJECTURE",
plainText: plainText,
a: a,
region: region,
});
}
static createProofChallenge(region, challenge) {
return this._sign({
...this._createBase("PROOF"),
stage: "CHALLENGE",
challenge: challenge,
region: region,
});
}
static createProof(region, z) {
return this._sign({
...this._createBase("PROOF"),
stage: "PROOF",
z: z,
region: region,
});
}
2023-04-30 17:42:52 +00:00
static createRegionProof(proof) {
return this._sign({
...this._createBase("RESOLVE"),
action: "MAINTAIN",
proof: proof,
});
}
static createRegionYield() {
return this._sign({
...this._createBase("RESOLVE"),
action: "YIELD",
});
}
static createRegionCapture(cipherText) {
return this._sign({
...this._createBase("RESOLVE"),
action: "CAPTURE",
cipherText: cipherText,
});
}
}