From b0e9a3d6341faaccc17a6d518607d48640747f71 Mon Sep 17 00:00:00 2001 From: liamcottle Date: Mon, 3 Mar 2025 09:46:03 +1300 Subject: [PATCH] add advert parser and verifier --- package-lock.json | 30 +++++++++++++++++++++++++++++- package.json | 5 ++++- src/advert.js | 44 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 src/advert.js diff --git a/package-lock.json b/package-lock.json index ce706f9..8ad0220 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7,7 +7,35 @@ "": { "name": "@liamcottle/meshcore.js", "version": "1.0.9", - "license": "MIT" + "license": "MIT", + "dependencies": { + "@noble/curves": "^1.8.1" + } + }, + "node_modules/@noble/curves": { + "version": "1.8.1", + "resolved": "https://registry.npmjs.org/@noble/curves/-/curves-1.8.1.tgz", + "integrity": "sha512-warwspo+UYUPep0Q+vtdVB4Ugn8GGQj8iyB3gnRWsztmUHTI3S1nhdiWNsPUGL0vud7JlRRk1XEu7Lq1KGTnMQ==", + "dependencies": { + "@noble/hashes": "1.7.1" + }, + "engines": { + "node": "^14.21.3 || >=16" + }, + "funding": { + "url": "https://paulmillr.com/funding/" + } + }, + "node_modules/@noble/hashes": { + "version": "1.7.1", + "resolved": "https://registry.npmjs.org/@noble/hashes/-/hashes-1.7.1.tgz", + "integrity": "sha512-B8XBPsn4vT/KJAGqDzbwztd+6Yte3P4V7iafm24bxgDe/mlRuK6xmWPuCNrKt2vDafZ8MfJLlchDG/vYafQEjQ==", + "engines": { + "node": "^14.21.3 || >=16" + }, + "funding": { + "url": "https://paulmillr.com/funding/" + } } } } diff --git a/package.json b/package.json index 83cc762..ea1ea01 100644 --- a/package.json +++ b/package.json @@ -8,5 +8,8 @@ "test": "echo \"Error: no test specified\" && exit 1" }, "author": "Liam Cottle ", - "license": "MIT" + "license": "MIT", + "dependencies": { + "@noble/curves": "^1.8.1" + } } diff --git a/src/advert.js b/src/advert.js new file mode 100644 index 0000000..5a4d45a --- /dev/null +++ b/src/advert.js @@ -0,0 +1,44 @@ +import { ed25519 } from "@noble/curves/ed25519"; +import BufferReader from "./buffer_reader.js"; +import Packet from "./packet.js"; + +class Advert { + + constructor(publicKey, timestamp, signature, appData) { + this.publicKey = publicKey; + this.timestamp = timestamp; + this.signature = signature; + this.appData = appData; + } + + getTimestamp() { + return (new BufferReader(this.timestamp)).readUInt32LE(); + } + + isVerified() { + return ed25519.verify(this.signature, Buffer.concat([ + this.publicKey, + this.timestamp, + this.appData, + ]), this.publicKey); + } + + static fromPacketBytes(bytes) { + + // parse packet from bytes + const packet = Packet.fromBytes(bytes); + + // read packet payload + const bufferedReader = new BufferReader(packet.payload); + const publicKey = bufferedReader.readBytes(32); + const timestamp = bufferedReader.readBytes(4); // read as bytes for signature + const signature = bufferedReader.readBytes(64); + const appData = bufferedReader.readRemainingBytes(); + + return new Advert(publicKey, timestamp, signature, appData); + + } + +} + +export default Advert;