@@ -257,13 +257,56 @@ export class SyncEngineLevel implements SyncEngine {
257
257
// Get a reference to the `registeredIdentities` sublevel.
258
258
const registeredIdentities = this . _db . sublevel ( 'registeredIdentities' ) ;
259
259
260
+ const existing = await this . getIdentityOptions ( did ) ;
261
+ if ( existing ) {
262
+ throw new Error ( `SyncEngineLevel: Identity with DID ${ did } is already registered.` ) ;
263
+ }
264
+
260
265
// if no options are provided, we default to no delegateDid and all protocols (empty array)
261
266
options ??= { protocols : [ ] } ;
262
267
263
268
// Add (or overwrite, if present) the Identity's DID as a registered identity.
264
269
await registeredIdentities . put ( did , JSON . stringify ( options ) ) ;
265
270
}
266
271
272
+ public async unregisterIdentity ( did : string ) : Promise < void > {
273
+ const registeredIdentities = this . _db . sublevel ( 'registeredIdentities' ) ;
274
+ const existing = await this . getIdentityOptions ( did ) ;
275
+ if ( ! existing ) {
276
+ throw new Error ( `SyncEngineLevel: Identity with DID ${ did } is not registered.` ) ;
277
+ }
278
+
279
+ await registeredIdentities . del ( did ) ;
280
+ }
281
+
282
+ public async getIdentityOptions ( did : string ) : Promise < SyncIdentityOptions | undefined > {
283
+ const registeredIdentities = this . _db . sublevel ( 'registeredIdentities' ) ;
284
+ try {
285
+ const options = await registeredIdentities . get ( did ) ;
286
+ if ( options ) {
287
+ return JSON . parse ( options ) as SyncIdentityOptions ;
288
+ }
289
+ } catch ( error ) {
290
+ const e = error as { code : string } ;
291
+ // `Level`` throws an error if the key is not present. Return `undefined` in this case.
292
+ if ( e . code === 'LEVEL_NOT_FOUND' ) {
293
+ return ;
294
+ } else {
295
+ throw new Error ( `SyncEngineLevel: Error reading level: ${ e . code } .` ) ;
296
+ }
297
+ }
298
+ }
299
+
300
+ public async updateIdentityOptions ( { did, options } : { did : string , options : SyncIdentityOptions } ) : Promise < void > {
301
+ const registeredIdentities = this . _db . sublevel ( 'registeredIdentities' ) ;
302
+ const existingOptions = await this . getIdentityOptions ( did ) ;
303
+ if ( ! existingOptions ) {
304
+ throw new Error ( `SyncEngineLevel: Identity with DID ${ did } is not registered.` ) ;
305
+ }
306
+
307
+ await registeredIdentities . put ( did , JSON . stringify ( options ) ) ;
308
+ }
309
+
267
310
public async sync ( direction ?: 'push' | 'pull' ) : Promise < void > {
268
311
if ( this . _syncLock ) {
269
312
throw new Error ( 'SyncEngineLevel: Sync operation is already in progress.' ) ;
0 commit comments