@sovereignbase/explorer 0.0.0-alpha.1 → 0.0.0-alpha.2

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.
@@ -0,0 +1,274 @@
1
+
2
+ import { URLString } from "@sovereignbase/storage";
3
+ import { DeepReadonly } from "@sovereignbase/utils";
4
+ import { DeviceBindingStorage } from "@sovereignbase/hardware-bound";
5
+ import { CipherKey } from "@sovereignbase/cryptosuite";
6
+ import { SequencedArrayChange, SequencedArrayDelta } from "@sovereignbase/sequenced";
7
+ /**
8
+ * Describes an entry in the recent objects list.
9
+ */
10
+ type RecentEntry = {
11
+ /** The identifier of the referenced object. */
12
+ id: string;
13
+ /** The display name of the referenced object. */
14
+ name: string;
15
+ /** The cipher key used to access the referenced object. */
16
+ cipherKey: CipherKey;
17
+ /** The date and time at which the object was most recently accessed. */
18
+ dateAccessed: number;
19
+ };
20
+ /**
21
+ * Represents a sequenced delta produced by the recent entries list.
22
+ */
23
+ type RecentEntriesDelta = Readonly<['recentEntries', DeepReadonly<SequencedArrayDelta<RecentEntry>>]>;
24
+ /**
25
+ * Represents a sequenced change produced by the recent entries list.
26
+ */
27
+ type RecentEntriesChange = DeepReadonly<SequencedArrayChange<RecentEntry>>;
28
+ /**
29
+ * Describes a credential registered with the database.
30
+ */
31
+ type CredentialEntry = {
32
+ /** The identifier of the credential. */
33
+ id: string;
34
+ /** The display name of the credential. */
35
+ name: string;
36
+ /** The entropy associated with the credential. */
37
+ entropy: Uint8Array;
38
+ /** The storage mechanism used for device binding. */
39
+ storage: DeviceBindingStorage;
40
+ /** The timestamp of the most recent use of the credential. */
41
+ lastUsed: number;
42
+ /** The user agents that have used the credential. */
43
+ userAgents: Array<{
44
+ /** The user agent string. */
45
+ name: string;
46
+ /** The timestamp of the most recent use by the user agent. */
47
+ lastSeen: number;
48
+ }>;
49
+ };
50
+ /**
51
+ * Represents a sequenced delta produced by the credential entries list.
52
+ */
53
+ type CredentialEntriesDelta = Readonly<['credentialEntries', DeepReadonly<SequencedArrayDelta<CredentialEntry>>]>;
54
+ /**
55
+ * Represents a sequenced change produced by the credential entries list.
56
+ */
57
+ type CredentialEntriesChange = DeepReadonly<SequencedArrayChange<CredentialEntry>>;
58
+ /**
59
+ * Represents the outcome of a database authentication attempt.
60
+ */
61
+ type AuthenticationResult =
62
+ /** The database was successfully authenticated. */
63
+ 'authenticated' |
64
+ /** The device authenticator could not provide the required entropy. */
65
+ 'authenticator-error' |
66
+ /** The supplied data PIN could not unlock the credential. */
67
+ 'invalid-data-pin';
68
+ /**
69
+ * Describes the parameters used to register a new database credential.
70
+ */
71
+ type AuthenticationSetupParams = Readonly<{
72
+ /** The display name assigned to the credential. */
73
+ credentialName: string;
74
+ /** The storage mechanism used to bind the credential to the device. */
75
+ bindingStorage: DeviceBindingStorage;
76
+ }>;
77
+ /**
78
+ * Represents a synchronized delta exchanged between Explorer instances.
79
+ *
80
+ * Includes changes to recent and credential entries, as well as
81
+ * a reserved variant for other delta types.
82
+ */
83
+ type ExplorerDelta = RecentEntriesDelta | CredentialEntriesDelta | ['other' | []];
84
+ /**
85
+ * Maps Explorer event names to their detail types.
86
+ */
87
+ type ExplorerEventMap = {
88
+ /** Fired when the database authentication state changes. */
89
+ authenticated: boolean;
90
+ /** Fired when a synchronized delta is produced. */
91
+ delta: ExplorerDelta;
92
+ };
93
+ /**
94
+ * Represents an event listener for an {@link Explorer} event.
95
+ *
96
+ * @typeParam K The event name that determines the event detail type.
97
+ */
98
+ type ExplorerEventListener<K extends keyof ExplorerEventMap> = ((event: CustomEvent<ExplorerEventMap[K]>) => void) | {
99
+ handleEvent(event: CustomEvent<ExplorerEventMap[K]>): void;
100
+ };
101
+ /**
102
+ * Resolves an event name to its corresponding listener type.
103
+ *
104
+ * @typeParam K The event name to resolve.
105
+ */
106
+ type ExplorerEventListenerFor<K extends string> = K extends keyof ExplorerEventMap ? ExplorerEventListener<K> : EventListenerOrEventListenerObject;
107
+ /**
108
+ * Provides authentication, encrypted object storage, and access to
109
+ * the database's synchronized entries.
110
+ *
111
+ * Maintains the authenticated database context and exposes static methods
112
+ * for managing stored objects, observing entries, and applying remote deltas.
113
+ */
114
+ export declare class Explorer {
115
+ /** The identifier of the authenticated database. */
116
+ static databaseID: string;
117
+ /** Indicates whether the database has been authenticated. */
118
+ static isAuthenticated: boolean;
119
+ private static storageURL;
120
+ private static eventTarget?;
121
+ private static databaseSecret;
122
+ private static sessionCredential?;
123
+ /**
124
+ * Caches cipher keys by object identifier.
125
+ *
126
+ * Existing entries are preserved when an object is accessed with
127
+ * a cipher key.
128
+ */
129
+ static readonly cipherKeyCache: Map<string, CipherKey>;
130
+ /**
131
+ * Authenticates a database using a data PIN and device-bound entropy.
132
+ *
133
+ * Derives the credential and database identifiers from cryptographic
134
+ * secrets. When setup parameters are provided, creates and stores a
135
+ * new credential. Otherwise, loads the existing credential.
136
+ *
137
+ * Records the authenticated credential in the database's credential list.
138
+ *
139
+ * @param dataPIN The PIN used to derive the credential and database secrets.
140
+ * @param storageURL The base URL used to access persisted database objects.
141
+ * @param setupParams Optional parameters for creating a new credential.
142
+ * @returns The authentication result.
143
+ */
144
+ static authenticate(dataPIN: string, storageURL: URLString, setupParams?: AuthenticationSetupParams): Promise<AuthenticationResult>;
145
+ /**
146
+ * Permanently revokes a credential and deletes its persisted data.
147
+ *
148
+ * Removes the credential from the database's registered credentials.
149
+ *
150
+ * @param credentialID The identifier of the credential to revoke.
151
+ * @throws {ExplorerError} If the database is not authenticated.
152
+ */
153
+ static permanentlyDeleteCredential(credentialID: string): Promise<void>;
154
+ /**
155
+ * Returns the registered credentials and optionally observes their changes.
156
+ *
157
+ * The returned tuple contains a cleanup function, the current credential
158
+ * entries, and the identifier of the current session's credential.
159
+ *
160
+ * The cleanup function removes the registered change listener, if any.
161
+ *
162
+ * @param onChange An optional callback invoked when the entries change.
163
+ * @returns The cleanup function, credential entries, and session credential ID.
164
+ * @throws {ExplorerError} If the database is not authenticated.
165
+ */
166
+ static listCredentialEntries(onChange?: (splice: CredentialEntriesChange) => void): Promise<[cleanup: () => void, entries: DeepReadonly<Array<CredentialEntry>>, sessionCredentialID: string]>;
167
+ /**
168
+ * Returns the recently accessed objects and optionally observes their changes.
169
+ *
170
+ * The returned tuple contains a cleanup function and the current recent
171
+ * entries. The cleanup function removes the registered change listener,
172
+ * if any.
173
+ *
174
+ * @param onChange An optional callback invoked when the entries change.
175
+ * @returns The cleanup function and recent entries.
176
+ * @throws {ExplorerError} If the database is not authenticated.
177
+ */
178
+ static listRecentEntries(onChange?: (splice: RecentEntriesChange) => void): Promise<[cleanup: () => void, entries: DeepReadonly<Array<RecentEntry>>]>;
179
+ /**
180
+ * Loads an encrypted object using its identifier and cipher key.
181
+ *
182
+ * Caches the cipher key if the identifier has not been encountered before.
183
+ *
184
+ * @typeParam T The expected type of the loaded object.
185
+ * @param id The identifier of the object to load.
186
+ * @param cipherKey The key used to decrypt the object.
187
+ * @param onNetworkResponse An optional callback for network responses.
188
+ * @returns The loaded object, or `undefined` if no object is available.
189
+ */
190
+ static loadObjectByID<T>(id: string, cipherKey: CipherKey, onNetworkResponse?: (data: unknown) => void | Promise<void>): Promise<T | undefined>;
191
+ /**
192
+ * Encrypts and stores an object using its identifier and cipher key.
193
+ *
194
+ * Caches the cipher key if the identifier has not been encountered before.
195
+ *
196
+ * @typeParam T The type of the object to store.
197
+ * @param id The identifier of the object to store.
198
+ * @param object The object to encrypt and store.
199
+ * @param cipherKey The key used to encrypt the object.
200
+ */
201
+ static storeObjectByID<T>(id: string, object: T, cipherKey: CipherKey): Promise<void>;
202
+ /**
203
+ * Deletes a persisted object using its identifier.
204
+ *
205
+ * @param id The identifier of the object to delete.
206
+ */
207
+ static deleteObjectByID(id: string): Promise<void>;
208
+ /**
209
+ * Schedules the application of a synchronized Explorer delta.
210
+ *
211
+ * Recognized deltas are forwarded to their corresponding entries list.
212
+ * Invalid or unrecognized delta types are ignored.
213
+ *
214
+ * Processing is scheduled in a microtask and is not awaited by the caller.
215
+ *
216
+ * @param data The delta to apply.
217
+ */
218
+ static apply(data: unknown): void;
219
+ /**
220
+ * Registers an event listener for the specified event type.
221
+ *
222
+ * @param type The event type.
223
+ * @param listener The event listener, or `null`.
224
+ * @param options The event listener options.
225
+ */
226
+ static addEventListener<K extends keyof ExplorerEventMap>(type: K, listener: ExplorerEventListenerFor<K> | null, options?: boolean | AddEventListenerOptions): void;
227
+ /**
228
+ * Removes an event listener previously registered with
229
+ * {@link addEventListener}.
230
+ *
231
+ * @param type The event type.
232
+ * @param listener The event listener to remove, or `null`.
233
+ * @param options The event listener options.
234
+ */
235
+ static removeEventListener<K extends keyof ExplorerEventMap>(type: K, listener: ExplorerEventListenerFor<K> | null, options?: boolean | EventListenerOptions): void;
236
+ /**
237
+ * Dispatches an event to the registered listeners.
238
+ *
239
+ * @param type The event type.
240
+ * @param detail The value exposed through `CustomEvent.detail`.
241
+ */
242
+ static dispatchEvent<K extends keyof ExplorerEventMap>(type: K, detail: ExplorerEventMap[K]): void;
243
+ /**
244
+ * Determines whether a value has the basic structure of an Explorer delta.
245
+ *
246
+ * @param data The value to inspect.
247
+ * @returns Whether the value has a string discriminator.
248
+ */
249
+ private static isExplorerDelta;
250
+ private static recentEntries?;
251
+ /**
252
+ * Loads or returns the cached recent entries list.
253
+ *
254
+ * Derives the list identifier and cipher key from the database secret
255
+ * and merges subsequent network responses into the loaded state.
256
+ *
257
+ * @returns The recent entries list.
258
+ * @throws {ExplorerError} If the database is not authenticated.
259
+ */
260
+ private static loadRecentEntries;
261
+ private static credentialEntries?;
262
+ /**
263
+ * Loads or returns the cached credential entries list.
264
+ *
265
+ * Derives the list identifier and cipher key from the database secret
266
+ * and merges subsequent network responses into the loaded state.
267
+ *
268
+ * @returns The credential entries list.
269
+ * @throws {ExplorerError} If the database is not authenticated.
270
+ */
271
+ private static loadCredentialEntries;
272
+ }
273
+ export {};
274
+ //# sourceMappingURL=class.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"class.d.ts","names":[],"sources":["../src/RecentEntries/class.ts","../src/CredentialEntries/class.ts","../src/.types/type.ts","../src/class.ts"],"mappings":";;;;;;;;;KAsBY;;EAEV;;EAGA;;EAGA,WAAW;;EAGX;;;;;KAMU,qBAAqB,2BACb,aAAa,oBAAoB;;;;KAMzC,sBAAsB,aAChC,qBAAqB;;;;KCvBX;;EAEV;;EAGA;;EAGA,SAAS;;EAGT,SAAS;;EAGT;;EAGA,YAAY;;IAEV;;IAGA;;;;;;KAiCQ,yBAAyB,+BACb,aAAa,oBAAoB;;;;KAM7C,0BAA0B,aACpC,qBAAqB;;;;KChFX;;;;;;;;;;KAWA,4BAA4B;;EAEtC;;EAGA,gBAAgB;;;;;;;;KASN,gBACV,qBAAqB;;;;KAKX;;EAEV;;EAGA,OAAO;;;;;;;KAQG,sBAAsB,gBAAgB,sBAC5C,OAAO,YAAY,iBAAiB;EACpC,YAAY,OAAO,YAAY,iBAAiB;;;;;;;KAO1C,yBAAyB,oBACnC,gBAAgB,mBACZ,sBAAsB,KACtB;;;;;;;;qBClBO;;SAEG;;SAGA;iBAGC;iBACA;iBACA;iBACA;;;;;;;kBAQQ,gBAAgB,YAAY;;;;;;;;;;;;;;;SAgB/B,aAClB,iBACA,YAAY,WACZ,cAAc,4BACb,QAAQ;;;;;;;;;SA0FS,4BAClB,uBACC;;;;;;;;;;;;;SAsBiB,sBAClB,YAAY,QAAQ,mCACnB,SAEC,qBACA,SAAS,aAAa,MAAM,mBAC5B;;;;;;;;;;;;SAoCgB,kBAClB,YAAY,QAAQ,+BACnB,SAAS,qBAAqB,SAAS,aAAa,MAAM;;;;;;;;;;;;SAgCzC,eAAe,GACjC,YACA,WAAW,WACX,qBAAqB,yBAAyB,gBAC7C,QAAQ;;;;;;;;;;;SA0BS,gBAAgB,GAClC,YACA,QAAQ,GACR,WAAW,YACV;;;;;;SAoBiB,iBAAiB,aAAa;;;;;;;;;;;SAmBpC,MAAM;;;;;;;;SAmCN,iBAAiB,gBAAgB,kBAC7C,MAAM,GACN,UAAU,yBAAyB,WACnC,oBAAoB;;;;;;;;;SAqBR,oBAAoB,gBAAgB,kBAChD,MAAM,GACN,UAAU,yBAAyB,WACnC,oBAAoB;;;;;;;SAmBR,cAAc,gBAAgB,kBAC1C,MAAM,GACN,QAAQ,iBAAiB;;;;;;;iBAsBZ;iBAIA;;;;;;;;;;iBAWM;iBA8BN;;;;;;;;;;iBAWM"}