summaryrefslogtreecommitdiffstats
path: root/comm/chat/content/otrWorker.js
blob: 32d96ea9ddacbdce662747303db754fc0d649c44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

/* eslint-env mozilla/chrome-worker, node */
importScripts("resource://gre/modules/workers/require.js");
var PromiseWorker = require("resource://gre/modules/workers/PromiseWorker.js");
var Funcs = {};

// Only what we need from libotr.js
Funcs.generateKey = function (path, otrl_version, address) {
  let libotr = ctypes.open(path);

  let abi = ctypes.default_abi;
  let gcry_error_t = ctypes.unsigned_int;

  // Initialize the OTR library. Pass the version of the API you are using.
  let otrl_init = libotr.declare(
    "otrl_init",
    abi,
    gcry_error_t,
    ctypes.unsigned_int,
    ctypes.unsigned_int,
    ctypes.unsigned_int
  );

  // Do the private key generation calculation. You may call this from a
  // background thread.  When it completes, call
  // otrl_privkey_generate_finish from the _main_ thread.
  let otrl_privkey_generate_calculate = libotr.declare(
    "otrl_privkey_generate_calculate",
    abi,
    gcry_error_t,
    ctypes.void_t.ptr
  );

  otrl_init.apply(libotr, otrl_version);

  let newkey = ctypes.voidptr_t(ctypes.UInt64("0x" + address));
  let err = otrl_privkey_generate_calculate(newkey);
  libotr.close();
  if (err) {
    throw new Error("otrl_privkey_generate_calculate (" + err + ")");
  }
};

var worker = new PromiseWorker.AbstractWorker();

worker.dispatch = function (method, args = []) {
  return Funcs[method](...args);
};

worker.postMessage = function (res, ...args) {
  self.postMessage(res, ...args);
};

worker.close = function () {
  self.close();
};

self.addEventListener("message", msg => worker.handleMessage(msg));