@zubari/sdk 0.1.5 → 0.1.7

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.
@@ -4,2410 +4,12 @@ var ethers = require('ethers');
4
4
  var viem = require('viem');
5
5
  var chains = require('viem/chains');
6
6
  var bip39 = require('@scure/bip39');
7
- require('@scure/bip39/wordlists/english');
7
+ var english = require('@scure/bip39/wordlists/english');
8
8
  var bip32 = require('@scure/bip32');
9
9
  var base = require('@scure/base');
10
10
  var sha256 = require('@noble/hashes/sha256');
11
11
  var ripemd160 = require('@noble/hashes/ripemd160');
12
12
 
13
- var __create = Object.create;
14
- var __defProp = Object.defineProperty;
15
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
16
- var __getOwnPropNames = Object.getOwnPropertyNames;
17
- var __getProtoOf = Object.getPrototypeOf;
18
- var __hasOwnProp = Object.prototype.hasOwnProperty;
19
- var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
20
- get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
21
- }) : x)(function(x) {
22
- if (typeof require !== "undefined") return require.apply(this, arguments);
23
- throw Error('Dynamic require of "' + x + '" is not supported');
24
- });
25
- var __commonJS = (cb, mod) => function __require2() {
26
- return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
27
- };
28
- var __copyProps = (to, from, except, desc) => {
29
- if (from && typeof from === "object" || typeof from === "function") {
30
- for (let key of __getOwnPropNames(from))
31
- if (!__hasOwnProp.call(to, key) && key !== except)
32
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
33
- }
34
- return to;
35
- };
36
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
37
- // If the importer is in node compatibility mode or this is not an ESM
38
- // file that has been converted to a CommonJS file using a Babel-
39
- // compatible transform (i.e. "__esModule" has not been set), then set
40
- // "default" to the CommonJS "module.exports" for node compatibility.
41
- !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
42
- mod
43
- ));
44
-
45
- // ../../node_modules/tweetnacl/nacl-fast.js
46
- var require_nacl_fast = __commonJS({
47
- "../../node_modules/tweetnacl/nacl-fast.js"(exports$1, module) {
48
- (function(nacl) {
49
- var gf = function(init) {
50
- var i, r = new Float64Array(16);
51
- if (init) for (i = 0; i < init.length; i++) r[i] = init[i];
52
- return r;
53
- };
54
- var randombytes = function() {
55
- throw new Error("no PRNG");
56
- };
57
- var _0 = new Uint8Array(16);
58
- var _9 = new Uint8Array(32);
59
- _9[0] = 9;
60
- 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]);
61
- function ts64(x, i, h, l) {
62
- x[i] = h >> 24 & 255;
63
- x[i + 1] = h >> 16 & 255;
64
- x[i + 2] = h >> 8 & 255;
65
- x[i + 3] = h & 255;
66
- x[i + 4] = l >> 24 & 255;
67
- x[i + 5] = l >> 16 & 255;
68
- x[i + 6] = l >> 8 & 255;
69
- x[i + 7] = l & 255;
70
- }
71
- function vn(x, xi, y, yi, n) {
72
- var i, d = 0;
73
- for (i = 0; i < n; i++) d |= x[xi + i] ^ y[yi + i];
74
- return (1 & d - 1 >>> 8) - 1;
75
- }
76
- function crypto_verify_16(x, xi, y, yi) {
77
- return vn(x, xi, y, yi, 16);
78
- }
79
- function crypto_verify_32(x, xi, y, yi) {
80
- return vn(x, xi, y, yi, 32);
81
- }
82
- function core_salsa20(o, p, k, c) {
83
- 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;
84
- 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;
85
- for (var i = 0; i < 20; i += 2) {
86
- u = x0 + x12 | 0;
87
- x4 ^= u << 7 | u >>> 32 - 7;
88
- u = x4 + x0 | 0;
89
- x8 ^= u << 9 | u >>> 32 - 9;
90
- u = x8 + x4 | 0;
91
- x12 ^= u << 13 | u >>> 32 - 13;
92
- u = x12 + x8 | 0;
93
- x0 ^= u << 18 | u >>> 32 - 18;
94
- u = x5 + x1 | 0;
95
- x9 ^= u << 7 | u >>> 32 - 7;
96
- u = x9 + x5 | 0;
97
- x13 ^= u << 9 | u >>> 32 - 9;
98
- u = x13 + x9 | 0;
99
- x1 ^= u << 13 | u >>> 32 - 13;
100
- u = x1 + x13 | 0;
101
- x5 ^= u << 18 | u >>> 32 - 18;
102
- u = x10 + x6 | 0;
103
- x14 ^= u << 7 | u >>> 32 - 7;
104
- u = x14 + x10 | 0;
105
- x2 ^= u << 9 | u >>> 32 - 9;
106
- u = x2 + x14 | 0;
107
- x6 ^= u << 13 | u >>> 32 - 13;
108
- u = x6 + x2 | 0;
109
- x10 ^= u << 18 | u >>> 32 - 18;
110
- u = x15 + x11 | 0;
111
- x3 ^= u << 7 | u >>> 32 - 7;
112
- u = x3 + x15 | 0;
113
- x7 ^= u << 9 | u >>> 32 - 9;
114
- u = x7 + x3 | 0;
115
- x11 ^= u << 13 | u >>> 32 - 13;
116
- u = x11 + x7 | 0;
117
- x15 ^= u << 18 | u >>> 32 - 18;
118
- u = x0 + x3 | 0;
119
- x1 ^= u << 7 | u >>> 32 - 7;
120
- u = x1 + x0 | 0;
121
- x2 ^= u << 9 | u >>> 32 - 9;
122
- u = x2 + x1 | 0;
123
- x3 ^= u << 13 | u >>> 32 - 13;
124
- u = x3 + x2 | 0;
125
- x0 ^= u << 18 | u >>> 32 - 18;
126
- u = x5 + x4 | 0;
127
- x6 ^= u << 7 | u >>> 32 - 7;
128
- u = x6 + x5 | 0;
129
- x7 ^= u << 9 | u >>> 32 - 9;
130
- u = x7 + x6 | 0;
131
- x4 ^= u << 13 | u >>> 32 - 13;
132
- u = x4 + x7 | 0;
133
- x5 ^= u << 18 | u >>> 32 - 18;
134
- u = x10 + x9 | 0;
135
- x11 ^= u << 7 | u >>> 32 - 7;
136
- u = x11 + x10 | 0;
137
- x8 ^= u << 9 | u >>> 32 - 9;
138
- u = x8 + x11 | 0;
139
- x9 ^= u << 13 | u >>> 32 - 13;
140
- u = x9 + x8 | 0;
141
- x10 ^= u << 18 | u >>> 32 - 18;
142
- u = x15 + x14 | 0;
143
- x12 ^= u << 7 | u >>> 32 - 7;
144
- u = x12 + x15 | 0;
145
- x13 ^= u << 9 | u >>> 32 - 9;
146
- u = x13 + x12 | 0;
147
- x14 ^= u << 13 | u >>> 32 - 13;
148
- u = x14 + x13 | 0;
149
- x15 ^= u << 18 | u >>> 32 - 18;
150
- }
151
- x0 = x0 + j0 | 0;
152
- x1 = x1 + j1 | 0;
153
- x2 = x2 + j2 | 0;
154
- x3 = x3 + j3 | 0;
155
- x4 = x4 + j4 | 0;
156
- x5 = x5 + j5 | 0;
157
- x6 = x6 + j6 | 0;
158
- x7 = x7 + j7 | 0;
159
- x8 = x8 + j8 | 0;
160
- x9 = x9 + j9 | 0;
161
- x10 = x10 + j10 | 0;
162
- x11 = x11 + j11 | 0;
163
- x12 = x12 + j12 | 0;
164
- x13 = x13 + j13 | 0;
165
- x14 = x14 + j14 | 0;
166
- x15 = x15 + j15 | 0;
167
- o[0] = x0 >>> 0 & 255;
168
- o[1] = x0 >>> 8 & 255;
169
- o[2] = x0 >>> 16 & 255;
170
- o[3] = x0 >>> 24 & 255;
171
- o[4] = x1 >>> 0 & 255;
172
- o[5] = x1 >>> 8 & 255;
173
- o[6] = x1 >>> 16 & 255;
174
- o[7] = x1 >>> 24 & 255;
175
- o[8] = x2 >>> 0 & 255;
176
- o[9] = x2 >>> 8 & 255;
177
- o[10] = x2 >>> 16 & 255;
178
- o[11] = x2 >>> 24 & 255;
179
- o[12] = x3 >>> 0 & 255;
180
- o[13] = x3 >>> 8 & 255;
181
- o[14] = x3 >>> 16 & 255;
182
- o[15] = x3 >>> 24 & 255;
183
- o[16] = x4 >>> 0 & 255;
184
- o[17] = x4 >>> 8 & 255;
185
- o[18] = x4 >>> 16 & 255;
186
- o[19] = x4 >>> 24 & 255;
187
- o[20] = x5 >>> 0 & 255;
188
- o[21] = x5 >>> 8 & 255;
189
- o[22] = x5 >>> 16 & 255;
190
- o[23] = x5 >>> 24 & 255;
191
- o[24] = x6 >>> 0 & 255;
192
- o[25] = x6 >>> 8 & 255;
193
- o[26] = x6 >>> 16 & 255;
194
- o[27] = x6 >>> 24 & 255;
195
- o[28] = x7 >>> 0 & 255;
196
- o[29] = x7 >>> 8 & 255;
197
- o[30] = x7 >>> 16 & 255;
198
- o[31] = x7 >>> 24 & 255;
199
- o[32] = x8 >>> 0 & 255;
200
- o[33] = x8 >>> 8 & 255;
201
- o[34] = x8 >>> 16 & 255;
202
- o[35] = x8 >>> 24 & 255;
203
- o[36] = x9 >>> 0 & 255;
204
- o[37] = x9 >>> 8 & 255;
205
- o[38] = x9 >>> 16 & 255;
206
- o[39] = x9 >>> 24 & 255;
207
- o[40] = x10 >>> 0 & 255;
208
- o[41] = x10 >>> 8 & 255;
209
- o[42] = x10 >>> 16 & 255;
210
- o[43] = x10 >>> 24 & 255;
211
- o[44] = x11 >>> 0 & 255;
212
- o[45] = x11 >>> 8 & 255;
213
- o[46] = x11 >>> 16 & 255;
214
- o[47] = x11 >>> 24 & 255;
215
- o[48] = x12 >>> 0 & 255;
216
- o[49] = x12 >>> 8 & 255;
217
- o[50] = x12 >>> 16 & 255;
218
- o[51] = x12 >>> 24 & 255;
219
- o[52] = x13 >>> 0 & 255;
220
- o[53] = x13 >>> 8 & 255;
221
- o[54] = x13 >>> 16 & 255;
222
- o[55] = x13 >>> 24 & 255;
223
- o[56] = x14 >>> 0 & 255;
224
- o[57] = x14 >>> 8 & 255;
225
- o[58] = x14 >>> 16 & 255;
226
- o[59] = x14 >>> 24 & 255;
227
- o[60] = x15 >>> 0 & 255;
228
- o[61] = x15 >>> 8 & 255;
229
- o[62] = x15 >>> 16 & 255;
230
- o[63] = x15 >>> 24 & 255;
231
- }
232
- function core_hsalsa20(o, p, k, c) {
233
- 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;
234
- 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;
235
- for (var i = 0; i < 20; i += 2) {
236
- u = x0 + x12 | 0;
237
- x4 ^= u << 7 | u >>> 32 - 7;
238
- u = x4 + x0 | 0;
239
- x8 ^= u << 9 | u >>> 32 - 9;
240
- u = x8 + x4 | 0;
241
- x12 ^= u << 13 | u >>> 32 - 13;
242
- u = x12 + x8 | 0;
243
- x0 ^= u << 18 | u >>> 32 - 18;
244
- u = x5 + x1 | 0;
245
- x9 ^= u << 7 | u >>> 32 - 7;
246
- u = x9 + x5 | 0;
247
- x13 ^= u << 9 | u >>> 32 - 9;
248
- u = x13 + x9 | 0;
249
- x1 ^= u << 13 | u >>> 32 - 13;
250
- u = x1 + x13 | 0;
251
- x5 ^= u << 18 | u >>> 32 - 18;
252
- u = x10 + x6 | 0;
253
- x14 ^= u << 7 | u >>> 32 - 7;
254
- u = x14 + x10 | 0;
255
- x2 ^= u << 9 | u >>> 32 - 9;
256
- u = x2 + x14 | 0;
257
- x6 ^= u << 13 | u >>> 32 - 13;
258
- u = x6 + x2 | 0;
259
- x10 ^= u << 18 | u >>> 32 - 18;
260
- u = x15 + x11 | 0;
261
- x3 ^= u << 7 | u >>> 32 - 7;
262
- u = x3 + x15 | 0;
263
- x7 ^= u << 9 | u >>> 32 - 9;
264
- u = x7 + x3 | 0;
265
- x11 ^= u << 13 | u >>> 32 - 13;
266
- u = x11 + x7 | 0;
267
- x15 ^= u << 18 | u >>> 32 - 18;
268
- u = x0 + x3 | 0;
269
- x1 ^= u << 7 | u >>> 32 - 7;
270
- u = x1 + x0 | 0;
271
- x2 ^= u << 9 | u >>> 32 - 9;
272
- u = x2 + x1 | 0;
273
- x3 ^= u << 13 | u >>> 32 - 13;
274
- u = x3 + x2 | 0;
275
- x0 ^= u << 18 | u >>> 32 - 18;
276
- u = x5 + x4 | 0;
277
- x6 ^= u << 7 | u >>> 32 - 7;
278
- u = x6 + x5 | 0;
279
- x7 ^= u << 9 | u >>> 32 - 9;
280
- u = x7 + x6 | 0;
281
- x4 ^= u << 13 | u >>> 32 - 13;
282
- u = x4 + x7 | 0;
283
- x5 ^= u << 18 | u >>> 32 - 18;
284
- u = x10 + x9 | 0;
285
- x11 ^= u << 7 | u >>> 32 - 7;
286
- u = x11 + x10 | 0;
287
- x8 ^= u << 9 | u >>> 32 - 9;
288
- u = x8 + x11 | 0;
289
- x9 ^= u << 13 | u >>> 32 - 13;
290
- u = x9 + x8 | 0;
291
- x10 ^= u << 18 | u >>> 32 - 18;
292
- u = x15 + x14 | 0;
293
- x12 ^= u << 7 | u >>> 32 - 7;
294
- u = x12 + x15 | 0;
295
- x13 ^= u << 9 | u >>> 32 - 9;
296
- u = x13 + x12 | 0;
297
- x14 ^= u << 13 | u >>> 32 - 13;
298
- u = x14 + x13 | 0;
299
- x15 ^= u << 18 | u >>> 32 - 18;
300
- }
301
- o[0] = x0 >>> 0 & 255;
302
- o[1] = x0 >>> 8 & 255;
303
- o[2] = x0 >>> 16 & 255;
304
- o[3] = x0 >>> 24 & 255;
305
- o[4] = x5 >>> 0 & 255;
306
- o[5] = x5 >>> 8 & 255;
307
- o[6] = x5 >>> 16 & 255;
308
- o[7] = x5 >>> 24 & 255;
309
- o[8] = x10 >>> 0 & 255;
310
- o[9] = x10 >>> 8 & 255;
311
- o[10] = x10 >>> 16 & 255;
312
- o[11] = x10 >>> 24 & 255;
313
- o[12] = x15 >>> 0 & 255;
314
- o[13] = x15 >>> 8 & 255;
315
- o[14] = x15 >>> 16 & 255;
316
- o[15] = x15 >>> 24 & 255;
317
- o[16] = x6 >>> 0 & 255;
318
- o[17] = x6 >>> 8 & 255;
319
- o[18] = x6 >>> 16 & 255;
320
- o[19] = x6 >>> 24 & 255;
321
- o[20] = x7 >>> 0 & 255;
322
- o[21] = x7 >>> 8 & 255;
323
- o[22] = x7 >>> 16 & 255;
324
- o[23] = x7 >>> 24 & 255;
325
- o[24] = x8 >>> 0 & 255;
326
- o[25] = x8 >>> 8 & 255;
327
- o[26] = x8 >>> 16 & 255;
328
- o[27] = x8 >>> 24 & 255;
329
- o[28] = x9 >>> 0 & 255;
330
- o[29] = x9 >>> 8 & 255;
331
- o[30] = x9 >>> 16 & 255;
332
- o[31] = x9 >>> 24 & 255;
333
- }
334
- function crypto_core_salsa20(out, inp, k, c) {
335
- core_salsa20(out, inp, k, c);
336
- }
337
- function crypto_core_hsalsa20(out, inp, k, c) {
338
- core_hsalsa20(out, inp, k, c);
339
- }
340
- var sigma = new Uint8Array([101, 120, 112, 97, 110, 100, 32, 51, 50, 45, 98, 121, 116, 101, 32, 107]);
341
- function crypto_stream_salsa20_xor(c, cpos, m, mpos, b, n, k) {
342
- var z = new Uint8Array(16), x = new Uint8Array(64);
343
- var u, i;
344
- for (i = 0; i < 16; i++) z[i] = 0;
345
- for (i = 0; i < 8; i++) z[i] = n[i];
346
- while (b >= 64) {
347
- crypto_core_salsa20(x, z, k, sigma);
348
- for (i = 0; i < 64; i++) c[cpos + i] = m[mpos + i] ^ x[i];
349
- u = 1;
350
- for (i = 8; i < 16; i++) {
351
- u = u + (z[i] & 255) | 0;
352
- z[i] = u & 255;
353
- u >>>= 8;
354
- }
355
- b -= 64;
356
- cpos += 64;
357
- mpos += 64;
358
- }
359
- if (b > 0) {
360
- crypto_core_salsa20(x, z, k, sigma);
361
- for (i = 0; i < b; i++) c[cpos + i] = m[mpos + i] ^ x[i];
362
- }
363
- return 0;
364
- }
365
- function crypto_stream_salsa20(c, cpos, b, n, k) {
366
- var z = new Uint8Array(16), x = new Uint8Array(64);
367
- var u, i;
368
- for (i = 0; i < 16; i++) z[i] = 0;
369
- for (i = 0; i < 8; i++) z[i] = n[i];
370
- while (b >= 64) {
371
- crypto_core_salsa20(x, z, k, sigma);
372
- for (i = 0; i < 64; i++) c[cpos + i] = x[i];
373
- u = 1;
374
- for (i = 8; i < 16; i++) {
375
- u = u + (z[i] & 255) | 0;
376
- z[i] = u & 255;
377
- u >>>= 8;
378
- }
379
- b -= 64;
380
- cpos += 64;
381
- }
382
- if (b > 0) {
383
- crypto_core_salsa20(x, z, k, sigma);
384
- for (i = 0; i < b; i++) c[cpos + i] = x[i];
385
- }
386
- return 0;
387
- }
388
- function crypto_stream(c, cpos, d, n, k) {
389
- var s = new Uint8Array(32);
390
- crypto_core_hsalsa20(s, n, k, sigma);
391
- var sn = new Uint8Array(8);
392
- for (var i = 0; i < 8; i++) sn[i] = n[i + 16];
393
- return crypto_stream_salsa20(c, cpos, d, sn, s);
394
- }
395
- function crypto_stream_xor(c, cpos, m, mpos, d, n, k) {
396
- var s = new Uint8Array(32);
397
- crypto_core_hsalsa20(s, n, k, sigma);
398
- var sn = new Uint8Array(8);
399
- for (var i = 0; i < 8; i++) sn[i] = n[i + 16];
400
- return crypto_stream_salsa20_xor(c, cpos, m, mpos, d, sn, s);
401
- }
402
- var poly1305 = function(key) {
403
- this.buffer = new Uint8Array(16);
404
- this.r = new Uint16Array(10);
405
- this.h = new Uint16Array(10);
406
- this.pad = new Uint16Array(8);
407
- this.leftover = 0;
408
- this.fin = 0;
409
- var t0, t1, t2, t3, t4, t5, t6, t7;
410
- t0 = key[0] & 255 | (key[1] & 255) << 8;
411
- this.r[0] = t0 & 8191;
412
- t1 = key[2] & 255 | (key[3] & 255) << 8;
413
- this.r[1] = (t0 >>> 13 | t1 << 3) & 8191;
414
- t2 = key[4] & 255 | (key[5] & 255) << 8;
415
- this.r[2] = (t1 >>> 10 | t2 << 6) & 7939;
416
- t3 = key[6] & 255 | (key[7] & 255) << 8;
417
- this.r[3] = (t2 >>> 7 | t3 << 9) & 8191;
418
- t4 = key[8] & 255 | (key[9] & 255) << 8;
419
- this.r[4] = (t3 >>> 4 | t4 << 12) & 255;
420
- this.r[5] = t4 >>> 1 & 8190;
421
- t5 = key[10] & 255 | (key[11] & 255) << 8;
422
- this.r[6] = (t4 >>> 14 | t5 << 2) & 8191;
423
- t6 = key[12] & 255 | (key[13] & 255) << 8;
424
- this.r[7] = (t5 >>> 11 | t6 << 5) & 8065;
425
- t7 = key[14] & 255 | (key[15] & 255) << 8;
426
- this.r[8] = (t6 >>> 8 | t7 << 8) & 8191;
427
- this.r[9] = t7 >>> 5 & 127;
428
- this.pad[0] = key[16] & 255 | (key[17] & 255) << 8;
429
- this.pad[1] = key[18] & 255 | (key[19] & 255) << 8;
430
- this.pad[2] = key[20] & 255 | (key[21] & 255) << 8;
431
- this.pad[3] = key[22] & 255 | (key[23] & 255) << 8;
432
- this.pad[4] = key[24] & 255 | (key[25] & 255) << 8;
433
- this.pad[5] = key[26] & 255 | (key[27] & 255) << 8;
434
- this.pad[6] = key[28] & 255 | (key[29] & 255) << 8;
435
- this.pad[7] = key[30] & 255 | (key[31] & 255) << 8;
436
- };
437
- poly1305.prototype.blocks = function(m, mpos, bytes) {
438
- var hibit = this.fin ? 0 : 1 << 11;
439
- var t0, t1, t2, t3, t4, t5, t6, t7, c;
440
- var d0, d1, d2, d3, d4, d5, d6, d7, d8, d9;
441
- 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];
442
- 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];
443
- while (bytes >= 16) {
444
- t0 = m[mpos + 0] & 255 | (m[mpos + 1] & 255) << 8;
445
- h0 += t0 & 8191;
446
- t1 = m[mpos + 2] & 255 | (m[mpos + 3] & 255) << 8;
447
- h1 += (t0 >>> 13 | t1 << 3) & 8191;
448
- t2 = m[mpos + 4] & 255 | (m[mpos + 5] & 255) << 8;
449
- h2 += (t1 >>> 10 | t2 << 6) & 8191;
450
- t3 = m[mpos + 6] & 255 | (m[mpos + 7] & 255) << 8;
451
- h3 += (t2 >>> 7 | t3 << 9) & 8191;
452
- t4 = m[mpos + 8] & 255 | (m[mpos + 9] & 255) << 8;
453
- h4 += (t3 >>> 4 | t4 << 12) & 8191;
454
- h5 += t4 >>> 1 & 8191;
455
- t5 = m[mpos + 10] & 255 | (m[mpos + 11] & 255) << 8;
456
- h6 += (t4 >>> 14 | t5 << 2) & 8191;
457
- t6 = m[mpos + 12] & 255 | (m[mpos + 13] & 255) << 8;
458
- h7 += (t5 >>> 11 | t6 << 5) & 8191;
459
- t7 = m[mpos + 14] & 255 | (m[mpos + 15] & 255) << 8;
460
- h8 += (t6 >>> 8 | t7 << 8) & 8191;
461
- h9 += t7 >>> 5 | hibit;
462
- c = 0;
463
- d0 = c;
464
- d0 += h0 * r0;
465
- d0 += h1 * (5 * r9);
466
- d0 += h2 * (5 * r8);
467
- d0 += h3 * (5 * r7);
468
- d0 += h4 * (5 * r6);
469
- c = d0 >>> 13;
470
- d0 &= 8191;
471
- d0 += h5 * (5 * r5);
472
- d0 += h6 * (5 * r4);
473
- d0 += h7 * (5 * r3);
474
- d0 += h8 * (5 * r2);
475
- d0 += h9 * (5 * r1);
476
- c += d0 >>> 13;
477
- d0 &= 8191;
478
- d1 = c;
479
- d1 += h0 * r1;
480
- d1 += h1 * r0;
481
- d1 += h2 * (5 * r9);
482
- d1 += h3 * (5 * r8);
483
- d1 += h4 * (5 * r7);
484
- c = d1 >>> 13;
485
- d1 &= 8191;
486
- d1 += h5 * (5 * r6);
487
- d1 += h6 * (5 * r5);
488
- d1 += h7 * (5 * r4);
489
- d1 += h8 * (5 * r3);
490
- d1 += h9 * (5 * r2);
491
- c += d1 >>> 13;
492
- d1 &= 8191;
493
- d2 = c;
494
- d2 += h0 * r2;
495
- d2 += h1 * r1;
496
- d2 += h2 * r0;
497
- d2 += h3 * (5 * r9);
498
- d2 += h4 * (5 * r8);
499
- c = d2 >>> 13;
500
- d2 &= 8191;
501
- d2 += h5 * (5 * r7);
502
- d2 += h6 * (5 * r6);
503
- d2 += h7 * (5 * r5);
504
- d2 += h8 * (5 * r4);
505
- d2 += h9 * (5 * r3);
506
- c += d2 >>> 13;
507
- d2 &= 8191;
508
- d3 = c;
509
- d3 += h0 * r3;
510
- d3 += h1 * r2;
511
- d3 += h2 * r1;
512
- d3 += h3 * r0;
513
- d3 += h4 * (5 * r9);
514
- c = d3 >>> 13;
515
- d3 &= 8191;
516
- d3 += h5 * (5 * r8);
517
- d3 += h6 * (5 * r7);
518
- d3 += h7 * (5 * r6);
519
- d3 += h8 * (5 * r5);
520
- d3 += h9 * (5 * r4);
521
- c += d3 >>> 13;
522
- d3 &= 8191;
523
- d4 = c;
524
- d4 += h0 * r4;
525
- d4 += h1 * r3;
526
- d4 += h2 * r2;
527
- d4 += h3 * r1;
528
- d4 += h4 * r0;
529
- c = d4 >>> 13;
530
- d4 &= 8191;
531
- d4 += h5 * (5 * r9);
532
- d4 += h6 * (5 * r8);
533
- d4 += h7 * (5 * r7);
534
- d4 += h8 * (5 * r6);
535
- d4 += h9 * (5 * r5);
536
- c += d4 >>> 13;
537
- d4 &= 8191;
538
- d5 = c;
539
- d5 += h0 * r5;
540
- d5 += h1 * r4;
541
- d5 += h2 * r3;
542
- d5 += h3 * r2;
543
- d5 += h4 * r1;
544
- c = d5 >>> 13;
545
- d5 &= 8191;
546
- d5 += h5 * r0;
547
- d5 += h6 * (5 * r9);
548
- d5 += h7 * (5 * r8);
549
- d5 += h8 * (5 * r7);
550
- d5 += h9 * (5 * r6);
551
- c += d5 >>> 13;
552
- d5 &= 8191;
553
- d6 = c;
554
- d6 += h0 * r6;
555
- d6 += h1 * r5;
556
- d6 += h2 * r4;
557
- d6 += h3 * r3;
558
- d6 += h4 * r2;
559
- c = d6 >>> 13;
560
- d6 &= 8191;
561
- d6 += h5 * r1;
562
- d6 += h6 * r0;
563
- d6 += h7 * (5 * r9);
564
- d6 += h8 * (5 * r8);
565
- d6 += h9 * (5 * r7);
566
- c += d6 >>> 13;
567
- d6 &= 8191;
568
- d7 = c;
569
- d7 += h0 * r7;
570
- d7 += h1 * r6;
571
- d7 += h2 * r5;
572
- d7 += h3 * r4;
573
- d7 += h4 * r3;
574
- c = d7 >>> 13;
575
- d7 &= 8191;
576
- d7 += h5 * r2;
577
- d7 += h6 * r1;
578
- d7 += h7 * r0;
579
- d7 += h8 * (5 * r9);
580
- d7 += h9 * (5 * r8);
581
- c += d7 >>> 13;
582
- d7 &= 8191;
583
- d8 = c;
584
- d8 += h0 * r8;
585
- d8 += h1 * r7;
586
- d8 += h2 * r6;
587
- d8 += h3 * r5;
588
- d8 += h4 * r4;
589
- c = d8 >>> 13;
590
- d8 &= 8191;
591
- d8 += h5 * r3;
592
- d8 += h6 * r2;
593
- d8 += h7 * r1;
594
- d8 += h8 * r0;
595
- d8 += h9 * (5 * r9);
596
- c += d8 >>> 13;
597
- d8 &= 8191;
598
- d9 = c;
599
- d9 += h0 * r9;
600
- d9 += h1 * r8;
601
- d9 += h2 * r7;
602
- d9 += h3 * r6;
603
- d9 += h4 * r5;
604
- c = d9 >>> 13;
605
- d9 &= 8191;
606
- d9 += h5 * r4;
607
- d9 += h6 * r3;
608
- d9 += h7 * r2;
609
- d9 += h8 * r1;
610
- d9 += h9 * r0;
611
- c += d9 >>> 13;
612
- d9 &= 8191;
613
- c = (c << 2) + c | 0;
614
- c = c + d0 | 0;
615
- d0 = c & 8191;
616
- c = c >>> 13;
617
- d1 += c;
618
- h0 = d0;
619
- h1 = d1;
620
- h2 = d2;
621
- h3 = d3;
622
- h4 = d4;
623
- h5 = d5;
624
- h6 = d6;
625
- h7 = d7;
626
- h8 = d8;
627
- h9 = d9;
628
- mpos += 16;
629
- bytes -= 16;
630
- }
631
- this.h[0] = h0;
632
- this.h[1] = h1;
633
- this.h[2] = h2;
634
- this.h[3] = h3;
635
- this.h[4] = h4;
636
- this.h[5] = h5;
637
- this.h[6] = h6;
638
- this.h[7] = h7;
639
- this.h[8] = h8;
640
- this.h[9] = h9;
641
- };
642
- poly1305.prototype.finish = function(mac, macpos) {
643
- var g = new Uint16Array(10);
644
- var c, mask, f, i;
645
- if (this.leftover) {
646
- i = this.leftover;
647
- this.buffer[i++] = 1;
648
- for (; i < 16; i++) this.buffer[i] = 0;
649
- this.fin = 1;
650
- this.blocks(this.buffer, 0, 16);
651
- }
652
- c = this.h[1] >>> 13;
653
- this.h[1] &= 8191;
654
- for (i = 2; i < 10; i++) {
655
- this.h[i] += c;
656
- c = this.h[i] >>> 13;
657
- this.h[i] &= 8191;
658
- }
659
- this.h[0] += c * 5;
660
- c = this.h[0] >>> 13;
661
- this.h[0] &= 8191;
662
- this.h[1] += c;
663
- c = this.h[1] >>> 13;
664
- this.h[1] &= 8191;
665
- this.h[2] += c;
666
- g[0] = this.h[0] + 5;
667
- c = g[0] >>> 13;
668
- g[0] &= 8191;
669
- for (i = 1; i < 10; i++) {
670
- g[i] = this.h[i] + c;
671
- c = g[i] >>> 13;
672
- g[i] &= 8191;
673
- }
674
- g[9] -= 1 << 13;
675
- mask = (c ^ 1) - 1;
676
- for (i = 0; i < 10; i++) g[i] &= mask;
677
- mask = ~mask;
678
- for (i = 0; i < 10; i++) this.h[i] = this.h[i] & mask | g[i];
679
- this.h[0] = (this.h[0] | this.h[1] << 13) & 65535;
680
- this.h[1] = (this.h[1] >>> 3 | this.h[2] << 10) & 65535;
681
- this.h[2] = (this.h[2] >>> 6 | this.h[3] << 7) & 65535;
682
- this.h[3] = (this.h[3] >>> 9 | this.h[4] << 4) & 65535;
683
- this.h[4] = (this.h[4] >>> 12 | this.h[5] << 1 | this.h[6] << 14) & 65535;
684
- this.h[5] = (this.h[6] >>> 2 | this.h[7] << 11) & 65535;
685
- this.h[6] = (this.h[7] >>> 5 | this.h[8] << 8) & 65535;
686
- this.h[7] = (this.h[8] >>> 8 | this.h[9] << 5) & 65535;
687
- f = this.h[0] + this.pad[0];
688
- this.h[0] = f & 65535;
689
- for (i = 1; i < 8; i++) {
690
- f = (this.h[i] + this.pad[i] | 0) + (f >>> 16) | 0;
691
- this.h[i] = f & 65535;
692
- }
693
- mac[macpos + 0] = this.h[0] >>> 0 & 255;
694
- mac[macpos + 1] = this.h[0] >>> 8 & 255;
695
- mac[macpos + 2] = this.h[1] >>> 0 & 255;
696
- mac[macpos + 3] = this.h[1] >>> 8 & 255;
697
- mac[macpos + 4] = this.h[2] >>> 0 & 255;
698
- mac[macpos + 5] = this.h[2] >>> 8 & 255;
699
- mac[macpos + 6] = this.h[3] >>> 0 & 255;
700
- mac[macpos + 7] = this.h[3] >>> 8 & 255;
701
- mac[macpos + 8] = this.h[4] >>> 0 & 255;
702
- mac[macpos + 9] = this.h[4] >>> 8 & 255;
703
- mac[macpos + 10] = this.h[5] >>> 0 & 255;
704
- mac[macpos + 11] = this.h[5] >>> 8 & 255;
705
- mac[macpos + 12] = this.h[6] >>> 0 & 255;
706
- mac[macpos + 13] = this.h[6] >>> 8 & 255;
707
- mac[macpos + 14] = this.h[7] >>> 0 & 255;
708
- mac[macpos + 15] = this.h[7] >>> 8 & 255;
709
- };
710
- poly1305.prototype.update = function(m, mpos, bytes) {
711
- var i, want;
712
- if (this.leftover) {
713
- want = 16 - this.leftover;
714
- if (want > bytes)
715
- want = bytes;
716
- for (i = 0; i < want; i++)
717
- this.buffer[this.leftover + i] = m[mpos + i];
718
- bytes -= want;
719
- mpos += want;
720
- this.leftover += want;
721
- if (this.leftover < 16)
722
- return;
723
- this.blocks(this.buffer, 0, 16);
724
- this.leftover = 0;
725
- }
726
- if (bytes >= 16) {
727
- want = bytes - bytes % 16;
728
- this.blocks(m, mpos, want);
729
- mpos += want;
730
- bytes -= want;
731
- }
732
- if (bytes) {
733
- for (i = 0; i < bytes; i++)
734
- this.buffer[this.leftover + i] = m[mpos + i];
735
- this.leftover += bytes;
736
- }
737
- };
738
- function crypto_onetimeauth(out, outpos, m, mpos, n, k) {
739
- var s = new poly1305(k);
740
- s.update(m, mpos, n);
741
- s.finish(out, outpos);
742
- return 0;
743
- }
744
- function crypto_onetimeauth_verify(h, hpos, m, mpos, n, k) {
745
- var x = new Uint8Array(16);
746
- crypto_onetimeauth(x, 0, m, mpos, n, k);
747
- return crypto_verify_16(h, hpos, x, 0);
748
- }
749
- function crypto_secretbox(c, m, d, n, k) {
750
- var i;
751
- if (d < 32) return -1;
752
- crypto_stream_xor(c, 0, m, 0, d, n, k);
753
- crypto_onetimeauth(c, 16, c, 32, d - 32, c);
754
- for (i = 0; i < 16; i++) c[i] = 0;
755
- return 0;
756
- }
757
- function crypto_secretbox_open(m, c, d, n, k) {
758
- var i;
759
- var x = new Uint8Array(32);
760
- if (d < 32) return -1;
761
- crypto_stream(x, 0, 32, n, k);
762
- if (crypto_onetimeauth_verify(c, 16, c, 32, d - 32, x) !== 0) return -1;
763
- crypto_stream_xor(m, 0, c, 0, d, n, k);
764
- for (i = 0; i < 32; i++) m[i] = 0;
765
- return 0;
766
- }
767
- function set25519(r, a) {
768
- var i;
769
- for (i = 0; i < 16; i++) r[i] = a[i] | 0;
770
- }
771
- function car25519(o) {
772
- var i, v, c = 1;
773
- for (i = 0; i < 16; i++) {
774
- v = o[i] + c + 65535;
775
- c = Math.floor(v / 65536);
776
- o[i] = v - c * 65536;
777
- }
778
- o[0] += c - 1 + 37 * (c - 1);
779
- }
780
- function sel25519(p, q, b) {
781
- var t, c = ~(b - 1);
782
- for (var i = 0; i < 16; i++) {
783
- t = c & (p[i] ^ q[i]);
784
- p[i] ^= t;
785
- q[i] ^= t;
786
- }
787
- }
788
- function pack25519(o, n) {
789
- var i, j, b;
790
- var m = gf(), t = gf();
791
- for (i = 0; i < 16; i++) t[i] = n[i];
792
- car25519(t);
793
- car25519(t);
794
- car25519(t);
795
- for (j = 0; j < 2; j++) {
796
- m[0] = t[0] - 65517;
797
- for (i = 1; i < 15; i++) {
798
- m[i] = t[i] - 65535 - (m[i - 1] >> 16 & 1);
799
- m[i - 1] &= 65535;
800
- }
801
- m[15] = t[15] - 32767 - (m[14] >> 16 & 1);
802
- b = m[15] >> 16 & 1;
803
- m[14] &= 65535;
804
- sel25519(t, m, 1 - b);
805
- }
806
- for (i = 0; i < 16; i++) {
807
- o[2 * i] = t[i] & 255;
808
- o[2 * i + 1] = t[i] >> 8;
809
- }
810
- }
811
- function neq25519(a, b) {
812
- var c = new Uint8Array(32), d = new Uint8Array(32);
813
- pack25519(c, a);
814
- pack25519(d, b);
815
- return crypto_verify_32(c, 0, d, 0);
816
- }
817
- function par25519(a) {
818
- var d = new Uint8Array(32);
819
- pack25519(d, a);
820
- return d[0] & 1;
821
- }
822
- function unpack25519(o, n) {
823
- var i;
824
- for (i = 0; i < 16; i++) o[i] = n[2 * i] + (n[2 * i + 1] << 8);
825
- o[15] &= 32767;
826
- }
827
- function A(o, a, b) {
828
- for (var i = 0; i < 16; i++) o[i] = a[i] + b[i];
829
- }
830
- function Z(o, a, b) {
831
- for (var i = 0; i < 16; i++) o[i] = a[i] - b[i];
832
- }
833
- function M(o, a, b) {
834
- 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];
835
- v = a[0];
836
- t0 += v * b0;
837
- t1 += v * b1;
838
- t2 += v * b2;
839
- t3 += v * b3;
840
- t4 += v * b4;
841
- t5 += v * b5;
842
- t6 += v * b6;
843
- t7 += v * b7;
844
- t8 += v * b8;
845
- t9 += v * b9;
846
- t10 += v * b10;
847
- t11 += v * b11;
848
- t12 += v * b12;
849
- t13 += v * b13;
850
- t14 += v * b14;
851
- t15 += v * b15;
852
- v = a[1];
853
- t1 += v * b0;
854
- t2 += v * b1;
855
- t3 += v * b2;
856
- t4 += v * b3;
857
- t5 += v * b4;
858
- t6 += v * b5;
859
- t7 += v * b6;
860
- t8 += v * b7;
861
- t9 += v * b8;
862
- t10 += v * b9;
863
- t11 += v * b10;
864
- t12 += v * b11;
865
- t13 += v * b12;
866
- t14 += v * b13;
867
- t15 += v * b14;
868
- t16 += v * b15;
869
- v = a[2];
870
- t2 += v * b0;
871
- t3 += v * b1;
872
- t4 += v * b2;
873
- t5 += v * b3;
874
- t6 += v * b4;
875
- t7 += v * b5;
876
- t8 += v * b6;
877
- t9 += v * b7;
878
- t10 += v * b8;
879
- t11 += v * b9;
880
- t12 += v * b10;
881
- t13 += v * b11;
882
- t14 += v * b12;
883
- t15 += v * b13;
884
- t16 += v * b14;
885
- t17 += v * b15;
886
- v = a[3];
887
- t3 += v * b0;
888
- t4 += v * b1;
889
- t5 += v * b2;
890
- t6 += v * b3;
891
- t7 += v * b4;
892
- t8 += v * b5;
893
- t9 += v * b6;
894
- t10 += v * b7;
895
- t11 += v * b8;
896
- t12 += v * b9;
897
- t13 += v * b10;
898
- t14 += v * b11;
899
- t15 += v * b12;
900
- t16 += v * b13;
901
- t17 += v * b14;
902
- t18 += v * b15;
903
- v = a[4];
904
- t4 += v * b0;
905
- t5 += v * b1;
906
- t6 += v * b2;
907
- t7 += v * b3;
908
- t8 += v * b4;
909
- t9 += v * b5;
910
- t10 += v * b6;
911
- t11 += v * b7;
912
- t12 += v * b8;
913
- t13 += v * b9;
914
- t14 += v * b10;
915
- t15 += v * b11;
916
- t16 += v * b12;
917
- t17 += v * b13;
918
- t18 += v * b14;
919
- t19 += v * b15;
920
- v = a[5];
921
- t5 += v * b0;
922
- t6 += v * b1;
923
- t7 += v * b2;
924
- t8 += v * b3;
925
- t9 += v * b4;
926
- t10 += v * b5;
927
- t11 += v * b6;
928
- t12 += v * b7;
929
- t13 += v * b8;
930
- t14 += v * b9;
931
- t15 += v * b10;
932
- t16 += v * b11;
933
- t17 += v * b12;
934
- t18 += v * b13;
935
- t19 += v * b14;
936
- t20 += v * b15;
937
- v = a[6];
938
- t6 += v * b0;
939
- t7 += v * b1;
940
- t8 += v * b2;
941
- t9 += v * b3;
942
- t10 += v * b4;
943
- t11 += v * b5;
944
- t12 += v * b6;
945
- t13 += v * b7;
946
- t14 += v * b8;
947
- t15 += v * b9;
948
- t16 += v * b10;
949
- t17 += v * b11;
950
- t18 += v * b12;
951
- t19 += v * b13;
952
- t20 += v * b14;
953
- t21 += v * b15;
954
- v = a[7];
955
- t7 += v * b0;
956
- t8 += v * b1;
957
- t9 += v * b2;
958
- t10 += v * b3;
959
- t11 += v * b4;
960
- t12 += v * b5;
961
- t13 += v * b6;
962
- t14 += v * b7;
963
- t15 += v * b8;
964
- t16 += v * b9;
965
- t17 += v * b10;
966
- t18 += v * b11;
967
- t19 += v * b12;
968
- t20 += v * b13;
969
- t21 += v * b14;
970
- t22 += v * b15;
971
- v = a[8];
972
- t8 += v * b0;
973
- t9 += v * b1;
974
- t10 += v * b2;
975
- t11 += v * b3;
976
- t12 += v * b4;
977
- t13 += v * b5;
978
- t14 += v * b6;
979
- t15 += v * b7;
980
- t16 += v * b8;
981
- t17 += v * b9;
982
- t18 += v * b10;
983
- t19 += v * b11;
984
- t20 += v * b12;
985
- t21 += v * b13;
986
- t22 += v * b14;
987
- t23 += v * b15;
988
- v = a[9];
989
- t9 += v * b0;
990
- t10 += v * b1;
991
- t11 += v * b2;
992
- t12 += v * b3;
993
- t13 += v * b4;
994
- t14 += v * b5;
995
- t15 += v * b6;
996
- t16 += v * b7;
997
- t17 += v * b8;
998
- t18 += v * b9;
999
- t19 += v * b10;
1000
- t20 += v * b11;
1001
- t21 += v * b12;
1002
- t22 += v * b13;
1003
- t23 += v * b14;
1004
- t24 += v * b15;
1005
- v = a[10];
1006
- t10 += v * b0;
1007
- t11 += v * b1;
1008
- t12 += v * b2;
1009
- t13 += v * b3;
1010
- t14 += v * b4;
1011
- t15 += v * b5;
1012
- t16 += v * b6;
1013
- t17 += v * b7;
1014
- t18 += v * b8;
1015
- t19 += v * b9;
1016
- t20 += v * b10;
1017
- t21 += v * b11;
1018
- t22 += v * b12;
1019
- t23 += v * b13;
1020
- t24 += v * b14;
1021
- t25 += v * b15;
1022
- v = a[11];
1023
- t11 += v * b0;
1024
- t12 += v * b1;
1025
- t13 += v * b2;
1026
- t14 += v * b3;
1027
- t15 += v * b4;
1028
- t16 += v * b5;
1029
- t17 += v * b6;
1030
- t18 += v * b7;
1031
- t19 += v * b8;
1032
- t20 += v * b9;
1033
- t21 += v * b10;
1034
- t22 += v * b11;
1035
- t23 += v * b12;
1036
- t24 += v * b13;
1037
- t25 += v * b14;
1038
- t26 += v * b15;
1039
- v = a[12];
1040
- t12 += v * b0;
1041
- t13 += v * b1;
1042
- t14 += v * b2;
1043
- t15 += v * b3;
1044
- t16 += v * b4;
1045
- t17 += v * b5;
1046
- t18 += v * b6;
1047
- t19 += v * b7;
1048
- t20 += v * b8;
1049
- t21 += v * b9;
1050
- t22 += v * b10;
1051
- t23 += v * b11;
1052
- t24 += v * b12;
1053
- t25 += v * b13;
1054
- t26 += v * b14;
1055
- t27 += v * b15;
1056
- v = a[13];
1057
- t13 += v * b0;
1058
- t14 += v * b1;
1059
- t15 += v * b2;
1060
- t16 += v * b3;
1061
- t17 += v * b4;
1062
- t18 += v * b5;
1063
- t19 += v * b6;
1064
- t20 += v * b7;
1065
- t21 += v * b8;
1066
- t22 += v * b9;
1067
- t23 += v * b10;
1068
- t24 += v * b11;
1069
- t25 += v * b12;
1070
- t26 += v * b13;
1071
- t27 += v * b14;
1072
- t28 += v * b15;
1073
- v = a[14];
1074
- t14 += v * b0;
1075
- t15 += v * b1;
1076
- t16 += v * b2;
1077
- t17 += v * b3;
1078
- t18 += v * b4;
1079
- t19 += v * b5;
1080
- t20 += v * b6;
1081
- t21 += v * b7;
1082
- t22 += v * b8;
1083
- t23 += v * b9;
1084
- t24 += v * b10;
1085
- t25 += v * b11;
1086
- t26 += v * b12;
1087
- t27 += v * b13;
1088
- t28 += v * b14;
1089
- t29 += v * b15;
1090
- v = a[15];
1091
- t15 += v * b0;
1092
- t16 += v * b1;
1093
- t17 += v * b2;
1094
- t18 += v * b3;
1095
- t19 += v * b4;
1096
- t20 += v * b5;
1097
- t21 += v * b6;
1098
- t22 += v * b7;
1099
- t23 += v * b8;
1100
- t24 += v * b9;
1101
- t25 += v * b10;
1102
- t26 += v * b11;
1103
- t27 += v * b12;
1104
- t28 += v * b13;
1105
- t29 += v * b14;
1106
- t30 += v * b15;
1107
- t0 += 38 * t16;
1108
- t1 += 38 * t17;
1109
- t2 += 38 * t18;
1110
- t3 += 38 * t19;
1111
- t4 += 38 * t20;
1112
- t5 += 38 * t21;
1113
- t6 += 38 * t22;
1114
- t7 += 38 * t23;
1115
- t8 += 38 * t24;
1116
- t9 += 38 * t25;
1117
- t10 += 38 * t26;
1118
- t11 += 38 * t27;
1119
- t12 += 38 * t28;
1120
- t13 += 38 * t29;
1121
- t14 += 38 * t30;
1122
- c = 1;
1123
- v = t0 + c + 65535;
1124
- c = Math.floor(v / 65536);
1125
- t0 = v - c * 65536;
1126
- v = t1 + c + 65535;
1127
- c = Math.floor(v / 65536);
1128
- t1 = v - c * 65536;
1129
- v = t2 + c + 65535;
1130
- c = Math.floor(v / 65536);
1131
- t2 = v - c * 65536;
1132
- v = t3 + c + 65535;
1133
- c = Math.floor(v / 65536);
1134
- t3 = v - c * 65536;
1135
- v = t4 + c + 65535;
1136
- c = Math.floor(v / 65536);
1137
- t4 = v - c * 65536;
1138
- v = t5 + c + 65535;
1139
- c = Math.floor(v / 65536);
1140
- t5 = v - c * 65536;
1141
- v = t6 + c + 65535;
1142
- c = Math.floor(v / 65536);
1143
- t6 = v - c * 65536;
1144
- v = t7 + c + 65535;
1145
- c = Math.floor(v / 65536);
1146
- t7 = v - c * 65536;
1147
- v = t8 + c + 65535;
1148
- c = Math.floor(v / 65536);
1149
- t8 = v - c * 65536;
1150
- v = t9 + c + 65535;
1151
- c = Math.floor(v / 65536);
1152
- t9 = v - c * 65536;
1153
- v = t10 + c + 65535;
1154
- c = Math.floor(v / 65536);
1155
- t10 = v - c * 65536;
1156
- v = t11 + c + 65535;
1157
- c = Math.floor(v / 65536);
1158
- t11 = v - c * 65536;
1159
- v = t12 + c + 65535;
1160
- c = Math.floor(v / 65536);
1161
- t12 = v - c * 65536;
1162
- v = t13 + c + 65535;
1163
- c = Math.floor(v / 65536);
1164
- t13 = v - c * 65536;
1165
- v = t14 + c + 65535;
1166
- c = Math.floor(v / 65536);
1167
- t14 = v - c * 65536;
1168
- v = t15 + c + 65535;
1169
- c = Math.floor(v / 65536);
1170
- t15 = v - c * 65536;
1171
- t0 += c - 1 + 37 * (c - 1);
1172
- c = 1;
1173
- v = t0 + c + 65535;
1174
- c = Math.floor(v / 65536);
1175
- t0 = v - c * 65536;
1176
- v = t1 + c + 65535;
1177
- c = Math.floor(v / 65536);
1178
- t1 = v - c * 65536;
1179
- v = t2 + c + 65535;
1180
- c = Math.floor(v / 65536);
1181
- t2 = v - c * 65536;
1182
- v = t3 + c + 65535;
1183
- c = Math.floor(v / 65536);
1184
- t3 = v - c * 65536;
1185
- v = t4 + c + 65535;
1186
- c = Math.floor(v / 65536);
1187
- t4 = v - c * 65536;
1188
- v = t5 + c + 65535;
1189
- c = Math.floor(v / 65536);
1190
- t5 = v - c * 65536;
1191
- v = t6 + c + 65535;
1192
- c = Math.floor(v / 65536);
1193
- t6 = v - c * 65536;
1194
- v = t7 + c + 65535;
1195
- c = Math.floor(v / 65536);
1196
- t7 = v - c * 65536;
1197
- v = t8 + c + 65535;
1198
- c = Math.floor(v / 65536);
1199
- t8 = v - c * 65536;
1200
- v = t9 + c + 65535;
1201
- c = Math.floor(v / 65536);
1202
- t9 = v - c * 65536;
1203
- v = t10 + c + 65535;
1204
- c = Math.floor(v / 65536);
1205
- t10 = v - c * 65536;
1206
- v = t11 + c + 65535;
1207
- c = Math.floor(v / 65536);
1208
- t11 = v - c * 65536;
1209
- v = t12 + c + 65535;
1210
- c = Math.floor(v / 65536);
1211
- t12 = v - c * 65536;
1212
- v = t13 + c + 65535;
1213
- c = Math.floor(v / 65536);
1214
- t13 = v - c * 65536;
1215
- v = t14 + c + 65535;
1216
- c = Math.floor(v / 65536);
1217
- t14 = v - c * 65536;
1218
- v = t15 + c + 65535;
1219
- c = Math.floor(v / 65536);
1220
- t15 = v - c * 65536;
1221
- t0 += c - 1 + 37 * (c - 1);
1222
- o[0] = t0;
1223
- o[1] = t1;
1224
- o[2] = t2;
1225
- o[3] = t3;
1226
- o[4] = t4;
1227
- o[5] = t5;
1228
- o[6] = t6;
1229
- o[7] = t7;
1230
- o[8] = t8;
1231
- o[9] = t9;
1232
- o[10] = t10;
1233
- o[11] = t11;
1234
- o[12] = t12;
1235
- o[13] = t13;
1236
- o[14] = t14;
1237
- o[15] = t15;
1238
- }
1239
- function S(o, a) {
1240
- M(o, a, a);
1241
- }
1242
- function inv25519(o, i) {
1243
- var c = gf();
1244
- var a;
1245
- for (a = 0; a < 16; a++) c[a] = i[a];
1246
- for (a = 253; a >= 0; a--) {
1247
- S(c, c);
1248
- if (a !== 2 && a !== 4) M(c, c, i);
1249
- }
1250
- for (a = 0; a < 16; a++) o[a] = c[a];
1251
- }
1252
- function pow2523(o, i) {
1253
- var c = gf();
1254
- var a;
1255
- for (a = 0; a < 16; a++) c[a] = i[a];
1256
- for (a = 250; a >= 0; a--) {
1257
- S(c, c);
1258
- if (a !== 1) M(c, c, i);
1259
- }
1260
- for (a = 0; a < 16; a++) o[a] = c[a];
1261
- }
1262
- function crypto_scalarmult(q, n, p) {
1263
- var z = new Uint8Array(32);
1264
- var x = new Float64Array(80), r, i;
1265
- var a = gf(), b = gf(), c = gf(), d = gf(), e = gf(), f = gf();
1266
- for (i = 0; i < 31; i++) z[i] = n[i];
1267
- z[31] = n[31] & 127 | 64;
1268
- z[0] &= 248;
1269
- unpack25519(x, p);
1270
- for (i = 0; i < 16; i++) {
1271
- b[i] = x[i];
1272
- d[i] = a[i] = c[i] = 0;
1273
- }
1274
- a[0] = d[0] = 1;
1275
- for (i = 254; i >= 0; --i) {
1276
- r = z[i >>> 3] >>> (i & 7) & 1;
1277
- sel25519(a, b, r);
1278
- sel25519(c, d, r);
1279
- A(e, a, c);
1280
- Z(a, a, c);
1281
- A(c, b, d);
1282
- Z(b, b, d);
1283
- S(d, e);
1284
- S(f, a);
1285
- M(a, c, a);
1286
- M(c, b, e);
1287
- A(e, a, c);
1288
- Z(a, a, c);
1289
- S(b, a);
1290
- Z(c, d, f);
1291
- M(a, c, _121665);
1292
- A(a, a, d);
1293
- M(c, c, a);
1294
- M(a, d, f);
1295
- M(d, b, x);
1296
- S(b, e);
1297
- sel25519(a, b, r);
1298
- sel25519(c, d, r);
1299
- }
1300
- for (i = 0; i < 16; i++) {
1301
- x[i + 16] = a[i];
1302
- x[i + 32] = c[i];
1303
- x[i + 48] = b[i];
1304
- x[i + 64] = d[i];
1305
- }
1306
- var x32 = x.subarray(32);
1307
- var x16 = x.subarray(16);
1308
- inv25519(x32, x32);
1309
- M(x16, x16, x32);
1310
- pack25519(q, x16);
1311
- return 0;
1312
- }
1313
- function crypto_scalarmult_base(q, n) {
1314
- return crypto_scalarmult(q, n, _9);
1315
- }
1316
- function crypto_box_keypair(y, x) {
1317
- randombytes(x, 32);
1318
- return crypto_scalarmult_base(y, x);
1319
- }
1320
- function crypto_box_beforenm(k, y, x) {
1321
- var s = new Uint8Array(32);
1322
- crypto_scalarmult(s, x, y);
1323
- return crypto_core_hsalsa20(k, _0, s, sigma);
1324
- }
1325
- var crypto_box_afternm = crypto_secretbox;
1326
- var crypto_box_open_afternm = crypto_secretbox_open;
1327
- function crypto_box(c, m, d, n, y, x) {
1328
- var k = new Uint8Array(32);
1329
- crypto_box_beforenm(k, y, x);
1330
- return crypto_box_afternm(c, m, d, n, k);
1331
- }
1332
- function crypto_box_open(m, c, d, n, y, x) {
1333
- var k = new Uint8Array(32);
1334
- crypto_box_beforenm(k, y, x);
1335
- return crypto_box_open_afternm(m, c, d, n, k);
1336
- }
1337
- var K = [
1338
- 1116352408,
1339
- 3609767458,
1340
- 1899447441,
1341
- 602891725,
1342
- 3049323471,
1343
- 3964484399,
1344
- 3921009573,
1345
- 2173295548,
1346
- 961987163,
1347
- 4081628472,
1348
- 1508970993,
1349
- 3053834265,
1350
- 2453635748,
1351
- 2937671579,
1352
- 2870763221,
1353
- 3664609560,
1354
- 3624381080,
1355
- 2734883394,
1356
- 310598401,
1357
- 1164996542,
1358
- 607225278,
1359
- 1323610764,
1360
- 1426881987,
1361
- 3590304994,
1362
- 1925078388,
1363
- 4068182383,
1364
- 2162078206,
1365
- 991336113,
1366
- 2614888103,
1367
- 633803317,
1368
- 3248222580,
1369
- 3479774868,
1370
- 3835390401,
1371
- 2666613458,
1372
- 4022224774,
1373
- 944711139,
1374
- 264347078,
1375
- 2341262773,
1376
- 604807628,
1377
- 2007800933,
1378
- 770255983,
1379
- 1495990901,
1380
- 1249150122,
1381
- 1856431235,
1382
- 1555081692,
1383
- 3175218132,
1384
- 1996064986,
1385
- 2198950837,
1386
- 2554220882,
1387
- 3999719339,
1388
- 2821834349,
1389
- 766784016,
1390
- 2952996808,
1391
- 2566594879,
1392
- 3210313671,
1393
- 3203337956,
1394
- 3336571891,
1395
- 1034457026,
1396
- 3584528711,
1397
- 2466948901,
1398
- 113926993,
1399
- 3758326383,
1400
- 338241895,
1401
- 168717936,
1402
- 666307205,
1403
- 1188179964,
1404
- 773529912,
1405
- 1546045734,
1406
- 1294757372,
1407
- 1522805485,
1408
- 1396182291,
1409
- 2643833823,
1410
- 1695183700,
1411
- 2343527390,
1412
- 1986661051,
1413
- 1014477480,
1414
- 2177026350,
1415
- 1206759142,
1416
- 2456956037,
1417
- 344077627,
1418
- 2730485921,
1419
- 1290863460,
1420
- 2820302411,
1421
- 3158454273,
1422
- 3259730800,
1423
- 3505952657,
1424
- 3345764771,
1425
- 106217008,
1426
- 3516065817,
1427
- 3606008344,
1428
- 3600352804,
1429
- 1432725776,
1430
- 4094571909,
1431
- 1467031594,
1432
- 275423344,
1433
- 851169720,
1434
- 430227734,
1435
- 3100823752,
1436
- 506948616,
1437
- 1363258195,
1438
- 659060556,
1439
- 3750685593,
1440
- 883997877,
1441
- 3785050280,
1442
- 958139571,
1443
- 3318307427,
1444
- 1322822218,
1445
- 3812723403,
1446
- 1537002063,
1447
- 2003034995,
1448
- 1747873779,
1449
- 3602036899,
1450
- 1955562222,
1451
- 1575990012,
1452
- 2024104815,
1453
- 1125592928,
1454
- 2227730452,
1455
- 2716904306,
1456
- 2361852424,
1457
- 442776044,
1458
- 2428436474,
1459
- 593698344,
1460
- 2756734187,
1461
- 3733110249,
1462
- 3204031479,
1463
- 2999351573,
1464
- 3329325298,
1465
- 3815920427,
1466
- 3391569614,
1467
- 3928383900,
1468
- 3515267271,
1469
- 566280711,
1470
- 3940187606,
1471
- 3454069534,
1472
- 4118630271,
1473
- 4000239992,
1474
- 116418474,
1475
- 1914138554,
1476
- 174292421,
1477
- 2731055270,
1478
- 289380356,
1479
- 3203993006,
1480
- 460393269,
1481
- 320620315,
1482
- 685471733,
1483
- 587496836,
1484
- 852142971,
1485
- 1086792851,
1486
- 1017036298,
1487
- 365543100,
1488
- 1126000580,
1489
- 2618297676,
1490
- 1288033470,
1491
- 3409855158,
1492
- 1501505948,
1493
- 4234509866,
1494
- 1607167915,
1495
- 987167468,
1496
- 1816402316,
1497
- 1246189591
1498
- ];
1499
- function crypto_hashblocks_hl(hh, hl, m, n) {
1500
- 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;
1501
- 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];
1502
- var pos = 0;
1503
- while (n >= 128) {
1504
- for (i = 0; i < 16; i++) {
1505
- j = 8 * i + pos;
1506
- wh[i] = m[j + 0] << 24 | m[j + 1] << 16 | m[j + 2] << 8 | m[j + 3];
1507
- wl[i] = m[j + 4] << 24 | m[j + 5] << 16 | m[j + 6] << 8 | m[j + 7];
1508
- }
1509
- for (i = 0; i < 80; i++) {
1510
- bh0 = ah0;
1511
- bh1 = ah1;
1512
- bh2 = ah2;
1513
- bh3 = ah3;
1514
- bh4 = ah4;
1515
- bh5 = ah5;
1516
- bh6 = ah6;
1517
- bh7 = ah7;
1518
- bl0 = al0;
1519
- bl1 = al1;
1520
- bl2 = al2;
1521
- bl3 = al3;
1522
- bl4 = al4;
1523
- bl5 = al5;
1524
- bl6 = al6;
1525
- bl7 = al7;
1526
- h = ah7;
1527
- l = al7;
1528
- a = l & 65535;
1529
- b = l >>> 16;
1530
- c = h & 65535;
1531
- d = h >>> 16;
1532
- h = (ah4 >>> 14 | al4 << 32 - 14) ^ (ah4 >>> 18 | al4 << 32 - 18) ^ (al4 >>> 41 - 32 | ah4 << 32 - (41 - 32));
1533
- l = (al4 >>> 14 | ah4 << 32 - 14) ^ (al4 >>> 18 | ah4 << 32 - 18) ^ (ah4 >>> 41 - 32 | al4 << 32 - (41 - 32));
1534
- a += l & 65535;
1535
- b += l >>> 16;
1536
- c += h & 65535;
1537
- d += h >>> 16;
1538
- h = ah4 & ah5 ^ ~ah4 & ah6;
1539
- l = al4 & al5 ^ ~al4 & al6;
1540
- a += l & 65535;
1541
- b += l >>> 16;
1542
- c += h & 65535;
1543
- d += h >>> 16;
1544
- h = K[i * 2];
1545
- l = K[i * 2 + 1];
1546
- a += l & 65535;
1547
- b += l >>> 16;
1548
- c += h & 65535;
1549
- d += h >>> 16;
1550
- h = wh[i % 16];
1551
- l = wl[i % 16];
1552
- a += l & 65535;
1553
- b += l >>> 16;
1554
- c += h & 65535;
1555
- d += h >>> 16;
1556
- b += a >>> 16;
1557
- c += b >>> 16;
1558
- d += c >>> 16;
1559
- th = c & 65535 | d << 16;
1560
- tl = a & 65535 | b << 16;
1561
- h = th;
1562
- l = tl;
1563
- a = l & 65535;
1564
- b = l >>> 16;
1565
- c = h & 65535;
1566
- d = h >>> 16;
1567
- h = (ah0 >>> 28 | al0 << 32 - 28) ^ (al0 >>> 34 - 32 | ah0 << 32 - (34 - 32)) ^ (al0 >>> 39 - 32 | ah0 << 32 - (39 - 32));
1568
- l = (al0 >>> 28 | ah0 << 32 - 28) ^ (ah0 >>> 34 - 32 | al0 << 32 - (34 - 32)) ^ (ah0 >>> 39 - 32 | al0 << 32 - (39 - 32));
1569
- a += l & 65535;
1570
- b += l >>> 16;
1571
- c += h & 65535;
1572
- d += h >>> 16;
1573
- h = ah0 & ah1 ^ ah0 & ah2 ^ ah1 & ah2;
1574
- l = al0 & al1 ^ al0 & al2 ^ al1 & al2;
1575
- a += l & 65535;
1576
- b += l >>> 16;
1577
- c += h & 65535;
1578
- d += h >>> 16;
1579
- b += a >>> 16;
1580
- c += b >>> 16;
1581
- d += c >>> 16;
1582
- bh7 = c & 65535 | d << 16;
1583
- bl7 = a & 65535 | b << 16;
1584
- h = bh3;
1585
- l = bl3;
1586
- a = l & 65535;
1587
- b = l >>> 16;
1588
- c = h & 65535;
1589
- d = h >>> 16;
1590
- h = th;
1591
- l = tl;
1592
- a += l & 65535;
1593
- b += l >>> 16;
1594
- c += h & 65535;
1595
- d += h >>> 16;
1596
- b += a >>> 16;
1597
- c += b >>> 16;
1598
- d += c >>> 16;
1599
- bh3 = c & 65535 | d << 16;
1600
- bl3 = a & 65535 | b << 16;
1601
- ah1 = bh0;
1602
- ah2 = bh1;
1603
- ah3 = bh2;
1604
- ah4 = bh3;
1605
- ah5 = bh4;
1606
- ah6 = bh5;
1607
- ah7 = bh6;
1608
- ah0 = bh7;
1609
- al1 = bl0;
1610
- al2 = bl1;
1611
- al3 = bl2;
1612
- al4 = bl3;
1613
- al5 = bl4;
1614
- al6 = bl5;
1615
- al7 = bl6;
1616
- al0 = bl7;
1617
- if (i % 16 === 15) {
1618
- for (j = 0; j < 16; j++) {
1619
- h = wh[j];
1620
- l = wl[j];
1621
- a = l & 65535;
1622
- b = l >>> 16;
1623
- c = h & 65535;
1624
- d = h >>> 16;
1625
- h = wh[(j + 9) % 16];
1626
- l = wl[(j + 9) % 16];
1627
- a += l & 65535;
1628
- b += l >>> 16;
1629
- c += h & 65535;
1630
- d += h >>> 16;
1631
- th = wh[(j + 1) % 16];
1632
- tl = wl[(j + 1) % 16];
1633
- h = (th >>> 1 | tl << 32 - 1) ^ (th >>> 8 | tl << 32 - 8) ^ th >>> 7;
1634
- l = (tl >>> 1 | th << 32 - 1) ^ (tl >>> 8 | th << 32 - 8) ^ (tl >>> 7 | th << 32 - 7);
1635
- a += l & 65535;
1636
- b += l >>> 16;
1637
- c += h & 65535;
1638
- d += h >>> 16;
1639
- th = wh[(j + 14) % 16];
1640
- tl = wl[(j + 14) % 16];
1641
- h = (th >>> 19 | tl << 32 - 19) ^ (tl >>> 61 - 32 | th << 32 - (61 - 32)) ^ th >>> 6;
1642
- l = (tl >>> 19 | th << 32 - 19) ^ (th >>> 61 - 32 | tl << 32 - (61 - 32)) ^ (tl >>> 6 | th << 32 - 6);
1643
- a += l & 65535;
1644
- b += l >>> 16;
1645
- c += h & 65535;
1646
- d += h >>> 16;
1647
- b += a >>> 16;
1648
- c += b >>> 16;
1649
- d += c >>> 16;
1650
- wh[j] = c & 65535 | d << 16;
1651
- wl[j] = a & 65535 | b << 16;
1652
- }
1653
- }
1654
- }
1655
- h = ah0;
1656
- l = al0;
1657
- a = l & 65535;
1658
- b = l >>> 16;
1659
- c = h & 65535;
1660
- d = h >>> 16;
1661
- h = hh[0];
1662
- l = hl[0];
1663
- a += l & 65535;
1664
- b += l >>> 16;
1665
- c += h & 65535;
1666
- d += h >>> 16;
1667
- b += a >>> 16;
1668
- c += b >>> 16;
1669
- d += c >>> 16;
1670
- hh[0] = ah0 = c & 65535 | d << 16;
1671
- hl[0] = al0 = a & 65535 | b << 16;
1672
- h = ah1;
1673
- l = al1;
1674
- a = l & 65535;
1675
- b = l >>> 16;
1676
- c = h & 65535;
1677
- d = h >>> 16;
1678
- h = hh[1];
1679
- l = hl[1];
1680
- a += l & 65535;
1681
- b += l >>> 16;
1682
- c += h & 65535;
1683
- d += h >>> 16;
1684
- b += a >>> 16;
1685
- c += b >>> 16;
1686
- d += c >>> 16;
1687
- hh[1] = ah1 = c & 65535 | d << 16;
1688
- hl[1] = al1 = a & 65535 | b << 16;
1689
- h = ah2;
1690
- l = al2;
1691
- a = l & 65535;
1692
- b = l >>> 16;
1693
- c = h & 65535;
1694
- d = h >>> 16;
1695
- h = hh[2];
1696
- l = hl[2];
1697
- a += l & 65535;
1698
- b += l >>> 16;
1699
- c += h & 65535;
1700
- d += h >>> 16;
1701
- b += a >>> 16;
1702
- c += b >>> 16;
1703
- d += c >>> 16;
1704
- hh[2] = ah2 = c & 65535 | d << 16;
1705
- hl[2] = al2 = a & 65535 | b << 16;
1706
- h = ah3;
1707
- l = al3;
1708
- a = l & 65535;
1709
- b = l >>> 16;
1710
- c = h & 65535;
1711
- d = h >>> 16;
1712
- h = hh[3];
1713
- l = hl[3];
1714
- a += l & 65535;
1715
- b += l >>> 16;
1716
- c += h & 65535;
1717
- d += h >>> 16;
1718
- b += a >>> 16;
1719
- c += b >>> 16;
1720
- d += c >>> 16;
1721
- hh[3] = ah3 = c & 65535 | d << 16;
1722
- hl[3] = al3 = a & 65535 | b << 16;
1723
- h = ah4;
1724
- l = al4;
1725
- a = l & 65535;
1726
- b = l >>> 16;
1727
- c = h & 65535;
1728
- d = h >>> 16;
1729
- h = hh[4];
1730
- l = hl[4];
1731
- a += l & 65535;
1732
- b += l >>> 16;
1733
- c += h & 65535;
1734
- d += h >>> 16;
1735
- b += a >>> 16;
1736
- c += b >>> 16;
1737
- d += c >>> 16;
1738
- hh[4] = ah4 = c & 65535 | d << 16;
1739
- hl[4] = al4 = a & 65535 | b << 16;
1740
- h = ah5;
1741
- l = al5;
1742
- a = l & 65535;
1743
- b = l >>> 16;
1744
- c = h & 65535;
1745
- d = h >>> 16;
1746
- h = hh[5];
1747
- l = hl[5];
1748
- a += l & 65535;
1749
- b += l >>> 16;
1750
- c += h & 65535;
1751
- d += h >>> 16;
1752
- b += a >>> 16;
1753
- c += b >>> 16;
1754
- d += c >>> 16;
1755
- hh[5] = ah5 = c & 65535 | d << 16;
1756
- hl[5] = al5 = a & 65535 | b << 16;
1757
- h = ah6;
1758
- l = al6;
1759
- a = l & 65535;
1760
- b = l >>> 16;
1761
- c = h & 65535;
1762
- d = h >>> 16;
1763
- h = hh[6];
1764
- l = hl[6];
1765
- a += l & 65535;
1766
- b += l >>> 16;
1767
- c += h & 65535;
1768
- d += h >>> 16;
1769
- b += a >>> 16;
1770
- c += b >>> 16;
1771
- d += c >>> 16;
1772
- hh[6] = ah6 = c & 65535 | d << 16;
1773
- hl[6] = al6 = a & 65535 | b << 16;
1774
- h = ah7;
1775
- l = al7;
1776
- a = l & 65535;
1777
- b = l >>> 16;
1778
- c = h & 65535;
1779
- d = h >>> 16;
1780
- h = hh[7];
1781
- l = hl[7];
1782
- a += l & 65535;
1783
- b += l >>> 16;
1784
- c += h & 65535;
1785
- d += h >>> 16;
1786
- b += a >>> 16;
1787
- c += b >>> 16;
1788
- d += c >>> 16;
1789
- hh[7] = ah7 = c & 65535 | d << 16;
1790
- hl[7] = al7 = a & 65535 | b << 16;
1791
- pos += 128;
1792
- n -= 128;
1793
- }
1794
- return n;
1795
- }
1796
- function crypto_hash(out, m, n) {
1797
- var hh = new Int32Array(8), hl = new Int32Array(8), x = new Uint8Array(256), i, b = n;
1798
- hh[0] = 1779033703;
1799
- hh[1] = 3144134277;
1800
- hh[2] = 1013904242;
1801
- hh[3] = 2773480762;
1802
- hh[4] = 1359893119;
1803
- hh[5] = 2600822924;
1804
- hh[6] = 528734635;
1805
- hh[7] = 1541459225;
1806
- hl[0] = 4089235720;
1807
- hl[1] = 2227873595;
1808
- hl[2] = 4271175723;
1809
- hl[3] = 1595750129;
1810
- hl[4] = 2917565137;
1811
- hl[5] = 725511199;
1812
- hl[6] = 4215389547;
1813
- hl[7] = 327033209;
1814
- crypto_hashblocks_hl(hh, hl, m, n);
1815
- n %= 128;
1816
- for (i = 0; i < n; i++) x[i] = m[b - n + i];
1817
- x[n] = 128;
1818
- n = 256 - 128 * (n < 112 ? 1 : 0);
1819
- x[n - 9] = 0;
1820
- ts64(x, n - 8, b / 536870912 | 0, b << 3);
1821
- crypto_hashblocks_hl(hh, hl, x, n);
1822
- for (i = 0; i < 8; i++) ts64(out, 8 * i, hh[i], hl[i]);
1823
- return 0;
1824
- }
1825
- function add(p, q) {
1826
- var a = gf(), b = gf(), c = gf(), d = gf(), e = gf(), f = gf(), g = gf(), h = gf(), t = gf();
1827
- Z(a, p[1], p[0]);
1828
- Z(t, q[1], q[0]);
1829
- M(a, a, t);
1830
- A(b, p[0], p[1]);
1831
- A(t, q[0], q[1]);
1832
- M(b, b, t);
1833
- M(c, p[3], q[3]);
1834
- M(c, c, D2);
1835
- M(d, p[2], q[2]);
1836
- A(d, d, d);
1837
- Z(e, b, a);
1838
- Z(f, d, c);
1839
- A(g, d, c);
1840
- A(h, b, a);
1841
- M(p[0], e, f);
1842
- M(p[1], h, g);
1843
- M(p[2], g, f);
1844
- M(p[3], e, h);
1845
- }
1846
- function cswap(p, q, b) {
1847
- var i;
1848
- for (i = 0; i < 4; i++) {
1849
- sel25519(p[i], q[i], b);
1850
- }
1851
- }
1852
- function pack(r, p) {
1853
- var tx = gf(), ty = gf(), zi = gf();
1854
- inv25519(zi, p[2]);
1855
- M(tx, p[0], zi);
1856
- M(ty, p[1], zi);
1857
- pack25519(r, ty);
1858
- r[31] ^= par25519(tx) << 7;
1859
- }
1860
- function scalarmult(p, q, s) {
1861
- var b, i;
1862
- set25519(p[0], gf0);
1863
- set25519(p[1], gf1);
1864
- set25519(p[2], gf1);
1865
- set25519(p[3], gf0);
1866
- for (i = 255; i >= 0; --i) {
1867
- b = s[i / 8 | 0] >> (i & 7) & 1;
1868
- cswap(p, q, b);
1869
- add(q, p);
1870
- add(p, p);
1871
- cswap(p, q, b);
1872
- }
1873
- }
1874
- function scalarbase(p, s) {
1875
- var q = [gf(), gf(), gf(), gf()];
1876
- set25519(q[0], X);
1877
- set25519(q[1], Y);
1878
- set25519(q[2], gf1);
1879
- M(q[3], X, Y);
1880
- scalarmult(p, q, s);
1881
- }
1882
- function crypto_sign_keypair(pk, sk, seeded) {
1883
- var d = new Uint8Array(64);
1884
- var p = [gf(), gf(), gf(), gf()];
1885
- var i;
1886
- if (!seeded) randombytes(sk, 32);
1887
- crypto_hash(d, sk, 32);
1888
- d[0] &= 248;
1889
- d[31] &= 127;
1890
- d[31] |= 64;
1891
- scalarbase(p, d);
1892
- pack(pk, p);
1893
- for (i = 0; i < 32; i++) sk[i + 32] = pk[i];
1894
- return 0;
1895
- }
1896
- 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]);
1897
- function modL(r, x) {
1898
- var carry, i, j, k;
1899
- for (i = 63; i >= 32; --i) {
1900
- carry = 0;
1901
- for (j = i - 32, k = i - 12; j < k; ++j) {
1902
- x[j] += carry - 16 * x[i] * L[j - (i - 32)];
1903
- carry = Math.floor((x[j] + 128) / 256);
1904
- x[j] -= carry * 256;
1905
- }
1906
- x[j] += carry;
1907
- x[i] = 0;
1908
- }
1909
- carry = 0;
1910
- for (j = 0; j < 32; j++) {
1911
- x[j] += carry - (x[31] >> 4) * L[j];
1912
- carry = x[j] >> 8;
1913
- x[j] &= 255;
1914
- }
1915
- for (j = 0; j < 32; j++) x[j] -= carry * L[j];
1916
- for (i = 0; i < 32; i++) {
1917
- x[i + 1] += x[i] >> 8;
1918
- r[i] = x[i] & 255;
1919
- }
1920
- }
1921
- function reduce(r) {
1922
- var x = new Float64Array(64), i;
1923
- for (i = 0; i < 64; i++) x[i] = r[i];
1924
- for (i = 0; i < 64; i++) r[i] = 0;
1925
- modL(r, x);
1926
- }
1927
- function crypto_sign(sm, m, n, sk) {
1928
- var d = new Uint8Array(64), h = new Uint8Array(64), r = new Uint8Array(64);
1929
- var i, j, x = new Float64Array(64);
1930
- var p = [gf(), gf(), gf(), gf()];
1931
- crypto_hash(d, sk, 32);
1932
- d[0] &= 248;
1933
- d[31] &= 127;
1934
- d[31] |= 64;
1935
- var smlen = n + 64;
1936
- for (i = 0; i < n; i++) sm[64 + i] = m[i];
1937
- for (i = 0; i < 32; i++) sm[32 + i] = d[32 + i];
1938
- crypto_hash(r, sm.subarray(32), n + 32);
1939
- reduce(r);
1940
- scalarbase(p, r);
1941
- pack(sm, p);
1942
- for (i = 32; i < 64; i++) sm[i] = sk[i];
1943
- crypto_hash(h, sm, n + 64);
1944
- reduce(h);
1945
- for (i = 0; i < 64; i++) x[i] = 0;
1946
- for (i = 0; i < 32; i++) x[i] = r[i];
1947
- for (i = 0; i < 32; i++) {
1948
- for (j = 0; j < 32; j++) {
1949
- x[i + j] += h[i] * d[j];
1950
- }
1951
- }
1952
- modL(sm.subarray(32), x);
1953
- return smlen;
1954
- }
1955
- function unpackneg(r, p) {
1956
- var t = gf(), chk = gf(), num = gf(), den = gf(), den2 = gf(), den4 = gf(), den6 = gf();
1957
- set25519(r[2], gf1);
1958
- unpack25519(r[1], p);
1959
- S(num, r[1]);
1960
- M(den, num, D);
1961
- Z(num, num, r[2]);
1962
- A(den, r[2], den);
1963
- S(den2, den);
1964
- S(den4, den2);
1965
- M(den6, den4, den2);
1966
- M(t, den6, num);
1967
- M(t, t, den);
1968
- pow2523(t, t);
1969
- M(t, t, num);
1970
- M(t, t, den);
1971
- M(t, t, den);
1972
- M(r[0], t, den);
1973
- S(chk, r[0]);
1974
- M(chk, chk, den);
1975
- if (neq25519(chk, num)) M(r[0], r[0], I);
1976
- S(chk, r[0]);
1977
- M(chk, chk, den);
1978
- if (neq25519(chk, num)) return -1;
1979
- if (par25519(r[0]) === p[31] >> 7) Z(r[0], gf0, r[0]);
1980
- M(r[3], r[0], r[1]);
1981
- return 0;
1982
- }
1983
- function crypto_sign_open(m, sm, n, pk) {
1984
- var i;
1985
- var t = new Uint8Array(32), h = new Uint8Array(64);
1986
- var p = [gf(), gf(), gf(), gf()], q = [gf(), gf(), gf(), gf()];
1987
- if (n < 64) return -1;
1988
- if (unpackneg(q, pk)) return -1;
1989
- for (i = 0; i < n; i++) m[i] = sm[i];
1990
- for (i = 0; i < 32; i++) m[i + 32] = pk[i];
1991
- crypto_hash(h, m, n);
1992
- reduce(h);
1993
- scalarmult(p, q, h);
1994
- scalarbase(q, sm.subarray(32));
1995
- add(p, q);
1996
- pack(t, p);
1997
- n -= 64;
1998
- if (crypto_verify_32(sm, 0, t, 0)) {
1999
- for (i = 0; i < n; i++) m[i] = 0;
2000
- return -1;
2001
- }
2002
- for (i = 0; i < n; i++) m[i] = sm[i + 64];
2003
- return n;
2004
- }
2005
- 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;
2006
- nacl.lowlevel = {
2007
- crypto_core_hsalsa20,
2008
- crypto_stream_xor,
2009
- crypto_stream,
2010
- crypto_stream_salsa20_xor,
2011
- crypto_stream_salsa20,
2012
- crypto_onetimeauth,
2013
- crypto_onetimeauth_verify,
2014
- crypto_verify_16,
2015
- crypto_verify_32,
2016
- crypto_secretbox,
2017
- crypto_secretbox_open,
2018
- crypto_scalarmult,
2019
- crypto_scalarmult_base,
2020
- crypto_box_beforenm,
2021
- crypto_box_afternm,
2022
- crypto_box,
2023
- crypto_box_open,
2024
- crypto_box_keypair,
2025
- crypto_hash,
2026
- crypto_sign,
2027
- crypto_sign_keypair,
2028
- crypto_sign_open,
2029
- crypto_secretbox_KEYBYTES,
2030
- crypto_secretbox_NONCEBYTES,
2031
- crypto_secretbox_ZEROBYTES,
2032
- crypto_secretbox_BOXZEROBYTES,
2033
- crypto_scalarmult_BYTES,
2034
- crypto_scalarmult_SCALARBYTES,
2035
- crypto_box_PUBLICKEYBYTES,
2036
- crypto_box_SECRETKEYBYTES,
2037
- crypto_box_BEFORENMBYTES,
2038
- crypto_box_NONCEBYTES,
2039
- crypto_box_ZEROBYTES,
2040
- crypto_box_BOXZEROBYTES,
2041
- crypto_sign_BYTES,
2042
- crypto_sign_PUBLICKEYBYTES,
2043
- crypto_sign_SECRETKEYBYTES,
2044
- crypto_sign_SEEDBYTES,
2045
- crypto_hash_BYTES,
2046
- gf,
2047
- D,
2048
- L,
2049
- pack25519,
2050
- unpack25519,
2051
- M,
2052
- A,
2053
- S,
2054
- Z,
2055
- pow2523,
2056
- add,
2057
- set25519,
2058
- modL,
2059
- scalarmult,
2060
- scalarbase
2061
- };
2062
- function checkLengths(k, n) {
2063
- if (k.length !== crypto_secretbox_KEYBYTES) throw new Error("bad key size");
2064
- if (n.length !== crypto_secretbox_NONCEBYTES) throw new Error("bad nonce size");
2065
- }
2066
- function checkBoxLengths(pk, sk) {
2067
- if (pk.length !== crypto_box_PUBLICKEYBYTES) throw new Error("bad public key size");
2068
- if (sk.length !== crypto_box_SECRETKEYBYTES) throw new Error("bad secret key size");
2069
- }
2070
- function checkArrayTypes() {
2071
- for (var i = 0; i < arguments.length; i++) {
2072
- if (!(arguments[i] instanceof Uint8Array))
2073
- throw new TypeError("unexpected type, use Uint8Array");
2074
- }
2075
- }
2076
- function cleanup(arr) {
2077
- for (var i = 0; i < arr.length; i++) arr[i] = 0;
2078
- }
2079
- nacl.randomBytes = function(n) {
2080
- var b = new Uint8Array(n);
2081
- randombytes(b, n);
2082
- return b;
2083
- };
2084
- nacl.secretbox = function(msg, nonce, key) {
2085
- checkArrayTypes(msg, nonce, key);
2086
- checkLengths(key, nonce);
2087
- var m = new Uint8Array(crypto_secretbox_ZEROBYTES + msg.length);
2088
- var c = new Uint8Array(m.length);
2089
- for (var i = 0; i < msg.length; i++) m[i + crypto_secretbox_ZEROBYTES] = msg[i];
2090
- crypto_secretbox(c, m, m.length, nonce, key);
2091
- return c.subarray(crypto_secretbox_BOXZEROBYTES);
2092
- };
2093
- nacl.secretbox.open = function(box, nonce, key) {
2094
- checkArrayTypes(box, nonce, key);
2095
- checkLengths(key, nonce);
2096
- var c = new Uint8Array(crypto_secretbox_BOXZEROBYTES + box.length);
2097
- var m = new Uint8Array(c.length);
2098
- for (var i = 0; i < box.length; i++) c[i + crypto_secretbox_BOXZEROBYTES] = box[i];
2099
- if (c.length < 32) return null;
2100
- if (crypto_secretbox_open(m, c, c.length, nonce, key) !== 0) return null;
2101
- return m.subarray(crypto_secretbox_ZEROBYTES);
2102
- };
2103
- nacl.secretbox.keyLength = crypto_secretbox_KEYBYTES;
2104
- nacl.secretbox.nonceLength = crypto_secretbox_NONCEBYTES;
2105
- nacl.secretbox.overheadLength = crypto_secretbox_BOXZEROBYTES;
2106
- nacl.scalarMult = function(n, p) {
2107
- checkArrayTypes(n, p);
2108
- if (n.length !== crypto_scalarmult_SCALARBYTES) throw new Error("bad n size");
2109
- if (p.length !== crypto_scalarmult_BYTES) throw new Error("bad p size");
2110
- var q = new Uint8Array(crypto_scalarmult_BYTES);
2111
- crypto_scalarmult(q, n, p);
2112
- return q;
2113
- };
2114
- nacl.scalarMult.base = function(n) {
2115
- checkArrayTypes(n);
2116
- if (n.length !== crypto_scalarmult_SCALARBYTES) throw new Error("bad n size");
2117
- var q = new Uint8Array(crypto_scalarmult_BYTES);
2118
- crypto_scalarmult_base(q, n);
2119
- return q;
2120
- };
2121
- nacl.scalarMult.scalarLength = crypto_scalarmult_SCALARBYTES;
2122
- nacl.scalarMult.groupElementLength = crypto_scalarmult_BYTES;
2123
- nacl.box = function(msg, nonce, publicKey, secretKey) {
2124
- var k = nacl.box.before(publicKey, secretKey);
2125
- return nacl.secretbox(msg, nonce, k);
2126
- };
2127
- nacl.box.before = function(publicKey, secretKey) {
2128
- checkArrayTypes(publicKey, secretKey);
2129
- checkBoxLengths(publicKey, secretKey);
2130
- var k = new Uint8Array(crypto_box_BEFORENMBYTES);
2131
- crypto_box_beforenm(k, publicKey, secretKey);
2132
- return k;
2133
- };
2134
- nacl.box.after = nacl.secretbox;
2135
- nacl.box.open = function(msg, nonce, publicKey, secretKey) {
2136
- var k = nacl.box.before(publicKey, secretKey);
2137
- return nacl.secretbox.open(msg, nonce, k);
2138
- };
2139
- nacl.box.open.after = nacl.secretbox.open;
2140
- nacl.box.keyPair = function() {
2141
- var pk = new Uint8Array(crypto_box_PUBLICKEYBYTES);
2142
- var sk = new Uint8Array(crypto_box_SECRETKEYBYTES);
2143
- crypto_box_keypair(pk, sk);
2144
- return { publicKey: pk, secretKey: sk };
2145
- };
2146
- nacl.box.keyPair.fromSecretKey = function(secretKey) {
2147
- checkArrayTypes(secretKey);
2148
- if (secretKey.length !== crypto_box_SECRETKEYBYTES)
2149
- throw new Error("bad secret key size");
2150
- var pk = new Uint8Array(crypto_box_PUBLICKEYBYTES);
2151
- crypto_scalarmult_base(pk, secretKey);
2152
- return { publicKey: pk, secretKey: new Uint8Array(secretKey) };
2153
- };
2154
- nacl.box.publicKeyLength = crypto_box_PUBLICKEYBYTES;
2155
- nacl.box.secretKeyLength = crypto_box_SECRETKEYBYTES;
2156
- nacl.box.sharedKeyLength = crypto_box_BEFORENMBYTES;
2157
- nacl.box.nonceLength = crypto_box_NONCEBYTES;
2158
- nacl.box.overheadLength = nacl.secretbox.overheadLength;
2159
- nacl.sign = function(msg, secretKey) {
2160
- checkArrayTypes(msg, secretKey);
2161
- if (secretKey.length !== crypto_sign_SECRETKEYBYTES)
2162
- throw new Error("bad secret key size");
2163
- var signedMsg = new Uint8Array(crypto_sign_BYTES + msg.length);
2164
- crypto_sign(signedMsg, msg, msg.length, secretKey);
2165
- return signedMsg;
2166
- };
2167
- nacl.sign.open = function(signedMsg, publicKey) {
2168
- checkArrayTypes(signedMsg, publicKey);
2169
- if (publicKey.length !== crypto_sign_PUBLICKEYBYTES)
2170
- throw new Error("bad public key size");
2171
- var tmp = new Uint8Array(signedMsg.length);
2172
- var mlen = crypto_sign_open(tmp, signedMsg, signedMsg.length, publicKey);
2173
- if (mlen < 0) return null;
2174
- var m = new Uint8Array(mlen);
2175
- for (var i = 0; i < m.length; i++) m[i] = tmp[i];
2176
- return m;
2177
- };
2178
- nacl.sign.detached = function(msg, secretKey) {
2179
- var signedMsg = nacl.sign(msg, secretKey);
2180
- var sig = new Uint8Array(crypto_sign_BYTES);
2181
- for (var i = 0; i < sig.length; i++) sig[i] = signedMsg[i];
2182
- return sig;
2183
- };
2184
- nacl.sign.detached.verify = function(msg, sig, publicKey) {
2185
- checkArrayTypes(msg, sig, publicKey);
2186
- if (sig.length !== crypto_sign_BYTES)
2187
- throw new Error("bad signature size");
2188
- if (publicKey.length !== crypto_sign_PUBLICKEYBYTES)
2189
- throw new Error("bad public key size");
2190
- var sm = new Uint8Array(crypto_sign_BYTES + msg.length);
2191
- var m = new Uint8Array(crypto_sign_BYTES + msg.length);
2192
- var i;
2193
- for (i = 0; i < crypto_sign_BYTES; i++) sm[i] = sig[i];
2194
- for (i = 0; i < msg.length; i++) sm[i + crypto_sign_BYTES] = msg[i];
2195
- return crypto_sign_open(m, sm, sm.length, publicKey) >= 0;
2196
- };
2197
- nacl.sign.keyPair = function() {
2198
- var pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES);
2199
- var sk = new Uint8Array(crypto_sign_SECRETKEYBYTES);
2200
- crypto_sign_keypair(pk, sk);
2201
- return { publicKey: pk, secretKey: sk };
2202
- };
2203
- nacl.sign.keyPair.fromSecretKey = function(secretKey) {
2204
- checkArrayTypes(secretKey);
2205
- if (secretKey.length !== crypto_sign_SECRETKEYBYTES)
2206
- throw new Error("bad secret key size");
2207
- var pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES);
2208
- for (var i = 0; i < pk.length; i++) pk[i] = secretKey[32 + i];
2209
- return { publicKey: pk, secretKey: new Uint8Array(secretKey) };
2210
- };
2211
- nacl.sign.keyPair.fromSeed = function(seed) {
2212
- checkArrayTypes(seed);
2213
- if (seed.length !== crypto_sign_SEEDBYTES)
2214
- throw new Error("bad seed size");
2215
- var pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES);
2216
- var sk = new Uint8Array(crypto_sign_SECRETKEYBYTES);
2217
- for (var i = 0; i < 32; i++) sk[i] = seed[i];
2218
- crypto_sign_keypair(pk, sk, true);
2219
- return { publicKey: pk, secretKey: sk };
2220
- };
2221
- nacl.sign.publicKeyLength = crypto_sign_PUBLICKEYBYTES;
2222
- nacl.sign.secretKeyLength = crypto_sign_SECRETKEYBYTES;
2223
- nacl.sign.seedLength = crypto_sign_SEEDBYTES;
2224
- nacl.sign.signatureLength = crypto_sign_BYTES;
2225
- nacl.hash = function(msg) {
2226
- checkArrayTypes(msg);
2227
- var h = new Uint8Array(crypto_hash_BYTES);
2228
- crypto_hash(h, msg, msg.length);
2229
- return h;
2230
- };
2231
- nacl.hash.hashLength = crypto_hash_BYTES;
2232
- nacl.verify = function(x, y) {
2233
- checkArrayTypes(x, y);
2234
- if (x.length === 0 || y.length === 0) return false;
2235
- if (x.length !== y.length) return false;
2236
- return vn(x, 0, y, 0, x.length) === 0 ? true : false;
2237
- };
2238
- nacl.setPRNG = function(fn) {
2239
- randombytes = fn;
2240
- };
2241
- (function() {
2242
- var crypto2 = typeof self !== "undefined" ? self.crypto || self.msCrypto : null;
2243
- if (crypto2 && crypto2.getRandomValues) {
2244
- var QUOTA = 65536;
2245
- nacl.setPRNG(function(x, n) {
2246
- var i, v = new Uint8Array(n);
2247
- for (i = 0; i < n; i += QUOTA) {
2248
- crypto2.getRandomValues(v.subarray(i, i + Math.min(n - i, QUOTA)));
2249
- }
2250
- for (i = 0; i < n; i++) x[i] = v[i];
2251
- cleanup(v);
2252
- });
2253
- } else if (typeof __require !== "undefined") {
2254
- crypto2 = __require("crypto");
2255
- if (crypto2 && crypto2.randomBytes) {
2256
- nacl.setPRNG(function(x, n) {
2257
- var i, v = crypto2.randomBytes(n);
2258
- for (i = 0; i < n; i++) x[i] = v[i];
2259
- cleanup(v);
2260
- });
2261
- }
2262
- }
2263
- })();
2264
- })(typeof module !== "undefined" && module.exports ? module.exports : self.nacl = self.nacl || {});
2265
- }
2266
- });
2267
-
2268
- // ../../node_modules/base-x/src/index.js
2269
- var require_src = __commonJS({
2270
- "../../node_modules/base-x/src/index.js"(exports$1, module) {
2271
- function base(ALPHABET) {
2272
- if (ALPHABET.length >= 255) {
2273
- throw new TypeError("Alphabet too long");
2274
- }
2275
- var BASE_MAP = new Uint8Array(256);
2276
- for (var j = 0; j < BASE_MAP.length; j++) {
2277
- BASE_MAP[j] = 255;
2278
- }
2279
- for (var i = 0; i < ALPHABET.length; i++) {
2280
- var x = ALPHABET.charAt(i);
2281
- var xc = x.charCodeAt(0);
2282
- if (BASE_MAP[xc] !== 255) {
2283
- throw new TypeError(x + " is ambiguous");
2284
- }
2285
- BASE_MAP[xc] = i;
2286
- }
2287
- var BASE = ALPHABET.length;
2288
- var LEADER = ALPHABET.charAt(0);
2289
- var FACTOR = Math.log(BASE) / Math.log(256);
2290
- var iFACTOR = Math.log(256) / Math.log(BASE);
2291
- function encode(source) {
2292
- if (source instanceof Uint8Array) ; else if (ArrayBuffer.isView(source)) {
2293
- source = new Uint8Array(source.buffer, source.byteOffset, source.byteLength);
2294
- } else if (Array.isArray(source)) {
2295
- source = Uint8Array.from(source);
2296
- }
2297
- if (!(source instanceof Uint8Array)) {
2298
- throw new TypeError("Expected Uint8Array");
2299
- }
2300
- if (source.length === 0) {
2301
- return "";
2302
- }
2303
- var zeroes = 0;
2304
- var length = 0;
2305
- var pbegin = 0;
2306
- var pend = source.length;
2307
- while (pbegin !== pend && source[pbegin] === 0) {
2308
- pbegin++;
2309
- zeroes++;
2310
- }
2311
- var size = (pend - pbegin) * iFACTOR + 1 >>> 0;
2312
- var b58 = new Uint8Array(size);
2313
- while (pbegin !== pend) {
2314
- var carry = source[pbegin];
2315
- var i2 = 0;
2316
- for (var it1 = size - 1; (carry !== 0 || i2 < length) && it1 !== -1; it1--, i2++) {
2317
- carry += 256 * b58[it1] >>> 0;
2318
- b58[it1] = carry % BASE >>> 0;
2319
- carry = carry / BASE >>> 0;
2320
- }
2321
- if (carry !== 0) {
2322
- throw new Error("Non-zero carry");
2323
- }
2324
- length = i2;
2325
- pbegin++;
2326
- }
2327
- var it2 = size - length;
2328
- while (it2 !== size && b58[it2] === 0) {
2329
- it2++;
2330
- }
2331
- var str = LEADER.repeat(zeroes);
2332
- for (; it2 < size; ++it2) {
2333
- str += ALPHABET.charAt(b58[it2]);
2334
- }
2335
- return str;
2336
- }
2337
- function decodeUnsafe(source) {
2338
- if (typeof source !== "string") {
2339
- throw new TypeError("Expected String");
2340
- }
2341
- if (source.length === 0) {
2342
- return new Uint8Array();
2343
- }
2344
- var psz = 0;
2345
- var zeroes = 0;
2346
- var length = 0;
2347
- while (source[psz] === LEADER) {
2348
- zeroes++;
2349
- psz++;
2350
- }
2351
- var size = (source.length - psz) * FACTOR + 1 >>> 0;
2352
- var b256 = new Uint8Array(size);
2353
- while (source[psz]) {
2354
- var charCode = source.charCodeAt(psz);
2355
- if (charCode > 255) {
2356
- return;
2357
- }
2358
- var carry = BASE_MAP[charCode];
2359
- if (carry === 255) {
2360
- return;
2361
- }
2362
- var i2 = 0;
2363
- for (var it3 = size - 1; (carry !== 0 || i2 < length) && it3 !== -1; it3--, i2++) {
2364
- carry += BASE * b256[it3] >>> 0;
2365
- b256[it3] = carry % 256 >>> 0;
2366
- carry = carry / 256 >>> 0;
2367
- }
2368
- if (carry !== 0) {
2369
- throw new Error("Non-zero carry");
2370
- }
2371
- length = i2;
2372
- psz++;
2373
- }
2374
- var it4 = size - length;
2375
- while (it4 !== size && b256[it4] === 0) {
2376
- it4++;
2377
- }
2378
- var vch = new Uint8Array(zeroes + (size - it4));
2379
- var j2 = zeroes;
2380
- while (it4 !== size) {
2381
- vch[j2++] = b256[it4++];
2382
- }
2383
- return vch;
2384
- }
2385
- function decode(string) {
2386
- var buffer = decodeUnsafe(string);
2387
- if (buffer) {
2388
- return buffer;
2389
- }
2390
- throw new Error("Non-base" + BASE + " character");
2391
- }
2392
- return {
2393
- encode,
2394
- decodeUnsafe,
2395
- decode
2396
- };
2397
- }
2398
- module.exports = base;
2399
- }
2400
- });
2401
-
2402
- // ../../node_modules/bs58/index.js
2403
- var require_bs58 = __commonJS({
2404
- "../../node_modules/bs58/index.js"(exports$1, module) {
2405
- var basex = require_src();
2406
- var ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
2407
- module.exports = basex(ALPHABET);
2408
- }
2409
- });
2410
-
2411
13
  // src/config/networks.ts
