Skip to content

Commit 5ef2a22

Browse files
authored
Adding some documentation to the webassembly client (#244)
* Adding some documentation to the webassembly client * Noting that binary is unsupported for now
1 parent 1f749df commit 5ef2a22

File tree

6 files changed

+121
-24
lines changed

6 files changed

+121
-24
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

clients/webassembly/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "nym-client-wasm"
33
authors = ["Dave Hrycyszyn <futurechimp@users.noreply.github.com>", "Jedrzej Stuczynski <andrew@nymtech.net>"]
4-
version = "0.7.2"
4+
version = "0.7.3"
55
edition = "2018"
66
keywords = ["nym", "sphinx", "wasm", "webassembly", "privacy", "client"]
77
license = "Apache-2.0"

clients/webassembly/client.js

+92-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import * as wasm from ".";
22

3+
/**
4+
* A Nym identity, consisting of a public/private keypair and a Nym
5+
* gateway address.
6+
*/
37
export class Identity {
48
// in the future this should allow for loading from local storage
59
constructor() {
@@ -10,6 +14,10 @@ export class Identity {
1014
}
1115
}
1216

17+
/**
18+
* A Client which connects to a Nym gateway via websocket. All communication
19+
* with the Nym network happens through this connection.
20+
*/
1321
export class Client {
1422
constructor(directoryUrl, identity, authToken) {
1523
this.authToken = authToken
@@ -19,10 +27,17 @@ export class Client {
1927
this.topologyEndpoint = directoryUrl + "/api/presence/topology";
2028
}
2129

30+
/**
31+
* @return {string} a user-pubkey@nym-gateway recipient address
32+
*/
2233
formatAsRecipient() {
2334
return `${this.identity.address}@${this.gateway.mixAddress}`
2435
}
2536

37+
/**
38+
* Get the current network topology, then connect to this client's Nym gateway
39+
* via websocket.
40+
*/
2641
async start() {
2742
await this.updateTopology();
2843
this._getInitialGateway();
@@ -34,6 +49,11 @@ export class Client {
3449
return this.authToken !== null
3550
}
3651

52+
/**
53+
* Update the Nym network topology.
54+
*
55+
* @returns an object containing the current Nym network topology
56+
*/
3757
async updateTopology() {
3858
let response = await http('get', this.topologyEndpoint);
3959
let topology = JSON.parse(response); // make sure it's a valid json
@@ -42,9 +62,11 @@ export class Client {
4262
return topology;
4363
}
4464

45-
/* Gets the address of a Nym gateway to send the Sphinx packet to.
46-
At present we choose the first gateway as the network should only be running
47-
one. Later, we will implement multiple gateways. */
65+
/**
66+
* Gets the address of a Nym gateway to send the Sphinx packet to.
67+
* At present we choose the first gateway as the network should only be
68+
* running one. Later, we will implement multiple gateways.
69+
*/
4870
_getInitialGateway() {
4971
if (this.gateway !== null) {
5072
console.error("tried to re-initialise gateway data");
@@ -60,6 +82,9 @@ export class Client {
6082
}
6183
}
6284

85+
/**
86+
* Connect to the client's defined Nym gateway via websocket.
87+
*/
6388
connect() {
6489
return new Promise((resolve, reject) => {
6590
const conn = new WebSocket(this.gateway.socketAddress);
@@ -84,22 +109,39 @@ export class Client {
84109
})
85110
}
86111

112+
/**
113+
* Sends a registration request to a Nym gateway node. Use it only if you
114+
* haven't registered this client before.
115+
*/
87116
sendRegisterRequest() {
88-
const registerReq = makeRegisterRequest(this.identity.address);
117+
const registerReq = buildRegisterRequest(this.identity.address);
89118
this.gateway.conn.send(registerReq);
90119
this.onRegisterRequestSend();
91120
}
92121

122+
/**
123+
* Authenticates with a Nym gateway for this client.
124+
*
125+
* @param {string} token
126+
*/
93127
sendAuthenticateRequest(token) {
94-
const authenticateReq = makeAuthenticateRequest(this.identity.address, token);
128+
const authenticateReq = buildAuthenticateRequest(this.identity.address, token);
95129
this.conn.send(authenticateReq);
96130
this.onAuthenticateRequestSend();
97131
}
98132

99-
/*
100-
NOTE: this currently does not implement chunking and messages over ~1KB
101-
will cause a panic. This will be fixed in a future version.
102-
*/
133+
/**
134+
* Sends a message up the websocket to this client's Nym gateway.
135+
*
136+
* NOTE: this currently does not implement chunking and messages over ~1KB
137+
* will cause a panic. This will be fixed in a future version.
138+
*
139+
* `message` must be text at the moment. Binary `Blob` and `ArrayBuffer`
140+
* will be supported soon.
141+
*
142+
* @param {*} message
143+
* @param {string} recipient
144+
*/
103145
sendMessage(message, recipient) {
104146
if (this.gateway === null || this.gateway.conn === null) {
105147
console.error("Client was not initialised");
@@ -116,6 +158,15 @@ export class Client {
116158
this.onMessageSend();
117159
}
118160

161+
/**
162+
* A callback triggered when a message is received from this client's Nym
163+
* gateway.
164+
*
165+
* The `event` may be a binary blob which was the payload of a Sphinx packet,
166+
* or it may be a JSON control message (for example, the result of an
167+
* authenticate request).
168+
* @param {*} event
169+
*/
119170
onMessage(event) {
120171
if (event.data instanceof Blob) {
121172
this.onBlobResponse(event);
@@ -133,10 +184,17 @@ export class Client {
133184

134185
// all the callbacks that can be overwritten
135186

187+
/**
188+
* A callback that fires when network topology is updated.
189+
*/
136190
onUpdatedTopology() {
137191
console.log("Default: Updated topology")
138192
}
139193

194+
/**
195+
*
196+
* @param {*} event
197+
*/
140198
onConnect(event) {
141199
console.log("Default: Established gateway connection", event);
142200
}
@@ -200,20 +258,42 @@ export class Client {
200258
reader.readAsText(event.data);
201259
}
202260

203-
// Alternatively you may use default implementation and treat everything as text
261+
/**
262+
* @callback that makes a best-effort attempt to return decrypted Sphinx bytes as text.
263+
*
264+
* Note that no checks are performed to determine whether something is
265+
* really text. If the received data is in fact binary, you'll get
266+
* binary-as-text from this callback.
267+
*/
204268
onText(data) {
205269
console.log("Default: parsed the following data", data);
206270
}
207271
}
208272

209-
function makeRegisterRequest(address) {
273+
/**
274+
* Build a JSON registration request.
275+
*
276+
* @param {string} address
277+
*/
278+
function buildRegisterRequest(address) {
210279
return JSON.stringify({ "type": "register", "address": address });
211280
}
212281

213-
function makeAuthenticateRequest(address, token) {
282+
/**
283+
* Build a JSON authentication request.
284+
*
285+
* @param {string} address
286+
* @param {string} token
287+
*/
288+
function buildAuthenticateRequest(address, token) {
214289
return JSON.stringify({ "type": "authenticate", "address": address, "token": token });
215290
}
216291

292+
/**
293+
* Make an HTTP request.
294+
* @param {string} method
295+
* @param {string} url
296+
*/
217297
function http(method, url) {
218298
return new Promise(function (resolve, reject) {
219299
let xhr = new XMLHttpRequest();

clients/webassembly/js-example/index.js

+23-3
Original file line numberDiff line numberDiff line change
@@ -36,30 +36,50 @@ async function main() {
3636
await nymClient.start();
3737
}
3838

39-
// Create a Sphinx packet and send it to the mixnet through the gateway node.
39+
/**
40+
* Create a Sphinx packet and send it to the mixnet through the gateway node.
41+
*
42+
* Message and recipient are taken from the values in the user interface.
43+
*
44+
* @param {Client} nymClient the nym client to use for message sending
45+
*/
4046
function sendMessageTo(nymClient) {
4147
var message = document.getElementById("message").value;
4248
var recipient = document.getElementById("recipient").value;
4349
nymClient.sendMessage(message, recipient);
4450
displaySend(message);
4551
}
46-
52+
/**
53+
* Display messages that have been sent up the websocket. Colours them blue.
54+
*
55+
* @param {string} message
56+
*/
4757
function displaySend(message) {
4858
let timestamp = new Date().toISOString().substr(11, 12);
4959
let out = "<p style='color: blue; word-break: break-all;'>" + timestamp + " <b>sent</b> >>> " + message + "</p >";
5060
document.getElementById("output").innerHTML = out + document.getElementById("output").innerHTML;
5161
}
5262

63+
/**
64+
* Display received text messages in the browser. Colour them green.
65+
*
66+
* @param {string} message
67+
*/
5368
function displayReceived(message) {
5469
let timestamp = new Date().toISOString().substr(11, 12);
5570
let out = "<p style='color: green; word-break: break-all;'>" + timestamp + " <b>received</b> >>> " + message + "</p >";
5671
document.getElementById("output").innerHTML = out + document.getElementById("output").innerHTML;
5772
}
5873

74+
75+
/**
76+
* Display the nymClient's sender address in the user interface
77+
*
78+
* @param {Client} nymClient
79+
*/
5980
function displaySenderAddress(nymClient) {
6081
document.getElementById("sender").value = nymClient.formatAsRecipient();
6182
}
6283

63-
6484
// Let's get started!
6585
main();

clients/webassembly/js-example/package-lock.json

+3-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

clients/webassembly/js-example/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@
3434
"webpack-dev-server": "^3.11.0"
3535
},
3636
"dependencies": {
37-
"@nymproject/nym-client-wasm": "^0.7.2"
37+
"@nymproject/nym-client-wasm": "^0.7.3"
3838
}
3939
}

0 commit comments

Comments
 (0)