@ -49,7 +49,7 @@ import {
type Settings ,
type SettingsWithJsonStrings ,
} from "@/db/tables/settings" ;
import { DEFAULT_SCOPE , type ActiveIdentity } from "@/db/tables/activeIdentity" ;
import { type ActiveIdentity } from "@/db/tables/activeIdentity" ;
import { FLAGS } from "@/config/featureFlags" ;
import { logger } from "@/utils/logger" ;
import { Contact , ContactMaybeWithJsonStrings } from "@/db/tables/contacts" ;
@ -974,17 +974,15 @@ export const PlatformServiceMixin = {
* Get the current active DID from the active_identity table
* Falls back to legacy settings . activeDid during Phase A transition
*
* @param scope Scope identifier ( default : 'default' )
* @returns Promise < string | null > The active DID or null if not found
* /
async $getActiveDid ( scope : string = DEFAULT_SCOPE ) : Promise < string | null > {
async $getActiveDid ( ) : Promise < string | null > {
try {
logger . debug ( "[ActiveDid] Getting activeDid for scope: " , scope ) ;
logger . debug ( "[ActiveDid] Getting activeDid" ) ;
// Try new active_identity table first
const row = await this . $first < ActiveIdentity > (
"SELECT active_did FROM active_identity WHERE scope = ? LIMIT 1" ,
[ scope ] ,
"SELECT active_did FROM active_identity LIMIT 1" ,
) ;
logger . debug ( "[ActiveDid] New system result:" , row ? . active_did || "null" ) ;
@ -1013,109 +1011,87 @@ export const PlatformServiceMixin = {
// Log current database state for debugging
try {
const activeIdentityCount = await this . $first < { count : number } > (
"SELECT COUNT(*) as count FROM active_identity"
const activeIdentityCount = await this . $first < { count : number } > (
"SELECT COUNT(*) as count FROM active_identity" ,
) ;
const settingsCount = await this . $first < { count : number } > (
"SELECT COUNT(*) as count FROM settings"
) ;
const accountsCount = await this . $first < { count : number } > (
"SELECT COUNT(*) as count FROM accounts"
) ;
// Also check actual values
const activeIdentityValue = await this . $first < { active_did : string } > (
"SELECT active_did FROM active_identity WHERE scope = 'default' LIMIT 1"
) ;
const settingsValue = await this . $first < { activeDid : string } > (
"SELECT activeDid FROM settings WHERE id = 1 LIMIT 1"
) ;
const firstAccount = await this . $first < { did : string } > (
"SELECT did FROM accounts LIMIT 1"
) ;
logger . debug ( "[ActiveDid] Database state - active_identity:" , activeIdentityCount ? . count , "value:" , activeIdentityValue ? . active_did || "null" ) ;
logger . debug ( "[ActiveDid] Database state - settings:" , settingsCount ? . count , "value:" , settingsValue ? . activeDid || "null" ) ;
logger . debug ( "[ActiveDid] Database state - accounts:" , accountsCount ? . count , "first:" , firstAccount ? . did || "null" ) ;
} catch ( dbError ) {
logger . debug ( "[ActiveDid] Could not log database state:" , dbError ) ;
logger . debug ( "[ActiveDid] Active identity records:" , activeIdentityCount ? . count || 0 ) ;
} catch ( error ) {
logger . debug ( "[ActiveDid] Could not count active identity records:" , error ) ;
}
return null ;
} catch ( error ) {
logger . error ( "[PlatformServiceMixin] Error getting activeDid:" , error ) ;
logger . error ( "[ActiveDid] Error getting activeDid:" , error ) ;
// Fallback to legacy settings.activeDid during Phase A/B
if ( ! FLAGS . DROP_SETTINGS_ACTIVEDID ) {
try {
const legacy = await this . $first < Settings > (
"SELECT activeDid FROM settings WHERE id = ? LIMIT 1" ,
[ MASTER_SETTINGS_KEY ] ,
) ;
return legacy ? . activeDid || null ;
} catch ( fallbackError ) {
logger . error ( "[ActiveDid] Legacy fallback also failed:" , fallbackError ) ;
return null ;
}
}
return null ;
}
} ,
/ * *
* Update the active DID in the active_identity table
* Also maintains legacy settings . activeDid during Phase A transition
* Set the active DID in the active_identity table
* Also update s legacy settings . activeDid during Phase A / B transition
*
* @param did The DID to set as active ( or null to clear )
* @param scope Scope identifier ( default : 'default' )
* @param did The DID to set as active
* @returns Promise < void >
* /
async $setActiveDid (
did : string | null ,
scope : string = DEFAULT_SCOPE ,
) : Promise < void > {
async $setActiveDid ( did : string | null ) : Promise < void > {
try {
if ( ! did ) {
throw new Error ( "Cannot set null DID as active ") ;
logger . warn ( "[ActiveDid] Attempting to set null activeDid - this may cause issues" ) ;
}
// Validate that the DID exists in accounts table
const accountExists = await this . $first < Account > (
"SELECT did FROM accounts WHERE did = ? LIMIT 1" ,
[ did ] ,
logger . debug ( "[ActiveDid] Setting activeDid to:" , did ) ;
// Update/insert into new active_identity table
const existingRecord = await this . $first < ActiveIdentity > (
"SELECT id FROM active_identity LIMIT 1" ,
) ;
if ( ! accountExists ) {
throw new Error (
` Cannot set activeDid to non-existent account: ${ did } ` ,
if ( existingRecord ? . id ) {
// Update existing record
await this . $exec (
` UPDATE active_identity
SET active_did = ? , updated_at = strftime ( '%Y-%m-%dT%H:%M:%fZ' , 'now' )
WHERE id = ? ` ,
[ did , existingRecord . id ] ,
) ;
logger . debug ( "[ActiveDid] Updated existing record" ) ;
} else {
// Insert new record
await this . $exec (
` INSERT INTO active_identity (active_did, updated_at)
VALUES ( ? , strftime ( '%Y-%m-%dT%H:%M:%fZ' , 'now' ) ) ` ,
[ did ] ,
) ;
logger . debug ( "[ActiveDid] Inserted new record" ) ;
}
await this . $withTransaction ( async ( ) = > {
// Update/insert into active_identity table
const existingRecord = await this . $first < ActiveIdentity > (
"SELECT id FROM active_identity WHERE scope = ? LIMIT 1" ,
[ scope ] ,
// Legacy fallback - update settings.activeDid during Phase A/B
if ( ! FLAGS . USE_ACTIVE_IDENTITY_ONLY ) {
await this . $exec (
"UPDATE settings SET activeDid = ? WHERE id = ? " ,
[ did , MASTER_SETTINGS_KEY ] ,
) ;
logger . debug ( "[ActiveDid] Updated legacy settings.activeDid" ) ;
}
if ( existingRecord ) {
// Update existing record
await this . $dbExec (
` UPDATE active_identity
SET active_did = ? , updated_at = strftime ( '%Y-%m-%dT%H:%M:%fZ' , 'now' )
WHERE scope = ? ` ,
[ did , scope ] ,
) ;
} else {
// Insert new record
await this . $dbExec (
` INSERT INTO active_identity (scope, active_did, updated_at)
VALUES ( ? , ? , strftime ( '%Y-%m-%dT%H:%M:%fZ' , 'now' ) ) ` ,
[ scope , did ] ,
) ;
}
// Maintain legacy settings.activeDid during Phase A (unless Phase C is complete)
if ( ! FLAGS . DROP_SETTINGS_ACTIVEDID ) {
await this . $dbExec (
"UPDATE settings SET activeDid = ? WHERE id = ?" ,
[ did , MASTER_SETTINGS_KEY ] ,
) ;
}
} ) ;
// Update component cache for change detection
await this . $updateActiveDid ( did ) ;
logger . info ( ` [PlatformServiceMixin] Active DID updated to: ${ did } ` ) ;
logger . debug ( "[ActiveDid] Successfully set activeDid to:" , did ) ;
} catch ( error ) {
logger . error ( "[PlatformServiceMixin ] Error setting activeDid:" , error ) ;
logger . error ( "[ActiveDid] Error setting activeDid:" , error ) ;
throw error ;
}
} ,
@ -1132,24 +1108,12 @@ export const PlatformServiceMixin = {
} ,
/ * *
* Get all available active identity scopes
* Useful for multi - profile support in the future
*
* @returns Promise < string [ ] > Array of scope identifiers
* Get all available identity scopes ( simplified to single scope )
* @returns Promise < string [ ] > Array containing only 'default' scope
* /
async $getActiveIdentityScopes ( ) : Promise < string [ ] > {
try {
const scopes = await this . $query < { scope : string } > (
"SELECT DISTINCT scope FROM active_identity ORDER BY scope" ,
) ;
return scopes . map ( ( row ) = > row . scope ) ;
} catch ( error ) {
logger . error (
"[PlatformServiceMixin] Error getting active identity scopes:" ,
error ,
) ;
return [ ] ;
}
// Simplified to single scope since we removed multi-scope support
return [ "default" ] ;
} ,
// =================================================
@ -1898,8 +1862,8 @@ export interface IPlatformServiceMixin {
$debugMergedSettings ( did : string ) : Promise < void > ;
// Active Identity façade methods
$getActiveDid ( scope? : string ) : Promise < string | null > ;
$setActiveDid ( did : string | null , scope? : string ) : Promise < void > ;
$getActiveDid ( ) : Promise < string | null > ;
$setActiveDid ( did : string | null ) : Promise < void > ;
$switchActiveIdentity ( did : string ) : Promise < void > ;
$getActiveIdentityScopes ( ) : Promise < string [ ] > ;
}
@ -1919,8 +1883,8 @@ declare module "@vue/runtime-core" {
$updateActiveDid ( newDid : string | null ) : Promise < void > ;
// Active Identity façade methods
$getActiveDid ( scope? : string ) : Promise < string | null > ;
$setActiveDid ( did : string | null , scope? : string ) : Promise < void > ;
$getActiveDid ( ) : Promise < string | null > ;
$setActiveDid ( did : string | null ) : Promise < void > ;
$switchActiveIdentity ( did : string ) : Promise < void > ;
$getActiveIdentityScopes ( ) : Promise < string [ ] > ;