@voidly/agent-sdk 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js ADDED
@@ -0,0 +1,2590 @@
1
+ import { createRequire as __createRequire } from 'module'; const require = __createRequire(import.meta.url);
2
+ "use strict";
3
+ var __create = Object.create;
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getProtoOf = Object.getPrototypeOf;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __commonJS = (cb, mod) => function __require() {
10
+ return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
11
+ };
12
+ var __export = (target, all) => {
13
+ for (var name in all)
14
+ __defProp(target, name, { get: all[name], enumerable: true });
15
+ };
16
+ var __copyProps = (to, from, except, desc) => {
17
+ if (from && typeof from === "object" || typeof from === "function") {
18
+ for (let key of __getOwnPropNames(from))
19
+ if (!__hasOwnProp.call(to, key) && key !== except)
20
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
21
+ }
22
+ return to;
23
+ };
24
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
25
+ // If the importer is in node compatibility mode or this is not an ESM
26
+ // file that has been converted to a CommonJS file using a Babel-
27
+ // compatible transform (i.e. "__esModule" has not been set), then set
28
+ // "default" to the CommonJS "module.exports" for node compatibility.
29
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
30
+ mod
31
+ ));
32
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
33
+
34
+ // node_modules/tweetnacl/nacl-fast.js
35
+ var require_nacl_fast = __commonJS({
36
+ "node_modules/tweetnacl/nacl-fast.js"(exports2, module2) {
37
+ "use strict";
38
+ (function(nacl2) {
39
+ "use strict";
40
+ var gf = function(init) {
41
+ var i, r = new Float64Array(16);
42
+ if (init) for (i = 0; i < init.length; i++) r[i] = init[i];
43
+ return r;
44
+ };
45
+ var randombytes = function() {
46
+ throw new Error("no PRNG");
47
+ };
48
+ var _0 = new Uint8Array(16);
49
+ var _9 = new Uint8Array(32);
50
+ _9[0] = 9;
51
+ var gf0 = gf(), gf1 = gf([1]), _121665 = gf([56129, 1]), D = gf([30883, 4953, 19914, 30187, 55467, 16705, 2637, 112, 59544, 30585, 16505, 36039, 65139, 11119, 27886, 20995]), D2 = gf([61785, 9906, 39828, 60374, 45398, 33411, 5274, 224, 53552, 61171, 33010, 6542, 64743, 22239, 55772, 9222]), X = gf([54554, 36645, 11616, 51542, 42930, 38181, 51040, 26924, 56412, 64982, 57905, 49316, 21502, 52590, 14035, 8553]), Y = gf([26200, 26214, 26214, 26214, 26214, 26214, 26214, 26214, 26214, 26214, 26214, 26214, 26214, 26214, 26214, 26214]), I = gf([41136, 18958, 6951, 50414, 58488, 44335, 6150, 12099, 55207, 15867, 153, 11085, 57099, 20417, 9344, 11139]);
52
+ function ts64(x, i, h, l) {
53
+ x[i] = h >> 24 & 255;
54
+ x[i + 1] = h >> 16 & 255;
55
+ x[i + 2] = h >> 8 & 255;
56
+ x[i + 3] = h & 255;
57
+ x[i + 4] = l >> 24 & 255;
58
+ x[i + 5] = l >> 16 & 255;
59
+ x[i + 6] = l >> 8 & 255;
60
+ x[i + 7] = l & 255;
61
+ }
62
+ function vn(x, xi, y, yi, n) {
63
+ var i, d = 0;
64
+ for (i = 0; i < n; i++) d |= x[xi + i] ^ y[yi + i];
65
+ return (1 & d - 1 >>> 8) - 1;
66
+ }
67
+ function crypto_verify_16(x, xi, y, yi) {
68
+ return vn(x, xi, y, yi, 16);
69
+ }
70
+ function crypto_verify_32(x, xi, y, yi) {
71
+ return vn(x, xi, y, yi, 32);
72
+ }
73
+ function core_salsa20(o, p, k, c) {
74
+ var j0 = c[0] & 255 | (c[1] & 255) << 8 | (c[2] & 255) << 16 | (c[3] & 255) << 24, j1 = k[0] & 255 | (k[1] & 255) << 8 | (k[2] & 255) << 16 | (k[3] & 255) << 24, j2 = k[4] & 255 | (k[5] & 255) << 8 | (k[6] & 255) << 16 | (k[7] & 255) << 24, j3 = k[8] & 255 | (k[9] & 255) << 8 | (k[10] & 255) << 16 | (k[11] & 255) << 24, j4 = k[12] & 255 | (k[13] & 255) << 8 | (k[14] & 255) << 16 | (k[15] & 255) << 24, j5 = c[4] & 255 | (c[5] & 255) << 8 | (c[6] & 255) << 16 | (c[7] & 255) << 24, j6 = p[0] & 255 | (p[1] & 255) << 8 | (p[2] & 255) << 16 | (p[3] & 255) << 24, j7 = p[4] & 255 | (p[5] & 255) << 8 | (p[6] & 255) << 16 | (p[7] & 255) << 24, j8 = p[8] & 255 | (p[9] & 255) << 8 | (p[10] & 255) << 16 | (p[11] & 255) << 24, j9 = p[12] & 255 | (p[13] & 255) << 8 | (p[14] & 255) << 16 | (p[15] & 255) << 24, j10 = c[8] & 255 | (c[9] & 255) << 8 | (c[10] & 255) << 16 | (c[11] & 255) << 24, j11 = k[16] & 255 | (k[17] & 255) << 8 | (k[18] & 255) << 16 | (k[19] & 255) << 24, j12 = k[20] & 255 | (k[21] & 255) << 8 | (k[22] & 255) << 16 | (k[23] & 255) << 24, j13 = k[24] & 255 | (k[25] & 255) << 8 | (k[26] & 255) << 16 | (k[27] & 255) << 24, j14 = k[28] & 255 | (k[29] & 255) << 8 | (k[30] & 255) << 16 | (k[31] & 255) << 24, j15 = c[12] & 255 | (c[13] & 255) << 8 | (c[14] & 255) << 16 | (c[15] & 255) << 24;
75
+ var x0 = j0, x1 = j1, x2 = j2, x3 = j3, x4 = j4, x5 = j5, x6 = j6, x7 = j7, x8 = j8, x9 = j9, x10 = j10, x11 = j11, x12 = j12, x13 = j13, x14 = j14, x15 = j15, u;
76
+ for (var i = 0; i < 20; i += 2) {
77
+ u = x0 + x12 | 0;
78
+ x4 ^= u << 7 | u >>> 32 - 7;
79
+ u = x4 + x0 | 0;
80
+ x8 ^= u << 9 | u >>> 32 - 9;
81
+ u = x8 + x4 | 0;
82
+ x12 ^= u << 13 | u >>> 32 - 13;
83
+ u = x12 + x8 | 0;
84
+ x0 ^= u << 18 | u >>> 32 - 18;
85
+ u = x5 + x1 | 0;
86
+ x9 ^= u << 7 | u >>> 32 - 7;
87
+ u = x9 + x5 | 0;
88
+ x13 ^= u << 9 | u >>> 32 - 9;
89
+ u = x13 + x9 | 0;
90
+ x1 ^= u << 13 | u >>> 32 - 13;
91
+ u = x1 + x13 | 0;
92
+ x5 ^= u << 18 | u >>> 32 - 18;
93
+ u = x10 + x6 | 0;
94
+ x14 ^= u << 7 | u >>> 32 - 7;
95
+ u = x14 + x10 | 0;
96
+ x2 ^= u << 9 | u >>> 32 - 9;
97
+ u = x2 + x14 | 0;
98
+ x6 ^= u << 13 | u >>> 32 - 13;
99
+ u = x6 + x2 | 0;
100
+ x10 ^= u << 18 | u >>> 32 - 18;
101
+ u = x15 + x11 | 0;
102
+ x3 ^= u << 7 | u >>> 32 - 7;
103
+ u = x3 + x15 | 0;
104
+ x7 ^= u << 9 | u >>> 32 - 9;
105
+ u = x7 + x3 | 0;
106
+ x11 ^= u << 13 | u >>> 32 - 13;
107
+ u = x11 + x7 | 0;
108
+ x15 ^= u << 18 | u >>> 32 - 18;
109
+ u = x0 + x3 | 0;
110
+ x1 ^= u << 7 | u >>> 32 - 7;
111
+ u = x1 + x0 | 0;
112
+ x2 ^= u << 9 | u >>> 32 - 9;
113
+ u = x2 + x1 | 0;
114
+ x3 ^= u << 13 | u >>> 32 - 13;
115
+ u = x3 + x2 | 0;
116
+ x0 ^= u << 18 | u >>> 32 - 18;
117
+ u = x5 + x4 | 0;
118
+ x6 ^= u << 7 | u >>> 32 - 7;
119
+ u = x6 + x5 | 0;
120
+ x7 ^= u << 9 | u >>> 32 - 9;
121
+ u = x7 + x6 | 0;
122
+ x4 ^= u << 13 | u >>> 32 - 13;
123
+ u = x4 + x7 | 0;
124
+ x5 ^= u << 18 | u >>> 32 - 18;
125
+ u = x10 + x9 | 0;
126
+ x11 ^= u << 7 | u >>> 32 - 7;
127
+ u = x11 + x10 | 0;
128
+ x8 ^= u << 9 | u >>> 32 - 9;
129
+ u = x8 + x11 | 0;
130
+ x9 ^= u << 13 | u >>> 32 - 13;
131
+ u = x9 + x8 | 0;
132
+ x10 ^= u << 18 | u >>> 32 - 18;
133
+ u = x15 + x14 | 0;
134
+ x12 ^= u << 7 | u >>> 32 - 7;
135
+ u = x12 + x15 | 0;
136
+ x13 ^= u << 9 | u >>> 32 - 9;
137
+ u = x13 + x12 | 0;
138
+ x14 ^= u << 13 | u >>> 32 - 13;
139
+ u = x14 + x13 | 0;
140
+ x15 ^= u << 18 | u >>> 32 - 18;
141
+ }
142
+ x0 = x0 + j0 | 0;
143
+ x1 = x1 + j1 | 0;
144
+ x2 = x2 + j2 | 0;
145
+ x3 = x3 + j3 | 0;
146
+ x4 = x4 + j4 | 0;
147
+ x5 = x5 + j5 | 0;
148
+ x6 = x6 + j6 | 0;
149
+ x7 = x7 + j7 | 0;
150
+ x8 = x8 + j8 | 0;
151
+ x9 = x9 + j9 | 0;
152
+ x10 = x10 + j10 | 0;
153
+ x11 = x11 + j11 | 0;
154
+ x12 = x12 + j12 | 0;
155
+ x13 = x13 + j13 | 0;
156
+ x14 = x14 + j14 | 0;
157
+ x15 = x15 + j15 | 0;
158
+ o[0] = x0 >>> 0 & 255;
159
+ o[1] = x0 >>> 8 & 255;
160
+ o[2] = x0 >>> 16 & 255;
161
+ o[3] = x0 >>> 24 & 255;
162
+ o[4] = x1 >>> 0 & 255;
163
+ o[5] = x1 >>> 8 & 255;
164
+ o[6] = x1 >>> 16 & 255;
165
+ o[7] = x1 >>> 24 & 255;
166
+ o[8] = x2 >>> 0 & 255;
167
+ o[9] = x2 >>> 8 & 255;
168
+ o[10] = x2 >>> 16 & 255;
169
+ o[11] = x2 >>> 24 & 255;
170
+ o[12] = x3 >>> 0 & 255;
171
+ o[13] = x3 >>> 8 & 255;
172
+ o[14] = x3 >>> 16 & 255;
173
+ o[15] = x3 >>> 24 & 255;
174
+ o[16] = x4 >>> 0 & 255;
175
+ o[17] = x4 >>> 8 & 255;
176
+ o[18] = x4 >>> 16 & 255;
177
+ o[19] = x4 >>> 24 & 255;
178
+ o[20] = x5 >>> 0 & 255;
179
+ o[21] = x5 >>> 8 & 255;
180
+ o[22] = x5 >>> 16 & 255;
181
+ o[23] = x5 >>> 24 & 255;
182
+ o[24] = x6 >>> 0 & 255;
183
+ o[25] = x6 >>> 8 & 255;
184
+ o[26] = x6 >>> 16 & 255;
185
+ o[27] = x6 >>> 24 & 255;
186
+ o[28] = x7 >>> 0 & 255;
187
+ o[29] = x7 >>> 8 & 255;
188
+ o[30] = x7 >>> 16 & 255;
189
+ o[31] = x7 >>> 24 & 255;
190
+ o[32] = x8 >>> 0 & 255;
191
+ o[33] = x8 >>> 8 & 255;
192
+ o[34] = x8 >>> 16 & 255;
193
+ o[35] = x8 >>> 24 & 255;
194
+ o[36] = x9 >>> 0 & 255;
195
+ o[37] = x9 >>> 8 & 255;
196
+ o[38] = x9 >>> 16 & 255;
197
+ o[39] = x9 >>> 24 & 255;
198
+ o[40] = x10 >>> 0 & 255;
199
+ o[41] = x10 >>> 8 & 255;
200
+ o[42] = x10 >>> 16 & 255;
201
+ o[43] = x10 >>> 24 & 255;
202
+ o[44] = x11 >>> 0 & 255;
203
+ o[45] = x11 >>> 8 & 255;
204
+ o[46] = x11 >>> 16 & 255;
205
+ o[47] = x11 >>> 24 & 255;
206
+ o[48] = x12 >>> 0 & 255;
207
+ o[49] = x12 >>> 8 & 255;
208
+ o[50] = x12 >>> 16 & 255;
209
+ o[51] = x12 >>> 24 & 255;
210
+ o[52] = x13 >>> 0 & 255;
211
+ o[53] = x13 >>> 8 & 255;
212
+ o[54] = x13 >>> 16 & 255;
213
+ o[55] = x13 >>> 24 & 255;
214
+ o[56] = x14 >>> 0 & 255;
215
+ o[57] = x14 >>> 8 & 255;
216
+ o[58] = x14 >>> 16 & 255;
217
+ o[59] = x14 >>> 24 & 255;
218
+ o[60] = x15 >>> 0 & 255;
219
+ o[61] = x15 >>> 8 & 255;
220
+ o[62] = x15 >>> 16 & 255;
221
+ o[63] = x15 >>> 24 & 255;
222
+ }
223
+ function core_hsalsa20(o, p, k, c) {
224
+ var j0 = c[0] & 255 | (c[1] & 255) << 8 | (c[2] & 255) << 16 | (c[3] & 255) << 24, j1 = k[0] & 255 | (k[1] & 255) << 8 | (k[2] & 255) << 16 | (k[3] & 255) << 24, j2 = k[4] & 255 | (k[5] & 255) << 8 | (k[6] & 255) << 16 | (k[7] & 255) << 24, j3 = k[8] & 255 | (k[9] & 255) << 8 | (k[10] & 255) << 16 | (k[11] & 255) << 24, j4 = k[12] & 255 | (k[13] & 255) << 8 | (k[14] & 255) << 16 | (k[15] & 255) << 24, j5 = c[4] & 255 | (c[5] & 255) << 8 | (c[6] & 255) << 16 | (c[7] & 255) << 24, j6 = p[0] & 255 | (p[1] & 255) << 8 | (p[2] & 255) << 16 | (p[3] & 255) << 24, j7 = p[4] & 255 | (p[5] & 255) << 8 | (p[6] & 255) << 16 | (p[7] & 255) << 24, j8 = p[8] & 255 | (p[9] & 255) << 8 | (p[10] & 255) << 16 | (p[11] & 255) << 24, j9 = p[12] & 255 | (p[13] & 255) << 8 | (p[14] & 255) << 16 | (p[15] & 255) << 24, j10 = c[8] & 255 | (c[9] & 255) << 8 | (c[10] & 255) << 16 | (c[11] & 255) << 24, j11 = k[16] & 255 | (k[17] & 255) << 8 | (k[18] & 255) << 16 | (k[19] & 255) << 24, j12 = k[20] & 255 | (k[21] & 255) << 8 | (k[22] & 255) << 16 | (k[23] & 255) << 24, j13 = k[24] & 255 | (k[25] & 255) << 8 | (k[26] & 255) << 16 | (k[27] & 255) << 24, j14 = k[28] & 255 | (k[29] & 255) << 8 | (k[30] & 255) << 16 | (k[31] & 255) << 24, j15 = c[12] & 255 | (c[13] & 255) << 8 | (c[14] & 255) << 16 | (c[15] & 255) << 24;
225
+ var x0 = j0, x1 = j1, x2 = j2, x3 = j3, x4 = j4, x5 = j5, x6 = j6, x7 = j7, x8 = j8, x9 = j9, x10 = j10, x11 = j11, x12 = j12, x13 = j13, x14 = j14, x15 = j15, u;
226
+ for (var i = 0; i < 20; i += 2) {
227
+ u = x0 + x12 | 0;
228
+ x4 ^= u << 7 | u >>> 32 - 7;
229
+ u = x4 + x0 | 0;
230
+ x8 ^= u << 9 | u >>> 32 - 9;
231
+ u = x8 + x4 | 0;
232
+ x12 ^= u << 13 | u >>> 32 - 13;
233
+ u = x12 + x8 | 0;
234
+ x0 ^= u << 18 | u >>> 32 - 18;
235
+ u = x5 + x1 | 0;
236
+ x9 ^= u << 7 | u >>> 32 - 7;
237
+ u = x9 + x5 | 0;
238
+ x13 ^= u << 9 | u >>> 32 - 9;
239
+ u = x13 + x9 | 0;
240
+ x1 ^= u << 13 | u >>> 32 - 13;
241
+ u = x1 + x13 | 0;
242
+ x5 ^= u << 18 | u >>> 32 - 18;
243
+ u = x10 + x6 | 0;
244
+ x14 ^= u << 7 | u >>> 32 - 7;
245
+ u = x14 + x10 | 0;
246
+ x2 ^= u << 9 | u >>> 32 - 9;
247
+ u = x2 + x14 | 0;
248
+ x6 ^= u << 13 | u >>> 32 - 13;
249
+ u = x6 + x2 | 0;
250
+ x10 ^= u << 18 | u >>> 32 - 18;
251
+ u = x15 + x11 | 0;
252
+ x3 ^= u << 7 | u >>> 32 - 7;
253
+ u = x3 + x15 | 0;
254
+ x7 ^= u << 9 | u >>> 32 - 9;
255
+ u = x7 + x3 | 0;
256
+ x11 ^= u << 13 | u >>> 32 - 13;
257
+ u = x11 + x7 | 0;
258
+ x15 ^= u << 18 | u >>> 32 - 18;
259
+ u = x0 + x3 | 0;
260
+ x1 ^= u << 7 | u >>> 32 - 7;
261
+ u = x1 + x0 | 0;
262
+ x2 ^= u << 9 | u >>> 32 - 9;
263
+ u = x2 + x1 | 0;
264
+ x3 ^= u << 13 | u >>> 32 - 13;
265
+ u = x3 + x2 | 0;
266
+ x0 ^= u << 18 | u >>> 32 - 18;
267
+ u = x5 + x4 | 0;
268
+ x6 ^= u << 7 | u >>> 32 - 7;
269
+ u = x6 + x5 | 0;
270
+ x7 ^= u << 9 | u >>> 32 - 9;
271
+ u = x7 + x6 | 0;
272
+ x4 ^= u << 13 | u >>> 32 - 13;
273
+ u = x4 + x7 | 0;
274
+ x5 ^= u << 18 | u >>> 32 - 18;
275
+ u = x10 + x9 | 0;
276
+ x11 ^= u << 7 | u >>> 32 - 7;
277
+ u = x11 + x10 | 0;
278
+ x8 ^= u << 9 | u >>> 32 - 9;
279
+ u = x8 + x11 | 0;
280
+ x9 ^= u << 13 | u >>> 32 - 13;
281
+ u = x9 + x8 | 0;
282
+ x10 ^= u << 18 | u >>> 32 - 18;
283
+ u = x15 + x14 | 0;
284
+ x12 ^= u << 7 | u >>> 32 - 7;
285
+ u = x12 + x15 | 0;
286
+ x13 ^= u << 9 | u >>> 32 - 9;
287
+ u = x13 + x12 | 0;
288
+ x14 ^= u << 13 | u >>> 32 - 13;
289
+ u = x14 + x13 | 0;
290
+ x15 ^= u << 18 | u >>> 32 - 18;
291
+ }
292
+ o[0] = x0 >>> 0 & 255;
293
+ o[1] = x0 >>> 8 & 255;
294
+ o[2] = x0 >>> 16 & 255;
295
+ o[3] = x0 >>> 24 & 255;
296
+ o[4] = x5 >>> 0 & 255;
297
+ o[5] = x5 >>> 8 & 255;
298
+ o[6] = x5 >>> 16 & 255;
299
+ o[7] = x5 >>> 24 & 255;
300
+ o[8] = x10 >>> 0 & 255;
301
+ o[9] = x10 >>> 8 & 255;
302
+ o[10] = x10 >>> 16 & 255;
303
+ o[11] = x10 >>> 24 & 255;
304
+ o[12] = x15 >>> 0 & 255;
305
+ o[13] = x15 >>> 8 & 255;
306
+ o[14] = x15 >>> 16 & 255;
307
+ o[15] = x15 >>> 24 & 255;
308
+ o[16] = x6 >>> 0 & 255;
309
+ o[17] = x6 >>> 8 & 255;
310
+ o[18] = x6 >>> 16 & 255;
311
+ o[19] = x6 >>> 24 & 255;
312
+ o[20] = x7 >>> 0 & 255;
313
+ o[21] = x7 >>> 8 & 255;
314
+ o[22] = x7 >>> 16 & 255;
315
+ o[23] = x7 >>> 24 & 255;
316
+ o[24] = x8 >>> 0 & 255;
317
+ o[25] = x8 >>> 8 & 255;
318
+ o[26] = x8 >>> 16 & 255;
319
+ o[27] = x8 >>> 24 & 255;
320
+ o[28] = x9 >>> 0 & 255;
321
+ o[29] = x9 >>> 8 & 255;
322
+ o[30] = x9 >>> 16 & 255;
323
+ o[31] = x9 >>> 24 & 255;
324
+ }
325
+ function crypto_core_salsa20(out, inp, k, c) {
326
+ core_salsa20(out, inp, k, c);
327
+ }
328
+ function crypto_core_hsalsa20(out, inp, k, c) {
329
+ core_hsalsa20(out, inp, k, c);
330
+ }
331
+ var sigma = new Uint8Array([101, 120, 112, 97, 110, 100, 32, 51, 50, 45, 98, 121, 116, 101, 32, 107]);
332
+ function crypto_stream_salsa20_xor(c, cpos, m, mpos, b, n, k) {
333
+ var z = new Uint8Array(16), x = new Uint8Array(64);
334
+ var u, i;
335
+ for (i = 0; i < 16; i++) z[i] = 0;
336
+ for (i = 0; i < 8; i++) z[i] = n[i];
337
+ while (b >= 64) {
338
+ crypto_core_salsa20(x, z, k, sigma);
339
+ for (i = 0; i < 64; i++) c[cpos + i] = m[mpos + i] ^ x[i];
340
+ u = 1;
341
+ for (i = 8; i < 16; i++) {
342
+ u = u + (z[i] & 255) | 0;
343
+ z[i] = u & 255;
344
+ u >>>= 8;
345
+ }
346
+ b -= 64;
347
+ cpos += 64;
348
+ mpos += 64;
349
+ }
350
+ if (b > 0) {
351
+ crypto_core_salsa20(x, z, k, sigma);
352
+ for (i = 0; i < b; i++) c[cpos + i] = m[mpos + i] ^ x[i];
353
+ }
354
+ return 0;
355
+ }
356
+ function crypto_stream_salsa20(c, cpos, b, n, k) {
357
+ var z = new Uint8Array(16), x = new Uint8Array(64);
358
+ var u, i;
359
+ for (i = 0; i < 16; i++) z[i] = 0;
360
+ for (i = 0; i < 8; i++) z[i] = n[i];
361
+ while (b >= 64) {
362
+ crypto_core_salsa20(x, z, k, sigma);
363
+ for (i = 0; i < 64; i++) c[cpos + i] = x[i];
364
+ u = 1;
365
+ for (i = 8; i < 16; i++) {
366
+ u = u + (z[i] & 255) | 0;
367
+ z[i] = u & 255;
368
+ u >>>= 8;
369
+ }
370
+ b -= 64;
371
+ cpos += 64;
372
+ }
373
+ if (b > 0) {
374
+ crypto_core_salsa20(x, z, k, sigma);
375
+ for (i = 0; i < b; i++) c[cpos + i] = x[i];
376
+ }
377
+ return 0;
378
+ }
379
+ function crypto_stream(c, cpos, d, n, k) {
380
+ var s = new Uint8Array(32);
381
+ crypto_core_hsalsa20(s, n, k, sigma);
382
+ var sn = new Uint8Array(8);
383
+ for (var i = 0; i < 8; i++) sn[i] = n[i + 16];
384
+ return crypto_stream_salsa20(c, cpos, d, sn, s);
385
+ }
386
+ function crypto_stream_xor(c, cpos, m, mpos, d, n, k) {
387
+ var s = new Uint8Array(32);
388
+ crypto_core_hsalsa20(s, n, k, sigma);
389
+ var sn = new Uint8Array(8);
390
+ for (var i = 0; i < 8; i++) sn[i] = n[i + 16];
391
+ return crypto_stream_salsa20_xor(c, cpos, m, mpos, d, sn, s);
392
+ }
393
+ var poly1305 = function(key) {
394
+ this.buffer = new Uint8Array(16);
395
+ this.r = new Uint16Array(10);
396
+ this.h = new Uint16Array(10);
397
+ this.pad = new Uint16Array(8);
398
+ this.leftover = 0;
399
+ this.fin = 0;
400
+ var t0, t1, t2, t3, t4, t5, t6, t7;
401
+ t0 = key[0] & 255 | (key[1] & 255) << 8;
402
+ this.r[0] = t0 & 8191;
403
+ t1 = key[2] & 255 | (key[3] & 255) << 8;
404
+ this.r[1] = (t0 >>> 13 | t1 << 3) & 8191;
405
+ t2 = key[4] & 255 | (key[5] & 255) << 8;
406
+ this.r[2] = (t1 >>> 10 | t2 << 6) & 7939;
407
+ t3 = key[6] & 255 | (key[7] & 255) << 8;
408
+ this.r[3] = (t2 >>> 7 | t3 << 9) & 8191;
409
+ t4 = key[8] & 255 | (key[9] & 255) << 8;
410
+ this.r[4] = (t3 >>> 4 | t4 << 12) & 255;
411
+ this.r[5] = t4 >>> 1 & 8190;
412
+ t5 = key[10] & 255 | (key[11] & 255) << 8;
413
+ this.r[6] = (t4 >>> 14 | t5 << 2) & 8191;
414
+ t6 = key[12] & 255 | (key[13] & 255) << 8;
415
+ this.r[7] = (t5 >>> 11 | t6 << 5) & 8065;
416
+ t7 = key[14] & 255 | (key[15] & 255) << 8;
417
+ this.r[8] = (t6 >>> 8 | t7 << 8) & 8191;
418
+ this.r[9] = t7 >>> 5 & 127;
419
+ this.pad[0] = key[16] & 255 | (key[17] & 255) << 8;
420
+ this.pad[1] = key[18] & 255 | (key[19] & 255) << 8;
421
+ this.pad[2] = key[20] & 255 | (key[21] & 255) << 8;
422
+ this.pad[3] = key[22] & 255 | (key[23] & 255) << 8;
423
+ this.pad[4] = key[24] & 255 | (key[25] & 255) << 8;
424
+ this.pad[5] = key[26] & 255 | (key[27] & 255) << 8;
425
+ this.pad[6] = key[28] & 255 | (key[29] & 255) << 8;
426
+ this.pad[7] = key[30] & 255 | (key[31] & 255) << 8;
427
+ };
428
+ poly1305.prototype.blocks = function(m, mpos, bytes) {
429
+ var hibit = this.fin ? 0 : 1 << 11;
430
+ var t0, t1, t2, t3, t4, t5, t6, t7, c;
431
+ var d0, d1, d2, d3, d4, d5, d6, d7, d8, d9;
432
+ var h0 = this.h[0], h1 = this.h[1], h2 = this.h[2], h3 = this.h[3], h4 = this.h[4], h5 = this.h[5], h6 = this.h[6], h7 = this.h[7], h8 = this.h[8], h9 = this.h[9];
433
+ var r0 = this.r[0], r1 = this.r[1], r2 = this.r[2], r3 = this.r[3], r4 = this.r[4], r5 = this.r[5], r6 = this.r[6], r7 = this.r[7], r8 = this.r[8], r9 = this.r[9];
434
+ while (bytes >= 16) {
435
+ t0 = m[mpos + 0] & 255 | (m[mpos + 1] & 255) << 8;
436
+ h0 += t0 & 8191;
437
+ t1 = m[mpos + 2] & 255 | (m[mpos + 3] & 255) << 8;
438
+ h1 += (t0 >>> 13 | t1 << 3) & 8191;
439
+ t2 = m[mpos + 4] & 255 | (m[mpos + 5] & 255) << 8;
440
+ h2 += (t1 >>> 10 | t2 << 6) & 8191;
441
+ t3 = m[mpos + 6] & 255 | (m[mpos + 7] & 255) << 8;
442
+ h3 += (t2 >>> 7 | t3 << 9) & 8191;
443
+ t4 = m[mpos + 8] & 255 | (m[mpos + 9] & 255) << 8;
444
+ h4 += (t3 >>> 4 | t4 << 12) & 8191;
445
+ h5 += t4 >>> 1 & 8191;
446
+ t5 = m[mpos + 10] & 255 | (m[mpos + 11] & 255) << 8;
447
+ h6 += (t4 >>> 14 | t5 << 2) & 8191;
448
+ t6 = m[mpos + 12] & 255 | (m[mpos + 13] & 255) << 8;
449
+ h7 += (t5 >>> 11 | t6 << 5) & 8191;
450
+ t7 = m[mpos + 14] & 255 | (m[mpos + 15] & 255) << 8;
451
+ h8 += (t6 >>> 8 | t7 << 8) & 8191;
452
+ h9 += t7 >>> 5 | hibit;
453
+ c = 0;
454
+ d0 = c;
455
+ d0 += h0 * r0;
456
+ d0 += h1 * (5 * r9);
457
+ d0 += h2 * (5 * r8);
458
+ d0 += h3 * (5 * r7);
459
+ d0 += h4 * (5 * r6);
460
+ c = d0 >>> 13;
461
+ d0 &= 8191;
462
+ d0 += h5 * (5 * r5);
463
+ d0 += h6 * (5 * r4);
464
+ d0 += h7 * (5 * r3);
465
+ d0 += h8 * (5 * r2);
466
+ d0 += h9 * (5 * r1);
467
+ c += d0 >>> 13;
468
+ d0 &= 8191;
469
+ d1 = c;
470
+ d1 += h0 * r1;
471
+ d1 += h1 * r0;
472
+ d1 += h2 * (5 * r9);
473
+ d1 += h3 * (5 * r8);
474
+ d1 += h4 * (5 * r7);
475
+ c = d1 >>> 13;
476
+ d1 &= 8191;
477
+ d1 += h5 * (5 * r6);
478
+ d1 += h6 * (5 * r5);
479
+ d1 += h7 * (5 * r4);
480
+ d1 += h8 * (5 * r3);
481
+ d1 += h9 * (5 * r2);
482
+ c += d1 >>> 13;
483
+ d1 &= 8191;
484
+ d2 = c;
485
+ d2 += h0 * r2;
486
+ d2 += h1 * r1;
487
+ d2 += h2 * r0;
488
+ d2 += h3 * (5 * r9);
489
+ d2 += h4 * (5 * r8);
490
+ c = d2 >>> 13;
491
+ d2 &= 8191;
492
+ d2 += h5 * (5 * r7);
493
+ d2 += h6 * (5 * r6);
494
+ d2 += h7 * (5 * r5);
495
+ d2 += h8 * (5 * r4);
496
+ d2 += h9 * (5 * r3);
497
+ c += d2 >>> 13;
498
+ d2 &= 8191;
499
+ d3 = c;
500
+ d3 += h0 * r3;
501
+ d3 += h1 * r2;
502
+ d3 += h2 * r1;
503
+ d3 += h3 * r0;
504
+ d3 += h4 * (5 * r9);
505
+ c = d3 >>> 13;
506
+ d3 &= 8191;
507
+ d3 += h5 * (5 * r8);
508
+ d3 += h6 * (5 * r7);
509
+ d3 += h7 * (5 * r6);
510
+ d3 += h8 * (5 * r5);
511
+ d3 += h9 * (5 * r4);
512
+ c += d3 >>> 13;
513
+ d3 &= 8191;
514
+ d4 = c;
515
+ d4 += h0 * r4;
516
+ d4 += h1 * r3;
517
+ d4 += h2 * r2;
518
+ d4 += h3 * r1;
519
+ d4 += h4 * r0;
520
+ c = d4 >>> 13;
521
+ d4 &= 8191;
522
+ d4 += h5 * (5 * r9);
523
+ d4 += h6 * (5 * r8);
524
+ d4 += h7 * (5 * r7);
525
+ d4 += h8 * (5 * r6);
526
+ d4 += h9 * (5 * r5);
527
+ c += d4 >>> 13;
528
+ d4 &= 8191;
529
+ d5 = c;
530
+ d5 += h0 * r5;
531
+ d5 += h1 * r4;
532
+ d5 += h2 * r3;
533
+ d5 += h3 * r2;
534
+ d5 += h4 * r1;
535
+ c = d5 >>> 13;
536
+ d5 &= 8191;
537
+ d5 += h5 * r0;
538
+ d5 += h6 * (5 * r9);
539
+ d5 += h7 * (5 * r8);
540
+ d5 += h8 * (5 * r7);
541
+ d5 += h9 * (5 * r6);
542
+ c += d5 >>> 13;
543
+ d5 &= 8191;
544
+ d6 = c;
545
+ d6 += h0 * r6;
546
+ d6 += h1 * r5;
547
+ d6 += h2 * r4;
548
+ d6 += h3 * r3;
549
+ d6 += h4 * r2;
550
+ c = d6 >>> 13;
551
+ d6 &= 8191;
552
+ d6 += h5 * r1;
553
+ d6 += h6 * r0;
554
+ d6 += h7 * (5 * r9);
555
+ d6 += h8 * (5 * r8);
556
+ d6 += h9 * (5 * r7);
557
+ c += d6 >>> 13;
558
+ d6 &= 8191;
559
+ d7 = c;
560
+ d7 += h0 * r7;
561
+ d7 += h1 * r6;
562
+ d7 += h2 * r5;
563
+ d7 += h3 * r4;
564
+ d7 += h4 * r3;
565
+ c = d7 >>> 13;
566
+ d7 &= 8191;
567
+ d7 += h5 * r2;
568
+ d7 += h6 * r1;
569
+ d7 += h7 * r0;
570
+ d7 += h8 * (5 * r9);
571
+ d7 += h9 * (5 * r8);
572
+ c += d7 >>> 13;
573
+ d7 &= 8191;
574
+ d8 = c;
575
+ d8 += h0 * r8;
576
+ d8 += h1 * r7;
577
+ d8 += h2 * r6;
578
+ d8 += h3 * r5;
579
+ d8 += h4 * r4;
580
+ c = d8 >>> 13;
581
+ d8 &= 8191;
582
+ d8 += h5 * r3;
583
+ d8 += h6 * r2;
584
+ d8 += h7 * r1;
585
+ d8 += h8 * r0;
586
+ d8 += h9 * (5 * r9);
587
+ c += d8 >>> 13;
588
+ d8 &= 8191;
589
+ d9 = c;
590
+ d9 += h0 * r9;
591
+ d9 += h1 * r8;
592
+ d9 += h2 * r7;
593
+ d9 += h3 * r6;
594
+ d9 += h4 * r5;
595
+ c = d9 >>> 13;
596
+ d9 &= 8191;
597
+ d9 += h5 * r4;
598
+ d9 += h6 * r3;
599
+ d9 += h7 * r2;
600
+ d9 += h8 * r1;
601
+ d9 += h9 * r0;
602
+ c += d9 >>> 13;
603
+ d9 &= 8191;
604
+ c = (c << 2) + c | 0;
605
+ c = c + d0 | 0;
606
+ d0 = c & 8191;
607
+ c = c >>> 13;
608
+ d1 += c;
609
+ h0 = d0;
610
+ h1 = d1;
611
+ h2 = d2;
612
+ h3 = d3;
613
+ h4 = d4;
614
+ h5 = d5;
615
+ h6 = d6;
616
+ h7 = d7;
617
+ h8 = d8;
618
+ h9 = d9;
619
+ mpos += 16;
620
+ bytes -= 16;
621
+ }
622
+ this.h[0] = h0;
623
+ this.h[1] = h1;
624
+ this.h[2] = h2;
625
+ this.h[3] = h3;
626
+ this.h[4] = h4;
627
+ this.h[5] = h5;
628
+ this.h[6] = h6;
629
+ this.h[7] = h7;
630
+ this.h[8] = h8;
631
+ this.h[9] = h9;
632
+ };
633
+ poly1305.prototype.finish = function(mac, macpos) {
634
+ var g = new Uint16Array(10);
635
+ var c, mask, f, i;
636
+ if (this.leftover) {
637
+ i = this.leftover;
638
+ this.buffer[i++] = 1;
639
+ for (; i < 16; i++) this.buffer[i] = 0;
640
+ this.fin = 1;
641
+ this.blocks(this.buffer, 0, 16);
642
+ }
643
+ c = this.h[1] >>> 13;
644
+ this.h[1] &= 8191;
645
+ for (i = 2; i < 10; i++) {
646
+ this.h[i] += c;
647
+ c = this.h[i] >>> 13;
648
+ this.h[i] &= 8191;
649
+ }
650
+ this.h[0] += c * 5;
651
+ c = this.h[0] >>> 13;
652
+ this.h[0] &= 8191;
653
+ this.h[1] += c;
654
+ c = this.h[1] >>> 13;
655
+ this.h[1] &= 8191;
656
+ this.h[2] += c;
657
+ g[0] = this.h[0] + 5;
658
+ c = g[0] >>> 13;
659
+ g[0] &= 8191;
660
+ for (i = 1; i < 10; i++) {
661
+ g[i] = this.h[i] + c;
662
+ c = g[i] >>> 13;
663
+ g[i] &= 8191;
664
+ }
665
+ g[9] -= 1 << 13;
666
+ mask = (c ^ 1) - 1;
667
+ for (i = 0; i < 10; i++) g[i] &= mask;
668
+ mask = ~mask;
669
+ for (i = 0; i < 10; i++) this.h[i] = this.h[i] & mask | g[i];
670
+ this.h[0] = (this.h[0] | this.h[1] << 13) & 65535;
671
+ this.h[1] = (this.h[1] >>> 3 | this.h[2] << 10) & 65535;
672
+ this.h[2] = (this.h[2] >>> 6 | this.h[3] << 7) & 65535;
673
+ this.h[3] = (this.h[3] >>> 9 | this.h[4] << 4) & 65535;
674
+ this.h[4] = (this.h[4] >>> 12 | this.h[5] << 1 | this.h[6] << 14) & 65535;
675
+ this.h[5] = (this.h[6] >>> 2 | this.h[7] << 11) & 65535;
676
+ this.h[6] = (this.h[7] >>> 5 | this.h[8] << 8) & 65535;
677
+ this.h[7] = (this.h[8] >>> 8 | this.h[9] << 5) & 65535;
678
+ f = this.h[0] + this.pad[0];
679
+ this.h[0] = f & 65535;
680
+ for (i = 1; i < 8; i++) {
681
+ f = (this.h[i] + this.pad[i] | 0) + (f >>> 16) | 0;
682
+ this.h[i] = f & 65535;
683
+ }
684
+ mac[macpos + 0] = this.h[0] >>> 0 & 255;
685
+ mac[macpos + 1] = this.h[0] >>> 8 & 255;
686
+ mac[macpos + 2] = this.h[1] >>> 0 & 255;
687
+ mac[macpos + 3] = this.h[1] >>> 8 & 255;
688
+ mac[macpos + 4] = this.h[2] >>> 0 & 255;
689
+ mac[macpos + 5] = this.h[2] >>> 8 & 255;
690
+ mac[macpos + 6] = this.h[3] >>> 0 & 255;
691
+ mac[macpos + 7] = this.h[3] >>> 8 & 255;
692
+ mac[macpos + 8] = this.h[4] >>> 0 & 255;
693
+ mac[macpos + 9] = this.h[4] >>> 8 & 255;
694
+ mac[macpos + 10] = this.h[5] >>> 0 & 255;
695
+ mac[macpos + 11] = this.h[5] >>> 8 & 255;
696
+ mac[macpos + 12] = this.h[6] >>> 0 & 255;
697
+ mac[macpos + 13] = this.h[6] >>> 8 & 255;
698
+ mac[macpos + 14] = this.h[7] >>> 0 & 255;
699
+ mac[macpos + 15] = this.h[7] >>> 8 & 255;
700
+ };
701
+ poly1305.prototype.update = function(m, mpos, bytes) {
702
+ var i, want;
703
+ if (this.leftover) {
704
+ want = 16 - this.leftover;
705
+ if (want > bytes)
706
+ want = bytes;
707
+ for (i = 0; i < want; i++)
708
+ this.buffer[this.leftover + i] = m[mpos + i];
709
+ bytes -= want;
710
+ mpos += want;
711
+ this.leftover += want;
712
+ if (this.leftover < 16)
713
+ return;
714
+ this.blocks(this.buffer, 0, 16);
715
+ this.leftover = 0;
716
+ }
717
+ if (bytes >= 16) {
718
+ want = bytes - bytes % 16;
719
+ this.blocks(m, mpos, want);
720
+ mpos += want;
721
+ bytes -= want;
722
+ }
723
+ if (bytes) {
724
+ for (i = 0; i < bytes; i++)
725
+ this.buffer[this.leftover + i] = m[mpos + i];
726
+ this.leftover += bytes;
727
+ }
728
+ };
729
+ function crypto_onetimeauth(out, outpos, m, mpos, n, k) {
730
+ var s = new poly1305(k);
731
+ s.update(m, mpos, n);
732
+ s.finish(out, outpos);
733
+ return 0;
734
+ }
735
+ function crypto_onetimeauth_verify(h, hpos, m, mpos, n, k) {
736
+ var x = new Uint8Array(16);
737
+ crypto_onetimeauth(x, 0, m, mpos, n, k);
738
+ return crypto_verify_16(h, hpos, x, 0);
739
+ }
740
+ function crypto_secretbox(c, m, d, n, k) {
741
+ var i;
742
+ if (d < 32) return -1;
743
+ crypto_stream_xor(c, 0, m, 0, d, n, k);
744
+ crypto_onetimeauth(c, 16, c, 32, d - 32, c);
745
+ for (i = 0; i < 16; i++) c[i] = 0;
746
+ return 0;
747
+ }
748
+ function crypto_secretbox_open(m, c, d, n, k) {
749
+ var i;
750
+ var x = new Uint8Array(32);
751
+ if (d < 32) return -1;
752
+ crypto_stream(x, 0, 32, n, k);
753
+ if (crypto_onetimeauth_verify(c, 16, c, 32, d - 32, x) !== 0) return -1;
754
+ crypto_stream_xor(m, 0, c, 0, d, n, k);
755
+ for (i = 0; i < 32; i++) m[i] = 0;
756
+ return 0;
757
+ }
758
+ function set25519(r, a) {
759
+ var i;
760
+ for (i = 0; i < 16; i++) r[i] = a[i] | 0;
761
+ }
762
+ function car25519(o) {
763
+ var i, v, c = 1;
764
+ for (i = 0; i < 16; i++) {
765
+ v = o[i] + c + 65535;
766
+ c = Math.floor(v / 65536);
767
+ o[i] = v - c * 65536;
768
+ }
769
+ o[0] += c - 1 + 37 * (c - 1);
770
+ }
771
+ function sel25519(p, q, b) {
772
+ var t, c = ~(b - 1);
773
+ for (var i = 0; i < 16; i++) {
774
+ t = c & (p[i] ^ q[i]);
775
+ p[i] ^= t;
776
+ q[i] ^= t;
777
+ }
778
+ }
779
+ function pack25519(o, n) {
780
+ var i, j, b;
781
+ var m = gf(), t = gf();
782
+ for (i = 0; i < 16; i++) t[i] = n[i];
783
+ car25519(t);
784
+ car25519(t);
785
+ car25519(t);
786
+ for (j = 0; j < 2; j++) {
787
+ m[0] = t[0] - 65517;
788
+ for (i = 1; i < 15; i++) {
789
+ m[i] = t[i] - 65535 - (m[i - 1] >> 16 & 1);
790
+ m[i - 1] &= 65535;
791
+ }
792
+ m[15] = t[15] - 32767 - (m[14] >> 16 & 1);
793
+ b = m[15] >> 16 & 1;
794
+ m[14] &= 65535;
795
+ sel25519(t, m, 1 - b);
796
+ }
797
+ for (i = 0; i < 16; i++) {
798
+ o[2 * i] = t[i] & 255;
799
+ o[2 * i + 1] = t[i] >> 8;
800
+ }
801
+ }
802
+ function neq25519(a, b) {
803
+ var c = new Uint8Array(32), d = new Uint8Array(32);
804
+ pack25519(c, a);
805
+ pack25519(d, b);
806
+ return crypto_verify_32(c, 0, d, 0);
807
+ }
808
+ function par25519(a) {
809
+ var d = new Uint8Array(32);
810
+ pack25519(d, a);
811
+ return d[0] & 1;
812
+ }
813
+ function unpack25519(o, n) {
814
+ var i;
815
+ for (i = 0; i < 16; i++) o[i] = n[2 * i] + (n[2 * i + 1] << 8);
816
+ o[15] &= 32767;
817
+ }
818
+ function A(o, a, b) {
819
+ for (var i = 0; i < 16; i++) o[i] = a[i] + b[i];
820
+ }
821
+ function Z(o, a, b) {
822
+ for (var i = 0; i < 16; i++) o[i] = a[i] - b[i];
823
+ }
824
+ function M(o, a, b) {
825
+ var v, c, t0 = 0, t1 = 0, t2 = 0, t3 = 0, t4 = 0, t5 = 0, t6 = 0, t7 = 0, t8 = 0, t9 = 0, t10 = 0, t11 = 0, t12 = 0, t13 = 0, t14 = 0, t15 = 0, t16 = 0, t17 = 0, t18 = 0, t19 = 0, t20 = 0, t21 = 0, t22 = 0, t23 = 0, t24 = 0, t25 = 0, t26 = 0, t27 = 0, t28 = 0, t29 = 0, t30 = 0, b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3], b4 = b[4], b5 = b[5], b6 = b[6], b7 = b[7], b8 = b[8], b9 = b[9], b10 = b[10], b11 = b[11], b12 = b[12], b13 = b[13], b14 = b[14], b15 = b[15];
826
+ v = a[0];
827
+ t0 += v * b0;
828
+ t1 += v * b1;
829
+ t2 += v * b2;
830
+ t3 += v * b3;
831
+ t4 += v * b4;
832
+ t5 += v * b5;
833
+ t6 += v * b6;
834
+ t7 += v * b7;
835
+ t8 += v * b8;
836
+ t9 += v * b9;
837
+ t10 += v * b10;
838
+ t11 += v * b11;
839
+ t12 += v * b12;
840
+ t13 += v * b13;
841
+ t14 += v * b14;
842
+ t15 += v * b15;
843
+ v = a[1];
844
+ t1 += v * b0;
845
+ t2 += v * b1;
846
+ t3 += v * b2;
847
+ t4 += v * b3;
848
+ t5 += v * b4;
849
+ t6 += v * b5;
850
+ t7 += v * b6;
851
+ t8 += v * b7;
852
+ t9 += v * b8;
853
+ t10 += v * b9;
854
+ t11 += v * b10;
855
+ t12 += v * b11;
856
+ t13 += v * b12;
857
+ t14 += v * b13;
858
+ t15 += v * b14;
859
+ t16 += v * b15;
860
+ v = a[2];
861
+ t2 += v * b0;
862
+ t3 += v * b1;
863
+ t4 += v * b2;
864
+ t5 += v * b3;
865
+ t6 += v * b4;
866
+ t7 += v * b5;
867
+ t8 += v * b6;
868
+ t9 += v * b7;
869
+ t10 += v * b8;
870
+ t11 += v * b9;
871
+ t12 += v * b10;
872
+ t13 += v * b11;
873
+ t14 += v * b12;
874
+ t15 += v * b13;
875
+ t16 += v * b14;
876
+ t17 += v * b15;
877
+ v = a[3];
878
+ t3 += v * b0;
879
+ t4 += v * b1;
880
+ t5 += v * b2;
881
+ t6 += v * b3;
882
+ t7 += v * b4;
883
+ t8 += v * b5;
884
+ t9 += v * b6;
885
+ t10 += v * b7;
886
+ t11 += v * b8;
887
+ t12 += v * b9;
888
+ t13 += v * b10;
889
+ t14 += v * b11;
890
+ t15 += v * b12;
891
+ t16 += v * b13;
892
+ t17 += v * b14;
893
+ t18 += v * b15;
894
+ v = a[4];
895
+ t4 += v * b0;
896
+ t5 += v * b1;
897
+ t6 += v * b2;
898
+ t7 += v * b3;
899
+ t8 += v * b4;
900
+ t9 += v * b5;
901
+ t10 += v * b6;
902
+ t11 += v * b7;
903
+ t12 += v * b8;
904
+ t13 += v * b9;
905
+ t14 += v * b10;
906
+ t15 += v * b11;
907
+ t16 += v * b12;
908
+ t17 += v * b13;
909
+ t18 += v * b14;
910
+ t19 += v * b15;
911
+ v = a[5];
912
+ t5 += v * b0;
913
+ t6 += v * b1;
914
+ t7 += v * b2;
915
+ t8 += v * b3;
916
+ t9 += v * b4;
917
+ t10 += v * b5;
918
+ t11 += v * b6;
919
+ t12 += v * b7;
920
+ t13 += v * b8;
921
+ t14 += v * b9;
922
+ t15 += v * b10;
923
+ t16 += v * b11;
924
+ t17 += v * b12;
925
+ t18 += v * b13;
926
+ t19 += v * b14;
927
+ t20 += v * b15;
928
+ v = a[6];
929
+ t6 += v * b0;
930
+ t7 += v * b1;
931
+ t8 += v * b2;
932
+ t9 += v * b3;
933
+ t10 += v * b4;
934
+ t11 += v * b5;
935
+ t12 += v * b6;
936
+ t13 += v * b7;
937
+ t14 += v * b8;
938
+ t15 += v * b9;
939
+ t16 += v * b10;
940
+ t17 += v * b11;
941
+ t18 += v * b12;
942
+ t19 += v * b13;
943
+ t20 += v * b14;
944
+ t21 += v * b15;
945
+ v = a[7];
946
+ t7 += v * b0;
947
+ t8 += v * b1;
948
+ t9 += v * b2;
949
+ t10 += v * b3;
950
+ t11 += v * b4;
951
+ t12 += v * b5;
952
+ t13 += v * b6;
953
+ t14 += v * b7;
954
+ t15 += v * b8;
955
+ t16 += v * b9;
956
+ t17 += v * b10;
957
+ t18 += v * b11;
958
+ t19 += v * b12;
959
+ t20 += v * b13;
960
+ t21 += v * b14;
961
+ t22 += v * b15;
962
+ v = a[8];
963
+ t8 += v * b0;
964
+ t9 += v * b1;
965
+ t10 += v * b2;
966
+ t11 += v * b3;
967
+ t12 += v * b4;
968
+ t13 += v * b5;
969
+ t14 += v * b6;
970
+ t15 += v * b7;
971
+ t16 += v * b8;
972
+ t17 += v * b9;
973
+ t18 += v * b10;
974
+ t19 += v * b11;
975
+ t20 += v * b12;
976
+ t21 += v * b13;
977
+ t22 += v * b14;
978
+ t23 += v * b15;
979
+ v = a[9];
980
+ t9 += v * b0;
981
+ t10 += v * b1;
982
+ t11 += v * b2;
983
+ t12 += v * b3;
984
+ t13 += v * b4;
985
+ t14 += v * b5;
986
+ t15 += v * b6;
987
+ t16 += v * b7;
988
+ t17 += v * b8;
989
+ t18 += v * b9;
990
+ t19 += v * b10;
991
+ t20 += v * b11;
992
+ t21 += v * b12;
993
+ t22 += v * b13;
994
+ t23 += v * b14;
995
+ t24 += v * b15;
996
+ v = a[10];
997
+ t10 += v * b0;
998
+ t11 += v * b1;
999
+ t12 += v * b2;
1000
+ t13 += v * b3;
1001
+ t14 += v * b4;
1002
+ t15 += v * b5;
1003
+ t16 += v * b6;
1004
+ t17 += v * b7;
1005
+ t18 += v * b8;
1006
+ t19 += v * b9;
1007
+ t20 += v * b10;
1008
+ t21 += v * b11;
1009
+ t22 += v * b12;
1010
+ t23 += v * b13;
1011
+ t24 += v * b14;
1012
+ t25 += v * b15;
1013
+ v = a[11];
1014
+ t11 += v * b0;
1015
+ t12 += v * b1;
1016
+ t13 += v * b2;
1017
+ t14 += v * b3;
1018
+ t15 += v * b4;
1019
+ t16 += v * b5;
1020
+ t17 += v * b6;
1021
+ t18 += v * b7;
1022
+ t19 += v * b8;
1023
+ t20 += v * b9;
1024
+ t21 += v * b10;
1025
+ t22 += v * b11;
1026
+ t23 += v * b12;
1027
+ t24 += v * b13;
1028
+ t25 += v * b14;
1029
+ t26 += v * b15;
1030
+ v = a[12];
1031
+ t12 += v * b0;
1032
+ t13 += v * b1;
1033
+ t14 += v * b2;
1034
+ t15 += v * b3;
1035
+ t16 += v * b4;
1036
+ t17 += v * b5;
1037
+ t18 += v * b6;
1038
+ t19 += v * b7;
1039
+ t20 += v * b8;
1040
+ t21 += v * b9;
1041
+ t22 += v * b10;
1042
+ t23 += v * b11;
1043
+ t24 += v * b12;
1044
+ t25 += v * b13;
1045
+ t26 += v * b14;
1046
+ t27 += v * b15;
1047
+ v = a[13];
1048
+ t13 += v * b0;
1049
+ t14 += v * b1;
1050
+ t15 += v * b2;
1051
+ t16 += v * b3;
1052
+ t17 += v * b4;
1053
+ t18 += v * b5;
1054
+ t19 += v * b6;
1055
+ t20 += v * b7;
1056
+ t21 += v * b8;
1057
+ t22 += v * b9;
1058
+ t23 += v * b10;
1059
+ t24 += v * b11;
1060
+ t25 += v * b12;
1061
+ t26 += v * b13;
1062
+ t27 += v * b14;
1063
+ t28 += v * b15;
1064
+ v = a[14];
1065
+ t14 += v * b0;
1066
+ t15 += v * b1;
1067
+ t16 += v * b2;
1068
+ t17 += v * b3;
1069
+ t18 += v * b4;
1070
+ t19 += v * b5;
1071
+ t20 += v * b6;
1072
+ t21 += v * b7;
1073
+ t22 += v * b8;
1074
+ t23 += v * b9;
1075
+ t24 += v * b10;
1076
+ t25 += v * b11;
1077
+ t26 += v * b12;
1078
+ t27 += v * b13;
1079
+ t28 += v * b14;
1080
+ t29 += v * b15;
1081
+ v = a[15];
1082
+ t15 += v * b0;
1083
+ t16 += v * b1;
1084
+ t17 += v * b2;
1085
+ t18 += v * b3;
1086
+ t19 += v * b4;
1087
+ t20 += v * b5;
1088
+ t21 += v * b6;
1089
+ t22 += v * b7;
1090
+ t23 += v * b8;
1091
+ t24 += v * b9;
1092
+ t25 += v * b10;
1093
+ t26 += v * b11;
1094
+ t27 += v * b12;
1095
+ t28 += v * b13;
1096
+ t29 += v * b14;
1097
+ t30 += v * b15;
1098
+ t0 += 38 * t16;
1099
+ t1 += 38 * t17;
1100
+ t2 += 38 * t18;
1101
+ t3 += 38 * t19;
1102
+ t4 += 38 * t20;
1103
+ t5 += 38 * t21;
1104
+ t6 += 38 * t22;
1105
+ t7 += 38 * t23;
1106
+ t8 += 38 * t24;
1107
+ t9 += 38 * t25;
1108
+ t10 += 38 * t26;
1109
+ t11 += 38 * t27;
1110
+ t12 += 38 * t28;
1111
+ t13 += 38 * t29;
1112
+ t14 += 38 * t30;
1113
+ c = 1;
1114
+ v = t0 + c + 65535;
1115
+ c = Math.floor(v / 65536);
1116
+ t0 = v - c * 65536;
1117
+ v = t1 + c + 65535;
1118
+ c = Math.floor(v / 65536);
1119
+ t1 = v - c * 65536;
1120
+ v = t2 + c + 65535;
1121
+ c = Math.floor(v / 65536);
1122
+ t2 = v - c * 65536;
1123
+ v = t3 + c + 65535;
1124
+ c = Math.floor(v / 65536);
1125
+ t3 = v - c * 65536;
1126
+ v = t4 + c + 65535;
1127
+ c = Math.floor(v / 65536);
1128
+ t4 = v - c * 65536;
1129
+ v = t5 + c + 65535;
1130
+ c = Math.floor(v / 65536);
1131
+ t5 = v - c * 65536;
1132
+ v = t6 + c + 65535;
1133
+ c = Math.floor(v / 65536);
1134
+ t6 = v - c * 65536;
1135
+ v = t7 + c + 65535;
1136
+ c = Math.floor(v / 65536);
1137
+ t7 = v - c * 65536;
1138
+ v = t8 + c + 65535;
1139
+ c = Math.floor(v / 65536);
1140
+ t8 = v - c * 65536;
1141
+ v = t9 + c + 65535;
1142
+ c = Math.floor(v / 65536);
1143
+ t9 = v - c * 65536;
1144
+ v = t10 + c + 65535;
1145
+ c = Math.floor(v / 65536);
1146
+ t10 = v - c * 65536;
1147
+ v = t11 + c + 65535;
1148
+ c = Math.floor(v / 65536);
1149
+ t11 = v - c * 65536;
1150
+ v = t12 + c + 65535;
1151
+ c = Math.floor(v / 65536);
1152
+ t12 = v - c * 65536;
1153
+ v = t13 + c + 65535;
1154
+ c = Math.floor(v / 65536);
1155
+ t13 = v - c * 65536;
1156
+ v = t14 + c + 65535;
1157
+ c = Math.floor(v / 65536);
1158
+ t14 = v - c * 65536;
1159
+ v = t15 + c + 65535;
1160
+ c = Math.floor(v / 65536);
1161
+ t15 = v - c * 65536;
1162
+ t0 += c - 1 + 37 * (c - 1);
1163
+ c = 1;
1164
+ v = t0 + c + 65535;
1165
+ c = Math.floor(v / 65536);
1166
+ t0 = v - c * 65536;
1167
+ v = t1 + c + 65535;
1168
+ c = Math.floor(v / 65536);
1169
+ t1 = v - c * 65536;
1170
+ v = t2 + c + 65535;
1171
+ c = Math.floor(v / 65536);
1172
+ t2 = v - c * 65536;
1173
+ v = t3 + c + 65535;
1174
+ c = Math.floor(v / 65536);
1175
+ t3 = v - c * 65536;
1176
+ v = t4 + c + 65535;
1177
+ c = Math.floor(v / 65536);
1178
+ t4 = v - c * 65536;
1179
+ v = t5 + c + 65535;
1180
+ c = Math.floor(v / 65536);
1181
+ t5 = v - c * 65536;
1182
+ v = t6 + c + 65535;
1183
+ c = Math.floor(v / 65536);
1184
+ t6 = v - c * 65536;
1185
+ v = t7 + c + 65535;
1186
+ c = Math.floor(v / 65536);
1187
+ t7 = v - c * 65536;
1188
+ v = t8 + c + 65535;
1189
+ c = Math.floor(v / 65536);
1190
+ t8 = v - c * 65536;
1191
+ v = t9 + c + 65535;
1192
+ c = Math.floor(v / 65536);
1193
+ t9 = v - c * 65536;
1194
+ v = t10 + c + 65535;
1195
+ c = Math.floor(v / 65536);
1196
+ t10 = v - c * 65536;
1197
+ v = t11 + c + 65535;
1198
+ c = Math.floor(v / 65536);
1199
+ t11 = v - c * 65536;
1200
+ v = t12 + c + 65535;
1201
+ c = Math.floor(v / 65536);
1202
+ t12 = v - c * 65536;
1203
+ v = t13 + c + 65535;
1204
+ c = Math.floor(v / 65536);
1205
+ t13 = v - c * 65536;
1206
+ v = t14 + c + 65535;
1207
+ c = Math.floor(v / 65536);
1208
+ t14 = v - c * 65536;
1209
+ v = t15 + c + 65535;
1210
+ c = Math.floor(v / 65536);
1211
+ t15 = v - c * 65536;
1212
+ t0 += c - 1 + 37 * (c - 1);
1213
+ o[0] = t0;
1214
+ o[1] = t1;
1215
+ o[2] = t2;
1216
+ o[3] = t3;
1217
+ o[4] = t4;
1218
+ o[5] = t5;
1219
+ o[6] = t6;
1220
+ o[7] = t7;
1221
+ o[8] = t8;
1222
+ o[9] = t9;
1223
+ o[10] = t10;
1224
+ o[11] = t11;
1225
+ o[12] = t12;
1226
+ o[13] = t13;
1227
+ o[14] = t14;
1228
+ o[15] = t15;
1229
+ }
1230
+ function S(o, a) {
1231
+ M(o, a, a);
1232
+ }
1233
+ function inv25519(o, i) {
1234
+ var c = gf();
1235
+ var a;
1236
+ for (a = 0; a < 16; a++) c[a] = i[a];
1237
+ for (a = 253; a >= 0; a--) {
1238
+ S(c, c);
1239
+ if (a !== 2 && a !== 4) M(c, c, i);
1240
+ }
1241
+ for (a = 0; a < 16; a++) o[a] = c[a];
1242
+ }
1243
+ function pow2523(o, i) {
1244
+ var c = gf();
1245
+ var a;
1246
+ for (a = 0; a < 16; a++) c[a] = i[a];
1247
+ for (a = 250; a >= 0; a--) {
1248
+ S(c, c);
1249
+ if (a !== 1) M(c, c, i);
1250
+ }
1251
+ for (a = 0; a < 16; a++) o[a] = c[a];
1252
+ }
1253
+ function crypto_scalarmult(q, n, p) {
1254
+ var z = new Uint8Array(32);
1255
+ var x = new Float64Array(80), r, i;
1256
+ var a = gf(), b = gf(), c = gf(), d = gf(), e = gf(), f = gf();
1257
+ for (i = 0; i < 31; i++) z[i] = n[i];
1258
+ z[31] = n[31] & 127 | 64;
1259
+ z[0] &= 248;
1260
+ unpack25519(x, p);
1261
+ for (i = 0; i < 16; i++) {
1262
+ b[i] = x[i];
1263
+ d[i] = a[i] = c[i] = 0;
1264
+ }
1265
+ a[0] = d[0] = 1;
1266
+ for (i = 254; i >= 0; --i) {
1267
+ r = z[i >>> 3] >>> (i & 7) & 1;
1268
+ sel25519(a, b, r);
1269
+ sel25519(c, d, r);
1270
+ A(e, a, c);
1271
+ Z(a, a, c);
1272
+ A(c, b, d);
1273
+ Z(b, b, d);
1274
+ S(d, e);
1275
+ S(f, a);
1276
+ M(a, c, a);
1277
+ M(c, b, e);
1278
+ A(e, a, c);
1279
+ Z(a, a, c);
1280
+ S(b, a);
1281
+ Z(c, d, f);
1282
+ M(a, c, _121665);
1283
+ A(a, a, d);
1284
+ M(c, c, a);
1285
+ M(a, d, f);
1286
+ M(d, b, x);
1287
+ S(b, e);
1288
+ sel25519(a, b, r);
1289
+ sel25519(c, d, r);
1290
+ }
1291
+ for (i = 0; i < 16; i++) {
1292
+ x[i + 16] = a[i];
1293
+ x[i + 32] = c[i];
1294
+ x[i + 48] = b[i];
1295
+ x[i + 64] = d[i];
1296
+ }
1297
+ var x32 = x.subarray(32);
1298
+ var x16 = x.subarray(16);
1299
+ inv25519(x32, x32);
1300
+ M(x16, x16, x32);
1301
+ pack25519(q, x16);
1302
+ return 0;
1303
+ }
1304
+ function crypto_scalarmult_base(q, n) {
1305
+ return crypto_scalarmult(q, n, _9);
1306
+ }
1307
+ function crypto_box_keypair(y, x) {
1308
+ randombytes(x, 32);
1309
+ return crypto_scalarmult_base(y, x);
1310
+ }
1311
+ function crypto_box_beforenm(k, y, x) {
1312
+ var s = new Uint8Array(32);
1313
+ crypto_scalarmult(s, x, y);
1314
+ return crypto_core_hsalsa20(k, _0, s, sigma);
1315
+ }
1316
+ var crypto_box_afternm = crypto_secretbox;
1317
+ var crypto_box_open_afternm = crypto_secretbox_open;
1318
+ function crypto_box(c, m, d, n, y, x) {
1319
+ var k = new Uint8Array(32);
1320
+ crypto_box_beforenm(k, y, x);
1321
+ return crypto_box_afternm(c, m, d, n, k);
1322
+ }
1323
+ function crypto_box_open(m, c, d, n, y, x) {
1324
+ var k = new Uint8Array(32);
1325
+ crypto_box_beforenm(k, y, x);
1326
+ return crypto_box_open_afternm(m, c, d, n, k);
1327
+ }
1328
+ var K = [
1329
+ 1116352408,
1330
+ 3609767458,
1331
+ 1899447441,
1332
+ 602891725,
1333
+ 3049323471,
1334
+ 3964484399,
1335
+ 3921009573,
1336
+ 2173295548,
1337
+ 961987163,
1338
+ 4081628472,
1339
+ 1508970993,
1340
+ 3053834265,
1341
+ 2453635748,
1342
+ 2937671579,
1343
+ 2870763221,
1344
+ 3664609560,
1345
+ 3624381080,
1346
+ 2734883394,
1347
+ 310598401,
1348
+ 1164996542,
1349
+ 607225278,
1350
+ 1323610764,
1351
+ 1426881987,
1352
+ 3590304994,
1353
+ 1925078388,
1354
+ 4068182383,
1355
+ 2162078206,
1356
+ 991336113,
1357
+ 2614888103,
1358
+ 633803317,
1359
+ 3248222580,
1360
+ 3479774868,
1361
+ 3835390401,
1362
+ 2666613458,
1363
+ 4022224774,
1364
+ 944711139,
1365
+ 264347078,
1366
+ 2341262773,
1367
+ 604807628,
1368
+ 2007800933,
1369
+ 770255983,
1370
+ 1495990901,
1371
+ 1249150122,
1372
+ 1856431235,
1373
+ 1555081692,
1374
+ 3175218132,
1375
+ 1996064986,
1376
+ 2198950837,
1377
+ 2554220882,
1378
+ 3999719339,
1379
+ 2821834349,
1380
+ 766784016,
1381
+ 2952996808,
1382
+ 2566594879,
1383
+ 3210313671,
1384
+ 3203337956,
1385
+ 3336571891,
1386
+ 1034457026,
1387
+ 3584528711,
1388
+ 2466948901,
1389
+ 113926993,
1390
+ 3758326383,
1391
+ 338241895,
1392
+ 168717936,
1393
+ 666307205,
1394
+ 1188179964,
1395
+ 773529912,
1396
+ 1546045734,
1397
+ 1294757372,
1398
+ 1522805485,
1399
+ 1396182291,
1400
+ 2643833823,
1401
+ 1695183700,
1402
+ 2343527390,
1403
+ 1986661051,
1404
+ 1014477480,
1405
+ 2177026350,
1406
+ 1206759142,
1407
+ 2456956037,
1408
+ 344077627,
1409
+ 2730485921,
1410
+ 1290863460,
1411
+ 2820302411,
1412
+ 3158454273,
1413
+ 3259730800,
1414
+ 3505952657,
1415
+ 3345764771,
1416
+ 106217008,
1417
+ 3516065817,
1418
+ 3606008344,
1419
+ 3600352804,
1420
+ 1432725776,
1421
+ 4094571909,
1422
+ 1467031594,
1423
+ 275423344,
1424
+ 851169720,
1425
+ 430227734,
1426
+ 3100823752,
1427
+ 506948616,
1428
+ 1363258195,
1429
+ 659060556,
1430
+ 3750685593,
1431
+ 883997877,
1432
+ 3785050280,
1433
+ 958139571,
1434
+ 3318307427,
1435
+ 1322822218,
1436
+ 3812723403,
1437
+ 1537002063,
1438
+ 2003034995,
1439
+ 1747873779,
1440
+ 3602036899,
1441
+ 1955562222,
1442
+ 1575990012,
1443
+ 2024104815,
1444
+ 1125592928,
1445
+ 2227730452,
1446
+ 2716904306,
1447
+ 2361852424,
1448
+ 442776044,
1449
+ 2428436474,
1450
+ 593698344,
1451
+ 2756734187,
1452
+ 3733110249,
1453
+ 3204031479,
1454
+ 2999351573,
1455
+ 3329325298,
1456
+ 3815920427,
1457
+ 3391569614,
1458
+ 3928383900,
1459
+ 3515267271,
1460
+ 566280711,
1461
+ 3940187606,
1462
+ 3454069534,
1463
+ 4118630271,
1464
+ 4000239992,
1465
+ 116418474,
1466
+ 1914138554,
1467
+ 174292421,
1468
+ 2731055270,
1469
+ 289380356,
1470
+ 3203993006,
1471
+ 460393269,
1472
+ 320620315,
1473
+ 685471733,
1474
+ 587496836,
1475
+ 852142971,
1476
+ 1086792851,
1477
+ 1017036298,
1478
+ 365543100,
1479
+ 1126000580,
1480
+ 2618297676,
1481
+ 1288033470,
1482
+ 3409855158,
1483
+ 1501505948,
1484
+ 4234509866,
1485
+ 1607167915,
1486
+ 987167468,
1487
+ 1816402316,
1488
+ 1246189591
1489
+ ];
1490
+ function crypto_hashblocks_hl(hh, hl, m, n) {
1491
+ var wh = new Int32Array(16), wl = new Int32Array(16), bh0, bh1, bh2, bh3, bh4, bh5, bh6, bh7, bl0, bl1, bl2, bl3, bl4, bl5, bl6, bl7, th, tl, i, j, h, l, a, b, c, d;
1492
+ var ah0 = hh[0], ah1 = hh[1], ah2 = hh[2], ah3 = hh[3], ah4 = hh[4], ah5 = hh[5], ah6 = hh[6], ah7 = hh[7], al0 = hl[0], al1 = hl[1], al2 = hl[2], al3 = hl[3], al4 = hl[4], al5 = hl[5], al6 = hl[6], al7 = hl[7];
1493
+ var pos = 0;
1494
+ while (n >= 128) {
1495
+ for (i = 0; i < 16; i++) {
1496
+ j = 8 * i + pos;
1497
+ wh[i] = m[j + 0] << 24 | m[j + 1] << 16 | m[j + 2] << 8 | m[j + 3];
1498
+ wl[i] = m[j + 4] << 24 | m[j + 5] << 16 | m[j + 6] << 8 | m[j + 7];
1499
+ }
1500
+ for (i = 0; i < 80; i++) {
1501
+ bh0 = ah0;
1502
+ bh1 = ah1;
1503
+ bh2 = ah2;
1504
+ bh3 = ah3;
1505
+ bh4 = ah4;
1506
+ bh5 = ah5;
1507
+ bh6 = ah6;
1508
+ bh7 = ah7;
1509
+ bl0 = al0;
1510
+ bl1 = al1;
1511
+ bl2 = al2;
1512
+ bl3 = al3;
1513
+ bl4 = al4;
1514
+ bl5 = al5;
1515
+ bl6 = al6;
1516
+ bl7 = al7;
1517
+ h = ah7;
1518
+ l = al7;
1519
+ a = l & 65535;
1520
+ b = l >>> 16;
1521
+ c = h & 65535;
1522
+ d = h >>> 16;
1523
+ h = (ah4 >>> 14 | al4 << 32 - 14) ^ (ah4 >>> 18 | al4 << 32 - 18) ^ (al4 >>> 41 - 32 | ah4 << 32 - (41 - 32));
1524
+ l = (al4 >>> 14 | ah4 << 32 - 14) ^ (al4 >>> 18 | ah4 << 32 - 18) ^ (ah4 >>> 41 - 32 | al4 << 32 - (41 - 32));
1525
+ a += l & 65535;
1526
+ b += l >>> 16;
1527
+ c += h & 65535;
1528
+ d += h >>> 16;
1529
+ h = ah4 & ah5 ^ ~ah4 & ah6;
1530
+ l = al4 & al5 ^ ~al4 & al6;
1531
+ a += l & 65535;
1532
+ b += l >>> 16;
1533
+ c += h & 65535;
1534
+ d += h >>> 16;
1535
+ h = K[i * 2];
1536
+ l = K[i * 2 + 1];
1537
+ a += l & 65535;
1538
+ b += l >>> 16;
1539
+ c += h & 65535;
1540
+ d += h >>> 16;
1541
+ h = wh[i % 16];
1542
+ l = wl[i % 16];
1543
+ a += l & 65535;
1544
+ b += l >>> 16;
1545
+ c += h & 65535;
1546
+ d += h >>> 16;
1547
+ b += a >>> 16;
1548
+ c += b >>> 16;
1549
+ d += c >>> 16;
1550
+ th = c & 65535 | d << 16;
1551
+ tl = a & 65535 | b << 16;
1552
+ h = th;
1553
+ l = tl;
1554
+ a = l & 65535;
1555
+ b = l >>> 16;
1556
+ c = h & 65535;
1557
+ d = h >>> 16;
1558
+ h = (ah0 >>> 28 | al0 << 32 - 28) ^ (al0 >>> 34 - 32 | ah0 << 32 - (34 - 32)) ^ (al0 >>> 39 - 32 | ah0 << 32 - (39 - 32));
1559
+ l = (al0 >>> 28 | ah0 << 32 - 28) ^ (ah0 >>> 34 - 32 | al0 << 32 - (34 - 32)) ^ (ah0 >>> 39 - 32 | al0 << 32 - (39 - 32));
1560
+ a += l & 65535;
1561
+ b += l >>> 16;
1562
+ c += h & 65535;
1563
+ d += h >>> 16;
1564
+ h = ah0 & ah1 ^ ah0 & ah2 ^ ah1 & ah2;
1565
+ l = al0 & al1 ^ al0 & al2 ^ al1 & al2;
1566
+ a += l & 65535;
1567
+ b += l >>> 16;
1568
+ c += h & 65535;
1569
+ d += h >>> 16;
1570
+ b += a >>> 16;
1571
+ c += b >>> 16;
1572
+ d += c >>> 16;
1573
+ bh7 = c & 65535 | d << 16;
1574
+ bl7 = a & 65535 | b << 16;
1575
+ h = bh3;
1576
+ l = bl3;
1577
+ a = l & 65535;
1578
+ b = l >>> 16;
1579
+ c = h & 65535;
1580
+ d = h >>> 16;
1581
+ h = th;
1582
+ l = tl;
1583
+ a += l & 65535;
1584
+ b += l >>> 16;
1585
+ c += h & 65535;
1586
+ d += h >>> 16;
1587
+ b += a >>> 16;
1588
+ c += b >>> 16;
1589
+ d += c >>> 16;
1590
+ bh3 = c & 65535 | d << 16;
1591
+ bl3 = a & 65535 | b << 16;
1592
+ ah1 = bh0;
1593
+ ah2 = bh1;
1594
+ ah3 = bh2;
1595
+ ah4 = bh3;
1596
+ ah5 = bh4;
1597
+ ah6 = bh5;
1598
+ ah7 = bh6;
1599
+ ah0 = bh7;
1600
+ al1 = bl0;
1601
+ al2 = bl1;
1602
+ al3 = bl2;
1603
+ al4 = bl3;
1604
+ al5 = bl4;
1605
+ al6 = bl5;
1606
+ al7 = bl6;
1607
+ al0 = bl7;
1608
+ if (i % 16 === 15) {
1609
+ for (j = 0; j < 16; j++) {
1610
+ h = wh[j];
1611
+ l = wl[j];
1612
+ a = l & 65535;
1613
+ b = l >>> 16;
1614
+ c = h & 65535;
1615
+ d = h >>> 16;
1616
+ h = wh[(j + 9) % 16];
1617
+ l = wl[(j + 9) % 16];
1618
+ a += l & 65535;
1619
+ b += l >>> 16;
1620
+ c += h & 65535;
1621
+ d += h >>> 16;
1622
+ th = wh[(j + 1) % 16];
1623
+ tl = wl[(j + 1) % 16];
1624
+ h = (th >>> 1 | tl << 32 - 1) ^ (th >>> 8 | tl << 32 - 8) ^ th >>> 7;
1625
+ l = (tl >>> 1 | th << 32 - 1) ^ (tl >>> 8 | th << 32 - 8) ^ (tl >>> 7 | th << 32 - 7);
1626
+ a += l & 65535;
1627
+ b += l >>> 16;
1628
+ c += h & 65535;
1629
+ d += h >>> 16;
1630
+ th = wh[(j + 14) % 16];
1631
+ tl = wl[(j + 14) % 16];
1632
+ h = (th >>> 19 | tl << 32 - 19) ^ (tl >>> 61 - 32 | th << 32 - (61 - 32)) ^ th >>> 6;
1633
+ l = (tl >>> 19 | th << 32 - 19) ^ (th >>> 61 - 32 | tl << 32 - (61 - 32)) ^ (tl >>> 6 | th << 32 - 6);
1634
+ a += l & 65535;
1635
+ b += l >>> 16;
1636
+ c += h & 65535;
1637
+ d += h >>> 16;
1638
+ b += a >>> 16;
1639
+ c += b >>> 16;
1640
+ d += c >>> 16;
1641
+ wh[j] = c & 65535 | d << 16;
1642
+ wl[j] = a & 65535 | b << 16;
1643
+ }
1644
+ }
1645
+ }
1646
+ h = ah0;
1647
+ l = al0;
1648
+ a = l & 65535;
1649
+ b = l >>> 16;
1650
+ c = h & 65535;
1651
+ d = h >>> 16;
1652
+ h = hh[0];
1653
+ l = hl[0];
1654
+ a += l & 65535;
1655
+ b += l >>> 16;
1656
+ c += h & 65535;
1657
+ d += h >>> 16;
1658
+ b += a >>> 16;
1659
+ c += b >>> 16;
1660
+ d += c >>> 16;
1661
+ hh[0] = ah0 = c & 65535 | d << 16;
1662
+ hl[0] = al0 = a & 65535 | b << 16;
1663
+ h = ah1;
1664
+ l = al1;
1665
+ a = l & 65535;
1666
+ b = l >>> 16;
1667
+ c = h & 65535;
1668
+ d = h >>> 16;
1669
+ h = hh[1];
1670
+ l = hl[1];
1671
+ a += l & 65535;
1672
+ b += l >>> 16;
1673
+ c += h & 65535;
1674
+ d += h >>> 16;
1675
+ b += a >>> 16;
1676
+ c += b >>> 16;
1677
+ d += c >>> 16;
1678
+ hh[1] = ah1 = c & 65535 | d << 16;
1679
+ hl[1] = al1 = a & 65535 | b << 16;
1680
+ h = ah2;
1681
+ l = al2;
1682
+ a = l & 65535;
1683
+ b = l >>> 16;
1684
+ c = h & 65535;
1685
+ d = h >>> 16;
1686
+ h = hh[2];
1687
+ l = hl[2];
1688
+ a += l & 65535;
1689
+ b += l >>> 16;
1690
+ c += h & 65535;
1691
+ d += h >>> 16;
1692
+ b += a >>> 16;
1693
+ c += b >>> 16;
1694
+ d += c >>> 16;
1695
+ hh[2] = ah2 = c & 65535 | d << 16;
1696
+ hl[2] = al2 = a & 65535 | b << 16;
1697
+ h = ah3;
1698
+ l = al3;
1699
+ a = l & 65535;
1700
+ b = l >>> 16;
1701
+ c = h & 65535;
1702
+ d = h >>> 16;
1703
+ h = hh[3];
1704
+ l = hl[3];
1705
+ a += l & 65535;
1706
+ b += l >>> 16;
1707
+ c += h & 65535;
1708
+ d += h >>> 16;
1709
+ b += a >>> 16;
1710
+ c += b >>> 16;
1711
+ d += c >>> 16;
1712
+ hh[3] = ah3 = c & 65535 | d << 16;
1713
+ hl[3] = al3 = a & 65535 | b << 16;
1714
+ h = ah4;
1715
+ l = al4;
1716
+ a = l & 65535;
1717
+ b = l >>> 16;
1718
+ c = h & 65535;
1719
+ d = h >>> 16;
1720
+ h = hh[4];
1721
+ l = hl[4];
1722
+ a += l & 65535;
1723
+ b += l >>> 16;
1724
+ c += h & 65535;
1725
+ d += h >>> 16;
1726
+ b += a >>> 16;
1727
+ c += b >>> 16;
1728
+ d += c >>> 16;
1729
+ hh[4] = ah4 = c & 65535 | d << 16;
1730
+ hl[4] = al4 = a & 65535 | b << 16;
1731
+ h = ah5;
1732
+ l = al5;
1733
+ a = l & 65535;
1734
+ b = l >>> 16;
1735
+ c = h & 65535;
1736
+ d = h >>> 16;
1737
+ h = hh[5];
1738
+ l = hl[5];
1739
+ a += l & 65535;
1740
+ b += l >>> 16;
1741
+ c += h & 65535;
1742
+ d += h >>> 16;
1743
+ b += a >>> 16;
1744
+ c += b >>> 16;
1745
+ d += c >>> 16;
1746
+ hh[5] = ah5 = c & 65535 | d << 16;
1747
+ hl[5] = al5 = a & 65535 | b << 16;
1748
+ h = ah6;
1749
+ l = al6;
1750
+ a = l & 65535;
1751
+ b = l >>> 16;
1752
+ c = h & 65535;
1753
+ d = h >>> 16;
1754
+ h = hh[6];
1755
+ l = hl[6];
1756
+ a += l & 65535;
1757
+ b += l >>> 16;
1758
+ c += h & 65535;
1759
+ d += h >>> 16;
1760
+ b += a >>> 16;
1761
+ c += b >>> 16;
1762
+ d += c >>> 16;
1763
+ hh[6] = ah6 = c & 65535 | d << 16;
1764
+ hl[6] = al6 = a & 65535 | b << 16;
1765
+ h = ah7;
1766
+ l = al7;
1767
+ a = l & 65535;
1768
+ b = l >>> 16;
1769
+ c = h & 65535;
1770
+ d = h >>> 16;
1771
+ h = hh[7];
1772
+ l = hl[7];
1773
+ a += l & 65535;
1774
+ b += l >>> 16;
1775
+ c += h & 65535;
1776
+ d += h >>> 16;
1777
+ b += a >>> 16;
1778
+ c += b >>> 16;
1779
+ d += c >>> 16;
1780
+ hh[7] = ah7 = c & 65535 | d << 16;
1781
+ hl[7] = al7 = a & 65535 | b << 16;
1782
+ pos += 128;
1783
+ n -= 128;
1784
+ }
1785
+ return n;
1786
+ }
1787
+ function crypto_hash(out, m, n) {
1788
+ var hh = new Int32Array(8), hl = new Int32Array(8), x = new Uint8Array(256), i, b = n;
1789
+ hh[0] = 1779033703;
1790
+ hh[1] = 3144134277;
1791
+ hh[2] = 1013904242;
1792
+ hh[3] = 2773480762;
1793
+ hh[4] = 1359893119;
1794
+ hh[5] = 2600822924;
1795
+ hh[6] = 528734635;
1796
+ hh[7] = 1541459225;
1797
+ hl[0] = 4089235720;
1798
+ hl[1] = 2227873595;
1799
+ hl[2] = 4271175723;
1800
+ hl[3] = 1595750129;
1801
+ hl[4] = 2917565137;
1802
+ hl[5] = 725511199;
1803
+ hl[6] = 4215389547;
1804
+ hl[7] = 327033209;
1805
+ crypto_hashblocks_hl(hh, hl, m, n);
1806
+ n %= 128;
1807
+ for (i = 0; i < n; i++) x[i] = m[b - n + i];
1808
+ x[n] = 128;
1809
+ n = 256 - 128 * (n < 112 ? 1 : 0);
1810
+ x[n - 9] = 0;
1811
+ ts64(x, n - 8, b / 536870912 | 0, b << 3);
1812
+ crypto_hashblocks_hl(hh, hl, x, n);
1813
+ for (i = 0; i < 8; i++) ts64(out, 8 * i, hh[i], hl[i]);
1814
+ return 0;
1815
+ }
1816
+ function add(p, q) {
1817
+ var a = gf(), b = gf(), c = gf(), d = gf(), e = gf(), f = gf(), g = gf(), h = gf(), t = gf();
1818
+ Z(a, p[1], p[0]);
1819
+ Z(t, q[1], q[0]);
1820
+ M(a, a, t);
1821
+ A(b, p[0], p[1]);
1822
+ A(t, q[0], q[1]);
1823
+ M(b, b, t);
1824
+ M(c, p[3], q[3]);
1825
+ M(c, c, D2);
1826
+ M(d, p[2], q[2]);
1827
+ A(d, d, d);
1828
+ Z(e, b, a);
1829
+ Z(f, d, c);
1830
+ A(g, d, c);
1831
+ A(h, b, a);
1832
+ M(p[0], e, f);
1833
+ M(p[1], h, g);
1834
+ M(p[2], g, f);
1835
+ M(p[3], e, h);
1836
+ }
1837
+ function cswap(p, q, b) {
1838
+ var i;
1839
+ for (i = 0; i < 4; i++) {
1840
+ sel25519(p[i], q[i], b);
1841
+ }
1842
+ }
1843
+ function pack(r, p) {
1844
+ var tx = gf(), ty = gf(), zi = gf();
1845
+ inv25519(zi, p[2]);
1846
+ M(tx, p[0], zi);
1847
+ M(ty, p[1], zi);
1848
+ pack25519(r, ty);
1849
+ r[31] ^= par25519(tx) << 7;
1850
+ }
1851
+ function scalarmult(p, q, s) {
1852
+ var b, i;
1853
+ set25519(p[0], gf0);
1854
+ set25519(p[1], gf1);
1855
+ set25519(p[2], gf1);
1856
+ set25519(p[3], gf0);
1857
+ for (i = 255; i >= 0; --i) {
1858
+ b = s[i / 8 | 0] >> (i & 7) & 1;
1859
+ cswap(p, q, b);
1860
+ add(q, p);
1861
+ add(p, p);
1862
+ cswap(p, q, b);
1863
+ }
1864
+ }
1865
+ function scalarbase(p, s) {
1866
+ var q = [gf(), gf(), gf(), gf()];
1867
+ set25519(q[0], X);
1868
+ set25519(q[1], Y);
1869
+ set25519(q[2], gf1);
1870
+ M(q[3], X, Y);
1871
+ scalarmult(p, q, s);
1872
+ }
1873
+ function crypto_sign_keypair(pk, sk, seeded) {
1874
+ var d = new Uint8Array(64);
1875
+ var p = [gf(), gf(), gf(), gf()];
1876
+ var i;
1877
+ if (!seeded) randombytes(sk, 32);
1878
+ crypto_hash(d, sk, 32);
1879
+ d[0] &= 248;
1880
+ d[31] &= 127;
1881
+ d[31] |= 64;
1882
+ scalarbase(p, d);
1883
+ pack(pk, p);
1884
+ for (i = 0; i < 32; i++) sk[i + 32] = pk[i];
1885
+ return 0;
1886
+ }
1887
+ var L = new Float64Array([237, 211, 245, 92, 26, 99, 18, 88, 214, 156, 247, 162, 222, 249, 222, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16]);
1888
+ function modL(r, x) {
1889
+ var carry, i, j, k;
1890
+ for (i = 63; i >= 32; --i) {
1891
+ carry = 0;
1892
+ for (j = i - 32, k = i - 12; j < k; ++j) {
1893
+ x[j] += carry - 16 * x[i] * L[j - (i - 32)];
1894
+ carry = Math.floor((x[j] + 128) / 256);
1895
+ x[j] -= carry * 256;
1896
+ }
1897
+ x[j] += carry;
1898
+ x[i] = 0;
1899
+ }
1900
+ carry = 0;
1901
+ for (j = 0; j < 32; j++) {
1902
+ x[j] += carry - (x[31] >> 4) * L[j];
1903
+ carry = x[j] >> 8;
1904
+ x[j] &= 255;
1905
+ }
1906
+ for (j = 0; j < 32; j++) x[j] -= carry * L[j];
1907
+ for (i = 0; i < 32; i++) {
1908
+ x[i + 1] += x[i] >> 8;
1909
+ r[i] = x[i] & 255;
1910
+ }
1911
+ }
1912
+ function reduce(r) {
1913
+ var x = new Float64Array(64), i;
1914
+ for (i = 0; i < 64; i++) x[i] = r[i];
1915
+ for (i = 0; i < 64; i++) r[i] = 0;
1916
+ modL(r, x);
1917
+ }
1918
+ function crypto_sign(sm, m, n, sk) {
1919
+ var d = new Uint8Array(64), h = new Uint8Array(64), r = new Uint8Array(64);
1920
+ var i, j, x = new Float64Array(64);
1921
+ var p = [gf(), gf(), gf(), gf()];
1922
+ crypto_hash(d, sk, 32);
1923
+ d[0] &= 248;
1924
+ d[31] &= 127;
1925
+ d[31] |= 64;
1926
+ var smlen = n + 64;
1927
+ for (i = 0; i < n; i++) sm[64 + i] = m[i];
1928
+ for (i = 0; i < 32; i++) sm[32 + i] = d[32 + i];
1929
+ crypto_hash(r, sm.subarray(32), n + 32);
1930
+ reduce(r);
1931
+ scalarbase(p, r);
1932
+ pack(sm, p);
1933
+ for (i = 32; i < 64; i++) sm[i] = sk[i];
1934
+ crypto_hash(h, sm, n + 64);
1935
+ reduce(h);
1936
+ for (i = 0; i < 64; i++) x[i] = 0;
1937
+ for (i = 0; i < 32; i++) x[i] = r[i];
1938
+ for (i = 0; i < 32; i++) {
1939
+ for (j = 0; j < 32; j++) {
1940
+ x[i + j] += h[i] * d[j];
1941
+ }
1942
+ }
1943
+ modL(sm.subarray(32), x);
1944
+ return smlen;
1945
+ }
1946
+ function unpackneg(r, p) {
1947
+ var t = gf(), chk = gf(), num = gf(), den = gf(), den2 = gf(), den4 = gf(), den6 = gf();
1948
+ set25519(r[2], gf1);
1949
+ unpack25519(r[1], p);
1950
+ S(num, r[1]);
1951
+ M(den, num, D);
1952
+ Z(num, num, r[2]);
1953
+ A(den, r[2], den);
1954
+ S(den2, den);
1955
+ S(den4, den2);
1956
+ M(den6, den4, den2);
1957
+ M(t, den6, num);
1958
+ M(t, t, den);
1959
+ pow2523(t, t);
1960
+ M(t, t, num);
1961
+ M(t, t, den);
1962
+ M(t, t, den);
1963
+ M(r[0], t, den);
1964
+ S(chk, r[0]);
1965
+ M(chk, chk, den);
1966
+ if (neq25519(chk, num)) M(r[0], r[0], I);
1967
+ S(chk, r[0]);
1968
+ M(chk, chk, den);
1969
+ if (neq25519(chk, num)) return -1;
1970
+ if (par25519(r[0]) === p[31] >> 7) Z(r[0], gf0, r[0]);
1971
+ M(r[3], r[0], r[1]);
1972
+ return 0;
1973
+ }
1974
+ function crypto_sign_open(m, sm, n, pk) {
1975
+ var i;
1976
+ var t = new Uint8Array(32), h = new Uint8Array(64);
1977
+ var p = [gf(), gf(), gf(), gf()], q = [gf(), gf(), gf(), gf()];
1978
+ if (n < 64) return -1;
1979
+ if (unpackneg(q, pk)) return -1;
1980
+ for (i = 0; i < n; i++) m[i] = sm[i];
1981
+ for (i = 0; i < 32; i++) m[i + 32] = pk[i];
1982
+ crypto_hash(h, m, n);
1983
+ reduce(h);
1984
+ scalarmult(p, q, h);
1985
+ scalarbase(q, sm.subarray(32));
1986
+ add(p, q);
1987
+ pack(t, p);
1988
+ n -= 64;
1989
+ if (crypto_verify_32(sm, 0, t, 0)) {
1990
+ for (i = 0; i < n; i++) m[i] = 0;
1991
+ return -1;
1992
+ }
1993
+ for (i = 0; i < n; i++) m[i] = sm[i + 64];
1994
+ return n;
1995
+ }
1996
+ var crypto_secretbox_KEYBYTES = 32, crypto_secretbox_NONCEBYTES = 24, crypto_secretbox_ZEROBYTES = 32, crypto_secretbox_BOXZEROBYTES = 16, crypto_scalarmult_BYTES = 32, crypto_scalarmult_SCALARBYTES = 32, crypto_box_PUBLICKEYBYTES = 32, crypto_box_SECRETKEYBYTES = 32, crypto_box_BEFORENMBYTES = 32, crypto_box_NONCEBYTES = crypto_secretbox_NONCEBYTES, crypto_box_ZEROBYTES = crypto_secretbox_ZEROBYTES, crypto_box_BOXZEROBYTES = crypto_secretbox_BOXZEROBYTES, crypto_sign_BYTES = 64, crypto_sign_PUBLICKEYBYTES = 32, crypto_sign_SECRETKEYBYTES = 64, crypto_sign_SEEDBYTES = 32, crypto_hash_BYTES = 64;
1997
+ nacl2.lowlevel = {
1998
+ crypto_core_hsalsa20,
1999
+ crypto_stream_xor,
2000
+ crypto_stream,
2001
+ crypto_stream_salsa20_xor,
2002
+ crypto_stream_salsa20,
2003
+ crypto_onetimeauth,
2004
+ crypto_onetimeauth_verify,
2005
+ crypto_verify_16,
2006
+ crypto_verify_32,
2007
+ crypto_secretbox,
2008
+ crypto_secretbox_open,
2009
+ crypto_scalarmult,
2010
+ crypto_scalarmult_base,
2011
+ crypto_box_beforenm,
2012
+ crypto_box_afternm,
2013
+ crypto_box,
2014
+ crypto_box_open,
2015
+ crypto_box_keypair,
2016
+ crypto_hash,
2017
+ crypto_sign,
2018
+ crypto_sign_keypair,
2019
+ crypto_sign_open,
2020
+ crypto_secretbox_KEYBYTES,
2021
+ crypto_secretbox_NONCEBYTES,
2022
+ crypto_secretbox_ZEROBYTES,
2023
+ crypto_secretbox_BOXZEROBYTES,
2024
+ crypto_scalarmult_BYTES,
2025
+ crypto_scalarmult_SCALARBYTES,
2026
+ crypto_box_PUBLICKEYBYTES,
2027
+ crypto_box_SECRETKEYBYTES,
2028
+ crypto_box_BEFORENMBYTES,
2029
+ crypto_box_NONCEBYTES,
2030
+ crypto_box_ZEROBYTES,
2031
+ crypto_box_BOXZEROBYTES,
2032
+ crypto_sign_BYTES,
2033
+ crypto_sign_PUBLICKEYBYTES,
2034
+ crypto_sign_SECRETKEYBYTES,
2035
+ crypto_sign_SEEDBYTES,
2036
+ crypto_hash_BYTES,
2037
+ gf,
2038
+ D,
2039
+ L,
2040
+ pack25519,
2041
+ unpack25519,
2042
+ M,
2043
+ A,
2044
+ S,
2045
+ Z,
2046
+ pow2523,
2047
+ add,
2048
+ set25519,
2049
+ modL,
2050
+ scalarmult,
2051
+ scalarbase
2052
+ };
2053
+ function checkLengths(k, n) {
2054
+ if (k.length !== crypto_secretbox_KEYBYTES) throw new Error("bad key size");
2055
+ if (n.length !== crypto_secretbox_NONCEBYTES) throw new Error("bad nonce size");
2056
+ }
2057
+ function checkBoxLengths(pk, sk) {
2058
+ if (pk.length !== crypto_box_PUBLICKEYBYTES) throw new Error("bad public key size");
2059
+ if (sk.length !== crypto_box_SECRETKEYBYTES) throw new Error("bad secret key size");
2060
+ }
2061
+ function checkArrayTypes() {
2062
+ for (var i = 0; i < arguments.length; i++) {
2063
+ if (!(arguments[i] instanceof Uint8Array))
2064
+ throw new TypeError("unexpected type, use Uint8Array");
2065
+ }
2066
+ }
2067
+ function cleanup(arr) {
2068
+ for (var i = 0; i < arr.length; i++) arr[i] = 0;
2069
+ }
2070
+ nacl2.randomBytes = function(n) {
2071
+ var b = new Uint8Array(n);
2072
+ randombytes(b, n);
2073
+ return b;
2074
+ };
2075
+ nacl2.secretbox = function(msg, nonce, key) {
2076
+ checkArrayTypes(msg, nonce, key);
2077
+ checkLengths(key, nonce);
2078
+ var m = new Uint8Array(crypto_secretbox_ZEROBYTES + msg.length);
2079
+ var c = new Uint8Array(m.length);
2080
+ for (var i = 0; i < msg.length; i++) m[i + crypto_secretbox_ZEROBYTES] = msg[i];
2081
+ crypto_secretbox(c, m, m.length, nonce, key);
2082
+ return c.subarray(crypto_secretbox_BOXZEROBYTES);
2083
+ };
2084
+ nacl2.secretbox.open = function(box, nonce, key) {
2085
+ checkArrayTypes(box, nonce, key);
2086
+ checkLengths(key, nonce);
2087
+ var c = new Uint8Array(crypto_secretbox_BOXZEROBYTES + box.length);
2088
+ var m = new Uint8Array(c.length);
2089
+ for (var i = 0; i < box.length; i++) c[i + crypto_secretbox_BOXZEROBYTES] = box[i];
2090
+ if (c.length < 32) return null;
2091
+ if (crypto_secretbox_open(m, c, c.length, nonce, key) !== 0) return null;
2092
+ return m.subarray(crypto_secretbox_ZEROBYTES);
2093
+ };
2094
+ nacl2.secretbox.keyLength = crypto_secretbox_KEYBYTES;
2095
+ nacl2.secretbox.nonceLength = crypto_secretbox_NONCEBYTES;
2096
+ nacl2.secretbox.overheadLength = crypto_secretbox_BOXZEROBYTES;
2097
+ nacl2.scalarMult = function(n, p) {
2098
+ checkArrayTypes(n, p);
2099
+ if (n.length !== crypto_scalarmult_SCALARBYTES) throw new Error("bad n size");
2100
+ if (p.length !== crypto_scalarmult_BYTES) throw new Error("bad p size");
2101
+ var q = new Uint8Array(crypto_scalarmult_BYTES);
2102
+ crypto_scalarmult(q, n, p);
2103
+ return q;
2104
+ };
2105
+ nacl2.scalarMult.base = function(n) {
2106
+ checkArrayTypes(n);
2107
+ if (n.length !== crypto_scalarmult_SCALARBYTES) throw new Error("bad n size");
2108
+ var q = new Uint8Array(crypto_scalarmult_BYTES);
2109
+ crypto_scalarmult_base(q, n);
2110
+ return q;
2111
+ };
2112
+ nacl2.scalarMult.scalarLength = crypto_scalarmult_SCALARBYTES;
2113
+ nacl2.scalarMult.groupElementLength = crypto_scalarmult_BYTES;
2114
+ nacl2.box = function(msg, nonce, publicKey, secretKey) {
2115
+ var k = nacl2.box.before(publicKey, secretKey);
2116
+ return nacl2.secretbox(msg, nonce, k);
2117
+ };
2118
+ nacl2.box.before = function(publicKey, secretKey) {
2119
+ checkArrayTypes(publicKey, secretKey);
2120
+ checkBoxLengths(publicKey, secretKey);
2121
+ var k = new Uint8Array(crypto_box_BEFORENMBYTES);
2122
+ crypto_box_beforenm(k, publicKey, secretKey);
2123
+ return k;
2124
+ };
2125
+ nacl2.box.after = nacl2.secretbox;
2126
+ nacl2.box.open = function(msg, nonce, publicKey, secretKey) {
2127
+ var k = nacl2.box.before(publicKey, secretKey);
2128
+ return nacl2.secretbox.open(msg, nonce, k);
2129
+ };
2130
+ nacl2.box.open.after = nacl2.secretbox.open;
2131
+ nacl2.box.keyPair = function() {
2132
+ var pk = new Uint8Array(crypto_box_PUBLICKEYBYTES);
2133
+ var sk = new Uint8Array(crypto_box_SECRETKEYBYTES);
2134
+ crypto_box_keypair(pk, sk);
2135
+ return { publicKey: pk, secretKey: sk };
2136
+ };
2137
+ nacl2.box.keyPair.fromSecretKey = function(secretKey) {
2138
+ checkArrayTypes(secretKey);
2139
+ if (secretKey.length !== crypto_box_SECRETKEYBYTES)
2140
+ throw new Error("bad secret key size");
2141
+ var pk = new Uint8Array(crypto_box_PUBLICKEYBYTES);
2142
+ crypto_scalarmult_base(pk, secretKey);
2143
+ return { publicKey: pk, secretKey: new Uint8Array(secretKey) };
2144
+ };
2145
+ nacl2.box.publicKeyLength = crypto_box_PUBLICKEYBYTES;
2146
+ nacl2.box.secretKeyLength = crypto_box_SECRETKEYBYTES;
2147
+ nacl2.box.sharedKeyLength = crypto_box_BEFORENMBYTES;
2148
+ nacl2.box.nonceLength = crypto_box_NONCEBYTES;
2149
+ nacl2.box.overheadLength = nacl2.secretbox.overheadLength;
2150
+ nacl2.sign = function(msg, secretKey) {
2151
+ checkArrayTypes(msg, secretKey);
2152
+ if (secretKey.length !== crypto_sign_SECRETKEYBYTES)
2153
+ throw new Error("bad secret key size");
2154
+ var signedMsg = new Uint8Array(crypto_sign_BYTES + msg.length);
2155
+ crypto_sign(signedMsg, msg, msg.length, secretKey);
2156
+ return signedMsg;
2157
+ };
2158
+ nacl2.sign.open = function(signedMsg, publicKey) {
2159
+ checkArrayTypes(signedMsg, publicKey);
2160
+ if (publicKey.length !== crypto_sign_PUBLICKEYBYTES)
2161
+ throw new Error("bad public key size");
2162
+ var tmp = new Uint8Array(signedMsg.length);
2163
+ var mlen = crypto_sign_open(tmp, signedMsg, signedMsg.length, publicKey);
2164
+ if (mlen < 0) return null;
2165
+ var m = new Uint8Array(mlen);
2166
+ for (var i = 0; i < m.length; i++) m[i] = tmp[i];
2167
+ return m;
2168
+ };
2169
+ nacl2.sign.detached = function(msg, secretKey) {
2170
+ var signedMsg = nacl2.sign(msg, secretKey);
2171
+ var sig = new Uint8Array(crypto_sign_BYTES);
2172
+ for (var i = 0; i < sig.length; i++) sig[i] = signedMsg[i];
2173
+ return sig;
2174
+ };
2175
+ nacl2.sign.detached.verify = function(msg, sig, publicKey) {
2176
+ checkArrayTypes(msg, sig, publicKey);
2177
+ if (sig.length !== crypto_sign_BYTES)
2178
+ throw new Error("bad signature size");
2179
+ if (publicKey.length !== crypto_sign_PUBLICKEYBYTES)
2180
+ throw new Error("bad public key size");
2181
+ var sm = new Uint8Array(crypto_sign_BYTES + msg.length);
2182
+ var m = new Uint8Array(crypto_sign_BYTES + msg.length);
2183
+ var i;
2184
+ for (i = 0; i < crypto_sign_BYTES; i++) sm[i] = sig[i];
2185
+ for (i = 0; i < msg.length; i++) sm[i + crypto_sign_BYTES] = msg[i];
2186
+ return crypto_sign_open(m, sm, sm.length, publicKey) >= 0;
2187
+ };
2188
+ nacl2.sign.keyPair = function() {
2189
+ var pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES);
2190
+ var sk = new Uint8Array(crypto_sign_SECRETKEYBYTES);
2191
+ crypto_sign_keypair(pk, sk);
2192
+ return { publicKey: pk, secretKey: sk };
2193
+ };
2194
+ nacl2.sign.keyPair.fromSecretKey = function(secretKey) {
2195
+ checkArrayTypes(secretKey);
2196
+ if (secretKey.length !== crypto_sign_SECRETKEYBYTES)
2197
+ throw new Error("bad secret key size");
2198
+ var pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES);
2199
+ for (var i = 0; i < pk.length; i++) pk[i] = secretKey[32 + i];
2200
+ return { publicKey: pk, secretKey: new Uint8Array(secretKey) };
2201
+ };
2202
+ nacl2.sign.keyPair.fromSeed = function(seed) {
2203
+ checkArrayTypes(seed);
2204
+ if (seed.length !== crypto_sign_SEEDBYTES)
2205
+ throw new Error("bad seed size");
2206
+ var pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES);
2207
+ var sk = new Uint8Array(crypto_sign_SECRETKEYBYTES);
2208
+ for (var i = 0; i < 32; i++) sk[i] = seed[i];
2209
+ crypto_sign_keypair(pk, sk, true);
2210
+ return { publicKey: pk, secretKey: sk };
2211
+ };
2212
+ nacl2.sign.publicKeyLength = crypto_sign_PUBLICKEYBYTES;
2213
+ nacl2.sign.secretKeyLength = crypto_sign_SECRETKEYBYTES;
2214
+ nacl2.sign.seedLength = crypto_sign_SEEDBYTES;
2215
+ nacl2.sign.signatureLength = crypto_sign_BYTES;
2216
+ nacl2.hash = function(msg) {
2217
+ checkArrayTypes(msg);
2218
+ var h = new Uint8Array(crypto_hash_BYTES);
2219
+ crypto_hash(h, msg, msg.length);
2220
+ return h;
2221
+ };
2222
+ nacl2.hash.hashLength = crypto_hash_BYTES;
2223
+ nacl2.verify = function(x, y) {
2224
+ checkArrayTypes(x, y);
2225
+ if (x.length === 0 || y.length === 0) return false;
2226
+ if (x.length !== y.length) return false;
2227
+ return vn(x, 0, y, 0, x.length) === 0 ? true : false;
2228
+ };
2229
+ nacl2.setPRNG = function(fn) {
2230
+ randombytes = fn;
2231
+ };
2232
+ (function() {
2233
+ var crypto = typeof self !== "undefined" ? self.crypto || self.msCrypto : null;
2234
+ if (crypto && crypto.getRandomValues) {
2235
+ var QUOTA = 65536;
2236
+ nacl2.setPRNG(function(x, n) {
2237
+ var i, v = new Uint8Array(n);
2238
+ for (i = 0; i < n; i += QUOTA) {
2239
+ crypto.getRandomValues(v.subarray(i, i + Math.min(n - i, QUOTA)));
2240
+ }
2241
+ for (i = 0; i < n; i++) x[i] = v[i];
2242
+ cleanup(v);
2243
+ });
2244
+ } else if (typeof require !== "undefined") {
2245
+ crypto = require("crypto");
2246
+ if (crypto && crypto.randomBytes) {
2247
+ nacl2.setPRNG(function(x, n) {
2248
+ var i, v = crypto.randomBytes(n);
2249
+ for (i = 0; i < n; i++) x[i] = v[i];
2250
+ cleanup(v);
2251
+ });
2252
+ }
2253
+ }
2254
+ })();
2255
+ })(typeof module2 !== "undefined" && module2.exports ? module2.exports : self.nacl = self.nacl || {});
2256
+ }
2257
+ });
2258
+
2259
+ // node_modules/tweetnacl-util/nacl-util.js
2260
+ var require_nacl_util = __commonJS({
2261
+ "node_modules/tweetnacl-util/nacl-util.js"(exports2, module2) {
2262
+ "use strict";
2263
+ (function(root, f) {
2264
+ "use strict";
2265
+ if (typeof module2 !== "undefined" && module2.exports) module2.exports = f();
2266
+ else if (root.nacl) root.nacl.util = f();
2267
+ else {
2268
+ root.nacl = {};
2269
+ root.nacl.util = f();
2270
+ }
2271
+ })(exports2, function() {
2272
+ "use strict";
2273
+ var util = {};
2274
+ function validateBase64(s) {
2275
+ if (!/^(?:[A-Za-z0-9+\/]{2}[A-Za-z0-9+\/]{2})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?$/.test(s)) {
2276
+ throw new TypeError("invalid encoding");
2277
+ }
2278
+ }
2279
+ util.decodeUTF8 = function(s) {
2280
+ if (typeof s !== "string") throw new TypeError("expected string");
2281
+ var i, d = unescape(encodeURIComponent(s)), b = new Uint8Array(d.length);
2282
+ for (i = 0; i < d.length; i++) b[i] = d.charCodeAt(i);
2283
+ return b;
2284
+ };
2285
+ util.encodeUTF8 = function(arr) {
2286
+ var i, s = [];
2287
+ for (i = 0; i < arr.length; i++) s.push(String.fromCharCode(arr[i]));
2288
+ return decodeURIComponent(escape(s.join("")));
2289
+ };
2290
+ if (typeof atob === "undefined") {
2291
+ if (typeof Buffer.from !== "undefined") {
2292
+ util.encodeBase64 = function(arr) {
2293
+ return Buffer.from(arr).toString("base64");
2294
+ };
2295
+ util.decodeBase64 = function(s) {
2296
+ validateBase64(s);
2297
+ return new Uint8Array(Array.prototype.slice.call(Buffer.from(s, "base64"), 0));
2298
+ };
2299
+ } else {
2300
+ util.encodeBase64 = function(arr) {
2301
+ return new Buffer(arr).toString("base64");
2302
+ };
2303
+ util.decodeBase64 = function(s) {
2304
+ validateBase64(s);
2305
+ return new Uint8Array(Array.prototype.slice.call(new Buffer(s, "base64"), 0));
2306
+ };
2307
+ }
2308
+ } else {
2309
+ util.encodeBase64 = function(arr) {
2310
+ var i, s = [], len = arr.length;
2311
+ for (i = 0; i < len; i++) s.push(String.fromCharCode(arr[i]));
2312
+ return btoa(s.join(""));
2313
+ };
2314
+ util.decodeBase64 = function(s) {
2315
+ validateBase64(s);
2316
+ var i, d = atob(s), b = new Uint8Array(d.length);
2317
+ for (i = 0; i < d.length; i++) b[i] = d.charCodeAt(i);
2318
+ return b;
2319
+ };
2320
+ }
2321
+ return util;
2322
+ });
2323
+ }
2324
+ });
2325
+
2326
+ // src/index.ts
2327
+ var index_exports = {};
2328
+ __export(index_exports, {
2329
+ VoidlyAgent: () => VoidlyAgent,
2330
+ decodeBase64: () => import_tweetnacl_util.decodeBase64,
2331
+ decodeUTF8: () => import_tweetnacl_util.decodeUTF8,
2332
+ encodeBase64: () => import_tweetnacl_util.encodeBase64,
2333
+ encodeUTF8: () => import_tweetnacl_util.encodeUTF8,
2334
+ nacl: () => import_tweetnacl.default
2335
+ });
2336
+ module.exports = __toCommonJS(index_exports);
2337
+ var import_tweetnacl = __toESM(require_nacl_fast());
2338
+ var import_tweetnacl_util = __toESM(require_nacl_util());
2339
+ async function sha256(data) {
2340
+ if (typeof globalThis.crypto?.subtle !== "undefined") {
2341
+ const encoder = new TextEncoder();
2342
+ const hash = await globalThis.crypto.subtle.digest("SHA-256", encoder.encode(data));
2343
+ return Array.from(new Uint8Array(hash)).map((b) => b.toString(16).padStart(2, "0")).join("");
2344
+ }
2345
+ const { createHash } = await import("crypto");
2346
+ return createHash("sha256").update(data).digest("hex");
2347
+ }
2348
+ var VoidlyAgent = class _VoidlyAgent {
2349
+ constructor(identity, config) {
2350
+ this.did = identity.did;
2351
+ this.apiKey = identity.apiKey;
2352
+ this.signingKeyPair = identity.signingKeyPair;
2353
+ this.encryptionKeyPair = identity.encryptionKeyPair;
2354
+ this.baseUrl = config?.baseUrl || "https://api.voidly.ai";
2355
+ }
2356
+ // ─── Factory Methods ────────────────────────────────────────────────────────
2357
+ /**
2358
+ * Register a new agent on the Voidly relay.
2359
+ * Keys are generated locally — the server only receives public keys.
2360
+ */
2361
+ static async register(options = {}, config) {
2362
+ const baseUrl = config?.baseUrl || "https://api.voidly.ai";
2363
+ const signingKeyPair = import_tweetnacl.default.sign.keyPair();
2364
+ const encryptionKeyPair = import_tweetnacl.default.box.keyPair();
2365
+ const res = await fetch(`${baseUrl}/v1/agent/register`, {
2366
+ method: "POST",
2367
+ headers: { "Content-Type": "application/json" },
2368
+ body: JSON.stringify({
2369
+ name: options.name,
2370
+ capabilities: options.capabilities,
2371
+ signing_public_key: (0, import_tweetnacl_util.encodeBase64)(signingKeyPair.publicKey),
2372
+ encryption_public_key: (0, import_tweetnacl_util.encodeBase64)(encryptionKeyPair.publicKey)
2373
+ })
2374
+ });
2375
+ if (!res.ok) {
2376
+ const err = await res.json().catch(() => ({}));
2377
+ const errMsg = err.error?.message || err.error || res.statusText;
2378
+ throw new Error(`Registration failed: ${errMsg}`);
2379
+ }
2380
+ const data = await res.json();
2381
+ return new _VoidlyAgent({
2382
+ did: data.did,
2383
+ apiKey: data.api_key,
2384
+ signingKeyPair,
2385
+ encryptionKeyPair
2386
+ }, config);
2387
+ }
2388
+ /**
2389
+ * Restore an agent from saved credentials.
2390
+ * Use this to resume an agent across sessions.
2391
+ */
2392
+ static fromCredentials(creds, config) {
2393
+ const signingSecret = (0, import_tweetnacl_util.decodeBase64)(creds.signingSecretKey);
2394
+ const encryptionSecret = (0, import_tweetnacl_util.decodeBase64)(creds.encryptionSecretKey);
2395
+ return new _VoidlyAgent({
2396
+ did: creds.did,
2397
+ apiKey: creds.apiKey,
2398
+ signingKeyPair: import_tweetnacl.default.sign.keyPair.fromSecretKey(signingSecret),
2399
+ encryptionKeyPair: {
2400
+ publicKey: import_tweetnacl.default.box.keyPair.fromSecretKey(encryptionSecret).publicKey,
2401
+ secretKey: encryptionSecret
2402
+ }
2403
+ }, config);
2404
+ }
2405
+ /**
2406
+ * Export credentials for persistence.
2407
+ * Store these securely — they contain private keys.
2408
+ */
2409
+ exportCredentials() {
2410
+ return {
2411
+ did: this.did,
2412
+ apiKey: this.apiKey,
2413
+ signingSecretKey: (0, import_tweetnacl_util.encodeBase64)(this.signingKeyPair.secretKey),
2414
+ encryptionSecretKey: (0, import_tweetnacl_util.encodeBase64)(this.encryptionKeyPair.secretKey),
2415
+ signingPublicKey: (0, import_tweetnacl_util.encodeBase64)(this.signingKeyPair.publicKey),
2416
+ encryptionPublicKey: (0, import_tweetnacl_util.encodeBase64)(this.encryptionKeyPair.publicKey)
2417
+ };
2418
+ }
2419
+ // ─── Messaging ──────────────────────────────────────────────────────────────
2420
+ /**
2421
+ * Send an E2E encrypted message. Encryption happens locally.
2422
+ * The relay server NEVER sees the plaintext or private keys.
2423
+ */
2424
+ async send(recipientDid, message, options = {}) {
2425
+ const profile = await this.getIdentity(recipientDid);
2426
+ if (!profile) {
2427
+ throw new Error(`Recipient ${recipientDid} not found`);
2428
+ }
2429
+ const recipientPubKey = (0, import_tweetnacl_util.decodeBase64)(profile.encryption_public_key);
2430
+ const messageBytes = (0, import_tweetnacl_util.decodeUTF8)(message);
2431
+ const nonce = import_tweetnacl.default.randomBytes(import_tweetnacl.default.box.nonceLength);
2432
+ const ciphertext = import_tweetnacl.default.box(messageBytes, nonce, recipientPubKey, this.encryptionKeyPair.secretKey);
2433
+ if (!ciphertext) {
2434
+ throw new Error("Encryption failed");
2435
+ }
2436
+ const envelopeData = JSON.stringify({
2437
+ from: this.did,
2438
+ to: recipientDid,
2439
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
2440
+ nonce: (0, import_tweetnacl_util.encodeBase64)(nonce),
2441
+ ciphertext_hash: await sha256((0, import_tweetnacl_util.encodeBase64)(ciphertext))
2442
+ });
2443
+ const signature = import_tweetnacl.default.sign.detached((0, import_tweetnacl_util.decodeUTF8)(envelopeData), this.signingKeyPair.secretKey);
2444
+ const res = await fetch(`${this.baseUrl}/v1/agent/send/encrypted`, {
2445
+ method: "POST",
2446
+ headers: {
2447
+ "Content-Type": "application/json",
2448
+ "X-Agent-Key": this.apiKey
2449
+ },
2450
+ body: JSON.stringify({
2451
+ to: recipientDid,
2452
+ ciphertext: (0, import_tweetnacl_util.encodeBase64)(ciphertext),
2453
+ nonce: (0, import_tweetnacl_util.encodeBase64)(nonce),
2454
+ signature: (0, import_tweetnacl_util.encodeBase64)(signature),
2455
+ envelope: envelopeData,
2456
+ // Pass signed envelope so receiver can verify
2457
+ content_type: options.contentType || "text/plain",
2458
+ thread_id: options.threadId,
2459
+ reply_to: options.replyTo,
2460
+ ttl: options.ttl
2461
+ })
2462
+ });
2463
+ if (!res.ok) {
2464
+ const err = await res.json().catch(() => ({}));
2465
+ throw new Error(`Send failed: ${err.error?.message || err.error || res.statusText}`);
2466
+ }
2467
+ return await res.json();
2468
+ }
2469
+ /**
2470
+ * Receive and decrypt messages. Decryption happens locally.
2471
+ * The relay server returns raw ciphertext — never touches private keys.
2472
+ */
2473
+ async receive(options = {}) {
2474
+ const params = new URLSearchParams();
2475
+ if (options.since) params.set("since", options.since);
2476
+ if (options.limit) params.set("limit", String(options.limit));
2477
+ const res = await fetch(`${this.baseUrl}/v1/agent/receive/raw?${params}`, {
2478
+ headers: { "X-Agent-Key": this.apiKey }
2479
+ });
2480
+ if (!res.ok) {
2481
+ const err = await res.json().catch(() => ({}));
2482
+ throw new Error(`Receive failed: ${err.error?.message || err.error || res.statusText}`);
2483
+ }
2484
+ const data = await res.json();
2485
+ const decrypted = [];
2486
+ for (const msg of data.messages) {
2487
+ try {
2488
+ const senderEncPub = (0, import_tweetnacl_util.decodeBase64)(msg.sender_encryption_key);
2489
+ const ciphertext = (0, import_tweetnacl_util.decodeBase64)(msg.ciphertext);
2490
+ const nonce = (0, import_tweetnacl_util.decodeBase64)(msg.nonce);
2491
+ const plaintext = import_tweetnacl.default.box.open(ciphertext, nonce, senderEncPub, this.encryptionKeyPair.secretKey);
2492
+ if (!plaintext) continue;
2493
+ let signatureValid = false;
2494
+ try {
2495
+ const senderSignPub = (0, import_tweetnacl_util.decodeBase64)(msg.sender_signing_key);
2496
+ const signatureBytes = (0, import_tweetnacl_util.decodeBase64)(msg.signature);
2497
+ const envelopeStr = msg.envelope || JSON.stringify({
2498
+ from: msg.from,
2499
+ to: msg.to,
2500
+ timestamp: msg.timestamp,
2501
+ nonce: msg.nonce,
2502
+ ciphertext_hash: await sha256(msg.ciphertext)
2503
+ });
2504
+ signatureValid = import_tweetnacl.default.sign.detached.verify(
2505
+ (0, import_tweetnacl_util.decodeUTF8)(envelopeStr),
2506
+ signatureBytes,
2507
+ senderSignPub
2508
+ );
2509
+ } catch {
2510
+ signatureValid = false;
2511
+ }
2512
+ decrypted.push({
2513
+ id: msg.id,
2514
+ from: msg.from,
2515
+ to: msg.to,
2516
+ content: (0, import_tweetnacl_util.encodeUTF8)(plaintext),
2517
+ contentType: msg.content_type,
2518
+ threadId: msg.thread_id,
2519
+ replyTo: msg.reply_to,
2520
+ signatureValid,
2521
+ timestamp: msg.timestamp,
2522
+ expiresAt: msg.expires_at
2523
+ });
2524
+ } catch {
2525
+ }
2526
+ }
2527
+ return decrypted;
2528
+ }
2529
+ // ─── Discovery ──────────────────────────────────────────────────────────────
2530
+ /**
2531
+ * Look up an agent's public profile and keys.
2532
+ */
2533
+ async getIdentity(did) {
2534
+ const res = await fetch(`${this.baseUrl}/v1/agent/identity/${did}`);
2535
+ if (!res.ok) return null;
2536
+ return await res.json();
2537
+ }
2538
+ /**
2539
+ * Search for agents by name or capability.
2540
+ */
2541
+ async discover(options = {}) {
2542
+ const params = new URLSearchParams();
2543
+ if (options.query) params.set("query", options.query);
2544
+ if (options.capability) params.set("capability", options.capability);
2545
+ if (options.limit) params.set("limit", String(options.limit));
2546
+ const res = await fetch(`${this.baseUrl}/v1/agent/discover?${params}`);
2547
+ if (!res.ok) return [];
2548
+ const data = await res.json();
2549
+ return data.agents;
2550
+ }
2551
+ /**
2552
+ * Get relay network statistics.
2553
+ */
2554
+ async stats() {
2555
+ const res = await fetch(`${this.baseUrl}/v1/agent/stats`);
2556
+ return await res.json();
2557
+ }
2558
+ /**
2559
+ * Rotate this agent's keypairs. Old messages encrypted with old keys cannot be re-decrypted.
2560
+ */
2561
+ async rotateKeys() {
2562
+ const newSigningKeyPair = import_tweetnacl.default.sign.keyPair();
2563
+ const newEncryptionKeyPair = import_tweetnacl.default.box.keyPair();
2564
+ const res = await fetch(`${this.baseUrl}/v1/agent/rotate-keys`, {
2565
+ method: "POST",
2566
+ headers: {
2567
+ "Content-Type": "application/json",
2568
+ "X-Agent-Key": this.apiKey
2569
+ },
2570
+ body: JSON.stringify({
2571
+ signing_public_key: (0, import_tweetnacl_util.encodeBase64)(newSigningKeyPair.publicKey),
2572
+ encryption_public_key: (0, import_tweetnacl_util.encodeBase64)(newEncryptionKeyPair.publicKey)
2573
+ })
2574
+ });
2575
+ if (!res.ok) {
2576
+ throw new Error("Key rotation failed");
2577
+ }
2578
+ this.signingKeyPair = newSigningKeyPair;
2579
+ this.encryptionKeyPair = newEncryptionKeyPair;
2580
+ }
2581
+ };
2582
+ // Annotate the CommonJS export names for ESM import in node:
2583
+ 0 && (module.exports = {
2584
+ VoidlyAgent,
2585
+ decodeBase64,
2586
+ decodeUTF8,
2587
+ encodeBase64,
2588
+ encodeUTF8,
2589
+ nacl
2590
+ });