@secrecy/lib 1.7.0 → 1.8.0
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/LICENSE +1 -1
- package/dist/lib/base-client.js +91 -0
- package/dist/lib/cache.js +4 -0
- package/dist/lib/client/SecrecryCareClient.js +19 -0
- package/dist/lib/client/SecrecyAppClient.js +87 -0
- package/dist/lib/client/SecrecyCloudClient.js +475 -0
- package/dist/lib/client/SecrecyDbClient.js +10 -0
- package/dist/lib/client/SecrecyMailClient.js +283 -0
- package/dist/lib/client/SecrecyPayClient.js +34 -0
- package/dist/lib/client/SecrecyUserClient.js +27 -0
- package/dist/lib/client/SecrecyWalletClient.js +50 -0
- package/dist/lib/client/convert/file.js +29 -0
- package/dist/lib/client/convert/mail.js +42 -0
- package/dist/lib/client/convert/node.js +100 -0
- package/dist/lib/client/helpers.js +103 -0
- package/dist/lib/client/index.js +49 -0
- package/dist/lib/client/storage.js +7 -0
- package/dist/lib/client/types/app.js +1 -0
- package/dist/lib/client/types/file.js +1 -0
- package/dist/lib/client/types/index.js +15 -0
- package/dist/lib/client/types/mail.js +1 -0
- package/dist/lib/client/types/node.js +1 -0
- package/dist/lib/client/types/user.js +1 -0
- package/dist/lib/client.js +47 -0
- package/dist/lib/crypto/file.js +184 -0
- package/dist/lib/crypto/index.js +41 -0
- package/dist/lib/error/client.js +10 -0
- package/dist/lib/error/index.js +10 -0
- package/dist/lib/error/server.js +27 -0
- package/dist/lib/index.js +10 -0
- package/dist/lib/minify/index.js +23 -0
- package/dist/lib/minify/lz4.js +517 -0
- package/dist/lib/sodium.js +5 -0
- package/dist/lib/types.js +1 -0
- package/dist/lib/utils/array.js +25 -0
- package/dist/lib/utils/base64.js +2 -0
- package/dist/lib/utils/popup-tools.js +180 -0
- package/dist/lib/utils/promise.js +20 -0
- package/dist/lib/utils/store-buddy.js +60 -0
- package/dist/lib/utils/time.js +13 -0
- package/dist/lib/worker/md5.js +18 -0
- package/dist/lib/worker/sodium.js +95 -0
- package/dist/lib/worker/workerCodes.js +254 -0
- package/dist/types/base-client.d.ts +28 -0
- package/dist/types/cache.d.ts +11 -0
- package/dist/types/client/SecrecryCareClient.d.ts +9 -0
- package/dist/types/client/SecrecyAppClient.d.ts +20 -0
- package/dist/types/client/SecrecyCloudClient.d.ts +88 -0
- package/dist/types/client/SecrecyDbClient.d.ts +7 -0
- package/dist/types/client/SecrecyMailClient.d.ts +42 -0
- package/dist/types/client/SecrecyPayClient.d.ts +27 -0
- package/dist/types/client/SecrecyUserClient.d.ts +14 -0
- package/dist/{client → types/client}/SecrecyWalletClient.d.ts +9 -12
- package/dist/types/client/convert/file.d.ts +4 -0
- package/dist/types/client/convert/mail.d.ts +8 -0
- package/dist/types/client/convert/node.d.ts +7 -0
- package/dist/types/client/helpers.d.ts +26 -0
- package/dist/types/client/index.d.ts +29 -0
- package/dist/{client → types/client}/storage.d.ts +2 -2
- package/dist/types/client/types/app.d.ts +2 -0
- package/dist/types/client/types/file.d.ts +6 -0
- package/dist/types/client/types/index.d.ts +46 -0
- package/dist/types/client/types/mail.d.ts +87 -0
- package/dist/{client/types/Node.d.ts → types/client/types/node.d.ts} +20 -25
- package/dist/types/client/types/user.d.ts +3 -0
- package/dist/types/client.d.ts +12138 -0
- package/dist/{crypto → types/crypto}/file.d.ts +4 -4
- package/dist/{crypto → types/crypto}/index.d.ts +1 -4
- package/dist/types/error/client.d.ts +22 -0
- package/dist/types/error/index.d.ts +40 -0
- package/dist/types/error/server.d.ts +26 -0
- package/dist/types/index.d.ts +13 -0
- package/dist/{minify → types/minify}/index.d.ts +1 -1
- package/dist/types/minify/lz4.d.ts +35 -0
- package/dist/{sodium.d.ts → types/sodium.d.ts} +1 -1
- package/dist/types/types.d.ts +10 -0
- package/dist/{utils/utils.d.ts → types/utils/array.d.ts} +0 -1
- package/dist/types/utils/base64.d.ts +1 -0
- package/dist/types/utils/popup-tools.d.ts +52 -0
- package/dist/types/utils/promise.d.ts +1 -0
- package/dist/types/utils/store-buddy.d.ts +130 -0
- package/dist/{worker → types/worker}/sodium.d.ts +1 -1
- package/package.json +48 -73
- package/dist/BaseClient.d.ts +0 -111
- package/dist/BaseClient.js +0 -506
- package/dist/PopupTools.d.ts +0 -17
- package/dist/PopupTools.js +0 -195
- package/dist/ZeusThunder.d.ts +0 -2
- package/dist/ZeusThunder.js +0 -65
- package/dist/cache.d.ts +0 -6
- package/dist/cache.js +0 -4
- package/dist/client/SecrecyAppClient.d.ts +0 -17
- package/dist/client/SecrecyAppClient.js +0 -226
- package/dist/client/SecrecyCloudClient.d.ts +0 -89
- package/dist/client/SecrecyCloudClient.js +0 -1405
- package/dist/client/SecrecyDbClient.d.ts +0 -48
- package/dist/client/SecrecyDbClient.js +0 -419
- package/dist/client/SecrecyMailClient.d.ts +0 -42
- package/dist/client/SecrecyMailClient.js +0 -1022
- package/dist/client/SecrecyPayClient.d.ts +0 -28
- package/dist/client/SecrecyPayClient.js +0 -68
- package/dist/client/SecrecyWalletClient.js +0 -73
- package/dist/client/convert/file.d.ts +0 -5
- package/dist/client/convert/file.js +0 -33
- package/dist/client/convert/mail.d.ts +0 -3
- package/dist/client/convert/mail.js +0 -42
- package/dist/client/convert/node.d.ts +0 -9
- package/dist/client/convert/node.js +0 -87
- package/dist/client/helpers.d.ts +0 -28
- package/dist/client/helpers.js +0 -119
- package/dist/client/index.d.ts +0 -34
- package/dist/client/index.js +0 -46
- package/dist/client/storage.js +0 -12
- package/dist/client/types/File.d.ts +0 -14
- package/dist/client/types/File.js +0 -3
- package/dist/client/types/Inputs.d.ts +0 -16
- package/dist/client/types/Inputs.js +0 -3
- package/dist/client/types/Node.js +0 -3
- package/dist/client/types/UserAppNotifications.d.ts +0 -6
- package/dist/client/types/UserAppNotifications.js +0 -3
- package/dist/client/types/UserAppSettings.d.ts +0 -5
- package/dist/client/types/UserAppSettings.js +0 -3
- package/dist/client/types/index.d.ts +0 -120
- package/dist/client/types/index.js +0 -8
- package/dist/client/types/selectors.d.ts +0 -400
- package/dist/client/types/selectors.js +0 -135
- package/dist/crypto/file.js +0 -195
- package/dist/crypto/index.js +0 -45
- package/dist/error.d.ts +0 -33
- package/dist/error.js +0 -3
- package/dist/index.d.ts +0 -14
- package/dist/index.js +0 -10
- package/dist/minify/index.js +0 -23
- package/dist/minify/lz4.d.ts +0 -5
- package/dist/minify/lz4.js +0 -539
- package/dist/sodium.js +0 -6
- package/dist/utils/encoders.d.ts +0 -26
- package/dist/utils/encoders.js +0 -18
- package/dist/utils/store-buddy.d.ts +0 -14
- package/dist/utils/store-buddy.js +0 -58
- package/dist/utils/time.js +0 -12
- package/dist/utils/utils.js +0 -47
- package/dist/worker/md5.js +0 -24
- package/dist/worker/sodium.js +0 -118
- package/dist/worker/workerCodes.js +0 -255
- package/dist/zeus/const.d.ts +0 -7
- package/dist/zeus/const.js +0 -1846
- package/dist/zeus/index.d.ts +0 -8721
- package/dist/zeus/index.js +0 -642
- /package/dist/{utils → types/utils}/time.d.ts +0 -0
- /package/dist/{worker → types/worker}/md5.d.ts +0 -0
- /package/dist/{worker → types/worker}/workerCodes.d.ts +0 -0
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TS Rewrite of https://github.com/enhancv/popup-tools
|
|
3
|
+
*/
|
|
4
|
+
const defaultOptions = {
|
|
5
|
+
width: 700,
|
|
6
|
+
height: 520,
|
|
7
|
+
menubar: 'no',
|
|
8
|
+
resizable: 'yes',
|
|
9
|
+
location: 'yes',
|
|
10
|
+
scrollbars: 'no',
|
|
11
|
+
centered: true,
|
|
12
|
+
};
|
|
13
|
+
let popupCount = 1;
|
|
14
|
+
/**
|
|
15
|
+
* Return options converted to a string
|
|
16
|
+
*
|
|
17
|
+
* @param {Object} options
|
|
18
|
+
* @return {String}
|
|
19
|
+
*/
|
|
20
|
+
function optionsToString(options) {
|
|
21
|
+
return Object.entries(options)
|
|
22
|
+
.map(([k, v]) => `${k}=${v}`)
|
|
23
|
+
.join(',');
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Get a unique name on each call
|
|
27
|
+
* @return {String}
|
|
28
|
+
*/
|
|
29
|
+
function defaultPopupName() {
|
|
30
|
+
popupCount += 1;
|
|
31
|
+
return `Popup ${popupCount}`;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Convert "centered: true" key into concrete left and top arguments
|
|
35
|
+
* Both can be overwritten
|
|
36
|
+
*
|
|
37
|
+
* @param {Object} options
|
|
38
|
+
* @return {Object}
|
|
39
|
+
*/
|
|
40
|
+
function optionsResolveCentered(options) {
|
|
41
|
+
if (options.width === undefined || options.height === undefined) {
|
|
42
|
+
throw new Error('Should have width and height');
|
|
43
|
+
}
|
|
44
|
+
const result = { ...options };
|
|
45
|
+
const width = window.outerWidth - options.width;
|
|
46
|
+
const height = window.outerHeight - options.height;
|
|
47
|
+
if (options.centered === true) {
|
|
48
|
+
result.left = options.left ?? Math.round(window.screenX + width / 2);
|
|
49
|
+
result.top = options.top ?? Math.round(window.screenY + height / 2.5);
|
|
50
|
+
delete result.centered;
|
|
51
|
+
}
|
|
52
|
+
return result;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Create a form element, add hidden inputs for all the post data
|
|
56
|
+
* and post it into a newly opened popup
|
|
57
|
+
*
|
|
58
|
+
* @param {String} url
|
|
59
|
+
* @param {Object} postData
|
|
60
|
+
* @param {String} name
|
|
61
|
+
* @param {Object} options
|
|
62
|
+
* @return {Object}
|
|
63
|
+
*/
|
|
64
|
+
function openPopupWithPost(url, postData, name, options) {
|
|
65
|
+
const form = document.createElement('form');
|
|
66
|
+
let win = null;
|
|
67
|
+
form.setAttribute('method', 'post');
|
|
68
|
+
form.setAttribute('action', url);
|
|
69
|
+
form.setAttribute('target', name);
|
|
70
|
+
Object.keys(postData).forEach(function addFormItem(key) {
|
|
71
|
+
const input = document.createElement('input');
|
|
72
|
+
input.type = 'hidden';
|
|
73
|
+
input.name = key;
|
|
74
|
+
input.value = postData[key];
|
|
75
|
+
form.appendChild(input);
|
|
76
|
+
});
|
|
77
|
+
document.body.appendChild(form);
|
|
78
|
+
win = window.open('/', name, options);
|
|
79
|
+
win?.document.write('Loading...');
|
|
80
|
+
form.submit();
|
|
81
|
+
document.body.removeChild(form);
|
|
82
|
+
return win;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Open a popup using the first argument. Wait for it to close.
|
|
86
|
+
* Returns the window object
|
|
87
|
+
*
|
|
88
|
+
* @param {Function}
|
|
89
|
+
* @param {String}
|
|
90
|
+
* @param {String}
|
|
91
|
+
* @param {Object}
|
|
92
|
+
* @param {Function}
|
|
93
|
+
* @return {Object}
|
|
94
|
+
*/
|
|
95
|
+
function popupExecute(execute, url, name, options, callback) {
|
|
96
|
+
const popupName = name ?? defaultPopupName();
|
|
97
|
+
const popupOptions = optionsResolveCentered(Object.assign({}, defaultOptions, options));
|
|
98
|
+
const popupCallback = callback ??
|
|
99
|
+
function noop() {
|
|
100
|
+
//
|
|
101
|
+
};
|
|
102
|
+
const optionsString = optionsToString(popupOptions);
|
|
103
|
+
const win = execute(url, popupName, optionsString);
|
|
104
|
+
let isMessageSent = false;
|
|
105
|
+
let interval;
|
|
106
|
+
function popupCallbackOnce(err, data) {
|
|
107
|
+
if (!isMessageSent) {
|
|
108
|
+
isMessageSent = true;
|
|
109
|
+
popupCallback(err, data);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
function onMessage(message) {
|
|
113
|
+
const data = message !== undefined ? message.data : undefined;
|
|
114
|
+
if (data !== undefined && data.id === popupName) {
|
|
115
|
+
popupCallbackOnce(undefined, data.data);
|
|
116
|
+
window.removeEventListener('message', onMessage);
|
|
117
|
+
clearInterval(interval);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
window.addEventListener('message', onMessage, false);
|
|
121
|
+
if (win !== null) {
|
|
122
|
+
interval = window.setInterval(function closePopupCallback() {
|
|
123
|
+
if (win === null || win.closed) {
|
|
124
|
+
setTimeout(function delayWindowClosing() {
|
|
125
|
+
clearInterval(interval);
|
|
126
|
+
popupCallbackOnce(new Error('Popup closed'));
|
|
127
|
+
}, 500);
|
|
128
|
+
}
|
|
129
|
+
}, 100);
|
|
130
|
+
}
|
|
131
|
+
else {
|
|
132
|
+
popupCallbackOnce(new Error('Popup blocked'));
|
|
133
|
+
}
|
|
134
|
+
return win;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Open a popup using the first argument.
|
|
138
|
+
* Wait for it to close and call the callback.
|
|
139
|
+
* Set the options string using the options object
|
|
140
|
+
* Returns the window object
|
|
141
|
+
*
|
|
142
|
+
* @param {String} url
|
|
143
|
+
* @param {String} name
|
|
144
|
+
* @param {Object} options
|
|
145
|
+
* @param {Function} callback
|
|
146
|
+
* @return {Object}
|
|
147
|
+
*/
|
|
148
|
+
export function popup(url, name, options, callback) {
|
|
149
|
+
return popupExecute(window.open, url, name, options, callback);
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Open a popup using the first argument.
|
|
153
|
+
* Post the data into the open popup.
|
|
154
|
+
* Wait for it to close and call the callback.
|
|
155
|
+
* Set the options string using the options object
|
|
156
|
+
* Returns the window object
|
|
157
|
+
*
|
|
158
|
+
* @param {String} url
|
|
159
|
+
* @param {Object} postData
|
|
160
|
+
* @param {String} name
|
|
161
|
+
* @param {Object} options
|
|
162
|
+
* @param {Function} callback
|
|
163
|
+
* @return {Object}
|
|
164
|
+
*/
|
|
165
|
+
export function popupWithPost(url, postData, name, options, callback) {
|
|
166
|
+
function openWithPostData(popupUrl, popupName, optionsString) {
|
|
167
|
+
return openPopupWithPost(popupUrl, postData, popupName, optionsString);
|
|
168
|
+
}
|
|
169
|
+
return popupExecute(openWithPostData, url, name, options, callback);
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Return html that when executed, will trigger the popup to callback with a response
|
|
173
|
+
*
|
|
174
|
+
* @param {Object}
|
|
175
|
+
* @return {String}
|
|
176
|
+
*/
|
|
177
|
+
export function popupResponse(id, data) {
|
|
178
|
+
const jsonData = JSON.stringify({ id, data });
|
|
179
|
+
return `<script>window.opener.postMessage(${jsonData}, "*");setTimeout(function() { window.close(); }, 50);</script>`;
|
|
180
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export const promiseAllLimit = async (n, list) => {
|
|
2
|
+
const head = list.slice(0, n);
|
|
3
|
+
const tail = list.slice(n);
|
|
4
|
+
const result = [];
|
|
5
|
+
const execute = async (promise, i, runNext) => {
|
|
6
|
+
result[i] = await promise();
|
|
7
|
+
await runNext();
|
|
8
|
+
};
|
|
9
|
+
const runNext = async () => {
|
|
10
|
+
const i = list.length - tail.length;
|
|
11
|
+
const promise = tail.shift();
|
|
12
|
+
if (promise !== undefined) {
|
|
13
|
+
await execute(promise, i, runNext);
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
await Promise.all(head.map(async (promise, i) => {
|
|
17
|
+
await execute(promise, i, runNext);
|
|
18
|
+
}));
|
|
19
|
+
return result;
|
|
20
|
+
};
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Create persistent or temporary client-side storage with a portable,
|
|
3
|
+
* type-safe wrapper around the [Web Storage API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API).
|
|
4
|
+
* @param key The key used to access the stored value.
|
|
5
|
+
* @param [session] Save data in sessionStorage (`true`) or in localStorage
|
|
6
|
+
* (`false`). Default is `false`.
|
|
7
|
+
* @example
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* import storeBuddy from "store-buddy";
|
|
11
|
+
*
|
|
12
|
+
* // "foo" is the key used to access the stored value, which is created in the
|
|
13
|
+
* // method `init`
|
|
14
|
+
* const storage1 = storeBuddy("foo").init("bar");
|
|
15
|
+
*
|
|
16
|
+
* // Using sessionStorage instead of localStorage is possible by specifying
|
|
17
|
+
* // `true` in the `session` parameter
|
|
18
|
+
* const storage2 = storeBuddy("foo", true).init("bar");
|
|
19
|
+
*
|
|
20
|
+
* // Type safety can be enabled by providing an argument to the type parameter
|
|
21
|
+
* type Data = {
|
|
22
|
+
* hello: string;
|
|
23
|
+
* world: number;
|
|
24
|
+
* }
|
|
25
|
+
*
|
|
26
|
+
* const storage3 = storeBuddy<Data>("foo").init({
|
|
27
|
+
* hello: "foo",
|
|
28
|
+
* world: 123
|
|
29
|
+
* })
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export function storeBuddy(key, session = false) {
|
|
33
|
+
return {
|
|
34
|
+
init(initialData) {
|
|
35
|
+
const sessionOrLocal = session ? sessionStorage : localStorage;
|
|
36
|
+
if (sessionOrLocal.getItem(key) === null) {
|
|
37
|
+
sessionOrLocal.setItem(key, JSON.stringify(initialData));
|
|
38
|
+
}
|
|
39
|
+
return {
|
|
40
|
+
load() {
|
|
41
|
+
const raw = sessionOrLocal.getItem(key);
|
|
42
|
+
if (raw === null) {
|
|
43
|
+
throw new Error('store-buddy: data has not been set or cannot be recognised');
|
|
44
|
+
}
|
|
45
|
+
const data = JSON.parse(raw);
|
|
46
|
+
return data;
|
|
47
|
+
},
|
|
48
|
+
save: (data) => {
|
|
49
|
+
sessionOrLocal.setItem(key, JSON.stringify(data));
|
|
50
|
+
},
|
|
51
|
+
reset: () => {
|
|
52
|
+
sessionOrLocal.setItem(key, JSON.stringify(initialData));
|
|
53
|
+
},
|
|
54
|
+
clear: () => {
|
|
55
|
+
sessionOrLocal.removeItem(key);
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export const sleep = async (ms) => {
|
|
2
|
+
await new Promise((resolve) => setTimeout(resolve, ms));
|
|
3
|
+
};
|
|
4
|
+
const calc = (m) => (n) => Math.round(n * m);
|
|
5
|
+
export const ms = {
|
|
6
|
+
seconds: calc(1e3),
|
|
7
|
+
minutes: calc(6e4),
|
|
8
|
+
hours: calc(36e5),
|
|
9
|
+
days: calc(864e5),
|
|
10
|
+
weeks: calc(6048e5),
|
|
11
|
+
months: calc(26298e5),
|
|
12
|
+
years: calc(315576e5),
|
|
13
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { workerMd5Script } from './workerCodes.js';
|
|
2
|
+
export async function md5(data) {
|
|
3
|
+
return await new Promise((resolve, reject) => {
|
|
4
|
+
const worker = new Worker(URL.createObjectURL(new Blob([workerMd5Script], { type: 'text/javascript' })));
|
|
5
|
+
worker.addEventListener('error', reject);
|
|
6
|
+
worker.addEventListener('messageerror', reject);
|
|
7
|
+
worker.addEventListener('message', ({ data }) => {
|
|
8
|
+
if (data.event === 'md5-result') {
|
|
9
|
+
worker.terminate();
|
|
10
|
+
resolve(data.data);
|
|
11
|
+
}
|
|
12
|
+
});
|
|
13
|
+
worker.postMessage({
|
|
14
|
+
event: 'md5',
|
|
15
|
+
data,
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { workerSodiumScript } from './workerCodes.js';
|
|
2
|
+
// const ensureNonDetachedUniqueBuffers = (
|
|
3
|
+
// values: Transferable[]
|
|
4
|
+
// ): Set<Transferable> =>
|
|
5
|
+
// new Set(
|
|
6
|
+
// values.filter(v => {
|
|
7
|
+
// if (v instanceof ArrayBuffer) {
|
|
8
|
+
// return v.byteLength === 0;
|
|
9
|
+
// } else {
|
|
10
|
+
// return true;
|
|
11
|
+
// }
|
|
12
|
+
// })
|
|
13
|
+
// );
|
|
14
|
+
export async function encrypt(key, dataToEncrypt, progress, signal) {
|
|
15
|
+
return await new Promise((resolve, reject) => {
|
|
16
|
+
void progress?.({
|
|
17
|
+
current: 0,
|
|
18
|
+
percent: 0,
|
|
19
|
+
total: dataToEncrypt.byteLength,
|
|
20
|
+
});
|
|
21
|
+
const worker = new Worker(URL.createObjectURL(new Blob([workerSodiumScript], { type: 'text/javascript' })));
|
|
22
|
+
worker.addEventListener('error', reject);
|
|
23
|
+
worker.addEventListener('messageerror', reject);
|
|
24
|
+
worker.addEventListener('message', ({ data }) => {
|
|
25
|
+
switch (data.event) {
|
|
26
|
+
case 'ready': {
|
|
27
|
+
const postData = {
|
|
28
|
+
event: 'encrypt',
|
|
29
|
+
data: dataToEncrypt,
|
|
30
|
+
key,
|
|
31
|
+
};
|
|
32
|
+
worker.postMessage(postData, {
|
|
33
|
+
transfer: [postData.data.buffer],
|
|
34
|
+
});
|
|
35
|
+
break;
|
|
36
|
+
}
|
|
37
|
+
case 'encrypt-progress': {
|
|
38
|
+
if (signal?.aborted === true) {
|
|
39
|
+
const abortError = new Error('Aborted');
|
|
40
|
+
abortError.name = 'AbortError';
|
|
41
|
+
worker.terminate();
|
|
42
|
+
reject(abortError);
|
|
43
|
+
}
|
|
44
|
+
void progress?.(data.data);
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
case 'encrypt-result': {
|
|
48
|
+
worker.terminate();
|
|
49
|
+
resolve(data.data);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
export async function decrypt(key, dataToDecrypt, progress, signal) {
|
|
56
|
+
return await new Promise((resolve, reject) => {
|
|
57
|
+
void progress?.({
|
|
58
|
+
current: 0,
|
|
59
|
+
percent: 0,
|
|
60
|
+
total: dataToDecrypt.byteLength,
|
|
61
|
+
});
|
|
62
|
+
const worker = new Worker(URL.createObjectURL(new Blob([workerSodiumScript], { type: 'text/javascript' })));
|
|
63
|
+
worker.addEventListener('error', reject);
|
|
64
|
+
worker.addEventListener('messageerror', reject);
|
|
65
|
+
worker.addEventListener('message', ({ data }) => {
|
|
66
|
+
switch (data.event) {
|
|
67
|
+
case 'ready': {
|
|
68
|
+
const postData = {
|
|
69
|
+
event: 'decrypt',
|
|
70
|
+
key,
|
|
71
|
+
data: dataToDecrypt,
|
|
72
|
+
};
|
|
73
|
+
worker.postMessage(postData, {
|
|
74
|
+
transfer: [postData.data.buffer],
|
|
75
|
+
});
|
|
76
|
+
break;
|
|
77
|
+
}
|
|
78
|
+
case 'decrypt-progress': {
|
|
79
|
+
if (signal?.aborted === true) {
|
|
80
|
+
const abortError = new Error('Aborted');
|
|
81
|
+
abortError.name = 'AbortError';
|
|
82
|
+
worker.terminate();
|
|
83
|
+
reject(abortError);
|
|
84
|
+
}
|
|
85
|
+
void progress?.(data.data);
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
case 'decrypt-result': {
|
|
89
|
+
worker.terminate();
|
|
90
|
+
resolve(data.data);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
}
|
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
export const workerSodiumScript = `let sodium;
|
|
2
|
+
|
|
3
|
+
self.sodium = {
|
|
4
|
+
onload: (sod) => {
|
|
5
|
+
sodium = sod
|
|
6
|
+
postMessage({ event: "ready" })
|
|
7
|
+
}
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
importScripts('https://cdn.jsdelivr.net/gh/jedisct1/libsodium.js@master/dist/browsers/sodium.js');
|
|
11
|
+
importScripts('https://cdnjs.cloudflare.com/ajax/libs/spark-md5/3.0.0/spark-md5.min.js');
|
|
12
|
+
|
|
13
|
+
function* chunks(arr, n) {
|
|
14
|
+
for (let i = 0; i < arr.length; i += n) {
|
|
15
|
+
yield arr.slice(i, i + n);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function assert(c, message) {
|
|
20
|
+
if (!c) {
|
|
21
|
+
throw new Error(message);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function encrypt(key) {
|
|
26
|
+
let destroyed = false;
|
|
27
|
+
const {
|
|
28
|
+
state,
|
|
29
|
+
header
|
|
30
|
+
} = sodium.crypto_secretstream_xchacha20poly1305_init_push(key);
|
|
31
|
+
|
|
32
|
+
const encrypt = (tag, plaintext) => {
|
|
33
|
+
assert(destroyed === false, "state already destroyed");
|
|
34
|
+
|
|
35
|
+
return sodium.crypto_secretstream_xchacha20poly1305_push(
|
|
36
|
+
state,
|
|
37
|
+
plaintext,
|
|
38
|
+
null,
|
|
39
|
+
tag
|
|
40
|
+
);
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
function destroy() {
|
|
44
|
+
assert(destroyed === false, "state already destroyed");
|
|
45
|
+
destroyed = true;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return {
|
|
49
|
+
encrypt,
|
|
50
|
+
destroy,
|
|
51
|
+
header
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function decrypt(header, key) {
|
|
56
|
+
assert(
|
|
57
|
+
header.byteLength >=
|
|
58
|
+
sodium.crypto_secretstream_xchacha20poly1305_HEADERBYTES,
|
|
59
|
+
"header must be at least HEADERBYTES (" + sodium.crypto_secretstream_xchacha20poly1305_HEADERBYTES + ") long"
|
|
60
|
+
);
|
|
61
|
+
assert(
|
|
62
|
+
key.byteLength >= sodium.crypto_secretstream_xchacha20poly1305_KEYBYTES,
|
|
63
|
+
"key must be at least KEYBYTES (" + sodium.crypto_secretstream_xchacha20poly1305_KEYBYTES + ") long"
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
let destroyed = false;
|
|
67
|
+
const state = sodium.crypto_secretstream_xchacha20poly1305_init_pull(
|
|
68
|
+
header,
|
|
69
|
+
key
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
const decrypt = ciphertext => {
|
|
73
|
+
assert(destroyed === false, "state already destroyed");
|
|
74
|
+
|
|
75
|
+
return sodium.crypto_secretstream_xchacha20poly1305_pull(state, ciphertext);
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
function destroy() {
|
|
79
|
+
assert(destroyed === false, "state already destroyed");
|
|
80
|
+
destroyed = true;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return {
|
|
84
|
+
decrypt,
|
|
85
|
+
destroy
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const CHUNK_SIZE = 8192;
|
|
90
|
+
|
|
91
|
+
async function encryptSecretstream(key, data, progress) {
|
|
92
|
+
const { encrypt: crypt, destroy, header } = encrypt(key);
|
|
93
|
+
const cryptedChunk =
|
|
94
|
+
CHUNK_SIZE + sodium.crypto_secretstream_xchacha20poly1305_ABYTES;
|
|
95
|
+
const max =
|
|
96
|
+
Math.ceil(data.byteLength / CHUNK_SIZE) * cryptedChunk + header.byteLength;
|
|
97
|
+
|
|
98
|
+
progress?.({
|
|
99
|
+
percent: 0,
|
|
100
|
+
total: max,
|
|
101
|
+
current: 0
|
|
102
|
+
});
|
|
103
|
+
const final = new Uint8Array(max);
|
|
104
|
+
const sparkEncrypted = new SparkMD5.ArrayBuffer();
|
|
105
|
+
const spark = new SparkMD5.ArrayBuffer();
|
|
106
|
+
|
|
107
|
+
final.set(header);
|
|
108
|
+
sparkEncrypted.append(header);
|
|
109
|
+
let total = header.byteLength;
|
|
110
|
+
progress?.({
|
|
111
|
+
percent: total / max,
|
|
112
|
+
total: max,
|
|
113
|
+
current: total
|
|
114
|
+
});
|
|
115
|
+
let lastPercent = total / max;
|
|
116
|
+
|
|
117
|
+
for (const chunk of chunks(data, CHUNK_SIZE)) {
|
|
118
|
+
spark.append(chunk);
|
|
119
|
+
const tag =
|
|
120
|
+
chunk.length < CHUNK_SIZE
|
|
121
|
+
? sodium.crypto_secretstream_xchacha20poly1305_TAG_FINAL
|
|
122
|
+
: sodium.crypto_secretstream_xchacha20poly1305_TAG_MESSAGE;
|
|
123
|
+
const crypted = crypt(tag, chunk);
|
|
124
|
+
sparkEncrypted.append(crypted);
|
|
125
|
+
final.set(crypted, total);
|
|
126
|
+
total += crypted.byteLength;
|
|
127
|
+
const percent = total / max;
|
|
128
|
+
if (percent > lastPercent + 0.01) {
|
|
129
|
+
progress?.({
|
|
130
|
+
percent,
|
|
131
|
+
total: max,
|
|
132
|
+
current: total
|
|
133
|
+
});
|
|
134
|
+
lastPercent = percent;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
destroy();
|
|
139
|
+
progress?.({
|
|
140
|
+
percent: 1,
|
|
141
|
+
total,
|
|
142
|
+
current: total
|
|
143
|
+
});
|
|
144
|
+
return {
|
|
145
|
+
data: final.slice(0, total),
|
|
146
|
+
md5Encrypted: sparkEncrypted.end(),
|
|
147
|
+
md5: spark.end()
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
async function decryptSecretstream(key, data, progress) {
|
|
152
|
+
const header = data.slice(
|
|
153
|
+
0,
|
|
154
|
+
sodium.crypto_secretstream_xchacha20poly1305_HEADERBYTES
|
|
155
|
+
);
|
|
156
|
+
data = data.slice(sodium.crypto_secretstream_xchacha20poly1305_HEADERBYTES);
|
|
157
|
+
|
|
158
|
+
const { decrypt: decryptt, destroy } = decrypt(header, key);
|
|
159
|
+
const chunkSize =
|
|
160
|
+
CHUNK_SIZE + sodium.crypto_secretstream_xchacha20poly1305_ABYTES;
|
|
161
|
+
const max = Math.ceil(data.byteLength / chunkSize) * CHUNK_SIZE;
|
|
162
|
+
|
|
163
|
+
progress?.({
|
|
164
|
+
percent: 0,
|
|
165
|
+
total: max,
|
|
166
|
+
current: 0
|
|
167
|
+
});
|
|
168
|
+
const final = new Uint8Array(max);
|
|
169
|
+
let total = 0;
|
|
170
|
+
let lastPercent = total / max;
|
|
171
|
+
|
|
172
|
+
for (const chunk of chunks(data, chunkSize)) {
|
|
173
|
+
const tmp = decryptt(chunk);
|
|
174
|
+
final.set(tmp.message, total);
|
|
175
|
+
total += tmp.message.byteLength;
|
|
176
|
+
|
|
177
|
+
const percent = total / max;
|
|
178
|
+
if (percent > lastPercent + 0.01) {
|
|
179
|
+
progress?.({
|
|
180
|
+
percent,
|
|
181
|
+
total: max,
|
|
182
|
+
current: total
|
|
183
|
+
});
|
|
184
|
+
lastPercent = percent;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
destroy();
|
|
189
|
+
progress?.({
|
|
190
|
+
percent: 1,
|
|
191
|
+
total,
|
|
192
|
+
current: total
|
|
193
|
+
});
|
|
194
|
+
return final.slice(0, total);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
self.onmessage = async ({ data }) => {
|
|
198
|
+
switch (data.event) {
|
|
199
|
+
case "encrypt": {
|
|
200
|
+
postMessage({ event: "working" })
|
|
201
|
+
postMessage({
|
|
202
|
+
event: "encrypt-result",
|
|
203
|
+
data: await encryptSecretstream(data.key, data.data, progress => postMessage({
|
|
204
|
+
event: "encrypt-progress",
|
|
205
|
+
data: progress
|
|
206
|
+
}))
|
|
207
|
+
});
|
|
208
|
+
postMessage({ event: "ready" })
|
|
209
|
+
break;
|
|
210
|
+
}
|
|
211
|
+
case "decrypt": {
|
|
212
|
+
postMessage({ event: "working" })
|
|
213
|
+
postMessage({
|
|
214
|
+
event: "decrypt-result",
|
|
215
|
+
data: await decryptSecretstream(data.key, data.data, progress => postMessage({
|
|
216
|
+
event: "decrypt-progress",
|
|
217
|
+
data: progress
|
|
218
|
+
}))
|
|
219
|
+
});
|
|
220
|
+
postMessage({ event: "ready" })
|
|
221
|
+
break;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}`;
|
|
225
|
+
export const workerMd5Script = `importScripts('https://cdnjs.cloudflare.com/ajax/libs/spark-md5/3.0.0/spark-md5.min.js');
|
|
226
|
+
|
|
227
|
+
function* chunks(arr, n) {
|
|
228
|
+
for (let i = 0; i < arr.length; i += n) {
|
|
229
|
+
yield arr.slice(i, i + n);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
const CHUNK_SIZE = 8192;
|
|
234
|
+
|
|
235
|
+
function md5(data) {
|
|
236
|
+
const spark = new SparkMD5.ArrayBuffer();
|
|
237
|
+
for (const chunk of chunks(data, CHUNK_SIZE)) {
|
|
238
|
+
spark.append(chunk);
|
|
239
|
+
}
|
|
240
|
+
return spark.end();
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
self.onmessage = ({ data }) => {
|
|
245
|
+
switch (data.event) {
|
|
246
|
+
case "md5": {
|
|
247
|
+
postMessage({
|
|
248
|
+
event: "md5-result",
|
|
249
|
+
data: md5(data.data)
|
|
250
|
+
});
|
|
251
|
+
break;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}`;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { type ApiClient, type RouterOutputs, type RouterInputs } from './client.js';
|
|
2
|
+
import { type InfuraNetwork, type PublicUser } from './index.js';
|
|
3
|
+
import { type SelfUser } from './client/types/user.js';
|
|
4
|
+
export declare class BaseClient {
|
|
5
|
+
#private;
|
|
6
|
+
static readonly getBaseClient: (session?: string | null | undefined, onAccessDenied?: () => void) => ApiClient;
|
|
7
|
+
protected client: ApiClient;
|
|
8
|
+
sessionId: string;
|
|
9
|
+
constructor(session: string);
|
|
10
|
+
logout(sessionId?: string | null | undefined): Promise<void>;
|
|
11
|
+
me(): Promise<SelfUser>;
|
|
12
|
+
static getUser(userId: string, sessionId?: string | null | undefined): Promise<PublicUser>;
|
|
13
|
+
getUser(userId: string): Promise<PublicUser>;
|
|
14
|
+
searchUsers(search: string): Promise<PublicUser[]>;
|
|
15
|
+
updateProfile(data: RouterInputs['user']['updateProfile']): Promise<SelfUser>;
|
|
16
|
+
static isCryptoTransactionDone({ idOrHash, network, }: {
|
|
17
|
+
idOrHash: string;
|
|
18
|
+
network?: InfuraNetwork;
|
|
19
|
+
}): Promise<boolean>;
|
|
20
|
+
reportUser(data: RouterInputs['report']['create']): Promise<RouterOutputs['report']['create']>;
|
|
21
|
+
getSponsorshipLink({ backUrl, }: {
|
|
22
|
+
backUrl: string;
|
|
23
|
+
}): Promise<string | null>;
|
|
24
|
+
static getPaymentRequest: ({ paymentRequestId, secrecyIdSeller, }: {
|
|
25
|
+
paymentRequestId: string;
|
|
26
|
+
secrecyIdSeller: string;
|
|
27
|
+
}) => Promise<RouterOutputs['stripe']['paymentRequestToPay']>;
|
|
28
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { InternalNode, InternalFile, InternalNodeFull } from './client/types/index.js';
|
|
2
|
+
export declare const filesCache: Map<string, InternalFile>;
|
|
3
|
+
export declare const nodesCache: Map<string, InternalNode | InternalNodeFull>;
|
|
4
|
+
export declare const usersCache: Map<string, {
|
|
5
|
+
id: string;
|
|
6
|
+
lastname: string;
|
|
7
|
+
firstname: string;
|
|
8
|
+
avatar: string | null;
|
|
9
|
+
isSearchable: boolean;
|
|
10
|
+
}>;
|
|
11
|
+
export declare const publicKeysCache: Map<string, string>;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { SecrecyClient, KeyPair } from '../index.js';
|
|
2
|
+
import type { ApiClient, RouterInputs, RouterOutputs } from '../client.js';
|
|
3
|
+
export declare class SecrecyCareClient {
|
|
4
|
+
#private;
|
|
5
|
+
constructor(client: SecrecyClient, keys: KeyPair, apiClient: ApiClient);
|
|
6
|
+
getProfessional(input: RouterInputs['care']['professional']['get']): Promise<RouterOutputs['care']['professional']['get']>;
|
|
7
|
+
listProfessionals(input: RouterInputs['care']['professional']['list']): Promise<RouterOutputs['care']['professional']['list']>;
|
|
8
|
+
registerProfessional(input: RouterInputs['care']['professional']['register']): Promise<RouterOutputs['care']['professional']['register']>;
|
|
9
|
+
}
|