1
1
import * as wasm from "." ;
2
2
3
+ /**
4
+ * A Nym identity, consisting of a public/private keypair and a Nym
5
+ * gateway address.
6
+ */
3
7
export class Identity {
4
8
// in the future this should allow for loading from local storage
5
9
constructor ( ) {
@@ -10,6 +14,10 @@ export class Identity {
10
14
}
11
15
}
12
16
17
+ /**
18
+ * A Client which connects to a Nym gateway via websocket. All communication
19
+ * with the Nym network happens through this connection.
20
+ */
13
21
export class Client {
14
22
constructor ( directoryUrl , identity , authToken ) {
15
23
this . authToken = authToken
@@ -19,10 +27,17 @@ export class Client {
19
27
this . topologyEndpoint = directoryUrl + "/api/presence/topology" ;
20
28
}
21
29
30
+ /**
31
+ * @return {string } a user-pubkey@nym-gateway recipient address
32
+ */
22
33
formatAsRecipient ( ) {
23
34
return `${ this . identity . address } @${ this . gateway . mixAddress } `
24
35
}
25
36
37
+ /**
38
+ * Get the current network topology, then connect to this client's Nym gateway
39
+ * via websocket.
40
+ */
26
41
async start ( ) {
27
42
await this . updateTopology ( ) ;
28
43
this . _getInitialGateway ( ) ;
@@ -34,6 +49,11 @@ export class Client {
34
49
return this . authToken !== null
35
50
}
36
51
52
+ /**
53
+ * Update the Nym network topology.
54
+ *
55
+ * @returns an object containing the current Nym network topology
56
+ */
37
57
async updateTopology ( ) {
38
58
let response = await http ( 'get' , this . topologyEndpoint ) ;
39
59
let topology = JSON . parse ( response ) ; // make sure it's a valid json
@@ -42,9 +62,11 @@ export class Client {
42
62
return topology ;
43
63
}
44
64
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
+ */
48
70
_getInitialGateway ( ) {
49
71
if ( this . gateway !== null ) {
50
72
console . error ( "tried to re-initialise gateway data" ) ;
@@ -60,6 +82,9 @@ export class Client {
60
82
}
61
83
}
62
84
85
+ /**
86
+ * Connect to the client's defined Nym gateway via websocket.
87
+ */
63
88
connect ( ) {
64
89
return new Promise ( ( resolve , reject ) => {
65
90
const conn = new WebSocket ( this . gateway . socketAddress ) ;
@@ -84,22 +109,39 @@ export class Client {
84
109
} )
85
110
}
86
111
112
+ /**
113
+ * Sends a registration request to a Nym gateway node. Use it only if you
114
+ * haven't registered this client before.
115
+ */
87
116
sendRegisterRequest ( ) {
88
- const registerReq = makeRegisterRequest ( this . identity . address ) ;
117
+ const registerReq = buildRegisterRequest ( this . identity . address ) ;
89
118
this . gateway . conn . send ( registerReq ) ;
90
119
this . onRegisterRequestSend ( ) ;
91
120
}
92
121
122
+ /**
123
+ * Authenticates with a Nym gateway for this client.
124
+ *
125
+ * @param {string } token
126
+ */
93
127
sendAuthenticateRequest ( token ) {
94
- const authenticateReq = makeAuthenticateRequest ( this . identity . address , token ) ;
128
+ const authenticateReq = buildAuthenticateRequest ( this . identity . address , token ) ;
95
129
this . conn . send ( authenticateReq ) ;
96
130
this . onAuthenticateRequestSend ( ) ;
97
131
}
98
132
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
+ */
103
145
sendMessage ( message , recipient ) {
104
146
if ( this . gateway === null || this . gateway . conn === null ) {
105
147
console . error ( "Client was not initialised" ) ;
@@ -116,6 +158,15 @@ export class Client {
116
158
this . onMessageSend ( ) ;
117
159
}
118
160
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
+ */
119
170
onMessage ( event ) {
120
171
if ( event . data instanceof Blob ) {
121
172
this . onBlobResponse ( event ) ;
@@ -133,10 +184,17 @@ export class Client {
133
184
134
185
// all the callbacks that can be overwritten
135
186
187
+ /**
188
+ * A callback that fires when network topology is updated.
189
+ */
136
190
onUpdatedTopology ( ) {
137
191
console . log ( "Default: Updated topology" )
138
192
}
139
193
194
+ /**
195
+ *
196
+ * @param {* } event
197
+ */
140
198
onConnect ( event ) {
141
199
console . log ( "Default: Established gateway connection" , event ) ;
142
200
}
@@ -200,20 +258,42 @@ export class Client {
200
258
reader . readAsText ( event . data ) ;
201
259
}
202
260
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
+ */
204
268
onText ( data ) {
205
269
console . log ( "Default: parsed the following data" , data ) ;
206
270
}
207
271
}
208
272
209
- function makeRegisterRequest ( address ) {
273
+ /**
274
+ * Build a JSON registration request.
275
+ *
276
+ * @param {string } address
277
+ */
278
+ function buildRegisterRequest ( address ) {
210
279
return JSON . stringify ( { "type" : "register" , "address" : address } ) ;
211
280
}
212
281
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 ) {
214
289
return JSON . stringify ( { "type" : "authenticate" , "address" : address , "token" : token } ) ;
215
290
}
216
291
292
+ /**
293
+ * Make an HTTP request.
294
+ * @param {string } method
295
+ * @param {string } url
296
+ */
217
297
function http ( method , url ) {
218
298
return new Promise ( function ( resolve , reject ) {
219
299
let xhr = new XMLHttpRequest ( ) ;
0 commit comments