"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.initRustCrypto = initRustCrypto;
var RustSdkCryptoJs = _interopRequireWildcard(require("@matrix-org/matrix-sdk-crypto-wasm"));
var _rustCrypto = require("./rust-crypto");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
/*
Copyright 2022 The Matrix.org Foundation C.I.C.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

/**
 * Create a new `RustCrypto` implementation
 *
 * @param logger - A `Logger` instance that will be used for debug output.
 * @param http - Low-level HTTP interface: used to make outgoing requests required by the rust SDK.
 *     We expect it to set the access token, etc.
 * @param userId - The local user's User ID.
 * @param deviceId - The local user's Device ID.
 * @param secretStorage - Interface to server-side secret storage.
 * @param cryptoCallbacks - Crypto callbacks provided by the application
 * @param storePrefix - the prefix to use on the indexeddbs created by rust-crypto.
 *     If `null`, a memory store will be used.
 * @param storePassphrase - a passphrase to use to encrypt the indexeddbs created by rust-crypto.
 *     Ignored if `storePrefix` is null. If this is `undefined` (and `storePrefix` is not null), the indexeddbs
 *     will be unencrypted.
 *
 * @internal
 */
async function initRustCrypto(logger, http, userId, deviceId, secretStorage, cryptoCallbacks, storePrefix, storePassphrase) {
  var _ref;
  // initialise the rust matrix-sdk-crypto-wasm, if it hasn't already been done
  await RustSdkCryptoJs.initAsync();

  // enable tracing in the rust-sdk
  new RustSdkCryptoJs.Tracing(RustSdkCryptoJs.LoggerLevel.Debug).turnOn();
  const u = new RustSdkCryptoJs.UserId(userId);
  const d = new RustSdkCryptoJs.DeviceId(deviceId);
  logger.info("Init OlmMachine");

  // TODO: use the pickle key for the passphrase
  const olmMachine = await RustSdkCryptoJs.OlmMachine.initialize(u, d, storePrefix !== null && storePrefix !== void 0 ? storePrefix : undefined, (_ref = storePrefix && storePassphrase) !== null && _ref !== void 0 ? _ref : undefined);
  const rustCrypto = new _rustCrypto.RustCrypto(logger, olmMachine, http, userId, deviceId, secretStorage, cryptoCallbacks);
  await olmMachine.registerRoomKeyUpdatedCallback(sessions => rustCrypto.onRoomKeysUpdated(sessions));
  await olmMachine.registerUserIdentityUpdatedCallback(userId => rustCrypto.onUserIdentityUpdated(userId));

  // Check if there are any key backup secrets pending processing. There may be multiple secrets to process if several devices have gossiped them.
  // The `registerReceiveSecretCallback` function will only be triggered for new secrets. If the client is restarted before processing them, the secrets will need to be manually handled.
  rustCrypto.checkSecrets("m.megolm_backup.v1");

  // Register a callback to be notified when a new secret is received, as for now only the key backup secret is supported (the cross signing secrets are handled automatically by the OlmMachine)
  await olmMachine.registerReceiveSecretCallback((name, _value) =>
  // Instead of directly checking the secret value, we poll the inbox to get all values for that secret type.
  // Once we have all the values, we can safely clear the secret inbox.
  rustCrypto.checkSecrets(name));

  // Tell the OlmMachine to think about its outgoing requests before we hand control back to the application.
  //
  // This is primarily a fudge to get it to correctly populate the `users_for_key_query` list, so that future
  // calls to getIdentity (etc) block until the key queries are performed.
  //
  // Note that we don't actually need to *make* any requests here; it is sufficient to tell the Rust side to think
  // about them.
  //
  // XXX: find a less hacky way to do this.
  await olmMachine.outgoingRequests();
  logger.info("Completed rust crypto-sdk setup");
  return rustCrypto;
}
//# sourceMappingURL=index.js.map