Skip to content

Instantly share code, notes, and snippets.

@futurechimp
Last active April 23, 2020 10:32
Show Gist options
  • Save futurechimp/a5fd17822c7701e7e9e6a9221a133753 to your computer and use it in GitHub Desktop.
Save futurechimp/a5fd17822c7701e7e9e6a9221a133753 to your computer and use it in GitHub Desktop.
Sphinx WebAssembly example
import * as wasm from "nym-sphinx-wasm";
async function main() {
var gatewayUrl = "wss://path.to.gateway:1793";
var directoryUrl = "https://directory.nymtech.net/api/presence/topology";
// Get the topology, then the mixnode and provider data
const topology = await getTopology(directoryUrl);
// Set up a websocket connection to the gateway node
var connection = await connectWebsocket(gatewayUrl).then(function (c) {
return c
}).catch(function (err) {
console.log("Websocket ERROR: " + err);
})
// Set up the send button
const sendButton = document.querySelector('#send-button');
sendButton.onclick = function () {
sendMessageToMixnet(connection, topology);
}
}
// Create a Sphinx packet and send it to the mixnet through the Gateway node.
function sendMessageToMixnet(connection, topology) {
var recipient = document.getElementById("recipient").value;
var sendText = document.getElementById("sendtext").value;
let packet = wasm.create_sphinx_packet(JSON.stringify(topology), sendText, recipient);
connection.send(packet);
displaySend(packet);
display("Sent a Sphinx packet containing message: " + sendText);
}
async function getTopology(directoryUrl) {
let response = await http('get', directoryUrl);
let topology = JSON.parse(response);
return topology;
}
// Let's get started!
main();
// http and websocket connection code below here, omitted for example
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment