@bounded-sh/client 0.0.7 → 0.0.9

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.
Files changed (32) hide show
  1. package/dist/auth/index.d.ts +15 -0
  2. package/dist/auth/oidc-auth.d.ts +25 -11
  3. package/dist/auth/providers/email-auth-provider.d.ts +10 -0
  4. package/dist/auth/providers/mock-auth-provider.d.ts +1 -0
  5. package/dist/index.d.ts +2 -1
  6. package/dist/index.esm.js +483 -2615
  7. package/dist/index.esm.js.map +1 -1
  8. package/dist/index.js +484 -2614
  9. package/dist/index.js.map +1 -1
  10. package/dist/{index.native-CCc03Zxr.js → index.native-C-duG1XV.js} +488 -2618
  11. package/dist/index.native-C-duG1XV.js.map +1 -0
  12. package/dist/{index.native-BxNuEqgy.esm.js → index.native-DnVLH-7o.esm.js} +487 -2619
  13. package/dist/index.native-DnVLH-7o.esm.js.map +1 -0
  14. package/dist/index.native.d.ts +2 -1
  15. package/dist/index.native.esm.js +1 -1
  16. package/dist/index.native.js +3 -1
  17. package/dist/index.native.js.map +1 -1
  18. package/dist/{phantom-wallet-provider-yMlFdfKn.js → phantom-wallet-provider-DKScS8m6.js} +2 -2
  19. package/dist/{phantom-wallet-provider-yMlFdfKn.js.map → phantom-wallet-provider-DKScS8m6.js.map} +1 -1
  20. package/dist/{phantom-wallet-provider-CxnVOBTM.esm.js → phantom-wallet-provider-DfTqdjx7.esm.js} +2 -2
  21. package/dist/{phantom-wallet-provider-CxnVOBTM.esm.js.map → phantom-wallet-provider-DfTqdjx7.esm.js.map} +1 -1
  22. package/dist/{privy-wallet-provider-BbjUoSdY.esm.js → privy-wallet-provider-BpPAZyUD.esm.js} +3 -3
  23. package/dist/{privy-wallet-provider-BbjUoSdY.esm.js.map → privy-wallet-provider-BpPAZyUD.esm.js.map} +1 -1
  24. package/dist/{privy-wallet-provider-BcbjdOfk.js → privy-wallet-provider-wCTdqpRq.js} +3 -3
  25. package/dist/{privy-wallet-provider-BcbjdOfk.js.map → privy-wallet-provider-wCTdqpRq.js.map} +1 -1
  26. package/dist/{solana-mobile-wallet-provider-BCoTvY5g.js → solana-mobile-wallet-provider-DZx3xVit.js} +2 -2
  27. package/dist/{solana-mobile-wallet-provider-BCoTvY5g.js.map → solana-mobile-wallet-provider-DZx3xVit.js.map} +1 -1
  28. package/dist/{solana-mobile-wallet-provider-wl_xDwIC.esm.js → solana-mobile-wallet-provider-xmASWrPf.esm.js} +2 -2
  29. package/dist/{solana-mobile-wallet-provider-wl_xDwIC.esm.js.map → solana-mobile-wallet-provider-xmASWrPf.esm.js.map} +1 -1
  30. package/package.json +2 -5
  31. package/dist/index.native-BxNuEqgy.esm.js.map +0 -1
  32. package/dist/index.native-CCc03Zxr.js.map +0 -1
package/dist/index.esm.js CHANGED
@@ -17,7 +17,7 @@ function commonjsRequire(path) {
17
17
  throw new Error('Could not dynamically require "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.');
18
18
  }
19
19
 
20
- var naclFast$1 = {exports: {}};
20
+ var naclFast = {exports: {}};
21
21
 
22
22
  var _nodeResolve_empty = {};
23
23
 
@@ -28,11 +28,11 @@ var _nodeResolve_empty$1 = /*#__PURE__*/Object.freeze({
28
28
 
29
29
  var require$$0 = /*@__PURE__*/getDefaultExportFromNamespaceIfNotNamed(_nodeResolve_empty$1);
30
30
 
31
- var hasRequiredNaclFast$1;
31
+ var hasRequiredNaclFast;
32
32
 
33
- function requireNaclFast$1 () {
34
- if (hasRequiredNaclFast$1) return naclFast$1.exports;
35
- hasRequiredNaclFast$1 = 1;
33
+ function requireNaclFast () {
34
+ if (hasRequiredNaclFast) return naclFast.exports;
35
+ hasRequiredNaclFast = 1;
36
36
  (function (module) {
37
37
  (function(nacl) {
38
38
 
@@ -2424,12 +2424,12 @@ function requireNaclFast$1 () {
2424
2424
  })();
2425
2425
 
2426
2426
  })(module.exports ? module.exports : (self.nacl = self.nacl || {}));
2427
- } (naclFast$1));
2428
- return naclFast$1.exports;
2427
+ } (naclFast));
2428
+ return naclFast.exports;
2429
2429
  }
2430
2430
 
2431
- var naclFastExports$1 = requireNaclFast$1();
2432
- var nacl$1 = /*@__PURE__*/getDefaultExportFromCjs$1(naclFastExports$1);
2431
+ var naclFastExports = requireNaclFast();
2432
+ var nacl = /*@__PURE__*/getDefaultExportFromCjs$1(naclFastExports);
2433
2433
 
2434
2434
  var bn$1 = {exports: {}};
2435
2435
 
