Many of the Rust tcn crate structs are now exposed via the native addon as JS classes. The instantiated JS objects hold handles to Rust structs under the hood so that we can call the native API's methods on them.
We can now run the the tcn example flow using javascript:
const { ReportAuthorizationKey, MemoType } = require("tcn-node").native;
const assert = require("assert");
// Generate a report authorization key. This key represents the capability
// to publish a report about a collection of derived temporary contact numbers.
let rak = new ReportAuthorizationKey();
// Use the temporary contact key ratchet mechanism to compute a list
// of temporary contact numbers.
let tck = rak.initial_temporary_contact_key(); // tck <- tck_1
let tcns = [];
for (let i = 0; i < 100; i++) {
tcns.push(tck.temporary_contact_number());
tck = tck.ratchet();
}
// Prepare a report about a subset of the temporary contact numbers.
let signed_report = rak.create_report(
MemoType.CoEpiV1, // The memo type
Buffer.from("symptom data"), // The memo data
20, // Index of the first TCN to disclose
90 // Index of the last TCN to check
);
// Verify the source integrity of the report...
let report = signed_report.verify();
// ...allowing the disclosed TCNs to be recomputed.
let recomputed_tcns = report.temporary_contact_numbers();
// Check that the recomputed TCNs match the originals.
// The slice is offset by 1 because tcn_0 is not included.
assert.deepEqual(recomputed_tcns, tcns.slice(20 - 1, 90 - 1));