@zubari/sdk 0.1.5 → 0.1.7

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