@@ -6394,10 +6394,13 @@ let clientConfig = {
6394
6394
  humanAuthApiUrl: 'https://auth.bounded.sh',
6395
6395
  functionsUrl: 'https://functions.bounded.sh',
6396
6396
  appId: '',
6397
- // 'email' = Bounded Better Auth human login (inline OTP) — the out-of-box default
6398
- // for most apps. For wallet login use authMethod:'phantom' (Solana / Phantom, the
6399
- // recommended wallet option), or signInAnonymously() for zero-friction 'guest'
6400
- // accounts — all coexist. ('wallet' is an unimplemented stub; don't use.)
6397
+ // 'email' = Bounded Auth human login (inline email OTP) — the out-of-box default
6398
+ // for normal apps. Hosted OAuth/social uses loginWithRedirect/loginWithPopup.
6399
+ // Text OTP is off by default and uses hosted/headless text helpers only when
6400
+ // Bounded explicitly enables it for the issuer. For
6401
+ // crypto/onchain wallet login use authMethod:'phantom' (Solana / Phantom), or
6402
+ // signInAnonymously() for zero-friction 'guest' accounts. ('wallet' is an
6403
+ // unimplemented stub; don't use.)
6401
6404
  authMethod: 'email',
6402
6405
  chain: '',
6403
6406
  rpcUrl: '',
@@ -10212,7 +10215,7 @@ class ServerSessionManager {
10212
10215
  const nonce = await genAuthNonce();
10213
10216
  const message = await genSolanaMessage(address, nonce);
10214
10217
  /* sign the message */
10215
- const sigBytes = nacl$1.sign.detached(new TextEncoder().encode(message), kp.secretKey);
10218
+ const sigBytes = nacl.sign.detached(new TextEncoder().encode(message), kp.secretKey);
10216
10219
  const signature = bufferExports$1.Buffer.from(sigBytes).toString("base64");
10217
10220
  /* call auth API */
10218
10221
  const { accessToken, idToken, refreshToken, } = await createSessionWithSignature(address, message, signature);
@@ -10597,7 +10600,7 @@ async function makeApiRequest(method, urlPath, data, _overrides) {
10597
10600
  }
10598
10601
  }
10599
10602
 
10600
- var __rest = function (s, e) {
10603
+ var __rest$1 = function (s, e) {
10601
10604
  var t = {};
10602
10605
  for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
10603
10606
  t[p] = s[p];
@@ -11072,7 +11075,7 @@ async function search(path, query, opts = {}) {
11072
11075
  normalizedPath = normalizedPath.slice(0, -1);
11073
11076
  }
11074
11077
  if (!normalizedPath || normalizedPath.length === 0) {
11075
- return new Error("Invalid path provided.");
11078
+ throw new Error("Invalid path provided.");
11076
11079
  }
11077
11080
  if (typeof query !== "string" || query.trim().length === 0) {
11078
11081
  throw new Error("search query must be a non-empty string");
@@ -11099,7 +11102,7 @@ async function get(path, opts = {}) {
11099
11102
  normalizedPath = normalizedPath.slice(0, -1);
11100
11103
  }
11101
11104
  if (!normalizedPath || normalizedPath.length === 0) {
11102
- return new Error("Invalid path provided.");
11105
+ throw new Error("Invalid path provided.");
11103
11106
  }
11104
11107
  // Create cache key combining path, prompt, filter, sort, includeSubPaths,
11105
11108
  // shape, limit, cursor — and (H1) the caller's appId + principal fingerprint,
@@ -11227,6 +11230,23 @@ function cleanupExpiredCache() {
11227
11230
  });
11228
11231
  lastCacheCleanup = now;
11229
11232
  }
11233
+ function classifyGetManyBatchError(error) {
11234
+ var _a, _b, _c;
11235
+ const err = error;
11236
+ const status = (_b = (_a = err === null || err === void 0 ? void 0 : err.status) !== null && _a !== void 0 ? _a : err === null || err === void 0 ? void 0 : err.statusCode) !== null && _b !== void 0 ? _b : (_c = err === null || err === void 0 ? void 0 : err.response) === null || _c === void 0 ? void 0 : _c.status;
11237
+ const message = error instanceof Error
11238
+ ? error.message
11239
+ : typeof (err === null || err === void 0 ? void 0 : err.message) === 'string'
11240
+ ? err.message
11241
+ : 'Unknown error';
11242
+ if (status === 401 || status === 403) {
11243
+ return { code: 'UNAUTHORIZED', message };
11244
+ }
11245
+ if (status === 400) {
11246
+ return { code: 'INVALID_PATH', message };
11247
+ }
11248
+ return { code: 'REQUEST_FAILED', message };
11249
+ }
11230
11250
  async function getMany(paths, opts = {}) {
11231
11251
  var _a, _b, _c, _d, _e;
11232
11252
  if (paths.length === 0) {
@@ -11271,6 +11291,11 @@ async function getMany(paths, opts = {}) {
11271
11291
  if (uncachedPaths.length > 0) {
11272
11292
  try {
11273
11293
  const response = await makeApiRequest('POST', 'items/batch', { paths: uncachedPaths }, opts._overrides);
11294
+ if (response.status === 404 && response.data == null) {
11295
+ const endpointError = new Error('Batch read endpoint returned 404');
11296
+ endpointError.status = 404;
11297
+ throw endpointError;
11298
+ }
11274
11299
  // makeApiRequest returns `{ data: <httpBody> }`, and the worker's items/batch
11275
11300
  // httpBody is `{ data: { results: [...] }, status }` — so the results are
11276
11301
  // double-nested at response.data.data.results. (Reading response.data.results
@@ -11313,11 +11338,12 @@ async function getMany(paths, opts = {}) {
11313
11338
  }
11314
11339
  }
11315
11340
  catch (error) {
11341
+ const batchError = classifyGetManyBatchError(error);
11316
11342
  for (const originalIndex of uncachedIndices) {
11317
11343
  results[originalIndex] = {
11318
11344
  path: normalizedPaths[originalIndex],
11319
11345
  data: null,
11320
- error: { code: 'NOT_FOUND', message: error instanceof Error ? error.message : 'Unknown error' }
11346
+ error: batchError,
11321
11347
  };
11322
11348
  }
11323
11349
  }
@@ -11479,26 +11505,23 @@ async function setMany(many, options) {
11479
11505
  return Object.assign(Object.assign({}, documents.map(d => d.document)), { transactionId: transactionResult.signature, signedTransaction: transactionResult.signedTransaction });
11480
11506
  }
11481
11507
  // Handle Solana on-chain transaction flow
11482
- let lastTxSignature = undefined;
11483
- let signedTransaction = undefined;
11484
- for (let i = 0; i < transactions.length; i++) {
11485
- const curTx = transactions[i];
11486
- let transactionResult;
11487
- if (curTx.serializedTransaction) {
11488
- transactionResult = await handlePreBuiltTransaction(curTx, authProvider, options);
11489
- }
11490
- else {
11491
- transactionResult = await handleSolanaTransaction(curTx, authProvider, options);
11492
- }
11493
- lastTxSignature = transactionResult.transactionSignature;
11494
- signedTransaction = transactionResult.signedTransaction;
11508
+ if (!Array.isArray(transactions) || transactions.length !== 1) {
11509
+ throw new Error(`Expected exactly one on-chain transaction, received ${Array.isArray(transactions) ? transactions.length : 0}`);
11510
+ }
11511
+ const curTx = transactions[0];
11512
+ let transactionResult;
11513
+ if (curTx.serializedTransaction) {
11514
+ transactionResult = await handlePreBuiltTransaction(curTx, authProvider, options);
11515
+ }
11516
+ else {
11517
+ transactionResult = await handleSolanaTransaction(curTx, authProvider, options);
11495
11518
  }
11496
11519
  // Sync items after all transactions are confirmed
11497
11520
  // Wait for 1.5 seconds to ensure all transactions are confirmed
11498
11521
  await new Promise(resolve => setTimeout(resolve, 1500));
11499
11522
  await syncItems(many.map(m => m.path), options);
11500
11523
  // TODO: Should we wait here or do the optimistic subscription updates like below?
11501
- return Object.assign(Object.assign({}, documents.map(d => d.document)), { transactionId: lastTxSignature, signedTransaction: signedTransaction });
11524
+ return Object.assign(Object.assign({}, documents.map(d => d.document)), { transactionId: transactionResult.transactionSignature, signedTransaction: transactionResult.signedTransaction });
11502
11525
  }
11503
11526
  else if (setResponse.status === 200) {
11504
11527
  // This means that the document was set successfully.
@@ -11511,7 +11534,7 @@ async function setMany(many, options) {
11511
11534
  else if (setResponse.data &&
11512
11535
  typeof setResponse.data === 'object' &&
11513
11536
  setResponse.data.success === true) {
11514
- const _k = setResponse.data, { success: _success } = _k, rest = __rest(_k, ["success"]);
11537
+ const _k = setResponse.data, { success: _success } = _k, rest = __rest$1(_k, ["success"]);
11515
11538
  return Object.assign(Object.assign(Object.assign({}, documents.map(d => d.document)), rest), { transactionId: null });
11516
11539
  }
11517
11540
  else {
@@ -11747,7 +11770,7 @@ async function getFiles(path, options) {
11747
11770
  try {
11748
11771
  const normalizedPath = path.startsWith("/") ? path.slice(1) : path;
11749
11772
  if (!normalizedPath || normalizedPath.length === 0) {
11750
- return new Error("Invalid path provided.");
11773
+ throw new Error("Invalid path provided.");
11751
11774
  }
11752
11775
  const apiPath = `storage?path=${normalizedPath}`;
11753
11776
  const response = await makeApiRequest('GET', apiPath, null, options === null || options === void 0 ? void 0 : options._overrides);
@@ -12167,6 +12190,12 @@ function roomKeyFromRoutePath(routePath) {
12167
12190
  return null;
12168
12191
  return `${segs[0]}/${segs[1]}`;
12169
12192
  }
12193
+ function replaySubscriptions(connection) {
12194
+ for (const sub of connection.subscriptions.values()) {
12195
+ sub.lastData = undefined;
12196
+ sendSubscribe(connection, sub);
12197
+ }
12198
+ }
12170
12199
  async function getOrCreateConnection(appId, isServer, routePath, authTokenProvider, principalKey) {
12171
12200
  attachBrowserReconnectHooksOnce();
12172
12201
  // A per-room subscription gets its OWN connection routed to the room DO; all
@@ -12192,10 +12221,12 @@ async function getOrCreateConnection(appId, isServer, routePath, authTokenProvid
12192
12221
  pendingRequests: new Map(),
12193
12222
  isConnecting: false,
12194
12223
  isConnected: false,
12224
+ isAuthenticating: false,
12195
12225
  appId,
12196
12226
  key: connKey,
12197
12227
  routePath: roomKey ? routePath : undefined,
12198
12228
  authTokenProvider,
12229
+ pendingAuthToken: null,
12199
12230
  tokenRefreshTimer: null,
12200
12231
  consecutiveAuthFailures: 0,
12201
12232
  };
@@ -12220,16 +12251,17 @@ async function getOrCreateConnection(appId, isServer, routePath, authTokenProvid
12220
12251
  // Per-room connection: carry the room path so the worker routes this WS
12221
12252
  // to the room DO (appId#room#roomId) where the live view fan-out lives.
12222
12253
  if (connection.routePath) {
12223
- wsUrl.searchParams.append('path', connection.routePath);
12254
+ wsUrl.searchParams.append('routePath', connection.routePath);
12224
12255
  }
12225
- // Add auth token if available. A wallet-scoped connection resolves its
12226
- // token from the wallet's own session (self-refreshing); all others use
12227
- // the ambient env/web session.
12256
+ // Resolve auth token if available. A wallet-scoped connection resolves
12257
+ // its token from the wallet's own session (self-refreshing); all others
12258
+ // use the ambient env/web session. The token is sent as the first WS
12259
+ // frame after open, never as a URL query parameter.
12228
12260
  const authToken = connection.authTokenProvider
12229
12261
  ? await connection.authTokenProvider().catch(() => null)
12230
12262
  : await getFreshAuthToken(isServer);
12263
+ connection.pendingAuthToken = authToken || null;
12231
12264
  if (authToken) {
12232
- wsUrl.searchParams.append('authorization', authToken);
12233
12265
  // Successful token acquisition — reset failure counter
12234
12266
  connection.consecutiveAuthFailures = 0;
12235
12267
  }
@@ -12255,6 +12287,7 @@ async function getOrCreateConnection(appId, isServer, routePath, authTokenProvid
12255
12287
  connection.ws = ws;
12256
12288
  // Handle connection open
12257
12289
  ws.addEventListener('open', () => {
12290
+ var _a, _b;
12258
12291
  connection.isConnecting = false;
12259
12292
  connection.isConnected = true;
12260
12293
  // NOTE: Do NOT reset consecutiveAuthFailures here. It is reset when a
@@ -12269,10 +12302,26 @@ async function getOrCreateConnection(appId, isServer, routePath, authTokenProvid
12269
12302
  // urlProvider skips straight to unauthenticated connection.
12270
12303
  // Schedule periodic token freshness checks
12271
12304
  scheduleTokenRefresh(connection, isServer);
12272
- // Re-subscribe to all existing subscriptions after reconnect
12273
- for (const sub of connection.subscriptions.values()) {
12274
- sub.lastData = undefined;
12275
- sendSubscribe(connection, sub);
12305
+ if (connection.pendingAuthToken) {
12306
+ connection.isAuthenticating = true;
12307
+ try {
12308
+ (_a = connection.ws) === null || _a === void 0 ? void 0 : _a.send(JSON.stringify({ type: 'auth', token: connection.pendingAuthToken }));
12309
+ }
12310
+ catch (error) {
12311
+ connection.isAuthenticating = false;
12312
+ connection.isConnected = false;
12313
+ console.error('[WS v2] Error sending auth message:', error);
12314
+ try {
12315
+ (_b = connection.ws) === null || _b === void 0 ? void 0 : _b.close(1008, 'Authentication send failed');
12316
+ }
12317
+ catch (_c) {
12318
+ // Already closed.
12319
+ }
12320
+ }
12321
+ }
12322
+ else {
12323
+ connection.isAuthenticating = false;
12324
+ replaySubscriptions(connection);
12276
12325
  }
12277
12326
  });
12278
12327
  // Handle incoming messages
@@ -12296,6 +12345,7 @@ async function getOrCreateConnection(appId, isServer, routePath, authTokenProvid
12296
12345
  // Handle close
12297
12346
  ws.addEventListener('close', () => {
12298
12347
  connection.isConnected = false;
12348
+ connection.isAuthenticating = false;
12299
12349
  if (connection.tokenRefreshTimer) {
12300
12350
  clearInterval(connection.tokenRefreshTimer);
12301
12351
  connection.tokenRefreshTimer = null;
@@ -12312,6 +12362,11 @@ async function getOrCreateConnection(appId, isServer, routePath, authTokenProvid
12312
12362
  function handleServerMessage(connection, message) {
12313
12363
  var _a, _b;
12314
12364
  switch (message.type) {
12365
+ case 'authenticated': {
12366
+ connection.isAuthenticating = false;
12367
+ replaySubscriptions(connection);
12368
+ break;
12369
+ }
12315
12370
  case 'subscribed': {
12316
12371
  const subscription = connection.subscriptions.get(message.subscriptionId);
12317
12372
  if (subscription) {
@@ -12612,7 +12667,7 @@ async function subscribeV2(path, subscriptionOptions, roomRoutePath) {
12612
12667
  };
12613
12668
  connection.subscriptions.set(subscriptionId, subscription);
12614
12669
  // Send subscribe message if connected
12615
- if (connection.isConnected) {
12670
+ if (connection.isConnected && !connection.isAuthenticating) {
12616
12671
  // Create a promise to wait for subscription confirmation
12617
12672
  const subscriptionPromise = new Promise((resolve, reject) => {
12618
12673
  connection.pendingSubscriptions.set(subscriptionId, { resolve, reject });
@@ -12650,7 +12705,7 @@ async function removeCallbackFromSubscription(connection, subscriptionId, callba
12650
12705
  }
12651
12706
  // No more callbacks, unsubscribe from server
12652
12707
  connection.subscriptions.delete(subscriptionId);
12653
- if (connection.isConnected) {
12708
+ if (connection.isConnected && !connection.isAuthenticating) {
12654
12709
  // Create a promise to wait for unsubscription confirmation
12655
12710
  const unsubscribePromise = new Promise((resolve, reject) => {
12656
12711
  connection.pendingUnsubscriptions.set(subscriptionId, { resolve, reject });
@@ -12826,12 +12881,59 @@ function generateRequestId() {
12826
12881
  */
12827
12882
  function hasActiveConnection() {
12828
12883
  for (const connection of connections.values()) {
12829
- if (connection.ws && connection.isConnected) {
12884
+ if (connection.ws && connection.isConnected && !connection.isAuthenticating) {
12830
12885
  return true;
12831
12886
  }
12832
12887
  }
12833
12888
  return false;
12834
12889
  }
12890
+ async function waitForConnectionAuthenticated(connection) {
12891
+ if (!connection.isAuthenticating || !connection.ws)
12892
+ return;
12893
+ const ws = connection.ws;
12894
+ await new Promise((resolve, reject) => {
12895
+ let timeout;
12896
+ let cleanup = () => { };
12897
+ const onMessage = (event) => {
12898
+ try {
12899
+ const message = JSON.parse(event.data);
12900
+ if ((message === null || message === void 0 ? void 0 : message.type) === 'authenticated') {
12901
+ cleanup();
12902
+ resolve();
12903
+ }
12904
+ }
12905
+ catch (_a) {
12906
+ // Other frames are handled by the main listener.
12907
+ }
12908
+ };
12909
+ const onClose = () => {
12910
+ cleanup();
12911
+ reject(new Error('WebSocket disconnected during authentication'));
12912
+ };
12913
+ const onError = () => {
12914
+ cleanup();
12915
+ reject(new Error('WebSocket authentication failed'));
12916
+ };
12917
+ cleanup = () => {
12918
+ clearTimeout(timeout);
12919
+ ws.removeEventListener('message', onMessage);
12920
+ ws.removeEventListener('close', onClose);
12921
+ ws.removeEventListener('error', onError);
12922
+ };
12923
+ timeout = setTimeout(() => {
12924
+ cleanup();
12925
+ reject(new Error('WebSocket authentication timeout'));
12926
+ }, 10000);
12927
+ if (!connection.isAuthenticating) {
12928
+ cleanup();
12929
+ resolve();
12930
+ return;
12931
+ }
12932
+ ws.addEventListener('message', onMessage);
12933
+ ws.addEventListener('close', onClose);
12934
+ ws.addEventListener('error', onError);
12935
+ });
12936
+ }
12835
12937
  async function sendRequest(msgBuilder) {
12836
12938
  const config = await getConfig();
12837
12939
  const appId = config.appId;
@@ -12857,6 +12959,7 @@ async function sendRequest(msgBuilder) {
12857
12959
  if (!connection.ws || !connection.isConnected) {
12858
12960
  throw new Error('WebSocket connection not available');
12859
12961
  }
12962
+ await waitForConnectionAuthenticated(connection);
12860
12963
  const requestId = generateRequestId();
12861
12964
  const message = msgBuilder(requestId);
12862
12965
  return new Promise((resolve, reject) => {
@@ -12893,7 +12996,7 @@ function wsIntent(appId, roomRoutePath, intent) {
12893
12996
  const roomKey = roomKeyFromRoutePath(roomRoutePath);
12894
12997
  const connKey = roomKey ? `${appId}#room#${roomKey}` : appId;
12895
12998
  const connection = connections.get(connKey);
12896
- if (!connection || !connection.ws || connection.ws.readyState !== WS_READY_STATE_OPEN) {
12999
+ if (!connection || !connection.ws || connection.ws.readyState !== WS_READY_STATE_OPEN || connection.isAuthenticating) {
12897
13000
  return false;
12898
13001
  }
12899
13002
  try {
@@ -12916,7 +13019,7 @@ function wsIntentReliable(appId, roomRoutePath, intent) {
12916
13019
  const roomKey = roomKeyFromRoutePath(roomRoutePath);
12917
13020
  const connKey = roomKey ? `${appId}#room#${roomKey}` : appId;
12918
13021
  const connection = connections.get(connKey);
12919
- if (!connection || !connection.ws || connection.ws.readyState !== WS_READY_STATE_OPEN) {
13022
+ if (!connection || !connection.ws || connection.ws.readyState !== WS_READY_STATE_OPEN || connection.isAuthenticating) {
12920
13023
  return undefined;
12921
13024
  }
12922
13025
  const requestId = generateRequestId();
@@ -13243,6 +13346,7 @@ class RealtimeStore {
13243
13346
  this.idbDirtyKeys = new Set();
13244
13347
  this.closed = false;
13245
13348
  this.authToken = null;
13349
+ this.authenticating = false;
13246
13350
  this.isServer = false;
13247
13351
  this.tokenRefreshTimer = null;
13248
13352
  // -----------------------------------------------------------------------
@@ -13290,7 +13394,7 @@ class RealtimeStore {
13290
13394
  this.initPromise = this.init();
13291
13395
  await this.initPromise;
13292
13396
  }
13293
- if (((_a = this.ws) === null || _a === void 0 ? void 0 : _a.readyState) === WebSocket.OPEN)
13397
+ if (((_a = this.ws) === null || _a === void 0 ? void 0 : _a.readyState) === WebSocket.OPEN && !this.authenticating)
13294
13398
  return;
13295
13399
  if (this.connectPromise)
13296
13400
  return this.connectPromise;
@@ -13305,21 +13409,52 @@ class RealtimeStore {
13305
13409
  }
13306
13410
  const params = new URLSearchParams();
13307
13411
  params.set('apiKey', this.appId);
13308
- if (this.authToken)
13309
- params.set('authorization', this.authToken);
13310
- // Note: token in URL is required until DO server supports subprotocol auth.
13311
- // WSS encrypts the full URL including query params on the wire.
13312
13412
  const url = `${this.wsUrl}?${params.toString()}`;
13313
13413
  const ws = new WebSocket(url);
13314
13414
  this.ws = ws;
13315
- const onOpen = () => {
13415
+ let authTimer = null;
13416
+ const finishConnected = () => {
13417
+ if (authTimer) {
13418
+ clearTimeout(authTimer);
13419
+ authTimer = null;
13420
+ }
13421
+ this.authenticating = false;
13316
13422
  ws.removeEventListener('error', onError);
13317
13423
  this.reconnectDelay = 1000;
13318
13424
  this.connectPromise = null;
13319
13425
  this.resubscribeAll();
13320
13426
  resolve();
13321
13427
  };
13428
+ const onOpen = () => {
13429
+ if (!this.authToken) {
13430
+ finishConnected();
13431
+ return;
13432
+ }
13433
+ this.authenticating = true;
13434
+ authTimer = setTimeout(() => {
13435
+ this.authenticating = false;
13436
+ this.connectPromise = null;
13437
+ try {
13438
+ ws.close(1008, 'Authentication timeout');
13439
+ }
13440
+ catch ( /* ignore */_a) { /* ignore */ }
13441
+ reject(new Error('WebSocket authentication timeout'));
13442
+ }, 10000);
13443
+ try {
13444
+ ws.send(JSON.stringify({ type: 'auth', token: this.authToken }));
13445
+ }
13446
+ catch (e) {
13447
+ if (authTimer)
13448
+ clearTimeout(authTimer);
13449
+ this.authenticating = false;
13450
+ this.connectPromise = null;
13451
+ reject(e);
13452
+ }
13453
+ };
13322
13454
  const onError = (e) => {
13455
+ if (authTimer)
13456
+ clearTimeout(authTimer);
13457
+ this.authenticating = false;
13323
13458
  ws.removeEventListener('open', onOpen);
13324
13459
  this.connectPromise = null;
13325
13460
  reject(new Error('WebSocket connection failed'));
@@ -13327,9 +13462,22 @@ class RealtimeStore {
13327
13462
  ws.addEventListener('open', onOpen, { once: true });
13328
13463
  ws.addEventListener('error', onError, { once: true });
13329
13464
  ws.addEventListener('message', (event) => {
13465
+ if (this.authenticating) {
13466
+ try {
13467
+ const msg = JSON.parse(typeof event.data === 'string' ? event.data : new TextDecoder().decode(event.data));
13468
+ if ((msg === null || msg === void 0 ? void 0 : msg.type) === 'authenticated') {
13469
+ finishConnected();
13470
+ return;
13471
+ }
13472
+ }
13473
+ catch ( /* fall through to normal handling */_a) { /* fall through to normal handling */ }
13474
+ }
13330
13475
  this.handleMessage(event.data);
13331
13476
  });
13332
13477
  ws.addEventListener('close', () => {
13478
+ if (authTimer)
13479
+ clearTimeout(authTimer);
13480
+ this.authenticating = false;
13333
13481
  this.ws = null;
13334
13482
  this.connectPromise = null;
13335
13483
  this.rejectAllPending('WebSocket closed');
@@ -13382,6 +13530,8 @@ class RealtimeStore {
13382
13530
  break;
13383
13531
  case 'pong':
13384
13532
  break;
13533
+ case 'authenticated':
13534
+ break;
13385
13535
  // v1 compat: handle legacy message types during transition
13386
13536
  case 'subscribed':
13387
13537
  this.handleSnapshot(Object.assign(Object.assign({}, msg), { type: 'snapshot', docs: msg.data }));
@@ -13983,6 +14133,17 @@ function resetRealtimeStore() {
13983
14133
  // rule, and dispatches to the function — returning its JSON, or throwing on
13984
14134
  // 403 / error.
13985
14135
  // ---------------------------------------------------------------------------
14136
+ var __rest = function (s, e) {
14137
+ var t = {};
14138
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
14139
+ t[p] = s[p];
14140
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
14141
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
14142
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
14143
+ t[p[i]] = s[p[i]];
14144
+ }
14145
+ return t;
14146
+ };
13986
14147
  /** Prod functions dispatcher; overridable via init({ functionsUrl }) or network preset. */
13987
14148
  const DEFAULT_FUNCTIONS_URL = 'https://functions.bounded.sh';
13988
14149
  class FunctionInvokeError extends Error {
@@ -13993,6 +14154,12 @@ class FunctionInvokeError extends Error {
13993
14154
  this.name = 'FunctionInvokeError';
13994
14155
  }
13995
14156
  }
14157
+ function stripAuthHeaders(headers) {
14158
+ if (!headers)
14159
+ return undefined;
14160
+ const { Authorization, authorization } = headers, rest = __rest(headers, ["Authorization", "authorization"]);
14161
+ return Object.keys(rest).length > 0 ? rest : undefined;
14162
+ }
13996
14163
  /**
13997
14164
  * Invoke a deployed Bounded Function by name. Returns the function's JSON.
13998
14165
  *
@@ -14015,7 +14182,7 @@ async function invoke(name, args = {}, opts = {}) {
14015
14182
  const authHeader = ((_a = opts._overrides) === null || _a === void 0 ? void 0 : _a._getAuthHeaders)
14016
14183
  ? await opts._overrides._getAuthHeaders()
14017
14184
  : await createAuthHeader(config.isServer);
14018
- const headers = Object.assign(Object.assign({ 'Content-Type': 'application/json', 'X-App-Id': config.appId, 'X-Public-App-Id': config.appId }, (authHeader !== null && authHeader !== void 0 ? authHeader : {})), ((_b = opts.headers) !== null && _b !== void 0 ? _b : {}));
14185
+ const headers = Object.assign(Object.assign({ 'Content-Type': 'application/json', 'X-App-Id': config.appId, 'X-Public-App-Id': config.appId }, ((_b = stripAuthHeaders(opts.headers)) !== null && _b !== void 0 ? _b : {})), (authHeader !== null && authHeader !== void 0 ? authHeader : {}));
14019
14186
  const controller = new AbortController();
14020
14187
  const timeoutMs = (_c = opts.timeoutMs) !== null && _c !== void 0 ? _c : 60000;
14021
14188
  const timer = setTimeout(() => controller.abort(), timeoutMs);
@@ -14566,14 +14733,22 @@ class MockAuthProvider {
14566
14733
  }
14567
14734
  async signMessageMock(message) {
14568
14735
  const address = getPlatform().sessionStorage.getItem('test-user-address') || DEFAULT_TEST_ADDRESS;
14569
- // Generate a fake signature
14570
- const fakeSignature = `mock_sig_${Math.random().toString(36).substring(2, 15)}`;
14736
+ // Offchain transactions are verified by hashing the exact message. Keep the
14737
+ // mock provider deterministic so mockAuth exercises the same integrity check.
14738
+ const signature = await this.generateOffchainSignature(message);
14571
14739
  console.log('[MockAuth] Mock message signed:', {
14572
14740
  address,
14573
14741
  message,
14574
- signature: fakeSignature
14742
+ signature
14575
14743
  });
14576
- return fakeSignature;
14744
+ return signature;
14745
+ }
14746
+ async generateOffchainSignature(message) {
14747
+ const platform = getPlatform();
14748
+ const data = platform.textEncode(message);
14749
+ const hashBuffer = await platform.sha256(data);
14750
+ const hashArray = Array.from(new Uint8Array(hashBuffer));
14751
+ return platform.btoa(String.fromCharCode(...hashArray));
14577
14752
  }
14578
14753
  /**
14579
14754
  * Mock transaction signing - returns the transaction as-is (no actual signing)
@@ -17944,2549 +18119,144 @@ var index = /*#__PURE__*/Object.freeze({
17944
18119
  default: bs58
17945
18120
  });
17946
18121
 
17947
- var naclFast = {exports: {}};
17948
-
17949
- var hasRequiredNaclFast;
17950
-
17951
- function requireNaclFast () {
17952
- if (hasRequiredNaclFast) return naclFast.exports;
17953
- hasRequiredNaclFast = 1;
17954
- (function (module) {
17955
- (function(nacl) {
17956
-
17957
- // Ported in 2014 by Dmitry Chestnykh and Devi Mandiri.
17958
- // Public domain.
17959
- //
17960
- // Implementation derived from TweetNaCl version 20140427.
17961
- // See for details: http://tweetnacl.cr.yp.to/
17962
-
17963
- var gf = function(init) {
17964
- var i, r = new Float64Array(16);
17965
- if (init) for (i = 0; i < init.length; i++) r[i] = init[i];
17966
- return r;
17967
- };
17968
-
17969
- // Pluggable, initialized in high-level API below.
17970
- var randombytes = function(/* x, n */) { throw new Error('no PRNG'); };
17971
-
17972
- var _0 = new Uint8Array(16);
17973
- var _9 = new Uint8Array(32); _9[0] = 9;
17974
-
17975
- var gf0 = gf(),
17976
- gf1 = gf([1]),
17977
- _121665 = gf([0xdb41, 1]),
17978
- D = gf([0x78a3, 0x1359, 0x4dca, 0x75eb, 0xd8ab, 0x4141, 0x0a4d, 0x0070, 0xe898, 0x7779, 0x4079, 0x8cc7, 0xfe73, 0x2b6f, 0x6cee, 0x5203]),
17979
- D2 = gf([0xf159, 0x26b2, 0x9b94, 0xebd6, 0xb156, 0x8283, 0x149a, 0x00e0, 0xd130, 0xeef3, 0x80f2, 0x198e, 0xfce7, 0x56df, 0xd9dc, 0x2406]),
17980
- X = gf([0xd51a, 0x8f25, 0x2d60, 0xc956, 0xa7b2, 0x9525, 0xc760, 0x692c, 0xdc5c, 0xfdd6, 0xe231, 0xc0a4, 0x53fe, 0xcd6e, 0x36d3, 0x2169]),
17981
- Y = gf([0x6658, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666]),
17982
- I = gf([0xa0b0, 0x4a0e, 0x1b27, 0xc4ee, 0xe478, 0xad2f, 0x1806, 0x2f43, 0xd7a7, 0x3dfb, 0x0099, 0x2b4d, 0xdf0b, 0x4fc1, 0x2480, 0x2b83]);
17983
-
17984
- function ts64(x, i, h, l) {
17985
- x[i] = (h >> 24) & 0xff;
17986
- x[i+1] = (h >> 16) & 0xff;
17987
- x[i+2] = (h >> 8) & 0xff;
17988
- x[i+3] = h & 0xff;
17989
- x[i+4] = (l >> 24) & 0xff;
17990
- x[i+5] = (l >> 16) & 0xff;
17991
- x[i+6] = (l >> 8) & 0xff;
17992
- x[i+7] = l & 0xff;
17993
- }
17994
-
17995
- function vn(x, xi, y, yi, n) {
17996
- var i,d = 0;
17997
- for (i = 0; i < n; i++) d |= x[xi+i]^y[yi+i];
17998
- return (1 & ((d - 1) >>> 8)) - 1;
17999
- }
18000
-
18001
- function crypto_verify_16(x, xi, y, yi) {
18002
- return vn(x,xi,y,yi,16);
18003
- }
18004
-
18005
- function crypto_verify_32(x, xi, y, yi) {
18006
- return vn(x,xi,y,yi,32);
18007
- }
18008
-
18009
- function core_salsa20(o, p, k, c) {
18010
- var j0 = c[ 0] & 0xff | (c[ 1] & 0xff)<<8 | (c[ 2] & 0xff)<<16 | (c[ 3] & 0xff)<<24,
18011
- j1 = k[ 0] & 0xff | (k[ 1] & 0xff)<<8 | (k[ 2] & 0xff)<<16 | (k[ 3] & 0xff)<<24,
18012
- j2 = k[ 4] & 0xff | (k[ 5] & 0xff)<<8 | (k[ 6] & 0xff)<<16 | (k[ 7] & 0xff)<<24,
18013
- j3 = k[ 8] & 0xff | (k[ 9] & 0xff)<<8 | (k[10] & 0xff)<<16 | (k[11] & 0xff)<<24,
18014
- j4 = k[12] & 0xff | (k[13] & 0xff)<<8 | (k[14] & 0xff)<<16 | (k[15] & 0xff)<<24,
18015
- j5 = c[ 4] & 0xff | (c[ 5] & 0xff)<<8 | (c[ 6] & 0xff)<<16 | (c[ 7] & 0xff)<<24,
18016
- j6 = p[ 0] & 0xff | (p[ 1] & 0xff)<<8 | (p[ 2] & 0xff)<<16 | (p[ 3] & 0xff)<<24,
18017
- j7 = p[ 4] & 0xff | (p[ 5] & 0xff)<<8 | (p[ 6] & 0xff)<<16 | (p[ 7] & 0xff)<<24,
18018
- j8 = p[ 8] & 0xff | (p[ 9] & 0xff)<<8 | (p[10] & 0xff)<<16 | (p[11] & 0xff)<<24,
18019
- j9 = p[12] & 0xff | (p[13] & 0xff)<<8 | (p[14] & 0xff)<<16 | (p[15] & 0xff)<<24,
18020
- j10 = c[ 8] & 0xff | (c[ 9] & 0xff)<<8 | (c[10] & 0xff)<<16 | (c[11] & 0xff)<<24,
18021
- j11 = k[16] & 0xff | (k[17] & 0xff)<<8 | (k[18] & 0xff)<<16 | (k[19] & 0xff)<<24,
18022
- j12 = k[20] & 0xff | (k[21] & 0xff)<<8 | (k[22] & 0xff)<<16 | (k[23] & 0xff)<<24,
18023
- j13 = k[24] & 0xff | (k[25] & 0xff)<<8 | (k[26] & 0xff)<<16 | (k[27] & 0xff)<<24,
18024
- j14 = k[28] & 0xff | (k[29] & 0xff)<<8 | (k[30] & 0xff)<<16 | (k[31] & 0xff)<<24,
18025
- j15 = c[12] & 0xff | (c[13] & 0xff)<<8 | (c[14] & 0xff)<<16 | (c[15] & 0xff)<<24;
18026
-
18027
- var x0 = j0, x1 = j1, x2 = j2, x3 = j3, x4 = j4, x5 = j5, x6 = j6, x7 = j7,
18028
- x8 = j8, x9 = j9, x10 = j10, x11 = j11, x12 = j12, x13 = j13, x14 = j14,
18029
- x15 = j15, u;
18030
-
18031
- for (var i = 0; i < 20; i += 2) {
18032
- u = x0 + x12 | 0;
18033
- x4 ^= u<<7 | u>>>(32-7);
18034
- u = x4 + x0 | 0;
18035
- x8 ^= u<<9 | u>>>(32-9);
18036
- u = x8 + x4 | 0;
18037
- x12 ^= u<<13 | u>>>(32-13);
18038
- u = x12 + x8 | 0;
18039
- x0 ^= u<<18 | u>>>(32-18);
18040
-
18041
- u = x5 + x1 | 0;
18042
- x9 ^= u<<7 | u>>>(32-7);
18043
- u = x9 + x5 | 0;
18044
- x13 ^= u<<9 | u>>>(32-9);
18045
- u = x13 + x9 | 0;
18046
- x1 ^= u<<13 | u>>>(32-13);
18047
- u = x1 + x13 | 0;
18048
- x5 ^= u<<18 | u>>>(32-18);
18049
-
18050
- u = x10 + x6 | 0;
18051
- x14 ^= u<<7 | u>>>(32-7);
18052
- u = x14 + x10 | 0;
18053
- x2 ^= u<<9 | u>>>(32-9);
18054
- u = x2 + x14 | 0;
18055
- x6 ^= u<<13 | u>>>(32-13);
18056
- u = x6 + x2 | 0;
18057
- x10 ^= u<<18 | u>>>(32-18);
18058
-
18059
- u = x15 + x11 | 0;
18060
- x3 ^= u<<7 | u>>>(32-7);
18061
- u = x3 + x15 | 0;
18062
- x7 ^= u<<9 | u>>>(32-9);
18063
- u = x7 + x3 | 0;
18064
- x11 ^= u<<13 | u>>>(32-13);
18065
- u = x11 + x7 | 0;
18066
- x15 ^= u<<18 | u>>>(32-18);
18067
-
18068
- u = x0 + x3 | 0;
18069
- x1 ^= u<<7 | u>>>(32-7);
18070
- u = x1 + x0 | 0;
18071
- x2 ^= u<<9 | u>>>(32-9);
18072
- u = x2 + x1 | 0;
18073
- x3 ^= u<<13 | u>>>(32-13);
18074
- u = x3 + x2 | 0;
18075
- x0 ^= u<<18 | u>>>(32-18);
18076
-
18077
- u = x5 + x4 | 0;
18078
- x6 ^= u<<7 | u>>>(32-7);
18079
- u = x6 + x5 | 0;
18080
- x7 ^= u<<9 | u>>>(32-9);
18081
- u = x7 + x6 | 0;
18082
- x4 ^= u<<13 | u>>>(32-13);
18083
- u = x4 + x7 | 0;
18084
- x5 ^= u<<18 | u>>>(32-18);
18085
-
18086
- u = x10 + x9 | 0;
18087
- x11 ^= u<<7 | u>>>(32-7);
18088
- u = x11 + x10 | 0;
18089
- x8 ^= u<<9 | u>>>(32-9);
18090
- u = x8 + x11 | 0;
18091
- x9 ^= u<<13 | u>>>(32-13);
18092
- u = x9 + x8 | 0;
18093
- x10 ^= u<<18 | u>>>(32-18);
18094
-
18095
- u = x15 + x14 | 0;
18096
- x12 ^= u<<7 | u>>>(32-7);
18097
- u = x12 + x15 | 0;
18098
- x13 ^= u<<9 | u>>>(32-9);
18099
- u = x13 + x12 | 0;
18100
- x14 ^= u<<13 | u>>>(32-13);
18101
- u = x14 + x13 | 0;
18102
- x15 ^= u<<18 | u>>>(32-18);
18103
- }
18104
- x0 = x0 + j0 | 0;
18105
- x1 = x1 + j1 | 0;
18106
- x2 = x2 + j2 | 0;
18107
- x3 = x3 + j3 | 0;
18108
- x4 = x4 + j4 | 0;
18109
- x5 = x5 + j5 | 0;
18110
- x6 = x6 + j6 | 0;
18111
- x7 = x7 + j7 | 0;
18112
- x8 = x8 + j8 | 0;
18113
- x9 = x9 + j9 | 0;
18114
- x10 = x10 + j10 | 0;
18115
- x11 = x11 + j11 | 0;
18116
- x12 = x12 + j12 | 0;
18117
- x13 = x13 + j13 | 0;
18118
- x14 = x14 + j14 | 0;
18119
- x15 = x15 + j15 | 0;
18120
-
18121
- o[ 0] = x0 >>> 0 & 0xff;
18122
- o[ 1] = x0 >>> 8 & 0xff;
18123
- o[ 2] = x0 >>> 16 & 0xff;
18124
- o[ 3] = x0 >>> 24 & 0xff;
18125
-
18126
- o[ 4] = x1 >>> 0 & 0xff;
18127
- o[ 5] = x1 >>> 8 & 0xff;
18128
- o[ 6] = x1 >>> 16 & 0xff;
18129
- o[ 7] = x1 >>> 24 & 0xff;
18130
-
18131
- o[ 8] = x2 >>> 0 & 0xff;
18132
- o[ 9] = x2 >>> 8 & 0xff;
18133
- o[10] = x2 >>> 16 & 0xff;
18134
- o[11] = x2 >>> 24 & 0xff;
18135
-
18136
- o[12] = x3 >>> 0 & 0xff;
18137
- o[13] = x3 >>> 8 & 0xff;
18138
- o[14] = x3 >>> 16 & 0xff;
18139
- o[15] = x3 >>> 24 & 0xff;
18140
-
18141
- o[16] = x4 >>> 0 & 0xff;
18142
- o[17] = x4 >>> 8 & 0xff;
18143
- o[18] = x4 >>> 16 & 0xff;
18144
- o[19] = x4 >>> 24 & 0xff;
18145
-
18146
- o[20] = x5 >>> 0 & 0xff;
18147
- o[21] = x5 >>> 8 & 0xff;
18148
- o[22] = x5 >>> 16 & 0xff;
18149
- o[23] = x5 >>> 24 & 0xff;
18150
-
18151
- o[24] = x6 >>> 0 & 0xff;
18152
- o[25] = x6 >>> 8 & 0xff;
18153
- o[26] = x6 >>> 16 & 0xff;
18154
- o[27] = x6 >>> 24 & 0xff;
18155
-
18156
- o[28] = x7 >>> 0 & 0xff;
18157
- o[29] = x7 >>> 8 & 0xff;
18158
- o[30] = x7 >>> 16 & 0xff;
18159
- o[31] = x7 >>> 24 & 0xff;
18160
-
18161
- o[32] = x8 >>> 0 & 0xff;
18162
- o[33] = x8 >>> 8 & 0xff;
18163
- o[34] = x8 >>> 16 & 0xff;
18164
- o[35] = x8 >>> 24 & 0xff;
18165
-
18166
- o[36] = x9 >>> 0 & 0xff;
18167
- o[37] = x9 >>> 8 & 0xff;
18168
- o[38] = x9 >>> 16 & 0xff;
18169
- o[39] = x9 >>> 24 & 0xff;
18170
-
18171
- o[40] = x10 >>> 0 & 0xff;
18172
- o[41] = x10 >>> 8 & 0xff;
18173
- o[42] = x10 >>> 16 & 0xff;
18174
- o[43] = x10 >>> 24 & 0xff;
18175
-
18176
- o[44] = x11 >>> 0 & 0xff;
18177
- o[45] = x11 >>> 8 & 0xff;
18178
- o[46] = x11 >>> 16 & 0xff;
18179
- o[47] = x11 >>> 24 & 0xff;
18180
-
18181
- o[48] = x12 >>> 0 & 0xff;
18182
- o[49] = x12 >>> 8 & 0xff;
18183
- o[50] = x12 >>> 16 & 0xff;
18184
- o[51] = x12 >>> 24 & 0xff;
18185
-
18186
- o[52] = x13 >>> 0 & 0xff;
18187
- o[53] = x13 >>> 8 & 0xff;
18188
- o[54] = x13 >>> 16 & 0xff;
18189
- o[55] = x13 >>> 24 & 0xff;
18190
-
18191
- o[56] = x14 >>> 0 & 0xff;
18192
- o[57] = x14 >>> 8 & 0xff;
18193
- o[58] = x14 >>> 16 & 0xff;
18194
- o[59] = x14 >>> 24 & 0xff;
18195
-
18196
- o[60] = x15 >>> 0 & 0xff;
18197
- o[61] = x15 >>> 8 & 0xff;
18198
- o[62] = x15 >>> 16 & 0xff;
18199
- o[63] = x15 >>> 24 & 0xff;
18200
- }
18201
-
18202
- function core_hsalsa20(o,p,k,c) {
18203
- var j0 = c[ 0] & 0xff | (c[ 1] & 0xff)<<8 | (c[ 2] & 0xff)<<16 | (c[ 3] & 0xff)<<24,
18204
- j1 = k[ 0] & 0xff | (k[ 1] & 0xff)<<8 | (k[ 2] & 0xff)<<16 | (k[ 3] & 0xff)<<24,
18205
- j2 = k[ 4] & 0xff | (k[ 5] & 0xff)<<8 | (k[ 6] & 0xff)<<16 | (k[ 7] & 0xff)<<24,
18206
- j3 = k[ 8] & 0xff | (k[ 9] & 0xff)<<8 | (k[10] & 0xff)<<16 | (k[11] & 0xff)<<24,
18207
- j4 = k[12] & 0xff | (k[13] & 0xff)<<8 | (k[14] & 0xff)<<16 | (k[15] & 0xff)<<24,
18208
- j5 = c[ 4] & 0xff | (c[ 5] & 0xff)<<8 | (c[ 6] & 0xff)<<16 | (c[ 7] & 0xff)<<24,
18209
- j6 = p[ 0] & 0xff | (p[ 1] & 0xff)<<8 | (p[ 2] & 0xff)<<16 | (p[ 3] & 0xff)<<24,
18210
- j7 = p[ 4] & 0xff | (p[ 5] & 0xff)<<8 | (p[ 6] & 0xff)<<16 | (p[ 7] & 0xff)<<24,
18211
- j8 = p[ 8] & 0xff | (p[ 9] & 0xff)<<8 | (p[10] & 0xff)<<16 | (p[11] & 0xff)<<24,
18212
- j9 = p[12] & 0xff | (p[13] & 0xff)<<8 | (p[14] & 0xff)<<16 | (p[15] & 0xff)<<24,
18213
- j10 = c[ 8] & 0xff | (c[ 9] & 0xff)<<8 | (c[10] & 0xff)<<16 | (c[11] & 0xff)<<24,
18214
- j11 = k[16] & 0xff | (k[17] & 0xff)<<8 | (k[18] & 0xff)<<16 | (k[19] & 0xff)<<24,
18215
- j12 = k[20] & 0xff | (k[21] & 0xff)<<8 | (k[22] & 0xff)<<16 | (k[23] & 0xff)<<24,
18216
- j13 = k[24] & 0xff | (k[25] & 0xff)<<8 | (k[26] & 0xff)<<16 | (k[27] & 0xff)<<24,
18217
- j14 = k[28] & 0xff | (k[29] & 0xff)<<8 | (k[30] & 0xff)<<16 | (k[31] & 0xff)<<24,
18218
- j15 = c[12] & 0xff | (c[13] & 0xff)<<8 | (c[14] & 0xff)<<16 | (c[15] & 0xff)<<24;
18219
-
18220
- var x0 = j0, x1 = j1, x2 = j2, x3 = j3, x4 = j4, x5 = j5, x6 = j6, x7 = j7,
18221
- x8 = j8, x9 = j9, x10 = j10, x11 = j11, x12 = j12, x13 = j13, x14 = j14,
18222
- x15 = j15, u;
18223
-
18224
- for (var i = 0; i < 20; i += 2) {
18225
- u = x0 + x12 | 0;
18226
- x4 ^= u<<7 | u>>>(32-7);
18227
- u = x4 + x0 | 0;
18228
- x8 ^= u<<9 | u>>>(32-9);
18229
- u = x8 + x4 | 0;
18230
- x12 ^= u<<13 | u>>>(32-13);
18231
- u = x12 + x8 | 0;
18232
- x0 ^= u<<18 | u>>>(32-18);
18233
-
18234
- u = x5 + x1 | 0;
18235
- x9 ^= u<<7 | u>>>(32-7);
18236
- u = x9 + x5 | 0;
18237
- x13 ^= u<<9 | u>>>(32-9);
18238
- u = x13 + x9 | 0;
18239
- x1 ^= u<<13 | u>>>(32-13);
18240
- u = x1 + x13 | 0;
18241
- x5 ^= u<<18 | u>>>(32-18);
18242
-
18243
- u = x10 + x6 | 0;
18244
- x14 ^= u<<7 | u>>>(32-7);
18245
- u = x14 + x10 | 0;
18246
- x2 ^= u<<9 | u>>>(32-9);
18247
- u = x2 + x14 | 0;
18248
- x6 ^= u<<13 | u>>>(32-13);
18249
- u = x6 + x2 | 0;
18250
- x10 ^= u<<18 | u>>>(32-18);
18251
-
18252
- u = x15 + x11 | 0;
18253
- x3 ^= u<<7 | u>>>(32-7);
18254
- u = x3 + x15 | 0;
18255
- x7 ^= u<<9 | u>>>(32-9);
18256
- u = x7 + x3 | 0;
18257
- x11 ^= u<<13 | u>>>(32-13);
18258
- u = x11 + x7 | 0;
18259
- x15 ^= u<<18 | u>>>(32-18);
18260
-
18261
- u = x0 + x3 | 0;
18262
- x1 ^= u<<7 | u>>>(32-7);
18263
- u = x1 + x0 | 0;
18264
- x2 ^= u<<9 | u>>>(32-9);
18265
- u = x2 + x1 | 0;
18266
- x3 ^= u<<13 | u>>>(32-13);
18267
- u = x3 + x2 | 0;
18268
- x0 ^= u<<18 | u>>>(32-18);
18269
-
18270
- u = x5 + x4 | 0;
18271
- x6 ^= u<<7 | u>>>(32-7);
18272
- u = x6 + x5 | 0;
18273
- x7 ^= u<<9 | u>>>(32-9);
18274
- u = x7 + x6 | 0;
18275
- x4 ^= u<<13 | u>>>(32-13);
18276
- u = x4 + x7 | 0;
18277
- x5 ^= u<<18 | u>>>(32-18);
18278
-
18279
- u = x10 + x9 | 0;
18280
- x11 ^= u<<7 | u>>>(32-7);
18281
- u = x11 + x10 | 0;
18282
- x8 ^= u<<9 | u>>>(32-9);
18283
- u = x8 + x11 | 0;
18284
- x9 ^= u<<13 | u>>>(32-13);
18285
- u = x9 + x8 | 0;
18286
- x10 ^= u<<18 | u>>>(32-18);
18287
-
18288
- u = x15 + x14 | 0;
18289
- x12 ^= u<<7 | u>>>(32-7);
18290
- u = x12 + x15 | 0;
18291
- x13 ^= u<<9 | u>>>(32-9);
18292
- u = x13 + x12 | 0;
18293
- x14 ^= u<<13 | u>>>(32-13);
18294
- u = x14 + x13 | 0;
18295
- x15 ^= u<<18 | u>>>(32-18);
18296
- }
18297
-
18298
- o[ 0] = x0 >>> 0 & 0xff;
18299
- o[ 1] = x0 >>> 8 & 0xff;
18300
- o[ 2] = x0 >>> 16 & 0xff;
18301
- o[ 3] = x0 >>> 24 & 0xff;
18302
-
18303
- o[ 4] = x5 >>> 0 & 0xff;
18304
- o[ 5] = x5 >>> 8 & 0xff;
18305
- o[ 6] = x5 >>> 16 & 0xff;
18306
- o[ 7] = x5 >>> 24 & 0xff;
18307
-
18308
- o[ 8] = x10 >>> 0 & 0xff;
18309
- o[ 9] = x10 >>> 8 & 0xff;
18310
- o[10] = x10 >>> 16 & 0xff;
18311
- o[11] = x10 >>> 24 & 0xff;
18312
-
18313
- o[12] = x15 >>> 0 & 0xff;
18314
- o[13] = x15 >>> 8 & 0xff;
18315
- o[14] = x15 >>> 16 & 0xff;
18316
- o[15] = x15 >>> 24 & 0xff;
18317
-
18318
- o[16] = x6 >>> 0 & 0xff;
18319
- o[17] = x6 >>> 8 & 0xff;
18320
- o[18] = x6 >>> 16 & 0xff;
18321
- o[19] = x6 >>> 24 & 0xff;
18322
-
18323
- o[20] = x7 >>> 0 & 0xff;
18324
- o[21] = x7 >>> 8 & 0xff;
18325
- o[22] = x7 >>> 16 & 0xff;
18326
- o[23] = x7 >>> 24 & 0xff;
18327
-
18328
- o[24] = x8 >>> 0 & 0xff;
18329
- o[25] = x8 >>> 8 & 0xff;
18330
- o[26] = x8 >>> 16 & 0xff;
18331
- o[27] = x8 >>> 24 & 0xff;
18332
-
18333
- o[28] = x9 >>> 0 & 0xff;
18334
- o[29] = x9 >>> 8 & 0xff;
18335
- o[30] = x9 >>> 16 & 0xff;
18336
- o[31] = x9 >>> 24 & 0xff;
18337
- }
18338
-
18339
- function crypto_core_salsa20(out,inp,k,c) {
18340
- core_salsa20(out,inp,k,c);
18341
- }
18342
-
18343
- function crypto_core_hsalsa20(out,inp,k,c) {
18344
- core_hsalsa20(out,inp,k,c);
18345
- }
18346
-
18347
- var sigma = new Uint8Array([101, 120, 112, 97, 110, 100, 32, 51, 50, 45, 98, 121, 116, 101, 32, 107]);
18348
- // "expand 32-byte k"
18349
-
18350
- function crypto_stream_salsa20_xor(c,cpos,m,mpos,b,n,k) {
18351
- var z = new Uint8Array(16), x = new Uint8Array(64);
18352
- var u, i;
18353
- for (i = 0; i < 16; i++) z[i] = 0;
18354
- for (i = 0; i < 8; i++) z[i] = n[i];
18355
- while (b >= 64) {
18356
- crypto_core_salsa20(x,z,k,sigma);
18357
- for (i = 0; i < 64; i++) c[cpos+i] = m[mpos+i] ^ x[i];
18358
- u = 1;
18359
- for (i = 8; i < 16; i++) {
18360
- u = u + (z[i] & 0xff) | 0;
18361
- z[i] = u & 0xff;
18362
- u >>>= 8;
18363
- }
18364
- b -= 64;
18365
- cpos += 64;
18366
- mpos += 64;
18367
- }
18368
- if (b > 0) {
18369
- crypto_core_salsa20(x,z,k,sigma);
18370
- for (i = 0; i < b; i++) c[cpos+i] = m[mpos+i] ^ x[i];
18371
- }
18372
- return 0;
18373
- }
18374
-
18375
- function crypto_stream_salsa20(c,cpos,b,n,k) {
18376
- var z = new Uint8Array(16), x = new Uint8Array(64);
18377
- var u, i;
18378
- for (i = 0; i < 16; i++) z[i] = 0;
18379
- for (i = 0; i < 8; i++) z[i] = n[i];
18380
- while (b >= 64) {
18381
- crypto_core_salsa20(x,z,k,sigma);
18382
- for (i = 0; i < 64; i++) c[cpos+i] = x[i];
18383
- u = 1;
18384
- for (i = 8; i < 16; i++) {
18385
- u = u + (z[i] & 0xff) | 0;
18386
- z[i] = u & 0xff;
18387
- u >>>= 8;
18388
- }
18389
- b -= 64;
18390
- cpos += 64;
18391
- }
18392
- if (b > 0) {
18393
- crypto_core_salsa20(x,z,k,sigma);
18394
- for (i = 0; i < b; i++) c[cpos+i] = x[i];
18395
- }
18396
- return 0;
18397
- }
18398
-
18399
- function crypto_stream(c,cpos,d,n,k) {
18400
- var s = new Uint8Array(32);
18401
- crypto_core_hsalsa20(s,n,k,sigma);
18402
- var sn = new Uint8Array(8);
18403
- for (var i = 0; i < 8; i++) sn[i] = n[i+16];
18404
- return crypto_stream_salsa20(c,cpos,d,sn,s);
18405
- }
18406
-
18407
- function crypto_stream_xor(c,cpos,m,mpos,d,n,k) {
18408
- var s = new Uint8Array(32);
18409
- crypto_core_hsalsa20(s,n,k,sigma);
18410
- var sn = new Uint8Array(8);
18411
- for (var i = 0; i < 8; i++) sn[i] = n[i+16];
18412
- return crypto_stream_salsa20_xor(c,cpos,m,mpos,d,sn,s);
18413
- }
18414
-
18415
- /*
18416
- * Port of Andrew Moon's Poly1305-donna-16. Public domain.
18417
- * https://github.com/floodyberry/poly1305-donna
18418
- */
18419
-
18420
- var poly1305 = function(key) {
18421
- this.buffer = new Uint8Array(16);
18422
- this.r = new Uint16Array(10);
18423
- this.h = new Uint16Array(10);
18424
- this.pad = new Uint16Array(8);
18425
- this.leftover = 0;
18426
- this.fin = 0;
18427
-
18428
- var t0, t1, t2, t3, t4, t5, t6, t7;
18429
-
18430
- t0 = key[ 0] & 0xff | (key[ 1] & 0xff) << 8; this.r[0] = ( t0 ) & 0x1fff;
18431
- t1 = key[ 2] & 0xff | (key[ 3] & 0xff) << 8; this.r[1] = ((t0 >>> 13) | (t1 << 3)) & 0x1fff;
18432
- t2 = key[ 4] & 0xff | (key[ 5] & 0xff) << 8; this.r[2] = ((t1 >>> 10) | (t2 << 6)) & 0x1f03;
18433
- t3 = key[ 6] & 0xff | (key[ 7] & 0xff) << 8; this.r[3] = ((t2 >>> 7) | (t3 << 9)) & 0x1fff;
18434
- t4 = key[ 8] & 0xff | (key[ 9] & 0xff) << 8; this.r[4] = ((t3 >>> 4) | (t4 << 12)) & 0x00ff;
18435
- this.r[5] = ((t4 >>> 1)) & 0x1ffe;
18436
- t5 = key[10] & 0xff | (key[11] & 0xff) << 8; this.r[6] = ((t4 >>> 14) | (t5 << 2)) & 0x1fff;
18437
- t6 = key[12] & 0xff | (key[13] & 0xff) << 8; this.r[7] = ((t5 >>> 11) | (t6 << 5)) & 0x1f81;
18438
- t7 = key[14] & 0xff | (key[15] & 0xff) << 8; this.r[8] = ((t6 >>> 8) | (t7 << 8)) & 0x1fff;
18439
- this.r[9] = ((t7 >>> 5)) & 0x007f;
18440
-
18441
- this.pad[0] = key[16] & 0xff | (key[17] & 0xff) << 8;
18442
- this.pad[1] = key[18] & 0xff | (key[19] & 0xff) << 8;
18443
- this.pad[2] = key[20] & 0xff | (key[21] & 0xff) << 8;
18444
- this.pad[3] = key[22] & 0xff | (key[23] & 0xff) << 8;
18445
- this.pad[4] = key[24] & 0xff | (key[25] & 0xff) << 8;
18446
- this.pad[5] = key[26] & 0xff | (key[27] & 0xff) << 8;
18447
- this.pad[6] = key[28] & 0xff | (key[29] & 0xff) << 8;
18448
- this.pad[7] = key[30] & 0xff | (key[31] & 0xff) << 8;
18449
- };
18450
-
18451
- poly1305.prototype.blocks = function(m, mpos, bytes) {
18452
- var hibit = this.fin ? 0 : (1 << 11);
18453
- var t0, t1, t2, t3, t4, t5, t6, t7, c;
18454
- var d0, d1, d2, d3, d4, d5, d6, d7, d8, d9;
18455
-
18456
- var h0 = this.h[0],
18457
- h1 = this.h[1],
18458
- h2 = this.h[2],
18459
- h3 = this.h[3],
18460
- h4 = this.h[4],
18461
- h5 = this.h[5],
18462
- h6 = this.h[6],
18463
- h7 = this.h[7],
18464
- h8 = this.h[8],
18465
- h9 = this.h[9];
18466
-
18467
- var r0 = this.r[0],
18468
- r1 = this.r[1],
18469
- r2 = this.r[2],
18470
- r3 = this.r[3],
18471
- r4 = this.r[4],
18472
- r5 = this.r[5],
18473
- r6 = this.r[6],
18474
- r7 = this.r[7],
18475
- r8 = this.r[8],
18476
- r9 = this.r[9];
18477
-
18478
- while (bytes >= 16) {
18479
- t0 = m[mpos+ 0] & 0xff | (m[mpos+ 1] & 0xff) << 8; h0 += ( t0 ) & 0x1fff;
18480
- t1 = m[mpos+ 2] & 0xff | (m[mpos+ 3] & 0xff) << 8; h1 += ((t0 >>> 13) | (t1 << 3)) & 0x1fff;
18481
- t2 = m[mpos+ 4] & 0xff | (m[mpos+ 5] & 0xff) << 8; h2 += ((t1 >>> 10) | (t2 << 6)) & 0x1fff;
18482
- t3 = m[mpos+ 6] & 0xff | (m[mpos+ 7] & 0xff) << 8; h3 += ((t2 >>> 7) | (t3 << 9)) & 0x1fff;
18483
- t4 = m[mpos+ 8] & 0xff | (m[mpos+ 9] & 0xff) << 8; h4 += ((t3 >>> 4) | (t4 << 12)) & 0x1fff;
18484
- h5 += ((t4 >>> 1)) & 0x1fff;
18485
- t5 = m[mpos+10] & 0xff | (m[mpos+11] & 0xff) << 8; h6 += ((t4 >>> 14) | (t5 << 2)) & 0x1fff;
18486
- t6 = m[mpos+12] & 0xff | (m[mpos+13] & 0xff) << 8; h7 += ((t5 >>> 11) | (t6 << 5)) & 0x1fff;
18487
- t7 = m[mpos+14] & 0xff | (m[mpos+15] & 0xff) << 8; h8 += ((t6 >>> 8) | (t7 << 8)) & 0x1fff;
18488
- h9 += ((t7 >>> 5)) | hibit;
18489
-
18490
- c = 0;
18491
-
18492
- d0 = c;
18493
- d0 += h0 * r0;
18494
- d0 += h1 * (5 * r9);
18495
- d0 += h2 * (5 * r8);
18496
- d0 += h3 * (5 * r7);
18497
- d0 += h4 * (5 * r6);
18498
- c = (d0 >>> 13); d0 &= 0x1fff;
18499
- d0 += h5 * (5 * r5);
18500
- d0 += h6 * (5 * r4);
18501
- d0 += h7 * (5 * r3);
18502
- d0 += h8 * (5 * r2);
18503
- d0 += h9 * (5 * r1);
18504
- c += (d0 >>> 13); d0 &= 0x1fff;
18505
-
18506
- d1 = c;
18507
- d1 += h0 * r1;
18508
- d1 += h1 * r0;
18509
- d1 += h2 * (5 * r9);
18510
- d1 += h3 * (5 * r8);
18511
- d1 += h4 * (5 * r7);
18512
- c = (d1 >>> 13); d1 &= 0x1fff;
18513
- d1 += h5 * (5 * r6);
18514
- d1 += h6 * (5 * r5);
18515
- d1 += h7 * (5 * r4);
18516
- d1 += h8 * (5 * r3);
18517
- d1 += h9 * (5 * r2);
18518
- c += (d1 >>> 13); d1 &= 0x1fff;
18519
-
18520
- d2 = c;
18521
- d2 += h0 * r2;
18522
- d2 += h1 * r1;
18523
- d2 += h2 * r0;
18524
- d2 += h3 * (5 * r9);
18525
- d2 += h4 * (5 * r8);
18526
- c = (d2 >>> 13); d2 &= 0x1fff;
18527
- d2 += h5 * (5 * r7);
18528
- d2 += h6 * (5 * r6);
18529
- d2 += h7 * (5 * r5);
18530
- d2 += h8 * (5 * r4);
18531
- d2 += h9 * (5 * r3);
18532
- c += (d2 >>> 13); d2 &= 0x1fff;
18533
-
18534
- d3 = c;
18535
- d3 += h0 * r3;
18536
- d3 += h1 * r2;
18537
- d3 += h2 * r1;
18538
- d3 += h3 * r0;
18539
- d3 += h4 * (5 * r9);
18540
- c = (d3 >>> 13); d3 &= 0x1fff;
18541
- d3 += h5 * (5 * r8);
18542
- d3 += h6 * (5 * r7);
18543
- d3 += h7 * (5 * r6);
18544
- d3 += h8 * (5 * r5);
18545
- d3 += h9 * (5 * r4);
18546
- c += (d3 >>> 13); d3 &= 0x1fff;
18547
-
18548
- d4 = c;
18549
- d4 += h0 * r4;
18550
- d4 += h1 * r3;
18551
- d4 += h2 * r2;
18552
- d4 += h3 * r1;
18553
- d4 += h4 * r0;
18554
- c = (d4 >>> 13); d4 &= 0x1fff;
18555
- d4 += h5 * (5 * r9);
18556
- d4 += h6 * (5 * r8);
18557
- d4 += h7 * (5 * r7);
18558
- d4 += h8 * (5 * r6);
18559
- d4 += h9 * (5 * r5);
18560
- c += (d4 >>> 13); d4 &= 0x1fff;
18561
-
18562
- d5 = c;
18563
- d5 += h0 * r5;
18564
- d5 += h1 * r4;
18565
- d5 += h2 * r3;
18566
- d5 += h3 * r2;
18567
- d5 += h4 * r1;
18568
- c = (d5 >>> 13); d5 &= 0x1fff;
18569
- d5 += h5 * r0;
18570
- d5 += h6 * (5 * r9);
18571
- d5 += h7 * (5 * r8);
18572
- d5 += h8 * (5 * r7);
18573
- d5 += h9 * (5 * r6);
18574
- c += (d5 >>> 13); d5 &= 0x1fff;
18575
-
18576
- d6 = c;
18577
- d6 += h0 * r6;
18578
- d6 += h1 * r5;
18579
- d6 += h2 * r4;
18580
- d6 += h3 * r3;
18581
- d6 += h4 * r2;
18582
- c = (d6 >>> 13); d6 &= 0x1fff;
18583
- d6 += h5 * r1;
18584
- d6 += h6 * r0;
18585
- d6 += h7 * (5 * r9);
18586
- d6 += h8 * (5 * r8);
18587
- d6 += h9 * (5 * r7);
18588
- c += (d6 >>> 13); d6 &= 0x1fff;
18589
-
18590
- d7 = c;
18591
- d7 += h0 * r7;
18592
- d7 += h1 * r6;
18593
- d7 += h2 * r5;
18594
- d7 += h3 * r4;
18595
- d7 += h4 * r3;
18596
- c = (d7 >>> 13); d7 &= 0x1fff;
18597
- d7 += h5 * r2;
18598
- d7 += h6 * r1;
18599
- d7 += h7 * r0;
18600
- d7 += h8 * (5 * r9);
18601
- d7 += h9 * (5 * r8);
18602
- c += (d7 >>> 13); d7 &= 0x1fff;
18603
-
18604
- d8 = c;
18605
- d8 += h0 * r8;
18606
- d8 += h1 * r7;
18607
- d8 += h2 * r6;
18608
- d8 += h3 * r5;
18609
- d8 += h4 * r4;
18610
- c = (d8 >>> 13); d8 &= 0x1fff;
18611
- d8 += h5 * r3;
18612
- d8 += h6 * r2;
18613
- d8 += h7 * r1;
18614
- d8 += h8 * r0;
18615
- d8 += h9 * (5 * r9);
18616
- c += (d8 >>> 13); d8 &= 0x1fff;
18617
-
18618
- d9 = c;
18619
- d9 += h0 * r9;
18620
- d9 += h1 * r8;
18621
- d9 += h2 * r7;
18622
- d9 += h3 * r6;
18623
- d9 += h4 * r5;
18624
- c = (d9 >>> 13); d9 &= 0x1fff;
18625
- d9 += h5 * r4;
18626
- d9 += h6 * r3;
18627
- d9 += h7 * r2;
18628
- d9 += h8 * r1;
18629
- d9 += h9 * r0;
18630
- c += (d9 >>> 13); d9 &= 0x1fff;
18631
-
18632
- c = (((c << 2) + c)) | 0;
18633
- c = (c + d0) | 0;
18634
- d0 = c & 0x1fff;
18635
- c = (c >>> 13);
18636
- d1 += c;
18637
-
18638
- h0 = d0;
18639
- h1 = d1;
18640
- h2 = d2;
18641
- h3 = d3;
18642
- h4 = d4;
18643
- h5 = d5;
18644
- h6 = d6;
18645
- h7 = d7;
18646
- h8 = d8;
18647
- h9 = d9;
18648
-
18649
- mpos += 16;
18650
- bytes -= 16;
18651
- }
18652
- this.h[0] = h0;
18653
- this.h[1] = h1;
18654
- this.h[2] = h2;
18655
- this.h[3] = h3;
18656
- this.h[4] = h4;
18657
- this.h[5] = h5;
18658
- this.h[6] = h6;
18659
- this.h[7] = h7;
18660
- this.h[8] = h8;
18661
- this.h[9] = h9;
18662
- };
18663
-
18664
- poly1305.prototype.finish = function(mac, macpos) {
18665
- var g = new Uint16Array(10);
18666
- var c, mask, f, i;
18667
-
18668
- if (this.leftover) {
18669
- i = this.leftover;
18670
- this.buffer[i++] = 1;
18671
- for (; i < 16; i++) this.buffer[i] = 0;
18672
- this.fin = 1;
18673
- this.blocks(this.buffer, 0, 16);
18674
- }
18675
-
18676
- c = this.h[1] >>> 13;
18677
- this.h[1] &= 0x1fff;
18678
- for (i = 2; i < 10; i++) {
18679
- this.h[i] += c;
18680
- c = this.h[i] >>> 13;
18681
- this.h[i] &= 0x1fff;
18682
- }
18683
- this.h[0] += (c * 5);
18684
- c = this.h[0] >>> 13;
18685
- this.h[0] &= 0x1fff;
18686
- this.h[1] += c;
18687
- c = this.h[1] >>> 13;
18688
- this.h[1] &= 0x1fff;
18689
- this.h[2] += c;
18690
-
18691
- g[0] = this.h[0] + 5;
18692
- c = g[0] >>> 13;
18693
- g[0] &= 0x1fff;
18694
- for (i = 1; i < 10; i++) {
18695
- g[i] = this.h[i] + c;
18696
- c = g[i] >>> 13;
18697
- g[i] &= 0x1fff;
18698
- }
18699
- g[9] -= (1 << 13);
18700
-
18701
- mask = (c ^ 1) - 1;
18702
- for (i = 0; i < 10; i++) g[i] &= mask;
18703
- mask = ~mask;
18704
- for (i = 0; i < 10; i++) this.h[i] = (this.h[i] & mask) | g[i];
18705
-
18706
- this.h[0] = ((this.h[0] ) | (this.h[1] << 13) ) & 0xffff;
18707
- this.h[1] = ((this.h[1] >>> 3) | (this.h[2] << 10) ) & 0xffff;
18708
- this.h[2] = ((this.h[2] >>> 6) | (this.h[3] << 7) ) & 0xffff;
18709
- this.h[3] = ((this.h[3] >>> 9) | (this.h[4] << 4) ) & 0xffff;
18710
- this.h[4] = ((this.h[4] >>> 12) | (this.h[5] << 1) | (this.h[6] << 14)) & 0xffff;
18711
- this.h[5] = ((this.h[6] >>> 2) | (this.h[7] << 11) ) & 0xffff;
18712
- this.h[6] = ((this.h[7] >>> 5) | (this.h[8] << 8) ) & 0xffff;
18713
- this.h[7] = ((this.h[8] >>> 8) | (this.h[9] << 5) ) & 0xffff;
18714
-
18715
- f = this.h[0] + this.pad[0];
18716
- this.h[0] = f & 0xffff;
18717
- for (i = 1; i < 8; i++) {
18718
- f = (((this.h[i] + this.pad[i]) | 0) + (f >>> 16)) | 0;
18719
- this.h[i] = f & 0xffff;
18720
- }
18721
-
18722
- mac[macpos+ 0] = (this.h[0] >>> 0) & 0xff;
18723
- mac[macpos+ 1] = (this.h[0] >>> 8) & 0xff;
18724
- mac[macpos+ 2] = (this.h[1] >>> 0) & 0xff;
18725
- mac[macpos+ 3] = (this.h[1] >>> 8) & 0xff;
18726
- mac[macpos+ 4] = (this.h[2] >>> 0) & 0xff;
18727
- mac[macpos+ 5] = (this.h[2] >>> 8) & 0xff;
18728
- mac[macpos+ 6] = (this.h[3] >>> 0) & 0xff;
18729
- mac[macpos+ 7] = (this.h[3] >>> 8) & 0xff;
18730
- mac[macpos+ 8] = (this.h[4] >>> 0) & 0xff;
18731
- mac[macpos+ 9] = (this.h[4] >>> 8) & 0xff;
18732
- mac[macpos+10] = (this.h[5] >>> 0) & 0xff;
18733
- mac[macpos+11] = (this.h[5] >>> 8) & 0xff;
18734
- mac[macpos+12] = (this.h[6] >>> 0) & 0xff;
18735
- mac[macpos+13] = (this.h[6] >>> 8) & 0xff;
18736
- mac[macpos+14] = (this.h[7] >>> 0) & 0xff;
18737
- mac[macpos+15] = (this.h[7] >>> 8) & 0xff;
18738
- };
18739
-
18740
- poly1305.prototype.update = function(m, mpos, bytes) {
18741
- var i, want;
18742
-
18743
- if (this.leftover) {
18744
- want = (16 - this.leftover);
18745
- if (want > bytes)
18746
- want = bytes;
18747
- for (i = 0; i < want; i++)
18748
- this.buffer[this.leftover + i] = m[mpos+i];
18749
- bytes -= want;
18750
- mpos += want;
18751
- this.leftover += want;
18752
- if (this.leftover < 16)
18753
- return;
18754
- this.blocks(this.buffer, 0, 16);
18755
- this.leftover = 0;
18756
- }
18757
-
18758
- if (bytes >= 16) {
18759
- want = bytes - (bytes % 16);
18760
- this.blocks(m, mpos, want);
18761
- mpos += want;
18762
- bytes -= want;
18763
- }
18764
-
18765
- if (bytes) {
18766
- for (i = 0; i < bytes; i++)
18767
- this.buffer[this.leftover + i] = m[mpos+i];
18768
- this.leftover += bytes;
18769
- }
18770
- };
18771
-
18772
- function crypto_onetimeauth(out, outpos, m, mpos, n, k) {
18773
- var s = new poly1305(k);
18774
- s.update(m, mpos, n);
18775
- s.finish(out, outpos);
18776
- return 0;
18777
- }
18778
-
18779
- function crypto_onetimeauth_verify(h, hpos, m, mpos, n, k) {
18780
- var x = new Uint8Array(16);
18781
- crypto_onetimeauth(x,0,m,mpos,n,k);
18782
- return crypto_verify_16(h,hpos,x,0);
18783
- }
18784
-
18785
- function crypto_secretbox(c,m,d,n,k) {
18786
- var i;
18787
- if (d < 32) return -1;
18788
- crypto_stream_xor(c,0,m,0,d,n,k);
18789
- crypto_onetimeauth(c, 16, c, 32, d - 32, c);
18790
- for (i = 0; i < 16; i++) c[i] = 0;
18791
- return 0;
18792
- }
18793
-
18794
- function crypto_secretbox_open(m,c,d,n,k) {
18795
- var i;
18796
- var x = new Uint8Array(32);
18797
- if (d < 32) return -1;
18798
- crypto_stream(x,0,32,n,k);
18799
- if (crypto_onetimeauth_verify(c, 16,c, 32,d - 32,x) !== 0) return -1;
18800
- crypto_stream_xor(m,0,c,0,d,n,k);
18801
- for (i = 0; i < 32; i++) m[i] = 0;
18802
- return 0;
18803
- }
18804
-
18805
- function set25519(r, a) {
18806
- var i;
18807
- for (i = 0; i < 16; i++) r[i] = a[i]|0;
18808
- }
18809
-
18810
- function car25519(o) {
18811
- var i, v, c = 1;
18812
- for (i = 0; i < 16; i++) {
18813
- v = o[i] + c + 65535;
18814
- c = Math.floor(v / 65536);
18815
- o[i] = v - c * 65536;
18816
- }
18817
- o[0] += c-1 + 37 * (c-1);
18818
- }
18819
-
18820
- function sel25519(p, q, b) {
18821
- var t, c = ~(b-1);
18822
- for (var i = 0; i < 16; i++) {
18823
- t = c & (p[i] ^ q[i]);
18824
- p[i] ^= t;
18825
- q[i] ^= t;
18826
- }
18827
- }
18828
-
18829
- function pack25519(o, n) {
18830
- var i, j, b;
18831
- var m = gf(), t = gf();
18832
- for (i = 0; i < 16; i++) t[i] = n[i];
18833
- car25519(t);
18834
- car25519(t);
18835
- car25519(t);
18836
- for (j = 0; j < 2; j++) {
18837
- m[0] = t[0] - 0xffed;
18838
- for (i = 1; i < 15; i++) {
18839
- m[i] = t[i] - 0xffff - ((m[i-1]>>16) & 1);
18840
- m[i-1] &= 0xffff;
18841
- }
18842
- m[15] = t[15] - 0x7fff - ((m[14]>>16) & 1);
18843
- b = (m[15]>>16) & 1;
18844
- m[14] &= 0xffff;
18845
- sel25519(t, m, 1-b);
18846
- }
18847
- for (i = 0; i < 16; i++) {
18848
- o[2*i] = t[i] & 0xff;
18849
- o[2*i+1] = t[i]>>8;
18850
- }
18851
- }
18852
-
18853
- function neq25519(a, b) {
18854
- var c = new Uint8Array(32), d = new Uint8Array(32);
18855
- pack25519(c, a);
18856
- pack25519(d, b);
18857
- return crypto_verify_32(c, 0, d, 0);
18858
- }
18859
-
18860
- function par25519(a) {
18861
- var d = new Uint8Array(32);
18862
- pack25519(d, a);
18863
- return d[0] & 1;
18864
- }
18865
-
18866
- function unpack25519(o, n) {
18867
- var i;
18868
- for (i = 0; i < 16; i++) o[i] = n[2*i] + (n[2*i+1] << 8);
18869
- o[15] &= 0x7fff;
18870
- }
18871
-
18872
- function A(o, a, b) {
18873
- for (var i = 0; i < 16; i++) o[i] = a[i] + b[i];
18874
- }
18875
-
18876
- function Z(o, a, b) {
18877
- for (var i = 0; i < 16; i++) o[i] = a[i] - b[i];
18878
- }
18879
-
18880
- function M(o, a, b) {
18881
- var v, c,
18882
- t0 = 0, t1 = 0, t2 = 0, t3 = 0, t4 = 0, t5 = 0, t6 = 0, t7 = 0,
18883
- t8 = 0, t9 = 0, t10 = 0, t11 = 0, t12 = 0, t13 = 0, t14 = 0, t15 = 0,
18884
- t16 = 0, t17 = 0, t18 = 0, t19 = 0, t20 = 0, t21 = 0, t22 = 0, t23 = 0,
18885
- t24 = 0, t25 = 0, t26 = 0, t27 = 0, t28 = 0, t29 = 0, t30 = 0,
18886
- b0 = b[0],
18887
- b1 = b[1],
18888
- b2 = b[2],
18889
- b3 = b[3],
18890
- b4 = b[4],
18891
- b5 = b[5],
18892
- b6 = b[6],
18893
- b7 = b[7],
18894
- b8 = b[8],
18895
- b9 = b[9],
18896
- b10 = b[10],
18897
- b11 = b[11],
18898
- b12 = b[12],
18899
- b13 = b[13],
18900
- b14 = b[14],
18901
- b15 = b[15];
18902
-
18903
- v = a[0];
18904
- t0 += v * b0;
18905
- t1 += v * b1;
18906
- t2 += v * b2;
18907
- t3 += v * b3;
18908
- t4 += v * b4;
18909
- t5 += v * b5;
18910
- t6 += v * b6;
18911
- t7 += v * b7;
18912
- t8 += v * b8;
18913
- t9 += v * b9;
18914
- t10 += v * b10;
18915
- t11 += v * b11;
18916
- t12 += v * b12;
18917
- t13 += v * b13;
18918
- t14 += v * b14;
18919
- t15 += v * b15;
18920
- v = a[1];
18921
- t1 += v * b0;
18922
- t2 += v * b1;
18923
- t3 += v * b2;
18924
- t4 += v * b3;
18925
- t5 += v * b4;
18926
- t6 += v * b5;
18927
- t7 += v * b6;
18928
- t8 += v * b7;
18929
- t9 += v * b8;
18930
- t10 += v * b9;
18931
- t11 += v * b10;
18932
- t12 += v * b11;
18933
- t13 += v * b12;
18934
- t14 += v * b13;
18935
- t15 += v * b14;
18936
- t16 += v * b15;
18937
- v = a[2];
18938
- t2 += v * b0;
18939
- t3 += v * b1;
18940
- t4 += v * b2;
18941
- t5 += v * b3;
18942
- t6 += v * b4;
18943
- t7 += v * b5;
18944
- t8 += v * b6;
18945
- t9 += v * b7;
18946
- t10 += v * b8;
18947
- t11 += v * b9;
18948
- t12 += v * b10;
18949
- t13 += v * b11;
18950
- t14 += v * b12;
18951
- t15 += v * b13;
18952
- t16 += v * b14;
18953
- t17 += v * b15;
18954
- v = a[3];
18955
- t3 += v * b0;
18956
- t4 += v * b1;
18957
- t5 += v * b2;
18958
- t6 += v * b3;
18959
- t7 += v * b4;
18960
- t8 += v * b5;
18961
- t9 += v * b6;
18962
- t10 += v * b7;
18963
- t11 += v * b8;
18964
- t12 += v * b9;
18965
- t13 += v * b10;
18966
- t14 += v * b11;
18967
- t15 += v * b12;
18968
- t16 += v * b13;
18969
- t17 += v * b14;
18970
- t18 += v * b15;
18971
- v = a[4];
18972
- t4 += v * b0;
18973
- t5 += v * b1;
18974
- t6 += v * b2;
18975
- t7 += v * b3;
18976
- t8 += v * b4;
18977
- t9 += v * b5;
18978
- t10 += v * b6;
18979
- t11 += v * b7;
18980
- t12 += v * b8;
18981
- t13 += v * b9;
18982
- t14 += v * b10;
18983
- t15 += v * b11;
18984
- t16 += v * b12;
18985
- t17 += v * b13;
18986
- t18 += v * b14;
18987
- t19 += v * b15;
18988
- v = a[5];
18989
- t5 += v * b0;
18990
- t6 += v * b1;
18991
- t7 += v * b2;
18992
- t8 += v * b3;
18993
- t9 += v * b4;
18994
- t10 += v * b5;
18995
- t11 += v * b6;
18996
- t12 += v * b7;
18997
- t13 += v * b8;
18998
- t14 += v * b9;
18999
- t15 += v * b10;
19000
- t16 += v * b11;
19001
- t17 += v * b12;
19002
- t18 += v * b13;
19003
- t19 += v * b14;
19004
- t20 += v * b15;
19005
- v = a[6];
19006
- t6 += v * b0;
19007
- t7 += v * b1;
19008
- t8 += v * b2;
19009
- t9 += v * b3;
19010
- t10 += v * b4;
19011
- t11 += v * b5;
19012
- t12 += v * b6;
19013
- t13 += v * b7;
19014
- t14 += v * b8;
19015
- t15 += v * b9;
19016
- t16 += v * b10;
19017
- t17 += v * b11;
19018
- t18 += v * b12;
19019
- t19 += v * b13;
19020
- t20 += v * b14;
19021
- t21 += v * b15;
19022
- v = a[7];
19023
- t7 += v * b0;
19024
- t8 += v * b1;
19025
- t9 += v * b2;
19026
- t10 += v * b3;
19027
- t11 += v * b4;
19028
- t12 += v * b5;
19029
- t13 += v * b6;
19030
- t14 += v * b7;
19031
- t15 += v * b8;
19032
- t16 += v * b9;
19033
- t17 += v * b10;
19034
- t18 += v * b11;
19035
- t19 += v * b12;
19036
- t20 += v * b13;
19037
- t21 += v * b14;
19038
- t22 += v * b15;
19039
- v = a[8];
19040
- t8 += v * b0;
19041
- t9 += v * b1;
19042
- t10 += v * b2;
19043
- t11 += v * b3;
19044
- t12 += v * b4;
19045
- t13 += v * b5;
19046
- t14 += v * b6;
19047
- t15 += v * b7;
19048
- t16 += v * b8;
19049
- t17 += v * b9;
19050
- t18 += v * b10;
19051
- t19 += v * b11;
19052
- t20 += v * b12;
19053
- t21 += v * b13;
19054
- t22 += v * b14;
19055
- t23 += v * b15;
19056
- v = a[9];
19057
- t9 += v * b0;
19058
- t10 += v * b1;
19059
- t11 += v * b2;
19060
- t12 += v * b3;
19061
- t13 += v * b4;
19062
- t14 += v * b5;
19063
- t15 += v * b6;
19064
- t16 += v * b7;
19065
- t17 += v * b8;
19066
- t18 += v * b9;
19067
- t19 += v * b10;
19068
- t20 += v * b11;
19069
- t21 += v * b12;
19070
- t22 += v * b13;
19071
- t23 += v * b14;
19072
- t24 += v * b15;
19073
- v = a[10];
19074
- t10 += v * b0;
19075
- t11 += v * b1;
19076
- t12 += v * b2;
19077
- t13 += v * b3;
19078
- t14 += v * b4;
19079
- t15 += v * b5;
19080
- t16 += v * b6;
19081
- t17 += v * b7;
19082
- t18 += v * b8;
19083
- t19 += v * b9;
19084
- t20 += v * b10;
19085
- t21 += v * b11;
19086
- t22 += v * b12;
19087
- t23 += v * b13;
19088
- t24 += v * b14;
19089
- t25 += v * b15;
19090
- v = a[11];
19091
- t11 += v * b0;
19092
- t12 += v * b1;
19093
- t13 += v * b2;
19094
- t14 += v * b3;
19095
- t15 += v * b4;
19096
- t16 += v * b5;
19097
- t17 += v * b6;
19098
- t18 += v * b7;
19099
- t19 += v * b8;
19100
- t20 += v * b9;
19101
- t21 += v * b10;
19102
- t22 += v * b11;
19103
- t23 += v * b12;
19104
- t24 += v * b13;
19105
- t25 += v * b14;
19106
- t26 += v * b15;
19107
- v = a[12];
19108
- t12 += v * b0;
19109
- t13 += v * b1;
19110
- t14 += v * b2;
19111
- t15 += v * b3;
19112
- t16 += v * b4;
19113
- t17 += v * b5;
19114
- t18 += v * b6;
19115
- t19 += v * b7;
19116
- t20 += v * b8;
19117
- t21 += v * b9;
19118
- t22 += v * b10;
19119
- t23 += v * b11;
19120
- t24 += v * b12;
19121
- t25 += v * b13;
19122
- t26 += v * b14;
19123
- t27 += v * b15;
19124
- v = a[13];
19125
- t13 += v * b0;
19126
- t14 += v * b1;
19127
- t15 += v * b2;
19128
- t16 += v * b3;
19129
- t17 += v * b4;
19130
- t18 += v * b5;
19131
- t19 += v * b6;
19132
- t20 += v * b7;
19133
- t21 += v * b8;
19134
- t22 += v * b9;
19135
- t23 += v * b10;
19136
- t24 += v * b11;
19137
- t25 += v * b12;
19138
- t26 += v * b13;
19139
- t27 += v * b14;
19140
- t28 += v * b15;
19141
- v = a[14];
19142
- t14 += v * b0;
19143
- t15 += v * b1;
19144
- t16 += v * b2;
19145
- t17 += v * b3;
19146
- t18 += v * b4;
19147
- t19 += v * b5;
19148
- t20 += v * b6;
19149
- t21 += v * b7;
19150
- t22 += v * b8;
19151
- t23 += v * b9;
19152
- t24 += v * b10;
19153
- t25 += v * b11;
19154
- t26 += v * b12;
19155
- t27 += v * b13;
19156
- t28 += v * b14;
19157
- t29 += v * b15;
19158
- v = a[15];
19159
- t15 += v * b0;
19160
- t16 += v * b1;
19161
- t17 += v * b2;
19162
- t18 += v * b3;
19163
- t19 += v * b4;
19164
- t20 += v * b5;
19165
- t21 += v * b6;
19166
- t22 += v * b7;
19167
- t23 += v * b8;
19168
- t24 += v * b9;
19169
- t25 += v * b10;
19170
- t26 += v * b11;
19171
- t27 += v * b12;
19172
- t28 += v * b13;
19173
- t29 += v * b14;
19174
- t30 += v * b15;
19175
-
19176
- t0 += 38 * t16;
19177
- t1 += 38 * t17;
19178
- t2 += 38 * t18;
19179
- t3 += 38 * t19;
19180
- t4 += 38 * t20;
19181
- t5 += 38 * t21;
19182
- t6 += 38 * t22;
19183
- t7 += 38 * t23;
19184
- t8 += 38 * t24;
19185
- t9 += 38 * t25;
19186
- t10 += 38 * t26;
19187
- t11 += 38 * t27;
19188
- t12 += 38 * t28;
19189
- t13 += 38 * t29;
19190
- t14 += 38 * t30;
19191
- // t15 left as is
19192
-
19193
- // first car
19194
- c = 1;
19195
- v = t0 + c + 65535; c = Math.floor(v / 65536); t0 = v - c * 65536;
19196
- v = t1 + c + 65535; c = Math.floor(v / 65536); t1 = v - c * 65536;
19197
- v = t2 + c + 65535; c = Math.floor(v / 65536); t2 = v - c * 65536;
19198
- v = t3 + c + 65535; c = Math.floor(v / 65536); t3 = v - c * 65536;
19199
- v = t4 + c + 65535; c = Math.floor(v / 65536); t4 = v - c * 65536;
19200
- v = t5 + c + 65535; c = Math.floor(v / 65536); t5 = v - c * 65536;
19201
- v = t6 + c + 65535; c = Math.floor(v / 65536); t6 = v - c * 65536;
19202
- v = t7 + c + 65535; c = Math.floor(v / 65536); t7 = v - c * 65536;
19203
- v = t8 + c + 65535; c = Math.floor(v / 65536); t8 = v - c * 65536;
19204
- v = t9 + c + 65535; c = Math.floor(v / 65536); t9 = v - c * 65536;
19205
- v = t10 + c + 65535; c = Math.floor(v / 65536); t10 = v - c * 65536;
19206
- v = t11 + c + 65535; c = Math.floor(v / 65536); t11 = v - c * 65536;
19207
- v = t12 + c + 65535; c = Math.floor(v / 65536); t12 = v - c * 65536;
19208
- v = t13 + c + 65535; c = Math.floor(v / 65536); t13 = v - c * 65536;
19209
- v = t14 + c + 65535; c = Math.floor(v / 65536); t14 = v - c * 65536;
19210
- v = t15 + c + 65535; c = Math.floor(v / 65536); t15 = v - c * 65536;
19211
- t0 += c-1 + 37 * (c-1);
19212
-
19213
- // second car
19214
- c = 1;
19215
- v = t0 + c + 65535; c = Math.floor(v / 65536); t0 = v - c * 65536;
19216
- v = t1 + c + 65535; c = Math.floor(v / 65536); t1 = v - c * 65536;
19217
- v = t2 + c + 65535; c = Math.floor(v / 65536); t2 = v - c * 65536;
19218
- v = t3 + c + 65535; c = Math.floor(v / 65536); t3 = v - c * 65536;
19219
- v = t4 + c + 65535; c = Math.floor(v / 65536); t4 = v - c * 65536;
19220
- v = t5 + c + 65535; c = Math.floor(v / 65536); t5 = v - c * 65536;
19221
- v = t6 + c + 65535; c = Math.floor(v / 65536); t6 = v - c * 65536;
19222
- v = t7 + c + 65535; c = Math.floor(v / 65536); t7 = v - c * 65536;
19223
- v = t8 + c + 65535; c = Math.floor(v / 65536); t8 = v - c * 65536;
19224
- v = t9 + c + 65535; c = Math.floor(v / 65536); t9 = v - c * 65536;
19225
- v = t10 + c + 65535; c = Math.floor(v / 65536); t10 = v - c * 65536;
19226
- v = t11 + c + 65535; c = Math.floor(v / 65536); t11 = v - c * 65536;
19227
- v = t12 + c + 65535; c = Math.floor(v / 65536); t12 = v - c * 65536;
19228
- v = t13 + c + 65535; c = Math.floor(v / 65536); t13 = v - c * 65536;
19229
- v = t14 + c + 65535; c = Math.floor(v / 65536); t14 = v - c * 65536;
19230
- v = t15 + c + 65535; c = Math.floor(v / 65536); t15 = v - c * 65536;
19231
- t0 += c-1 + 37 * (c-1);
19232
-
19233
- o[ 0] = t0;
19234
- o[ 1] = t1;
19235
- o[ 2] = t2;
19236
- o[ 3] = t3;
19237
- o[ 4] = t4;
19238
- o[ 5] = t5;
19239
- o[ 6] = t6;
19240
- o[ 7] = t7;
19241
- o[ 8] = t8;
19242
- o[ 9] = t9;
19243
- o[10] = t10;
19244
- o[11] = t11;
19245
- o[12] = t12;
19246
- o[13] = t13;
19247
- o[14] = t14;
19248
- o[15] = t15;
19249
- }
19250
-
19251
- function S(o, a) {
19252
- M(o, a, a);
19253
- }
19254
-
19255
- function inv25519(o, i) {
19256
- var c = gf();
19257
- var a;
19258
- for (a = 0; a < 16; a++) c[a] = i[a];
19259
- for (a = 253; a >= 0; a--) {
19260
- S(c, c);
19261
- if(a !== 2 && a !== 4) M(c, c, i);
19262
- }
19263
- for (a = 0; a < 16; a++) o[a] = c[a];
19264
- }
19265
-
19266
- function pow2523(o, i) {
19267
- var c = gf();
19268
- var a;
19269
- for (a = 0; a < 16; a++) c[a] = i[a];
19270
- for (a = 250; a >= 0; a--) {
19271
- S(c, c);
19272
- if(a !== 1) M(c, c, i);
19273
- }
19274
- for (a = 0; a < 16; a++) o[a] = c[a];
19275
- }
19276
-
19277
- function crypto_scalarmult(q, n, p) {
19278
- var z = new Uint8Array(32);
19279
- var x = new Float64Array(80), r, i;
19280
- var a = gf(), b = gf(), c = gf(),
19281
- d = gf(), e = gf(), f = gf();
19282
- for (i = 0; i < 31; i++) z[i] = n[i];
19283
- z[31]=(n[31]&127)|64;
19284
- z[0]&=248;
19285
- unpack25519(x,p);
19286
- for (i = 0; i < 16; i++) {
19287
- b[i]=x[i];
19288
- d[i]=a[i]=c[i]=0;
19289
- }
19290
- a[0]=d[0]=1;
19291
- for (i=254; i>=0; --i) {
19292
- r=(z[i>>>3]>>>(i&7))&1;
19293
- sel25519(a,b,r);
19294
- sel25519(c,d,r);
19295
- A(e,a,c);
19296
- Z(a,a,c);
19297
- A(c,b,d);
19298
- Z(b,b,d);
19299
- S(d,e);
19300
- S(f,a);
19301
- M(a,c,a);
19302
- M(c,b,e);
19303
- A(e,a,c);
19304
- Z(a,a,c);
19305
- S(b,a);
19306
- Z(c,d,f);
19307
- M(a,c,_121665);
19308
- A(a,a,d);
19309
- M(c,c,a);
19310
- M(a,d,f);
19311
- M(d,b,x);
19312
- S(b,e);
19313
- sel25519(a,b,r);
19314
- sel25519(c,d,r);
19315
- }
19316
- for (i = 0; i < 16; i++) {
19317
- x[i+16]=a[i];
19318
- x[i+32]=c[i];
19319
- x[i+48]=b[i];
19320
- x[i+64]=d[i];
19321
- }
19322
- var x32 = x.subarray(32);
19323
- var x16 = x.subarray(16);
19324
- inv25519(x32,x32);
19325
- M(x16,x16,x32);
19326
- pack25519(q,x16);
19327
- return 0;
19328
- }
19329
-
19330
- function crypto_scalarmult_base(q, n) {
19331
- return crypto_scalarmult(q, n, _9);
19332
- }
19333
-
19334
- function crypto_box_keypair(y, x) {
19335
- randombytes(x, 32);
19336
- return crypto_scalarmult_base(y, x);
19337
- }
19338
-
19339
- function crypto_box_beforenm(k, y, x) {
19340
- var s = new Uint8Array(32);
19341
- crypto_scalarmult(s, x, y);
19342
- return crypto_core_hsalsa20(k, _0, s, sigma);
19343
- }
19344
-
19345
- var crypto_box_afternm = crypto_secretbox;
19346
- var crypto_box_open_afternm = crypto_secretbox_open;
19347
-
19348
- function crypto_box(c, m, d, n, y, x) {
19349
- var k = new Uint8Array(32);
19350
- crypto_box_beforenm(k, y, x);
19351
- return crypto_box_afternm(c, m, d, n, k);
19352
- }
19353
-
19354
- function crypto_box_open(m, c, d, n, y, x) {
19355
- var k = new Uint8Array(32);
19356
- crypto_box_beforenm(k, y, x);
19357
- return crypto_box_open_afternm(m, c, d, n, k);
19358
- }
19359
-
19360
- var K = [
19361
- 0x428a2f98, 0xd728ae22, 0x71374491, 0x23ef65cd,
19362
- 0xb5c0fbcf, 0xec4d3b2f, 0xe9b5dba5, 0x8189dbbc,
19363
- 0x3956c25b, 0xf348b538, 0x59f111f1, 0xb605d019,
19364
- 0x923f82a4, 0xaf194f9b, 0xab1c5ed5, 0xda6d8118,
19365
- 0xd807aa98, 0xa3030242, 0x12835b01, 0x45706fbe,
19366
- 0x243185be, 0x4ee4b28c, 0x550c7dc3, 0xd5ffb4e2,
19367
- 0x72be5d74, 0xf27b896f, 0x80deb1fe, 0x3b1696b1,
19368
- 0x9bdc06a7, 0x25c71235, 0xc19bf174, 0xcf692694,
19369
- 0xe49b69c1, 0x9ef14ad2, 0xefbe4786, 0x384f25e3,
19370
- 0x0fc19dc6, 0x8b8cd5b5, 0x240ca1cc, 0x77ac9c65,
19371
- 0x2de92c6f, 0x592b0275, 0x4a7484aa, 0x6ea6e483,
19372
- 0x5cb0a9dc, 0xbd41fbd4, 0x76f988da, 0x831153b5,
19373
- 0x983e5152, 0xee66dfab, 0xa831c66d, 0x2db43210,
19374
- 0xb00327c8, 0x98fb213f, 0xbf597fc7, 0xbeef0ee4,
19375
- 0xc6e00bf3, 0x3da88fc2, 0xd5a79147, 0x930aa725,
19376
- 0x06ca6351, 0xe003826f, 0x14292967, 0x0a0e6e70,
19377
- 0x27b70a85, 0x46d22ffc, 0x2e1b2138, 0x5c26c926,
19378
- 0x4d2c6dfc, 0x5ac42aed, 0x53380d13, 0x9d95b3df,
19379
- 0x650a7354, 0x8baf63de, 0x766a0abb, 0x3c77b2a8,
19380
- 0x81c2c92e, 0x47edaee6, 0x92722c85, 0x1482353b,
19381
- 0xa2bfe8a1, 0x4cf10364, 0xa81a664b, 0xbc423001,
19382
- 0xc24b8b70, 0xd0f89791, 0xc76c51a3, 0x0654be30,
19383
- 0xd192e819, 0xd6ef5218, 0xd6990624, 0x5565a910,
19384
- 0xf40e3585, 0x5771202a, 0x106aa070, 0x32bbd1b8,
19385
- 0x19a4c116, 0xb8d2d0c8, 0x1e376c08, 0x5141ab53,
19386
- 0x2748774c, 0xdf8eeb99, 0x34b0bcb5, 0xe19b48a8,
19387
- 0x391c0cb3, 0xc5c95a63, 0x4ed8aa4a, 0xe3418acb,
19388
- 0x5b9cca4f, 0x7763e373, 0x682e6ff3, 0xd6b2b8a3,
19389
- 0x748f82ee, 0x5defb2fc, 0x78a5636f, 0x43172f60,
19390
- 0x84c87814, 0xa1f0ab72, 0x8cc70208, 0x1a6439ec,
19391
- 0x90befffa, 0x23631e28, 0xa4506ceb, 0xde82bde9,
19392
- 0xbef9a3f7, 0xb2c67915, 0xc67178f2, 0xe372532b,
19393
- 0xca273ece, 0xea26619c, 0xd186b8c7, 0x21c0c207,
19394
- 0xeada7dd6, 0xcde0eb1e, 0xf57d4f7f, 0xee6ed178,
19395
- 0x06f067aa, 0x72176fba, 0x0a637dc5, 0xa2c898a6,
19396
- 0x113f9804, 0xbef90dae, 0x1b710b35, 0x131c471b,
19397
- 0x28db77f5, 0x23047d84, 0x32caab7b, 0x40c72493,
19398
- 0x3c9ebe0a, 0x15c9bebc, 0x431d67c4, 0x9c100d4c,
19399
- 0x4cc5d4be, 0xcb3e42b6, 0x597f299c, 0xfc657e2a,
19400
- 0x5fcb6fab, 0x3ad6faec, 0x6c44198c, 0x4a475817
19401
- ];
19402
-
19403
- function crypto_hashblocks_hl(hh, hl, m, n) {
19404
- var wh = new Int32Array(16), wl = new Int32Array(16),
19405
- bh0, bh1, bh2, bh3, bh4, bh5, bh6, bh7,
19406
- bl0, bl1, bl2, bl3, bl4, bl5, bl6, bl7,
19407
- th, tl, i, j, h, l, a, b, c, d;
19408
-
19409
- var ah0 = hh[0],
19410
- ah1 = hh[1],
19411
- ah2 = hh[2],
19412
- ah3 = hh[3],
19413
- ah4 = hh[4],
19414
- ah5 = hh[5],
19415
- ah6 = hh[6],
19416
- ah7 = hh[7],
19417
-
19418
- al0 = hl[0],
19419
- al1 = hl[1],
19420
- al2 = hl[2],
19421
- al3 = hl[3],
19422
- al4 = hl[4],
19423
- al5 = hl[5],
19424
- al6 = hl[6],
19425
- al7 = hl[7];
19426
-
19427
- var pos = 0;
19428
- while (n >= 128) {
19429
- for (i = 0; i < 16; i++) {
19430
- j = 8 * i + pos;
19431
- wh[i] = (m[j+0] << 24) | (m[j+1] << 16) | (m[j+2] << 8) | m[j+3];
19432
- wl[i] = (m[j+4] << 24) | (m[j+5] << 16) | (m[j+6] << 8) | m[j+7];
19433
- }
19434
- for (i = 0; i < 80; i++) {
19435
- bh0 = ah0;
19436
- bh1 = ah1;
19437
- bh2 = ah2;
19438
- bh3 = ah3;
19439
- bh4 = ah4;
19440
- bh5 = ah5;
19441
- bh6 = ah6;
19442
- bh7 = ah7;
19443
-
19444
- bl0 = al0;
19445
- bl1 = al1;
19446
- bl2 = al2;
19447
- bl3 = al3;
19448
- bl4 = al4;
19449
- bl5 = al5;
19450
- bl6 = al6;
19451
- bl7 = al7;
19452
-
19453
- // add
19454
- h = ah7;
19455
- l = al7;
19456
-
19457
- a = l & 0xffff; b = l >>> 16;
19458
- c = h & 0xffff; d = h >>> 16;
19459
-
19460
- // Sigma1
19461
- h = ((ah4 >>> 14) | (al4 << (32-14))) ^ ((ah4 >>> 18) | (al4 << (32-18))) ^ ((al4 >>> (41-32)) | (ah4 << (32-(41-32))));
19462
- l = ((al4 >>> 14) | (ah4 << (32-14))) ^ ((al4 >>> 18) | (ah4 << (32-18))) ^ ((ah4 >>> (41-32)) | (al4 << (32-(41-32))));
19463
-
19464
- a += l & 0xffff; b += l >>> 16;
19465
- c += h & 0xffff; d += h >>> 16;
19466
-
19467
- // Ch
19468
- h = (ah4 & ah5) ^ (~ah4 & ah6);
19469
- l = (al4 & al5) ^ (~al4 & al6);
19470
-
19471
- a += l & 0xffff; b += l >>> 16;
19472
- c += h & 0xffff; d += h >>> 16;
19473
-
19474
- // K
19475
- h = K[i*2];
19476
- l = K[i*2+1];
19477
-
19478
- a += l & 0xffff; b += l >>> 16;
19479
- c += h & 0xffff; d += h >>> 16;
19480
-
19481
- // w
19482
- h = wh[i%16];
19483
- l = wl[i%16];
19484
-
19485
- a += l & 0xffff; b += l >>> 16;
19486
- c += h & 0xffff; d += h >>> 16;
19487
-
19488
- b += a >>> 16;
19489
- c += b >>> 16;
19490
- d += c >>> 16;
19491
-
19492
- th = c & 0xffff | d << 16;
19493
- tl = a & 0xffff | b << 16;
19494
-
19495
- // add
19496
- h = th;
19497
- l = tl;
19498
-
19499
- a = l & 0xffff; b = l >>> 16;
19500
- c = h & 0xffff; d = h >>> 16;
19501
-
19502
- // Sigma0
19503
- h = ((ah0 >>> 28) | (al0 << (32-28))) ^ ((al0 >>> (34-32)) | (ah0 << (32-(34-32)))) ^ ((al0 >>> (39-32)) | (ah0 << (32-(39-32))));
19504
- l = ((al0 >>> 28) | (ah0 << (32-28))) ^ ((ah0 >>> (34-32)) | (al0 << (32-(34-32)))) ^ ((ah0 >>> (39-32)) | (al0 << (32-(39-32))));
19505
-
19506
- a += l & 0xffff; b += l >>> 16;
19507
- c += h & 0xffff; d += h >>> 16;
19508
-
19509
- // Maj
19510
- h = (ah0 & ah1) ^ (ah0 & ah2) ^ (ah1 & ah2);
19511
- l = (al0 & al1) ^ (al0 & al2) ^ (al1 & al2);
19512
-
19513
- a += l & 0xffff; b += l >>> 16;
19514
- c += h & 0xffff; d += h >>> 16;
19515
-
19516
- b += a >>> 16;
19517
- c += b >>> 16;
19518
- d += c >>> 16;
19519
-
19520
- bh7 = (c & 0xffff) | (d << 16);
19521
- bl7 = (a & 0xffff) | (b << 16);
19522
-
19523
- // add
19524
- h = bh3;
19525
- l = bl3;
19526
-
19527
- a = l & 0xffff; b = l >>> 16;
19528
- c = h & 0xffff; d = h >>> 16;
19529
-
19530
- h = th;
19531
- l = tl;
19532
-
19533
- a += l & 0xffff; b += l >>> 16;
19534
- c += h & 0xffff; d += h >>> 16;
19535
-
19536
- b += a >>> 16;
19537
- c += b >>> 16;
19538
- d += c >>> 16;
19539
-
19540
- bh3 = (c & 0xffff) | (d << 16);
19541
- bl3 = (a & 0xffff) | (b << 16);
19542
-
19543
- ah1 = bh0;
19544
- ah2 = bh1;
19545
- ah3 = bh2;
19546
- ah4 = bh3;
19547
- ah5 = bh4;
19548
- ah6 = bh5;
19549
- ah7 = bh6;
19550
- ah0 = bh7;
19551
-
19552
- al1 = bl0;
19553
- al2 = bl1;
19554
- al3 = bl2;
19555
- al4 = bl3;
19556
- al5 = bl4;
19557
- al6 = bl5;
19558
- al7 = bl6;
19559
- al0 = bl7;
19560
-
19561
- if (i%16 === 15) {
19562
- for (j = 0; j < 16; j++) {
19563
- // add
19564
- h = wh[j];
19565
- l = wl[j];
19566
-
19567
- a = l & 0xffff; b = l >>> 16;
19568
- c = h & 0xffff; d = h >>> 16;
19569
-
19570
- h = wh[(j+9)%16];
19571
- l = wl[(j+9)%16];
19572
-
19573
- a += l & 0xffff; b += l >>> 16;
19574
- c += h & 0xffff; d += h >>> 16;
19575
-
19576
- // sigma0
19577
- th = wh[(j+1)%16];
19578
- tl = wl[(j+1)%16];
19579
- h = ((th >>> 1) | (tl << (32-1))) ^ ((th >>> 8) | (tl << (32-8))) ^ (th >>> 7);
19580
- l = ((tl >>> 1) | (th << (32-1))) ^ ((tl >>> 8) | (th << (32-8))) ^ ((tl >>> 7) | (th << (32-7)));
19581
-
19582
- a += l & 0xffff; b += l >>> 16;
19583
- c += h & 0xffff; d += h >>> 16;
19584
-
19585
- // sigma1
19586
- th = wh[(j+14)%16];
19587
- tl = wl[(j+14)%16];
19588
- h = ((th >>> 19) | (tl << (32-19))) ^ ((tl >>> (61-32)) | (th << (32-(61-32)))) ^ (th >>> 6);
19589
- l = ((tl >>> 19) | (th << (32-19))) ^ ((th >>> (61-32)) | (tl << (32-(61-32)))) ^ ((tl >>> 6) | (th << (32-6)));
19590
-
19591
- a += l & 0xffff; b += l >>> 16;
19592
- c += h & 0xffff; d += h >>> 16;
19593
-
19594
- b += a >>> 16;
19595
- c += b >>> 16;
19596
- d += c >>> 16;
19597
-
19598
- wh[j] = (c & 0xffff) | (d << 16);
19599
- wl[j] = (a & 0xffff) | (b << 16);
19600
- }
19601
- }
19602
- }
19603
-
19604
- // add
19605
- h = ah0;
19606
- l = al0;
19607
-
19608
- a = l & 0xffff; b = l >>> 16;
19609
- c = h & 0xffff; d = h >>> 16;
19610
-
19611
- h = hh[0];
19612
- l = hl[0];
19613
-
19614
- a += l & 0xffff; b += l >>> 16;
19615
- c += h & 0xffff; d += h >>> 16;
19616
-
19617
- b += a >>> 16;
19618
- c += b >>> 16;
19619
- d += c >>> 16;
19620
-
19621
- hh[0] = ah0 = (c & 0xffff) | (d << 16);
19622
- hl[0] = al0 = (a & 0xffff) | (b << 16);
19623
-
19624
- h = ah1;
19625
- l = al1;
19626
-
19627
- a = l & 0xffff; b = l >>> 16;
19628
- c = h & 0xffff; d = h >>> 16;
19629
-
19630
- h = hh[1];
19631
- l = hl[1];
19632
-
19633
- a += l & 0xffff; b += l >>> 16;
19634
- c += h & 0xffff; d += h >>> 16;
19635
-
19636
- b += a >>> 16;
19637
- c += b >>> 16;
19638
- d += c >>> 16;
19639
-
19640
- hh[1] = ah1 = (c & 0xffff) | (d << 16);
19641
- hl[1] = al1 = (a & 0xffff) | (b << 16);
19642
-
19643
- h = ah2;
19644
- l = al2;
19645
-
19646
- a = l & 0xffff; b = l >>> 16;
19647
- c = h & 0xffff; d = h >>> 16;
19648
-
19649
- h = hh[2];
19650
- l = hl[2];
19651
-
19652
- a += l & 0xffff; b += l >>> 16;
19653
- c += h & 0xffff; d += h >>> 16;
19654
-
19655
- b += a >>> 16;
19656
- c += b >>> 16;
19657
- d += c >>> 16;
19658
-
19659
- hh[2] = ah2 = (c & 0xffff) | (d << 16);
19660
- hl[2] = al2 = (a & 0xffff) | (b << 16);
19661
-
19662
- h = ah3;
19663
- l = al3;
19664
-
19665
- a = l & 0xffff; b = l >>> 16;
19666
- c = h & 0xffff; d = h >>> 16;
19667
-
19668
- h = hh[3];
19669
- l = hl[3];
19670
-
19671
- a += l & 0xffff; b += l >>> 16;
19672
- c += h & 0xffff; d += h >>> 16;
19673
-
19674
- b += a >>> 16;
19675
- c += b >>> 16;
19676
- d += c >>> 16;
19677
-
19678
- hh[3] = ah3 = (c & 0xffff) | (d << 16);
19679
- hl[3] = al3 = (a & 0xffff) | (b << 16);
19680
-
19681
- h = ah4;
19682
- l = al4;
19683
-
19684
- a = l & 0xffff; b = l >>> 16;
19685
- c = h & 0xffff; d = h >>> 16;
19686
-
19687
- h = hh[4];
19688
- l = hl[4];
19689
-
19690
- a += l & 0xffff; b += l >>> 16;
19691
- c += h & 0xffff; d += h >>> 16;
19692
-
19693
- b += a >>> 16;
19694
- c += b >>> 16;
19695
- d += c >>> 16;
19696
-
19697
- hh[4] = ah4 = (c & 0xffff) | (d << 16);
19698
- hl[4] = al4 = (a & 0xffff) | (b << 16);
19699
-
19700
- h = ah5;
19701
- l = al5;
19702
-
19703
- a = l & 0xffff; b = l >>> 16;
19704
- c = h & 0xffff; d = h >>> 16;
19705
-
19706
- h = hh[5];
19707
- l = hl[5];
19708
-
19709
- a += l & 0xffff; b += l >>> 16;
19710
- c += h & 0xffff; d += h >>> 16;
19711
-
19712
- b += a >>> 16;
19713
- c += b >>> 16;
19714
- d += c >>> 16;
19715
-
19716
- hh[5] = ah5 = (c & 0xffff) | (d << 16);
19717
- hl[5] = al5 = (a & 0xffff) | (b << 16);
19718
-
19719
- h = ah6;
19720
- l = al6;
19721
-
19722
- a = l & 0xffff; b = l >>> 16;
19723
- c = h & 0xffff; d = h >>> 16;
19724
-
19725
- h = hh[6];
19726
- l = hl[6];
19727
-
19728
- a += l & 0xffff; b += l >>> 16;
19729
- c += h & 0xffff; d += h >>> 16;
19730
-
19731
- b += a >>> 16;
19732
- c += b >>> 16;
19733
- d += c >>> 16;
19734
-
19735
- hh[6] = ah6 = (c & 0xffff) | (d << 16);
19736
- hl[6] = al6 = (a & 0xffff) | (b << 16);
19737
-
19738
- h = ah7;
19739
- l = al7;
19740
-
19741
- a = l & 0xffff; b = l >>> 16;
19742
- c = h & 0xffff; d = h >>> 16;
19743
-
19744
- h = hh[7];
19745
- l = hl[7];
19746
-
19747
- a += l & 0xffff; b += l >>> 16;
19748
- c += h & 0xffff; d += h >>> 16;
19749
-
19750
- b += a >>> 16;
19751
- c += b >>> 16;
19752
- d += c >>> 16;
19753
-
19754
- hh[7] = ah7 = (c & 0xffff) | (d << 16);
19755
- hl[7] = al7 = (a & 0xffff) | (b << 16);
19756
-
19757
- pos += 128;
19758
- n -= 128;
19759
- }
19760
-
19761
- return n;
19762
- }
19763
-
19764
- function crypto_hash(out, m, n) {
19765
- var hh = new Int32Array(8),
19766
- hl = new Int32Array(8),
19767
- x = new Uint8Array(256),
19768
- i, b = n;
19769
-
19770
- hh[0] = 0x6a09e667;
19771
- hh[1] = 0xbb67ae85;
19772
- hh[2] = 0x3c6ef372;
19773
- hh[3] = 0xa54ff53a;
19774
- hh[4] = 0x510e527f;
19775
- hh[5] = 0x9b05688c;
19776
- hh[6] = 0x1f83d9ab;
19777
- hh[7] = 0x5be0cd19;
19778
-
19779
- hl[0] = 0xf3bcc908;
19780
- hl[1] = 0x84caa73b;
19781
- hl[2] = 0xfe94f82b;
19782
- hl[3] = 0x5f1d36f1;
19783
- hl[4] = 0xade682d1;
19784
- hl[5] = 0x2b3e6c1f;
19785
- hl[6] = 0xfb41bd6b;
19786
- hl[7] = 0x137e2179;
19787
-
19788
- crypto_hashblocks_hl(hh, hl, m, n);
19789
- n %= 128;
19790
-
19791
- for (i = 0; i < n; i++) x[i] = m[b-n+i];
19792
- x[n] = 128;
19793
-
19794
- n = 256-128*(n<112?1:0);
19795
- x[n-9] = 0;
19796
- ts64(x, n-8, (b / 0x20000000) | 0, b << 3);
19797
- crypto_hashblocks_hl(hh, hl, x, n);
19798
-
19799
- for (i = 0; i < 8; i++) ts64(out, 8*i, hh[i], hl[i]);
19800
-
19801
- return 0;
19802
- }
19803
-
19804
- function add(p, q) {
19805
- var a = gf(), b = gf(), c = gf(),
19806
- d = gf(), e = gf(), f = gf(),
19807
- g = gf(), h = gf(), t = gf();
19808
-
19809
- Z(a, p[1], p[0]);
19810
- Z(t, q[1], q[0]);
19811
- M(a, a, t);
19812
- A(b, p[0], p[1]);
19813
- A(t, q[0], q[1]);
19814
- M(b, b, t);
19815
- M(c, p[3], q[3]);
19816
- M(c, c, D2);
19817
- M(d, p[2], q[2]);
19818
- A(d, d, d);
19819
- Z(e, b, a);
19820
- Z(f, d, c);
19821
- A(g, d, c);
19822
- A(h, b, a);
19823
-
19824
- M(p[0], e, f);
19825
- M(p[1], h, g);
19826
- M(p[2], g, f);
19827
- M(p[3], e, h);
19828
- }
19829
-
19830
- function cswap(p, q, b) {
19831
- var i;
19832
- for (i = 0; i < 4; i++) {
19833
- sel25519(p[i], q[i], b);
19834
- }
19835
- }
19836
-
19837
- function pack(r, p) {
19838
- var tx = gf(), ty = gf(), zi = gf();
19839
- inv25519(zi, p[2]);
19840
- M(tx, p[0], zi);
19841
- M(ty, p[1], zi);
19842
- pack25519(r, ty);
19843
- r[31] ^= par25519(tx) << 7;
19844
- }
19845
-
19846
- function scalarmult(p, q, s) {
19847
- var b, i;
19848
- set25519(p[0], gf0);
19849
- set25519(p[1], gf1);
19850
- set25519(p[2], gf1);
19851
- set25519(p[3], gf0);
19852
- for (i = 255; i >= 0; --i) {
19853
- b = (s[(i/8)|0] >> (i&7)) & 1;
19854
- cswap(p, q, b);
19855
- add(q, p);
19856
- add(p, p);
19857
- cswap(p, q, b);
19858
- }
19859
- }
19860
-
19861
- function scalarbase(p, s) {
19862
- var q = [gf(), gf(), gf(), gf()];
19863
- set25519(q[0], X);
19864
- set25519(q[1], Y);
19865
- set25519(q[2], gf1);
19866
- M(q[3], X, Y);
19867
- scalarmult(p, q, s);
19868
- }
19869
-
19870
- function crypto_sign_keypair(pk, sk, seeded) {
19871
- var d = new Uint8Array(64);
19872
- var p = [gf(), gf(), gf(), gf()];
19873
- var i;
19874
-
19875
- if (!seeded) randombytes(sk, 32);
19876
- crypto_hash(d, sk, 32);
19877
- d[0] &= 248;
19878
- d[31] &= 127;
19879
- d[31] |= 64;
19880
-
19881
- scalarbase(p, d);
19882
- pack(pk, p);
19883
-
19884
- for (i = 0; i < 32; i++) sk[i+32] = pk[i];
19885
- return 0;
19886
- }
19887
-
19888
- var L = new Float64Array([0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58, 0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x10]);
19889
-
19890
- function modL(r, x) {
19891
- var carry, i, j, k;
19892
- for (i = 63; i >= 32; --i) {
19893
- carry = 0;
19894
- for (j = i - 32, k = i - 12; j < k; ++j) {
19895
- x[j] += carry - 16 * x[i] * L[j - (i - 32)];
19896
- carry = Math.floor((x[j] + 128) / 256);
19897
- x[j] -= carry * 256;
19898
- }
19899
- x[j] += carry;
19900
- x[i] = 0;
19901
- }
19902
- carry = 0;
19903
- for (j = 0; j < 32; j++) {
19904
- x[j] += carry - (x[31] >> 4) * L[j];
19905
- carry = x[j] >> 8;
19906
- x[j] &= 255;
19907
- }
19908
- for (j = 0; j < 32; j++) x[j] -= carry * L[j];
19909
- for (i = 0; i < 32; i++) {
19910
- x[i+1] += x[i] >> 8;
19911
- r[i] = x[i] & 255;
19912
- }
19913
- }
19914
-
19915
- function reduce(r) {
19916
- var x = new Float64Array(64), i;
19917
- for (i = 0; i < 64; i++) x[i] = r[i];
19918
- for (i = 0; i < 64; i++) r[i] = 0;
19919
- modL(r, x);
19920
- }
19921
-
19922
- // Note: difference from C - smlen returned, not passed as argument.
19923
- function crypto_sign(sm, m, n, sk) {
19924
- var d = new Uint8Array(64), h = new Uint8Array(64), r = new Uint8Array(64);
19925
- var i, j, x = new Float64Array(64);
19926
- var p = [gf(), gf(), gf(), gf()];
19927
-
19928
- crypto_hash(d, sk, 32);
19929
- d[0] &= 248;
19930
- d[31] &= 127;
19931
- d[31] |= 64;
19932
-
19933
- var smlen = n + 64;
19934
- for (i = 0; i < n; i++) sm[64 + i] = m[i];
19935
- for (i = 0; i < 32; i++) sm[32 + i] = d[32 + i];
19936
-
19937
- crypto_hash(r, sm.subarray(32), n+32);
19938
- reduce(r);
19939
- scalarbase(p, r);
19940
- pack(sm, p);
19941
-
19942
- for (i = 32; i < 64; i++) sm[i] = sk[i];
19943
- crypto_hash(h, sm, n + 64);
19944
- reduce(h);
19945
-
19946
- for (i = 0; i < 64; i++) x[i] = 0;
19947
- for (i = 0; i < 32; i++) x[i] = r[i];
19948
- for (i = 0; i < 32; i++) {
19949
- for (j = 0; j < 32; j++) {
19950
- x[i+j] += h[i] * d[j];
19951
- }
19952
- }
19953
-
19954
- modL(sm.subarray(32), x);
19955
- return smlen;
19956
- }
19957
-
19958
- function unpackneg(r, p) {
19959
- var t = gf(), chk = gf(), num = gf(),
19960
- den = gf(), den2 = gf(), den4 = gf(),
19961
- den6 = gf();
19962
-
19963
- set25519(r[2], gf1);
19964
- unpack25519(r[1], p);
19965
- S(num, r[1]);
19966
- M(den, num, D);
19967
- Z(num, num, r[2]);
19968
- A(den, r[2], den);
19969
-
19970
- S(den2, den);
19971
- S(den4, den2);
19972
- M(den6, den4, den2);
19973
- M(t, den6, num);
19974
- M(t, t, den);
19975
-
19976
- pow2523(t, t);
19977
- M(t, t, num);
19978
- M(t, t, den);
19979
- M(t, t, den);
19980
- M(r[0], t, den);
19981
-
19982
- S(chk, r[0]);
19983
- M(chk, chk, den);
19984
- if (neq25519(chk, num)) M(r[0], r[0], I);
19985
-
19986
- S(chk, r[0]);
19987
- M(chk, chk, den);
19988
- if (neq25519(chk, num)) return -1;
19989
-
19990
- if (par25519(r[0]) === (p[31]>>7)) Z(r[0], gf0, r[0]);
19991
-
19992
- M(r[3], r[0], r[1]);
19993
- return 0;
19994
- }
19995
-
19996
- function crypto_sign_open(m, sm, n, pk) {
19997
- var i;
19998
- var t = new Uint8Array(32), h = new Uint8Array(64);
19999
- var p = [gf(), gf(), gf(), gf()],
20000
- q = [gf(), gf(), gf(), gf()];
20001
-
20002
- if (n < 64) return -1;
20003
-
20004
- if (unpackneg(q, pk)) return -1;
20005
-
20006
- for (i = 0; i < n; i++) m[i] = sm[i];
20007
- for (i = 0; i < 32; i++) m[i+32] = pk[i];
20008
- crypto_hash(h, m, n);
20009
- reduce(h);
20010
- scalarmult(p, q, h);
20011
-
20012
- scalarbase(q, sm.subarray(32));
20013
- add(p, q);
20014
- pack(t, p);
20015
-
20016
- n -= 64;
20017
- if (crypto_verify_32(sm, 0, t, 0)) {
20018
- for (i = 0; i < n; i++) m[i] = 0;
20019
- return -1;
20020
- }
20021
-
20022
- for (i = 0; i < n; i++) m[i] = sm[i + 64];
20023
- return n;
20024
- }
20025
-
20026
- var crypto_secretbox_KEYBYTES = 32,
20027
- crypto_secretbox_NONCEBYTES = 24,
20028
- crypto_secretbox_ZEROBYTES = 32,
20029
- crypto_secretbox_BOXZEROBYTES = 16,
20030
- crypto_scalarmult_BYTES = 32,
20031
- crypto_scalarmult_SCALARBYTES = 32,
20032
- crypto_box_PUBLICKEYBYTES = 32,
20033
- crypto_box_SECRETKEYBYTES = 32,
20034
- crypto_box_BEFORENMBYTES = 32,
20035
- crypto_box_NONCEBYTES = crypto_secretbox_NONCEBYTES,
20036
- crypto_box_ZEROBYTES = crypto_secretbox_ZEROBYTES,
20037
- crypto_box_BOXZEROBYTES = crypto_secretbox_BOXZEROBYTES,
20038
- crypto_sign_BYTES = 64,
20039
- crypto_sign_PUBLICKEYBYTES = 32,
20040
- crypto_sign_SECRETKEYBYTES = 64,
20041
- crypto_sign_SEEDBYTES = 32,
20042
- crypto_hash_BYTES = 64;
20043
-
20044
- nacl.lowlevel = {
20045
- crypto_core_hsalsa20: crypto_core_hsalsa20,
20046
- crypto_stream_xor: crypto_stream_xor,
20047
- crypto_stream: crypto_stream,
20048
- crypto_stream_salsa20_xor: crypto_stream_salsa20_xor,
20049
- crypto_stream_salsa20: crypto_stream_salsa20,
20050
- crypto_onetimeauth: crypto_onetimeauth,
20051
- crypto_onetimeauth_verify: crypto_onetimeauth_verify,
20052
- crypto_verify_16: crypto_verify_16,
20053
- crypto_verify_32: crypto_verify_32,
20054
- crypto_secretbox: crypto_secretbox,
20055
- crypto_secretbox_open: crypto_secretbox_open,
20056
- crypto_scalarmult: crypto_scalarmult,
20057
- crypto_scalarmult_base: crypto_scalarmult_base,
20058
- crypto_box_beforenm: crypto_box_beforenm,
20059
- crypto_box_afternm: crypto_box_afternm,
20060
- crypto_box: crypto_box,
20061
- crypto_box_open: crypto_box_open,
20062
- crypto_box_keypair: crypto_box_keypair,
20063
- crypto_hash: crypto_hash,
20064
- crypto_sign: crypto_sign,
20065
- crypto_sign_keypair: crypto_sign_keypair,
20066
- crypto_sign_open: crypto_sign_open,
20067
-
20068
- crypto_secretbox_KEYBYTES: crypto_secretbox_KEYBYTES,
20069
- crypto_secretbox_NONCEBYTES: crypto_secretbox_NONCEBYTES,
20070
- crypto_secretbox_ZEROBYTES: crypto_secretbox_ZEROBYTES,
20071
- crypto_secretbox_BOXZEROBYTES: crypto_secretbox_BOXZEROBYTES,
20072
- crypto_scalarmult_BYTES: crypto_scalarmult_BYTES,
20073
- crypto_scalarmult_SCALARBYTES: crypto_scalarmult_SCALARBYTES,
20074
- crypto_box_PUBLICKEYBYTES: crypto_box_PUBLICKEYBYTES,
20075
- crypto_box_SECRETKEYBYTES: crypto_box_SECRETKEYBYTES,
20076
- crypto_box_BEFORENMBYTES: crypto_box_BEFORENMBYTES,
20077
- crypto_box_NONCEBYTES: crypto_box_NONCEBYTES,
20078
- crypto_box_ZEROBYTES: crypto_box_ZEROBYTES,
20079
- crypto_box_BOXZEROBYTES: crypto_box_BOXZEROBYTES,
20080
- crypto_sign_BYTES: crypto_sign_BYTES,
20081
- crypto_sign_PUBLICKEYBYTES: crypto_sign_PUBLICKEYBYTES,
20082
- crypto_sign_SECRETKEYBYTES: crypto_sign_SECRETKEYBYTES,
20083
- crypto_sign_SEEDBYTES: crypto_sign_SEEDBYTES,
20084
- crypto_hash_BYTES: crypto_hash_BYTES,
20085
-
20086
- gf: gf,
20087
- D: D,
20088
- L: L,
20089
- pack25519: pack25519,
20090
- unpack25519: unpack25519,
20091
- M: M,
20092
- A: A,
20093
- S: S,
20094
- Z: Z,
20095
- pow2523: pow2523,
20096
- add: add,
20097
- set25519: set25519,
20098
- modL: modL,
20099
- scalarmult: scalarmult,
20100
- scalarbase: scalarbase,
20101
- };
20102
-
20103
- /* High-level API */
20104
-
20105
- function checkLengths(k, n) {
20106
- if (k.length !== crypto_secretbox_KEYBYTES) throw new Error('bad key size');
20107
- if (n.length !== crypto_secretbox_NONCEBYTES) throw new Error('bad nonce size');
20108
- }
20109
-
20110
- function checkBoxLengths(pk, sk) {
20111
- if (pk.length !== crypto_box_PUBLICKEYBYTES) throw new Error('bad public key size');
20112
- if (sk.length !== crypto_box_SECRETKEYBYTES) throw new Error('bad secret key size');
20113
- }
20114
-
20115
- function checkArrayTypes() {
20116
- for (var i = 0; i < arguments.length; i++) {
20117
- if (!(arguments[i] instanceof Uint8Array))
20118
- throw new TypeError('unexpected type, use Uint8Array');
20119
- }
20120
- }
20121
-
20122
- function cleanup(arr) {
20123
- for (var i = 0; i < arr.length; i++) arr[i] = 0;
20124
- }
20125
-
20126
- nacl.randomBytes = function(n) {
20127
- var b = new Uint8Array(n);
20128
- randombytes(b, n);
20129
- return b;
20130
- };
20131
-
20132
- nacl.secretbox = function(msg, nonce, key) {
20133
- checkArrayTypes(msg, nonce, key);
20134
- checkLengths(key, nonce);
20135
- var m = new Uint8Array(crypto_secretbox_ZEROBYTES + msg.length);
20136
- var c = new Uint8Array(m.length);
20137
- for (var i = 0; i < msg.length; i++) m[i+crypto_secretbox_ZEROBYTES] = msg[i];
20138
- crypto_secretbox(c, m, m.length, nonce, key);
20139
- return c.subarray(crypto_secretbox_BOXZEROBYTES);
20140
- };
20141
-
20142
- nacl.secretbox.open = function(box, nonce, key) {
20143
- checkArrayTypes(box, nonce, key);
20144
- checkLengths(key, nonce);
20145
- var c = new Uint8Array(crypto_secretbox_BOXZEROBYTES + box.length);
20146
- var m = new Uint8Array(c.length);
20147
- for (var i = 0; i < box.length; i++) c[i+crypto_secretbox_BOXZEROBYTES] = box[i];
20148
- if (c.length < 32) return null;
20149
- if (crypto_secretbox_open(m, c, c.length, nonce, key) !== 0) return null;
20150
- return m.subarray(crypto_secretbox_ZEROBYTES);
20151
- };
20152
-
20153
- nacl.secretbox.keyLength = crypto_secretbox_KEYBYTES;
20154
- nacl.secretbox.nonceLength = crypto_secretbox_NONCEBYTES;
20155
- nacl.secretbox.overheadLength = crypto_secretbox_BOXZEROBYTES;
20156
-
20157
- nacl.scalarMult = function(n, p) {
20158
- checkArrayTypes(n, p);
20159
- if (n.length !== crypto_scalarmult_SCALARBYTES) throw new Error('bad n size');
20160
- if (p.length !== crypto_scalarmult_BYTES) throw new Error('bad p size');
20161
- var q = new Uint8Array(crypto_scalarmult_BYTES);
20162
- crypto_scalarmult(q, n, p);
20163
- return q;
20164
- };
20165
-
20166
- nacl.scalarMult.base = function(n) {
20167
- checkArrayTypes(n);
20168
- if (n.length !== crypto_scalarmult_SCALARBYTES) throw new Error('bad n size');
20169
- var q = new Uint8Array(crypto_scalarmult_BYTES);
20170
- crypto_scalarmult_base(q, n);
20171
- return q;
20172
- };
20173
-
20174
- nacl.scalarMult.scalarLength = crypto_scalarmult_SCALARBYTES;
20175
- nacl.scalarMult.groupElementLength = crypto_scalarmult_BYTES;
20176
-
20177
- nacl.box = function(msg, nonce, publicKey, secretKey) {
20178
- var k = nacl.box.before(publicKey, secretKey);
20179
- return nacl.secretbox(msg, nonce, k);
20180
- };
20181
-
20182
- nacl.box.before = function(publicKey, secretKey) {
20183
- checkArrayTypes(publicKey, secretKey);
20184
- checkBoxLengths(publicKey, secretKey);
20185
- var k = new Uint8Array(crypto_box_BEFORENMBYTES);
20186
- crypto_box_beforenm(k, publicKey, secretKey);
20187
- return k;
20188
- };
20189
-
20190
- nacl.box.after = nacl.secretbox;
20191
-
20192
- nacl.box.open = function(msg, nonce, publicKey, secretKey) {
20193
- var k = nacl.box.before(publicKey, secretKey);
20194
- return nacl.secretbox.open(msg, nonce, k);
20195
- };
20196
-
20197
- nacl.box.open.after = nacl.secretbox.open;
20198
-
20199
- nacl.box.keyPair = function() {
20200
- var pk = new Uint8Array(crypto_box_PUBLICKEYBYTES);
20201
- var sk = new Uint8Array(crypto_box_SECRETKEYBYTES);
20202
- crypto_box_keypair(pk, sk);
20203
- return {publicKey: pk, secretKey: sk};
20204
- };
20205
-
20206
- nacl.box.keyPair.fromSecretKey = function(secretKey) {
20207
- checkArrayTypes(secretKey);
20208
- if (secretKey.length !== crypto_box_SECRETKEYBYTES)
20209
- throw new Error('bad secret key size');
20210
- var pk = new Uint8Array(crypto_box_PUBLICKEYBYTES);
20211
- crypto_scalarmult_base(pk, secretKey);
20212
- return {publicKey: pk, secretKey: new Uint8Array(secretKey)};
20213
- };
20214
-
20215
- nacl.box.publicKeyLength = crypto_box_PUBLICKEYBYTES;
20216
- nacl.box.secretKeyLength = crypto_box_SECRETKEYBYTES;
20217
- nacl.box.sharedKeyLength = crypto_box_BEFORENMBYTES;
20218
- nacl.box.nonceLength = crypto_box_NONCEBYTES;
20219
- nacl.box.overheadLength = nacl.secretbox.overheadLength;
20220
-
20221
- nacl.sign = function(msg, secretKey) {
20222
- checkArrayTypes(msg, secretKey);
20223
- if (secretKey.length !== crypto_sign_SECRETKEYBYTES)
20224
- throw new Error('bad secret key size');
20225
- var signedMsg = new Uint8Array(crypto_sign_BYTES+msg.length);
20226
- crypto_sign(signedMsg, msg, msg.length, secretKey);
20227
- return signedMsg;
20228
- };
20229
-
20230
- nacl.sign.open = function(signedMsg, publicKey) {
20231
- checkArrayTypes(signedMsg, publicKey);
20232
- if (publicKey.length !== crypto_sign_PUBLICKEYBYTES)
20233
- throw new Error('bad public key size');
20234
- var tmp = new Uint8Array(signedMsg.length);
20235
- var mlen = crypto_sign_open(tmp, signedMsg, signedMsg.length, publicKey);
20236
- if (mlen < 0) return null;
20237
- var m = new Uint8Array(mlen);
20238
- for (var i = 0; i < m.length; i++) m[i] = tmp[i];
20239
- return m;
20240
- };
20241
-
20242
- nacl.sign.detached = function(msg, secretKey) {
20243
- var signedMsg = nacl.sign(msg, secretKey);
20244
- var sig = new Uint8Array(crypto_sign_BYTES);
20245
- for (var i = 0; i < sig.length; i++) sig[i] = signedMsg[i];
20246
- return sig;
20247
- };
20248
-
20249
- nacl.sign.detached.verify = function(msg, sig, publicKey) {
20250
- checkArrayTypes(msg, sig, publicKey);
20251
- if (sig.length !== crypto_sign_BYTES)
20252
- throw new Error('bad signature size');
20253
- if (publicKey.length !== crypto_sign_PUBLICKEYBYTES)
20254
- throw new Error('bad public key size');
20255
- var sm = new Uint8Array(crypto_sign_BYTES + msg.length);
20256
- var m = new Uint8Array(crypto_sign_BYTES + msg.length);
20257
- var i;
20258
- for (i = 0; i < crypto_sign_BYTES; i++) sm[i] = sig[i];
20259
- for (i = 0; i < msg.length; i++) sm[i+crypto_sign_BYTES] = msg[i];
20260
- return (crypto_sign_open(m, sm, sm.length, publicKey) >= 0);
20261
- };
20262
-
20263
- nacl.sign.keyPair = function() {
20264
- var pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES);
20265
- var sk = new Uint8Array(crypto_sign_SECRETKEYBYTES);
20266
- crypto_sign_keypair(pk, sk);
20267
- return {publicKey: pk, secretKey: sk};
20268
- };
20269
-
20270
- nacl.sign.keyPair.fromSecretKey = function(secretKey) {
20271
- checkArrayTypes(secretKey);
20272
- if (secretKey.length !== crypto_sign_SECRETKEYBYTES)
20273
- throw new Error('bad secret key size');
20274
- var pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES);
20275
- for (var i = 0; i < pk.length; i++) pk[i] = secretKey[32+i];
20276
- return {publicKey: pk, secretKey: new Uint8Array(secretKey)};
20277
- };
20278
-
20279
- nacl.sign.keyPair.fromSeed = function(seed) {
20280
- checkArrayTypes(seed);
20281
- if (seed.length !== crypto_sign_SEEDBYTES)
20282
- throw new Error('bad seed size');
20283
- var pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES);
20284
- var sk = new Uint8Array(crypto_sign_SECRETKEYBYTES);
20285
- for (var i = 0; i < 32; i++) sk[i] = seed[i];
20286
- crypto_sign_keypair(pk, sk, true);
20287
- return {publicKey: pk, secretKey: sk};
20288
- };
20289
-
20290
- nacl.sign.publicKeyLength = crypto_sign_PUBLICKEYBYTES;
20291
- nacl.sign.secretKeyLength = crypto_sign_SECRETKEYBYTES;
20292
- nacl.sign.seedLength = crypto_sign_SEEDBYTES;
20293
- nacl.sign.signatureLength = crypto_sign_BYTES;
20294
-
20295
- nacl.hash = function(msg) {
20296
- checkArrayTypes(msg);
20297
- var h = new Uint8Array(crypto_hash_BYTES);
20298
- crypto_hash(h, msg, msg.length);
20299
- return h;
20300
- };
20301
-
20302
- nacl.hash.hashLength = crypto_hash_BYTES;
20303
-
20304
- nacl.verify = function(x, y) {
20305
- checkArrayTypes(x, y);
20306
- // Zero length arguments are considered not equal.
20307
- if (x.length === 0 || y.length === 0) return false;
20308
- if (x.length !== y.length) return false;
20309
- return (vn(x, 0, y, 0, x.length) === 0) ? true : false;
20310
- };
20311
-
20312
- nacl.setPRNG = function(fn) {
20313
- randombytes = fn;
20314
- };
20315
-
20316
- (function() {
20317
- // Initialize PRNG if environment provides CSPRNG.
20318
- // If not, methods calling randombytes will throw.
20319
- var crypto = typeof self !== 'undefined' ? (self.crypto || self.msCrypto) : null;
20320
- if (crypto && crypto.getRandomValues) {
20321
- // Browsers.
20322
- var QUOTA = 65536;
20323
- nacl.setPRNG(function(x, n) {
20324
- var i, v = new Uint8Array(n);
20325
- for (i = 0; i < n; i += QUOTA) {
20326
- crypto.getRandomValues(v.subarray(i, i + Math.min(n - i, QUOTA)));
20327
- }
20328
- for (i = 0; i < n; i++) x[i] = v[i];
20329
- cleanup(v);
20330
- });
20331
- } else if (typeof commonjsRequire !== 'undefined') {
20332
- // Node.js.
20333
- crypto = require$$0;
20334
- if (crypto && crypto.randomBytes) {
20335
- nacl.setPRNG(function(x, n) {
20336
- var i, v = crypto.randomBytes(n);
20337
- for (i = 0; i < n; i++) x[i] = v[i];
20338
- cleanup(v);
20339
- });
20340
- }
20341
- }
20342
- })();
20343
-
20344
- })(module.exports ? module.exports : (self.nacl = self.nacl || {}));
20345
- } (naclFast));
20346
- return naclFast.exports;
20347
- }
20348
-
20349
- var naclFastExports = requireNaclFast();
20350
- var nacl = /*@__PURE__*/getDefaultExportFromCjs$1(naclFastExports);
20351
-
20352
- // guest-keystore.ts — durable per-device keypair for anonymous ("guest") auth.
20353
- //
20354
- // A guest is an ed25519 keypair generated on the device. Its public key is the
20355
- // Bounded account address; it signs the auth challenge exactly like a wallet.
20356
- //
20357
- // Two backends, chosen automatically:
20358
- // 1. WebCrypto Ed25519 + IndexedDB (browsers that support it) — the key is
20359
- // NON-EXTRACTABLE: an XSS can ask it to *sign* but can never read the
20360
- // private bytes. This is the secure default.
20361
- // 2. tweetnacl keypair in platform storage (React Native / older browsers) —
20362
- // the secret is stored as bytes. Simpler, but exfiltratable by XSS, so
20363
- // treat it as a low-stakes guest identity and tell users to upgrade
20364
- // (link an email) before they care about not losing it.
20365
- //
20366
- // Either way the public output is identical: a base58 address + a base64
20367
- // detached signature, which is the format the session endpoint already accepts.
20368
- const FALLBACK_STORE_KEY = 'bounded_guest_key_v1';
20369
- const IDB_NAME = 'bounded-guest';
20370
- const IDB_STORE = 'keys';
20371
- const IDB_KEY = 'ed25519';
20372
- function toBase64(bytes) {
20373
- let bin = '';
20374
- for (let i = 0; i < bytes.length; i++)
20375
- bin += String.fromCharCode(bytes[i]);
20376
- // btoa exists in browsers + RN (Hermes) ; Buffer is the Node/SSR fallback.
20377
- return typeof btoa !== 'undefined' ? btoa(bin) : Buffer.from(bytes).toString('base64');
20378
- }
20379
- // ----- WebCrypto (non-extractable) backend -----------------------------------
20380
- function subtle() {
20381
- const c = globalThis === null || globalThis === void 0 ? void 0 : globalThis.crypto;
20382
- return c && c.subtle ? c.subtle : null;
20383
- }
20384
- function idb() {
20385
- return new Promise((resolve, reject) => {
20386
- const req = globalThis.indexedDB.open(IDB_NAME, 1);
20387
- req.onupgradeneeded = () => req.result.createObjectStore(IDB_STORE);
20388
- req.onsuccess = () => resolve(req.result);
20389
- req.onerror = () => reject(req.error);
20390
- });
20391
- }
20392
- function idbGet(db, key) {
20393
- return new Promise((resolve, reject) => {
20394
- const r = db.transaction(IDB_STORE, 'readonly').objectStore(IDB_STORE).get(key);
20395
- r.onsuccess = () => resolve(r.result);
20396
- r.onerror = () => reject(r.error);
20397
- });
20398
- }
20399
- function idbPut(db, key, val) {
20400
- return new Promise((resolve, reject) => {
20401
- const r = db.transaction(IDB_STORE, 'readwrite').objectStore(IDB_STORE).put(val, key);
20402
- r.onsuccess = () => resolve();
20403
- r.onerror = () => reject(r.error);
20404
- });
20405
- }
20406
- async function tryWebCrypto() {
20407
- const s = subtle();
20408
- if (!s || typeof globalThis.indexedDB === 'undefined')
20409
- return null;
20410
- try {
20411
- const db = await idb();
20412
- let pair = (await idbGet(db, IDB_KEY));
20413
- if (!pair) {
20414
- // Probe Ed25519 support by generating (throws on unsupported engines).
20415
- pair = (await s.generateKey({ name: 'Ed25519' }, /* extractable */ false, [
20416
- 'sign',
20417
- 'verify',
20418
- ]));
20419
- await idbPut(db, IDB_KEY, pair); // CryptoKey is structured-cloneable; bytes never exposed
20420
- }
20421
- const raw = new Uint8Array(await s.exportKey('raw', pair.publicKey)); // public is exportable
20422
- const address = bs58.encode(raw);
20423
- return {
20424
- address,
20425
- nonExtractable: true,
20426
- async sign(message) {
20427
- const data = new TextEncoder().encode(message);
20428
- const sig = new Uint8Array(await s.sign({ name: 'Ed25519' }, pair.privateKey, data));
20429
- return toBase64(sig);
20430
- },
20431
- };
20432
- }
20433
- catch (_a) {
20434
- return null; // engine lacks Ed25519 / IndexedDB blocked → fall back
20435
- }
20436
- }
20437
- // ----- tweetnacl (stored-secret) fallback ------------------------------------
20438
- function fallbackKey() {
20439
- const store = getPlatform().storage;
20440
- let secret;
20441
- const saved = store.getItem(FALLBACK_STORE_KEY);
20442
- if (saved) {
20443
- secret = bs58.decode(saved);
20444
- }
20445
- else {
20446
- const kp = Keypair.generate();
20447
- secret = kp.secretKey; // 64 bytes
20448
- store.setItem(FALLBACK_STORE_KEY, bs58.encode(secret));
20449
- }
20450
- const kp = Keypair.fromSecretKey(secret);
20451
- const address = kp.publicKey.toBase58();
20452
- return {
20453
- address,
20454
- nonExtractable: false,
20455
- async sign(message) {
20456
- const data = new TextEncoder().encode(message);
20457
- const sig = nacl.sign.detached(data, secret);
20458
- return toBase64(sig);
20459
- },
20460
- };
20461
- }
20462
- let cached = null;
20463
- /** Get (or create) this device's durable guest key. */
20464
- async function getGuestKey() {
20465
- var _a;
20466
- if (cached)
20467
- return cached;
20468
- cached = (_a = (await tryWebCrypto())) !== null && _a !== void 0 ? _a : fallbackKey();
20469
- return cached;
20470
- }
20471
- /** Forget the guest key on this device (a fresh signInAnonymously mints a new one). */
20472
- async function clearGuestKey() {
20473
- cached = null;
20474
- try {
20475
- getPlatform().storage.removeItem(FALLBACK_STORE_KEY);
20476
- }
20477
- catch (_a) { }
20478
- try {
20479
- if (typeof globalThis.indexedDB !== 'undefined') {
20480
- const db = await idb();
20481
- await new Promise((res) => {
20482
- const r = db.transaction(IDB_STORE, 'readwrite').objectStore(IDB_STORE).delete(IDB_KEY);
20483
- r.onsuccess = () => res();
20484
- r.onerror = () => res();
20485
- });
20486
- }
20487
- }
20488
- catch (_b) { }
20489
- }
18122
+ // guest-keystore.ts — durable per-device keypair for anonymous ("guest") auth.
18123
+ //
18124
+ // A guest is an ed25519 keypair generated on the device. Its public key is the
18125
+ // Bounded account address; it signs the auth challenge exactly like a wallet.
18126
+ //
18127
+ // Two backends, chosen automatically:
18128
+ // 1. WebCrypto Ed25519 + IndexedDB (browsers that support it) — the key is
18129
+ // NON-EXTRACTABLE: an XSS can ask it to *sign* but can never read the
18130
+ // private bytes. This is the secure default.
18131
+ // 2. tweetnacl keypair in platform storage (React Native / older browsers) —
18132
+ // the secret is stored as bytes. Simpler, but exfiltratable by XSS, so
18133
+ // treat it as a low-stakes guest identity and tell users to upgrade
18134
+ // (link an email) before they care about not losing it.
18135
+ //
18136
+ // Either way the public output is identical: a base58 address + a base64
18137
+ // detached signature, which is the format the session endpoint already accepts.
18138
+ const FALLBACK_STORE_KEY = 'bounded_guest_key_v1';
18139
+ const IDB_NAME = 'bounded-guest';
18140
+ const IDB_STORE = 'keys';
18141
+ const IDB_KEY = 'ed25519';
18142
+ function toBase64(bytes) {
18143
+ let bin = '';
18144
+ for (let i = 0; i < bytes.length; i++)
18145
+ bin += String.fromCharCode(bytes[i]);
18146
+ // btoa exists in browsers + RN (Hermes) ; Buffer is the Node/SSR fallback.
18147
+ return typeof btoa !== 'undefined' ? btoa(bin) : Buffer.from(bytes).toString('base64');
18148
+ }
18149
+ // ----- WebCrypto (non-extractable) backend -----------------------------------
18150
+ function subtle() {
18151
+ const c = globalThis === null || globalThis === void 0 ? void 0 : globalThis.crypto;
18152
+ return c && c.subtle ? c.subtle : null;
18153
+ }
18154
+ function idb() {
18155
+ return new Promise((resolve, reject) => {
18156
+ const req = globalThis.indexedDB.open(IDB_NAME, 1);
18157
+ req.onupgradeneeded = () => req.result.createObjectStore(IDB_STORE);
18158
+ req.onsuccess = () => resolve(req.result);
18159
+ req.onerror = () => reject(req.error);
18160
+ });
18161
+ }
18162
+ function idbGet(db, key) {
18163
+ return new Promise((resolve, reject) => {
18164
+ const r = db.transaction(IDB_STORE, 'readonly').objectStore(IDB_STORE).get(key);
18165
+ r.onsuccess = () => resolve(r.result);
18166
+ r.onerror = () => reject(r.error);
18167
+ });
18168
+ }
18169
+ function idbPut(db, key, val) {
18170
+ return new Promise((resolve, reject) => {
18171
+ const r = db.transaction(IDB_STORE, 'readwrite').objectStore(IDB_STORE).put(val, key);
18172
+ r.onsuccess = () => resolve();
18173
+ r.onerror = () => reject(r.error);
18174
+ });
18175
+ }
18176
+ async function tryWebCrypto() {
18177
+ const s = subtle();
18178
+ if (!s || typeof globalThis.indexedDB === 'undefined')
18179
+ return null;
18180
+ try {
18181
+ const db = await idb();
18182
+ let pair = (await idbGet(db, IDB_KEY));
18183
+ if (!pair) {
18184
+ // Probe Ed25519 support by generating (throws on unsupported engines).
18185
+ pair = (await s.generateKey({ name: 'Ed25519' }, /* extractable */ false, [
18186
+ 'sign',
18187
+ 'verify',
18188
+ ]));
18189
+ await idbPut(db, IDB_KEY, pair); // CryptoKey is structured-cloneable; bytes never exposed
18190
+ }
18191
+ const raw = new Uint8Array(await s.exportKey('raw', pair.publicKey)); // public is exportable
18192
+ const address = bs58.encode(raw);
18193
+ return {
18194
+ address,
18195
+ nonExtractable: true,
18196
+ async sign(message) {
18197
+ const data = new TextEncoder().encode(message);
18198
+ const sig = new Uint8Array(await s.sign({ name: 'Ed25519' }, pair.privateKey, data));
18199
+ return toBase64(sig);
18200
+ },
18201
+ };
18202
+ }
18203
+ catch (_a) {
18204
+ return null; // engine lacks Ed25519 / IndexedDB blocked → fall back
18205
+ }
18206
+ }
18207
+ // ----- tweetnacl (stored-secret) fallback ------------------------------------
18208
+ function fallbackKey() {
18209
+ const store = getPlatform().storage;
18210
+ let secret;
18211
+ const saved = store.getItem(FALLBACK_STORE_KEY);
18212
+ if (saved) {
18213
+ secret = bs58.decode(saved);
18214
+ }
18215
+ else {
18216
+ const kp = Keypair.generate();
18217
+ secret = kp.secretKey; // 64 bytes
18218
+ store.setItem(FALLBACK_STORE_KEY, bs58.encode(secret));
18219
+ }
18220
+ const kp = Keypair.fromSecretKey(secret);
18221
+ const address = kp.publicKey.toBase58();
18222
+ return {
18223
+ address,
18224
+ nonExtractable: false,
18225
+ async sign(message) {
18226
+ const data = new TextEncoder().encode(message);
18227
+ const sig = nacl.sign.detached(data, secret);
18228
+ return toBase64(sig);
18229
+ },
18230
+ };
18231
+ }
18232
+ let cached = null;
18233
+ /** Get (or create) this device's durable guest key. */
18234
+ async function getGuestKey() {
18235
+ var _a;
18236
+ if (cached)
18237
+ return cached;
18238
+ cached = (_a = (await tryWebCrypto())) !== null && _a !== void 0 ? _a : fallbackKey();
18239
+ return cached;
18240
+ }
18241
+ /** Forget the guest key on this device (a fresh signInAnonymously mints a new one). */
18242
+ async function clearGuestKey() {
18243
+ cached = null;
18244
+ try {
18245
+ getPlatform().storage.removeItem(FALLBACK_STORE_KEY);
18246
+ }
18247
+ catch (_a) { }
18248
+ try {
18249
+ if (typeof globalThis.indexedDB !== 'undefined') {
18250
+ const db = await idb();
18251
+ await new Promise((res) => {
18252
+ const r = db.transaction(IDB_STORE, 'readwrite').objectStore(IDB_STORE).delete(IDB_KEY);
18253
+ r.onsuccess = () => res();
18254
+ r.onerror = () => res();
18255
+ });
18256
+ }
18257
+ }
18258
+ catch (_b) { }
18259
+ }
20490
18260
 
20491
18261
  const GUEST_TX_UNSUPPORTED = 'Guest (anonymous) auth is offchain-only — it cannot sign or submit Solana transactions. ' +
20492
18262
  'Use a wallet provider for onchain actions.';
@@ -20625,6 +18395,17 @@ async function post(path, body) {
20625
18395
  // ---------------------------------------------------------------------------
20626
18396
  // headless primitives (used by the modal AND exported for custom UIs)
20627
18397
  // ---------------------------------------------------------------------------
18398
+ function normalizePhoneNumber(value) {
18399
+ const compact = String(value || '').trim().replace(/[()\s.-]/g, '');
18400
+ return compact.startsWith('00') ? `+${compact.slice(2)}` : compact;
18401
+ }
18402
+ function assertPhoneNumber(value) {
18403
+ const phoneNumber = normalizePhoneNumber(value);
18404
+ if (!/^\+[1-9]\d{7,14}$/.test(phoneNumber)) {
18405
+ throw new Error('phoneNumber must be in E.164 format, e.g. +14155550132');
18406
+ }
18407
+ return phoneNumber;
18408
+ }
20628
18409
  /** Send a login code to `email`. Step 1 of the inline email-OTP flow. */
20629
18410
  async function sendEmailOtp$1(email) {
20630
18411
  const cfg = await getConfig();
@@ -20634,6 +18415,13 @@ async function sendEmailOtp$1(email) {
20634
18415
  const data = await post('/email', { email: e, appId: cfg.appId });
20635
18416
  return { ok: true, expiresInSeconds: data === null || data === void 0 ? void 0 : data.expiresInSeconds };
20636
18417
  }
18418
+ /** Send a login code by text message. Requires the Bounded issuer to have text OTP explicitly enabled and SMS configured. */
18419
+ async function sendTextOtp$1(phoneNumber) {
18420
+ const cfg = await getConfig();
18421
+ const phone = assertPhoneNumber(phoneNumber);
18422
+ const data = await post('/text', { phoneNumber: phone, appId: cfg.appId });
18423
+ return { ok: true, expiresInSeconds: data === null || data === void 0 ? void 0 : data.expiresInSeconds };
18424
+ }
20637
18425
  /**
20638
18426
  * Verify the code for `email` and establish the session. Step 2 of the flow.
20639
18427
  * Stores the Bounded id token (the realtime bearer) and sets the current user.
@@ -20667,6 +18455,33 @@ async function verifyEmailOtp$1(email, otp) {
20667
18455
  setAuthLoading(false);
20668
18456
  }
20669
18457
  }
18458
+ /**
18459
+ * Verify the text-message code for `phoneNumber` and establish the session.
18460
+ * Phone-only users have `user.email === null`; use `user.id` for ownership.
18461
+ */
18462
+ async function verifyTextOtp$1(phoneNumber, otp) {
18463
+ setAuthLoading(true);
18464
+ try {
18465
+ const cfg = await getConfig();
18466
+ const phone = assertPhoneNumber(phoneNumber);
18467
+ const code = (otp || '').trim();
18468
+ if (!code)
18469
+ throw new Error('code is required');
18470
+ const data = await post('/text/verify', { phoneNumber: phone, appId: cfg.appId, otp: code });
18471
+ const idToken = data === null || data === void 0 ? void 0 : data.idToken;
18472
+ if (!idToken)
18473
+ throw new Error('No token returned from verification.');
18474
+ const user = userFromIdToken$1(idToken);
18475
+ const refreshToken = typeof (data === null || data === void 0 ? void 0 : data.refreshToken) === 'string' ? data.refreshToken : '';
18476
+ const issuer = (cfg.humanAuthApiUrl || '').replace(/\/$/, '');
18477
+ await sessionManager$1().storeSession(String(user.address || ''), idToken, idToken, refreshToken, issuer);
18478
+ setCurrentUser(user);
18479
+ return user;
18480
+ }
18481
+ finally {
18482
+ setAuthLoading(false);
18483
+ }
18484
+ }
20670
18485
  /**
20671
18486
  * UPGRADE an anonymous (guest) session by LINKING an email credential — Firebase
20672
18487
  * `linkWithCredential` parity. The guest's `guestToken` is sent to the auth issuer
@@ -20724,8 +18539,8 @@ class EmailAuthProvider {
20724
18539
  async login() {
20725
18540
  if (!getPlatform().hasDOM) {
20726
18541
  throw new Error('Email login on this platform (e.g. React Native) is headless: call ' +
20727
- "sendEmailOtp(email) then verifyEmailOtp(email, code) and render your own " +
20728
- 'email + code inputs. The built-in modal is web-only.');
18542
+ "sendEmailOtp(email) then verifyEmailOtp(email, code), or, when text OTP is enabled, " +
18543
+ "sendTextOtp(phone) then verifyTextOtp(phone, code). Render your own OTP inputs. The built-in modal is web-only.");
20729
18544
  }
20730
18545
  return openEmailModal();
20731
18546
  }
@@ -20942,6 +18757,11 @@ function userFromIdToken(idToken) {
20942
18757
  email: (c.email || null), provider: oidcProviderSingleton,
20943
18758
  };
20944
18759
  }
18760
+ function tokenField(data, snake, camel) {
18761
+ var _a;
18762
+ const value = (_a = data === null || data === void 0 ? void 0 : data[snake]) !== null && _a !== void 0 ? _a : data === null || data === void 0 ? void 0 : data[camel];
18763
+ return typeof value === 'string' ? value : '';
18764
+ }
20945
18765
  async function issuerBase() {
20946
18766
  const cfg = await getConfig();
20947
18767
  return (cfg.humanAuthApiUrl || 'https://auth.bounded.sh').replace(/\/$/, '');
@@ -20965,10 +18785,26 @@ function randomState() {
20965
18785
  crypto.getRandomValues(rnd);
20966
18786
  return b64url(rnd.buffer);
20967
18787
  }
20968
- async function buildAuthorizeUrl(redirectUri) {
18788
+ function normalizeMethod(value) {
18789
+ const normalized = String(value || '').trim().toLowerCase();
18790
+ return /^[a-z0-9_-]{1,40}$/.test(normalized) ? normalized : null;
18791
+ }
18792
+ function appendHostedLoginHints(params, opts) {
18793
+ const methods = (opts.methods || [])
18794
+ .map((method) => normalizeMethod(method))
18795
+ .filter((method) => !!method);
18796
+ if (methods.length > 0) {
18797
+ params.set('bounded_methods', Array.from(new Set(methods)).join(','));
18798
+ }
18799
+ const provider = normalizeMethod(opts.provider);
18800
+ if (provider)
18801
+ params.set('bounded_provider', provider);
18802
+ }
18803
+ async function buildAuthorizeUrl(opts) {
20969
18804
  const cfg = await getConfig();
20970
18805
  if (!cfg.appId)
20971
18806
  throw new Error('appId is not configured (init the SDK with your appId)');
18807
+ const redirectUri = opts.redirectUri;
20972
18808
  const { verifier, challenge } = await makePkce();
20973
18809
  const state = randomState();
20974
18810
  // Stash the verifier + state so the redirect_uri page can finish the exchange. sessionStorage
@@ -20979,6 +18815,7 @@ async function buildAuthorizeUrl(redirectUri) {
20979
18815
  client_id: cfg.appId, redirect_uri: redirectUri, response_type: 'code', scope: 'openid profile email',
20980
18816
  code_challenge: challenge, code_challenge_method: 'S256', state,
20981
18817
  });
18818
+ appendHostedLoginHints(params, opts);
20982
18819
  return `${base}/api/auth/oauth2/authorize?${params.toString()}`;
20983
18820
  }
20984
18821
  /** Start login: navigate the whole page to the hosted Bounded login. Returns after redirect
@@ -20986,7 +18823,7 @@ async function buildAuthorizeUrl(redirectUri) {
20986
18823
  async function loginWithRedirect(opts) {
20987
18824
  if (!hasDOM())
20988
18825
  throw new Error('loginWithRedirect is web-only');
20989
- const url = await buildAuthorizeUrl(opts.redirectUri);
18826
+ const url = await buildAuthorizeUrl(opts);
20990
18827
  window.location.href = url;
20991
18828
  }
20992
18829
  const LINK_GUEST_KEY = 'bounded.link.guest';
@@ -21002,7 +18839,7 @@ async function linkWithRedirect(opts) {
21002
18839
  if (!guestToken)
21003
18840
  throw new Error('linkWithRedirect requires an active anonymous session — call signInAnonymously() first.');
21004
18841
  sessionStorage.setItem(LINK_GUEST_KEY, guestToken);
21005
- const url = await buildAuthorizeUrl(opts.redirectUri);
18842
+ const url = await buildAuthorizeUrl(opts);
21006
18843
  window.location.href = url;
21007
18844
  }
21008
18845
  // If a guest-link is pending (linkWithRedirect stashed a guest token before this
@@ -21024,10 +18861,13 @@ async function finishGuestLinkIfPending(socialIdToken) {
21024
18861
  body: JSON.stringify({ appId: cfg.appId, guestToken, socialToken: socialIdToken }),
21025
18862
  });
21026
18863
  const data = await res.json().catch(() => null);
21027
- if (!res.ok || !(data === null || data === void 0 ? void 0 : data.idToken))
18864
+ const idToken = tokenField(data, 'id_token', 'idToken');
18865
+ if (!res.ok || !idToken)
21028
18866
  return null; // link failed -> keep the plain social session
21029
- const upgraded = userFromIdToken(data.idToken);
21030
- await sessionManager().storeSession(String(upgraded.address || ''), data.idToken, data.idToken, '');
18867
+ const upgraded = userFromIdToken(idToken);
18868
+ const accessToken = tokenField(data, 'access_token', 'accessToken') || idToken;
18869
+ const refreshToken = tokenField(data, 'refresh_token', 'refreshToken');
18870
+ await sessionManager().storeSession(String(upgraded.address || ''), accessToken, idToken, refreshToken, base);
21031
18871
  setCurrentUser(upgraded);
21032
18872
  return upgraded;
21033
18873
  }
@@ -21074,7 +18914,7 @@ async function completeLoginFromRedirect() {
21074
18914
  async function loginWithPopup(opts) {
21075
18915
  if (!hasDOM())
21076
18916
  throw new Error('loginWithPopup is web-only');
21077
- const url = await buildAuthorizeUrl(opts.redirectUri);
18917
+ const url = await buildAuthorizeUrl(opts);
21078
18918
  const w = opts.width || 480, h = opts.height || 720;
21079
18919
  const left = window.screenX + (window.outerWidth - w) / 2;
21080
18920
  const top = window.screenY + (window.outerHeight - h) / 2;
@@ -21166,8 +19006,10 @@ async function exchangeCode(code, verifier, redirectUri) {
21166
19006
  const idToken = (data === null || data === void 0 ? void 0 : data.id_token) || (data === null || data === void 0 ? void 0 : data.idToken);
21167
19007
  if (!idToken)
21168
19008
  throw new Error('No id_token returned from the token endpoint');
19009
+ const accessToken = tokenField(data, 'access_token', 'accessToken') || idToken;
19010
+ const refreshToken = tokenField(data, 'refresh_token', 'refreshToken');
21169
19011
  const user = userFromIdToken(idToken);
21170
- await sessionManager().storeSession(String(user.address || ''), idToken, idToken, '');
19012
+ await sessionManager().storeSession(String(user.address || ''), accessToken, idToken, refreshToken, base);
21171
19013
  setCurrentUser(user);
21172
19014
  setAuthProviderInstance(oidcProviderSingleton);
21173
19015
  return user;
@@ -21473,6 +19315,15 @@ async function sendEmailOtp(email) {
21473
19315
  assertBrowserAuthEnv('sendEmailOtp');
21474
19316
  return sendEmailOtp$1(email);
21475
19317
  }
19318
+ /**
19319
+ * Send a 6-digit login code by text message. Requires the configured Bounded
19320
+ * auth issuer to have text OTP explicitly enabled and SMS delivery configured.
19321
+ * Works on web AND React Native.
19322
+ */
19323
+ async function sendTextOtp(phoneNumber) {
19324
+ assertBrowserAuthEnv('sendTextOtp');
19325
+ return sendTextOtp$1(phoneNumber);
19326
+ }
21476
19327
  /**
21477
19328
  * Verify the `code` for `email`, establish the session, and return the user.
21478
19329
  * Step 2 of the inline email flow. Coexists with signInAnonymously()/wallets.
@@ -21482,6 +19333,15 @@ async function verifyEmailOtp(email, code) {
21482
19333
  await activateEmailProvider();
21483
19334
  return verifyEmailOtp$1(email, code);
21484
19335
  }
19336
+ /**
19337
+ * Verify the text-message `code`, establish the session, and return the user.
19338
+ * Phone-only users have no `user.email`; use `user.id` for app identity.
19339
+ */
19340
+ async function verifyTextOtp(phoneNumber, code) {
19341
+ assertBrowserAuthEnv('verifyTextOtp');
19342
+ await activateEmailProvider();
19343
+ return verifyTextOtp$1(phoneNumber, code);
19344
+ }
21485
19345
  /**
21486
19346
  * UPGRADE the current anonymous (guest) session by linking an email — Firebase
21487
19347
  * `linkWithCredential` parity. Send the code first with `sendEmailOtp(email)`,
@@ -21699,6 +19559,12 @@ async function logout() {
21699
19559
  setCurrentUser(null);
21700
19560
  }
21701
19561
  var hasRunFirstTime = false;
19562
+ function userPrincipalKey(user) {
19563
+ var _a, _b, _c;
19564
+ if (!user)
19565
+ return null;
19566
+ return (_c = (_b = (_a = user.id) !== null && _a !== void 0 ? _a : user.address) !== null && _b !== void 0 ? _b : user.email) !== null && _c !== void 0 ? _c : null;
19567
+ }
21702
19568
  // Update the currentUser and notify listeners
21703
19569
  function setCurrentUser(user) {
21704
19570
  // Enrich with the universal identity fields (id/email) derived from the
@@ -21708,7 +19574,9 @@ function setCurrentUser(user) {
21708
19574
  user = withUserIdentity(user);
21709
19575
  const lastUser = currentUser;
21710
19576
  const wasInitializedBefore = hasRunFirstTime;
21711
- const shouldRunListeners = !hasRunFirstTime || (currentUser === null || currentUser === void 0 ? void 0 : currentUser.address) !== (user === null || user === void 0 ? void 0 : user.address);
19577
+ const lastPrincipal = userPrincipalKey(currentUser);
19578
+ const nextPrincipal = userPrincipalKey(user);
19579
+ const shouldRunListeners = !hasRunFirstTime || lastPrincipal !== nextPrincipal;
21712
19580
  hasRunFirstTime = true;
21713
19581
  currentUser = user;
21714
19582
  if (shouldRunListeners) {
@@ -21716,7 +19584,7 @@ function setCurrentUser(user) {
21716
19584
  // Reconnect WebSocket connections with new auth when user changes.
21717
19585
  // Skip on the first setCurrentUser call during init() — there are no
21718
19586
  // existing connections to reconnect at that point.
21719
- if (wasInitializedBefore && (lastUser === null || lastUser === void 0 ? void 0 : lastUser.address) !== (user === null || user === void 0 ? void 0 : user.address)) {
19587
+ if (wasInitializedBefore && userPrincipalKey(lastUser) !== nextPrincipal) {
21720
19588
  reconnectWithNewAuth().catch((error) => {
21721
19589
  console.error('Error reconnecting WebSocket after auth change:', error);
21722
19590
  });
@@ -26187,5 +24055,5 @@ class PrivyExpoProvider {
26187
24055
  }
26188
24056
  }
26189
24057
 
26190
- export { DEFAULT_TEST_ADDRESS, EFFECT_INTENT_ADDRESS, FunctionInvokeError, InsufficientBalanceError, LiveIntentError, MockAuthProvider, OffchainAuthProvider, PhantomWalletProvider, PrivyExpoProvider, PrivyWalletProvider, ReactNativeSessionManager, RealtimeStore, ServerSessionManager, SolanaMobileWalletProvider, WebSessionManager, aggregate, buildSetDocumentsTransaction, clearCache, closeAllSubscriptions, completeLoginFromRedirect, completeLoginInPopup, convertRemainingAccounts, count, createSessionWithSignature, defineLiveModule, deriveUserIdentityFromIdToken, deserializeTransaction, docId, functions, genAuthNonce, genSolanaMessage, get, getActiveSessionManager, getAuthLoading, getAuthProvider, getCachedData, getConfig, getCurrentUser, getFiles, getIdToken, getMany, getPlatform, getRealtimeStore, getWebhookKeysUrl, hasActiveConnection, increment, init, invoke as invokeFunction, isEffectResult, isMobileWalletAvailable, linkEmail, linkWithRedirect, live, intent as liveIntent, status as liveStatus, login, loginWithPopup, loginWithPrivy, loginWithRedirect, logout, now, onAuthLoadingChanged, onAuthStateChanged, queryAggregate, reconnectWithNewAuth, refreshSession, registerMobileWalletAdapter, resetRealtimeStore, revokeSession, runExpression, runExpressionMany, runQuery, runQueryMany, search, sendEmailOtp, serverTimestamp, set, setFile, setMany, setPlatform, signAndSubmitTransaction, signInAnonymously, signMessage, signSessionCreateMessage, signTransaction, subscribe, subscribeView as subscribeLiveView, toMillis, toSeconds, useAuth, useQuery, verifyEmailOtp, withEffects, wsDelete, wsGet, wsGetMany, wsQuery, wsSet };
24058
+ export { DEFAULT_TEST_ADDRESS, EFFECT_INTENT_ADDRESS, FunctionInvokeError, InsufficientBalanceError, LiveIntentError, MockAuthProvider, OffchainAuthProvider, PhantomWalletProvider, PrivyExpoProvider, PrivyWalletProvider, ReactNativeSessionManager, RealtimeStore, ServerSessionManager, SolanaMobileWalletProvider, WebSessionManager, aggregate, buildSetDocumentsTransaction, clearCache, closeAllSubscriptions, completeLoginFromRedirect, completeLoginInPopup, convertRemainingAccounts, count, createSessionWithSignature, defineLiveModule, deriveUserIdentityFromIdToken, deserializeTransaction, docId, functions, genAuthNonce, genSolanaMessage, get, getActiveSessionManager, getAuthLoading, getAuthProvider, getCachedData, getConfig, getCurrentUser, getFiles, getIdToken, getMany, getPlatform, getRealtimeStore, getWebhookKeysUrl, hasActiveConnection, increment, init, invoke as invokeFunction, isEffectResult, isMobileWalletAvailable, linkEmail, linkWithRedirect, live, intent as liveIntent, status as liveStatus, login, loginWithPopup, loginWithPrivy, loginWithRedirect, logout, now, onAuthLoadingChanged, onAuthStateChanged, queryAggregate, reconnectWithNewAuth, refreshSession, registerMobileWalletAdapter, resetRealtimeStore, revokeSession, runExpression, runExpressionMany, runQuery, runQueryMany, search, sendEmailOtp, sendTextOtp, serverTimestamp, set, setFile, setMany, setPlatform, signAndSubmitTransaction, signInAnonymously, signMessage, signSessionCreateMessage, signTransaction, subscribe, subscribeView as subscribeLiveView, toMillis, toSeconds, useAuth, useQuery, verifyEmailOtp, verifyTextOtp, withEffects, wsDelete, wsGet, wsGetMany, wsQuery, wsSet };
26191
24059
  //# sourceMappingURL=index.esm.js.map