@evolu/common 6.0.1-preview.2 → 6.0.1-preview.21
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/dist/src/Assert.d.ts +6 -3
- package/dist/src/Assert.d.ts.map +1 -1
- package/dist/src/Assert.js +7 -4
- package/dist/src/Brand.d.ts +75 -0
- package/dist/src/Brand.d.ts.map +1 -0
- package/dist/src/Brand.js +1 -0
- package/dist/src/Buffer.d.ts +1 -1
- package/dist/src/Buffer.d.ts.map +1 -1
- package/dist/src/Buffer.js +1 -1
- package/dist/src/CallbackRegistry.d.ts +53 -0
- package/dist/src/CallbackRegistry.d.ts.map +1 -0
- package/dist/src/CallbackRegistry.js +25 -0
- package/dist/src/Console.d.ts +31 -6
- package/dist/src/Console.d.ts.map +1 -1
- package/dist/src/Console.js +72 -9
- package/dist/src/Crypto.d.ts +61 -34
- package/dist/src/Crypto.d.ts.map +1 -1
- package/dist/src/Crypto.js +29 -42
- package/dist/src/Evolu/Db.d.ts +159 -66
- package/dist/src/Evolu/Db.d.ts.map +1 -1
- package/dist/src/Evolu/Db.js +300 -705
- package/dist/src/Evolu/Diff.d.ts +3 -3
- package/dist/src/Evolu/Diff.d.ts.map +1 -1
- package/dist/src/Evolu/Diff.js +7 -5
- package/dist/src/Evolu/Evolu.d.ts +200 -115
- package/dist/src/Evolu/Evolu.d.ts.map +1 -1
- package/dist/src/Evolu/Evolu.js +287 -157
- package/dist/src/Evolu/Internal.d.ts +0 -2
- package/dist/src/Evolu/Internal.d.ts.map +1 -1
- package/dist/src/Evolu/Internal.js +0 -2
- package/dist/src/Evolu/LocalAuth.d.ts +144 -0
- package/dist/src/Evolu/LocalAuth.d.ts.map +1 -0
- package/dist/src/Evolu/LocalAuth.js +171 -0
- package/dist/src/Evolu/Owner.d.ts +209 -124
- package/dist/src/Evolu/Owner.d.ts.map +1 -1
- package/dist/src/Evolu/Owner.js +118 -104
- package/dist/src/Evolu/Platform.d.ts +9 -7
- package/dist/src/Evolu/Platform.d.ts.map +1 -1
- package/dist/src/Evolu/Protocol.d.ts +256 -211
- package/dist/src/Evolu/Protocol.d.ts.map +1 -1
- package/dist/src/Evolu/Protocol.js +558 -359
- package/dist/src/Evolu/Public.d.ts +6 -8
- package/dist/src/Evolu/Public.d.ts.map +1 -1
- package/dist/src/Evolu/Public.js +2 -3
- package/dist/src/Evolu/PublicKysely.js +3 -3
- package/dist/src/Evolu/Query.d.ts +2 -1
- package/dist/src/Evolu/Query.d.ts.map +1 -1
- package/dist/src/Evolu/Relay.d.ts +72 -4
- package/dist/src/Evolu/Relay.d.ts.map +1 -1
- package/dist/src/Evolu/Relay.js +124 -13
- package/dist/src/Evolu/Schema.d.ts +129 -73
- package/dist/src/Evolu/Schema.d.ts.map +1 -1
- package/dist/src/Evolu/Schema.js +169 -89
- package/dist/src/Evolu/Storage.d.ts +159 -14
- package/dist/src/Evolu/Storage.d.ts.map +1 -1
- package/dist/src/Evolu/Storage.js +42 -32
- package/dist/src/Evolu/Sync.d.ts +68 -13
- package/dist/src/Evolu/Sync.d.ts.map +1 -1
- package/dist/src/Evolu/Sync.js +414 -20
- package/dist/src/Evolu/Timestamp.d.ts +35 -27
- package/dist/src/Evolu/Timestamp.d.ts.map +1 -1
- package/dist/src/Evolu/Timestamp.js +27 -18
- package/dist/src/ManyToManyMap.d.ts +74 -10
- package/dist/src/ManyToManyMap.d.ts.map +1 -1
- package/dist/src/ManyToManyMap.js +41 -6
- package/dist/src/Number.d.ts +2 -1
- package/dist/src/Number.d.ts.map +1 -1
- package/dist/src/Random.d.ts +3 -2
- package/dist/src/Random.d.ts.map +1 -1
- package/dist/src/RefCountedResourceManager.d.ts +119 -0
- package/dist/src/RefCountedResourceManager.d.ts.map +1 -0
- package/dist/src/RefCountedResourceManager.js +197 -0
- package/dist/src/Result.d.ts +176 -51
- package/dist/src/Result.d.ts.map +1 -1
- package/dist/src/Result.js +30 -241
- package/dist/src/Sqlite.d.ts +21 -4
- package/dist/src/Sqlite.d.ts.map +1 -1
- package/dist/src/Sqlite.js +50 -8
- package/dist/src/Task.d.ts +511 -0
- package/dist/src/Task.d.ts.map +1 -0
- package/dist/src/Task.js +410 -0
- package/dist/src/Time.d.ts +59 -0
- package/dist/src/Time.d.ts.map +1 -1
- package/dist/src/Time.js +87 -4
- package/dist/src/Type.d.ts +474 -327
- package/dist/src/Type.d.ts.map +1 -1
- package/dist/src/Type.js +502 -436
- package/dist/src/Types.d.ts +0 -74
- package/dist/src/Types.d.ts.map +1 -1
- package/dist/src/WebSocket.d.ts +5 -2
- package/dist/src/WebSocket.d.ts.map +1 -1
- package/dist/src/WebSocket.js +12 -13
- package/dist/src/Worker.d.ts +39 -11
- package/dist/src/Worker.d.ts.map +1 -1
- package/dist/src/Worker.js +22 -4
- package/dist/src/index.d.ts +3 -3
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +3 -3
- package/package.json +14 -13
- package/src/Assert.ts +8 -7
- package/src/Brand.ts +75 -0
- package/src/Buffer.ts +1 -1
- package/src/CallbackRegistry.ts +84 -0
- package/src/Console.ts +91 -11
- package/src/Crypto.ts +94 -79
- package/src/Evolu/Db.ts +528 -1027
- package/src/Evolu/Diff.ts +7 -5
- package/src/Evolu/Evolu.ts +682 -329
- package/src/Evolu/Internal.ts +0 -2
- package/src/Evolu/LocalAuth.ts +422 -0
- package/src/Evolu/Owner.ts +282 -227
- package/src/Evolu/Platform.ts +9 -9
- package/src/Evolu/Protocol.ts +788 -640
- package/src/Evolu/Public.ts +7 -14
- package/src/Evolu/PublicKysely.ts +3 -3
- package/src/Evolu/Query.ts +2 -1
- package/src/Evolu/Relay.ts +238 -18
- package/src/Evolu/Schema.ts +391 -191
- package/src/Evolu/Storage.ts +275 -55
- package/src/Evolu/Sync.ts +708 -37
- package/src/Evolu/Timestamp.ts +38 -35
- package/src/ManyToManyMap.ts +127 -24
- package/src/Number.ts +2 -6
- package/src/Random.ts +3 -2
- package/src/RefCountedResourceManager.ts +368 -0
- package/src/Result.ts +183 -53
- package/src/Sqlite.ts +59 -23
- package/src/Task.ts +779 -0
- package/src/Time.ts +168 -4
- package/src/Type.ts +708 -660
- package/src/Types.ts +0 -76
- package/src/WebSocket.ts +23 -17
- package/src/Worker.ts +72 -23
- package/src/index.ts +3 -3
- package/dist/src/Callbacks.d.ts +0 -20
- package/dist/src/Callbacks.d.ts.map +0 -1
- package/dist/src/Callbacks.js +0 -18
- package/dist/src/Evolu/Config.d.ts +0 -69
- package/dist/src/Evolu/Config.d.ts.map +0 -1
- package/dist/src/Evolu/Config.js +0 -9
- package/dist/src/Evolu/Kysely.d.ts +0 -6
- package/dist/src/Evolu/Kysely.d.ts.map +0 -1
- package/dist/src/Evolu/Kysely.js +0 -21
- package/dist/src/NanoId.d.ts +0 -27
- package/dist/src/NanoId.d.ts.map +0 -1
- package/dist/src/NanoId.js +0 -6
- package/dist/src/Promise.d.ts +0 -180
- package/dist/src/Promise.d.ts.map +0 -1
- package/dist/src/Promise.js +0 -176
- package/src/Callbacks.ts +0 -43
- package/src/Evolu/Config.ts +0 -83
- package/src/Evolu/Kysely.ts +0 -38
- package/src/NanoId.ts +0 -39
- package/src/Promise.ts +0 -295
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { Brand } from "./Brand.js";
|
|
2
|
+
import { RandomBytesDep } from "./Crypto.js";
|
|
3
|
+
import { Result } from "./Result.js";
|
|
4
|
+
import { createId, Id } from "./Type.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A registry for one-time callback functions.
|
|
8
|
+
*
|
|
9
|
+
* Stores callbacks with unique IDs and executes them once with an optional
|
|
10
|
+
* argument. Executed callbacks are automatically removed from the registry.
|
|
11
|
+
*
|
|
12
|
+
* This is useful for correlating asynchronous operations across boundaries
|
|
13
|
+
* where callback functions cannot be passed directly (e.g., web workers).
|
|
14
|
+
*
|
|
15
|
+
* The `execute` method intentionally does not use try-catch or {@link Result}
|
|
16
|
+
* because it's the callback's responsibility to handle its own errors. The
|
|
17
|
+
* registry is just a correlation mechanism and should not interfere with error
|
|
18
|
+
* handling or debugging by masking the original error location.
|
|
19
|
+
*
|
|
20
|
+
* ### Example
|
|
21
|
+
*
|
|
22
|
+
* ```ts
|
|
23
|
+
* // No-argument callbacks
|
|
24
|
+
* const registry = createCallbackRegistry(deps);
|
|
25
|
+
* const id = registry.register(() => console.log("called"));
|
|
26
|
+
* registry.execute(id);
|
|
27
|
+
*
|
|
28
|
+
* // With argument callbacks
|
|
29
|
+
* const stringRegistry = createCallbackRegistry<string>(deps);
|
|
30
|
+
* const id = stringRegistry.register((value) => {
|
|
31
|
+
* console.log(value);
|
|
32
|
+
* });
|
|
33
|
+
* stringRegistry.execute(id, "hello");
|
|
34
|
+
*
|
|
35
|
+
* // Promise.withResolvers pattern
|
|
36
|
+
* const promiseRegistry = createCallbackRegistry<string>(deps);
|
|
37
|
+
* const { promise, resolve } = Promise.withResolvers<string>();
|
|
38
|
+
* const id = promiseRegistry.register(resolve);
|
|
39
|
+
* promiseRegistry.execute(id, "resolved value");
|
|
40
|
+
* await promise; // "resolved value"
|
|
41
|
+
* ```
|
|
42
|
+
*
|
|
43
|
+
* @template T - The type of argument passed to callbacks (defaults to undefined
|
|
44
|
+
* for no-argument callbacks)
|
|
45
|
+
*/
|
|
46
|
+
export interface CallbackRegistry<T = undefined> {
|
|
47
|
+
/** Registers a callback function and returns a unique ID. */
|
|
48
|
+
readonly register: (callback: (arg: T) => void) => CallbackId;
|
|
49
|
+
|
|
50
|
+
/** Executes and removes a callback associated with the given ID. */
|
|
51
|
+
readonly execute: T extends undefined
|
|
52
|
+
? (id: CallbackId) => undefined
|
|
53
|
+
: (id: CallbackId, arg: T) => undefined;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export type CallbackId = Id & Brand<"Callback">;
|
|
57
|
+
|
|
58
|
+
/** Creates a new {@link CallbackRegistry} for one-time callback functions. */
|
|
59
|
+
export const createCallbackRegistry = <T = undefined>(
|
|
60
|
+
deps: RandomBytesDep,
|
|
61
|
+
): CallbackRegistry<T> => {
|
|
62
|
+
const callbackMap = new Map<CallbackId, (arg: T) => void>();
|
|
63
|
+
|
|
64
|
+
return {
|
|
65
|
+
register: (callback) => {
|
|
66
|
+
const id = createId<"Callback">(deps);
|
|
67
|
+
callbackMap.set(id, callback);
|
|
68
|
+
return id;
|
|
69
|
+
},
|
|
70
|
+
|
|
71
|
+
execute: (id: CallbackId, ...args: T extends undefined ? [] : [T]) => {
|
|
72
|
+
const callback = callbackMap.get(id);
|
|
73
|
+
if (callback) {
|
|
74
|
+
callbackMap.delete(id);
|
|
75
|
+
if (args.length === 0) {
|
|
76
|
+
// Called without argument (undefined case)
|
|
77
|
+
(callback as () => void)();
|
|
78
|
+
} else {
|
|
79
|
+
callback(args[0]);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
} as CallbackRegistry<T>;
|
|
84
|
+
};
|
package/src/Console.ts
CHANGED
|
@@ -13,14 +13,28 @@
|
|
|
13
13
|
* ### Example
|
|
14
14
|
*
|
|
15
15
|
* ```ts
|
|
16
|
-
* deps.console.log("[
|
|
16
|
+
* deps.console.log("[evolu]", "createEvoluInstance", { name });
|
|
17
17
|
* ```
|
|
18
18
|
*
|
|
19
19
|
* **Tip**: In browser dev tools, you can filter logs by tag (e.g., `[db]`) to
|
|
20
|
-
* quickly find relevant messages.
|
|
20
|
+
* quickly find relevant messages. In Node.js, use `grep` to filter output:
|
|
21
21
|
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
22
|
+
* ```bash
|
|
23
|
+
* node app.js | grep "\[relay\]" # Show only relay logs
|
|
24
|
+
* node app.js | grep -E "\[db\]|\[sql\]" # Show db and sql logs
|
|
25
|
+
* node app.js | grep -v "\[debug\]" # Hide debug logs
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* Or add to package.json scripts:
|
|
29
|
+
*
|
|
30
|
+
* ```json
|
|
31
|
+
* {
|
|
32
|
+
* "scripts": {
|
|
33
|
+
* "dev:relay": "node app.js | grep \"\\[relay\\]\"",
|
|
34
|
+
* "dev:db": "node app.js | grep -E \"\\[db\\]|\\[sql\\]\""
|
|
35
|
+
* }
|
|
36
|
+
* }
|
|
37
|
+
* ```
|
|
24
38
|
*
|
|
25
39
|
* @module
|
|
26
40
|
*/
|
|
@@ -51,6 +65,9 @@ export interface Console {
|
|
|
51
65
|
/** Starts a timer with an optional label */
|
|
52
66
|
time: (label?: string) => void;
|
|
53
67
|
|
|
68
|
+
/** Logs the elapsed time for a timer without ending it */
|
|
69
|
+
timeLog: (label?: string, ...data: Array<any>) => void;
|
|
70
|
+
|
|
54
71
|
/** Ends a timer and logs the elapsed time */
|
|
55
72
|
timeEnd: (label?: string) => void;
|
|
56
73
|
|
|
@@ -69,9 +86,6 @@ export interface Console {
|
|
|
69
86
|
/** Writes a message if the value is falsy, otherwise does nothing */
|
|
70
87
|
assert: (value: any, message?: string, ...optionalParams: Array<any>) => void;
|
|
71
88
|
|
|
72
|
-
/** Logs the elapsed time for a timer without ending it */
|
|
73
|
-
timeLog: (label?: string, ...data: Array<any>) => void;
|
|
74
|
-
|
|
75
89
|
/** Prints a stack trace with an optional message */
|
|
76
90
|
trace: (message?: any, ...optionalParams: Array<any>) => void;
|
|
77
91
|
}
|
|
@@ -121,6 +135,10 @@ export const createConsole = (config: ConsoleConfig = {}): Console => {
|
|
|
121
135
|
// eslint-disable-next-line no-console
|
|
122
136
|
if (instance.enabled) console.time(label);
|
|
123
137
|
},
|
|
138
|
+
timeLog: (label, ...data) => {
|
|
139
|
+
// eslint-disable-next-line no-console, @typescript-eslint/no-unsafe-argument
|
|
140
|
+
if (instance.enabled) console.timeLog(label, ...data);
|
|
141
|
+
},
|
|
124
142
|
timeEnd: (label) => {
|
|
125
143
|
// eslint-disable-next-line no-console
|
|
126
144
|
if (instance.enabled) console.timeEnd(label);
|
|
@@ -145,10 +163,6 @@ export const createConsole = (config: ConsoleConfig = {}): Console => {
|
|
|
145
163
|
// eslint-disable-next-line no-console, @typescript-eslint/no-unsafe-argument
|
|
146
164
|
if (instance.enabled) console.assert(value, message, ...optionalParams);
|
|
147
165
|
},
|
|
148
|
-
timeLog: (label, ...data) => {
|
|
149
|
-
// eslint-disable-next-line no-console, @typescript-eslint/no-unsafe-argument
|
|
150
|
-
if (instance.enabled) console.timeLog(label, ...data);
|
|
151
|
-
},
|
|
152
166
|
trace: (message, ...optionalParams) => {
|
|
153
167
|
// eslint-disable-next-line no-console, @typescript-eslint/no-unsafe-argument
|
|
154
168
|
if (instance.enabled) console.trace(message, ...optionalParams);
|
|
@@ -157,3 +171,69 @@ export const createConsole = (config: ConsoleConfig = {}): Console => {
|
|
|
157
171
|
|
|
158
172
|
return instance;
|
|
159
173
|
};
|
|
174
|
+
|
|
175
|
+
export interface ConsoleWithTimeConfig extends ConsoleConfig {
|
|
176
|
+
/**
|
|
177
|
+
* Type of timestamp to prepend to log messages.
|
|
178
|
+
*
|
|
179
|
+
* - 'absolute': Shows actual time (e.g., "14:32:15.234")
|
|
180
|
+
* - 'relative': Shows time since console creation (e.g., "+1.234s")
|
|
181
|
+
*/
|
|
182
|
+
readonly timestampType: "absolute" | "relative";
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** Creates a console instance with timestamp prefixes. */
|
|
186
|
+
export const createConsoleWithTime = (
|
|
187
|
+
config: ConsoleWithTimeConfig = { timestampType: "relative" },
|
|
188
|
+
): Console => {
|
|
189
|
+
const console = createConsole(config);
|
|
190
|
+
const startTime = performance.now();
|
|
191
|
+
|
|
192
|
+
const getTimestamp = (): string => {
|
|
193
|
+
if (config.timestampType === "relative") {
|
|
194
|
+
const elapsed = (performance.now() - startTime) / 1000;
|
|
195
|
+
|
|
196
|
+
// Format for better readability at different time scales
|
|
197
|
+
if (elapsed < 60) {
|
|
198
|
+
// Under 1 minute: show seconds with millisecond precision
|
|
199
|
+
return `+${elapsed.toFixed(3)}s`;
|
|
200
|
+
} else if (elapsed < 3600) {
|
|
201
|
+
// 1 minute to 1 hour: show minutes and seconds with millisecond precision
|
|
202
|
+
const minutes = Math.floor(elapsed / 60);
|
|
203
|
+
const seconds = (elapsed % 60).toFixed(3);
|
|
204
|
+
return `+${minutes}m${seconds}s`;
|
|
205
|
+
} else {
|
|
206
|
+
// Over 1 hour: show hours, minutes, and seconds with millisecond precision
|
|
207
|
+
const hours = Math.floor(elapsed / 3600);
|
|
208
|
+
const minutes = Math.floor((elapsed % 3600) / 60);
|
|
209
|
+
const seconds = ((elapsed % 3600) % 60).toFixed(3);
|
|
210
|
+
return `+${hours}h${minutes}m${seconds}s`;
|
|
211
|
+
}
|
|
212
|
+
} else {
|
|
213
|
+
// Absolute time - format as HH:MM:SS.mmm
|
|
214
|
+
const now = new Date();
|
|
215
|
+
const hours = now.getHours().toString().padStart(2, "0");
|
|
216
|
+
const minutes = now.getMinutes().toString().padStart(2, "0");
|
|
217
|
+
const seconds = now.getSeconds().toString().padStart(2, "0");
|
|
218
|
+
const milliseconds = now.getMilliseconds().toString().padStart(3, "0");
|
|
219
|
+
return `${hours}:${minutes}:${seconds}.${milliseconds}`;
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
const withTimestamp =
|
|
224
|
+
(fn: (...args: Array<any>) => void) =>
|
|
225
|
+
(...args: Array<any>) => {
|
|
226
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
227
|
+
fn(`[${getTimestamp()}]`, ...args);
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
// Override methods that should have timestamps
|
|
231
|
+
console.log = withTimestamp(console.log);
|
|
232
|
+
console.info = withTimestamp(console.info);
|
|
233
|
+
console.warn = withTimestamp(console.warn);
|
|
234
|
+
console.error = withTimestamp(console.error);
|
|
235
|
+
console.debug = withTimestamp(console.debug);
|
|
236
|
+
console.trace = withTimestamp(console.trace);
|
|
237
|
+
|
|
238
|
+
return console;
|
|
239
|
+
};
|
package/src/Crypto.ts
CHANGED
|
@@ -3,51 +3,65 @@
|
|
|
3
3
|
*
|
|
4
4
|
* @module
|
|
5
5
|
*/
|
|
6
|
-
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
6
|
+
import { xchacha20poly1305 } from "@noble/ciphers/chacha.js";
|
|
7
|
+
import { hmac } from "@noble/hashes/hmac.js";
|
|
8
|
+
import { sha512 } from "@noble/hashes/sha2.js";
|
|
9
|
+
import { randomBytes, utf8ToBytes } from "@noble/hashes/utils.js";
|
|
10
|
+
import { Result, trySync } from "./Result.js";
|
|
11
|
+
import { brand, length, NonNegativeInt, Uint8Array } from "./Type.js";
|
|
12
|
+
|
|
13
|
+
export interface RandomBytes {
|
|
14
|
+
/**
|
|
15
|
+
* Creates cryptographically secure random bytes with type-safe length
|
|
16
|
+
* branding.
|
|
17
|
+
*
|
|
18
|
+
* Uses the operating system's cryptographically secure random number
|
|
19
|
+
* generator (crypto.getRandomValues) to generate high-quality entropy
|
|
20
|
+
* suitable for cryptographic operations.
|
|
21
|
+
*
|
|
22
|
+
* ### Type Safety
|
|
23
|
+
*
|
|
24
|
+
* Returns specific branded types for common sizes:
|
|
25
|
+
*
|
|
26
|
+
* - `Random16` for 16-byte values (128 bits)
|
|
27
|
+
* - `Random32` for 32-byte values (256 bits)
|
|
28
|
+
* - `Random64` for 64-byte values (512 bits)
|
|
29
|
+
* - `Random` for any other size
|
|
30
|
+
*
|
|
31
|
+
* ### Example
|
|
32
|
+
*
|
|
33
|
+
* ```ts
|
|
34
|
+
* const nonce = randomBytes.create(16); // Type: Random16
|
|
35
|
+
* const key = randomBytes.create(32); // Type: Random32
|
|
36
|
+
* const seed = randomBytes.create(64); // Type: Random64
|
|
37
|
+
* const custom = randomBytes.create(48); // Type: Random
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
create(bytesLength: 16): Entropy16;
|
|
41
|
+
create(bytesLength: 32): Entropy32;
|
|
42
|
+
create(bytesLength: 64): Entropy64;
|
|
43
|
+
create(bytesLength: number): Entropy;
|
|
32
44
|
}
|
|
33
45
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
46
|
+
export interface RandomBytesDep {
|
|
47
|
+
readonly randomBytes: RandomBytes;
|
|
48
|
+
}
|
|
37
49
|
|
|
38
|
-
|
|
50
|
+
const Entropy = brand("Entropy", Uint8Array);
|
|
51
|
+
type Entropy = typeof Entropy.Type;
|
|
39
52
|
|
|
40
|
-
export
|
|
41
|
-
|
|
42
|
-
}
|
|
53
|
+
export const Entropy16 = length(16)(Entropy);
|
|
54
|
+
export type Entropy16 = typeof Entropy16.Type;
|
|
43
55
|
|
|
44
|
-
export const
|
|
45
|
-
|
|
56
|
+
export const Entropy32 = length(32)(Entropy);
|
|
57
|
+
export type Entropy32 = typeof Entropy32.Type;
|
|
46
58
|
|
|
47
|
-
export
|
|
59
|
+
export const Entropy64 = length(64)(Entropy);
|
|
60
|
+
export type Entropy64 = typeof Entropy64.Type;
|
|
48
61
|
|
|
49
|
-
export const
|
|
50
|
-
|
|
62
|
+
export const createRandomBytes = (): RandomBytes => ({
|
|
63
|
+
create: randomBytes as RandomBytes["create"],
|
|
64
|
+
});
|
|
51
65
|
|
|
52
66
|
/**
|
|
53
67
|
* SLIP21.
|
|
@@ -55,54 +69,43 @@ export const mnemonicToMnemonicSeed = (mnemonic: Mnemonic): MnemonicSeed =>
|
|
|
55
69
|
* https://github.com/satoshilabs/slips/blob/master/slip-0021.md
|
|
56
70
|
*/
|
|
57
71
|
export const createSlip21 = (
|
|
58
|
-
seed:
|
|
59
|
-
path: ReadonlyArray<string>,
|
|
60
|
-
):
|
|
61
|
-
let
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
72
|
+
seed: Entropy16 | Entropy32 | Entropy64,
|
|
73
|
+
path: ReadonlyArray<string | number>,
|
|
74
|
+
): Entropy32 => {
|
|
75
|
+
let currentNode = hmac(
|
|
76
|
+
sha512,
|
|
77
|
+
utf8ToBytes("Symmetric key seed"),
|
|
78
|
+
seed,
|
|
79
|
+
) as Entropy64;
|
|
80
|
+
|
|
81
|
+
for (const element of path) {
|
|
82
|
+
const label = typeof element === "number" ? element.toString() : element;
|
|
83
|
+
currentNode = deriveSlip21Node(label, currentNode);
|
|
68
84
|
}
|
|
69
|
-
|
|
85
|
+
|
|
86
|
+
return currentNode.slice(32, 64) as Entropy32;
|
|
70
87
|
};
|
|
71
88
|
|
|
72
89
|
/**
|
|
73
|
-
*
|
|
74
|
-
* derived key.
|
|
90
|
+
* Derives a single node in the SLIP-21 hierarchical key derivation.
|
|
75
91
|
*
|
|
76
|
-
*
|
|
77
|
-
* compact, human-readable, and shareable identifier suitable for UI display or
|
|
78
|
-
* URL use. While this lowers entropy, 126 bits remains cryptographically secure
|
|
79
|
-
* for uniqueness and unpredictability in most applications (comparable to
|
|
80
|
-
* UUIDv4's 122 bits).
|
|
81
|
-
*
|
|
82
|
-
* See https://github.com/satoshilabs/slips/blob/master/slip-0021.md
|
|
92
|
+
* @see {@link createSlip21}
|
|
83
93
|
*/
|
|
84
|
-
export const
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
):
|
|
88
|
-
const
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
id = (id + urlAlphabet[slip21[i] & 63]) as Id;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
return id;
|
|
94
|
+
export const deriveSlip21Node = (
|
|
95
|
+
label: string,
|
|
96
|
+
parentNode: Entropy64,
|
|
97
|
+
): Entropy64 => {
|
|
98
|
+
const labelBytes = utf8ToBytes(label);
|
|
99
|
+
const message = new globalThis.Uint8Array(labelBytes.byteLength + 1);
|
|
100
|
+
message[0] = 0;
|
|
101
|
+
message.set(labelBytes, 1);
|
|
102
|
+
return hmac(sha512, parentNode.slice(0, 32), message) as Entropy64;
|
|
97
103
|
};
|
|
98
104
|
|
|
99
105
|
/** The encryption key for {@link SymmetricCrypto}. */
|
|
100
|
-
export const EncryptionKey = brand("EncryptionKey",
|
|
106
|
+
export const EncryptionKey = brand("EncryptionKey", Entropy32);
|
|
101
107
|
export type EncryptionKey = typeof EncryptionKey.Type;
|
|
102
108
|
|
|
103
|
-
export const createEncryptionKey = (seed: MnemonicSeed): EncryptionKey =>
|
|
104
|
-
createSlip21(seed, ["Evolu", "Encryption Key"]) as EncryptionKey;
|
|
105
|
-
|
|
106
109
|
/** Symmetric cryptography. */
|
|
107
110
|
export interface SymmetricCrypto {
|
|
108
111
|
readonly nonceLength: NonNegativeInt;
|
|
@@ -137,15 +140,15 @@ export interface SymmetricCryptoDecryptError {
|
|
|
137
140
|
* https://github.com/paulmillr/noble-ciphers?tab=readme-ov-file#which-cipher-should-i-pick
|
|
138
141
|
*/
|
|
139
142
|
export const createSymmetricCrypto = (
|
|
140
|
-
deps:
|
|
143
|
+
deps: RandomBytesDep,
|
|
141
144
|
): SymmetricCrypto => {
|
|
142
|
-
const nonceLength =
|
|
145
|
+
const nonceLength = NonNegativeInt.orThrow(24);
|
|
143
146
|
|
|
144
147
|
const symmetricCrypto: SymmetricCrypto = {
|
|
145
148
|
nonceLength,
|
|
146
149
|
|
|
147
150
|
encrypt: (plaintext, encryptionKey) => {
|
|
148
|
-
const nonce = deps.
|
|
151
|
+
const nonce = deps.randomBytes.create(nonceLength);
|
|
149
152
|
const ciphertext = xchacha20poly1305(encryptionKey, nonce).encrypt(
|
|
150
153
|
plaintext,
|
|
151
154
|
);
|
|
@@ -185,7 +188,19 @@ export const padmePaddedLength = (length: NonNegativeInt): NonNegativeInt => {
|
|
|
185
188
|
* Returns the PADMÉ padding length for a given input length. Uses
|
|
186
189
|
* {@link padmePaddedLength}.
|
|
187
190
|
*/
|
|
188
|
-
|
|
189
191
|
export const padmePaddingLength = (length: NonNegativeInt): NonNegativeInt => {
|
|
190
192
|
return (padmePaddedLength(length) - length) as NonNegativeInt;
|
|
191
193
|
};
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Performs a timing-safe comparison of two Uint8Arrays. Returns true if they
|
|
197
|
+
* are equal, false otherwise. Takes constant time regardless of where the
|
|
198
|
+
* arrays differ.
|
|
199
|
+
*
|
|
200
|
+
* @see https://nodejs.org/api/crypto.html#cryptotimingsafeequala-b
|
|
201
|
+
*/
|
|
202
|
+
export type TimingSafeEqual = (a: Uint8Array, b: Uint8Array) => boolean;
|
|
203
|
+
|
|
204
|
+
export interface TimingSafeEqualDep {
|
|
205
|
+
readonly timingSafeEqual: TimingSafeEqual;
|
|
206
|
+
}
|