Guide ยท @toip-tools/mesh-transport, @toip-tools/trust-core
Send Trust Tasks over LoRa radio
Send a signed, encrypted Trust Task over LoRa with no internet: compacted, fragmented into radio frames, acknowledged and reassembled.
LoRa radios carry a few hundred bytes at a time over kilometers, with no internet. @toip-tools/mesh-transport carries TSP over them: the same signed, encrypted Trust Tasks an Endpoint sends over a relay, so a field sensor or a door lock can check a credential offline.
This program runs on the package's simulated radio, so it needs no hardware: a sensor sends a soil-moisture reading to a base station, which answers with a signed reply.
Before you start
You need Node 22.13 or later. In a new folder, install the packages and tsx, a TypeScript runner:
mkdir trust-tasks-over-lora && cd trust-tasks-over-lora
npm init -y && npm pkg set type=module
npm install @toip-tools/mesh-transport @toip-tools/trust-core tsx
The code
Save this as trust-tasks-over-lora.ts. It is the complete program; the site's checks run it on every build.
// Send a signed, encrypted Trust Task over LoRa radio, with no internet and no relay. This runs on the
// simulated radio from @toip-tools/mesh-transport/sim; real radios use the same MeshTransport.
import { MeshTransport, plans, regions } from "@toip-tools/mesh-transport";
import { SimMedium, VirtualScheduler } from "@toip-tools/mesh-transport/sim";
import { Endpoint, ctt1, identityFromSeed } from "@toip-tools/trust-core";
const random = () => crypto.getRandomValues(new Uint8Array(32));
const sensor = identityFromSeed(random());
const base = identityFromSeed(random());
// A simulated LoRa channel: US915, 250-byte frames, and the US airtime budget.
const scheduler = new VirtualScheduler();
const medium = new SimMedium({ scheduler, seed: 1 });
const radio = { setting: plans["us915-a"].tx, mtu: 250, budget: regions.us915 };
const sensorMesh = new MeshTransport({ identities: [sensor], links: [medium.node("sensor", radio)], scheduler, random: () => 0.5 });
const baseMesh = new MeshTransport({ identities: [base], links: [medium.node("base", radio)], scheduler, random: () => 0.5 });
medium.connect("sensor", "base"); // in range of each other
// The Endpoint is the same one you'd use over a relay. CTT1 is a compact CBOR body that decodes to the
// identical signed document, so the Data Integrity proof still verifies.
const compact = (mesh: MeshTransport) => (to: string) => (mesh.supportsCompact(to) ? ctt1 : undefined);
const sensorEndpoint = new Endpoint({ identity: sensor, transport: sensorMesh, clock: scheduler, bodyCodecFor: compact(sensorMesh) });
const baseEndpoint = new Endpoint({ identity: base, transport: baseMesh, clock: scheduler, bodyCodecFor: compact(baseMesh) });
const READING = {
typeUri: "https://example.org/spec/field/reading/0.1",
isBearer: false,
isProofRequired: true,
isRecipientRequired: true,
isIssuedAtRequired: true,
};
baseEndpoint.handle<{ soilMoisture: number }, { stored: boolean }>(READING, (doc, { sender }) => {
console.log(`Base station got ${doc.payload.soilMoisture}% from ${sender === sensor.did ? "the sensor" : sender}`);
return { stored: true };
});
try {
await sensorMesh.start();
await baseMesh.start();
sensorMesh.addContact(base.did); // a did:key resolves locally: no lookup over the air
baseMesh.addContact(sensor.did);
// run() advances the simulated radio's clock while the request waits for its signed reply.
const reply = await scheduler.run(
sensorEndpoint.request<{ soilMoisture: number }, { stored: boolean }>(base.did, READING, { soilMoisture: 31 }, { timeoutMs: 60_000 }),
);
console.log("Reply:", reply.payload, "signed by the base:", reply.issuer === base.did);
const frames = medium.records();
console.log(`${frames.length} radio frames in both directions, ${frames.reduce((n, f) => n + f.bytes.length, 0)} bytes on the air, largest ${Math.max(...frames.map((f) => f.bytes.length))} bytes`);
} finally {
sensorEndpoint.close();
baseEndpoint.close();
await sensorMesh.stop();
await baseMesh.stop();
}
Run it:
npx tsx trust-tasks-over-lora.ts
Base station got 31% from the sensor
Reply: { stored: true } signed by the base: true
5 radio frames in both directions, 763 bytes on the air, largest 250 bytes
Keys are random, so identifiers and sizes differ on each run. From a clone of the repository, run npx tsx --tsconfig site/tsconfig.json site/examples/guides/trust-tasks-over-lora.ts, or read it in site/examples/guides/trust-tasks-over-lora.ts.
How it works
MeshTransportimplements trust-core'sTransport, so theEndpoint, the message type and the handler are the same as over a relay. The bytes the base station opens are exactly the TSP bytes the sensor sealed.- CTT1 (
ctt1, in trust-core) is a compact CBOR body that decodes to the identical document, so the Data Integrity proof still verifies. - The TSP envelope names both identifiers in clear, so it never goes on the air. A packet carries the encrypted parts and a fresh 4-byte tag that only the recipient can match to the sender.
- Packets are cut into frames no larger than the link's MTU (250 bytes here), acknowledged with a bitmap, and only the missing fragments are resent. A scheduler keeps each link inside its airtime budget (10% of channel time in the US region profile).
VirtualScheduler.run()advances simulated time while the request waits. Real radio adapters run in real time and don't need it.
Limits and production notes
- No sealed Trust Task fits in one 250-byte frame; two frames is the floor.
- Adapters for real hardware: a LoRaWAN gateway's packet forwarder (
GwmpServer, Node), a RAK3172 in LoRa P2P mode (RakP2PLink), and USB or Web Serial ports. See the library page.