1 | /* globals importScripts */ |
||
2 | |||
3 | var bip39 = require("bip39"); |
||
4 | var Encryption = require('./encryption'); |
||
5 | |||
6 | module.exports = function(self) { |
||
7 | self.addEventListener('message', function(e) { |
||
8 | var data = e.data || {}; |
||
9 | |||
10 | switch (data.method) { |
||
11 | case 'importScripts': |
||
12 | importScripts(data.script); |
||
13 | |||
14 | break; |
||
15 | |||
16 | case 'mnemonicToSeedHex': |
||
17 | (function() { |
||
18 | try { |
||
19 | var mnemonic = data.mnemonic; |
||
20 | var passphrase = data.passphrase; |
||
21 | |||
22 | if (!bip39.validateMnemonic(mnemonic)) { |
||
23 | e = new Error('Invalid passphrase'); |
||
0 ignored issues
–
show
Comprehensibility
introduced
by
![]() |
|||
24 | e.id = data.id; |
||
25 | throw e; |
||
26 | } |
||
27 | var seed = bip39.mnemonicToSeedHex(mnemonic, passphrase); |
||
28 | |||
29 | self.postMessage({id: data.id, seed: seed, mnemonic: mnemonic}); |
||
30 | } catch (e) { |
||
31 | e.id = data.id; |
||
32 | throw e; |
||
33 | } |
||
34 | })(); |
||
35 | break; |
||
36 | |||
37 | case 'Encryption.encryptWithSaltAndIV': |
||
38 | (function() { |
||
39 | try { |
||
40 | if (!data.pt || !data.pw || !data.saltBuf || !data.iv || !data.iterations) { |
||
41 | throw new Error("Invalid input"); |
||
42 | } |
||
43 | |||
44 | var pt = Buffer.from(data.pt.buffer); |
||
0 ignored issues
–
show
The variable
Buffer seems to be never declared. If this is a global, consider adding a /** global: Buffer */ comment.
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed. To learn more about declaring variables in Javascript, see the MDN. ![]() |
|||
45 | var pw = Buffer.from(data.pw.buffer); |
||
46 | var saltBuf = Buffer.from(data.saltBuf.buffer); |
||
47 | var iv = Buffer.from(data.iv.buffer); |
||
48 | var iterations = data.iterations; |
||
49 | |||
50 | var cipherText = Encryption.encryptWithSaltAndIV(pt, pw, saltBuf, iv, iterations); |
||
51 | |||
52 | self.postMessage({id: data.id, cipherText: cipherText}); |
||
53 | } catch (e) { |
||
54 | e.id = data.id; |
||
55 | throw e; |
||
56 | } |
||
57 | })(); |
||
58 | break; |
||
59 | |||
60 | case 'Encryption.decrypt': |
||
61 | (function() { |
||
62 | try { |
||
63 | if (!data.ct || !data.pw) { |
||
64 | throw new Error("Invalid input"); |
||
65 | } |
||
66 | |||
67 | var ct = Buffer.from(data.ct.buffer); |
||
0 ignored issues
–
show
The variable
Buffer seems to be never declared. If this is a global, consider adding a /** global: Buffer */ comment.
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed. To learn more about declaring variables in Javascript, see the MDN. ![]() |
|||
68 | var pw = Buffer.from(data.pw.buffer); |
||
69 | |||
70 | var plainText = Encryption.decrypt(ct, pw); |
||
71 | |||
72 | self.postMessage({id: data.id, plainText: plainText}); |
||
73 | } catch (e) { |
||
74 | e.id = data.id; |
||
75 | throw e; |
||
76 | } |
||
77 | })(); |
||
78 | break; |
||
79 | |||
80 | default: |
||
81 | e = new Error('Invalid method [' + e.method + ']'); |
||
82 | e.id = data.id; |
||
83 | throw e; |
||
84 | } |
||
85 | }, false); |
||
86 | }; |
||
87 |