core-3nweb-client-lib 0.19.5 → 0.20.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/api-defs/asmail.d.ts +2 -2
- package/build/api-defs/web3n.d.ts +2 -1
- package/build/core/asmail/delivery/index.js +1 -1
- package/build/core/index.js +1 -1
- package/build/core/sign-up.js +5 -6
- package/build/ipc-via-protobuf/asmail-cap.js +4 -4
- package/build/ipc-via-protobuf/bytes.js +1 -1
- package/build/ipc-via-protobuf/connector-clients-side.js +3 -0
- package/build/ipc-via-protobuf/connector-services-side.js +1 -1
- package/build/ipc-via-protobuf/connector.d.ts +2 -2
- package/build/ipc-via-protobuf/connector.js +4 -1
- package/build/ipc-via-protobuf/file.d.ts +2 -1
- package/build/ipc-via-protobuf/file.js +1 -1
- package/build/ipc-via-protobuf/fs.d.ts +2 -1
- package/build/ipc-via-protobuf/fs.js +1 -1
- package/build/ipc-via-protobuf/log-cap.js +1 -1
- package/build/ipc-via-protobuf/mailerid.js +1 -1
- package/build/ipc-via-protobuf/proto-defs.js +1 -1
- package/build/ipc-via-protobuf/protobuf-msg.d.ts +2 -9
- package/build/ipc-via-protobuf/protobuf-msg.js +13 -70
- package/build/ipc-via-protobuf/protos/asmail.proto +1 -1
- package/build/ipc-via-protobuf/startup-cap.js +1 -1
- package/build/ipc-via-protobuf/storage-cap.js +1 -1
- package/build/lib-client/cryptor/cryptor-in-worker.d.ts +19 -5
- package/build/lib-client/cryptor/cryptor-in-worker.js +191 -157
- package/build/lib-client/cryptor/cryptor-wasm.js +1 -0
- package/build/lib-client/cryptor/cryptor.d.ts +5 -3
- package/build/lib-client/cryptor/cryptor.js +19 -7
- package/build/lib-client/cryptor/cryptor.wasm +0 -0
- package/build/lib-client/cryptor/{cryptor-in-proc.d.ts → in-proc-js.d.ts} +0 -0
- package/build/lib-client/cryptor/{cryptor-in-proc.js → in-proc-js.js} +0 -0
- package/build/lib-client/cryptor/in-proc-wasm.d.ts +2 -0
- package/build/lib-client/cryptor/in-proc-wasm.js +187 -0
- package/build/lib-client/cryptor/proto-defs.js +50 -0
- package/build/lib-client/cryptor/protos/cryptor.proto +45 -0
- package/build/lib-client/cryptor/wasm-mp1-modules.d.ts +5 -0
- package/build/lib-client/cryptor/wasm-mp1-modules.js +79 -0
- package/build/lib-client/cryptor/worker-js.d.ts +1 -0
- package/build/lib-client/cryptor/worker-js.js +139 -0
- package/build/lib-client/cryptor/worker-wasm.d.ts +1 -0
- package/build/lib-client/cryptor/worker-wasm.js +37 -0
- package/build/lib-client/key-derivation.js +1 -1
- package/build/lib-client/protobuf-loader.d.ts +12 -0
- package/build/lib-client/protobuf-loader.js +96 -0
- package/build/lib-index.d.ts +5 -3
- package/build/lib-index.js +5 -3
- package/package.json +2 -2
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { signing } from 'ecma-nacl';
|
|
2
|
-
import { LogWarning } from '../logging/log-to-file';
|
|
2
|
+
import { LogError, LogWarning } from '../logging/log-to-file';
|
|
3
3
|
import { AsyncSBoxCryptor } from 'xsp-files';
|
|
4
4
|
export interface Cryptor {
|
|
5
5
|
scrypt(passwd: Uint8Array, salt: Uint8Array, logN: number, r: number, p: number, dkLen: number, progressCB: (p: number) => void): Promise<Uint8Array>;
|
|
@@ -14,12 +14,14 @@ export interface Cryptor {
|
|
|
14
14
|
generate_keypair(seed: Uint8Array): Promise<signing.Keypair>;
|
|
15
15
|
};
|
|
16
16
|
}
|
|
17
|
-
export declare type makeCryptor = (logWarning: LogWarning, maxThreads?: number) => {
|
|
17
|
+
export declare type makeCryptor = (logErr: LogError, logWarning: LogWarning, maxThreads?: number) => {
|
|
18
18
|
cryptor: Cryptor;
|
|
19
19
|
close: () => Promise<void>;
|
|
20
20
|
};
|
|
21
|
-
export declare
|
|
21
|
+
export declare const makeInWorkerCryptor: makeCryptor;
|
|
22
|
+
export declare const makeInWorkerWasmCryptor: makeCryptor;
|
|
22
23
|
export declare function makeInProcessCryptor(): ReturnType<makeCryptor>;
|
|
24
|
+
export declare function makeInProcessWasmCryptor(): ReturnType<makeCryptor>;
|
|
23
25
|
declare type RuntimeException = web3n.RuntimeException;
|
|
24
26
|
export interface CryptorException extends RuntimeException {
|
|
25
27
|
type: 'cryptor';
|
|
@@ -16,15 +16,27 @@
|
|
|
16
16
|
this program. If not, see <http://www.gnu.org/licenses/>.
|
|
17
17
|
*/
|
|
18
18
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
19
|
-
exports.makeInProcessCryptor = exports.makeInWorkerCryptor = void 0;
|
|
20
|
-
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
19
|
+
exports.makeInProcessWasmCryptor = exports.makeInProcessCryptor = exports.makeInWorkerWasmCryptor = exports.makeInWorkerCryptor = void 0;
|
|
20
|
+
exports.makeInWorkerCryptor = (logErr, logWarning, maxThreads) => {
|
|
21
|
+
const mod = require('./cryptor-in-worker');
|
|
22
|
+
const makeInWorkerCryptor = mod.makeInWorkerCryptor;
|
|
23
|
+
return makeInWorkerCryptor(logErr, logWarning, maxThreads);
|
|
24
|
+
};
|
|
25
|
+
exports.makeInWorkerWasmCryptor = (logErr, logWarning, maxThreads) => {
|
|
26
|
+
const mod = require('./cryptor-in-worker');
|
|
27
|
+
const makeInWorkerWasmCryptor = mod.makeInWorkerWasmCryptor;
|
|
28
|
+
return makeInWorkerWasmCryptor(logErr, logWarning, maxThreads);
|
|
29
|
+
};
|
|
25
30
|
function makeInProcessCryptor() {
|
|
26
|
-
const
|
|
31
|
+
const mod = require('./in-proc-js');
|
|
32
|
+
const makeInProcessCryptor = mod.makeInProcessCryptor;
|
|
27
33
|
return { cryptor: makeInProcessCryptor(), close: async () => { } };
|
|
28
34
|
}
|
|
29
35
|
exports.makeInProcessCryptor = makeInProcessCryptor;
|
|
36
|
+
function makeInProcessWasmCryptor() {
|
|
37
|
+
const mod = require('./in-proc-wasm');
|
|
38
|
+
const makeInProcessWasmCryptor = mod.makeInProcessWasmCryptor;
|
|
39
|
+
return { cryptor: makeInProcessWasmCryptor(), close: async () => { } };
|
|
40
|
+
}
|
|
41
|
+
exports.makeInProcessWasmCryptor = makeInProcessWasmCryptor;
|
|
30
42
|
Object.freeze(exports);
|
|
Binary file
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
Copyright (C) 2021 3NSoft Inc.
|
|
4
|
+
|
|
5
|
+
This program is free software: you can redistribute it and/or modify it under
|
|
6
|
+
the terms of the GNU General Public License as published by the Free Software
|
|
7
|
+
Foundation, either version 3 of the License, or (at your option) any later
|
|
8
|
+
version.
|
|
9
|
+
|
|
10
|
+
This program is distributed in the hope that it will be useful, but
|
|
11
|
+
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
13
|
+
See the GNU General Public License for more details.
|
|
14
|
+
|
|
15
|
+
You should have received a copy of the GNU General Public License along with
|
|
16
|
+
this program. If not, see <http://www.gnu.org/licenses/>.
|
|
17
|
+
*/
|
|
18
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
19
|
+
exports.makeInProcessWasmCryptor = void 0;
|
|
20
|
+
const wasm_mp1_modules_1 = require("./wasm-mp1-modules");
|
|
21
|
+
const fs_1 = require("fs");
|
|
22
|
+
const path_1 = require("path");
|
|
23
|
+
const protobuf_loader_1 = require("../protobuf-loader");
|
|
24
|
+
const processes_1 = require("../../lib-common/processes");
|
|
25
|
+
const assert_1 = require("../../lib-common/assert");
|
|
26
|
+
const error_1 = require("../../lib-common/exceptions/error");
|
|
27
|
+
function wasmBytes() {
|
|
28
|
+
// There is a bug with electrons 12, 13, that doesn't let
|
|
29
|
+
// worker_thread read this file from asar pack, even though main thread
|
|
30
|
+
// makes call from here.
|
|
31
|
+
// Therefore, in case this runs from asar pack, we should switch to
|
|
32
|
+
// unpacked in path that is given to worker thread.
|
|
33
|
+
// Of course, asarUnpack should be used in electron-builder.
|
|
34
|
+
const asarInd = __dirname.indexOf('app.asar');
|
|
35
|
+
const dirWithThis = ((asarInd < 0) ?
|
|
36
|
+
__dirname : `${__dirname.substring(0, asarInd + 8)}.unpacked${__dirname.substring(asarInd + 8)}`);
|
|
37
|
+
const wasmPath = path_1.join(dirWithThis, 'cryptor.wasm');
|
|
38
|
+
try {
|
|
39
|
+
return fs_1.readFileSync(wasmPath);
|
|
40
|
+
}
|
|
41
|
+
catch (err) {
|
|
42
|
+
// chances are that error is due to wasm file not being packaged, so, we
|
|
43
|
+
// look for module with base64 form in a module, that must've been packed
|
|
44
|
+
const str = require('./cryptor-wasm.js').wasm;
|
|
45
|
+
return Buffer.from(str, 'base64');
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
function makeProtobufType(type) {
|
|
49
|
+
const protoFile = 'cryptor.proto';
|
|
50
|
+
const typeName = `cryptor.${type}`;
|
|
51
|
+
try {
|
|
52
|
+
// make sure to copy protos with compile step (use npm script)
|
|
53
|
+
return protobuf_loader_1.ProtoType.makeFrom(__dirname, protoFile, typeName);
|
|
54
|
+
}
|
|
55
|
+
catch (err) {
|
|
56
|
+
// we won't get here if referenced module exists, but metro packager
|
|
57
|
+
// in LiqudCore needs static path require
|
|
58
|
+
const fallback = require('./proto-defs');
|
|
59
|
+
return protobuf_loader_1.ProtoType.makeFrom(__dirname, protoFile, typeName, fallback);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
function toArgs(...args) {
|
|
63
|
+
return args.map(val => ({ val }));
|
|
64
|
+
}
|
|
65
|
+
function toLocalErr(replyErr) {
|
|
66
|
+
if (replyErr.condition === 'cipher-verification') {
|
|
67
|
+
return { failedCipherVerification: true };
|
|
68
|
+
}
|
|
69
|
+
else if (replyErr.condition === 'signature-verification') {
|
|
70
|
+
return { failedSignatureVerification: true };
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
return new Error(`WASM cryptor ${replyErr.condition}: ${replyErr.message}`);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
const reqType = makeProtobufType('Request');
|
|
77
|
+
const replyType = makeProtobufType('Reply');
|
|
78
|
+
const boolValType = makeProtobufType('BoolVal');
|
|
79
|
+
const kpairType = makeProtobufType('Keypair');
|
|
80
|
+
function makeInProcessWasmCryptor() {
|
|
81
|
+
const wasmInstance = wasm_mp1_modules_1.startWasmFrom(wasmBytes());
|
|
82
|
+
let deferred = undefined;
|
|
83
|
+
let interimSink = undefined;
|
|
84
|
+
async function call(req, interim) {
|
|
85
|
+
// In LiquidCore on iOS call was able to get before completion of
|
|
86
|
+
// previous call, therefore, the following await loop is needed.
|
|
87
|
+
while (!!deferred) {
|
|
88
|
+
try {
|
|
89
|
+
await deferred.promise;
|
|
90
|
+
}
|
|
91
|
+
catch (err) { }
|
|
92
|
+
}
|
|
93
|
+
deferred = processes_1.defer();
|
|
94
|
+
if (interim) {
|
|
95
|
+
interimSink = interim;
|
|
96
|
+
}
|
|
97
|
+
process.nextTick(() => {
|
|
98
|
+
try {
|
|
99
|
+
wasmInstance.sendMsgIntoWASM(reqType.pack(req));
|
|
100
|
+
}
|
|
101
|
+
catch (err) {
|
|
102
|
+
deferred === null || deferred === void 0 ? void 0 : deferred.reject(err);
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
return deferred.promise;
|
|
106
|
+
}
|
|
107
|
+
wasmInstance.setMsgListener(msg => {
|
|
108
|
+
assert_1.assert(!!deferred, `Deferred is expected to be available for this output from wasm`);
|
|
109
|
+
try {
|
|
110
|
+
const reply = replyType.unpack(msg);
|
|
111
|
+
if (reply.res) {
|
|
112
|
+
deferred.resolve(reply.res.val);
|
|
113
|
+
deferred = undefined;
|
|
114
|
+
interimSink = undefined;
|
|
115
|
+
}
|
|
116
|
+
else if (reply.err) {
|
|
117
|
+
deferred.reject(error_1.errWithCause(toLocalErr(reply.err), `Error in cryptor`));
|
|
118
|
+
deferred = undefined;
|
|
119
|
+
interimSink = undefined;
|
|
120
|
+
}
|
|
121
|
+
else if (reply.interim) {
|
|
122
|
+
interimSink(reply.interim.val);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
catch (err) {
|
|
126
|
+
deferred.reject(err);
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
return {
|
|
130
|
+
scrypt: (passwd, salt, logN, r, p, dkLen, progressCB) => call({
|
|
131
|
+
func: 1,
|
|
132
|
+
scryptArgs: { passwd, salt, logN, r, p, dkLen }
|
|
133
|
+
}, bytes => progressCB(bytes[0])),
|
|
134
|
+
box: {
|
|
135
|
+
calc_dhshared_key: (pk, sk) => call({
|
|
136
|
+
func: 2,
|
|
137
|
+
byteArgs: toArgs(pk, sk)
|
|
138
|
+
}),
|
|
139
|
+
generate_pubkey: (sk) => call({
|
|
140
|
+
func: 3,
|
|
141
|
+
byteArgs: toArgs(sk)
|
|
142
|
+
})
|
|
143
|
+
},
|
|
144
|
+
sbox: {
|
|
145
|
+
open: (c, n, k) => call({
|
|
146
|
+
func: 4,
|
|
147
|
+
byteArgs: toArgs(c, n, k)
|
|
148
|
+
}),
|
|
149
|
+
pack: (m, n, k) => call({
|
|
150
|
+
func: 5,
|
|
151
|
+
byteArgs: toArgs(m, n, k)
|
|
152
|
+
}),
|
|
153
|
+
formatWN: {
|
|
154
|
+
open: (cn, k) => call({
|
|
155
|
+
func: 6,
|
|
156
|
+
byteArgs: toArgs(cn, k)
|
|
157
|
+
}),
|
|
158
|
+
pack: (m, n, k) => call({
|
|
159
|
+
func: 7,
|
|
160
|
+
byteArgs: toArgs(m, n, k)
|
|
161
|
+
})
|
|
162
|
+
}
|
|
163
|
+
},
|
|
164
|
+
signing: {
|
|
165
|
+
generate_keypair: async (seed) => {
|
|
166
|
+
const rep = await call({
|
|
167
|
+
func: 8,
|
|
168
|
+
byteArgs: toArgs(seed)
|
|
169
|
+
});
|
|
170
|
+
return kpairType.unpack(rep);
|
|
171
|
+
},
|
|
172
|
+
signature: (m, sk) => call({
|
|
173
|
+
func: 9,
|
|
174
|
+
byteArgs: toArgs(m, sk)
|
|
175
|
+
}),
|
|
176
|
+
verify: async (sig, m, pk) => {
|
|
177
|
+
const rep = await call({
|
|
178
|
+
func: 10,
|
|
179
|
+
byteArgs: toArgs(sig, m, pk)
|
|
180
|
+
});
|
|
181
|
+
return boolValType.unpack(rep).val;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
exports.makeInProcessWasmCryptor = makeInProcessWasmCryptor;
|
|
187
|
+
Object.freeze(exports);
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
exports.protos = {};
|
|
2
|
+
exports.protos['cryptor.proto'] = `
|
|
3
|
+
syntax = "proto3";
|
|
4
|
+
|
|
5
|
+
package cryptor;
|
|
6
|
+
|
|
7
|
+
message Request {
|
|
8
|
+
|
|
9
|
+
message ScryptArgs {
|
|
10
|
+
bytes passwd = 1;
|
|
11
|
+
bytes salt = 2;
|
|
12
|
+
uint32 logN = 3;
|
|
13
|
+
uint32 r = 4;
|
|
14
|
+
uint32 p = 5;
|
|
15
|
+
uint32 dkLen = 6;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
uint32 func = 1;
|
|
19
|
+
repeated BytesVal byte_args = 2;
|
|
20
|
+
ScryptArgs scrypt_args = 3;
|
|
21
|
+
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
message BytesVal {
|
|
25
|
+
bytes val = 1;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
message Reply {
|
|
29
|
+
|
|
30
|
+
message Error {
|
|
31
|
+
string condition = 1;
|
|
32
|
+
string message = 2;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
BytesVal res = 1;
|
|
36
|
+
BytesVal interim = 2;
|
|
37
|
+
Error err = 3;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
message BoolVal {
|
|
41
|
+
bool val = 1;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
message Keypair {
|
|
45
|
+
bytes skey = 1;
|
|
46
|
+
bytes pkey = 2;
|
|
47
|
+
}
|
|
48
|
+
`;
|
|
49
|
+
Object.freeze(exports.protos);
|
|
50
|
+
Object.freeze(exports);
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
syntax = "proto3";
|
|
2
|
+
|
|
3
|
+
package cryptor;
|
|
4
|
+
|
|
5
|
+
message Request {
|
|
6
|
+
|
|
7
|
+
message ScryptArgs {
|
|
8
|
+
bytes passwd = 1;
|
|
9
|
+
bytes salt = 2;
|
|
10
|
+
uint32 logN = 3;
|
|
11
|
+
uint32 r = 4;
|
|
12
|
+
uint32 p = 5;
|
|
13
|
+
uint32 dkLen = 6;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
uint32 func = 1;
|
|
17
|
+
repeated BytesVal byte_args = 2;
|
|
18
|
+
ScryptArgs scrypt_args = 3;
|
|
19
|
+
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
message BytesVal {
|
|
23
|
+
bytes val = 1;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
message Reply {
|
|
27
|
+
|
|
28
|
+
message Error {
|
|
29
|
+
string condition = 1;
|
|
30
|
+
string message = 2;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
BytesVal res = 1;
|
|
34
|
+
BytesVal interim = 2;
|
|
35
|
+
Error err = 3;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
message BoolVal {
|
|
39
|
+
bool val = 1;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
message Keypair {
|
|
43
|
+
bytes skey = 1;
|
|
44
|
+
bytes pkey = 2;
|
|
45
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
Copyright (C) 2021 3NSoft Inc.
|
|
4
|
+
|
|
5
|
+
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
|
6
|
+
|
|
7
|
+
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
8
|
+
|
|
9
|
+
You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
10
|
+
*/
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.startWasmFrom = void 0;
|
|
13
|
+
class MP1 {
|
|
14
|
+
constructor(wasmBytes) {
|
|
15
|
+
this.sinkMsgFromWasmInstance = undefined;
|
|
16
|
+
this.msgWaitingWriteCB = undefined;
|
|
17
|
+
const module = new WebAssembly.Module(wasmBytes);
|
|
18
|
+
this.instance = new WebAssembly.Instance(module, this.makeImports());
|
|
19
|
+
this.mp1_accept_msg = this.getExportedFn('_3nweb_mp1_accept_msg');
|
|
20
|
+
Object.seal(this);
|
|
21
|
+
}
|
|
22
|
+
getExportedFn(fName) {
|
|
23
|
+
const fn = this.instance.exports[fName];
|
|
24
|
+
if (typeof fn !== 'function') {
|
|
25
|
+
throw Error(`WASM instance doesn't export function ${fName}`);
|
|
26
|
+
}
|
|
27
|
+
return fn;
|
|
28
|
+
}
|
|
29
|
+
makeImports() {
|
|
30
|
+
const env = {
|
|
31
|
+
_3nweb_mp1_send_out_msg: (ptr, len) => this.processMsgFromModule(ptr, len),
|
|
32
|
+
_3nweb_mp1_write_msg_into: (ptr) => this.writeMsgInCB(ptr),
|
|
33
|
+
};
|
|
34
|
+
return { env };
|
|
35
|
+
}
|
|
36
|
+
processMsgFromModule(ptr, len) {
|
|
37
|
+
if (!this.sinkMsgFromWasmInstance) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
const msgBuf = this.getWasmMemoryArea(ptr, len);
|
|
41
|
+
const msg = new Uint8Array(msgBuf.length);
|
|
42
|
+
msg.set(msgBuf);
|
|
43
|
+
this.sinkMsgFromWasmInstance(msg);
|
|
44
|
+
}
|
|
45
|
+
writeMsgInCB(ptr) {
|
|
46
|
+
const buf = this.getWasmMemoryArea(ptr, this.msgWaitingWriteCB.length);
|
|
47
|
+
buf.set(this.msgWaitingWriteCB);
|
|
48
|
+
this.msgWaitingWriteCB = undefined;
|
|
49
|
+
}
|
|
50
|
+
getWasmMemoryArea(ptr, len) {
|
|
51
|
+
const memBuf = this.instance.exports.memory.buffer;
|
|
52
|
+
return new Uint8Array(memBuf, ptr, len);
|
|
53
|
+
}
|
|
54
|
+
start() {
|
|
55
|
+
this.getExportedFn('_start')();
|
|
56
|
+
}
|
|
57
|
+
sendMsg(msg) {
|
|
58
|
+
this.msgWaitingWriteCB = msg;
|
|
59
|
+
// Note that following call to WASM expects it to call back imported
|
|
60
|
+
// function that actually copies bytes from this.msgWaitingWriteCB into
|
|
61
|
+
// then given memory area.
|
|
62
|
+
this.mp1_accept_msg(msg.length);
|
|
63
|
+
}
|
|
64
|
+
setMsgListener(listener) {
|
|
65
|
+
this.sinkMsgFromWasmInstance = listener;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
Object.freeze(MP1.prototype);
|
|
69
|
+
Object.freeze(MP1);
|
|
70
|
+
function startWasmFrom(wasmModuleBytes) {
|
|
71
|
+
let mp1 = new MP1(wasmModuleBytes);
|
|
72
|
+
mp1.start();
|
|
73
|
+
return {
|
|
74
|
+
sendMsgIntoWASM: msg => mp1.sendMsg(msg),
|
|
75
|
+
setMsgListener: listener => mp1.setMsgListener(listener)
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
exports.startWasmFrom = startWasmFrom;
|
|
79
|
+
Object.freeze(exports);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
Copyright (C) 2020 3NSoft Inc.
|
|
4
|
+
|
|
5
|
+
This program is free software: you can redistribute it and/or modify it under
|
|
6
|
+
the terms of the GNU General Public License as published by the Free Software
|
|
7
|
+
Foundation, either version 3 of the License, or (at your option) any later
|
|
8
|
+
version.
|
|
9
|
+
|
|
10
|
+
This program is distributed in the hope that it will be useful, but
|
|
11
|
+
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
13
|
+
See the GNU General Public License for more details.
|
|
14
|
+
|
|
15
|
+
You should have received a copy of the GNU General Public License along with
|
|
16
|
+
this program. If not, see <http://www.gnu.org/licenses/>.
|
|
17
|
+
*/
|
|
18
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
19
|
+
const worker_threads_1 = require("worker_threads");
|
|
20
|
+
const ecma_nacl_1 = require("ecma-nacl");
|
|
21
|
+
const error_1 = require("../../lib-common/exceptions/error");
|
|
22
|
+
function transfer(...arrs) {
|
|
23
|
+
const transferLst = [];
|
|
24
|
+
for (const arr of arrs) {
|
|
25
|
+
const buffer = arr.buffer;
|
|
26
|
+
if (!transferLst.includes(buffer)) {
|
|
27
|
+
transferLst.push(buffer);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return transferLst;
|
|
31
|
+
}
|
|
32
|
+
if (!worker_threads_1.parentPort) {
|
|
33
|
+
throw new Error(`Missing expected parentPort. Is this called within WebWorker process?`);
|
|
34
|
+
}
|
|
35
|
+
const arrFactory = ecma_nacl_1.arrays.makeFactory();
|
|
36
|
+
const wipe = ecma_nacl_1.arrays.wipe;
|
|
37
|
+
const funcs = {
|
|
38
|
+
'scrypt': args => {
|
|
39
|
+
const progressCB = (n) => {
|
|
40
|
+
const reply = { interim: n };
|
|
41
|
+
worker_threads_1.parentPort.postMessage(reply);
|
|
42
|
+
};
|
|
43
|
+
const res = ecma_nacl_1.scrypt(args[0], args[1], args[2], args[3], args[4], args[5], progressCB, arrFactory);
|
|
44
|
+
wipe(args[0]);
|
|
45
|
+
return { res };
|
|
46
|
+
// electron v.11.0.3 worker thread fails on memory move
|
|
47
|
+
// return { res, trans: transfer(res) };
|
|
48
|
+
},
|
|
49
|
+
'box.calc_dhshared_key': args => {
|
|
50
|
+
const res = ecma_nacl_1.box.calc_dhshared_key(args[0], args[1], arrFactory);
|
|
51
|
+
wipe(args[0], args[1]);
|
|
52
|
+
return { res };
|
|
53
|
+
// electron v.11.0.3 worker thread fails on memory move
|
|
54
|
+
// return { res, trans: transfer(res) };
|
|
55
|
+
},
|
|
56
|
+
'box.generate_pubkey': args => {
|
|
57
|
+
const res = ecma_nacl_1.box.generate_pubkey(args[0], arrFactory);
|
|
58
|
+
wipe(args[0]);
|
|
59
|
+
return { res };
|
|
60
|
+
// electron v.11.0.3 worker thread fails on memory move
|
|
61
|
+
// return { res, trans: transfer(res) };
|
|
62
|
+
},
|
|
63
|
+
'sbox.open': args => {
|
|
64
|
+
const res = ecma_nacl_1.secret_box.open(args[0], args[1], args[2], arrFactory);
|
|
65
|
+
wipe(args[2]);
|
|
66
|
+
return { res };
|
|
67
|
+
// electron v.11.0.3 worker thread fails on memory move
|
|
68
|
+
// return { res, trans: transfer(res) };
|
|
69
|
+
},
|
|
70
|
+
'sbox.pack': args => {
|
|
71
|
+
const res = ecma_nacl_1.secret_box.pack(args[0], args[1], args[2], arrFactory);
|
|
72
|
+
wipe(args[2]);
|
|
73
|
+
return { res };
|
|
74
|
+
// electron v.11.0.3 worker thread fails on memory move
|
|
75
|
+
// return { res, trans: transfer(res) };
|
|
76
|
+
},
|
|
77
|
+
'sbox.formatWN.open': args => {
|
|
78
|
+
const res = ecma_nacl_1.secret_box.formatWN.open(args[0], args[1], arrFactory);
|
|
79
|
+
wipe(args[1]);
|
|
80
|
+
return { res };
|
|
81
|
+
// electron v.11.0.3 worker thread fails on memory move
|
|
82
|
+
// return { res, trans: transfer(res) };
|
|
83
|
+
},
|
|
84
|
+
'sbox.formatWN.pack': args => {
|
|
85
|
+
const res = ecma_nacl_1.secret_box.formatWN.pack(args[0], args[1], args[2], arrFactory);
|
|
86
|
+
wipe(args[2]);
|
|
87
|
+
return { res };
|
|
88
|
+
// electron v.11.0.3 worker thread fails on memory move
|
|
89
|
+
// return { res, trans: transfer(res) };
|
|
90
|
+
},
|
|
91
|
+
'sign.generate_keypair': args => {
|
|
92
|
+
const pair = ecma_nacl_1.signing.generate_keypair(args[0], arrFactory);
|
|
93
|
+
wipe(args[0]);
|
|
94
|
+
return { res: pair };
|
|
95
|
+
// electron v.11.0.3 worker thread fails on memory move
|
|
96
|
+
// return { res: pair, trans: transfer(pair.pkey, pair.skey) };
|
|
97
|
+
},
|
|
98
|
+
'sign.signature': args => {
|
|
99
|
+
const res = ecma_nacl_1.signing.signature(args[0], args[1], arrFactory);
|
|
100
|
+
wipe(args[1]);
|
|
101
|
+
return { res };
|
|
102
|
+
// electron v.11.0.3 worker thread fails on memory move
|
|
103
|
+
// return { res, trans: transfer(res) };
|
|
104
|
+
},
|
|
105
|
+
'sign.verify': args => {
|
|
106
|
+
const ok = ecma_nacl_1.signing.verify(args[0], args[1], args[2], arrFactory);
|
|
107
|
+
return { res: ok };
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
function wrapError(err) {
|
|
111
|
+
const exc = {
|
|
112
|
+
runtimeException: true,
|
|
113
|
+
type: 'cryptor'
|
|
114
|
+
};
|
|
115
|
+
if (err.failedCipherVerification) {
|
|
116
|
+
exc.failedCipherVerification = true;
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
exc.message = `Error occured in cryptor worker thread`;
|
|
120
|
+
exc.cause = error_1.stringifyErr(err);
|
|
121
|
+
}
|
|
122
|
+
return exc;
|
|
123
|
+
}
|
|
124
|
+
worker_threads_1.parentPort.on('message', (msg) => {
|
|
125
|
+
const { args, func } = msg;
|
|
126
|
+
const code = funcs[func];
|
|
127
|
+
if (!code) {
|
|
128
|
+
throw new Error(`Function ${func} is unknown`);
|
|
129
|
+
}
|
|
130
|
+
try {
|
|
131
|
+
const { res, trans } = code(args);
|
|
132
|
+
const reply = { res };
|
|
133
|
+
worker_threads_1.parentPort.postMessage(reply, trans);
|
|
134
|
+
}
|
|
135
|
+
catch (err) {
|
|
136
|
+
const reply = { err: wrapError(err) };
|
|
137
|
+
worker_threads_1.parentPort.postMessage(reply);
|
|
138
|
+
}
|
|
139
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
Copyright (C) 2020 3NSoft Inc.
|
|
4
|
+
|
|
5
|
+
This program is free software: you can redistribute it and/or modify it under
|
|
6
|
+
the terms of the GNU General Public License as published by the Free Software
|
|
7
|
+
Foundation, either version 3 of the License, or (at your option) any later
|
|
8
|
+
version.
|
|
9
|
+
|
|
10
|
+
This program is distributed in the hope that it will be useful, but
|
|
11
|
+
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
13
|
+
See the GNU General Public License for more details.
|
|
14
|
+
|
|
15
|
+
You should have received a copy of the GNU General Public License along with
|
|
16
|
+
this program. If not, see <http://www.gnu.org/licenses/>.
|
|
17
|
+
*/
|
|
18
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
19
|
+
const worker_threads_1 = require("worker_threads");
|
|
20
|
+
const path_1 = require("path");
|
|
21
|
+
const wasm_mp1_modules_1 = require("./wasm-mp1-modules");
|
|
22
|
+
const fs_1 = require("fs");
|
|
23
|
+
if (!worker_threads_1.parentPort) {
|
|
24
|
+
throw new Error(`Missing expected parentPort. Is this called within WebWorker process?`);
|
|
25
|
+
}
|
|
26
|
+
const wasmFName = 'cryptor.wasm';
|
|
27
|
+
const wasmModPath = path_1.join(worker_threads_1.workerData.loadDir, wasmFName);
|
|
28
|
+
const wasmInstance = wasm_mp1_modules_1.startWasmFrom(fs_1.readFileSync(wasmModPath));
|
|
29
|
+
wasmInstance.setMsgListener(msg => worker_threads_1.parentPort.postMessage(msg));
|
|
30
|
+
worker_threads_1.parentPort.on('message', (msg) => {
|
|
31
|
+
try {
|
|
32
|
+
wasmInstance.sendMsgIntoWASM(msg);
|
|
33
|
+
}
|
|
34
|
+
catch (err) {
|
|
35
|
+
console.error(err);
|
|
36
|
+
}
|
|
37
|
+
});
|
|
@@ -45,7 +45,7 @@ async function deriveMidKeyPair(cryptor, pass, derivParams, progressCB, use = ''
|
|
|
45
45
|
const passBytes = buffer_utils_1.utf8.pack(pass);
|
|
46
46
|
const saltBytes = buffer_utils_1.base64.open(derivParams.salt);
|
|
47
47
|
const skey = await cryptor.scrypt(passBytes, saltBytes, derivParams.logN, derivParams.r, derivParams.p, ecma_nacl_1.box.KEY_LENGTH, progressCB);
|
|
48
|
-
const pkey =
|
|
48
|
+
const pkey = await cryptor.box.generate_pubkey(skey);
|
|
49
49
|
const pkeyJSON = jwkeys_1.keyToJson({
|
|
50
50
|
k: pkey,
|
|
51
51
|
alg: ecma_nacl_1.box.JWK_ALG_NAME,
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
export declare class ProtoType<T extends object> {
|
|
3
|
+
private type;
|
|
4
|
+
private constructor();
|
|
5
|
+
static makeFrom<T extends object>(searchDir: string, protoFile: string, typeName: string, fallbackMod?: {
|
|
6
|
+
protos: object;
|
|
7
|
+
}): ProtoType<T>;
|
|
8
|
+
pack(msg: T): Buffer;
|
|
9
|
+
unpack(bytes: Buffer | void): T;
|
|
10
|
+
packToBase64(msg: T): string;
|
|
11
|
+
unpackFromBase64(str: string): T;
|
|
12
|
+
}
|