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

110 lines
2.6 KiB
JavaScript
Raw Normal View History

import { ID } from "./main.js";
export class Packet {
static _createBase(name) {
return {
type: name,
id: window.crypto.randomUUID(),
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-04 14:19:26 +00:00
static createAnnounce(rsaKeyPair) {
return this._sign({
...this._createBase("ANNOUNCE"),
rsaPubKey: rsaKeyPair.pubKey.toJSON(),
});
}
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
static createRegionClaim(region) {
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-05 17:19:37 +00:00
});
2023-02-10 15:47:21 +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-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
}
}