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