Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"arrowParens": "always",
"semi": true,
"singleQuote": true,
"tabWidth": 2,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The codebase is already formatted with tab width 4. Keeping that will significantly reduce the number of changes you're trying to make all at once.

Suggested change
"tabWidth": 2,
"tabWidth": 4,

"trailingComma": "all"
}
19 changes: 19 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@
"main": "src/index.js",
"type": "module",
"scripts": {
"prettier": "prettier --check src",
"prettier-write": "prettier --write src",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Liam Cottle <liam@liamcottle.com>",
"license": "MIT",
"dependencies": {
"@noble/curves": "^1.8.1",
"serialport": "^13.0.0"
},
"devDependencies": {
"prettier": "^3.6.2"
}
}
180 changes: 88 additions & 92 deletions src/advert.js
Original file line number Diff line number Diff line change
@@ -1,101 +1,97 @@
import BufferReader from "./buffer_reader.js";
import BufferWriter from "./buffer_writer.js";
import BufferReader from './buffer_reader.js';
import BufferWriter from './buffer_writer.js';

class Advert {

static ADV_TYPE_NONE = 0;
static ADV_TYPE_CHAT = 1;
static ADV_TYPE_REPEATER = 2;
static ADV_TYPE_ROOM = 3;

static ADV_LATLON_MASK = 0x10;
static ADV_BATTERY_MASK = 0x20;
static ADV_TEMPERATURE_MASK = 0x40;
static ADV_NAME_MASK = 0x80;

constructor(publicKey, timestamp, signature, appData) {
this.publicKey = publicKey;
this.timestamp = timestamp;
this.signature = signature;
this.appData = appData;
this.parsed = this.parseAppData();
}

static fromBytes(bytes) {

// read bytes
const bufferReader = new BufferReader(bytes);
const publicKey = bufferReader.readBytes(32);
const timestamp = bufferReader.readUInt32LE();
const signature = bufferReader.readBytes(64);
const appData = bufferReader.readRemainingBytes();

return new Advert(publicKey, timestamp, signature, appData);

}

getFlags() {
return this.appData[0];
}

getType() {
const flags = this.getFlags();
return flags & 0x0F;
static ADV_TYPE_NONE = 0;
static ADV_TYPE_CHAT = 1;
static ADV_TYPE_REPEATER = 2;
static ADV_TYPE_ROOM = 3;

static ADV_LATLON_MASK = 0x10;
static ADV_BATTERY_MASK = 0x20;
static ADV_TEMPERATURE_MASK = 0x40;
static ADV_NAME_MASK = 0x80;

constructor(publicKey, timestamp, signature, appData) {
this.publicKey = publicKey;
this.timestamp = timestamp;
this.signature = signature;
this.appData = appData;
this.parsed = this.parseAppData();
}

static fromBytes(bytes) {
// read bytes
const bufferReader = new BufferReader(bytes);
const publicKey = bufferReader.readBytes(32);
const timestamp = bufferReader.readUInt32LE();
const signature = bufferReader.readBytes(64);
const appData = bufferReader.readRemainingBytes();

return new Advert(publicKey, timestamp, signature, appData);
}

getFlags() {
return this.appData[0];
}

getType() {
const flags = this.getFlags();
return flags & 0x0f;
}

getTypeString() {
const type = this.getType();
if (type === Advert.ADV_TYPE_NONE) return 'NONE';
if (type === Advert.ADV_TYPE_CHAT) return 'CHAT';
if (type === Advert.ADV_TYPE_REPEATER) return 'REPEATER';
if (type === Advert.ADV_TYPE_ROOM) return 'ROOM';
return null;
}

async isVerified() {
const { ed25519 } = await import('@noble/curves/ed25519');

// 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,
);
}

parseAppData() {
// read app data
const bufferReader = new BufferReader(this.appData);
const flags = bufferReader.readByte();

// parse lat lon
var lat = null;
var lon = null;
if (flags & Advert.ADV_LATLON_MASK) {
lat = bufferReader.readInt32LE();
lon = bufferReader.readInt32LE();
}

getTypeString() {
const type = this.getType();
if(type === Advert.ADV_TYPE_NONE) return "NONE";
if(type === Advert.ADV_TYPE_CHAT) return "CHAT";
if(type === Advert.ADV_TYPE_REPEATER) return "REPEATER";
if(type === Advert.ADV_TYPE_ROOM) return "ROOM";
return null;
}

async isVerified() {

const { ed25519 } = await import("@noble/curves/ed25519");

// 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);

}

parseAppData() {

// read app data
const bufferReader = new BufferReader(this.appData);
const flags = bufferReader.readByte();

// parse lat lon
var lat = null;
var lon = null;
if(flags & Advert.ADV_LATLON_MASK){
lat = bufferReader.readInt32LE();
lon = bufferReader.readInt32LE();
}

// parse name (remainder of app data)
var name = null;
if(flags & Advert.ADV_NAME_MASK){
name = bufferReader.readString();
}

return {
type: this.getTypeString(),
lat: lat,
lon: lon,
name: name,
};

// parse name (remainder of app data)
var name = null;
if (flags & Advert.ADV_NAME_MASK) {
name = bufferReader.readString();
}

return {
type: this.getTypeString(),
lat: lat,
lon: lon,
name: name,
};
}
}

export default Advert;
Loading