@zubari/sdk 0.1.5 → 0.1.6

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