Last active
April 23, 2020 10:32
-
-
Save futurechimp/a5fd17822c7701e7e9e6a9221a133753 to your computer and use it in GitHub Desktop.
Sphinx WebAssembly example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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