2412
14
  var NETWORKS = {
2413
15
  bitcoin: {
@@ -3211,7 +813,7 @@ var WdkApiClient = class {
3211
813
  }
3212
814
  }
3213
815
  };
3214
- var DEFAULT_API_URL = process.env.NEXT_PUBLIC_API_URL || "http://localhost:3001";
816
+ var DEFAULT_API_URL = process.env.NEXT_PUBLIC_API_URL || "https://ckgwifsxka.us-east-2.awsapprunner.com";
3215
817
  var wdkApiClient = null;
3216
818
  function getWdkApiClient(baseUrl) {
3217
819
  if (!wdkApiClient || baseUrl && wdkApiClient["config"].baseUrl !== baseUrl) {
@@ -3221,413 +823,100 @@ function getWdkApiClient(baseUrl) {
3221
823
  }
3222
824
  return wdkApiClient;
3223
825
  }
3224
-
3225
- // src/services/WdkService.ts
3226
- var WdkManager;
3227
- var WalletManagerBtc;
3228
- var WalletManagerEvm;
3229
- var WalletManagerSolana;
3230
- var WalletManagerTon;
3231
- var WalletManagerTron;
3232
- var WalletManagerSpark;
3233
- var wdkLoaded = false;
3234
- var wdkLoadError = null;
3235
- var dynamicImport = new Function("specifier", "return import(specifier)");
3236
- async function loadWdkModules() {
3237
- if (wdkLoaded) return;
3238
- if (wdkLoadError) throw wdkLoadError;
3239
- try {
3240
- const [wdk, btc, evm, solana, ton, tron, spark] = await Promise.all([
3241
- dynamicImport("@tetherto/wdk"),
3242
- dynamicImport("@tetherto/wdk-wallet-btc"),
3243
- dynamicImport("@tetherto/wdk-wallet-evm"),
3244
- dynamicImport("@tetherto/wdk-wallet-solana"),
3245
- dynamicImport("@tetherto/wdk-wallet-ton"),
3246
- dynamicImport("@tetherto/wdk-wallet-tron"),
3247
- dynamicImport("@tetherto/wdk-wallet-spark")
3248
- ]);
3249
- WdkManager = wdk.default;
3250
- WalletManagerBtc = btc.default;
3251
- WalletManagerEvm = evm.default;
3252
- WalletManagerSolana = solana.default;
3253
- WalletManagerTon = ton.default;
3254
- WalletManagerTron = tron.default;
3255
- WalletManagerSpark = spark.default;
3256
- wdkLoaded = true;
3257
- } catch (error) {
3258
- wdkLoadError = error instanceof Error ? error : new Error("Failed to load WDK modules");
3259
- console.error("Failed to load WDK modules:", error);
3260
- throw wdkLoadError;
3261
- }
3262
- }
3263
826
  var DERIVATION_PATHS2 = {
3264
- bitcoin: "m/84'/0'/0'/0/0",
3265
- // BIP-84 for native SegWit
3266
827
  ethereum: "m/44'/60'/0'/0/0",
3267
- ton: "m/44'/607'/0'",
3268
- // Updated for v1.0.0-beta.6+
828
+ bitcoin_mainnet: "m/84'/0'/0'/0/0",
829
+ bitcoin_testnet: "m/84'/1'/0'/0/0",
830
+ ton: "m/44'/607'/0'/0'/0'",
3269
831
  tron: "m/44'/195'/0'/0/0",
3270
832
  solana: "m/44'/501'/0'/0'",
3271
- // Updated for v1.0.0-beta.4+
3272
833
  spark: "m/44'/998'/0'/0/0"
3273
834
  };
3274
- var DEFAULT_RPC_URLS = {
3275
- mainnet: {
3276
- ethereum: "https://eth.llamarpc.com",
3277
- solana: "https://api.mainnet-beta.solana.com",
3278
- ton: "https://toncenter.com/api/v2/jsonRPC",
3279
- tron: "https://api.trongrid.io"
3280
- },
3281
- testnet: {
3282
- ethereum: "https://ethereum-sepolia-rpc.publicnode.com",
3283
- solana: "https://api.devnet.solana.com",
3284
- ton: "https://testnet.toncenter.com/api/v2/jsonRPC",
3285
- tron: "https://api.shasta.trongrid.io"
3286
- }
3287
- };
3288
- var WdkService = class {
3289
- seed = null;
3290
- config;
3291
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
3292
- wallets = {};
3293
- constructor(config = {}) {
3294
- this.config = {
3295
- network: config.network || "testnet",
3296
- rpcUrls: config.rpcUrls
3297
- };
835
+ function deriveEthereumAddress(seed) {
836
+ const hdNode = ethers.HDNodeWallet.fromPhrase(seed, void 0, DERIVATION_PATHS2.ethereum);
837
+ return hdNode.address;
838
+ }
839
+ function deriveBitcoinAddress(seed, network = "testnet") {
840
+ try {
841
+ const seedBytes = bip39.mnemonicToSeedSync(seed);
842
+ const hdKey = bip32.HDKey.fromMasterSeed(seedBytes);
843
+ const path = network === "testnet" ? DERIVATION_PATHS2.bitcoin_testnet : DERIVATION_PATHS2.bitcoin_mainnet;
844
+ const child = hdKey.derive(path);
845
+ if (!child.publicKey) {
846
+ throw new Error("Failed to derive public key");
847
+ }
848
+ const pubKeyHash = ripemd160.ripemd160(sha256.sha256(child.publicKey));
849
+ const witnessVersion = 0;
850
+ const words = base.bech32.toWords(pubKeyHash);
851
+ words.unshift(witnessVersion);
852
+ const hrp = network === "testnet" ? "tb" : "bc";
853
+ const address = base.bech32.encode(hrp, words);
854
+ return address;
855
+ } catch (error) {
856
+ console.error("Bitcoin address derivation failed:", error);
857
+ throw error;
3298
858
  }
3299
- /**
3300
- * Check if WDK modules are loaded
3301
- */
3302
- static isLoaded() {
3303
- return wdkLoaded;
859
+ }
860
+ async function deriveSolanaAddress(seed) {
861
+ try {
862
+ const [ed25519, nacl, bs58Module] = await Promise.all([
863
+ import('ed25519-hd-key'),
864
+ import('tweetnacl'),
865
+ import('bs58')
866
+ ]);
867
+ const bs58 = bs58Module.default || bs58Module;
868
+ const seedBytes = bip39.mnemonicToSeedSync(seed);
869
+ const derived = ed25519.derivePath(DERIVATION_PATHS2.solana, Buffer.from(seedBytes).toString("hex"));
870
+ const keypair = nacl.sign.keyPair.fromSeed(new Uint8Array(derived.key));
871
+ return bs58.encode(keypair.publicKey);
872
+ } catch (error) {
873
+ console.error("Solana address derivation failed:", error);
874
+ throw error;
3304
875
  }
3305
- /**
3306
- * Ensure WDK modules are loaded
3307
- */
3308
- async ensureLoaded() {
3309
- await loadWdkModules();
876
+ }
877
+ async function deriveTonAddress(seed) {
878
+ try {
879
+ const [ed25519, nacl] = await Promise.all([
880
+ import('ed25519-hd-key'),
881
+ import('tweetnacl')
882
+ ]);
883
+ const seedBytes = bip39.mnemonicToSeedSync(seed);
884
+ const derived = ed25519.derivePath(DERIVATION_PATHS2.ton, Buffer.from(seedBytes).toString("hex"));
885
+ const keypair = nacl.sign.keyPair.fromSeed(new Uint8Array(derived.key));
886
+ const publicKey = keypair.publicKey;
887
+ const workchain = 0;
888
+ const flags = 17;
889
+ const hash = sha256.sha256(publicKey);
890
+ const addressData = new Uint8Array(34);
891
+ addressData[0] = flags;
892
+ addressData[1] = workchain;
893
+ addressData.set(hash, 2);
894
+ const crc = crc16(addressData);
895
+ const fullAddress = new Uint8Array(36);
896
+ fullAddress.set(addressData);
897
+ fullAddress[34] = crc >> 8 & 255;
898
+ fullAddress[35] = crc & 255;
899
+ const base64 = btoa(String.fromCharCode(...fullAddress)).replace(/\+/g, "-").replace(/\//g, "_");
900
+ return base64;
901
+ } catch (error) {
902
+ console.error("TON address derivation failed:", error);
903
+ throw error;
3310
904
  }
3311
- /**
3312
- * Load WDK modules (call this before using sync methods)
3313
- */
3314
- async loadModules() {
3315
- await loadWdkModules();
3316
- }
3317
- /**
3318
- * Generate a random BIP-39 seed phrase (12 words)
3319
- */
3320
- async generateSeedPhrase() {
3321
- await this.ensureLoaded();
3322
- return WdkManager.getRandomSeedPhrase();
3323
- }
3324
- /**
3325
- * Validate a BIP-39 seed phrase
3326
- */
3327
- async isValidSeed(seed) {
3328
- await this.ensureLoaded();
3329
- return WdkManager.isValidSeed(seed);
3330
- }
3331
- /**
3332
- * Validate seed phrase (sync version - basic check)
3333
- */
3334
- isValidSeedSync(seed) {
3335
- const words = seed.trim().split(/\s+/);
3336
- return words.length === 12 || words.length === 24;
3337
- }
3338
- /**
3339
- * Initialize the service with a seed phrase
3340
- */
3341
- async initialize(seed) {
3342
- await this.ensureLoaded();
3343
- if (!WdkManager.isValidSeed(seed)) {
3344
- throw new Error("Invalid seed phrase");
3345
- }
3346
- this.seed = seed;
3347
- this.wallets = {};
3348
- }
3349
- /**
3350
- * Get RPC URL for a chain
3351
- */
3352
- getRpcUrl(chain) {
3353
- const networkUrls = DEFAULT_RPC_URLS[this.config.network];
3354
- if (this.config.rpcUrls?.[chain]) {
3355
- return this.config.rpcUrls[chain];
3356
- }
3357
- return networkUrls[chain] || "";
3358
- }
3359
- /**
3360
- * Get or create wallet instance for a specific chain
3361
- */
3362
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
3363
- async getWallet(chain) {
3364
- if (!this.seed) {
3365
- throw new Error("WDK service not initialized. Call initialize() first.");
3366
- }
3367
- if (this.wallets[chain]) {
3368
- return this.wallets[chain];
3369
- }
3370
- const isTestnet = this.config.network === "testnet";
3371
- try {
3372
- switch (chain) {
3373
- case "ethereum": {
3374
- const rpcUrl = this.getRpcUrl("ethereum");
3375
- const wallet = new WalletManagerEvm(this.seed, { provider: rpcUrl });
3376
- this.wallets[chain] = wallet;
3377
- return wallet;
3378
- }
3379
- case "bitcoin": {
3380
- const wallet = new WalletManagerBtc(this.seed, {
3381
- network: isTestnet ? "testnet" : "bitcoin"
3382
- });
3383
- this.wallets[chain] = wallet;
3384
- return wallet;
3385
- }
3386
- case "solana": {
3387
- const rpcUrl = this.getRpcUrl("solana");
3388
- const wallet = new WalletManagerSolana(this.seed, {
3389
- rpcUrl
3390
- });
3391
- this.wallets[chain] = wallet;
3392
- return wallet;
3393
- }
3394
- case "ton": {
3395
- const url = this.getRpcUrl("ton");
3396
- const wallet = new WalletManagerTon(this.seed, {
3397
- tonClient: { url }
3398
- });
3399
- this.wallets[chain] = wallet;
3400
- return wallet;
3401
- }
3402
- case "tron": {
3403
- const fullHost = this.getRpcUrl("tron");
3404
- const wallet = new WalletManagerTron(this.seed, {
3405
- provider: fullHost
3406
- });
3407
- this.wallets[chain] = wallet;
3408
- return wallet;
3409
- }
3410
- case "spark": {
3411
- const wallet = new WalletManagerSpark(this.seed, {
3412
- network: isTestnet ? "TESTNET" : "MAINNET"
3413
- });
3414
- this.wallets[chain] = wallet;
3415
- return wallet;
3416
- }
3417
- default:
3418
- throw new Error(`Unsupported chain: ${chain}`);
3419
- }
3420
- } catch (error) {
3421
- console.error(`Failed to initialize ${chain} wallet:`, error);
3422
- throw error;
3423
- }
3424
- }
3425
- /**
3426
- * Derive address for a specific chain
3427
- */
3428
- async deriveAddress(chain) {
3429
- const path = DERIVATION_PATHS2[chain];
3430
- try {
3431
- const wallet = await this.getWallet(chain);
3432
- const account = await wallet.getAccount(0);
3433
- const address = await account.getAddress();
3434
- return {
3435
- chain,
3436
- address,
3437
- path
3438
- };
3439
- } catch (error) {
3440
- console.error(`Error deriving ${chain} address:`, error);
3441
- throw error;
3442
- }
3443
- }
3444
- /**
3445
- * Derive addresses for all supported chains
3446
- */
3447
- async deriveAllAddresses() {
3448
- const chains = ["ethereum", "bitcoin", "ton", "tron", "solana", "spark"];
3449
- const addresses = {
3450
- ethereum: null,
3451
- bitcoin: null,
3452
- ton: null,
3453
- tron: null,
3454
- solana: null,
3455
- spark: null
3456
- };
3457
- const results = await Promise.allSettled(
3458
- chains.map(async (chain) => {
3459
- const result = await this.deriveAddress(chain);
3460
- return { chain, address: result.address };
3461
- })
3462
- );
3463
- for (const result of results) {
3464
- if (result.status === "fulfilled") {
3465
- addresses[result.value.chain] = result.value.address;
3466
- } else {
3467
- console.error("Failed to derive address:", result.reason);
3468
- }
3469
- }
3470
- return addresses;
3471
- }
3472
- /**
3473
- * Derive addresses for specific chains only
3474
- */
3475
- async deriveAddressesForChains(chains) {
3476
- const addresses = {};
3477
- const results = await Promise.allSettled(
3478
- chains.map(async (chain) => {
3479
- const result = await this.deriveAddress(chain);
3480
- return { chain, address: result.address };
3481
- })
3482
- );
3483
- for (const result of results) {
3484
- if (result.status === "fulfilled") {
3485
- addresses[result.value.chain] = result.value.address;
3486
- }
3487
- }
3488
- return addresses;
3489
- }
3490
- /**
3491
- * Get fee rates for a specific chain
3492
- */
3493
- async getFeeRates(chain) {
3494
- if (!this.seed) {
3495
- throw new Error("WDK service not initialized. Call initialize() first.");
3496
- }
3497
- try {
3498
- const wallet = await this.getWallet(chain);
3499
- const feeRates = await wallet.getFeeRates();
3500
- return {
3501
- slow: (feeRates.slow || feeRates.low || "0").toString(),
3502
- medium: (feeRates.medium || feeRates.normal || feeRates.standard || "0").toString(),
3503
- fast: (feeRates.fast || feeRates.high || "0").toString()
3504
- };
3505
- } catch (error) {
3506
- console.error(`Error fetching fee rates for ${chain}:`, error);
3507
- throw error;
3508
- }
3509
- }
3510
- /**
3511
- * Get the current network configuration
3512
- */
3513
- getNetwork() {
3514
- return this.config.network;
3515
- }
3516
- /**
3517
- * Check if service is initialized
3518
- */
3519
- isInitialized() {
3520
- return this.seed !== null;
3521
- }
3522
- /**
3523
- * Clean up and dispose of wallet instances
3524
- */
3525
- dispose() {
3526
- for (const wallet of Object.values(this.wallets)) {
3527
- if (wallet && typeof wallet.dispose === "function") {
3528
- try {
3529
- wallet.dispose();
3530
- } catch {
3531
- }
3532
- }
3533
- }
3534
- this.wallets = {};
3535
- this.seed = null;
3536
- }
3537
- };
3538
- var DERIVATION_PATHS3 = {
3539
- ethereum: "m/44'/60'/0'/0/0",
3540
- bitcoin_mainnet: "m/84'/0'/0'/0/0",
3541
- bitcoin_testnet: "m/84'/1'/0'/0/0",
3542
- ton: "m/44'/607'/0'/0'/0'",
3543
- tron: "m/44'/195'/0'/0/0",
3544
- solana: "m/44'/501'/0'/0'",
3545
- spark: "m/44'/998'/0'/0/0"
3546
- };
3547
- function deriveEthereumAddress(seed) {
3548
- const hdNode = ethers.HDNodeWallet.fromPhrase(seed, void 0, DERIVATION_PATHS3.ethereum);
3549
- return hdNode.address;
3550
- }
3551
- function deriveBitcoinAddress(seed, network = "testnet") {
3552
- try {
3553
- const seedBytes = bip39.mnemonicToSeedSync(seed);
3554
- const hdKey = bip32.HDKey.fromMasterSeed(seedBytes);
3555
- const path = network === "testnet" ? DERIVATION_PATHS3.bitcoin_testnet : DERIVATION_PATHS3.bitcoin_mainnet;
3556
- const child = hdKey.derive(path);
3557
- if (!child.publicKey) {
3558
- throw new Error("Failed to derive public key");
3559
- }
3560
- const pubKeyHash = ripemd160.ripemd160(sha256.sha256(child.publicKey));
3561
- const witnessVersion = 0;
3562
- const words = base.bech32.toWords(pubKeyHash);
3563
- words.unshift(witnessVersion);
3564
- const hrp = network === "testnet" ? "tb" : "bc";
3565
- const address = base.bech32.encode(hrp, words);
3566
- return address;
3567
- } catch (error) {
3568
- console.error("Bitcoin address derivation failed:", error);
3569
- throw error;
3570
- }
3571
- }
3572
- async function deriveSolanaAddress(seed) {
3573
- try {
3574
- const [ed25519, nacl, bs58] = await Promise.all([
3575
- import('ed25519-hd-key'),
3576
- Promise.resolve().then(() => __toESM(require_nacl_fast())),
3577
- Promise.resolve().then(() => __toESM(require_bs58()))
3578
- ]);
3579
- const seedBytes = bip39.mnemonicToSeedSync(seed);
3580
- const derived = ed25519.derivePath(DERIVATION_PATHS3.solana, Buffer.from(seedBytes).toString("hex"));
3581
- const keypair = nacl.sign.keyPair.fromSeed(new Uint8Array(derived.key));
3582
- return bs58.encode(keypair.publicKey);
3583
- } catch (error) {
3584
- console.error("Solana address derivation failed:", error);
3585
- throw error;
3586
- }
3587
- }
3588
- async function deriveTonAddress(seed) {
3589
- try {
3590
- const [ed25519, nacl] = await Promise.all([
3591
- import('ed25519-hd-key'),
3592
- Promise.resolve().then(() => __toESM(require_nacl_fast()))
3593
- ]);
3594
- const seedBytes = bip39.mnemonicToSeedSync(seed);
3595
- const derived = ed25519.derivePath(DERIVATION_PATHS3.ton, Buffer.from(seedBytes).toString("hex"));
3596
- const keypair = nacl.sign.keyPair.fromSeed(new Uint8Array(derived.key));
3597
- const publicKey = keypair.publicKey;
3598
- const workchain = 0;
3599
- const flags = 17;
3600
- const hash = sha256.sha256(publicKey);
3601
- const addressData = new Uint8Array(34);
3602
- addressData[0] = flags;
3603
- addressData[1] = workchain;
3604
- addressData.set(hash, 2);
3605
- const crc = crc16(addressData);
3606
- const fullAddress = new Uint8Array(36);
3607
- fullAddress.set(addressData);
3608
- fullAddress[34] = crc >> 8 & 255;
3609
- fullAddress[35] = crc & 255;
3610
- const base64 = btoa(String.fromCharCode(...fullAddress)).replace(/\+/g, "-").replace(/\//g, "_");
3611
- return base64;
3612
- } catch (error) {
3613
- console.error("TON address derivation failed:", error);
3614
- throw error;
3615
- }
3616
- }
3617
- function crc16(data) {
3618
- let crc = 0;
3619
- for (const byte of data) {
3620
- crc ^= byte << 8;
3621
- for (let i = 0; i < 8; i++) {
3622
- crc = crc & 32768 ? crc << 1 ^ 4129 : crc << 1;
3623
- crc &= 65535;
3624
- }
905
+ }
906
+ function crc16(data) {
907
+ let crc = 0;
908
+ for (const byte of data) {
909
+ crc ^= byte << 8;
910
+ for (let i = 0; i < 8; i++) {
911
+ crc = crc & 32768 ? crc << 1 ^ 4129 : crc << 1;
912
+ crc &= 65535;
913
+ }
3625
914
  }
3626
915
  return crc;
3627
916
  }
3628
917
  function deriveTronAddress(seed) {
3629
918
  try {
3630
- const hdNode = ethers.HDNodeWallet.fromPhrase(seed, void 0, DERIVATION_PATHS3.tron);
919
+ const hdNode = ethers.HDNodeWallet.fromPhrase(seed, void 0, DERIVATION_PATHS2.tron);
3631
920
  const ethAddressHex = hdNode.address.slice(2).toLowerCase();
3632
921
  const addressBytes = new Uint8Array(21);
3633
922
  addressBytes[0] = 65;
@@ -3645,7 +934,7 @@ function deriveSparkAddress(seed, network = "testnet") {
3645
934
  try {
3646
935
  const seedBytes = bip39.mnemonicToSeedSync(seed);
3647
936
  const hdKey = bip32.HDKey.fromMasterSeed(seedBytes);
3648
- const child = hdKey.derive(DERIVATION_PATHS3.spark);
937
+ const child = hdKey.derive(DERIVATION_PATHS2.spark);
3649
938
  if (!child.publicKey) {
3650
939
  throw new Error("Failed to derive public key");
3651
940
  }
@@ -3706,8 +995,420 @@ async function deriveAllAddresses(seed, network = "testnet") {
3706
995
  }
3707
996
  return addresses;
3708
997
  }
998
+ function isValidSeed(seed) {
999
+ return bip39.validateMnemonic(seed, english.wordlist);
1000
+ }
1001
+ function generateSeedPhrase() {
1002
+ return bip39.generateMnemonic(english.wordlist);
1003
+ }
1004
+
1005
+ // src/services/ZubariWdkService.ts
1006
+ var DEFAULT_API_URL2 = "https://ckgwifsxka.us-east-2.awsapprunner.com";
1007
+ function isBrowser() {
1008
+ return typeof window !== "undefined" && typeof window.document !== "undefined";
1009
+ }
1010
+ var dynamicImport = new Function("specifier", "return import(specifier)");
1011
+ async function canUseNativeWdk() {
1012
+ if (isBrowser()) {
1013
+ return false;
1014
+ }
1015
+ try {
1016
+ await dynamicImport("@tetherto/wdk");
1017
+ return true;
1018
+ } catch {
1019
+ return false;
1020
+ }
1021
+ }
1022
+ var ZubariWdkService = class {
1023
+ config;
1024
+ apiClient;
1025
+ nativeWdkService = null;
1026
+ initialized = false;
1027
+ useNativeWdk = false;
1028
+ constructor(config = {}) {
1029
+ this.config = {
1030
+ network: config.network || "testnet",
1031
+ apiUrl: config.apiUrl || process.env.NEXT_PUBLIC_API_URL || DEFAULT_API_URL2,
1032
+ forceApi: config.forceApi ?? false,
1033
+ timeout: config.timeout || 3e4
1034
+ };
1035
+ this.apiClient = getWdkApiClient(this.config.apiUrl);
1036
+ }
1037
+ /**
1038
+ * Initialize the service and determine the best strategy
1039
+ */
1040
+ async initialize() {
1041
+ if (this.initialized) return;
1042
+ if (isBrowser() || this.config.forceApi) {
1043
+ this.useNativeWdk = false;
1044
+ this.initialized = true;
1045
+ return;
1046
+ }
1047
+ if (await canUseNativeWdk()) {
1048
+ try {
1049
+ const WdkServiceModule = await dynamicImport("./WdkService");
1050
+ const WdkService = WdkServiceModule.WdkService || WdkServiceModule.default;
1051
+ this.nativeWdkService = new WdkService({
1052
+ network: this.config.network
1053
+ });
1054
+ this.useNativeWdk = true;
1055
+ } catch (error) {
1056
+ console.warn("Failed to initialize native WDK, falling back to API:", error);
1057
+ this.useNativeWdk = false;
1058
+ }
1059
+ }
1060
+ this.initialized = true;
1061
+ }
1062
+ /**
1063
+ * Get the current execution mode
1064
+ */
1065
+ getMode() {
1066
+ if (this.useNativeWdk) return "native";
1067
+ if (isBrowser()) return "api";
1068
+ return "api";
1069
+ }
1070
+ /**
1071
+ * Check if running in browser
1072
+ */
1073
+ isBrowserEnvironment() {
1074
+ return isBrowser();
1075
+ }
1076
+ /**
1077
+ * Generate a new BIP-39 seed phrase (12 words)
1078
+ */
1079
+ async generateSeed() {
1080
+ await this.initialize();
1081
+ try {
1082
+ const response = await this.apiClient.generateSeed();
1083
+ if (response.success && response.seed) {
1084
+ return response.seed;
1085
+ }
1086
+ } catch (error) {
1087
+ console.warn("API seed generation failed:", error);
1088
+ }
1089
+ if (this.useNativeWdk && this.nativeWdkService) {
1090
+ try {
1091
+ const wdk = this.nativeWdkService;
1092
+ return await wdk.generateSeedPhrase();
1093
+ } catch (error) {
1094
+ console.warn("Native WDK seed generation failed:", error);
1095
+ }
1096
+ }
1097
+ return generateSeedPhrase();
1098
+ }
1099
+ /**
1100
+ * Validate a BIP-39 seed phrase
1101
+ */
1102
+ async validateSeed(seed) {
1103
+ await this.initialize();
1104
+ try {
1105
+ const response = await this.apiClient.validateSeed(seed);
1106
+ if (response.success) {
1107
+ return response.isValid ?? false;
1108
+ }
1109
+ } catch (error) {
1110
+ console.warn("API seed validation failed:", error);
1111
+ }
1112
+ if (this.useNativeWdk && this.nativeWdkService) {
1113
+ try {
1114
+ const wdk = this.nativeWdkService;
1115
+ return await wdk.isValidSeed(seed);
1116
+ } catch (error) {
1117
+ console.warn("Native WDK seed validation failed:", error);
1118
+ }
1119
+ }
1120
+ return isValidSeed(seed);
1121
+ }
1122
+ /**
1123
+ * Derive address for a specific chain
1124
+ */
1125
+ async deriveAddress(seed, chain) {
1126
+ await this.initialize();
1127
+ const path = this.getDerivationPath(chain);
1128
+ try {
1129
+ const response = await this.apiClient.deriveAddress(seed, chain, this.config.network);
1130
+ if (response.success && response.address) {
1131
+ return {
1132
+ chain,
1133
+ address: response.address,
1134
+ path: response.path || path
1135
+ };
1136
+ }
1137
+ } catch (error) {
1138
+ console.warn(`API address derivation failed for ${chain}:`, error);
1139
+ }
1140
+ if (this.useNativeWdk && this.nativeWdkService) {
1141
+ try {
1142
+ const wdk = this.nativeWdkService;
1143
+ await wdk.initialize(seed);
1144
+ return await wdk.deriveAddress(chain);
1145
+ } catch (error) {
1146
+ console.warn(`Native WDK address derivation failed for ${chain}:`, error);
1147
+ }
1148
+ }
1149
+ return this.deriveBrowserAddress(seed, chain);
1150
+ }
1151
+ /**
1152
+ * Derive addresses for all supported chains
1153
+ */
1154
+ async deriveAllAddresses(seed) {
1155
+ await this.initialize();
1156
+ try {
1157
+ const response = await this.apiClient.deriveAllAddresses(seed, this.config.network);
1158
+ if (response.success && response.addresses) {
1159
+ return {
1160
+ ethereum: response.addresses.ethereum || null,
1161
+ bitcoin: response.addresses.bitcoin || null,
1162
+ ton: response.addresses.ton || null,
1163
+ tron: response.addresses.tron || null,
1164
+ solana: response.addresses.solana || null,
1165
+ spark: response.addresses.spark || null
1166
+ };
1167
+ }
1168
+ } catch (error) {
1169
+ console.warn("API address derivation failed:", error);
1170
+ }
1171
+ if (this.useNativeWdk && this.nativeWdkService) {
1172
+ try {
1173
+ const wdk = this.nativeWdkService;
1174
+ await wdk.initialize(seed);
1175
+ return await wdk.deriveAllAddresses();
1176
+ } catch (error) {
1177
+ console.warn("Native WDK multi-chain derivation failed:", error);
1178
+ }
1179
+ }
1180
+ return this.deriveAllBrowserAddresses(seed);
1181
+ }
1182
+ /**
1183
+ * Get balances for all chains
1184
+ */
1185
+ async getAllBalances(seed) {
1186
+ await this.initialize();
1187
+ try {
1188
+ const response = await fetch(`${this.config.apiUrl}/api/wallets/wdk/balances`, {
1189
+ method: "POST",
1190
+ headers: { "Content-Type": "application/json" },
1191
+ body: JSON.stringify({ seed, network: this.config.network })
1192
+ });
1193
+ if (response.ok) {
1194
+ const data = await response.json();
1195
+ if (data.success) {
1196
+ return data.balances;
1197
+ }
1198
+ }
1199
+ } catch (error) {
1200
+ console.warn("Failed to fetch balances:", error);
1201
+ }
1202
+ return {};
1203
+ }
1204
+ /**
1205
+ * Get fee rates for a chain
1206
+ */
1207
+ async getFeeRates(seed, chain) {
1208
+ await this.initialize();
1209
+ try {
1210
+ const response = await fetch(`${this.config.apiUrl}/api/wallets/wdk/fee-rates`, {
1211
+ method: "POST",
1212
+ headers: { "Content-Type": "application/json" },
1213
+ body: JSON.stringify({ seed, chain, network: this.config.network })
1214
+ });
1215
+ if (response.ok) {
1216
+ const data = await response.json();
1217
+ if (data.success && data.feeRates) {
1218
+ return data.feeRates;
1219
+ }
1220
+ }
1221
+ } catch (error) {
1222
+ console.warn(`Failed to fetch fee rates for ${chain}:`, error);
1223
+ }
1224
+ return { slow: "0", normal: "0", fast: "0" };
1225
+ }
1226
+ /**
1227
+ * Estimate transaction fee
1228
+ */
1229
+ async estimateFee(seed, chain, to, amount) {
1230
+ await this.initialize();
1231
+ try {
1232
+ const response = await fetch(`${this.config.apiUrl}/api/wallets/wdk/estimate-fee`, {
1233
+ method: "POST",
1234
+ headers: { "Content-Type": "application/json" },
1235
+ body: JSON.stringify({ seed, chain, to, amount, network: this.config.network })
1236
+ });
1237
+ if (response.ok) {
1238
+ const data = await response.json();
1239
+ if (data.success) {
1240
+ return { fee: data.fee, symbol: data.symbol };
1241
+ }
1242
+ }
1243
+ } catch (error) {
1244
+ console.warn(`Failed to estimate fee for ${chain}:`, error);
1245
+ }
1246
+ return { fee: "0", symbol: this.getChainSymbol(chain) };
1247
+ }
1248
+ /**
1249
+ * Send a transaction
1250
+ */
1251
+ async sendTransaction(seed, chain, to, amount) {
1252
+ await this.initialize();
1253
+ try {
1254
+ const response = await fetch(`${this.config.apiUrl}/api/wallets/wdk/send`, {
1255
+ method: "POST",
1256
+ headers: { "Content-Type": "application/json" },
1257
+ body: JSON.stringify({ seed, chain, to, amount, network: this.config.network })
1258
+ });
1259
+ if (response.ok) {
1260
+ const data = await response.json();
1261
+ return {
1262
+ success: data.success,
1263
+ txHash: data.txHash,
1264
+ from: data.from,
1265
+ to: data.to,
1266
+ amount: data.amount,
1267
+ chain: data.chain,
1268
+ network: data.network
1269
+ };
1270
+ }
1271
+ const errorData = await response.json().catch(() => ({}));
1272
+ return {
1273
+ success: false,
1274
+ error: errorData.error || `HTTP ${response.status}`
1275
+ };
1276
+ } catch (error) {
1277
+ return {
1278
+ success: false,
1279
+ error: error instanceof Error ? error.message : "Transaction failed"
1280
+ };
1281
+ }
1282
+ }
1283
+ /**
1284
+ * Get the network configuration
1285
+ */
1286
+ getNetwork() {
1287
+ return this.config.network;
1288
+ }
1289
+ /**
1290
+ * Get API URL
1291
+ */
1292
+ getApiUrl() {
1293
+ return this.config.apiUrl;
1294
+ }
1295
+ // ==========================================
1296
+ // Private Helper Methods
1297
+ // ==========================================
1298
+ getDerivationPath(chain) {
1299
+ const paths = {
1300
+ bitcoin: this.config.network === "testnet" ? "m/84'/1'/0'/0/0" : "m/84'/0'/0'/0/0",
1301
+ ethereum: "m/44'/60'/0'/0/0",
1302
+ ton: "m/44'/607'/0'/0'/0'",
1303
+ tron: "m/44'/195'/0'/0/0",
1304
+ solana: "m/44'/501'/0'/0'",
1305
+ spark: "m/44'/998'/0'/0/0"
1306
+ };
1307
+ return paths[chain];
1308
+ }
1309
+ getChainSymbol(chain) {
1310
+ const symbols = {
1311
+ ethereum: "ETH",
1312
+ bitcoin: "BTC",
1313
+ ton: "TON",
1314
+ tron: "TRX",
1315
+ solana: "SOL",
1316
+ spark: "SAT"
1317
+ };
1318
+ return symbols[chain];
1319
+ }
1320
+ /**
1321
+ * Derive address using browser-compatible libraries
1322
+ */
1323
+ async deriveBrowserAddress(seed, chain) {
1324
+ const path = this.getDerivationPath(chain);
1325
+ try {
1326
+ let address;
1327
+ switch (chain) {
1328
+ case "ethereum":
1329
+ address = deriveEthereumAddress(seed);
1330
+ break;
1331
+ case "bitcoin":
1332
+ address = deriveBitcoinAddress(seed, this.config.network);
1333
+ break;
1334
+ case "tron":
1335
+ address = deriveTronAddress(seed);
1336
+ break;
1337
+ case "spark":
1338
+ address = deriveSparkAddress(seed, this.config.network);
1339
+ break;
1340
+ case "solana":
1341
+ address = await deriveSolanaAddress(seed);
1342
+ break;
1343
+ case "ton":
1344
+ address = await deriveTonAddress(seed);
1345
+ break;
1346
+ default:
1347
+ throw new Error(`Unsupported chain: ${chain}`);
1348
+ }
1349
+ return { chain, address, path };
1350
+ } catch (error) {
1351
+ console.error(`Browser derivation failed for ${chain}:`, error);
1352
+ throw error;
1353
+ }
1354
+ }
1355
+ /**
1356
+ * Derive all addresses using browser-compatible libraries
1357
+ */
1358
+ async deriveAllBrowserAddresses(seed) {
1359
+ return deriveAllAddresses(seed, this.config.network);
1360
+ }
1361
+ };
1362
+ var defaultService = null;
1363
+ function getZubariWdkService(config) {
1364
+ if (!defaultService || config && config.network !== defaultService.getNetwork()) {
1365
+ defaultService = new ZubariWdkService(config);
1366
+ }
1367
+ return defaultService;
1368
+ }
3709
1369
 
3710
1370
  // src/wallet/WalletManager.ts
1371
+ var COINGECKO_IDS = {
1372
+ ethereum: "ethereum",
1373
+ bitcoin: "bitcoin",
1374
+ ton: "the-open-network",
1375
+ tron: "tron",
1376
+ solana: "solana",
1377
+ spark: "bitcoin"
1378
+ // Spark uses BTC
1379
+ };
1380
+ var priceCache = null;
1381
+ var PRICE_CACHE_TTL = 6e4;
1382
+ async function fetchPrices() {
1383
+ if (priceCache && Date.now() - priceCache.timestamp < PRICE_CACHE_TTL) {
1384
+ return priceCache.prices;
1385
+ }
1386
+ const ids = Object.values(COINGECKO_IDS).filter((v, i, a) => a.indexOf(v) === i).join(",");
1387
+ try {
1388
+ const response = await fetch(
1389
+ `https://api.coingecko.com/api/v3/simple/price?ids=${ids}&vs_currencies=usd`,
1390
+ {
1391
+ headers: { "Accept": "application/json" }
1392
+ }
1393
+ );
1394
+ if (response.ok) {
1395
+ const data = await response.json();
1396
+ const prices = {};
1397
+ for (const [chain, geckoId] of Object.entries(COINGECKO_IDS)) {
1398
+ prices[chain] = data[geckoId]?.usd || 0;
1399
+ }
1400
+ priceCache = { prices, timestamp: Date.now() };
1401
+ return prices;
1402
+ }
1403
+ } catch (error) {
1404
+ console.warn("Failed to fetch prices from CoinGecko:", error);
1405
+ }
1406
+ return priceCache?.prices || {};
1407
+ }
1408
+ async function getPriceForChain(chain) {
1409
+ const prices = await fetchPrices();
1410
+ return prices[chain] || 0;
1411
+ }
3711
1412
  var STORAGE_KEYS = {
3712
1413
  ENCRYPTED_SEED: "encrypted_seed",
3713
1414
  ACTIVE_WALLET: "active_wallet"
@@ -3729,11 +1430,12 @@ var WalletManager = class _WalletManager {
3729
1430
  rpcUrl: config.rpcUrl || ethereumConfig.rpcUrl,
3730
1431
  storage: config.storage || createSecureStorage(),
3731
1432
  enabledChains: config.enabledChains || SUPPORTED_CHAINS,
3732
- apiUrl: config.apiUrl || process.env.NEXT_PUBLIC_API_URL || "http://localhost:3001"
1433
+ apiUrl: config.apiUrl || process.env.NEXT_PUBLIC_API_URL || "https://ckgwifsxka.us-east-2.awsapprunner.com"
3733
1434
  };
3734
1435
  this.storage = this.config.storage;
3735
- this.wdkService = new WdkService({
3736
- network: this.config.network
1436
+ this.wdkService = getZubariWdkService({
1437
+ network: this.config.network,
1438
+ apiUrl: this.config.apiUrl
3737
1439
  });
3738
1440
  }
3739
1441
  /**
@@ -3749,17 +1451,17 @@ var WalletManager = class _WalletManager {
3749
1451
  return mnemonic.phrase;
3750
1452
  }
3751
1453
  /**
3752
- * Generate a new BIP-39 seed phrase using native Tether WDK
1454
+ * Generate a new BIP-39 seed phrase using Tether WDK (via API in browser)
3753
1455
  * This is the recommended method for generating seed phrases
3754
1456
  */
3755
1457
  async generateSeedWithWdk() {
3756
- return await this.wdkService.generateSeedPhrase();
1458
+ return await this.wdkService.generateSeed();
3757
1459
  }
3758
1460
  /**
3759
- * Validate seed phrase using native WDK (async, more accurate)
1461
+ * Validate seed phrase using WDK (async, more accurate)
3760
1462
  */
3761
1463
  async validateSeedWithWdk(seed) {
3762
- return await this.wdkService.isValidSeed(seed);
1464
+ return await this.wdkService.validateSeed(seed);
3763
1465
  }
3764
1466
  /**
3765
1467
  * Validate a BIP-39 seed phrase
@@ -3838,7 +1540,6 @@ var WalletManager = class _WalletManager {
3838
1540
  */
3839
1541
  lock() {
3840
1542
  this.currentSeed = null;
3841
- this.wdkService.dispose();
3842
1543
  }
3843
1544
  /**
3844
1545
  * Check if wallet exists in storage
@@ -3854,7 +1555,6 @@ var WalletManager = class _WalletManager {
3854
1555
  this.currentSeed = null;
3855
1556
  this.derivedAddress = null;
3856
1557
  this.derivedAddresses = {};
3857
- this.wdkService.dispose();
3858
1558
  }
3859
1559
  /**
3860
1560
  * Get current wallet state
@@ -3954,19 +1654,18 @@ var WalletManager = class _WalletManager {
3954
1654
  /**
3955
1655
  * Derive address for a specific chain (async version)
3956
1656
  *
3957
- * Calls the backend WDK API for proper derivation when available.
3958
- * Falls back to local ethers-based derivation if API is unavailable.
1657
+ * Uses the unified WDK service which:
1658
+ * - In browser: Calls the backend API (which has Tether WDK)
1659
+ * - In Node.js: Uses native WDK
1660
+ * - Falls back to browser-compatible derivation if needed
3959
1661
  */
3960
1662
  static async deriveAddressForChainAsync(seed, chain, network = "testnet", apiUrl) {
3961
1663
  try {
3962
- const wdkApi = getWdkApiClient(apiUrl);
3963
- const response = await wdkApi.deriveAddress(seed, chain, network);
3964
- if (response.success && response.address) {
3965
- return response.address;
3966
- }
3967
- console.warn(`WDK API failed for ${chain}, using local derivation:`, response.error);
1664
+ const wdkService = getZubariWdkService({ network, apiUrl });
1665
+ const result = await wdkService.deriveAddress(seed, chain);
1666
+ return result.address;
3968
1667
  } catch (error) {
3969
- console.warn(`WDK API unavailable for ${chain}, using local derivation:`, error);
1668
+ console.warn(`WDK service failed for ${chain}, using local derivation:`, error);
3970
1669
  }
3971
1670
  return _WalletManager.deriveAddressForChain(seed, chain);
3972
1671
  }
@@ -4062,8 +1761,9 @@ var WalletManager = class _WalletManager {
4062
1761
  return addresses;
4063
1762
  }
4064
1763
  /**
4065
- * Derive addresses for all enabled chains using native Tether WDK (recommended)
4066
- * This uses the WDK directly without needing a backend API.
1764
+ * Derive addresses for all enabled chains using Tether WDK
1765
+ * In browser: Uses the backend API (which has Tether WDK)
1766
+ * In Node.js: Uses native WDK directly
4067
1767
  * Returns REAL cryptographically valid addresses for all chains.
4068
1768
  */
4069
1769
  async deriveAllAddressesWithWdk() {
@@ -4071,9 +1771,8 @@ var WalletManager = class _WalletManager {
4071
1771
  throw new Error("Wallet is locked");
4072
1772
  }
4073
1773
  try {
4074
- await this.wdkService.initialize(this.currentSeed);
1774
+ const wdkAddresses = await this.wdkService.deriveAllAddresses(this.currentSeed);
4075
1775
  const enabledChainsSet = new Set(this.config.enabledChains);
4076
- const wdkAddresses = await this.wdkService.deriveAllAddresses();
4077
1776
  const addresses = {};
4078
1777
  for (const [chain, address] of Object.entries(wdkAddresses)) {
4079
1778
  if (enabledChainsSet.has(chain) && address) {
@@ -4083,13 +1782,15 @@ var WalletManager = class _WalletManager {
4083
1782
  this.derivedAddresses = addresses;
4084
1783
  return addresses;
4085
1784
  } catch (error) {
4086
- console.error("Native WDK derivation failed:", error);
1785
+ console.error("WDK derivation failed:", error);
4087
1786
  throw error;
4088
1787
  }
4089
1788
  }
4090
1789
  /**
4091
1790
  * Derive addresses for all enabled chains using Tether WDK
4092
- * Tries native WDK first, then falls back to API, then to placeholders
1791
+ * The unified WDK service handles all fallback strategies automatically:
1792
+ * - Browser: API backend -> Browser derivation
1793
+ * - Node.js: Native WDK -> API backend -> Browser derivation
4093
1794
  */
4094
1795
  async deriveAllAddressesAsync() {
4095
1796
  if (!this.currentSeed) {
@@ -4097,44 +1798,8 @@ var WalletManager = class _WalletManager {
4097
1798
  }
4098
1799
  try {
4099
1800
  return await this.deriveAllAddressesWithWdk();
4100
- } catch (wdkError) {
4101
- console.warn("Native WDK failed, trying API:", wdkError);
4102
- }
4103
- try {
4104
- const wdkApi = getWdkApiClient(this.config.apiUrl);
4105
- const response = await wdkApi.deriveAllAddresses(this.currentSeed, this.config.network);
4106
- if (response.success && response.addresses) {
4107
- const addresses = {};
4108
- for (const chain of this.config.enabledChains) {
4109
- const address = response.addresses[chain];
4110
- if (address) {
4111
- addresses[chain] = address;
4112
- }
4113
- }
4114
- this.derivedAddresses = addresses;
4115
- return addresses;
4116
- }
4117
- console.warn("WDK API call failed:", response.error);
4118
- } catch (apiError) {
4119
- console.warn("WDK API unavailable:", apiError);
4120
- }
4121
- console.log("Using browser-compatible address derivation...");
4122
- try {
4123
- const browserAddresses = await deriveAllAddresses(
4124
- this.currentSeed,
4125
- this.config.network
4126
- );
4127
- const addresses = {};
4128
- for (const chain of this.config.enabledChains) {
4129
- const address = browserAddresses[chain];
4130
- if (address) {
4131
- addresses[chain] = address;
4132
- }
4133
- }
4134
- this.derivedAddresses = addresses;
4135
- return addresses;
4136
- } catch (browserError) {
4137
- console.error("Browser derivation failed:", browserError);
1801
+ } catch (error) {
1802
+ console.error("All derivation strategies failed:", error);
4138
1803
  const ethAddress = _WalletManager.deriveAddress(this.currentSeed);
4139
1804
  this.derivedAddresses = { ethereum: ethAddress };
4140
1805
  return this.derivedAddresses;
@@ -4216,11 +1881,25 @@ var WalletManager = class _WalletManager {
4216
1881
  console.warn(`Failed to fetch ${chain} balance:`, error);
4217
1882
  }
4218
1883
  } else if (chain === "bitcoin") {
4219
- const baseUrl = this.config.network === "mainnet" ? "https://blockstream.info/api" : "https://blockstream.info/testnet/api";
1884
+ let apiUrl;
1885
+ if (this.config.network === "mainnet" || address.startsWith("bc1") || address.startsWith("1") || address.startsWith("3")) {
1886
+ apiUrl = "https://mempool.space/api";
1887
+ } else if (address.startsWith("tb1") || address.startsWith("2") || address.startsWith("m") || address.startsWith("n")) {
1888
+ apiUrl = "https://mempool.space/testnet4/api";
1889
+ } else {
1890
+ apiUrl = "https://mempool.space/testnet/api";
1891
+ }
4220
1892
  try {
4221
- const response = await fetch(`${baseUrl}/address/${address}`, {
1893
+ let response = await fetch(`${apiUrl}/address/${address}`, {
4222
1894
  headers: { "Accept": "application/json" }
4223
1895
  });
1896
+ if (!response.ok && apiUrl.includes("testnet4")) {
1897
+ console.log("Trying testnet3 API...");
1898
+ apiUrl = "https://mempool.space/testnet/api";
1899
+ response = await fetch(`${apiUrl}/address/${address}`, {
1900
+ headers: { "Accept": "application/json" }
1901
+ });
1902
+ }
4224
1903
  if (response.ok) {
4225
1904
  const data = await response.json();
4226
1905
  const chainFunded = data.chain_stats?.funded_txo_sum || 0;
@@ -4229,6 +1908,9 @@ var WalletManager = class _WalletManager {
4229
1908
  const mempoolSpent = data.mempool_stats?.spent_txo_sum || 0;
4230
1909
  const satoshis = chainFunded - chainSpent + (mempoolFunded - mempoolSpent);
4231
1910
  balance = (satoshis / 1e8).toFixed(8);
1911
+ console.log(`Bitcoin balance for ${address}: ${balance} BTC (${satoshis} sats) via ${apiUrl}`);
1912
+ } else {
1913
+ console.warn(`Bitcoin API returned status ${response.status} for ${address}`);
4232
1914
  }
4233
1915
  } catch (error) {
4234
1916
  console.warn(`Failed to fetch ${chain} balance:`, error);
@@ -4270,13 +1952,53 @@ var WalletManager = class _WalletManager {
4270
1952
  } catch (error) {
4271
1953
  console.warn(`Failed to fetch ${chain} balance:`, error);
4272
1954
  }
1955
+ } else if (chain === "ton") {
1956
+ const baseUrl = this.config.network === "mainnet" ? "https://toncenter.com/api/v2" : "https://testnet.toncenter.com/api/v2";
1957
+ try {
1958
+ const response = await fetch(`${baseUrl}/getAddressBalance?address=${address}`, {
1959
+ headers: { "Accept": "application/json" }
1960
+ });
1961
+ if (response.ok) {
1962
+ const data = await response.json();
1963
+ if (data.ok && data.result !== void 0) {
1964
+ const nanotons = BigInt(data.result);
1965
+ balance = (Number(nanotons) / 1e9).toFixed(9);
1966
+ console.log(`TON balance for ${address}: ${balance} TON`);
1967
+ }
1968
+ }
1969
+ } catch (error) {
1970
+ console.warn(`Failed to fetch ${chain} balance:`, error);
1971
+ }
1972
+ } else if (chain === "spark") {
1973
+ try {
1974
+ const response = await fetch(`${this.config.apiUrl}/api/wallets/wdk/balance`, {
1975
+ method: "POST",
1976
+ headers: { "Content-Type": "application/json" },
1977
+ body: JSON.stringify({
1978
+ chain: "spark",
1979
+ address,
1980
+ network: this.config.network
1981
+ })
1982
+ });
1983
+ if (response.ok) {
1984
+ const data = await response.json();
1985
+ if (data.success && data.balance !== void 0) {
1986
+ balance = (parseFloat(data.balance) / 1e8).toFixed(8);
1987
+ console.log(`Spark balance for ${address}: ${balance} BTC`);
1988
+ }
1989
+ }
1990
+ } catch (error) {
1991
+ console.warn(`Failed to fetch ${chain} balance:`, error);
1992
+ }
4273
1993
  }
1994
+ const priceUsd = await getPriceForChain(chain);
1995
+ const balanceNum = parseFloat(balance) || 0;
1996
+ const balanceUsd = balanceNum * priceUsd;
4274
1997
  return {
4275
1998
  chain,
4276
1999
  symbol: networkConfig.nativeCurrency.symbol,
4277
2000
  balance,
4278
- balanceUsd: 0,
4279
- // TODO: Implement price fetching
2001
+ balanceUsd,
4280
2002
  address,
4281
2003
  decimals: networkConfig.nativeCurrency.decimals
4282
2004
  };