@zubari/sdk 0.1.4 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{WalletManager-CYJNiww6.d.ts → WalletManager-YnFArqAt.d.ts} +22 -17
- package/dist/{WalletManager-Dmmcbtiw.d.mts → WalletManager-zKc_xew_.d.mts} +22 -17
- package/dist/{index-OxzgPoRG.d.ts → index-CLssrNf2.d.ts} +1 -1
- package/dist/{index-poGbMJzn.d.mts → index-DXTHGOnP.d.mts} +1 -1
- package/dist/index.d.mts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +502 -2902
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +499 -2906
- package/dist/index.mjs.map +1 -1
- package/dist/react/index.d.mts +2 -2
- package/dist/react/index.d.ts +2 -2
- package/dist/react/index.js +421 -2849
- package/dist/react/index.js.map +1 -1
- package/dist/react/index.mjs +423 -2851
- package/dist/react/index.mjs.map +1 -1
- package/dist/services/index.d.mts +290 -55
- package/dist/services/index.d.ts +290 -55
- package/dist/services/index.js +340 -2744
- package/dist/services/index.js.map +1 -1
- package/dist/services/index.mjs +338 -2743
- package/dist/services/index.mjs.map +1 -1
- package/dist/wallet/index.d.mts +2 -2
- package/dist/wallet/index.d.ts +2 -2
- package/dist/wallet/index.js +420 -2850
- package/dist/wallet/index.js.map +1 -1
- package/dist/wallet/index.mjs +422 -2852
- package/dist/wallet/index.mjs.map +1 -1
- package/package.json +9 -5
- package/dist/TransactionService-CxwB1kpN.d.mts +0 -205
- package/dist/TransactionService-DdL6H6M-.d.ts +0 -205
package/dist/react/index.mjs
CHANGED
|
@@ -2,2463 +2,14 @@ import { useMemo, useState, useCallback, useEffect } from 'react';
|
|
|
2
2
|
import { Wallet, HDNodeWallet } from 'ethers';
|
|
3
3
|
import { createPublicClient, http, formatEther } from 'viem';
|
|
4
4
|
import { mainnet, sepolia } from 'viem/chains';
|
|
5
|
-
import { mnemonicToSeedSync } from '@scure/bip39';
|
|
6
|
-
import '@scure/bip39/wordlists/english';
|
|
5
|
+
import { generateMnemonic, validateMnemonic, mnemonicToSeedSync } from '@scure/bip39';
|
|
6
|
+
import { wordlist } from '@scure/bip39/wordlists/english';
|
|
7
7
|
import { HDKey } from '@scure/bip32';
|
|
8
|
-
import { bech32 } from '@scure/base';
|
|
8
|
+
import { bech32, base58check } from '@scure/base';
|
|
9
9
|
import { sha256 } from '@noble/hashes/sha256';
|
|
10
10
|
import { ripemd160 } from '@noble/hashes/ripemd160';
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
var __defProp = Object.defineProperty;
|
|
14
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
15
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
16
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
17
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
18
|
-
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
19
|
-
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
20
|
-
}) : x)(function(x) {
|
|
21
|
-
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
22
|
-
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
23
|
-
});
|
|
24
|
-
var __commonJS = (cb, mod) => function __require2() {
|
|
25
|
-
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
26
|
-
};
|
|
27
|
-
var __copyProps = (to, from, except, desc) => {
|
|
28
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
29
|
-
for (let key of __getOwnPropNames(from))
|
|
30
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
31
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
32
|
-
}
|
|
33
|
-
return to;
|
|
34
|
-
};
|
|
35
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
36
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
37
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
38
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
39
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
40
|
-
!mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
41
|
-
mod
|
|
42
|
-
));
|
|
43
|
-
|
|
44
|
-
// ../../node_modules/tweetnacl/nacl-fast.js
|
|
45
|
-
var require_nacl_fast = __commonJS({
|
|
46
|
-
"../../node_modules/tweetnacl/nacl-fast.js"(exports$1, module) {
|
|
47
|
-
(function(nacl) {
|
|
48
|
-
var gf = function(init) {
|
|
49
|
-
var i, r = new Float64Array(16);
|
|
50
|
-
if (init) for (i = 0; i < init.length; i++) r[i] = init[i];
|
|
51
|
-
return r;
|
|
52
|
-
};
|
|
53
|
-
var randombytes = function() {
|
|
54
|
-
throw new Error("no PRNG");
|
|
55
|
-
};
|
|
56
|
-
var _0 = new Uint8Array(16);
|
|
57
|
-
var _9 = new Uint8Array(32);
|
|
58
|
-
_9[0] = 9;
|
|
59
|
-
var gf0 = gf(), gf1 = gf([1]), _121665 = gf([56129, 1]), D = gf([30883, 4953, 19914, 30187, 55467, 16705, 2637, 112, 59544, 30585, 16505, 36039, 65139, 11119, 27886, 20995]), D2 = gf([61785, 9906, 39828, 60374, 45398, 33411, 5274, 224, 53552, 61171, 33010, 6542, 64743, 22239, 55772, 9222]), X = gf([54554, 36645, 11616, 51542, 42930, 38181, 51040, 26924, 56412, 64982, 57905, 49316, 21502, 52590, 14035, 8553]), Y = gf([26200, 26214, 26214, 26214, 26214, 26214, 26214, 26214, 26214, 26214, 26214, 26214, 26214, 26214, 26214, 26214]), I = gf([41136, 18958, 6951, 50414, 58488, 44335, 6150, 12099, 55207, 15867, 153, 11085, 57099, 20417, 9344, 11139]);
|
|
60
|
-
function ts64(x, i, h, l) {
|
|
61
|
-
x[i] = h >> 24 & 255;
|
|
62
|
-
x[i + 1] = h >> 16 & 255;
|
|
63
|
-
x[i + 2] = h >> 8 & 255;
|
|
64
|
-
x[i + 3] = h & 255;
|
|
65
|
-
x[i + 4] = l >> 24 & 255;
|
|
66
|
-
x[i + 5] = l >> 16 & 255;
|
|
67
|
-
x[i + 6] = l >> 8 & 255;
|
|
68
|
-
x[i + 7] = l & 255;
|
|
69
|
-
}
|
|
70
|
-
function vn(x, xi, y, yi, n) {
|
|
71
|
-
var i, d = 0;
|
|
72
|
-
for (i = 0; i < n; i++) d |= x[xi + i] ^ y[yi + i];
|
|
73
|
-
return (1 & d - 1 >>> 8) - 1;
|
|
74
|
-
}
|
|
75
|
-
function crypto_verify_16(x, xi, y, yi) {
|
|
76
|
-
return vn(x, xi, y, yi, 16);
|
|
77
|
-
}
|
|
78
|
-
function crypto_verify_32(x, xi, y, yi) {
|
|
79
|
-
return vn(x, xi, y, yi, 32);
|
|
80
|
-
}
|
|
81
|
-
function core_salsa20(o, p, k, c) {
|
|
82
|
-
var j0 = c[0] & 255 | (c[1] & 255) << 8 | (c[2] & 255) << 16 | (c[3] & 255) << 24, j1 = k[0] & 255 | (k[1] & 255) << 8 | (k[2] & 255) << 16 | (k[3] & 255) << 24, j2 = k[4] & 255 | (k[5] & 255) << 8 | (k[6] & 255) << 16 | (k[7] & 255) << 24, j3 = k[8] & 255 | (k[9] & 255) << 8 | (k[10] & 255) << 16 | (k[11] & 255) << 24, j4 = k[12] & 255 | (k[13] & 255) << 8 | (k[14] & 255) << 16 | (k[15] & 255) << 24, j5 = c[4] & 255 | (c[5] & 255) << 8 | (c[6] & 255) << 16 | (c[7] & 255) << 24, j6 = p[0] & 255 | (p[1] & 255) << 8 | (p[2] & 255) << 16 | (p[3] & 255) << 24, j7 = p[4] & 255 | (p[5] & 255) << 8 | (p[6] & 255) << 16 | (p[7] & 255) << 24, j8 = p[8] & 255 | (p[9] & 255) << 8 | (p[10] & 255) << 16 | (p[11] & 255) << 24, j9 = p[12] & 255 | (p[13] & 255) << 8 | (p[14] & 255) << 16 | (p[15] & 255) << 24, j10 = c[8] & 255 | (c[9] & 255) << 8 | (c[10] & 255) << 16 | (c[11] & 255) << 24, j11 = k[16] & 255 | (k[17] & 255) << 8 | (k[18] & 255) << 16 | (k[19] & 255) << 24, j12 = k[20] & 255 | (k[21] & 255) << 8 | (k[22] & 255) << 16 | (k[23] & 255) << 24, j13 = k[24] & 255 | (k[25] & 255) << 8 | (k[26] & 255) << 16 | (k[27] & 255) << 24, j14 = k[28] & 255 | (k[29] & 255) << 8 | (k[30] & 255) << 16 | (k[31] & 255) << 24, j15 = c[12] & 255 | (c[13] & 255) << 8 | (c[14] & 255) << 16 | (c[15] & 255) << 24;
|
|
83
|
-
var x0 = j0, x1 = j1, x2 = j2, x3 = j3, x4 = j4, x5 = j5, x6 = j6, x7 = j7, x8 = j8, x9 = j9, x10 = j10, x11 = j11, x12 = j12, x13 = j13, x14 = j14, x15 = j15, u;
|
|
84
|
-
for (var i = 0; i < 20; i += 2) {
|
|
85
|
-
u = x0 + x12 | 0;
|
|
86
|
-
x4 ^= u << 7 | u >>> 32 - 7;
|
|
87
|
-
u = x4 + x0 | 0;
|
|
88
|
-
x8 ^= u << 9 | u >>> 32 - 9;
|
|
89
|
-
u = x8 + x4 | 0;
|
|
90
|
-
x12 ^= u << 13 | u >>> 32 - 13;
|
|
91
|
-
u = x12 + x8 | 0;
|
|
92
|
-
x0 ^= u << 18 | u >>> 32 - 18;
|
|
93
|
-
u = x5 + x1 | 0;
|
|
94
|
-
x9 ^= u << 7 | u >>> 32 - 7;
|
|
95
|
-
u = x9 + x5 | 0;
|
|
96
|
-
x13 ^= u << 9 | u >>> 32 - 9;
|
|
97
|
-
u = x13 + x9 | 0;
|
|
98
|
-
x1 ^= u << 13 | u >>> 32 - 13;
|
|
99
|
-
u = x1 + x13 | 0;
|
|
100
|
-
x5 ^= u << 18 | u >>> 32 - 18;
|
|
101
|
-
u = x10 + x6 | 0;
|
|
102
|
-
x14 ^= u << 7 | u >>> 32 - 7;
|
|
103
|
-
u = x14 + x10 | 0;
|
|
104
|
-
x2 ^= u << 9 | u >>> 32 - 9;
|
|
105
|
-
u = x2 + x14 | 0;
|
|
106
|
-
x6 ^= u << 13 | u >>> 32 - 13;
|
|
107
|
-
u = x6 + x2 | 0;
|
|
108
|
-
x10 ^= u << 18 | u >>> 32 - 18;
|
|
109
|
-
u = x15 + x11 | 0;
|
|
110
|
-
x3 ^= u << 7 | u >>> 32 - 7;
|
|
111
|
-
u = x3 + x15 | 0;
|
|
112
|
-
x7 ^= u << 9 | u >>> 32 - 9;
|
|
113
|
-
u = x7 + x3 | 0;
|
|
114
|
-
x11 ^= u << 13 | u >>> 32 - 13;
|
|
115
|
-
u = x11 + x7 | 0;
|
|
116
|
-
x15 ^= u << 18 | u >>> 32 - 18;
|
|
117
|
-
u = x0 + x3 | 0;
|
|
118
|
-
x1 ^= u << 7 | u >>> 32 - 7;
|
|
119
|
-
u = x1 + x0 | 0;
|
|
120
|
-
x2 ^= u << 9 | u >>> 32 - 9;
|
|
121
|
-
u = x2 + x1 | 0;
|
|
122
|
-
x3 ^= u << 13 | u >>> 32 - 13;
|
|
123
|
-
u = x3 + x2 | 0;
|
|
124
|
-
x0 ^= u << 18 | u >>> 32 - 18;
|
|
125
|
-
u = x5 + x4 | 0;
|
|
126
|
-
x6 ^= u << 7 | u >>> 32 - 7;
|
|
127
|
-
u = x6 + x5 | 0;
|
|
128
|
-
x7 ^= u << 9 | u >>> 32 - 9;
|
|
129
|
-
u = x7 + x6 | 0;
|
|
130
|
-
x4 ^= u << 13 | u >>> 32 - 13;
|
|
131
|
-
u = x4 + x7 | 0;
|
|
132
|
-
x5 ^= u << 18 | u >>> 32 - 18;
|
|
133
|
-
u = x10 + x9 | 0;
|
|
134
|
-
x11 ^= u << 7 | u >>> 32 - 7;
|
|
135
|
-
u = x11 + x10 | 0;
|
|
136
|
-
x8 ^= u << 9 | u >>> 32 - 9;
|
|
137
|
-
u = x8 + x11 | 0;
|
|
138
|
-
x9 ^= u << 13 | u >>> 32 - 13;
|
|
139
|
-
u = x9 + x8 | 0;
|
|
140
|
-
x10 ^= u << 18 | u >>> 32 - 18;
|
|
141
|
-
u = x15 + x14 | 0;
|
|
142
|
-
x12 ^= u << 7 | u >>> 32 - 7;
|
|
143
|
-
u = x12 + x15 | 0;
|
|
144
|
-
x13 ^= u << 9 | u >>> 32 - 9;
|
|
145
|
-
u = x13 + x12 | 0;
|
|
146
|
-
x14 ^= u << 13 | u >>> 32 - 13;
|
|
147
|
-
u = x14 + x13 | 0;
|
|
148
|
-
x15 ^= u << 18 | u >>> 32 - 18;
|
|
149
|
-
}
|
|
150
|
-
x0 = x0 + j0 | 0;
|
|
151
|
-
x1 = x1 + j1 | 0;
|
|
152
|
-
x2 = x2 + j2 | 0;
|
|
153
|
-
x3 = x3 + j3 | 0;
|
|
154
|
-
x4 = x4 + j4 | 0;
|
|
155
|
-
x5 = x5 + j5 | 0;
|
|
156
|
-
x6 = x6 + j6 | 0;
|
|
157
|
-
x7 = x7 + j7 | 0;
|
|
158
|
-
x8 = x8 + j8 | 0;
|
|
159
|
-
x9 = x9 + j9 | 0;
|
|
160
|
-
x10 = x10 + j10 | 0;
|
|
161
|
-
x11 = x11 + j11 | 0;
|
|
162
|
-
x12 = x12 + j12 | 0;
|
|
163
|
-
x13 = x13 + j13 | 0;
|
|
164
|
-
x14 = x14 + j14 | 0;
|
|
165
|
-
x15 = x15 + j15 | 0;
|
|
166
|
-
o[0] = x0 >>> 0 & 255;
|
|
167
|
-
o[1] = x0 >>> 8 & 255;
|
|
168
|
-
o[2] = x0 >>> 16 & 255;
|
|
169
|
-
o[3] = x0 >>> 24 & 255;
|
|
170
|
-
o[4] = x1 >>> 0 & 255;
|
|
171
|
-
o[5] = x1 >>> 8 & 255;
|
|
172
|
-
o[6] = x1 >>> 16 & 255;
|
|
173
|
-
o[7] = x1 >>> 24 & 255;
|
|
174
|
-
o[8] = x2 >>> 0 & 255;
|
|
175
|
-
o[9] = x2 >>> 8 & 255;
|
|
176
|
-
o[10] = x2 >>> 16 & 255;
|
|
177
|
-
o[11] = x2 >>> 24 & 255;
|
|
178
|
-
o[12] = x3 >>> 0 & 255;
|
|
179
|
-
o[13] = x3 >>> 8 & 255;
|
|
180
|
-
o[14] = x3 >>> 16 & 255;
|
|
181
|
-
o[15] = x3 >>> 24 & 255;
|
|
182
|
-
o[16] = x4 >>> 0 & 255;
|
|
183
|
-
o[17] = x4 >>> 8 & 255;
|
|
184
|
-
o[18] = x4 >>> 16 & 255;
|
|
185
|
-
o[19] = x4 >>> 24 & 255;
|
|
186
|
-
o[20] = x5 >>> 0 & 255;
|
|
187
|
-
o[21] = x5 >>> 8 & 255;
|
|
188
|
-
o[22] = x5 >>> 16 & 255;
|
|
189
|
-
o[23] = x5 >>> 24 & 255;
|
|
190
|
-
o[24] = x6 >>> 0 & 255;
|
|
191
|
-
o[25] = x6 >>> 8 & 255;
|
|
192
|
-
o[26] = x6 >>> 16 & 255;
|
|
193
|
-
o[27] = x6 >>> 24 & 255;
|
|
194
|
-
o[28] = x7 >>> 0 & 255;
|
|
195
|
-
o[29] = x7 >>> 8 & 255;
|
|
196
|
-
o[30] = x7 >>> 16 & 255;
|
|
197
|
-
o[31] = x7 >>> 24 & 255;
|
|
198
|
-
o[32] = x8 >>> 0 & 255;
|
|
199
|
-
o[33] = x8 >>> 8 & 255;
|
|
200
|
-
o[34] = x8 >>> 16 & 255;
|
|
201
|
-
o[35] = x8 >>> 24 & 255;
|
|
202
|
-
o[36] = x9 >>> 0 & 255;
|
|
203
|
-
o[37] = x9 >>> 8 & 255;
|
|
204
|
-
o[38] = x9 >>> 16 & 255;
|
|
205
|
-
o[39] = x9 >>> 24 & 255;
|
|
206
|
-
o[40] = x10 >>> 0 & 255;
|
|
207
|
-
o[41] = x10 >>> 8 & 255;
|
|
208
|
-
o[42] = x10 >>> 16 & 255;
|
|
209
|
-
o[43] = x10 >>> 24 & 255;
|
|
210
|
-
o[44] = x11 >>> 0 & 255;
|
|
211
|
-
o[45] = x11 >>> 8 & 255;
|
|
212
|
-
o[46] = x11 >>> 16 & 255;
|
|
213
|
-
o[47] = x11 >>> 24 & 255;
|
|
214
|
-
o[48] = x12 >>> 0 & 255;
|
|
215
|
-
o[49] = x12 >>> 8 & 255;
|
|
216
|
-
o[50] = x12 >>> 16 & 255;
|
|
217
|
-
o[51] = x12 >>> 24 & 255;
|
|
218
|
-
o[52] = x13 >>> 0 & 255;
|
|
219
|
-
o[53] = x13 >>> 8 & 255;
|
|
220
|
-
o[54] = x13 >>> 16 & 255;
|
|
221
|
-
o[55] = x13 >>> 24 & 255;
|
|
222
|
-
o[56] = x14 >>> 0 & 255;
|
|
223
|
-
o[57] = x14 >>> 8 & 255;
|
|
224
|
-
o[58] = x14 >>> 16 & 255;
|
|
225
|
-
o[59] = x14 >>> 24 & 255;
|
|
226
|
-
o[60] = x15 >>> 0 & 255;
|
|
227
|
-
o[61] = x15 >>> 8 & 255;
|
|
228
|
-
o[62] = x15 >>> 16 & 255;
|
|
229
|
-
o[63] = x15 >>> 24 & 255;
|
|
230
|
-
}
|
|
231
|
-
function core_hsalsa20(o, p, k, c) {
|
|
232
|
-
var j0 = c[0] & 255 | (c[1] & 255) << 8 | (c[2] & 255) << 16 | (c[3] & 255) << 24, j1 = k[0] & 255 | (k[1] & 255) << 8 | (k[2] & 255) << 16 | (k[3] & 255) << 24, j2 = k[4] & 255 | (k[5] & 255) << 8 | (k[6] & 255) << 16 | (k[7] & 255) << 24, j3 = k[8] & 255 | (k[9] & 255) << 8 | (k[10] & 255) << 16 | (k[11] & 255) << 24, j4 = k[12] & 255 | (k[13] & 255) << 8 | (k[14] & 255) << 16 | (k[15] & 255) << 24, j5 = c[4] & 255 | (c[5] & 255) << 8 | (c[6] & 255) << 16 | (c[7] & 255) << 24, j6 = p[0] & 255 | (p[1] & 255) << 8 | (p[2] & 255) << 16 | (p[3] & 255) << 24, j7 = p[4] & 255 | (p[5] & 255) << 8 | (p[6] & 255) << 16 | (p[7] & 255) << 24, j8 = p[8] & 255 | (p[9] & 255) << 8 | (p[10] & 255) << 16 | (p[11] & 255) << 24, j9 = p[12] & 255 | (p[13] & 255) << 8 | (p[14] & 255) << 16 | (p[15] & 255) << 24, j10 = c[8] & 255 | (c[9] & 255) << 8 | (c[10] & 255) << 16 | (c[11] & 255) << 24, j11 = k[16] & 255 | (k[17] & 255) << 8 | (k[18] & 255) << 16 | (k[19] & 255) << 24, j12 = k[20] & 255 | (k[21] & 255) << 8 | (k[22] & 255) << 16 | (k[23] & 255) << 24, j13 = k[24] & 255 | (k[25] & 255) << 8 | (k[26] & 255) << 16 | (k[27] & 255) << 24, j14 = k[28] & 255 | (k[29] & 255) << 8 | (k[30] & 255) << 16 | (k[31] & 255) << 24, j15 = c[12] & 255 | (c[13] & 255) << 8 | (c[14] & 255) << 16 | (c[15] & 255) << 24;
|
|
233
|
-
var x0 = j0, x1 = j1, x2 = j2, x3 = j3, x4 = j4, x5 = j5, x6 = j6, x7 = j7, x8 = j8, x9 = j9, x10 = j10, x11 = j11, x12 = j12, x13 = j13, x14 = j14, x15 = j15, u;
|
|
234
|
-
for (var i = 0; i < 20; i += 2) {
|
|
235
|
-
u = x0 + x12 | 0;
|
|
236
|
-
x4 ^= u << 7 | u >>> 32 - 7;
|
|
237
|
-
u = x4 + x0 | 0;
|
|
238
|
-
x8 ^= u << 9 | u >>> 32 - 9;
|
|
239
|
-
u = x8 + x4 | 0;
|
|
240
|
-
x12 ^= u << 13 | u >>> 32 - 13;
|
|
241
|
-
u = x12 + x8 | 0;
|
|
242
|
-
x0 ^= u << 18 | u >>> 32 - 18;
|
|
243
|
-
u = x5 + x1 | 0;
|
|
244
|
-
x9 ^= u << 7 | u >>> 32 - 7;
|
|
245
|
-
u = x9 + x5 | 0;
|
|
246
|
-
x13 ^= u << 9 | u >>> 32 - 9;
|
|
247
|
-
u = x13 + x9 | 0;
|
|
248
|
-
x1 ^= u << 13 | u >>> 32 - 13;
|
|
249
|
-
u = x1 + x13 | 0;
|
|
250
|
-
x5 ^= u << 18 | u >>> 32 - 18;
|
|
251
|
-
u = x10 + x6 | 0;
|
|
252
|
-
x14 ^= u << 7 | u >>> 32 - 7;
|
|
253
|
-
u = x14 + x10 | 0;
|
|
254
|
-
x2 ^= u << 9 | u >>> 32 - 9;
|
|
255
|
-
u = x2 + x14 | 0;
|
|
256
|
-
x6 ^= u << 13 | u >>> 32 - 13;
|
|
257
|
-
u = x6 + x2 | 0;
|
|
258
|
-
x10 ^= u << 18 | u >>> 32 - 18;
|
|
259
|
-
u = x15 + x11 | 0;
|
|
260
|
-
x3 ^= u << 7 | u >>> 32 - 7;
|
|
261
|
-
u = x3 + x15 | 0;
|
|
262
|
-
x7 ^= u << 9 | u >>> 32 - 9;
|
|
263
|
-
u = x7 + x3 | 0;
|
|
264
|
-
x11 ^= u << 13 | u >>> 32 - 13;
|
|
265
|
-
u = x11 + x7 | 0;
|
|
266
|
-
x15 ^= u << 18 | u >>> 32 - 18;
|
|
267
|
-
u = x0 + x3 | 0;
|
|
268
|
-
x1 ^= u << 7 | u >>> 32 - 7;
|
|
269
|
-
u = x1 + x0 | 0;
|
|
270
|
-
x2 ^= u << 9 | u >>> 32 - 9;
|
|
271
|
-
u = x2 + x1 | 0;
|
|
272
|
-
x3 ^= u << 13 | u >>> 32 - 13;
|
|
273
|
-
u = x3 + x2 | 0;
|
|
274
|
-
x0 ^= u << 18 | u >>> 32 - 18;
|
|
275
|
-
u = x5 + x4 | 0;
|
|
276
|
-
x6 ^= u << 7 | u >>> 32 - 7;
|
|
277
|
-
u = x6 + x5 | 0;
|
|
278
|
-
x7 ^= u << 9 | u >>> 32 - 9;
|
|
279
|
-
u = x7 + x6 | 0;
|
|
280
|
-
x4 ^= u << 13 | u >>> 32 - 13;
|
|
281
|
-
u = x4 + x7 | 0;
|
|
282
|
-
x5 ^= u << 18 | u >>> 32 - 18;
|
|
283
|
-
u = x10 + x9 | 0;
|
|
284
|
-
x11 ^= u << 7 | u >>> 32 - 7;
|
|
285
|
-
u = x11 + x10 | 0;
|
|
286
|
-
x8 ^= u << 9 | u >>> 32 - 9;
|
|
287
|
-
u = x8 + x11 | 0;
|
|
288
|
-
x9 ^= u << 13 | u >>> 32 - 13;
|
|
289
|
-
u = x9 + x8 | 0;
|
|
290
|
-
x10 ^= u << 18 | u >>> 32 - 18;
|
|
291
|
-
u = x15 + x14 | 0;
|
|
292
|
-
x12 ^= u << 7 | u >>> 32 - 7;
|
|
293
|
-
u = x12 + x15 | 0;
|
|
294
|
-
x13 ^= u << 9 | u >>> 32 - 9;
|
|
295
|
-
u = x13 + x12 | 0;
|
|
296
|
-
x14 ^= u << 13 | u >>> 32 - 13;
|
|
297
|
-
u = x14 + x13 | 0;
|
|
298
|
-
x15 ^= u << 18 | u >>> 32 - 18;
|
|
299
|
-
}
|
|
300
|
-
o[0] = x0 >>> 0 & 255;
|
|
301
|
-
o[1] = x0 >>> 8 & 255;
|
|
302
|
-
o[2] = x0 >>> 16 & 255;
|
|
303
|
-
o[3] = x0 >>> 24 & 255;
|
|
304
|
-
o[4] = x5 >>> 0 & 255;
|
|
305
|
-
o[5] = x5 >>> 8 & 255;
|
|
306
|
-
o[6] = x5 >>> 16 & 255;
|
|
307
|
-
o[7] = x5 >>> 24 & 255;
|
|
308
|
-
o[8] = x10 >>> 0 & 255;
|
|
309
|
-
o[9] = x10 >>> 8 & 255;
|
|
310
|
-
o[10] = x10 >>> 16 & 255;
|
|
311
|
-
o[11] = x10 >>> 24 & 255;
|
|
312
|
-
o[12] = x15 >>> 0 & 255;
|
|
313
|
-
o[13] = x15 >>> 8 & 255;
|
|
314
|
-
o[14] = x15 >>> 16 & 255;
|
|
315
|
-
o[15] = x15 >>> 24 & 255;
|
|
316
|
-
o[16] = x6 >>> 0 & 255;
|
|
317
|
-
o[17] = x6 >>> 8 & 255;
|
|
318
|
-
o[18] = x6 >>> 16 & 255;
|
|
319
|
-
o[19] = x6 >>> 24 & 255;
|
|
320
|
-
o[20] = x7 >>> 0 & 255;
|
|
321
|
-
o[21] = x7 >>> 8 & 255;
|
|
322
|
-
o[22] = x7 >>> 16 & 255;
|
|
323
|
-
o[23] = x7 >>> 24 & 255;
|
|
324
|
-
o[24] = x8 >>> 0 & 255;
|
|
325
|
-
o[25] = x8 >>> 8 & 255;
|
|
326
|
-
o[26] = x8 >>> 16 & 255;
|
|
327
|
-
o[27] = x8 >>> 24 & 255;
|
|
328
|
-
o[28] = x9 >>> 0 & 255;
|
|
329
|
-
o[29] = x9 >>> 8 & 255;
|
|
330
|
-
o[30] = x9 >>> 16 & 255;
|
|
331
|
-
o[31] = x9 >>> 24 & 255;
|
|
332
|
-
}
|
|
333
|
-
function crypto_core_salsa20(out, inp, k, c) {
|
|
334
|
-
core_salsa20(out, inp, k, c);
|
|
335
|
-
}
|
|
336
|
-
function crypto_core_hsalsa20(out, inp, k, c) {
|
|
337
|
-
core_hsalsa20(out, inp, k, c);
|
|
338
|
-
}
|
|
339
|
-
var sigma = new Uint8Array([101, 120, 112, 97, 110, 100, 32, 51, 50, 45, 98, 121, 116, 101, 32, 107]);
|
|
340
|
-
function crypto_stream_salsa20_xor(c, cpos, m, mpos, b, n, k) {
|
|
341
|
-
var z = new Uint8Array(16), x = new Uint8Array(64);
|
|
342
|
-
var u, i;
|
|
343
|
-
for (i = 0; i < 16; i++) z[i] = 0;
|
|
344
|
-
for (i = 0; i < 8; i++) z[i] = n[i];
|
|
345
|
-
while (b >= 64) {
|
|
346
|
-
crypto_core_salsa20(x, z, k, sigma);
|
|
347
|
-
for (i = 0; i < 64; i++) c[cpos + i] = m[mpos + i] ^ x[i];
|
|
348
|
-
u = 1;
|
|
349
|
-
for (i = 8; i < 16; i++) {
|
|
350
|
-
u = u + (z[i] & 255) | 0;
|
|
351
|
-
z[i] = u & 255;
|
|
352
|
-
u >>>= 8;
|
|
353
|
-
}
|
|
354
|
-
b -= 64;
|
|
355
|
-
cpos += 64;
|
|
356
|
-
mpos += 64;
|
|
357
|
-
}
|
|
358
|
-
if (b > 0) {
|
|
359
|
-
crypto_core_salsa20(x, z, k, sigma);
|
|
360
|
-
for (i = 0; i < b; i++) c[cpos + i] = m[mpos + i] ^ x[i];
|
|
361
|
-
}
|
|
362
|
-
return 0;
|
|
363
|
-
}
|
|
364
|
-
function crypto_stream_salsa20(c, cpos, b, n, k) {
|
|
365
|
-
var z = new Uint8Array(16), x = new Uint8Array(64);
|
|
366
|
-
var u, i;
|
|
367
|
-
for (i = 0; i < 16; i++) z[i] = 0;
|
|
368
|
-
for (i = 0; i < 8; i++) z[i] = n[i];
|
|
369
|
-
while (b >= 64) {
|
|
370
|
-
crypto_core_salsa20(x, z, k, sigma);
|
|
371
|
-
for (i = 0; i < 64; i++) c[cpos + i] = x[i];
|
|
372
|
-
u = 1;
|
|
373
|
-
for (i = 8; i < 16; i++) {
|
|
374
|
-
u = u + (z[i] & 255) | 0;
|
|
375
|
-
z[i] = u & 255;
|
|
376
|
-
u >>>= 8;
|
|
377
|
-
}
|
|
378
|
-
b -= 64;
|
|
379
|
-
cpos += 64;
|
|
380
|
-
}
|
|
381
|
-
if (b > 0) {
|
|
382
|
-
crypto_core_salsa20(x, z, k, sigma);
|
|
383
|
-
for (i = 0; i < b; i++) c[cpos + i] = x[i];
|
|
384
|
-
}
|
|
385
|
-
return 0;
|
|
386
|
-
}
|
|
387
|
-
function crypto_stream(c, cpos, d, n, k) {
|
|
388
|
-
var s = new Uint8Array(32);
|
|
389
|
-
crypto_core_hsalsa20(s, n, k, sigma);
|
|
390
|
-
var sn = new Uint8Array(8);
|
|
391
|
-
for (var i = 0; i < 8; i++) sn[i] = n[i + 16];
|
|
392
|
-
return crypto_stream_salsa20(c, cpos, d, sn, s);
|
|
393
|
-
}
|
|
394
|
-
function crypto_stream_xor(c, cpos, m, mpos, d, n, k) {
|
|
395
|
-
var s = new Uint8Array(32);
|
|
396
|
-
crypto_core_hsalsa20(s, n, k, sigma);
|
|
397
|
-
var sn = new Uint8Array(8);
|
|
398
|
-
for (var i = 0; i < 8; i++) sn[i] = n[i + 16];
|
|
399
|
-
return crypto_stream_salsa20_xor(c, cpos, m, mpos, d, sn, s);
|
|
400
|
-
}
|
|
401
|
-
var poly1305 = function(key) {
|
|
402
|
-
this.buffer = new Uint8Array(16);
|
|
403
|
-
this.r = new Uint16Array(10);
|
|
404
|
-
this.h = new Uint16Array(10);
|
|
405
|
-
this.pad = new Uint16Array(8);
|
|
406
|
-
this.leftover = 0;
|
|
407
|
-
this.fin = 0;
|
|
408
|
-
var t0, t1, t2, t3, t4, t5, t6, t7;
|
|
409
|
-
t0 = key[0] & 255 | (key[1] & 255) << 8;
|
|
410
|
-
this.r[0] = t0 & 8191;
|
|
411
|
-
t1 = key[2] & 255 | (key[3] & 255) << 8;
|
|
412
|
-
this.r[1] = (t0 >>> 13 | t1 << 3) & 8191;
|
|
413
|
-
t2 = key[4] & 255 | (key[5] & 255) << 8;
|
|
414
|
-
this.r[2] = (t1 >>> 10 | t2 << 6) & 7939;
|
|
415
|
-
t3 = key[6] & 255 | (key[7] & 255) << 8;
|
|
416
|
-
this.r[3] = (t2 >>> 7 | t3 << 9) & 8191;
|
|
417
|
-
t4 = key[8] & 255 | (key[9] & 255) << 8;
|
|
418
|
-
this.r[4] = (t3 >>> 4 | t4 << 12) & 255;
|
|
419
|
-
this.r[5] = t4 >>> 1 & 8190;
|
|
420
|
-
t5 = key[10] & 255 | (key[11] & 255) << 8;
|
|
421
|
-
this.r[6] = (t4 >>> 14 | t5 << 2) & 8191;
|
|
422
|
-
t6 = key[12] & 255 | (key[13] & 255) << 8;
|
|
423
|
-
this.r[7] = (t5 >>> 11 | t6 << 5) & 8065;
|
|
424
|
-
t7 = key[14] & 255 | (key[15] & 255) << 8;
|
|
425
|
-
this.r[8] = (t6 >>> 8 | t7 << 8) & 8191;
|
|
426
|
-
this.r[9] = t7 >>> 5 & 127;
|
|
427
|
-
this.pad[0] = key[16] & 255 | (key[17] & 255) << 8;
|
|
428
|
-
this.pad[1] = key[18] & 255 | (key[19] & 255) << 8;
|
|
429
|
-
this.pad[2] = key[20] & 255 | (key[21] & 255) << 8;
|
|
430
|
-
this.pad[3] = key[22] & 255 | (key[23] & 255) << 8;
|
|
431
|
-
this.pad[4] = key[24] & 255 | (key[25] & 255) << 8;
|
|
432
|
-
this.pad[5] = key[26] & 255 | (key[27] & 255) << 8;
|
|
433
|
-
this.pad[6] = key[28] & 255 | (key[29] & 255) << 8;
|
|
434
|
-
this.pad[7] = key[30] & 255 | (key[31] & 255) << 8;
|
|
435
|
-
};
|
|
436
|
-
poly1305.prototype.blocks = function(m, mpos, bytes) {
|
|
437
|
-
var hibit = this.fin ? 0 : 1 << 11;
|
|
438
|
-
var t0, t1, t2, t3, t4, t5, t6, t7, c;
|
|
439
|
-
var d0, d1, d2, d3, d4, d5, d6, d7, d8, d9;
|
|
440
|
-
var h0 = this.h[0], h1 = this.h[1], h2 = this.h[2], h3 = this.h[3], h4 = this.h[4], h5 = this.h[5], h6 = this.h[6], h7 = this.h[7], h8 = this.h[8], h9 = this.h[9];
|
|
441
|
-
var r0 = this.r[0], r1 = this.r[1], r2 = this.r[2], r3 = this.r[3], r4 = this.r[4], r5 = this.r[5], r6 = this.r[6], r7 = this.r[7], r8 = this.r[8], r9 = this.r[9];
|
|
442
|
-
while (bytes >= 16) {
|
|
443
|
-
t0 = m[mpos + 0] & 255 | (m[mpos + 1] & 255) << 8;
|
|
444
|
-
h0 += t0 & 8191;
|
|
445
|
-
t1 = m[mpos + 2] & 255 | (m[mpos + 3] & 255) << 8;
|
|
446
|
-
h1 += (t0 >>> 13 | t1 << 3) & 8191;
|
|
447
|
-
t2 = m[mpos + 4] & 255 | (m[mpos + 5] & 255) << 8;
|
|
448
|
-
h2 += (t1 >>> 10 | t2 << 6) & 8191;
|
|
449
|
-
t3 = m[mpos + 6] & 255 | (m[mpos + 7] & 255) << 8;
|
|
450
|
-
h3 += (t2 >>> 7 | t3 << 9) & 8191;
|
|
451
|
-
t4 = m[mpos + 8] & 255 | (m[mpos + 9] & 255) << 8;
|
|
452
|
-
h4 += (t3 >>> 4 | t4 << 12) & 8191;
|
|
453
|
-
h5 += t4 >>> 1 & 8191;
|
|
454
|
-
t5 = m[mpos + 10] & 255 | (m[mpos + 11] & 255) << 8;
|
|
455
|
-
h6 += (t4 >>> 14 | t5 << 2) & 8191;
|
|
456
|
-
t6 = m[mpos + 12] & 255 | (m[mpos + 13] & 255) << 8;
|
|
457
|
-
h7 += (t5 >>> 11 | t6 << 5) & 8191;
|
|
458
|
-
t7 = m[mpos + 14] & 255 | (m[mpos + 15] & 255) << 8;
|
|
459
|
-
h8 += (t6 >>> 8 | t7 << 8) & 8191;
|
|
460
|
-
h9 += t7 >>> 5 | hibit;
|
|
461
|
-
c = 0;
|
|
462
|
-
d0 = c;
|
|
463
|
-
d0 += h0 * r0;
|
|
464
|
-
d0 += h1 * (5 * r9);
|
|
465
|
-
d0 += h2 * (5 * r8);
|
|
466
|
-
d0 += h3 * (5 * r7);
|
|
467
|
-
d0 += h4 * (5 * r6);
|
|
468
|
-
c = d0 >>> 13;
|
|
469
|
-
d0 &= 8191;
|
|
470
|
-
d0 += h5 * (5 * r5);
|
|
471
|
-
d0 += h6 * (5 * r4);
|
|
472
|
-
d0 += h7 * (5 * r3);
|
|
473
|
-
d0 += h8 * (5 * r2);
|
|
474
|
-
d0 += h9 * (5 * r1);
|
|
475
|
-
c += d0 >>> 13;
|
|
476
|
-
d0 &= 8191;
|
|
477
|
-
d1 = c;
|
|
478
|
-
d1 += h0 * r1;
|
|
479
|
-
d1 += h1 * r0;
|
|
480
|
-
d1 += h2 * (5 * r9);
|
|
481
|
-
d1 += h3 * (5 * r8);
|
|
482
|
-
d1 += h4 * (5 * r7);
|
|
483
|
-
c = d1 >>> 13;
|
|
484
|
-
d1 &= 8191;
|
|
485
|
-
d1 += h5 * (5 * r6);
|
|
486
|
-
d1 += h6 * (5 * r5);
|
|
487
|
-
d1 += h7 * (5 * r4);
|
|
488
|
-
d1 += h8 * (5 * r3);
|
|
489
|
-
d1 += h9 * (5 * r2);
|
|
490
|
-
c += d1 >>> 13;
|
|
491
|
-
d1 &= 8191;
|
|
492
|
-
d2 = c;
|
|
493
|
-
d2 += h0 * r2;
|
|
494
|
-
d2 += h1 * r1;
|
|
495
|
-
d2 += h2 * r0;
|
|
496
|
-
d2 += h3 * (5 * r9);
|
|
497
|
-
d2 += h4 * (5 * r8);
|
|
498
|
-
c = d2 >>> 13;
|
|
499
|
-
d2 &= 8191;
|
|
500
|
-
d2 += h5 * (5 * r7);
|
|
501
|
-
d2 += h6 * (5 * r6);
|
|
502
|
-
d2 += h7 * (5 * r5);
|
|
503
|
-
d2 += h8 * (5 * r4);
|
|
504
|
-
d2 += h9 * (5 * r3);
|
|
505
|
-
c += d2 >>> 13;
|
|
506
|
-
d2 &= 8191;
|
|
507
|
-
d3 = c;
|
|
508
|
-
d3 += h0 * r3;
|
|
509
|
-
d3 += h1 * r2;
|
|
510
|
-
d3 += h2 * r1;
|
|
511
|
-
d3 += h3 * r0;
|
|
512
|
-
d3 += h4 * (5 * r9);
|
|
513
|
-
c = d3 >>> 13;
|
|
514
|
-
d3 &= 8191;
|
|
515
|
-
d3 += h5 * (5 * r8);
|
|
516
|
-
d3 += h6 * (5 * r7);
|
|
517
|
-
d3 += h7 * (5 * r6);
|
|
518
|
-
d3 += h8 * (5 * r5);
|
|
519
|
-
d3 += h9 * (5 * r4);
|
|
520
|
-
c += d3 >>> 13;
|
|
521
|
-
d3 &= 8191;
|
|
522
|
-
d4 = c;
|
|
523
|
-
d4 += h0 * r4;
|
|
524
|
-
d4 += h1 * r3;
|
|
525
|
-
d4 += h2 * r2;
|
|
526
|
-
d4 += h3 * r1;
|
|
527
|
-
d4 += h4 * r0;
|
|
528
|
-
c = d4 >>> 13;
|
|
529
|
-
d4 &= 8191;
|
|
530
|
-
d4 += h5 * (5 * r9);
|
|
531
|
-
d4 += h6 * (5 * r8);
|
|
532
|
-
d4 += h7 * (5 * r7);
|
|
533
|
-
d4 += h8 * (5 * r6);
|
|
534
|
-
d4 += h9 * (5 * r5);
|
|
535
|
-
c += d4 >>> 13;
|
|
536
|
-
d4 &= 8191;
|
|
537
|
-
d5 = c;
|
|
538
|
-
d5 += h0 * r5;
|
|
539
|
-
d5 += h1 * r4;
|
|
540
|
-
d5 += h2 * r3;
|
|
541
|
-
d5 += h3 * r2;
|
|
542
|
-
d5 += h4 * r1;
|
|
543
|
-
c = d5 >>> 13;
|
|
544
|
-
d5 &= 8191;
|
|
545
|
-
d5 += h5 * r0;
|
|
546
|
-
d5 += h6 * (5 * r9);
|
|
547
|
-
d5 += h7 * (5 * r8);
|
|
548
|
-
d5 += h8 * (5 * r7);
|
|
549
|
-
d5 += h9 * (5 * r6);
|
|
550
|
-
c += d5 >>> 13;
|
|
551
|
-
d5 &= 8191;
|
|
552
|
-
d6 = c;
|
|
553
|
-
d6 += h0 * r6;
|
|
554
|
-
d6 += h1 * r5;
|
|
555
|
-
d6 += h2 * r4;
|
|
556
|
-
d6 += h3 * r3;
|
|
557
|
-
d6 += h4 * r2;
|
|
558
|
-
c = d6 >>> 13;
|
|
559
|
-
d6 &= 8191;
|
|
560
|
-
d6 += h5 * r1;
|
|
561
|
-
d6 += h6 * r0;
|
|
562
|
-
d6 += h7 * (5 * r9);
|
|
563
|
-
d6 += h8 * (5 * r8);
|
|
564
|
-
d6 += h9 * (5 * r7);
|
|
565
|
-
c += d6 >>> 13;
|
|
566
|
-
d6 &= 8191;
|
|
567
|
-
d7 = c;
|
|
568
|
-
d7 += h0 * r7;
|
|
569
|
-
d7 += h1 * r6;
|
|
570
|
-
d7 += h2 * r5;
|
|
571
|
-
d7 += h3 * r4;
|
|
572
|
-
d7 += h4 * r3;
|
|
573
|
-
c = d7 >>> 13;
|
|
574
|
-
d7 &= 8191;
|
|
575
|
-
d7 += h5 * r2;
|
|
576
|
-
d7 += h6 * r1;
|
|
577
|
-
d7 += h7 * r0;
|
|
578
|
-
d7 += h8 * (5 * r9);
|
|
579
|
-
d7 += h9 * (5 * r8);
|
|
580
|
-
c += d7 >>> 13;
|
|
581
|
-
d7 &= 8191;
|
|
582
|
-
d8 = c;
|
|
583
|
-
d8 += h0 * r8;
|
|
584
|
-
d8 += h1 * r7;
|
|
585
|
-
d8 += h2 * r6;
|
|
586
|
-
d8 += h3 * r5;
|
|
587
|
-
d8 += h4 * r4;
|
|
588
|
-
c = d8 >>> 13;
|
|
589
|
-
d8 &= 8191;
|
|
590
|
-
d8 += h5 * r3;
|
|
591
|
-
d8 += h6 * r2;
|
|
592
|
-
d8 += h7 * r1;
|
|
593
|
-
d8 += h8 * r0;
|
|
594
|
-
d8 += h9 * (5 * r9);
|
|
595
|
-
c += d8 >>> 13;
|
|
596
|
-
d8 &= 8191;
|
|
597
|
-
d9 = c;
|
|
598
|
-
d9 += h0 * r9;
|
|
599
|
-
d9 += h1 * r8;
|
|
600
|
-
d9 += h2 * r7;
|
|
601
|
-
d9 += h3 * r6;
|
|
602
|
-
d9 += h4 * r5;
|
|
603
|
-
c = d9 >>> 13;
|
|
604
|
-
d9 &= 8191;
|
|
605
|
-
d9 += h5 * r4;
|
|
606
|
-
d9 += h6 * r3;
|
|
607
|
-
d9 += h7 * r2;
|
|
608
|
-
d9 += h8 * r1;
|
|
609
|
-
d9 += h9 * r0;
|
|
610
|
-
c += d9 >>> 13;
|
|
611
|
-
d9 &= 8191;
|
|
612
|
-
c = (c << 2) + c | 0;
|
|
613
|
-
c = c + d0 | 0;
|
|
614
|
-
d0 = c & 8191;
|
|
615
|
-
c = c >>> 13;
|
|
616
|
-
d1 += c;
|
|
617
|
-
h0 = d0;
|
|
618
|
-
h1 = d1;
|
|
619
|
-
h2 = d2;
|
|
620
|
-
h3 = d3;
|
|
621
|
-
h4 = d4;
|
|
622
|
-
h5 = d5;
|
|
623
|
-
h6 = d6;
|
|
624
|
-
h7 = d7;
|
|
625
|
-
h8 = d8;
|
|
626
|
-
h9 = d9;
|
|
627
|
-
mpos += 16;
|
|
628
|
-
bytes -= 16;
|
|
629
|
-
}
|
|
630
|
-
this.h[0] = h0;
|
|
631
|
-
this.h[1] = h1;
|
|
632
|
-
this.h[2] = h2;
|
|
633
|
-
this.h[3] = h3;
|
|
634
|
-
this.h[4] = h4;
|
|
635
|
-
this.h[5] = h5;
|
|
636
|
-
this.h[6] = h6;
|
|
637
|
-
this.h[7] = h7;
|
|
638
|
-
this.h[8] = h8;
|
|
639
|
-
this.h[9] = h9;
|
|
640
|
-
};
|
|
641
|
-
poly1305.prototype.finish = function(mac, macpos) {
|
|
642
|
-
var g = new Uint16Array(10);
|
|
643
|
-
var c, mask, f, i;
|
|
644
|
-
if (this.leftover) {
|
|
645
|
-
i = this.leftover;
|
|
646
|
-
this.buffer[i++] = 1;
|
|
647
|
-
for (; i < 16; i++) this.buffer[i] = 0;
|
|
648
|
-
this.fin = 1;
|
|
649
|
-
this.blocks(this.buffer, 0, 16);
|
|
650
|
-
}
|
|
651
|
-
c = this.h[1] >>> 13;
|
|
652
|
-
this.h[1] &= 8191;
|
|
653
|
-
for (i = 2; i < 10; i++) {
|
|
654
|
-
this.h[i] += c;
|
|
655
|
-
c = this.h[i] >>> 13;
|
|
656
|
-
this.h[i] &= 8191;
|
|
657
|
-
}
|
|
658
|
-
this.h[0] += c * 5;
|
|
659
|
-
c = this.h[0] >>> 13;
|
|
660
|
-
this.h[0] &= 8191;
|
|
661
|
-
this.h[1] += c;
|
|
662
|
-
c = this.h[1] >>> 13;
|
|
663
|
-
this.h[1] &= 8191;
|
|
664
|
-
this.h[2] += c;
|
|
665
|
-
g[0] = this.h[0] + 5;
|
|
666
|
-
c = g[0] >>> 13;
|
|
667
|
-
g[0] &= 8191;
|
|
668
|
-
for (i = 1; i < 10; i++) {
|
|
669
|
-
g[i] = this.h[i] + c;
|
|
670
|
-
c = g[i] >>> 13;
|
|
671
|
-
g[i] &= 8191;
|
|
672
|
-
}
|
|
673
|
-
g[9] -= 1 << 13;
|
|
674
|
-
mask = (c ^ 1) - 1;
|
|
675
|
-
for (i = 0; i < 10; i++) g[i] &= mask;
|
|
676
|
-
mask = ~mask;
|
|
677
|
-
for (i = 0; i < 10; i++) this.h[i] = this.h[i] & mask | g[i];
|
|
678
|
-
this.h[0] = (this.h[0] | this.h[1] << 13) & 65535;
|
|
679
|
-
this.h[1] = (this.h[1] >>> 3 | this.h[2] << 10) & 65535;
|
|
680
|
-
this.h[2] = (this.h[2] >>> 6 | this.h[3] << 7) & 65535;
|
|
681
|
-
this.h[3] = (this.h[3] >>> 9 | this.h[4] << 4) & 65535;
|
|
682
|
-
this.h[4] = (this.h[4] >>> 12 | this.h[5] << 1 | this.h[6] << 14) & 65535;
|
|
683
|
-
this.h[5] = (this.h[6] >>> 2 | this.h[7] << 11) & 65535;
|
|
684
|
-
this.h[6] = (this.h[7] >>> 5 | this.h[8] << 8) & 65535;
|
|
685
|
-
this.h[7] = (this.h[8] >>> 8 | this.h[9] << 5) & 65535;
|
|
686
|
-
f = this.h[0] + this.pad[0];
|
|
687
|
-
this.h[0] = f & 65535;
|
|
688
|
-
for (i = 1; i < 8; i++) {
|
|
689
|
-
f = (this.h[i] + this.pad[i] | 0) + (f >>> 16) | 0;
|
|
690
|
-
this.h[i] = f & 65535;
|
|
691
|
-
}
|
|
692
|
-
mac[macpos + 0] = this.h[0] >>> 0 & 255;
|
|
693
|
-
mac[macpos + 1] = this.h[0] >>> 8 & 255;
|
|
694
|
-
mac[macpos + 2] = this.h[1] >>> 0 & 255;
|
|
695
|
-
mac[macpos + 3] = this.h[1] >>> 8 & 255;
|
|
696
|
-
mac[macpos + 4] = this.h[2] >>> 0 & 255;
|
|
697
|
-
mac[macpos + 5] = this.h[2] >>> 8 & 255;
|
|
698
|
-
mac[macpos + 6] = this.h[3] >>> 0 & 255;
|
|
699
|
-
mac[macpos + 7] = this.h[3] >>> 8 & 255;
|
|
700
|
-
mac[macpos + 8] = this.h[4] >>> 0 & 255;
|
|
701
|
-
mac[macpos + 9] = this.h[4] >>> 8 & 255;
|
|
702
|
-
mac[macpos + 10] = this.h[5] >>> 0 & 255;
|
|
703
|
-
mac[macpos + 11] = this.h[5] >>> 8 & 255;
|
|
704
|
-
mac[macpos + 12] = this.h[6] >>> 0 & 255;
|
|
705
|
-
mac[macpos + 13] = this.h[6] >>> 8 & 255;
|
|
706
|
-
mac[macpos + 14] = this.h[7] >>> 0 & 255;
|
|
707
|
-
mac[macpos + 15] = this.h[7] >>> 8 & 255;
|
|
708
|
-
};
|
|
709
|
-
poly1305.prototype.update = function(m, mpos, bytes) {
|
|
710
|
-
var i, want;
|
|
711
|
-
if (this.leftover) {
|
|
712
|
-
want = 16 - this.leftover;
|
|
713
|
-
if (want > bytes)
|
|
714
|
-
want = bytes;
|
|
715
|
-
for (i = 0; i < want; i++)
|
|
716
|
-
this.buffer[this.leftover + i] = m[mpos + i];
|
|
717
|
-
bytes -= want;
|
|
718
|
-
mpos += want;
|
|
719
|
-
this.leftover += want;
|
|
720
|
-
if (this.leftover < 16)
|
|
721
|
-
return;
|
|
722
|
-
this.blocks(this.buffer, 0, 16);
|
|
723
|
-
this.leftover = 0;
|
|
724
|
-
}
|
|
725
|
-
if (bytes >= 16) {
|
|
726
|
-
want = bytes - bytes % 16;
|
|
727
|
-
this.blocks(m, mpos, want);
|
|
728
|
-
mpos += want;
|
|
729
|
-
bytes -= want;
|
|
730
|
-
}
|
|
731
|
-
if (bytes) {
|
|
732
|
-
for (i = 0; i < bytes; i++)
|
|
733
|
-
this.buffer[this.leftover + i] = m[mpos + i];
|
|
734
|
-
this.leftover += bytes;
|
|
735
|
-
}
|
|
736
|
-
};
|
|
737
|
-
function crypto_onetimeauth(out, outpos, m, mpos, n, k) {
|
|
738
|
-
var s = new poly1305(k);
|
|
739
|
-
s.update(m, mpos, n);
|
|
740
|
-
s.finish(out, outpos);
|
|
741
|
-
return 0;
|
|
742
|
-
}
|
|
743
|
-
function crypto_onetimeauth_verify(h, hpos, m, mpos, n, k) {
|
|
744
|
-
var x = new Uint8Array(16);
|
|
745
|
-
crypto_onetimeauth(x, 0, m, mpos, n, k);
|
|
746
|
-
return crypto_verify_16(h, hpos, x, 0);
|
|
747
|
-
}
|
|
748
|
-
function crypto_secretbox(c, m, d, n, k) {
|
|
749
|
-
var i;
|
|
750
|
-
if (d < 32) return -1;
|
|
751
|
-
crypto_stream_xor(c, 0, m, 0, d, n, k);
|
|
752
|
-
crypto_onetimeauth(c, 16, c, 32, d - 32, c);
|
|
753
|
-
for (i = 0; i < 16; i++) c[i] = 0;
|
|
754
|
-
return 0;
|
|
755
|
-
}
|
|
756
|
-
function crypto_secretbox_open(m, c, d, n, k) {
|
|
757
|
-
var i;
|
|
758
|
-
var x = new Uint8Array(32);
|
|
759
|
-
if (d < 32) return -1;
|
|
760
|
-
crypto_stream(x, 0, 32, n, k);
|
|
761
|
-
if (crypto_onetimeauth_verify(c, 16, c, 32, d - 32, x) !== 0) return -1;
|
|
762
|
-
crypto_stream_xor(m, 0, c, 0, d, n, k);
|
|
763
|
-
for (i = 0; i < 32; i++) m[i] = 0;
|
|
764
|
-
return 0;
|
|
765
|
-
}
|
|
766
|
-
function set25519(r, a) {
|
|
767
|
-
var i;
|
|
768
|
-
for (i = 0; i < 16; i++) r[i] = a[i] | 0;
|
|
769
|
-
}
|
|
770
|
-
function car25519(o) {
|
|
771
|
-
var i, v, c = 1;
|
|
772
|
-
for (i = 0; i < 16; i++) {
|
|
773
|
-
v = o[i] + c + 65535;
|
|
774
|
-
c = Math.floor(v / 65536);
|
|
775
|
-
o[i] = v - c * 65536;
|
|
776
|
-
}
|
|
777
|
-
o[0] += c - 1 + 37 * (c - 1);
|
|
778
|
-
}
|
|
779
|
-
function sel25519(p, q, b) {
|
|
780
|
-
var t, c = ~(b - 1);
|
|
781
|
-
for (var i = 0; i < 16; i++) {
|
|
782
|
-
t = c & (p[i] ^ q[i]);
|
|
783
|
-
p[i] ^= t;
|
|
784
|
-
q[i] ^= t;
|
|
785
|
-
}
|
|
786
|
-
}
|
|
787
|
-
function pack25519(o, n) {
|
|
788
|
-
var i, j, b;
|
|
789
|
-
var m = gf(), t = gf();
|
|
790
|
-
for (i = 0; i < 16; i++) t[i] = n[i];
|
|
791
|
-
car25519(t);
|
|
792
|
-
car25519(t);
|
|
793
|
-
car25519(t);
|
|
794
|
-
for (j = 0; j < 2; j++) {
|
|
795
|
-
m[0] = t[0] - 65517;
|
|
796
|
-
for (i = 1; i < 15; i++) {
|
|
797
|
-
m[i] = t[i] - 65535 - (m[i - 1] >> 16 & 1);
|
|
798
|
-
m[i - 1] &= 65535;
|
|
799
|
-
}
|
|
800
|
-
m[15] = t[15] - 32767 - (m[14] >> 16 & 1);
|
|
801
|
-
b = m[15] >> 16 & 1;
|
|
802
|
-
m[14] &= 65535;
|
|
803
|
-
sel25519(t, m, 1 - b);
|
|
804
|
-
}
|
|
805
|
-
for (i = 0; i < 16; i++) {
|
|
806
|
-
o[2 * i] = t[i] & 255;
|
|
807
|
-
o[2 * i + 1] = t[i] >> 8;
|
|
808
|
-
}
|
|
809
|
-
}
|
|
810
|
-
function neq25519(a, b) {
|
|
811
|
-
var c = new Uint8Array(32), d = new Uint8Array(32);
|
|
812
|
-
pack25519(c, a);
|
|
813
|
-
pack25519(d, b);
|
|
814
|
-
return crypto_verify_32(c, 0, d, 0);
|
|
815
|
-
}
|
|
816
|
-
function par25519(a) {
|
|
817
|
-
var d = new Uint8Array(32);
|
|
818
|
-
pack25519(d, a);
|
|
819
|
-
return d[0] & 1;
|
|
820
|
-
}
|
|
821
|
-
function unpack25519(o, n) {
|
|
822
|
-
var i;
|
|
823
|
-
for (i = 0; i < 16; i++) o[i] = n[2 * i] + (n[2 * i + 1] << 8);
|
|
824
|
-
o[15] &= 32767;
|
|
825
|
-
}
|
|
826
|
-
function A(o, a, b) {
|
|
827
|
-
for (var i = 0; i < 16; i++) o[i] = a[i] + b[i];
|
|
828
|
-
}
|
|
829
|
-
function Z(o, a, b) {
|
|
830
|
-
for (var i = 0; i < 16; i++) o[i] = a[i] - b[i];
|
|
831
|
-
}
|
|
832
|
-
function M(o, a, b) {
|
|
833
|
-
var v, c, t0 = 0, t1 = 0, t2 = 0, t3 = 0, t4 = 0, t5 = 0, t6 = 0, t7 = 0, t8 = 0, t9 = 0, t10 = 0, t11 = 0, t12 = 0, t13 = 0, t14 = 0, t15 = 0, t16 = 0, t17 = 0, t18 = 0, t19 = 0, t20 = 0, t21 = 0, t22 = 0, t23 = 0, t24 = 0, t25 = 0, t26 = 0, t27 = 0, t28 = 0, t29 = 0, t30 = 0, b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3], b4 = b[4], b5 = b[5], b6 = b[6], b7 = b[7], b8 = b[8], b9 = b[9], b10 = b[10], b11 = b[11], b12 = b[12], b13 = b[13], b14 = b[14], b15 = b[15];
|
|
834
|
-
v = a[0];
|
|
835
|
-
t0 += v * b0;
|
|
836
|
-
t1 += v * b1;
|
|
837
|
-
t2 += v * b2;
|
|
838
|
-
t3 += v * b3;
|
|
839
|
-
t4 += v * b4;
|
|
840
|
-
t5 += v * b5;
|
|
841
|
-
t6 += v * b6;
|
|
842
|
-
t7 += v * b7;
|
|
843
|
-
t8 += v * b8;
|
|
844
|
-
t9 += v * b9;
|
|
845
|
-
t10 += v * b10;
|
|
846
|
-
t11 += v * b11;
|
|
847
|
-
t12 += v * b12;
|
|
848
|
-
t13 += v * b13;
|
|
849
|
-
t14 += v * b14;
|
|
850
|
-
t15 += v * b15;
|
|
851
|
-
v = a[1];
|
|
852
|
-
t1 += v * b0;
|
|
853
|
-
t2 += v * b1;
|
|
854
|
-
t3 += v * b2;
|
|
855
|
-
t4 += v * b3;
|
|
856
|
-
t5 += v * b4;
|
|
857
|
-
t6 += v * b5;
|
|
858
|
-
t7 += v * b6;
|
|
859
|
-
t8 += v * b7;
|
|
860
|
-
t9 += v * b8;
|
|
861
|
-
t10 += v * b9;
|
|
862
|
-
t11 += v * b10;
|
|
863
|
-
t12 += v * b11;
|
|
864
|
-
t13 += v * b12;
|
|
865
|
-
t14 += v * b13;
|
|
866
|
-
t15 += v * b14;
|
|
867
|
-
t16 += v * b15;
|
|
868
|
-
v = a[2];
|
|
869
|
-
t2 += v * b0;
|
|
870
|
-
t3 += v * b1;
|
|
871
|
-
t4 += v * b2;
|
|
872
|
-
t5 += v * b3;
|
|
873
|
-
t6 += v * b4;
|
|
874
|
-
t7 += v * b5;
|
|
875
|
-
t8 += v * b6;
|
|
876
|
-
t9 += v * b7;
|
|
877
|
-
t10 += v * b8;
|
|
878
|
-
t11 += v * b9;
|
|
879
|
-
t12 += v * b10;
|
|
880
|
-
t13 += v * b11;
|
|
881
|
-
t14 += v * b12;
|
|
882
|
-
t15 += v * b13;
|
|
883
|
-
t16 += v * b14;
|
|
884
|
-
t17 += v * b15;
|
|
885
|
-
v = a[3];
|
|
886
|
-
t3 += v * b0;
|
|
887
|
-
t4 += v * b1;
|
|
888
|
-
t5 += v * b2;
|
|
889
|
-
t6 += v * b3;
|
|
890
|
-
t7 += v * b4;
|
|
891
|
-
t8 += v * b5;
|
|
892
|
-
t9 += v * b6;
|
|
893
|
-
t10 += v * b7;
|
|
894
|
-
t11 += v * b8;
|
|
895
|
-
t12 += v * b9;
|
|
896
|
-
t13 += v * b10;
|
|
897
|
-
t14 += v * b11;
|
|
898
|
-
t15 += v * b12;
|
|
899
|
-
t16 += v * b13;
|
|
900
|
-
t17 += v * b14;
|
|
901
|
-
t18 += v * b15;
|
|
902
|
-
v = a[4];
|
|
903
|
-
t4 += v * b0;
|
|
904
|
-
t5 += v * b1;
|
|
905
|
-
t6 += v * b2;
|
|
906
|
-
t7 += v * b3;
|
|
907
|
-
t8 += v * b4;
|
|
908
|
-
t9 += v * b5;
|
|
909
|
-
t10 += v * b6;
|
|
910
|
-
t11 += v * b7;
|
|
911
|
-
t12 += v * b8;
|
|
912
|
-
t13 += v * b9;
|
|
913
|
-
t14 += v * b10;
|
|
914
|
-
t15 += v * b11;
|
|
915
|
-
t16 += v * b12;
|
|
916
|
-
t17 += v * b13;
|
|
917
|
-
t18 += v * b14;
|
|
918
|
-
t19 += v * b15;
|
|
919
|
-
v = a[5];
|
|
920
|
-
t5 += v * b0;
|
|
921
|
-
t6 += v * b1;
|
|
922
|
-
t7 += v * b2;
|
|
923
|
-
t8 += v * b3;
|
|
924
|
-
t9 += v * b4;
|
|
925
|
-
t10 += v * b5;
|
|
926
|
-
t11 += v * b6;
|
|
927
|
-
t12 += v * b7;
|
|
928
|
-
t13 += v * b8;
|
|
929
|
-
t14 += v * b9;
|
|
930
|
-
t15 += v * b10;
|
|
931
|
-
t16 += v * b11;
|
|
932
|
-
t17 += v * b12;
|
|
933
|
-
t18 += v * b13;
|
|
934
|
-
t19 += v * b14;
|
|
935
|
-
t20 += v * b15;
|
|
936
|
-
v = a[6];
|
|
937
|
-
t6 += v * b0;
|
|
938
|
-
t7 += v * b1;
|
|
939
|
-
t8 += v * b2;
|
|
940
|
-
t9 += v * b3;
|
|
941
|
-
t10 += v * b4;
|
|
942
|
-
t11 += v * b5;
|
|
943
|
-
t12 += v * b6;
|
|
944
|
-
t13 += v * b7;
|
|
945
|
-
t14 += v * b8;
|
|
946
|
-
t15 += v * b9;
|
|
947
|
-
t16 += v * b10;
|
|
948
|
-
t17 += v * b11;
|
|
949
|
-
t18 += v * b12;
|
|
950
|
-
t19 += v * b13;
|
|
951
|
-
t20 += v * b14;
|
|
952
|
-
t21 += v * b15;
|
|
953
|
-
v = a[7];
|
|
954
|
-
t7 += v * b0;
|
|
955
|
-
t8 += v * b1;
|
|
956
|
-
t9 += v * b2;
|
|
957
|
-
t10 += v * b3;
|
|
958
|
-
t11 += v * b4;
|
|
959
|
-
t12 += v * b5;
|
|
960
|
-
t13 += v * b6;
|
|
961
|
-
t14 += v * b7;
|
|
962
|
-
t15 += v * b8;
|
|
963
|
-
t16 += v * b9;
|
|
964
|
-
t17 += v * b10;
|
|
965
|
-
t18 += v * b11;
|
|
966
|
-
t19 += v * b12;
|
|
967
|
-
t20 += v * b13;
|
|
968
|
-
t21 += v * b14;
|
|
969
|
-
t22 += v * b15;
|
|
970
|
-
v = a[8];
|
|
971
|
-
t8 += v * b0;
|
|
972
|
-
t9 += v * b1;
|
|
973
|
-
t10 += v * b2;
|
|
974
|
-
t11 += v * b3;
|
|
975
|
-
t12 += v * b4;
|
|
976
|
-
t13 += v * b5;
|
|
977
|
-
t14 += v * b6;
|
|
978
|
-
t15 += v * b7;
|
|
979
|
-
t16 += v * b8;
|
|
980
|
-
t17 += v * b9;
|
|
981
|
-
t18 += v * b10;
|
|
982
|
-
t19 += v * b11;
|
|
983
|
-
t20 += v * b12;
|
|
984
|
-
t21 += v * b13;
|
|
985
|
-
t22 += v * b14;
|
|
986
|
-
t23 += v * b15;
|
|
987
|
-
v = a[9];
|
|
988
|
-
t9 += v * b0;
|
|
989
|
-
t10 += v * b1;
|
|
990
|
-
t11 += v * b2;
|
|
991
|
-
t12 += v * b3;
|
|
992
|
-
t13 += v * b4;
|
|
993
|
-
t14 += v * b5;
|
|
994
|
-
t15 += v * b6;
|
|
995
|
-
t16 += v * b7;
|
|
996
|
-
t17 += v * b8;
|
|
997
|
-
t18 += v * b9;
|
|
998
|
-
t19 += v * b10;
|
|
999
|
-
t20 += v * b11;
|
|
1000
|
-
t21 += v * b12;
|
|
1001
|
-
t22 += v * b13;
|
|
1002
|
-
t23 += v * b14;
|
|
1003
|
-
t24 += v * b15;
|
|
1004
|
-
v = a[10];
|
|
1005
|
-
t10 += v * b0;
|
|
1006
|
-
t11 += v * b1;
|
|
1007
|
-
t12 += v * b2;
|
|
1008
|
-
t13 += v * b3;
|
|
1009
|
-
t14 += v * b4;
|
|
1010
|
-
t15 += v * b5;
|
|
1011
|
-
t16 += v * b6;
|
|
1012
|
-
t17 += v * b7;
|
|
1013
|
-
t18 += v * b8;
|
|
1014
|
-
t19 += v * b9;
|
|
1015
|
-
t20 += v * b10;
|
|
1016
|
-
t21 += v * b11;
|
|
1017
|
-
t22 += v * b12;
|
|
1018
|
-
t23 += v * b13;
|
|
1019
|
-
t24 += v * b14;
|
|
1020
|
-
t25 += v * b15;
|
|
1021
|
-
v = a[11];
|
|
1022
|
-
t11 += v * b0;
|
|
1023
|
-
t12 += v * b1;
|
|
1024
|
-
t13 += v * b2;
|
|
1025
|
-
t14 += v * b3;
|
|
1026
|
-
t15 += v * b4;
|
|
1027
|
-
t16 += v * b5;
|
|
1028
|
-
t17 += v * b6;
|
|
1029
|
-
t18 += v * b7;
|
|
1030
|
-
t19 += v * b8;
|
|
1031
|
-
t20 += v * b9;
|
|
1032
|
-
t21 += v * b10;
|
|
1033
|
-
t22 += v * b11;
|
|
1034
|
-
t23 += v * b12;
|
|
1035
|
-
t24 += v * b13;
|
|
1036
|
-
t25 += v * b14;
|
|
1037
|
-
t26 += v * b15;
|
|
1038
|
-
v = a[12];
|
|
1039
|
-
t12 += v * b0;
|
|
1040
|
-
t13 += v * b1;
|
|
1041
|
-
t14 += v * b2;
|
|
1042
|
-
t15 += v * b3;
|
|
1043
|
-
t16 += v * b4;
|
|
1044
|
-
t17 += v * b5;
|
|
1045
|
-
t18 += v * b6;
|
|
1046
|
-
t19 += v * b7;
|
|
1047
|
-
t20 += v * b8;
|
|
1048
|
-
t21 += v * b9;
|
|
1049
|
-
t22 += v * b10;
|
|
1050
|
-
t23 += v * b11;
|
|
1051
|
-
t24 += v * b12;
|
|
1052
|
-
t25 += v * b13;
|
|
1053
|
-
t26 += v * b14;
|
|
1054
|
-
t27 += v * b15;
|
|
1055
|
-
v = a[13];
|
|
1056
|
-
t13 += v * b0;
|
|
1057
|
-
t14 += v * b1;
|
|
1058
|
-
t15 += v * b2;
|
|
1059
|
-
t16 += v * b3;
|
|
1060
|
-
t17 += v * b4;
|
|
1061
|
-
t18 += v * b5;
|
|
1062
|
-
t19 += v * b6;
|
|
1063
|
-
t20 += v * b7;
|
|
1064
|
-
t21 += v * b8;
|
|
1065
|
-
t22 += v * b9;
|
|
1066
|
-
t23 += v * b10;
|
|
1067
|
-
t24 += v * b11;
|
|
1068
|
-
t25 += v * b12;
|
|
1069
|
-
t26 += v * b13;
|
|
1070
|
-
t27 += v * b14;
|
|
1071
|
-
t28 += v * b15;
|
|
1072
|
-
v = a[14];
|
|
1073
|
-
t14 += v * b0;
|
|
1074
|
-
t15 += v * b1;
|
|
1075
|
-
t16 += v * b2;
|
|
1076
|
-
t17 += v * b3;
|
|
1077
|
-
t18 += v * b4;
|
|
1078
|
-
t19 += v * b5;
|
|
1079
|
-
t20 += v * b6;
|
|
1080
|
-
t21 += v * b7;
|
|
1081
|
-
t22 += v * b8;
|
|
1082
|
-
t23 += v * b9;
|
|
1083
|
-
t24 += v * b10;
|
|
1084
|
-
t25 += v * b11;
|
|
1085
|
-
t26 += v * b12;
|
|
1086
|
-
t27 += v * b13;
|
|
1087
|
-
t28 += v * b14;
|
|
1088
|
-
t29 += v * b15;
|
|
1089
|
-
v = a[15];
|
|
1090
|
-
t15 += v * b0;
|
|
1091
|
-
t16 += v * b1;
|
|
1092
|
-
t17 += v * b2;
|
|
1093
|
-
t18 += v * b3;
|
|
1094
|
-
t19 += v * b4;
|
|
1095
|
-
t20 += v * b5;
|
|
1096
|
-
t21 += v * b6;
|
|
1097
|
-
t22 += v * b7;
|
|
1098
|
-
t23 += v * b8;
|
|
1099
|
-
t24 += v * b9;
|
|
1100
|
-
t25 += v * b10;
|
|
1101
|
-
t26 += v * b11;
|
|
1102
|
-
t27 += v * b12;
|
|
1103
|
-
t28 += v * b13;
|
|
1104
|
-
t29 += v * b14;
|
|
1105
|
-
t30 += v * b15;
|
|
1106
|
-
t0 += 38 * t16;
|
|
1107
|
-
t1 += 38 * t17;
|
|
1108
|
-
t2 += 38 * t18;
|
|
1109
|
-
t3 += 38 * t19;
|
|
1110
|
-
t4 += 38 * t20;
|
|
1111
|
-
t5 += 38 * t21;
|
|
1112
|
-
t6 += 38 * t22;
|
|
1113
|
-
t7 += 38 * t23;
|
|
1114
|
-
t8 += 38 * t24;
|
|
1115
|
-
t9 += 38 * t25;
|
|
1116
|
-
t10 += 38 * t26;
|
|
1117
|
-
t11 += 38 * t27;
|
|
1118
|
-
t12 += 38 * t28;
|
|
1119
|
-
t13 += 38 * t29;
|
|
1120
|
-
t14 += 38 * t30;
|
|
1121
|
-
c = 1;
|
|
1122
|
-
v = t0 + c + 65535;
|
|
1123
|
-
c = Math.floor(v / 65536);
|
|
1124
|
-
t0 = v - c * 65536;
|
|
1125
|
-
v = t1 + c + 65535;
|
|
1126
|
-
c = Math.floor(v / 65536);
|
|
1127
|
-
t1 = v - c * 65536;
|
|
1128
|
-
v = t2 + c + 65535;
|
|
1129
|
-
c = Math.floor(v / 65536);
|
|
1130
|
-
t2 = v - c * 65536;
|
|
1131
|
-
v = t3 + c + 65535;
|
|
1132
|
-
c = Math.floor(v / 65536);
|
|
1133
|
-
t3 = v - c * 65536;
|
|
1134
|
-
v = t4 + c + 65535;
|
|
1135
|
-
c = Math.floor(v / 65536);
|
|
1136
|
-
t4 = v - c * 65536;
|
|
1137
|
-
v = t5 + c + 65535;
|
|
1138
|
-
c = Math.floor(v / 65536);
|
|
1139
|
-
t5 = v - c * 65536;
|
|
1140
|
-
v = t6 + c + 65535;
|
|
1141
|
-
c = Math.floor(v / 65536);
|
|
1142
|
-
t6 = v - c * 65536;
|
|
1143
|
-
v = t7 + c + 65535;
|
|
1144
|
-
c = Math.floor(v / 65536);
|
|
1145
|
-
t7 = v - c * 65536;
|
|
1146
|
-
v = t8 + c + 65535;
|
|
1147
|
-
c = Math.floor(v / 65536);
|
|
1148
|
-
t8 = v - c * 65536;
|
|
1149
|
-
v = t9 + c + 65535;
|
|
1150
|
-
c = Math.floor(v / 65536);
|
|
1151
|
-
t9 = v - c * 65536;
|
|
1152
|
-
v = t10 + c + 65535;
|
|
1153
|
-
c = Math.floor(v / 65536);
|
|
1154
|
-
t10 = v - c * 65536;
|
|
1155
|
-
v = t11 + c + 65535;
|
|
1156
|
-
c = Math.floor(v / 65536);
|
|
1157
|
-
t11 = v - c * 65536;
|
|
1158
|
-
v = t12 + c + 65535;
|
|
1159
|
-
c = Math.floor(v / 65536);
|
|
1160
|
-
t12 = v - c * 65536;
|
|
1161
|
-
v = t13 + c + 65535;
|
|
1162
|
-
c = Math.floor(v / 65536);
|
|
1163
|
-
t13 = v - c * 65536;
|
|
1164
|
-
v = t14 + c + 65535;
|
|
1165
|
-
c = Math.floor(v / 65536);
|
|
1166
|
-
t14 = v - c * 65536;
|
|
1167
|
-
v = t15 + c + 65535;
|
|
1168
|
-
c = Math.floor(v / 65536);
|
|
1169
|
-
t15 = v - c * 65536;
|
|
1170
|
-
t0 += c - 1 + 37 * (c - 1);
|
|
1171
|
-
c = 1;
|
|
1172
|
-
v = t0 + c + 65535;
|
|
1173
|
-
c = Math.floor(v / 65536);
|
|
1174
|
-
t0 = v - c * 65536;
|
|
1175
|
-
v = t1 + c + 65535;
|
|
1176
|
-
c = Math.floor(v / 65536);
|
|
1177
|
-
t1 = v - c * 65536;
|
|
1178
|
-
v = t2 + c + 65535;
|
|
1179
|
-
c = Math.floor(v / 65536);
|
|
1180
|
-
t2 = v - c * 65536;
|
|
1181
|
-
v = t3 + c + 65535;
|
|
1182
|
-
c = Math.floor(v / 65536);
|
|
1183
|
-
t3 = v - c * 65536;
|
|
1184
|
-
v = t4 + c + 65535;
|
|
1185
|
-
c = Math.floor(v / 65536);
|
|
1186
|
-
t4 = v - c * 65536;
|
|
1187
|
-
v = t5 + c + 65535;
|
|
1188
|
-
c = Math.floor(v / 65536);
|
|
1189
|
-
t5 = v - c * 65536;
|
|
1190
|
-
v = t6 + c + 65535;
|
|
1191
|
-
c = Math.floor(v / 65536);
|
|
1192
|
-
t6 = v - c * 65536;
|
|
1193
|
-
v = t7 + c + 65535;
|
|
1194
|
-
c = Math.floor(v / 65536);
|
|
1195
|
-
t7 = v - c * 65536;
|
|
1196
|
-
v = t8 + c + 65535;
|
|
1197
|
-
c = Math.floor(v / 65536);
|
|
1198
|
-
t8 = v - c * 65536;
|
|
1199
|
-
v = t9 + c + 65535;
|
|
1200
|
-
c = Math.floor(v / 65536);
|
|
1201
|
-
t9 = v - c * 65536;
|
|
1202
|
-
v = t10 + c + 65535;
|
|
1203
|
-
c = Math.floor(v / 65536);
|
|
1204
|
-
t10 = v - c * 65536;
|
|
1205
|
-
v = t11 + c + 65535;
|
|
1206
|
-
c = Math.floor(v / 65536);
|
|
1207
|
-
t11 = v - c * 65536;
|
|
1208
|
-
v = t12 + c + 65535;
|
|
1209
|
-
c = Math.floor(v / 65536);
|
|
1210
|
-
t12 = v - c * 65536;
|
|
1211
|
-
v = t13 + c + 65535;
|
|
1212
|
-
c = Math.floor(v / 65536);
|
|
1213
|
-
t13 = v - c * 65536;
|
|
1214
|
-
v = t14 + c + 65535;
|
|
1215
|
-
c = Math.floor(v / 65536);
|
|
1216
|
-
t14 = v - c * 65536;
|
|
1217
|
-
v = t15 + c + 65535;
|
|
1218
|
-
c = Math.floor(v / 65536);
|
|
1219
|
-
t15 = v - c * 65536;
|
|
1220
|
-
t0 += c - 1 + 37 * (c - 1);
|
|
1221
|
-
o[0] = t0;
|
|
1222
|
-
o[1] = t1;
|
|
1223
|
-
o[2] = t2;
|
|
1224
|
-
o[3] = t3;
|
|
1225
|
-
o[4] = t4;
|
|
1226
|
-
o[5] = t5;
|
|
1227
|
-
o[6] = t6;
|
|
1228
|
-
o[7] = t7;
|
|
1229
|
-
o[8] = t8;
|
|
1230
|
-
o[9] = t9;
|
|
1231
|
-
o[10] = t10;
|
|
1232
|
-
o[11] = t11;
|
|
1233
|
-
o[12] = t12;
|
|
1234
|
-
o[13] = t13;
|
|
1235
|
-
o[14] = t14;
|
|
1236
|
-
o[15] = t15;
|
|
1237
|
-
}
|
|
1238
|
-
function S(o, a) {
|
|
1239
|
-
M(o, a, a);
|
|
1240
|
-
}
|
|
1241
|
-
function inv25519(o, i) {
|
|
1242
|
-
var c = gf();
|
|
1243
|
-
var a;
|
|
1244
|
-
for (a = 0; a < 16; a++) c[a] = i[a];
|
|
1245
|
-
for (a = 253; a >= 0; a--) {
|
|
1246
|
-
S(c, c);
|
|
1247
|
-
if (a !== 2 && a !== 4) M(c, c, i);
|
|
1248
|
-
}
|
|
1249
|
-
for (a = 0; a < 16; a++) o[a] = c[a];
|
|
1250
|
-
}
|
|
1251
|
-
function pow2523(o, i) {
|
|
1252
|
-
var c = gf();
|
|
1253
|
-
var a;
|
|
1254
|
-
for (a = 0; a < 16; a++) c[a] = i[a];
|
|
1255
|
-
for (a = 250; a >= 0; a--) {
|
|
1256
|
-
S(c, c);
|
|
1257
|
-
if (a !== 1) M(c, c, i);
|
|
1258
|
-
}
|
|
1259
|
-
for (a = 0; a < 16; a++) o[a] = c[a];
|
|
1260
|
-
}
|
|
1261
|
-
function crypto_scalarmult(q, n, p) {
|
|
1262
|
-
var z = new Uint8Array(32);
|
|
1263
|
-
var x = new Float64Array(80), r, i;
|
|
1264
|
-
var a = gf(), b = gf(), c = gf(), d = gf(), e = gf(), f = gf();
|
|
1265
|
-
for (i = 0; i < 31; i++) z[i] = n[i];
|
|
1266
|
-
z[31] = n[31] & 127 | 64;
|
|
1267
|
-
z[0] &= 248;
|
|
1268
|
-
unpack25519(x, p);
|
|
1269
|
-
for (i = 0; i < 16; i++) {
|
|
1270
|
-
b[i] = x[i];
|
|
1271
|
-
d[i] = a[i] = c[i] = 0;
|
|
1272
|
-
}
|
|
1273
|
-
a[0] = d[0] = 1;
|
|
1274
|
-
for (i = 254; i >= 0; --i) {
|
|
1275
|
-
r = z[i >>> 3] >>> (i & 7) & 1;
|
|
1276
|
-
sel25519(a, b, r);
|
|
1277
|
-
sel25519(c, d, r);
|
|
1278
|
-
A(e, a, c);
|
|
1279
|
-
Z(a, a, c);
|
|
1280
|
-
A(c, b, d);
|
|
1281
|
-
Z(b, b, d);
|
|
1282
|
-
S(d, e);
|
|
1283
|
-
S(f, a);
|
|
1284
|
-
M(a, c, a);
|
|
1285
|
-
M(c, b, e);
|
|
1286
|
-
A(e, a, c);
|
|
1287
|
-
Z(a, a, c);
|
|
1288
|
-
S(b, a);
|
|
1289
|
-
Z(c, d, f);
|
|
1290
|
-
M(a, c, _121665);
|
|
1291
|
-
A(a, a, d);
|
|
1292
|
-
M(c, c, a);
|
|
1293
|
-
M(a, d, f);
|
|
1294
|
-
M(d, b, x);
|
|
1295
|
-
S(b, e);
|
|
1296
|
-
sel25519(a, b, r);
|
|
1297
|
-
sel25519(c, d, r);
|
|
1298
|
-
}
|
|
1299
|
-
for (i = 0; i < 16; i++) {
|
|
1300
|
-
x[i + 16] = a[i];
|
|
1301
|
-
x[i + 32] = c[i];
|
|
1302
|
-
x[i + 48] = b[i];
|
|
1303
|
-
x[i + 64] = d[i];
|
|
1304
|
-
}
|
|
1305
|
-
var x32 = x.subarray(32);
|
|
1306
|
-
var x16 = x.subarray(16);
|
|
1307
|
-
inv25519(x32, x32);
|
|
1308
|
-
M(x16, x16, x32);
|
|
1309
|
-
pack25519(q, x16);
|
|
1310
|
-
return 0;
|
|
1311
|
-
}
|
|
1312
|
-
function crypto_scalarmult_base(q, n) {
|
|
1313
|
-
return crypto_scalarmult(q, n, _9);
|
|
1314
|
-
}
|
|
1315
|
-
function crypto_box_keypair(y, x) {
|
|
1316
|
-
randombytes(x, 32);
|
|
1317
|
-
return crypto_scalarmult_base(y, x);
|
|
1318
|
-
}
|
|
1319
|
-
function crypto_box_beforenm(k, y, x) {
|
|
1320
|
-
var s = new Uint8Array(32);
|
|
1321
|
-
crypto_scalarmult(s, x, y);
|
|
1322
|
-
return crypto_core_hsalsa20(k, _0, s, sigma);
|
|
1323
|
-
}
|
|
1324
|
-
var crypto_box_afternm = crypto_secretbox;
|
|
1325
|
-
var crypto_box_open_afternm = crypto_secretbox_open;
|
|
1326
|
-
function crypto_box(c, m, d, n, y, x) {
|
|
1327
|
-
var k = new Uint8Array(32);
|
|
1328
|
-
crypto_box_beforenm(k, y, x);
|
|
1329
|
-
return crypto_box_afternm(c, m, d, n, k);
|
|
1330
|
-
}
|
|
1331
|
-
function crypto_box_open(m, c, d, n, y, x) {
|
|
1332
|
-
var k = new Uint8Array(32);
|
|
1333
|
-
crypto_box_beforenm(k, y, x);
|
|
1334
|
-
return crypto_box_open_afternm(m, c, d, n, k);
|
|
1335
|
-
}
|
|
1336
|
-
var K = [
|
|
1337
|
-
1116352408,
|
|
1338
|
-
3609767458,
|
|
1339
|
-
1899447441,
|
|
1340
|
-
602891725,
|
|
1341
|
-
3049323471,
|
|
1342
|
-
3964484399,
|
|
1343
|
-
3921009573,
|
|
1344
|
-
2173295548,
|
|
1345
|
-
961987163,
|
|
1346
|
-
4081628472,
|
|
1347
|
-
1508970993,
|
|
1348
|
-
3053834265,
|
|
1349
|
-
2453635748,
|
|
1350
|
-
2937671579,
|
|
1351
|
-
2870763221,
|
|
1352
|
-
3664609560,
|
|
1353
|
-
3624381080,
|
|
1354
|
-
2734883394,
|
|
1355
|
-
310598401,
|
|
1356
|
-
1164996542,
|
|
1357
|
-
607225278,
|
|
1358
|
-
1323610764,
|
|
1359
|
-
1426881987,
|
|
1360
|
-
3590304994,
|
|
1361
|
-
1925078388,
|
|
1362
|
-
4068182383,
|
|
1363
|
-
2162078206,
|
|
1364
|
-
991336113,
|
|
1365
|
-
2614888103,
|
|
1366
|
-
633803317,
|
|
1367
|
-
3248222580,
|
|
1368
|
-
3479774868,
|
|
1369
|
-
3835390401,
|
|
1370
|
-
2666613458,
|
|
1371
|
-
4022224774,
|
|
1372
|
-
944711139,
|
|
1373
|
-
264347078,
|
|
1374
|
-
2341262773,
|
|
1375
|
-
604807628,
|
|
1376
|
-
2007800933,
|
|
1377
|
-
770255983,
|
|
1378
|
-
1495990901,
|
|
1379
|
-
1249150122,
|
|
1380
|
-
1856431235,
|
|
1381
|
-
1555081692,
|
|
1382
|
-
3175218132,
|
|
1383
|
-
1996064986,
|
|
1384
|
-
2198950837,
|
|
1385
|
-
2554220882,
|
|
1386
|
-
3999719339,
|
|
1387
|
-
2821834349,
|
|
1388
|
-
766784016,
|
|
1389
|
-
2952996808,
|
|
1390
|
-
2566594879,
|
|
1391
|
-
3210313671,
|
|
1392
|
-
3203337956,
|
|
1393
|
-
3336571891,
|
|
1394
|
-
1034457026,
|
|
1395
|
-
3584528711,
|
|
1396
|
-
2466948901,
|
|
1397
|
-
113926993,
|
|
1398
|
-
3758326383,
|
|
1399
|
-
338241895,
|
|
1400
|
-
168717936,
|
|
1401
|
-
666307205,
|
|
1402
|
-
1188179964,
|
|
1403
|
-
773529912,
|
|
1404
|
-
1546045734,
|
|
1405
|
-
1294757372,
|
|
1406
|
-
1522805485,
|
|
1407
|
-
1396182291,
|
|
1408
|
-
2643833823,
|
|
1409
|
-
1695183700,
|
|
1410
|
-
2343527390,
|
|
1411
|
-
1986661051,
|
|
1412
|
-
1014477480,
|
|
1413
|
-
2177026350,
|
|
1414
|
-
1206759142,
|
|
1415
|
-
2456956037,
|
|
1416
|
-
344077627,
|
|
1417
|
-
2730485921,
|
|
1418
|
-
1290863460,
|
|
1419
|
-
2820302411,
|
|
1420
|
-
3158454273,
|
|
1421
|
-
3259730800,
|
|
1422
|
-
3505952657,
|
|
1423
|
-
3345764771,
|
|
1424
|
-
106217008,
|
|
1425
|
-
3516065817,
|
|
1426
|
-
3606008344,
|
|
1427
|
-
3600352804,
|
|
1428
|
-
1432725776,
|
|
1429
|
-
4094571909,
|
|
1430
|
-
1467031594,
|
|
1431
|
-
275423344,
|
|
1432
|
-
851169720,
|
|
1433
|
-
430227734,
|
|
1434
|
-
3100823752,
|
|
1435
|
-
506948616,
|
|
1436
|
-
1363258195,
|
|
1437
|
-
659060556,
|
|
1438
|
-
3750685593,
|
|
1439
|
-
883997877,
|
|
1440
|
-
3785050280,
|
|
1441
|
-
958139571,
|
|
1442
|
-
3318307427,
|
|
1443
|
-
1322822218,
|
|
1444
|
-
3812723403,
|
|
1445
|
-
1537002063,
|
|
1446
|
-
2003034995,
|
|
1447
|
-
1747873779,
|
|
1448
|
-
3602036899,
|
|
1449
|
-
1955562222,
|
|
1450
|
-
1575990012,
|
|
1451
|
-
2024104815,
|
|
1452
|
-
1125592928,
|
|
1453
|
-
2227730452,
|
|
1454
|
-
2716904306,
|
|
1455
|
-
2361852424,
|
|
1456
|
-
442776044,
|
|
1457
|
-
2428436474,
|
|
1458
|
-
593698344,
|
|
1459
|
-
2756734187,
|
|
1460
|
-
3733110249,
|
|
1461
|
-
3204031479,
|
|
1462
|
-
2999351573,
|
|
1463
|
-
3329325298,
|
|
1464
|
-
3815920427,
|
|
1465
|
-
3391569614,
|
|
1466
|
-
3928383900,
|
|
1467
|
-
3515267271,
|
|
1468
|
-
566280711,
|
|
1469
|
-
3940187606,
|
|
1470
|
-
3454069534,
|
|
1471
|
-
4118630271,
|
|
1472
|
-
4000239992,
|
|
1473
|
-
116418474,
|
|
1474
|
-
1914138554,
|
|
1475
|
-
174292421,
|
|
1476
|
-
2731055270,
|
|
1477
|
-
289380356,
|
|
1478
|
-
3203993006,
|
|
1479
|
-
460393269,
|
|
1480
|
-
320620315,
|
|
1481
|
-
685471733,
|
|
1482
|
-
587496836,
|
|
1483
|
-
852142971,
|
|
1484
|
-
1086792851,
|
|
1485
|
-
1017036298,
|
|
1486
|
-
365543100,
|
|
1487
|
-
1126000580,
|
|
1488
|
-
2618297676,
|
|
1489
|
-
1288033470,
|
|
1490
|
-
3409855158,
|
|
1491
|
-
1501505948,
|
|
1492
|
-
4234509866,
|
|
1493
|
-
1607167915,
|
|
1494
|
-
987167468,
|
|
1495
|
-
1816402316,
|
|
1496
|
-
1246189591
|
|
1497
|
-
];
|
|
1498
|
-
function crypto_hashblocks_hl(hh, hl, m, n) {
|
|
1499
|
-
var wh = new Int32Array(16), wl = new Int32Array(16), bh0, bh1, bh2, bh3, bh4, bh5, bh6, bh7, bl0, bl1, bl2, bl3, bl4, bl5, bl6, bl7, th, tl, i, j, h, l, a, b, c, d;
|
|
1500
|
-
var ah0 = hh[0], ah1 = hh[1], ah2 = hh[2], ah3 = hh[3], ah4 = hh[4], ah5 = hh[5], ah6 = hh[6], ah7 = hh[7], al0 = hl[0], al1 = hl[1], al2 = hl[2], al3 = hl[3], al4 = hl[4], al5 = hl[5], al6 = hl[6], al7 = hl[7];
|
|
1501
|
-
var pos = 0;
|
|
1502
|
-
while (n >= 128) {
|
|
1503
|
-
for (i = 0; i < 16; i++) {
|
|
1504
|
-
j = 8 * i + pos;
|
|
1505
|
-
wh[i] = m[j + 0] << 24 | m[j + 1] << 16 | m[j + 2] << 8 | m[j + 3];
|
|
1506
|
-
wl[i] = m[j + 4] << 24 | m[j + 5] << 16 | m[j + 6] << 8 | m[j + 7];
|
|
1507
|
-
}
|
|
1508
|
-
for (i = 0; i < 80; i++) {
|
|
1509
|
-
bh0 = ah0;
|
|
1510
|
-
bh1 = ah1;
|
|
1511
|
-
bh2 = ah2;
|
|
1512
|
-
bh3 = ah3;
|
|
1513
|
-
bh4 = ah4;
|
|
1514
|
-
bh5 = ah5;
|
|
1515
|
-
bh6 = ah6;
|
|
1516
|
-
bh7 = ah7;
|
|
1517
|
-
bl0 = al0;
|
|
1518
|
-
bl1 = al1;
|
|
1519
|
-
bl2 = al2;
|
|
1520
|
-
bl3 = al3;
|
|
1521
|
-
bl4 = al4;
|
|
1522
|
-
bl5 = al5;
|
|
1523
|
-
bl6 = al6;
|
|
1524
|
-
bl7 = al7;
|
|
1525
|
-
h = ah7;
|
|
1526
|
-
l = al7;
|
|
1527
|
-
a = l & 65535;
|
|
1528
|
-
b = l >>> 16;
|
|
1529
|
-
c = h & 65535;
|
|
1530
|
-
d = h >>> 16;
|
|
1531
|
-
h = (ah4 >>> 14 | al4 << 32 - 14) ^ (ah4 >>> 18 | al4 << 32 - 18) ^ (al4 >>> 41 - 32 | ah4 << 32 - (41 - 32));
|
|
1532
|
-
l = (al4 >>> 14 | ah4 << 32 - 14) ^ (al4 >>> 18 | ah4 << 32 - 18) ^ (ah4 >>> 41 - 32 | al4 << 32 - (41 - 32));
|
|
1533
|
-
a += l & 65535;
|
|
1534
|
-
b += l >>> 16;
|
|
1535
|
-
c += h & 65535;
|
|
1536
|
-
d += h >>> 16;
|
|
1537
|
-
h = ah4 & ah5 ^ ~ah4 & ah6;
|
|
1538
|
-
l = al4 & al5 ^ ~al4 & al6;
|
|
1539
|
-
a += l & 65535;
|
|
1540
|
-
b += l >>> 16;
|
|
1541
|
-
c += h & 65535;
|
|
1542
|
-
d += h >>> 16;
|
|
1543
|
-
h = K[i * 2];
|
|
1544
|
-
l = K[i * 2 + 1];
|
|
1545
|
-
a += l & 65535;
|
|
1546
|
-
b += l >>> 16;
|
|
1547
|
-
c += h & 65535;
|
|
1548
|
-
d += h >>> 16;
|
|
1549
|
-
h = wh[i % 16];
|
|
1550
|
-
l = wl[i % 16];
|
|
1551
|
-
a += l & 65535;
|
|
1552
|
-
b += l >>> 16;
|
|
1553
|
-
c += h & 65535;
|
|
1554
|
-
d += h >>> 16;
|
|
1555
|
-
b += a >>> 16;
|
|
1556
|
-
c += b >>> 16;
|
|
1557
|
-
d += c >>> 16;
|
|
1558
|
-
th = c & 65535 | d << 16;
|
|
1559
|
-
tl = a & 65535 | b << 16;
|
|
1560
|
-
h = th;
|
|
1561
|
-
l = tl;
|
|
1562
|
-
a = l & 65535;
|
|
1563
|
-
b = l >>> 16;
|
|
1564
|
-
c = h & 65535;
|
|
1565
|
-
d = h >>> 16;
|
|
1566
|
-
h = (ah0 >>> 28 | al0 << 32 - 28) ^ (al0 >>> 34 - 32 | ah0 << 32 - (34 - 32)) ^ (al0 >>> 39 - 32 | ah0 << 32 - (39 - 32));
|
|
1567
|
-
l = (al0 >>> 28 | ah0 << 32 - 28) ^ (ah0 >>> 34 - 32 | al0 << 32 - (34 - 32)) ^ (ah0 >>> 39 - 32 | al0 << 32 - (39 - 32));
|
|
1568
|
-
a += l & 65535;
|
|
1569
|
-
b += l >>> 16;
|
|
1570
|
-
c += h & 65535;
|
|
1571
|
-
d += h >>> 16;
|
|
1572
|
-
h = ah0 & ah1 ^ ah0 & ah2 ^ ah1 & ah2;
|
|
1573
|
-
l = al0 & al1 ^ al0 & al2 ^ al1 & al2;
|
|
1574
|
-
a += l & 65535;
|
|
1575
|
-
b += l >>> 16;
|
|
1576
|
-
c += h & 65535;
|
|
1577
|
-
d += h >>> 16;
|
|
1578
|
-
b += a >>> 16;
|
|
1579
|
-
c += b >>> 16;
|
|
1580
|
-
d += c >>> 16;
|
|
1581
|
-
bh7 = c & 65535 | d << 16;
|
|
1582
|
-
bl7 = a & 65535 | b << 16;
|
|
1583
|
-
h = bh3;
|
|
1584
|
-
l = bl3;
|
|
1585
|
-
a = l & 65535;
|
|
1586
|
-
b = l >>> 16;
|
|
1587
|
-
c = h & 65535;
|
|
1588
|
-
d = h >>> 16;
|
|
1589
|
-
h = th;
|
|
1590
|
-
l = tl;
|
|
1591
|
-
a += l & 65535;
|
|
1592
|
-
b += l >>> 16;
|
|
1593
|
-
c += h & 65535;
|
|
1594
|
-
d += h >>> 16;
|
|
1595
|
-
b += a >>> 16;
|
|
1596
|
-
c += b >>> 16;
|
|
1597
|
-
d += c >>> 16;
|
|
1598
|
-
bh3 = c & 65535 | d << 16;
|
|
1599
|
-
bl3 = a & 65535 | b << 16;
|
|
1600
|
-
ah1 = bh0;
|
|
1601
|
-
ah2 = bh1;
|
|
1602
|
-
ah3 = bh2;
|
|
1603
|
-
ah4 = bh3;
|
|
1604
|
-
ah5 = bh4;
|
|
1605
|
-
ah6 = bh5;
|
|
1606
|
-
ah7 = bh6;
|
|
1607
|
-
ah0 = bh7;
|
|
1608
|
-
al1 = bl0;
|
|
1609
|
-
al2 = bl1;
|
|
1610
|
-
al3 = bl2;
|
|
1611
|
-
al4 = bl3;
|
|
1612
|
-
al5 = bl4;
|
|
1613
|
-
al6 = bl5;
|
|
1614
|
-
al7 = bl6;
|
|
1615
|
-
al0 = bl7;
|
|
1616
|
-
if (i % 16 === 15) {
|
|
1617
|
-
for (j = 0; j < 16; j++) {
|
|
1618
|
-
h = wh[j];
|
|
1619
|
-
l = wl[j];
|
|
1620
|
-
a = l & 65535;
|
|
1621
|
-
b = l >>> 16;
|
|
1622
|
-
c = h & 65535;
|
|
1623
|
-
d = h >>> 16;
|
|
1624
|
-
h = wh[(j + 9) % 16];
|
|
1625
|
-
l = wl[(j + 9) % 16];
|
|
1626
|
-
a += l & 65535;
|
|
1627
|
-
b += l >>> 16;
|
|
1628
|
-
c += h & 65535;
|
|
1629
|
-
d += h >>> 16;
|
|
1630
|
-
th = wh[(j + 1) % 16];
|
|
1631
|
-
tl = wl[(j + 1) % 16];
|
|
1632
|
-
h = (th >>> 1 | tl << 32 - 1) ^ (th >>> 8 | tl << 32 - 8) ^ th >>> 7;
|
|
1633
|
-
l = (tl >>> 1 | th << 32 - 1) ^ (tl >>> 8 | th << 32 - 8) ^ (tl >>> 7 | th << 32 - 7);
|
|
1634
|
-
a += l & 65535;
|
|
1635
|
-
b += l >>> 16;
|
|
1636
|
-
c += h & 65535;
|
|
1637
|
-
d += h >>> 16;
|
|
1638
|
-
th = wh[(j + 14) % 16];
|
|
1639
|
-
tl = wl[(j + 14) % 16];
|
|
1640
|
-
h = (th >>> 19 | tl << 32 - 19) ^ (tl >>> 61 - 32 | th << 32 - (61 - 32)) ^ th >>> 6;
|
|
1641
|
-
l = (tl >>> 19 | th << 32 - 19) ^ (th >>> 61 - 32 | tl << 32 - (61 - 32)) ^ (tl >>> 6 | th << 32 - 6);
|
|
1642
|
-
a += l & 65535;
|
|
1643
|
-
b += l >>> 16;
|
|
1644
|
-
c += h & 65535;
|
|
1645
|
-
d += h >>> 16;
|
|
1646
|
-
b += a >>> 16;
|
|
1647
|
-
c += b >>> 16;
|
|
1648
|
-
d += c >>> 16;
|
|
1649
|
-
wh[j] = c & 65535 | d << 16;
|
|
1650
|
-
wl[j] = a & 65535 | b << 16;
|
|
1651
|
-
}
|
|
1652
|
-
}
|
|
1653
|
-
}
|
|
1654
|
-
h = ah0;
|
|
1655
|
-
l = al0;
|
|
1656
|
-
a = l & 65535;
|
|
1657
|
-
b = l >>> 16;
|
|
1658
|
-
c = h & 65535;
|
|
1659
|
-
d = h >>> 16;
|
|
1660
|
-
h = hh[0];
|
|
1661
|
-
l = hl[0];
|
|
1662
|
-
a += l & 65535;
|
|
1663
|
-
b += l >>> 16;
|
|
1664
|
-
c += h & 65535;
|
|
1665
|
-
d += h >>> 16;
|
|
1666
|
-
b += a >>> 16;
|
|
1667
|
-
c += b >>> 16;
|
|
1668
|
-
d += c >>> 16;
|
|
1669
|
-
hh[0] = ah0 = c & 65535 | d << 16;
|
|
1670
|
-
hl[0] = al0 = a & 65535 | b << 16;
|
|
1671
|
-
h = ah1;
|
|
1672
|
-
l = al1;
|
|
1673
|
-
a = l & 65535;
|
|
1674
|
-
b = l >>> 16;
|
|
1675
|
-
c = h & 65535;
|
|
1676
|
-
d = h >>> 16;
|
|
1677
|
-
h = hh[1];
|
|
1678
|
-
l = hl[1];
|
|
1679
|
-
a += l & 65535;
|
|
1680
|
-
b += l >>> 16;
|
|
1681
|
-
c += h & 65535;
|
|
1682
|
-
d += h >>> 16;
|
|
1683
|
-
b += a >>> 16;
|
|
1684
|
-
c += b >>> 16;
|
|
1685
|
-
d += c >>> 16;
|
|
1686
|
-
hh[1] = ah1 = c & 65535 | d << 16;
|
|
1687
|
-
hl[1] = al1 = a & 65535 | b << 16;
|
|
1688
|
-
h = ah2;
|
|
1689
|
-
l = al2;
|
|
1690
|
-
a = l & 65535;
|
|
1691
|
-
b = l >>> 16;
|
|
1692
|
-
c = h & 65535;
|
|
1693
|
-
d = h >>> 16;
|
|
1694
|
-
h = hh[2];
|
|
1695
|
-
l = hl[2];
|
|
1696
|
-
a += l & 65535;
|
|
1697
|
-
b += l >>> 16;
|
|
1698
|
-
c += h & 65535;
|
|
1699
|
-
d += h >>> 16;
|
|
1700
|
-
b += a >>> 16;
|
|
1701
|
-
c += b >>> 16;
|
|
1702
|
-
d += c >>> 16;
|
|
1703
|
-
hh[2] = ah2 = c & 65535 | d << 16;
|
|
1704
|
-
hl[2] = al2 = a & 65535 | b << 16;
|
|
1705
|
-
h = ah3;
|
|
1706
|
-
l = al3;
|
|
1707
|
-
a = l & 65535;
|
|
1708
|
-
b = l >>> 16;
|
|
1709
|
-
c = h & 65535;
|
|
1710
|
-
d = h >>> 16;
|
|
1711
|
-
h = hh[3];
|
|
1712
|
-
l = hl[3];
|
|
1713
|
-
a += l & 65535;
|
|
1714
|
-
b += l >>> 16;
|
|
1715
|
-
c += h & 65535;
|
|
1716
|
-
d += h >>> 16;
|
|
1717
|
-
b += a >>> 16;
|
|
1718
|
-
c += b >>> 16;
|
|
1719
|
-
d += c >>> 16;
|
|
1720
|
-
hh[3] = ah3 = c & 65535 | d << 16;
|
|
1721
|
-
hl[3] = al3 = a & 65535 | b << 16;
|
|
1722
|
-
h = ah4;
|
|
1723
|
-
l = al4;
|
|
1724
|
-
a = l & 65535;
|
|
1725
|
-
b = l >>> 16;
|
|
1726
|
-
c = h & 65535;
|
|
1727
|
-
d = h >>> 16;
|
|
1728
|
-
h = hh[4];
|
|
1729
|
-
l = hl[4];
|
|
1730
|
-
a += l & 65535;
|
|
1731
|
-
b += l >>> 16;
|
|
1732
|
-
c += h & 65535;
|
|
1733
|
-
d += h >>> 16;
|
|
1734
|
-
b += a >>> 16;
|
|
1735
|
-
c += b >>> 16;
|
|
1736
|
-
d += c >>> 16;
|
|
1737
|
-
hh[4] = ah4 = c & 65535 | d << 16;
|
|
1738
|
-
hl[4] = al4 = a & 65535 | b << 16;
|
|
1739
|
-
h = ah5;
|
|
1740
|
-
l = al5;
|
|
1741
|
-
a = l & 65535;
|
|
1742
|
-
b = l >>> 16;
|
|
1743
|
-
c = h & 65535;
|
|
1744
|
-
d = h >>> 16;
|
|
1745
|
-
h = hh[5];
|
|
1746
|
-
l = hl[5];
|
|
1747
|
-
a += l & 65535;
|
|
1748
|
-
b += l >>> 16;
|
|
1749
|
-
c += h & 65535;
|
|
1750
|
-
d += h >>> 16;
|
|
1751
|
-
b += a >>> 16;
|
|
1752
|
-
c += b >>> 16;
|
|
1753
|
-
d += c >>> 16;
|
|
1754
|
-
hh[5] = ah5 = c & 65535 | d << 16;
|
|
1755
|
-
hl[5] = al5 = a & 65535 | b << 16;
|
|
1756
|
-
h = ah6;
|
|
1757
|
-
l = al6;
|
|
1758
|
-
a = l & 65535;
|
|
1759
|
-
b = l >>> 16;
|
|
1760
|
-
c = h & 65535;
|
|
1761
|
-
d = h >>> 16;
|
|
1762
|
-
h = hh[6];
|
|
1763
|
-
l = hl[6];
|
|
1764
|
-
a += l & 65535;
|
|
1765
|
-
b += l >>> 16;
|
|
1766
|
-
c += h & 65535;
|
|
1767
|
-
d += h >>> 16;
|
|
1768
|
-
b += a >>> 16;
|
|
1769
|
-
c += b >>> 16;
|
|
1770
|
-
d += c >>> 16;
|
|
1771
|
-
hh[6] = ah6 = c & 65535 | d << 16;
|
|
1772
|
-
hl[6] = al6 = a & 65535 | b << 16;
|
|
1773
|
-
h = ah7;
|
|
1774
|
-
l = al7;
|
|
1775
|
-
a = l & 65535;
|
|
1776
|
-
b = l >>> 16;
|
|
1777
|
-
c = h & 65535;
|
|
1778
|
-
d = h >>> 16;
|
|
1779
|
-
h = hh[7];
|
|
1780
|
-
l = hl[7];
|
|
1781
|
-
a += l & 65535;
|
|
1782
|
-
b += l >>> 16;
|
|
1783
|
-
c += h & 65535;
|
|
1784
|
-
d += h >>> 16;
|
|
1785
|
-
b += a >>> 16;
|
|
1786
|
-
c += b >>> 16;
|
|
1787
|
-
d += c >>> 16;
|
|
1788
|
-
hh[7] = ah7 = c & 65535 | d << 16;
|
|
1789
|
-
hl[7] = al7 = a & 65535 | b << 16;
|
|
1790
|
-
pos += 128;
|
|
1791
|
-
n -= 128;
|
|
1792
|
-
}
|
|
1793
|
-
return n;
|
|
1794
|
-
}
|
|
1795
|
-
function crypto_hash(out, m, n) {
|
|
1796
|
-
var hh = new Int32Array(8), hl = new Int32Array(8), x = new Uint8Array(256), i, b = n;
|
|
1797
|
-
hh[0] = 1779033703;
|
|
1798
|
-
hh[1] = 3144134277;
|
|
1799
|
-
hh[2] = 1013904242;
|
|
1800
|
-
hh[3] = 2773480762;
|
|
1801
|
-
hh[4] = 1359893119;
|
|
1802
|
-
hh[5] = 2600822924;
|
|
1803
|
-
hh[6] = 528734635;
|
|
1804
|
-
hh[7] = 1541459225;
|
|
1805
|
-
hl[0] = 4089235720;
|
|
1806
|
-
hl[1] = 2227873595;
|
|
1807
|
-
hl[2] = 4271175723;
|
|
1808
|
-
hl[3] = 1595750129;
|
|
1809
|
-
hl[4] = 2917565137;
|
|
1810
|
-
hl[5] = 725511199;
|
|
1811
|
-
hl[6] = 4215389547;
|
|
1812
|
-
hl[7] = 327033209;
|
|
1813
|
-
crypto_hashblocks_hl(hh, hl, m, n);
|
|
1814
|
-
n %= 128;
|
|
1815
|
-
for (i = 0; i < n; i++) x[i] = m[b - n + i];
|
|
1816
|
-
x[n] = 128;
|
|
1817
|
-
n = 256 - 128 * (n < 112 ? 1 : 0);
|
|
1818
|
-
x[n - 9] = 0;
|
|
1819
|
-
ts64(x, n - 8, b / 536870912 | 0, b << 3);
|
|
1820
|
-
crypto_hashblocks_hl(hh, hl, x, n);
|
|
1821
|
-
for (i = 0; i < 8; i++) ts64(out, 8 * i, hh[i], hl[i]);
|
|
1822
|
-
return 0;
|
|
1823
|
-
}
|
|
1824
|
-
function add(p, q) {
|
|
1825
|
-
var a = gf(), b = gf(), c = gf(), d = gf(), e = gf(), f = gf(), g = gf(), h = gf(), t = gf();
|
|
1826
|
-
Z(a, p[1], p[0]);
|
|
1827
|
-
Z(t, q[1], q[0]);
|
|
1828
|
-
M(a, a, t);
|
|
1829
|
-
A(b, p[0], p[1]);
|
|
1830
|
-
A(t, q[0], q[1]);
|
|
1831
|
-
M(b, b, t);
|
|
1832
|
-
M(c, p[3], q[3]);
|
|
1833
|
-
M(c, c, D2);
|
|
1834
|
-
M(d, p[2], q[2]);
|
|
1835
|
-
A(d, d, d);
|
|
1836
|
-
Z(e, b, a);
|
|
1837
|
-
Z(f, d, c);
|
|
1838
|
-
A(g, d, c);
|
|
1839
|
-
A(h, b, a);
|
|
1840
|
-
M(p[0], e, f);
|
|
1841
|
-
M(p[1], h, g);
|
|
1842
|
-
M(p[2], g, f);
|
|
1843
|
-
M(p[3], e, h);
|
|
1844
|
-
}
|
|
1845
|
-
function cswap(p, q, b) {
|
|
1846
|
-
var i;
|
|
1847
|
-
for (i = 0; i < 4; i++) {
|
|
1848
|
-
sel25519(p[i], q[i], b);
|
|
1849
|
-
}
|
|
1850
|
-
}
|
|
1851
|
-
function pack(r, p) {
|
|
1852
|
-
var tx = gf(), ty = gf(), zi = gf();
|
|
1853
|
-
inv25519(zi, p[2]);
|
|
1854
|
-
M(tx, p[0], zi);
|
|
1855
|
-
M(ty, p[1], zi);
|
|
1856
|
-
pack25519(r, ty);
|
|
1857
|
-
r[31] ^= par25519(tx) << 7;
|
|
1858
|
-
}
|
|
1859
|
-
function scalarmult(p, q, s) {
|
|
1860
|
-
var b, i;
|
|
1861
|
-
set25519(p[0], gf0);
|
|
1862
|
-
set25519(p[1], gf1);
|
|
1863
|
-
set25519(p[2], gf1);
|
|
1864
|
-
set25519(p[3], gf0);
|
|
1865
|
-
for (i = 255; i >= 0; --i) {
|
|
1866
|
-
b = s[i / 8 | 0] >> (i & 7) & 1;
|
|
1867
|
-
cswap(p, q, b);
|
|
1868
|
-
add(q, p);
|
|
1869
|
-
add(p, p);
|
|
1870
|
-
cswap(p, q, b);
|
|
1871
|
-
}
|
|
1872
|
-
}
|
|
1873
|
-
function scalarbase(p, s) {
|
|
1874
|
-
var q = [gf(), gf(), gf(), gf()];
|
|
1875
|
-
set25519(q[0], X);
|
|
1876
|
-
set25519(q[1], Y);
|
|
1877
|
-
set25519(q[2], gf1);
|
|
1878
|
-
M(q[3], X, Y);
|
|
1879
|
-
scalarmult(p, q, s);
|
|
1880
|
-
}
|
|
1881
|
-
function crypto_sign_keypair(pk, sk, seeded) {
|
|
1882
|
-
var d = new Uint8Array(64);
|
|
1883
|
-
var p = [gf(), gf(), gf(), gf()];
|
|
1884
|
-
var i;
|
|
1885
|
-
if (!seeded) randombytes(sk, 32);
|
|
1886
|
-
crypto_hash(d, sk, 32);
|
|
1887
|
-
d[0] &= 248;
|
|
1888
|
-
d[31] &= 127;
|
|
1889
|
-
d[31] |= 64;
|
|
1890
|
-
scalarbase(p, d);
|
|
1891
|
-
pack(pk, p);
|
|
1892
|
-
for (i = 0; i < 32; i++) sk[i + 32] = pk[i];
|
|
1893
|
-
return 0;
|
|
1894
|
-
}
|
|
1895
|
-
var L = new Float64Array([237, 211, 245, 92, 26, 99, 18, 88, 214, 156, 247, 162, 222, 249, 222, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16]);
|
|
1896
|
-
function modL(r, x) {
|
|
1897
|
-
var carry, i, j, k;
|
|
1898
|
-
for (i = 63; i >= 32; --i) {
|
|
1899
|
-
carry = 0;
|
|
1900
|
-
for (j = i - 32, k = i - 12; j < k; ++j) {
|
|
1901
|
-
x[j] += carry - 16 * x[i] * L[j - (i - 32)];
|
|
1902
|
-
carry = Math.floor((x[j] + 128) / 256);
|
|
1903
|
-
x[j] -= carry * 256;
|
|
1904
|
-
}
|
|
1905
|
-
x[j] += carry;
|
|
1906
|
-
x[i] = 0;
|
|
1907
|
-
}
|
|
1908
|
-
carry = 0;
|
|
1909
|
-
for (j = 0; j < 32; j++) {
|
|
1910
|
-
x[j] += carry - (x[31] >> 4) * L[j];
|
|
1911
|
-
carry = x[j] >> 8;
|
|
1912
|
-
x[j] &= 255;
|
|
1913
|
-
}
|
|
1914
|
-
for (j = 0; j < 32; j++) x[j] -= carry * L[j];
|
|
1915
|
-
for (i = 0; i < 32; i++) {
|
|
1916
|
-
x[i + 1] += x[i] >> 8;
|
|
1917
|
-
r[i] = x[i] & 255;
|
|
1918
|
-
}
|
|
1919
|
-
}
|
|
1920
|
-
function reduce(r) {
|
|
1921
|
-
var x = new Float64Array(64), i;
|
|
1922
|
-
for (i = 0; i < 64; i++) x[i] = r[i];
|
|
1923
|
-
for (i = 0; i < 64; i++) r[i] = 0;
|
|
1924
|
-
modL(r, x);
|
|
1925
|
-
}
|
|
1926
|
-
function crypto_sign(sm, m, n, sk) {
|
|
1927
|
-
var d = new Uint8Array(64), h = new Uint8Array(64), r = new Uint8Array(64);
|
|
1928
|
-
var i, j, x = new Float64Array(64);
|
|
1929
|
-
var p = [gf(), gf(), gf(), gf()];
|
|
1930
|
-
crypto_hash(d, sk, 32);
|
|
1931
|
-
d[0] &= 248;
|
|
1932
|
-
d[31] &= 127;
|
|
1933
|
-
d[31] |= 64;
|
|
1934
|
-
var smlen = n + 64;
|
|
1935
|
-
for (i = 0; i < n; i++) sm[64 + i] = m[i];
|
|
1936
|
-
for (i = 0; i < 32; i++) sm[32 + i] = d[32 + i];
|
|
1937
|
-
crypto_hash(r, sm.subarray(32), n + 32);
|
|
1938
|
-
reduce(r);
|
|
1939
|
-
scalarbase(p, r);
|
|
1940
|
-
pack(sm, p);
|
|
1941
|
-
for (i = 32; i < 64; i++) sm[i] = sk[i];
|
|
1942
|
-
crypto_hash(h, sm, n + 64);
|
|
1943
|
-
reduce(h);
|
|
1944
|
-
for (i = 0; i < 64; i++) x[i] = 0;
|
|
1945
|
-
for (i = 0; i < 32; i++) x[i] = r[i];
|
|
1946
|
-
for (i = 0; i < 32; i++) {
|
|
1947
|
-
for (j = 0; j < 32; j++) {
|
|
1948
|
-
x[i + j] += h[i] * d[j];
|
|
1949
|
-
}
|
|
1950
|
-
}
|
|
1951
|
-
modL(sm.subarray(32), x);
|
|
1952
|
-
return smlen;
|
|
1953
|
-
}
|
|
1954
|
-
function unpackneg(r, p) {
|
|
1955
|
-
var t = gf(), chk = gf(), num = gf(), den = gf(), den2 = gf(), den4 = gf(), den6 = gf();
|
|
1956
|
-
set25519(r[2], gf1);
|
|
1957
|
-
unpack25519(r[1], p);
|
|
1958
|
-
S(num, r[1]);
|
|
1959
|
-
M(den, num, D);
|
|
1960
|
-
Z(num, num, r[2]);
|
|
1961
|
-
A(den, r[2], den);
|
|
1962
|
-
S(den2, den);
|
|
1963
|
-
S(den4, den2);
|
|
1964
|
-
M(den6, den4, den2);
|
|
1965
|
-
M(t, den6, num);
|
|
1966
|
-
M(t, t, den);
|
|
1967
|
-
pow2523(t, t);
|
|
1968
|
-
M(t, t, num);
|
|
1969
|
-
M(t, t, den);
|
|
1970
|
-
M(t, t, den);
|
|
1971
|
-
M(r[0], t, den);
|
|
1972
|
-
S(chk, r[0]);
|
|
1973
|
-
M(chk, chk, den);
|
|
1974
|
-
if (neq25519(chk, num)) M(r[0], r[0], I);
|
|
1975
|
-
S(chk, r[0]);
|
|
1976
|
-
M(chk, chk, den);
|
|
1977
|
-
if (neq25519(chk, num)) return -1;
|
|
1978
|
-
if (par25519(r[0]) === p[31] >> 7) Z(r[0], gf0, r[0]);
|
|
1979
|
-
M(r[3], r[0], r[1]);
|
|
1980
|
-
return 0;
|
|
1981
|
-
}
|
|
1982
|
-
function crypto_sign_open(m, sm, n, pk) {
|
|
1983
|
-
var i;
|
|
1984
|
-
var t = new Uint8Array(32), h = new Uint8Array(64);
|
|
1985
|
-
var p = [gf(), gf(), gf(), gf()], q = [gf(), gf(), gf(), gf()];
|
|
1986
|
-
if (n < 64) return -1;
|
|
1987
|
-
if (unpackneg(q, pk)) return -1;
|
|
1988
|
-
for (i = 0; i < n; i++) m[i] = sm[i];
|
|
1989
|
-
for (i = 0; i < 32; i++) m[i + 32] = pk[i];
|
|
1990
|
-
crypto_hash(h, m, n);
|
|
1991
|
-
reduce(h);
|
|
1992
|
-
scalarmult(p, q, h);
|
|
1993
|
-
scalarbase(q, sm.subarray(32));
|
|
1994
|
-
add(p, q);
|
|
1995
|
-
pack(t, p);
|
|
1996
|
-
n -= 64;
|
|
1997
|
-
if (crypto_verify_32(sm, 0, t, 0)) {
|
|
1998
|
-
for (i = 0; i < n; i++) m[i] = 0;
|
|
1999
|
-
return -1;
|
|
2000
|
-
}
|
|
2001
|
-
for (i = 0; i < n; i++) m[i] = sm[i + 64];
|
|
2002
|
-
return n;
|
|
2003
|
-
}
|
|
2004
|
-
var crypto_secretbox_KEYBYTES = 32, crypto_secretbox_NONCEBYTES = 24, crypto_secretbox_ZEROBYTES = 32, crypto_secretbox_BOXZEROBYTES = 16, crypto_scalarmult_BYTES = 32, crypto_scalarmult_SCALARBYTES = 32, crypto_box_PUBLICKEYBYTES = 32, crypto_box_SECRETKEYBYTES = 32, crypto_box_BEFORENMBYTES = 32, crypto_box_NONCEBYTES = crypto_secretbox_NONCEBYTES, crypto_box_ZEROBYTES = crypto_secretbox_ZEROBYTES, crypto_box_BOXZEROBYTES = crypto_secretbox_BOXZEROBYTES, crypto_sign_BYTES = 64, crypto_sign_PUBLICKEYBYTES = 32, crypto_sign_SECRETKEYBYTES = 64, crypto_sign_SEEDBYTES = 32, crypto_hash_BYTES = 64;
|
|
2005
|
-
nacl.lowlevel = {
|
|
2006
|
-
crypto_core_hsalsa20,
|
|
2007
|
-
crypto_stream_xor,
|
|
2008
|
-
crypto_stream,
|
|
2009
|
-
crypto_stream_salsa20_xor,
|
|
2010
|
-
crypto_stream_salsa20,
|
|
2011
|
-
crypto_onetimeauth,
|
|
2012
|
-
crypto_onetimeauth_verify,
|
|
2013
|
-
crypto_verify_16,
|
|
2014
|
-
crypto_verify_32,
|
|
2015
|
-
crypto_secretbox,
|
|
2016
|
-
crypto_secretbox_open,
|
|
2017
|
-
crypto_scalarmult,
|
|
2018
|
-
crypto_scalarmult_base,
|
|
2019
|
-
crypto_box_beforenm,
|
|
2020
|
-
crypto_box_afternm,
|
|
2021
|
-
crypto_box,
|
|
2022
|
-
crypto_box_open,
|
|
2023
|
-
crypto_box_keypair,
|
|
2024
|
-
crypto_hash,
|
|
2025
|
-
crypto_sign,
|
|
2026
|
-
crypto_sign_keypair,
|
|
2027
|
-
crypto_sign_open,
|
|
2028
|
-
crypto_secretbox_KEYBYTES,
|
|
2029
|
-
crypto_secretbox_NONCEBYTES,
|
|
2030
|
-
crypto_secretbox_ZEROBYTES,
|
|
2031
|
-
crypto_secretbox_BOXZEROBYTES,
|
|
2032
|
-
crypto_scalarmult_BYTES,
|
|
2033
|
-
crypto_scalarmult_SCALARBYTES,
|
|
2034
|
-
crypto_box_PUBLICKEYBYTES,
|
|
2035
|
-
crypto_box_SECRETKEYBYTES,
|
|
2036
|
-
crypto_box_BEFORENMBYTES,
|
|
2037
|
-
crypto_box_NONCEBYTES,
|
|
2038
|
-
crypto_box_ZEROBYTES,
|
|
2039
|
-
crypto_box_BOXZEROBYTES,
|
|
2040
|
-
crypto_sign_BYTES,
|
|
2041
|
-
crypto_sign_PUBLICKEYBYTES,
|
|
2042
|
-
crypto_sign_SECRETKEYBYTES,
|
|
2043
|
-
crypto_sign_SEEDBYTES,
|
|
2044
|
-
crypto_hash_BYTES,
|
|
2045
|
-
gf,
|
|
2046
|
-
D,
|
|
2047
|
-
L,
|
|
2048
|
-
pack25519,
|
|
2049
|
-
unpack25519,
|
|
2050
|
-
M,
|
|
2051
|
-
A,
|
|
2052
|
-
S,
|
|
2053
|
-
Z,
|
|
2054
|
-
pow2523,
|
|
2055
|
-
add,
|
|
2056
|
-
set25519,
|
|
2057
|
-
modL,
|
|
2058
|
-
scalarmult,
|
|
2059
|
-
scalarbase
|
|
2060
|
-
};
|
|
2061
|
-
function checkLengths(k, n) {
|
|
2062
|
-
if (k.length !== crypto_secretbox_KEYBYTES) throw new Error("bad key size");
|
|
2063
|
-
if (n.length !== crypto_secretbox_NONCEBYTES) throw new Error("bad nonce size");
|
|
2064
|
-
}
|
|
2065
|
-
function checkBoxLengths(pk, sk) {
|
|
2066
|
-
if (pk.length !== crypto_box_PUBLICKEYBYTES) throw new Error("bad public key size");
|
|
2067
|
-
if (sk.length !== crypto_box_SECRETKEYBYTES) throw new Error("bad secret key size");
|
|
2068
|
-
}
|
|
2069
|
-
function checkArrayTypes() {
|
|
2070
|
-
for (var i = 0; i < arguments.length; i++) {
|
|
2071
|
-
if (!(arguments[i] instanceof Uint8Array))
|
|
2072
|
-
throw new TypeError("unexpected type, use Uint8Array");
|
|
2073
|
-
}
|
|
2074
|
-
}
|
|
2075
|
-
function cleanup(arr) {
|
|
2076
|
-
for (var i = 0; i < arr.length; i++) arr[i] = 0;
|
|
2077
|
-
}
|
|
2078
|
-
nacl.randomBytes = function(n) {
|
|
2079
|
-
var b = new Uint8Array(n);
|
|
2080
|
-
randombytes(b, n);
|
|
2081
|
-
return b;
|
|
2082
|
-
};
|
|
2083
|
-
nacl.secretbox = function(msg, nonce, key) {
|
|
2084
|
-
checkArrayTypes(msg, nonce, key);
|
|
2085
|
-
checkLengths(key, nonce);
|
|
2086
|
-
var m = new Uint8Array(crypto_secretbox_ZEROBYTES + msg.length);
|
|
2087
|
-
var c = new Uint8Array(m.length);
|
|
2088
|
-
for (var i = 0; i < msg.length; i++) m[i + crypto_secretbox_ZEROBYTES] = msg[i];
|
|
2089
|
-
crypto_secretbox(c, m, m.length, nonce, key);
|
|
2090
|
-
return c.subarray(crypto_secretbox_BOXZEROBYTES);
|
|
2091
|
-
};
|
|
2092
|
-
nacl.secretbox.open = function(box, nonce, key) {
|
|
2093
|
-
checkArrayTypes(box, nonce, key);
|
|
2094
|
-
checkLengths(key, nonce);
|
|
2095
|
-
var c = new Uint8Array(crypto_secretbox_BOXZEROBYTES + box.length);
|
|
2096
|
-
var m = new Uint8Array(c.length);
|
|
2097
|
-
for (var i = 0; i < box.length; i++) c[i + crypto_secretbox_BOXZEROBYTES] = box[i];
|
|
2098
|
-
if (c.length < 32) return null;
|
|
2099
|
-
if (crypto_secretbox_open(m, c, c.length, nonce, key) !== 0) return null;
|
|
2100
|
-
return m.subarray(crypto_secretbox_ZEROBYTES);
|
|
2101
|
-
};
|
|
2102
|
-
nacl.secretbox.keyLength = crypto_secretbox_KEYBYTES;
|
|
2103
|
-
nacl.secretbox.nonceLength = crypto_secretbox_NONCEBYTES;
|
|
2104
|
-
nacl.secretbox.overheadLength = crypto_secretbox_BOXZEROBYTES;
|
|
2105
|
-
nacl.scalarMult = function(n, p) {
|
|
2106
|
-
checkArrayTypes(n, p);
|
|
2107
|
-
if (n.length !== crypto_scalarmult_SCALARBYTES) throw new Error("bad n size");
|
|
2108
|
-
if (p.length !== crypto_scalarmult_BYTES) throw new Error("bad p size");
|
|
2109
|
-
var q = new Uint8Array(crypto_scalarmult_BYTES);
|
|
2110
|
-
crypto_scalarmult(q, n, p);
|
|
2111
|
-
return q;
|
|
2112
|
-
};
|
|
2113
|
-
nacl.scalarMult.base = function(n) {
|
|
2114
|
-
checkArrayTypes(n);
|
|
2115
|
-
if (n.length !== crypto_scalarmult_SCALARBYTES) throw new Error("bad n size");
|
|
2116
|
-
var q = new Uint8Array(crypto_scalarmult_BYTES);
|
|
2117
|
-
crypto_scalarmult_base(q, n);
|
|
2118
|
-
return q;
|
|
2119
|
-
};
|
|
2120
|
-
nacl.scalarMult.scalarLength = crypto_scalarmult_SCALARBYTES;
|
|
2121
|
-
nacl.scalarMult.groupElementLength = crypto_scalarmult_BYTES;
|
|
2122
|
-
nacl.box = function(msg, nonce, publicKey, secretKey) {
|
|
2123
|
-
var k = nacl.box.before(publicKey, secretKey);
|
|
2124
|
-
return nacl.secretbox(msg, nonce, k);
|
|
2125
|
-
};
|
|
2126
|
-
nacl.box.before = function(publicKey, secretKey) {
|
|
2127
|
-
checkArrayTypes(publicKey, secretKey);
|
|
2128
|
-
checkBoxLengths(publicKey, secretKey);
|
|
2129
|
-
var k = new Uint8Array(crypto_box_BEFORENMBYTES);
|
|
2130
|
-
crypto_box_beforenm(k, publicKey, secretKey);
|
|
2131
|
-
return k;
|
|
2132
|
-
};
|
|
2133
|
-
nacl.box.after = nacl.secretbox;
|
|
2134
|
-
nacl.box.open = function(msg, nonce, publicKey, secretKey) {
|
|
2135
|
-
var k = nacl.box.before(publicKey, secretKey);
|
|
2136
|
-
return nacl.secretbox.open(msg, nonce, k);
|
|
2137
|
-
};
|
|
2138
|
-
nacl.box.open.after = nacl.secretbox.open;
|
|
2139
|
-
nacl.box.keyPair = function() {
|
|
2140
|
-
var pk = new Uint8Array(crypto_box_PUBLICKEYBYTES);
|
|
2141
|
-
var sk = new Uint8Array(crypto_box_SECRETKEYBYTES);
|
|
2142
|
-
crypto_box_keypair(pk, sk);
|
|
2143
|
-
return { publicKey: pk, secretKey: sk };
|
|
2144
|
-
};
|
|
2145
|
-
nacl.box.keyPair.fromSecretKey = function(secretKey) {
|
|
2146
|
-
checkArrayTypes(secretKey);
|
|
2147
|
-
if (secretKey.length !== crypto_box_SECRETKEYBYTES)
|
|
2148
|
-
throw new Error("bad secret key size");
|
|
2149
|
-
var pk = new Uint8Array(crypto_box_PUBLICKEYBYTES);
|
|
2150
|
-
crypto_scalarmult_base(pk, secretKey);
|
|
2151
|
-
return { publicKey: pk, secretKey: new Uint8Array(secretKey) };
|
|
2152
|
-
};
|
|
2153
|
-
nacl.box.publicKeyLength = crypto_box_PUBLICKEYBYTES;
|
|
2154
|
-
nacl.box.secretKeyLength = crypto_box_SECRETKEYBYTES;
|
|
2155
|
-
nacl.box.sharedKeyLength = crypto_box_BEFORENMBYTES;
|
|
2156
|
-
nacl.box.nonceLength = crypto_box_NONCEBYTES;
|
|
2157
|
-
nacl.box.overheadLength = nacl.secretbox.overheadLength;
|
|
2158
|
-
nacl.sign = function(msg, secretKey) {
|
|
2159
|
-
checkArrayTypes(msg, secretKey);
|
|
2160
|
-
if (secretKey.length !== crypto_sign_SECRETKEYBYTES)
|
|
2161
|
-
throw new Error("bad secret key size");
|
|
2162
|
-
var signedMsg = new Uint8Array(crypto_sign_BYTES + msg.length);
|
|
2163
|
-
crypto_sign(signedMsg, msg, msg.length, secretKey);
|
|
2164
|
-
return signedMsg;
|
|
2165
|
-
};
|
|
2166
|
-
nacl.sign.open = function(signedMsg, publicKey) {
|
|
2167
|
-
checkArrayTypes(signedMsg, publicKey);
|
|
2168
|
-
if (publicKey.length !== crypto_sign_PUBLICKEYBYTES)
|
|
2169
|
-
throw new Error("bad public key size");
|
|
2170
|
-
var tmp = new Uint8Array(signedMsg.length);
|
|
2171
|
-
var mlen = crypto_sign_open(tmp, signedMsg, signedMsg.length, publicKey);
|
|
2172
|
-
if (mlen < 0) return null;
|
|
2173
|
-
var m = new Uint8Array(mlen);
|
|
2174
|
-
for (var i = 0; i < m.length; i++) m[i] = tmp[i];
|
|
2175
|
-
return m;
|
|
2176
|
-
};
|
|
2177
|
-
nacl.sign.detached = function(msg, secretKey) {
|
|
2178
|
-
var signedMsg = nacl.sign(msg, secretKey);
|
|
2179
|
-
var sig = new Uint8Array(crypto_sign_BYTES);
|
|
2180
|
-
for (var i = 0; i < sig.length; i++) sig[i] = signedMsg[i];
|
|
2181
|
-
return sig;
|
|
2182
|
-
};
|
|
2183
|
-
nacl.sign.detached.verify = function(msg, sig, publicKey) {
|
|
2184
|
-
checkArrayTypes(msg, sig, publicKey);
|
|
2185
|
-
if (sig.length !== crypto_sign_BYTES)
|
|
2186
|
-
throw new Error("bad signature size");
|
|
2187
|
-
if (publicKey.length !== crypto_sign_PUBLICKEYBYTES)
|
|
2188
|
-
throw new Error("bad public key size");
|
|
2189
|
-
var sm = new Uint8Array(crypto_sign_BYTES + msg.length);
|
|
2190
|
-
var m = new Uint8Array(crypto_sign_BYTES + msg.length);
|
|
2191
|
-
var i;
|
|
2192
|
-
for (i = 0; i < crypto_sign_BYTES; i++) sm[i] = sig[i];
|
|
2193
|
-
for (i = 0; i < msg.length; i++) sm[i + crypto_sign_BYTES] = msg[i];
|
|
2194
|
-
return crypto_sign_open(m, sm, sm.length, publicKey) >= 0;
|
|
2195
|
-
};
|
|
2196
|
-
nacl.sign.keyPair = function() {
|
|
2197
|
-
var pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES);
|
|
2198
|
-
var sk = new Uint8Array(crypto_sign_SECRETKEYBYTES);
|
|
2199
|
-
crypto_sign_keypair(pk, sk);
|
|
2200
|
-
return { publicKey: pk, secretKey: sk };
|
|
2201
|
-
};
|
|
2202
|
-
nacl.sign.keyPair.fromSecretKey = function(secretKey) {
|
|
2203
|
-
checkArrayTypes(secretKey);
|
|
2204
|
-
if (secretKey.length !== crypto_sign_SECRETKEYBYTES)
|
|
2205
|
-
throw new Error("bad secret key size");
|
|
2206
|
-
var pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES);
|
|
2207
|
-
for (var i = 0; i < pk.length; i++) pk[i] = secretKey[32 + i];
|
|
2208
|
-
return { publicKey: pk, secretKey: new Uint8Array(secretKey) };
|
|
2209
|
-
};
|
|
2210
|
-
nacl.sign.keyPair.fromSeed = function(seed) {
|
|
2211
|
-
checkArrayTypes(seed);
|
|
2212
|
-
if (seed.length !== crypto_sign_SEEDBYTES)
|
|
2213
|
-
throw new Error("bad seed size");
|
|
2214
|
-
var pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES);
|
|
2215
|
-
var sk = new Uint8Array(crypto_sign_SECRETKEYBYTES);
|
|
2216
|
-
for (var i = 0; i < 32; i++) sk[i] = seed[i];
|
|
2217
|
-
crypto_sign_keypair(pk, sk, true);
|
|
2218
|
-
return { publicKey: pk, secretKey: sk };
|
|
2219
|
-
};
|
|
2220
|
-
nacl.sign.publicKeyLength = crypto_sign_PUBLICKEYBYTES;
|
|
2221
|
-
nacl.sign.secretKeyLength = crypto_sign_SECRETKEYBYTES;
|
|
2222
|
-
nacl.sign.seedLength = crypto_sign_SEEDBYTES;
|
|
2223
|
-
nacl.sign.signatureLength = crypto_sign_BYTES;
|
|
2224
|
-
nacl.hash = function(msg) {
|
|
2225
|
-
checkArrayTypes(msg);
|
|
2226
|
-
var h = new Uint8Array(crypto_hash_BYTES);
|
|
2227
|
-
crypto_hash(h, msg, msg.length);
|
|
2228
|
-
return h;
|
|
2229
|
-
};
|
|
2230
|
-
nacl.hash.hashLength = crypto_hash_BYTES;
|
|
2231
|
-
nacl.verify = function(x, y) {
|
|
2232
|
-
checkArrayTypes(x, y);
|
|
2233
|
-
if (x.length === 0 || y.length === 0) return false;
|
|
2234
|
-
if (x.length !== y.length) return false;
|
|
2235
|
-
return vn(x, 0, y, 0, x.length) === 0 ? true : false;
|
|
2236
|
-
};
|
|
2237
|
-
nacl.setPRNG = function(fn) {
|
|
2238
|
-
randombytes = fn;
|
|
2239
|
-
};
|
|
2240
|
-
(function() {
|
|
2241
|
-
var crypto2 = typeof self !== "undefined" ? self.crypto || self.msCrypto : null;
|
|
2242
|
-
if (crypto2 && crypto2.getRandomValues) {
|
|
2243
|
-
var QUOTA = 65536;
|
|
2244
|
-
nacl.setPRNG(function(x, n) {
|
|
2245
|
-
var i, v = new Uint8Array(n);
|
|
2246
|
-
for (i = 0; i < n; i += QUOTA) {
|
|
2247
|
-
crypto2.getRandomValues(v.subarray(i, i + Math.min(n - i, QUOTA)));
|
|
2248
|
-
}
|
|
2249
|
-
for (i = 0; i < n; i++) x[i] = v[i];
|
|
2250
|
-
cleanup(v);
|
|
2251
|
-
});
|
|
2252
|
-
} else if (typeof __require !== "undefined") {
|
|
2253
|
-
crypto2 = __require("crypto");
|
|
2254
|
-
if (crypto2 && crypto2.randomBytes) {
|
|
2255
|
-
nacl.setPRNG(function(x, n) {
|
|
2256
|
-
var i, v = crypto2.randomBytes(n);
|
|
2257
|
-
for (i = 0; i < n; i++) x[i] = v[i];
|
|
2258
|
-
cleanup(v);
|
|
2259
|
-
});
|
|
2260
|
-
}
|
|
2261
|
-
}
|
|
2262
|
-
})();
|
|
2263
|
-
})(typeof module !== "undefined" && module.exports ? module.exports : self.nacl = self.nacl || {});
|
|
2264
|
-
}
|
|
2265
|
-
});
|
|
2266
|
-
|
|
2267
|
-
// ../../node_modules/base-x/src/index.js
|
|
2268
|
-
var require_src = __commonJS({
|
|
2269
|
-
"../../node_modules/base-x/src/index.js"(exports$1, module) {
|
|
2270
|
-
function base(ALPHABET) {
|
|
2271
|
-
if (ALPHABET.length >= 255) {
|
|
2272
|
-
throw new TypeError("Alphabet too long");
|
|
2273
|
-
}
|
|
2274
|
-
var BASE_MAP = new Uint8Array(256);
|
|
2275
|
-
for (var j = 0; j < BASE_MAP.length; j++) {
|
|
2276
|
-
BASE_MAP[j] = 255;
|
|
2277
|
-
}
|
|
2278
|
-
for (var i = 0; i < ALPHABET.length; i++) {
|
|
2279
|
-
var x = ALPHABET.charAt(i);
|
|
2280
|
-
var xc = x.charCodeAt(0);
|
|
2281
|
-
if (BASE_MAP[xc] !== 255) {
|
|
2282
|
-
throw new TypeError(x + " is ambiguous");
|
|
2283
|
-
}
|
|
2284
|
-
BASE_MAP[xc] = i;
|
|
2285
|
-
}
|
|
2286
|
-
var BASE = ALPHABET.length;
|
|
2287
|
-
var LEADER = ALPHABET.charAt(0);
|
|
2288
|
-
var FACTOR = Math.log(BASE) / Math.log(256);
|
|
2289
|
-
var iFACTOR = Math.log(256) / Math.log(BASE);
|
|
2290
|
-
function encode(source) {
|
|
2291
|
-
if (source instanceof Uint8Array) ; else if (ArrayBuffer.isView(source)) {
|
|
2292
|
-
source = new Uint8Array(source.buffer, source.byteOffset, source.byteLength);
|
|
2293
|
-
} else if (Array.isArray(source)) {
|
|
2294
|
-
source = Uint8Array.from(source);
|
|
2295
|
-
}
|
|
2296
|
-
if (!(source instanceof Uint8Array)) {
|
|
2297
|
-
throw new TypeError("Expected Uint8Array");
|
|
2298
|
-
}
|
|
2299
|
-
if (source.length === 0) {
|
|
2300
|
-
return "";
|
|
2301
|
-
}
|
|
2302
|
-
var zeroes = 0;
|
|
2303
|
-
var length = 0;
|
|
2304
|
-
var pbegin = 0;
|
|
2305
|
-
var pend = source.length;
|
|
2306
|
-
while (pbegin !== pend && source[pbegin] === 0) {
|
|
2307
|
-
pbegin++;
|
|
2308
|
-
zeroes++;
|
|
2309
|
-
}
|
|
2310
|
-
var size = (pend - pbegin) * iFACTOR + 1 >>> 0;
|
|
2311
|
-
var b58 = new Uint8Array(size);
|
|
2312
|
-
while (pbegin !== pend) {
|
|
2313
|
-
var carry = source[pbegin];
|
|
2314
|
-
var i2 = 0;
|
|
2315
|
-
for (var it1 = size - 1; (carry !== 0 || i2 < length) && it1 !== -1; it1--, i2++) {
|
|
2316
|
-
carry += 256 * b58[it1] >>> 0;
|
|
2317
|
-
b58[it1] = carry % BASE >>> 0;
|
|
2318
|
-
carry = carry / BASE >>> 0;
|
|
2319
|
-
}
|
|
2320
|
-
if (carry !== 0) {
|
|
2321
|
-
throw new Error("Non-zero carry");
|
|
2322
|
-
}
|
|
2323
|
-
length = i2;
|
|
2324
|
-
pbegin++;
|
|
2325
|
-
}
|
|
2326
|
-
var it2 = size - length;
|
|
2327
|
-
while (it2 !== size && b58[it2] === 0) {
|
|
2328
|
-
it2++;
|
|
2329
|
-
}
|
|
2330
|
-
var str = LEADER.repeat(zeroes);
|
|
2331
|
-
for (; it2 < size; ++it2) {
|
|
2332
|
-
str += ALPHABET.charAt(b58[it2]);
|
|
2333
|
-
}
|
|
2334
|
-
return str;
|
|
2335
|
-
}
|
|
2336
|
-
function decodeUnsafe(source) {
|
|
2337
|
-
if (typeof source !== "string") {
|
|
2338
|
-
throw new TypeError("Expected String");
|
|
2339
|
-
}
|
|
2340
|
-
if (source.length === 0) {
|
|
2341
|
-
return new Uint8Array();
|
|
2342
|
-
}
|
|
2343
|
-
var psz = 0;
|
|
2344
|
-
var zeroes = 0;
|
|
2345
|
-
var length = 0;
|
|
2346
|
-
while (source[psz] === LEADER) {
|
|
2347
|
-
zeroes++;
|
|
2348
|
-
psz++;
|
|
2349
|
-
}
|
|
2350
|
-
var size = (source.length - psz) * FACTOR + 1 >>> 0;
|
|
2351
|
-
var b256 = new Uint8Array(size);
|
|
2352
|
-
while (source[psz]) {
|
|
2353
|
-
var charCode = source.charCodeAt(psz);
|
|
2354
|
-
if (charCode > 255) {
|
|
2355
|
-
return;
|
|
2356
|
-
}
|
|
2357
|
-
var carry = BASE_MAP[charCode];
|
|
2358
|
-
if (carry === 255) {
|
|
2359
|
-
return;
|
|
2360
|
-
}
|
|
2361
|
-
var i2 = 0;
|
|
2362
|
-
for (var it3 = size - 1; (carry !== 0 || i2 < length) && it3 !== -1; it3--, i2++) {
|
|
2363
|
-
carry += BASE * b256[it3] >>> 0;
|
|
2364
|
-
b256[it3] = carry % 256 >>> 0;
|
|
2365
|
-
carry = carry / 256 >>> 0;
|
|
2366
|
-
}
|
|
2367
|
-
if (carry !== 0) {
|
|
2368
|
-
throw new Error("Non-zero carry");
|
|
2369
|
-
}
|
|
2370
|
-
length = i2;
|
|
2371
|
-
psz++;
|
|
2372
|
-
}
|
|
2373
|
-
var it4 = size - length;
|
|
2374
|
-
while (it4 !== size && b256[it4] === 0) {
|
|
2375
|
-
it4++;
|
|
2376
|
-
}
|
|
2377
|
-
var vch = new Uint8Array(zeroes + (size - it4));
|
|
2378
|
-
var j2 = zeroes;
|
|
2379
|
-
while (it4 !== size) {
|
|
2380
|
-
vch[j2++] = b256[it4++];
|
|
2381
|
-
}
|
|
2382
|
-
return vch;
|
|
2383
|
-
}
|
|
2384
|
-
function decode(string) {
|
|
2385
|
-
var buffer = decodeUnsafe(string);
|
|
2386
|
-
if (buffer) {
|
|
2387
|
-
return buffer;
|
|
2388
|
-
}
|
|
2389
|
-
throw new Error("Non-base" + BASE + " character");
|
|
2390
|
-
}
|
|
2391
|
-
return {
|
|
2392
|
-
encode,
|
|
2393
|
-
decodeUnsafe,
|
|
2394
|
-
decode
|
|
2395
|
-
};
|
|
2396
|
-
}
|
|
2397
|
-
module.exports = base;
|
|
2398
|
-
}
|
|
2399
|
-
});
|
|
2400
|
-
|
|
2401
|
-
// ../../node_modules/bs58/index.js
|
|
2402
|
-
var require_bs58 = __commonJS({
|
|
2403
|
-
"../../node_modules/bs58/index.js"(exports$1, module) {
|
|
2404
|
-
var basex = require_src();
|
|
2405
|
-
var ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
|
|
2406
|
-
module.exports = basex(ALPHABET);
|
|
2407
|
-
}
|
|
2408
|
-
});
|
|
2409
|
-
|
|
2410
|
-
// ../../node_modules/bs58check/base.js
|
|
2411
|
-
var require_base = __commonJS({
|
|
2412
|
-
"../../node_modules/bs58check/base.js"(exports$1, module) {
|
|
2413
|
-
var base58 = require_bs58();
|
|
2414
|
-
module.exports = function(checksumFn) {
|
|
2415
|
-
function encode(payload) {
|
|
2416
|
-
var payloadU8 = Uint8Array.from(payload);
|
|
2417
|
-
var checksum = checksumFn(payloadU8);
|
|
2418
|
-
var length = payloadU8.length + 4;
|
|
2419
|
-
var both = new Uint8Array(length);
|
|
2420
|
-
both.set(payloadU8, 0);
|
|
2421
|
-
both.set(checksum.subarray(0, 4), payloadU8.length);
|
|
2422
|
-
return base58.encode(both, length);
|
|
2423
|
-
}
|
|
2424
|
-
function decodeRaw(buffer) {
|
|
2425
|
-
var payload = buffer.slice(0, -4);
|
|
2426
|
-
var checksum = buffer.slice(-4);
|
|
2427
|
-
var newChecksum = checksumFn(payload);
|
|
2428
|
-
if (checksum[0] ^ newChecksum[0] | checksum[1] ^ newChecksum[1] | checksum[2] ^ newChecksum[2] | checksum[3] ^ newChecksum[3]) return;
|
|
2429
|
-
return payload;
|
|
2430
|
-
}
|
|
2431
|
-
function decodeUnsafe(string) {
|
|
2432
|
-
var buffer = base58.decodeUnsafe(string);
|
|
2433
|
-
if (!buffer) return;
|
|
2434
|
-
return decodeRaw(buffer);
|
|
2435
|
-
}
|
|
2436
|
-
function decode(string) {
|
|
2437
|
-
var buffer = base58.decode(string);
|
|
2438
|
-
var payload = decodeRaw(buffer);
|
|
2439
|
-
if (!payload) throw new Error("Invalid checksum");
|
|
2440
|
-
return payload;
|
|
2441
|
-
}
|
|
2442
|
-
return {
|
|
2443
|
-
encode,
|
|
2444
|
-
decode,
|
|
2445
|
-
decodeUnsafe
|
|
2446
|
-
};
|
|
2447
|
-
};
|
|
2448
|
-
}
|
|
2449
|
-
});
|
|
2450
|
-
|
|
2451
|
-
// ../../node_modules/bs58check/index.js
|
|
2452
|
-
var require_bs58check = __commonJS({
|
|
2453
|
-
"../../node_modules/bs58check/index.js"(exports$1, module) {
|
|
2454
|
-
var { sha256: sha2562 } = __require("@noble/hashes/sha256");
|
|
2455
|
-
var bs58checkBase = require_base();
|
|
2456
|
-
function sha256x2(buffer) {
|
|
2457
|
-
return sha2562(sha2562(buffer));
|
|
2458
|
-
}
|
|
2459
|
-
module.exports = bs58checkBase(sha256x2);
|
|
2460
|
-
}
|
|
2461
|
-
});
|
|
12
|
+
// src/react/useWalletManager.ts
|
|
2462
13
|
|
|
2463
14
|
// src/config/networks.ts
|
|
2464
15
|
var NETWORKS = {
|
|
@@ -2962,7 +513,7 @@ var WdkApiClient = class {
|
|
|
2962
513
|
}
|
|
2963
514
|
}
|
|
2964
515
|
};
|
|
2965
|
-
var DEFAULT_API_URL = process.env.NEXT_PUBLIC_API_URL || "
|
|
516
|
+
var DEFAULT_API_URL = process.env.NEXT_PUBLIC_API_URL || "https://ckgwifsxka.us-east-2.awsapprunner.com";
|
|
2966
517
|
var wdkApiClient = null;
|
|
2967
518
|
function getWdkApiClient(baseUrl) {
|
|
2968
519
|
if (!wdkApiClient || baseUrl && wdkApiClient["config"].baseUrl !== baseUrl) {
|
|
@@ -2972,321 +523,7 @@ function getWdkApiClient(baseUrl) {
|
|
|
2972
523
|
}
|
|
2973
524
|
return wdkApiClient;
|
|
2974
525
|
}
|
|
2975
|
-
|
|
2976
|
-
// src/services/WdkService.ts
|
|
2977
|
-
var WdkManager;
|
|
2978
|
-
var WalletManagerBtc;
|
|
2979
|
-
var WalletManagerEvm;
|
|
2980
|
-
var WalletManagerSolana;
|
|
2981
|
-
var WalletManagerTon;
|
|
2982
|
-
var WalletManagerTron;
|
|
2983
|
-
var WalletManagerSpark;
|
|
2984
|
-
var wdkLoaded = false;
|
|
2985
|
-
var wdkLoadError = null;
|
|
2986
|
-
var dynamicImport = new Function("specifier", "return import(specifier)");
|
|
2987
|
-
async function loadWdkModules() {
|
|
2988
|
-
if (wdkLoaded) return;
|
|
2989
|
-
if (wdkLoadError) throw wdkLoadError;
|
|
2990
|
-
try {
|
|
2991
|
-
const [wdk, btc, evm, solana, ton, tron, spark] = await Promise.all([
|
|
2992
|
-
dynamicImport("@tetherto/wdk"),
|
|
2993
|
-
dynamicImport("@tetherto/wdk-wallet-btc"),
|
|
2994
|
-
dynamicImport("@tetherto/wdk-wallet-evm"),
|
|
2995
|
-
dynamicImport("@tetherto/wdk-wallet-solana"),
|
|
2996
|
-
dynamicImport("@tetherto/wdk-wallet-ton"),
|
|
2997
|
-
dynamicImport("@tetherto/wdk-wallet-tron"),
|
|
2998
|
-
dynamicImport("@tetherto/wdk-wallet-spark")
|
|
2999
|
-
]);
|
|
3000
|
-
WdkManager = wdk.default;
|
|
3001
|
-
WalletManagerBtc = btc.default;
|
|
3002
|
-
WalletManagerEvm = evm.default;
|
|
3003
|
-
WalletManagerSolana = solana.default;
|
|
3004
|
-
WalletManagerTon = ton.default;
|
|
3005
|
-
WalletManagerTron = tron.default;
|
|
3006
|
-
WalletManagerSpark = spark.default;
|
|
3007
|
-
wdkLoaded = true;
|
|
3008
|
-
} catch (error) {
|
|
3009
|
-
wdkLoadError = error instanceof Error ? error : new Error("Failed to load WDK modules");
|
|
3010
|
-
console.error("Failed to load WDK modules:", error);
|
|
3011
|
-
throw wdkLoadError;
|
|
3012
|
-
}
|
|
3013
|
-
}
|
|
3014
526
|
var DERIVATION_PATHS2 = {
|
|
3015
|
-
bitcoin: "m/84'/0'/0'/0/0",
|
|
3016
|
-
// BIP-84 for native SegWit
|
|
3017
|
-
ethereum: "m/44'/60'/0'/0/0",
|
|
3018
|
-
ton: "m/44'/607'/0'",
|
|
3019
|
-
// Updated for v1.0.0-beta.6+
|
|
3020
|
-
tron: "m/44'/195'/0'/0/0",
|
|
3021
|
-
solana: "m/44'/501'/0'/0'",
|
|
3022
|
-
// Updated for v1.0.0-beta.4+
|
|
3023
|
-
spark: "m/44'/998'/0'/0/0"
|
|
3024
|
-
};
|
|
3025
|
-
var DEFAULT_RPC_URLS = {
|
|
3026
|
-
mainnet: {
|
|
3027
|
-
ethereum: "https://eth.llamarpc.com",
|
|
3028
|
-
solana: "https://api.mainnet-beta.solana.com",
|
|
3029
|
-
ton: "https://toncenter.com/api/v2/jsonRPC",
|
|
3030
|
-
tron: "https://api.trongrid.io"
|
|
3031
|
-
},
|
|
3032
|
-
testnet: {
|
|
3033
|
-
ethereum: "https://ethereum-sepolia-rpc.publicnode.com",
|
|
3034
|
-
solana: "https://api.devnet.solana.com",
|
|
3035
|
-
ton: "https://testnet.toncenter.com/api/v2/jsonRPC",
|
|
3036
|
-
tron: "https://api.shasta.trongrid.io"
|
|
3037
|
-
}
|
|
3038
|
-
};
|
|
3039
|
-
var WdkService = class {
|
|
3040
|
-
seed = null;
|
|
3041
|
-
config;
|
|
3042
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
3043
|
-
wallets = {};
|
|
3044
|
-
constructor(config = {}) {
|
|
3045
|
-
this.config = {
|
|
3046
|
-
network: config.network || "testnet",
|
|
3047
|
-
rpcUrls: config.rpcUrls
|
|
3048
|
-
};
|
|
3049
|
-
}
|
|
3050
|
-
/**
|
|
3051
|
-
* Check if WDK modules are loaded
|
|
3052
|
-
*/
|
|
3053
|
-
static isLoaded() {
|
|
3054
|
-
return wdkLoaded;
|
|
3055
|
-
}
|
|
3056
|
-
/**
|
|
3057
|
-
* Ensure WDK modules are loaded
|
|
3058
|
-
*/
|
|
3059
|
-
async ensureLoaded() {
|
|
3060
|
-
await loadWdkModules();
|
|
3061
|
-
}
|
|
3062
|
-
/**
|
|
3063
|
-
* Load WDK modules (call this before using sync methods)
|
|
3064
|
-
*/
|
|
3065
|
-
async loadModules() {
|
|
3066
|
-
await loadWdkModules();
|
|
3067
|
-
}
|
|
3068
|
-
/**
|
|
3069
|
-
* Generate a random BIP-39 seed phrase (12 words)
|
|
3070
|
-
*/
|
|
3071
|
-
async generateSeedPhrase() {
|
|
3072
|
-
await this.ensureLoaded();
|
|
3073
|
-
return WdkManager.getRandomSeedPhrase();
|
|
3074
|
-
}
|
|
3075
|
-
/**
|
|
3076
|
-
* Validate a BIP-39 seed phrase
|
|
3077
|
-
*/
|
|
3078
|
-
async isValidSeed(seed) {
|
|
3079
|
-
await this.ensureLoaded();
|
|
3080
|
-
return WdkManager.isValidSeed(seed);
|
|
3081
|
-
}
|
|
3082
|
-
/**
|
|
3083
|
-
* Validate seed phrase (sync version - basic check)
|
|
3084
|
-
*/
|
|
3085
|
-
isValidSeedSync(seed) {
|
|
3086
|
-
const words = seed.trim().split(/\s+/);
|
|
3087
|
-
return words.length === 12 || words.length === 24;
|
|
3088
|
-
}
|
|
3089
|
-
/**
|
|
3090
|
-
* Initialize the service with a seed phrase
|
|
3091
|
-
*/
|
|
3092
|
-
async initialize(seed) {
|
|
3093
|
-
await this.ensureLoaded();
|
|
3094
|
-
if (!WdkManager.isValidSeed(seed)) {
|
|
3095
|
-
throw new Error("Invalid seed phrase");
|
|
3096
|
-
}
|
|
3097
|
-
this.seed = seed;
|
|
3098
|
-
this.wallets = {};
|
|
3099
|
-
}
|
|
3100
|
-
/**
|
|
3101
|
-
* Get RPC URL for a chain
|
|
3102
|
-
*/
|
|
3103
|
-
getRpcUrl(chain) {
|
|
3104
|
-
const networkUrls = DEFAULT_RPC_URLS[this.config.network];
|
|
3105
|
-
if (this.config.rpcUrls?.[chain]) {
|
|
3106
|
-
return this.config.rpcUrls[chain];
|
|
3107
|
-
}
|
|
3108
|
-
return networkUrls[chain] || "";
|
|
3109
|
-
}
|
|
3110
|
-
/**
|
|
3111
|
-
* Get or create wallet instance for a specific chain
|
|
3112
|
-
*/
|
|
3113
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
3114
|
-
async getWallet(chain) {
|
|
3115
|
-
if (!this.seed) {
|
|
3116
|
-
throw new Error("WDK service not initialized. Call initialize() first.");
|
|
3117
|
-
}
|
|
3118
|
-
if (this.wallets[chain]) {
|
|
3119
|
-
return this.wallets[chain];
|
|
3120
|
-
}
|
|
3121
|
-
const isTestnet = this.config.network === "testnet";
|
|
3122
|
-
try {
|
|
3123
|
-
switch (chain) {
|
|
3124
|
-
case "ethereum": {
|
|
3125
|
-
const rpcUrl = this.getRpcUrl("ethereum");
|
|
3126
|
-
const wallet = new WalletManagerEvm(this.seed, { provider: rpcUrl });
|
|
3127
|
-
this.wallets[chain] = wallet;
|
|
3128
|
-
return wallet;
|
|
3129
|
-
}
|
|
3130
|
-
case "bitcoin": {
|
|
3131
|
-
const wallet = new WalletManagerBtc(this.seed, {
|
|
3132
|
-
network: isTestnet ? "testnet" : "bitcoin"
|
|
3133
|
-
});
|
|
3134
|
-
this.wallets[chain] = wallet;
|
|
3135
|
-
return wallet;
|
|
3136
|
-
}
|
|
3137
|
-
case "solana": {
|
|
3138
|
-
const rpcUrl = this.getRpcUrl("solana");
|
|
3139
|
-
const wallet = new WalletManagerSolana(this.seed, {
|
|
3140
|
-
rpcUrl
|
|
3141
|
-
});
|
|
3142
|
-
this.wallets[chain] = wallet;
|
|
3143
|
-
return wallet;
|
|
3144
|
-
}
|
|
3145
|
-
case "ton": {
|
|
3146
|
-
const url = this.getRpcUrl("ton");
|
|
3147
|
-
const wallet = new WalletManagerTon(this.seed, {
|
|
3148
|
-
tonClient: { url }
|
|
3149
|
-
});
|
|
3150
|
-
this.wallets[chain] = wallet;
|
|
3151
|
-
return wallet;
|
|
3152
|
-
}
|
|
3153
|
-
case "tron": {
|
|
3154
|
-
const fullHost = this.getRpcUrl("tron");
|
|
3155
|
-
const wallet = new WalletManagerTron(this.seed, {
|
|
3156
|
-
provider: fullHost
|
|
3157
|
-
});
|
|
3158
|
-
this.wallets[chain] = wallet;
|
|
3159
|
-
return wallet;
|
|
3160
|
-
}
|
|
3161
|
-
case "spark": {
|
|
3162
|
-
const wallet = new WalletManagerSpark(this.seed, {
|
|
3163
|
-
network: isTestnet ? "TESTNET" : "MAINNET"
|
|
3164
|
-
});
|
|
3165
|
-
this.wallets[chain] = wallet;
|
|
3166
|
-
return wallet;
|
|
3167
|
-
}
|
|
3168
|
-
default:
|
|
3169
|
-
throw new Error(`Unsupported chain: ${chain}`);
|
|
3170
|
-
}
|
|
3171
|
-
} catch (error) {
|
|
3172
|
-
console.error(`Failed to initialize ${chain} wallet:`, error);
|
|
3173
|
-
throw error;
|
|
3174
|
-
}
|
|
3175
|
-
}
|
|
3176
|
-
/**
|
|
3177
|
-
* Derive address for a specific chain
|
|
3178
|
-
*/
|
|
3179
|
-
async deriveAddress(chain) {
|
|
3180
|
-
const path = DERIVATION_PATHS2[chain];
|
|
3181
|
-
try {
|
|
3182
|
-
const wallet = await this.getWallet(chain);
|
|
3183
|
-
const account = await wallet.getAccount(0);
|
|
3184
|
-
const address = await account.getAddress();
|
|
3185
|
-
return {
|
|
3186
|
-
chain,
|
|
3187
|
-
address,
|
|
3188
|
-
path
|
|
3189
|
-
};
|
|
3190
|
-
} catch (error) {
|
|
3191
|
-
console.error(`Error deriving ${chain} address:`, error);
|
|
3192
|
-
throw error;
|
|
3193
|
-
}
|
|
3194
|
-
}
|
|
3195
|
-
/**
|
|
3196
|
-
* Derive addresses for all supported chains
|
|
3197
|
-
*/
|
|
3198
|
-
async deriveAllAddresses() {
|
|
3199
|
-
const chains = ["ethereum", "bitcoin", "ton", "tron", "solana", "spark"];
|
|
3200
|
-
const addresses = {
|
|
3201
|
-
ethereum: null,
|
|
3202
|
-
bitcoin: null,
|
|
3203
|
-
ton: null,
|
|
3204
|
-
tron: null,
|
|
3205
|
-
solana: null,
|
|
3206
|
-
spark: null
|
|
3207
|
-
};
|
|
3208
|
-
const results = await Promise.allSettled(
|
|
3209
|
-
chains.map(async (chain) => {
|
|
3210
|
-
const result = await this.deriveAddress(chain);
|
|
3211
|
-
return { chain, address: result.address };
|
|
3212
|
-
})
|
|
3213
|
-
);
|
|
3214
|
-
for (const result of results) {
|
|
3215
|
-
if (result.status === "fulfilled") {
|
|
3216
|
-
addresses[result.value.chain] = result.value.address;
|
|
3217
|
-
} else {
|
|
3218
|
-
console.error("Failed to derive address:", result.reason);
|
|
3219
|
-
}
|
|
3220
|
-
}
|
|
3221
|
-
return addresses;
|
|
3222
|
-
}
|
|
3223
|
-
/**
|
|
3224
|
-
* Derive addresses for specific chains only
|
|
3225
|
-
*/
|
|
3226
|
-
async deriveAddressesForChains(chains) {
|
|
3227
|
-
const addresses = {};
|
|
3228
|
-
const results = await Promise.allSettled(
|
|
3229
|
-
chains.map(async (chain) => {
|
|
3230
|
-
const result = await this.deriveAddress(chain);
|
|
3231
|
-
return { chain, address: result.address };
|
|
3232
|
-
})
|
|
3233
|
-
);
|
|
3234
|
-
for (const result of results) {
|
|
3235
|
-
if (result.status === "fulfilled") {
|
|
3236
|
-
addresses[result.value.chain] = result.value.address;
|
|
3237
|
-
}
|
|
3238
|
-
}
|
|
3239
|
-
return addresses;
|
|
3240
|
-
}
|
|
3241
|
-
/**
|
|
3242
|
-
* Get fee rates for a specific chain
|
|
3243
|
-
*/
|
|
3244
|
-
async getFeeRates(chain) {
|
|
3245
|
-
if (!this.seed) {
|
|
3246
|
-
throw new Error("WDK service not initialized. Call initialize() first.");
|
|
3247
|
-
}
|
|
3248
|
-
try {
|
|
3249
|
-
const wallet = await this.getWallet(chain);
|
|
3250
|
-
const feeRates = await wallet.getFeeRates();
|
|
3251
|
-
return {
|
|
3252
|
-
slow: (feeRates.slow || feeRates.low || "0").toString(),
|
|
3253
|
-
medium: (feeRates.medium || feeRates.normal || feeRates.standard || "0").toString(),
|
|
3254
|
-
fast: (feeRates.fast || feeRates.high || "0").toString()
|
|
3255
|
-
};
|
|
3256
|
-
} catch (error) {
|
|
3257
|
-
console.error(`Error fetching fee rates for ${chain}:`, error);
|
|
3258
|
-
throw error;
|
|
3259
|
-
}
|
|
3260
|
-
}
|
|
3261
|
-
/**
|
|
3262
|
-
* Get the current network configuration
|
|
3263
|
-
*/
|
|
3264
|
-
getNetwork() {
|
|
3265
|
-
return this.config.network;
|
|
3266
|
-
}
|
|
3267
|
-
/**
|
|
3268
|
-
* Check if service is initialized
|
|
3269
|
-
*/
|
|
3270
|
-
isInitialized() {
|
|
3271
|
-
return this.seed !== null;
|
|
3272
|
-
}
|
|
3273
|
-
/**
|
|
3274
|
-
* Clean up and dispose of wallet instances
|
|
3275
|
-
*/
|
|
3276
|
-
dispose() {
|
|
3277
|
-
for (const wallet of Object.values(this.wallets)) {
|
|
3278
|
-
if (wallet && typeof wallet.dispose === "function") {
|
|
3279
|
-
try {
|
|
3280
|
-
wallet.dispose();
|
|
3281
|
-
} catch {
|
|
3282
|
-
}
|
|
3283
|
-
}
|
|
3284
|
-
}
|
|
3285
|
-
this.wallets = {};
|
|
3286
|
-
this.seed = null;
|
|
3287
|
-
}
|
|
3288
|
-
};
|
|
3289
|
-
var DERIVATION_PATHS3 = {
|
|
3290
527
|
ethereum: "m/44'/60'/0'/0/0",
|
|
3291
528
|
bitcoin_mainnet: "m/84'/0'/0'/0/0",
|
|
3292
529
|
bitcoin_testnet: "m/84'/1'/0'/0/0",
|
|
@@ -3296,14 +533,14 @@ var DERIVATION_PATHS3 = {
|
|
|
3296
533
|
spark: "m/44'/998'/0'/0/0"
|
|
3297
534
|
};
|
|
3298
535
|
function deriveEthereumAddress(seed) {
|
|
3299
|
-
const hdNode = HDNodeWallet.fromPhrase(seed, void 0,
|
|
536
|
+
const hdNode = HDNodeWallet.fromPhrase(seed, void 0, DERIVATION_PATHS2.ethereum);
|
|
3300
537
|
return hdNode.address;
|
|
3301
538
|
}
|
|
3302
539
|
function deriveBitcoinAddress(seed, network = "testnet") {
|
|
3303
540
|
try {
|
|
3304
541
|
const seedBytes = mnemonicToSeedSync(seed);
|
|
3305
542
|
const hdKey = HDKey.fromMasterSeed(seedBytes);
|
|
3306
|
-
const path = network === "testnet" ?
|
|
543
|
+
const path = network === "testnet" ? DERIVATION_PATHS2.bitcoin_testnet : DERIVATION_PATHS2.bitcoin_mainnet;
|
|
3307
544
|
const child = hdKey.derive(path);
|
|
3308
545
|
if (!child.publicKey) {
|
|
3309
546
|
throw new Error("Failed to derive public key");
|
|
@@ -3322,13 +559,14 @@ function deriveBitcoinAddress(seed, network = "testnet") {
|
|
|
3322
559
|
}
|
|
3323
560
|
async function deriveSolanaAddress(seed) {
|
|
3324
561
|
try {
|
|
3325
|
-
const [ed25519, nacl,
|
|
562
|
+
const [ed25519, nacl, bs58Module] = await Promise.all([
|
|
3326
563
|
import('ed25519-hd-key'),
|
|
3327
|
-
|
|
3328
|
-
|
|
564
|
+
import('tweetnacl'),
|
|
565
|
+
import('bs58')
|
|
3329
566
|
]);
|
|
567
|
+
const bs58 = bs58Module.default || bs58Module;
|
|
3330
568
|
const seedBytes = mnemonicToSeedSync(seed);
|
|
3331
|
-
const derived = ed25519.derivePath(
|
|
569
|
+
const derived = ed25519.derivePath(DERIVATION_PATHS2.solana, Buffer.from(seedBytes).toString("hex"));
|
|
3332
570
|
const keypair = nacl.sign.keyPair.fromSeed(new Uint8Array(derived.key));
|
|
3333
571
|
return bs58.encode(keypair.publicKey);
|
|
3334
572
|
} catch (error) {
|
|
@@ -3340,10 +578,10 @@ async function deriveTonAddress(seed) {
|
|
|
3340
578
|
try {
|
|
3341
579
|
const [ed25519, nacl] = await Promise.all([
|
|
3342
580
|
import('ed25519-hd-key'),
|
|
3343
|
-
|
|
581
|
+
import('tweetnacl')
|
|
3344
582
|
]);
|
|
3345
583
|
const seedBytes = mnemonicToSeedSync(seed);
|
|
3346
|
-
const derived = ed25519.derivePath(
|
|
584
|
+
const derived = ed25519.derivePath(DERIVATION_PATHS2.ton, Buffer.from(seedBytes).toString("hex"));
|
|
3347
585
|
const keypair = nacl.sign.keyPair.fromSeed(new Uint8Array(derived.key));
|
|
3348
586
|
const publicKey = keypair.publicKey;
|
|
3349
587
|
const workchain = 0;
|
|
@@ -3376,17 +614,17 @@ function crc16(data) {
|
|
|
3376
614
|
}
|
|
3377
615
|
return crc;
|
|
3378
616
|
}
|
|
3379
|
-
|
|
617
|
+
function deriveTronAddress(seed) {
|
|
3380
618
|
try {
|
|
3381
|
-
const
|
|
3382
|
-
const hdNode = HDNodeWallet.fromPhrase(seed, void 0, DERIVATION_PATHS3.tron);
|
|
619
|
+
const hdNode = HDNodeWallet.fromPhrase(seed, void 0, DERIVATION_PATHS2.tron);
|
|
3383
620
|
const ethAddressHex = hdNode.address.slice(2).toLowerCase();
|
|
3384
621
|
const addressBytes = new Uint8Array(21);
|
|
3385
622
|
addressBytes[0] = 65;
|
|
3386
623
|
for (let i = 0; i < 20; i++) {
|
|
3387
624
|
addressBytes[i + 1] = parseInt(ethAddressHex.slice(i * 2, i * 2 + 2), 16);
|
|
3388
625
|
}
|
|
3389
|
-
|
|
626
|
+
const tronBase58check = base58check(sha256);
|
|
627
|
+
return tronBase58check.encode(addressBytes);
|
|
3390
628
|
} catch (error) {
|
|
3391
629
|
console.error("TRON address derivation failed:", error);
|
|
3392
630
|
throw error;
|
|
@@ -3396,7 +634,7 @@ function deriveSparkAddress(seed, network = "testnet") {
|
|
|
3396
634
|
try {
|
|
3397
635
|
const seedBytes = mnemonicToSeedSync(seed);
|
|
3398
636
|
const hdKey = HDKey.fromMasterSeed(seedBytes);
|
|
3399
|
-
const child = hdKey.derive(
|
|
637
|
+
const child = hdKey.derive(DERIVATION_PATHS2.spark);
|
|
3400
638
|
if (!child.publicKey) {
|
|
3401
639
|
throw new Error("Failed to derive public key");
|
|
3402
640
|
}
|
|
@@ -3436,10 +674,14 @@ async function deriveAllAddresses(seed, network = "testnet") {
|
|
|
3436
674
|
} catch (e) {
|
|
3437
675
|
console.error("Spark derivation failed:", e);
|
|
3438
676
|
}
|
|
3439
|
-
|
|
677
|
+
try {
|
|
678
|
+
addresses.tron = deriveTronAddress(seed);
|
|
679
|
+
} catch (e) {
|
|
680
|
+
console.error("TRON derivation failed:", e);
|
|
681
|
+
}
|
|
682
|
+
const [solResult, tonResult] = await Promise.allSettled([
|
|
3440
683
|
deriveSolanaAddress(seed),
|
|
3441
|
-
deriveTonAddress(seed)
|
|
3442
|
-
deriveTronAddress(seed)
|
|
684
|
+
deriveTonAddress(seed)
|
|
3443
685
|
]);
|
|
3444
686
|
if (solResult.status === "fulfilled") {
|
|
3445
687
|
addresses.solana = solResult.value;
|
|
@@ -3451,13 +693,379 @@ async function deriveAllAddresses(seed, network = "testnet") {
|
|
|
3451
693
|
} else {
|
|
3452
694
|
console.error("TON derivation failed:", tonResult.reason);
|
|
3453
695
|
}
|
|
3454
|
-
if (tronResult.status === "fulfilled") {
|
|
3455
|
-
addresses.tron = tronResult.value;
|
|
3456
|
-
} else {
|
|
3457
|
-
console.error("TRON derivation failed:", tronResult.reason);
|
|
3458
|
-
}
|
|
3459
696
|
return addresses;
|
|
3460
697
|
}
|
|
698
|
+
function isValidSeed(seed) {
|
|
699
|
+
return validateMnemonic(seed, wordlist);
|
|
700
|
+
}
|
|
701
|
+
function generateSeedPhrase() {
|
|
702
|
+
return generateMnemonic(wordlist);
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
// src/services/ZubariWdkService.ts
|
|
706
|
+
var DEFAULT_API_URL2 = "https://ckgwifsxka.us-east-2.awsapprunner.com";
|
|
707
|
+
function isBrowser() {
|
|
708
|
+
return typeof window !== "undefined" && typeof window.document !== "undefined";
|
|
709
|
+
}
|
|
710
|
+
var dynamicImport = new Function("specifier", "return import(specifier)");
|
|
711
|
+
async function canUseNativeWdk() {
|
|
712
|
+
if (isBrowser()) {
|
|
713
|
+
return false;
|
|
714
|
+
}
|
|
715
|
+
try {
|
|
716
|
+
await dynamicImport("@tetherto/wdk");
|
|
717
|
+
return true;
|
|
718
|
+
} catch {
|
|
719
|
+
return false;
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
var ZubariWdkService = class {
|
|
723
|
+
config;
|
|
724
|
+
apiClient;
|
|
725
|
+
nativeWdkService = null;
|
|
726
|
+
initialized = false;
|
|
727
|
+
useNativeWdk = false;
|
|
728
|
+
constructor(config = {}) {
|
|
729
|
+
this.config = {
|
|
730
|
+
network: config.network || "testnet",
|
|
731
|
+
apiUrl: config.apiUrl || process.env.NEXT_PUBLIC_API_URL || DEFAULT_API_URL2,
|
|
732
|
+
forceApi: config.forceApi ?? false,
|
|
733
|
+
timeout: config.timeout || 3e4
|
|
734
|
+
};
|
|
735
|
+
this.apiClient = getWdkApiClient(this.config.apiUrl);
|
|
736
|
+
}
|
|
737
|
+
/**
|
|
738
|
+
* Initialize the service and determine the best strategy
|
|
739
|
+
*/
|
|
740
|
+
async initialize() {
|
|
741
|
+
if (this.initialized) return;
|
|
742
|
+
if (isBrowser() || this.config.forceApi) {
|
|
743
|
+
this.useNativeWdk = false;
|
|
744
|
+
this.initialized = true;
|
|
745
|
+
return;
|
|
746
|
+
}
|
|
747
|
+
if (await canUseNativeWdk()) {
|
|
748
|
+
try {
|
|
749
|
+
const WdkServiceModule = await dynamicImport("./WdkService");
|
|
750
|
+
const WdkService = WdkServiceModule.WdkService || WdkServiceModule.default;
|
|
751
|
+
this.nativeWdkService = new WdkService({
|
|
752
|
+
network: this.config.network
|
|
753
|
+
});
|
|
754
|
+
this.useNativeWdk = true;
|
|
755
|
+
} catch (error) {
|
|
756
|
+
console.warn("Failed to initialize native WDK, falling back to API:", error);
|
|
757
|
+
this.useNativeWdk = false;
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
this.initialized = true;
|
|
761
|
+
}
|
|
762
|
+
/**
|
|
763
|
+
* Get the current execution mode
|
|
764
|
+
*/
|
|
765
|
+
getMode() {
|
|
766
|
+
if (this.useNativeWdk) return "native";
|
|
767
|
+
if (isBrowser()) return "api";
|
|
768
|
+
return "api";
|
|
769
|
+
}
|
|
770
|
+
/**
|
|
771
|
+
* Check if running in browser
|
|
772
|
+
*/
|
|
773
|
+
isBrowserEnvironment() {
|
|
774
|
+
return isBrowser();
|
|
775
|
+
}
|
|
776
|
+
/**
|
|
777
|
+
* Generate a new BIP-39 seed phrase (12 words)
|
|
778
|
+
*/
|
|
779
|
+
async generateSeed() {
|
|
780
|
+
await this.initialize();
|
|
781
|
+
try {
|
|
782
|
+
const response = await this.apiClient.generateSeed();
|
|
783
|
+
if (response.success && response.seed) {
|
|
784
|
+
return response.seed;
|
|
785
|
+
}
|
|
786
|
+
} catch (error) {
|
|
787
|
+
console.warn("API seed generation failed:", error);
|
|
788
|
+
}
|
|
789
|
+
if (this.useNativeWdk && this.nativeWdkService) {
|
|
790
|
+
try {
|
|
791
|
+
const wdk = this.nativeWdkService;
|
|
792
|
+
return await wdk.generateSeedPhrase();
|
|
793
|
+
} catch (error) {
|
|
794
|
+
console.warn("Native WDK seed generation failed:", error);
|
|
795
|
+
}
|
|
796
|
+
}
|
|
797
|
+
return generateSeedPhrase();
|
|
798
|
+
}
|
|
799
|
+
/**
|
|
800
|
+
* Validate a BIP-39 seed phrase
|
|
801
|
+
*/
|
|
802
|
+
async validateSeed(seed) {
|
|
803
|
+
await this.initialize();
|
|
804
|
+
try {
|
|
805
|
+
const response = await this.apiClient.validateSeed(seed);
|
|
806
|
+
if (response.success) {
|
|
807
|
+
return response.isValid ?? false;
|
|
808
|
+
}
|
|
809
|
+
} catch (error) {
|
|
810
|
+
console.warn("API seed validation failed:", error);
|
|
811
|
+
}
|
|
812
|
+
if (this.useNativeWdk && this.nativeWdkService) {
|
|
813
|
+
try {
|
|
814
|
+
const wdk = this.nativeWdkService;
|
|
815
|
+
return await wdk.isValidSeed(seed);
|
|
816
|
+
} catch (error) {
|
|
817
|
+
console.warn("Native WDK seed validation failed:", error);
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
return isValidSeed(seed);
|
|
821
|
+
}
|
|
822
|
+
/**
|
|
823
|
+
* Derive address for a specific chain
|
|
824
|
+
*/
|
|
825
|
+
async deriveAddress(seed, chain) {
|
|
826
|
+
await this.initialize();
|
|
827
|
+
const path = this.getDerivationPath(chain);
|
|
828
|
+
try {
|
|
829
|
+
const response = await this.apiClient.deriveAddress(seed, chain, this.config.network);
|
|
830
|
+
if (response.success && response.address) {
|
|
831
|
+
return {
|
|
832
|
+
chain,
|
|
833
|
+
address: response.address,
|
|
834
|
+
path: response.path || path
|
|
835
|
+
};
|
|
836
|
+
}
|
|
837
|
+
} catch (error) {
|
|
838
|
+
console.warn(`API address derivation failed for ${chain}:`, error);
|
|
839
|
+
}
|
|
840
|
+
if (this.useNativeWdk && this.nativeWdkService) {
|
|
841
|
+
try {
|
|
842
|
+
const wdk = this.nativeWdkService;
|
|
843
|
+
await wdk.initialize(seed);
|
|
844
|
+
return await wdk.deriveAddress(chain);
|
|
845
|
+
} catch (error) {
|
|
846
|
+
console.warn(`Native WDK address derivation failed for ${chain}:`, error);
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
return this.deriveBrowserAddress(seed, chain);
|
|
850
|
+
}
|
|
851
|
+
/**
|
|
852
|
+
* Derive addresses for all supported chains
|
|
853
|
+
*/
|
|
854
|
+
async deriveAllAddresses(seed) {
|
|
855
|
+
await this.initialize();
|
|
856
|
+
try {
|
|
857
|
+
const response = await this.apiClient.deriveAllAddresses(seed, this.config.network);
|
|
858
|
+
if (response.success && response.addresses) {
|
|
859
|
+
return {
|
|
860
|
+
ethereum: response.addresses.ethereum || null,
|
|
861
|
+
bitcoin: response.addresses.bitcoin || null,
|
|
862
|
+
ton: response.addresses.ton || null,
|
|
863
|
+
tron: response.addresses.tron || null,
|
|
864
|
+
solana: response.addresses.solana || null,
|
|
865
|
+
spark: response.addresses.spark || null
|
|
866
|
+
};
|
|
867
|
+
}
|
|
868
|
+
} catch (error) {
|
|
869
|
+
console.warn("API address derivation failed:", error);
|
|
870
|
+
}
|
|
871
|
+
if (this.useNativeWdk && this.nativeWdkService) {
|
|
872
|
+
try {
|
|
873
|
+
const wdk = this.nativeWdkService;
|
|
874
|
+
await wdk.initialize(seed);
|
|
875
|
+
return await wdk.deriveAllAddresses();
|
|
876
|
+
} catch (error) {
|
|
877
|
+
console.warn("Native WDK multi-chain derivation failed:", error);
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
return this.deriveAllBrowserAddresses(seed);
|
|
881
|
+
}
|
|
882
|
+
/**
|
|
883
|
+
* Get balances for all chains
|
|
884
|
+
*/
|
|
885
|
+
async getAllBalances(seed) {
|
|
886
|
+
await this.initialize();
|
|
887
|
+
try {
|
|
888
|
+
const response = await fetch(`${this.config.apiUrl}/api/wallets/wdk/balances`, {
|
|
889
|
+
method: "POST",
|
|
890
|
+
headers: { "Content-Type": "application/json" },
|
|
891
|
+
body: JSON.stringify({ seed, network: this.config.network })
|
|
892
|
+
});
|
|
893
|
+
if (response.ok) {
|
|
894
|
+
const data = await response.json();
|
|
895
|
+
if (data.success) {
|
|
896
|
+
return data.balances;
|
|
897
|
+
}
|
|
898
|
+
}
|
|
899
|
+
} catch (error) {
|
|
900
|
+
console.warn("Failed to fetch balances:", error);
|
|
901
|
+
}
|
|
902
|
+
return {};
|
|
903
|
+
}
|
|
904
|
+
/**
|
|
905
|
+
* Get fee rates for a chain
|
|
906
|
+
*/
|
|
907
|
+
async getFeeRates(seed, chain) {
|
|
908
|
+
await this.initialize();
|
|
909
|
+
try {
|
|
910
|
+
const response = await fetch(`${this.config.apiUrl}/api/wallets/wdk/fee-rates`, {
|
|
911
|
+
method: "POST",
|
|
912
|
+
headers: { "Content-Type": "application/json" },
|
|
913
|
+
body: JSON.stringify({ seed, chain, network: this.config.network })
|
|
914
|
+
});
|
|
915
|
+
if (response.ok) {
|
|
916
|
+
const data = await response.json();
|
|
917
|
+
if (data.success && data.feeRates) {
|
|
918
|
+
return data.feeRates;
|
|
919
|
+
}
|
|
920
|
+
}
|
|
921
|
+
} catch (error) {
|
|
922
|
+
console.warn(`Failed to fetch fee rates for ${chain}:`, error);
|
|
923
|
+
}
|
|
924
|
+
return { slow: "0", normal: "0", fast: "0" };
|
|
925
|
+
}
|
|
926
|
+
/**
|
|
927
|
+
* Estimate transaction fee
|
|
928
|
+
*/
|
|
929
|
+
async estimateFee(seed, chain, to, amount) {
|
|
930
|
+
await this.initialize();
|
|
931
|
+
try {
|
|
932
|
+
const response = await fetch(`${this.config.apiUrl}/api/wallets/wdk/estimate-fee`, {
|
|
933
|
+
method: "POST",
|
|
934
|
+
headers: { "Content-Type": "application/json" },
|
|
935
|
+
body: JSON.stringify({ seed, chain, to, amount, network: this.config.network })
|
|
936
|
+
});
|
|
937
|
+
if (response.ok) {
|
|
938
|
+
const data = await response.json();
|
|
939
|
+
if (data.success) {
|
|
940
|
+
return { fee: data.fee, symbol: data.symbol };
|
|
941
|
+
}
|
|
942
|
+
}
|
|
943
|
+
} catch (error) {
|
|
944
|
+
console.warn(`Failed to estimate fee for ${chain}:`, error);
|
|
945
|
+
}
|
|
946
|
+
return { fee: "0", symbol: this.getChainSymbol(chain) };
|
|
947
|
+
}
|
|
948
|
+
/**
|
|
949
|
+
* Send a transaction
|
|
950
|
+
*/
|
|
951
|
+
async sendTransaction(seed, chain, to, amount) {
|
|
952
|
+
await this.initialize();
|
|
953
|
+
try {
|
|
954
|
+
const response = await fetch(`${this.config.apiUrl}/api/wallets/wdk/send`, {
|
|
955
|
+
method: "POST",
|
|
956
|
+
headers: { "Content-Type": "application/json" },
|
|
957
|
+
body: JSON.stringify({ seed, chain, to, amount, network: this.config.network })
|
|
958
|
+
});
|
|
959
|
+
if (response.ok) {
|
|
960
|
+
const data = await response.json();
|
|
961
|
+
return {
|
|
962
|
+
success: data.success,
|
|
963
|
+
txHash: data.txHash,
|
|
964
|
+
from: data.from,
|
|
965
|
+
to: data.to,
|
|
966
|
+
amount: data.amount,
|
|
967
|
+
chain: data.chain,
|
|
968
|
+
network: data.network
|
|
969
|
+
};
|
|
970
|
+
}
|
|
971
|
+
const errorData = await response.json().catch(() => ({}));
|
|
972
|
+
return {
|
|
973
|
+
success: false,
|
|
974
|
+
error: errorData.error || `HTTP ${response.status}`
|
|
975
|
+
};
|
|
976
|
+
} catch (error) {
|
|
977
|
+
return {
|
|
978
|
+
success: false,
|
|
979
|
+
error: error instanceof Error ? error.message : "Transaction failed"
|
|
980
|
+
};
|
|
981
|
+
}
|
|
982
|
+
}
|
|
983
|
+
/**
|
|
984
|
+
* Get the network configuration
|
|
985
|
+
*/
|
|
986
|
+
getNetwork() {
|
|
987
|
+
return this.config.network;
|
|
988
|
+
}
|
|
989
|
+
/**
|
|
990
|
+
* Get API URL
|
|
991
|
+
*/
|
|
992
|
+
getApiUrl() {
|
|
993
|
+
return this.config.apiUrl;
|
|
994
|
+
}
|
|
995
|
+
// ==========================================
|
|
996
|
+
// Private Helper Methods
|
|
997
|
+
// ==========================================
|
|
998
|
+
getDerivationPath(chain) {
|
|
999
|
+
const paths = {
|
|
1000
|
+
bitcoin: this.config.network === "testnet" ? "m/84'/1'/0'/0/0" : "m/84'/0'/0'/0/0",
|
|
1001
|
+
ethereum: "m/44'/60'/0'/0/0",
|
|
1002
|
+
ton: "m/44'/607'/0'/0'/0'",
|
|
1003
|
+
tron: "m/44'/195'/0'/0/0",
|
|
1004
|
+
solana: "m/44'/501'/0'/0'",
|
|
1005
|
+
spark: "m/44'/998'/0'/0/0"
|
|
1006
|
+
};
|
|
1007
|
+
return paths[chain];
|
|
1008
|
+
}
|
|
1009
|
+
getChainSymbol(chain) {
|
|
1010
|
+
const symbols = {
|
|
1011
|
+
ethereum: "ETH",
|
|
1012
|
+
bitcoin: "BTC",
|
|
1013
|
+
ton: "TON",
|
|
1014
|
+
tron: "TRX",
|
|
1015
|
+
solana: "SOL",
|
|
1016
|
+
spark: "SAT"
|
|
1017
|
+
};
|
|
1018
|
+
return symbols[chain];
|
|
1019
|
+
}
|
|
1020
|
+
/**
|
|
1021
|
+
* Derive address using browser-compatible libraries
|
|
1022
|
+
*/
|
|
1023
|
+
async deriveBrowserAddress(seed, chain) {
|
|
1024
|
+
const path = this.getDerivationPath(chain);
|
|
1025
|
+
try {
|
|
1026
|
+
let address;
|
|
1027
|
+
switch (chain) {
|
|
1028
|
+
case "ethereum":
|
|
1029
|
+
address = deriveEthereumAddress(seed);
|
|
1030
|
+
break;
|
|
1031
|
+
case "bitcoin":
|
|
1032
|
+
address = deriveBitcoinAddress(seed, this.config.network);
|
|
1033
|
+
break;
|
|
1034
|
+
case "tron":
|
|
1035
|
+
address = deriveTronAddress(seed);
|
|
1036
|
+
break;
|
|
1037
|
+
case "spark":
|
|
1038
|
+
address = deriveSparkAddress(seed, this.config.network);
|
|
1039
|
+
break;
|
|
1040
|
+
case "solana":
|
|
1041
|
+
address = await deriveSolanaAddress(seed);
|
|
1042
|
+
break;
|
|
1043
|
+
case "ton":
|
|
1044
|
+
address = await deriveTonAddress(seed);
|
|
1045
|
+
break;
|
|
1046
|
+
default:
|
|
1047
|
+
throw new Error(`Unsupported chain: ${chain}`);
|
|
1048
|
+
}
|
|
1049
|
+
return { chain, address, path };
|
|
1050
|
+
} catch (error) {
|
|
1051
|
+
console.error(`Browser derivation failed for ${chain}:`, error);
|
|
1052
|
+
throw error;
|
|
1053
|
+
}
|
|
1054
|
+
}
|
|
1055
|
+
/**
|
|
1056
|
+
* Derive all addresses using browser-compatible libraries
|
|
1057
|
+
*/
|
|
1058
|
+
async deriveAllBrowserAddresses(seed) {
|
|
1059
|
+
return deriveAllAddresses(seed, this.config.network);
|
|
1060
|
+
}
|
|
1061
|
+
};
|
|
1062
|
+
var defaultService = null;
|
|
1063
|
+
function getZubariWdkService(config) {
|
|
1064
|
+
if (!defaultService || config && config.network !== defaultService.getNetwork()) {
|
|
1065
|
+
defaultService = new ZubariWdkService(config);
|
|
1066
|
+
}
|
|
1067
|
+
return defaultService;
|
|
1068
|
+
}
|
|
3461
1069
|
|
|
3462
1070
|
// src/wallet/WalletManager.ts
|
|
3463
1071
|
var STORAGE_KEYS = {
|
|
@@ -3481,11 +1089,12 @@ var WalletManager = class _WalletManager {
|
|
|
3481
1089
|
rpcUrl: config.rpcUrl || ethereumConfig.rpcUrl,
|
|
3482
1090
|
storage: config.storage || createSecureStorage(),
|
|
3483
1091
|
enabledChains: config.enabledChains || SUPPORTED_CHAINS,
|
|
3484
|
-
apiUrl: config.apiUrl || process.env.NEXT_PUBLIC_API_URL || "
|
|
1092
|
+
apiUrl: config.apiUrl || process.env.NEXT_PUBLIC_API_URL || "https://ckgwifsxka.us-east-2.awsapprunner.com"
|
|
3485
1093
|
};
|
|
3486
1094
|
this.storage = this.config.storage;
|
|
3487
|
-
this.wdkService =
|
|
3488
|
-
network: this.config.network
|
|
1095
|
+
this.wdkService = getZubariWdkService({
|
|
1096
|
+
network: this.config.network,
|
|
1097
|
+
apiUrl: this.config.apiUrl
|
|
3489
1098
|
});
|
|
3490
1099
|
}
|
|
3491
1100
|
/**
|
|
@@ -3501,17 +1110,17 @@ var WalletManager = class _WalletManager {
|
|
|
3501
1110
|
return mnemonic.phrase;
|
|
3502
1111
|
}
|
|
3503
1112
|
/**
|
|
3504
|
-
* Generate a new BIP-39 seed phrase using
|
|
1113
|
+
* Generate a new BIP-39 seed phrase using Tether WDK (via API in browser)
|
|
3505
1114
|
* This is the recommended method for generating seed phrases
|
|
3506
1115
|
*/
|
|
3507
1116
|
async generateSeedWithWdk() {
|
|
3508
|
-
return await this.wdkService.
|
|
1117
|
+
return await this.wdkService.generateSeed();
|
|
3509
1118
|
}
|
|
3510
1119
|
/**
|
|
3511
|
-
* Validate seed phrase using
|
|
1120
|
+
* Validate seed phrase using WDK (async, more accurate)
|
|
3512
1121
|
*/
|
|
3513
1122
|
async validateSeedWithWdk(seed) {
|
|
3514
|
-
return await this.wdkService.
|
|
1123
|
+
return await this.wdkService.validateSeed(seed);
|
|
3515
1124
|
}
|
|
3516
1125
|
/**
|
|
3517
1126
|
* Validate a BIP-39 seed phrase
|
|
@@ -3590,7 +1199,6 @@ var WalletManager = class _WalletManager {
|
|
|
3590
1199
|
*/
|
|
3591
1200
|
lock() {
|
|
3592
1201
|
this.currentSeed = null;
|
|
3593
|
-
this.wdkService.dispose();
|
|
3594
1202
|
}
|
|
3595
1203
|
/**
|
|
3596
1204
|
* Check if wallet exists in storage
|
|
@@ -3606,7 +1214,6 @@ var WalletManager = class _WalletManager {
|
|
|
3606
1214
|
this.currentSeed = null;
|
|
3607
1215
|
this.derivedAddress = null;
|
|
3608
1216
|
this.derivedAddresses = {};
|
|
3609
|
-
this.wdkService.dispose();
|
|
3610
1217
|
}
|
|
3611
1218
|
/**
|
|
3612
1219
|
* Get current wallet state
|
|
@@ -3706,19 +1313,18 @@ var WalletManager = class _WalletManager {
|
|
|
3706
1313
|
/**
|
|
3707
1314
|
* Derive address for a specific chain (async version)
|
|
3708
1315
|
*
|
|
3709
|
-
*
|
|
3710
|
-
*
|
|
1316
|
+
* Uses the unified WDK service which:
|
|
1317
|
+
* - In browser: Calls the backend API (which has Tether WDK)
|
|
1318
|
+
* - In Node.js: Uses native WDK
|
|
1319
|
+
* - Falls back to browser-compatible derivation if needed
|
|
3711
1320
|
*/
|
|
3712
1321
|
static async deriveAddressForChainAsync(seed, chain, network = "testnet", apiUrl) {
|
|
3713
1322
|
try {
|
|
3714
|
-
const
|
|
3715
|
-
const
|
|
3716
|
-
|
|
3717
|
-
return response.address;
|
|
3718
|
-
}
|
|
3719
|
-
console.warn(`WDK API failed for ${chain}, using local derivation:`, response.error);
|
|
1323
|
+
const wdkService = getZubariWdkService({ network, apiUrl });
|
|
1324
|
+
const result = await wdkService.deriveAddress(seed, chain);
|
|
1325
|
+
return result.address;
|
|
3720
1326
|
} catch (error) {
|
|
3721
|
-
console.warn(`WDK
|
|
1327
|
+
console.warn(`WDK service failed for ${chain}, using local derivation:`, error);
|
|
3722
1328
|
}
|
|
3723
1329
|
return _WalletManager.deriveAddressForChain(seed, chain);
|
|
3724
1330
|
}
|
|
@@ -3814,8 +1420,9 @@ var WalletManager = class _WalletManager {
|
|
|
3814
1420
|
return addresses;
|
|
3815
1421
|
}
|
|
3816
1422
|
/**
|
|
3817
|
-
* Derive addresses for all enabled chains using
|
|
3818
|
-
*
|
|
1423
|
+
* Derive addresses for all enabled chains using Tether WDK
|
|
1424
|
+
* In browser: Uses the backend API (which has Tether WDK)
|
|
1425
|
+
* In Node.js: Uses native WDK directly
|
|
3819
1426
|
* Returns REAL cryptographically valid addresses for all chains.
|
|
3820
1427
|
*/
|
|
3821
1428
|
async deriveAllAddressesWithWdk() {
|
|
@@ -3823,9 +1430,8 @@ var WalletManager = class _WalletManager {
|
|
|
3823
1430
|
throw new Error("Wallet is locked");
|
|
3824
1431
|
}
|
|
3825
1432
|
try {
|
|
3826
|
-
await this.wdkService.
|
|
1433
|
+
const wdkAddresses = await this.wdkService.deriveAllAddresses(this.currentSeed);
|
|
3827
1434
|
const enabledChainsSet = new Set(this.config.enabledChains);
|
|
3828
|
-
const wdkAddresses = await this.wdkService.deriveAllAddresses();
|
|
3829
1435
|
const addresses = {};
|
|
3830
1436
|
for (const [chain, address] of Object.entries(wdkAddresses)) {
|
|
3831
1437
|
if (enabledChainsSet.has(chain) && address) {
|
|
@@ -3835,13 +1441,15 @@ var WalletManager = class _WalletManager {
|
|
|
3835
1441
|
this.derivedAddresses = addresses;
|
|
3836
1442
|
return addresses;
|
|
3837
1443
|
} catch (error) {
|
|
3838
|
-
console.error("
|
|
1444
|
+
console.error("WDK derivation failed:", error);
|
|
3839
1445
|
throw error;
|
|
3840
1446
|
}
|
|
3841
1447
|
}
|
|
3842
1448
|
/**
|
|
3843
1449
|
* Derive addresses for all enabled chains using Tether WDK
|
|
3844
|
-
*
|
|
1450
|
+
* The unified WDK service handles all fallback strategies automatically:
|
|
1451
|
+
* - Browser: API backend -> Browser derivation
|
|
1452
|
+
* - Node.js: Native WDK -> API backend -> Browser derivation
|
|
3845
1453
|
*/
|
|
3846
1454
|
async deriveAllAddressesAsync() {
|
|
3847
1455
|
if (!this.currentSeed) {
|
|
@@ -3849,44 +1457,8 @@ var WalletManager = class _WalletManager {
|
|
|
3849
1457
|
}
|
|
3850
1458
|
try {
|
|
3851
1459
|
return await this.deriveAllAddressesWithWdk();
|
|
3852
|
-
} catch (
|
|
3853
|
-
console.
|
|
3854
|
-
}
|
|
3855
|
-
try {
|
|
3856
|
-
const wdkApi = getWdkApiClient(this.config.apiUrl);
|
|
3857
|
-
const response = await wdkApi.deriveAllAddresses(this.currentSeed, this.config.network);
|
|
3858
|
-
if (response.success && response.addresses) {
|
|
3859
|
-
const addresses = {};
|
|
3860
|
-
for (const chain of this.config.enabledChains) {
|
|
3861
|
-
const address = response.addresses[chain];
|
|
3862
|
-
if (address) {
|
|
3863
|
-
addresses[chain] = address;
|
|
3864
|
-
}
|
|
3865
|
-
}
|
|
3866
|
-
this.derivedAddresses = addresses;
|
|
3867
|
-
return addresses;
|
|
3868
|
-
}
|
|
3869
|
-
console.warn("WDK API call failed:", response.error);
|
|
3870
|
-
} catch (apiError) {
|
|
3871
|
-
console.warn("WDK API unavailable:", apiError);
|
|
3872
|
-
}
|
|
3873
|
-
console.log("Using browser-compatible address derivation...");
|
|
3874
|
-
try {
|
|
3875
|
-
const browserAddresses = await deriveAllAddresses(
|
|
3876
|
-
this.currentSeed,
|
|
3877
|
-
this.config.network
|
|
3878
|
-
);
|
|
3879
|
-
const addresses = {};
|
|
3880
|
-
for (const chain of this.config.enabledChains) {
|
|
3881
|
-
const address = browserAddresses[chain];
|
|
3882
|
-
if (address) {
|
|
3883
|
-
addresses[chain] = address;
|
|
3884
|
-
}
|
|
3885
|
-
}
|
|
3886
|
-
this.derivedAddresses = addresses;
|
|
3887
|
-
return addresses;
|
|
3888
|
-
} catch (browserError) {
|
|
3889
|
-
console.error("Browser derivation failed:", browserError);
|
|
1460
|
+
} catch (error) {
|
|
1461
|
+
console.error("All derivation strategies failed:", error);
|
|
3890
1462
|
const ethAddress = _WalletManager.deriveAddress(this.currentSeed);
|
|
3891
1463
|
this.derivedAddresses = { ethereum: ethAddress };
|
|
3892
1464
|
return this.derivedAddresses;
|