2025-03-03 09:52:07 +13:00
|
|
|
import {ed25519} from "@noble/curves/ed25519";
|
2025-03-03 09:46:03 +13:00
|
|
|
import BufferReader from "./buffer_reader.js";
|
2025-03-03 09:52:07 +13:00
|
|
|
import BufferWriter from "./buffer_writer.js";
|
2025-03-03 09:46:03 +13:00
|
|
|
import Packet from "./packet.js";
|
|
|
|
|
|
|
|
|
|
class Advert {
|
|
|
|
|
|
|
|
|
|
constructor(publicKey, timestamp, signature, appData) {
|
|
|
|
|
this.publicKey = publicKey;
|
|
|
|
|
this.timestamp = timestamp;
|
|
|
|
|
this.signature = signature;
|
|
|
|
|
this.appData = appData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isVerified() {
|
2025-03-03 09:52:07 +13:00
|
|
|
|
|
|
|
|
// build signed data
|
|
|
|
|
const bufferWriter = new BufferWriter();
|
|
|
|
|
bufferWriter.writeBytes(this.publicKey);
|
|
|
|
|
bufferWriter.writeUInt32LE(this.timestamp);
|
|
|
|
|
bufferWriter.writeBytes(this.appData);
|
|
|
|
|
|
|
|
|
|
// verify signature
|
|
|
|
|
return ed25519.verify(this.signature, bufferWriter.toBytes(), this.publicKey);
|
|
|
|
|
|
2025-03-03 09:46:03 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static fromPacketBytes(bytes) {
|
|
|
|
|
|
|
|
|
|
// parse packet from bytes
|
|
|
|
|
const packet = Packet.fromBytes(bytes);
|
|
|
|
|
|
|
|
|
|
// read packet payload
|
2025-03-03 09:48:10 +13:00
|
|
|
const bufferReader = new BufferReader(packet.payload);
|
|
|
|
|
const publicKey = bufferReader.readBytes(32);
|
2025-03-03 09:52:07 +13:00
|
|
|
const timestamp = bufferReader.readUInt32LE();
|
2025-03-03 09:48:10 +13:00
|
|
|
const signature = bufferReader.readBytes(64);
|
|
|
|
|
const appData = bufferReader.readRemainingBytes();
|
2025-03-03 09:46:03 +13:00
|
|
|
|
|
|
|
|
return new Advert(publicKey, timestamp, signature, appData);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default Advert;
|