@ -31,9 +31,9 @@ import { OfferClaim } from "../interfaces/claims";
import { createPeerDid } from "../libs/crypto/vc/didPeer" ;
import { createPeerDid } from "../libs/crypto/vc/didPeer" ;
import { registerCredential } from "../libs/crypto/vc/passkeyDidPeer" ;
import { registerCredential } from "../libs/crypto/vc/passkeyDidPeer" ;
import { logger } from "../utils/logger" ;
import { logger } from "../utils/logger" ;
import { PlatformServiceFactory } from "@ /services/PlatformServiceFactory" ;
import { PlatformServiceFactory } from ".. /services/PlatformServiceFactory" ;
import { IIdentifier } from "@veramo/core" ;
import { IIdentifier } from "@veramo/core" ;
import { parseJsonField } from ".. /db/databaseUtil" ;
import { parseJsonField } from "@ /db/databaseUtil" ;
import { DEFAULT_ROOT_DERIVATION_PATH } from "./crypto" ;
import { DEFAULT_ROOT_DERIVATION_PATH } from "./crypto" ;
export interface GiverReceiverInputInfo {
export interface GiverReceiverInputInfo {
@ -603,9 +603,13 @@ export async function saveNewIdentity(
mnemonic : string ,
mnemonic : string ,
derivationPath : string ,
derivationPath : string ,
) : Promise < void > {
) : Promise < void > {
console . log ( "💾 DEBUG: saveNewIdentity called with DID:" , identity . did ) ;
try {
try {
// add to the new sql db
// add to the new sql db
const platformService = PlatformServiceFactory . getInstance ( ) ;
const platformService = PlatformServiceFactory . getInstance ( ) ;
console . log ( "💾 DEBUG: Getting secrets from database" ) ;
const secrets = await platformService . dbQuery (
const secrets = await platformService . dbQuery (
` SELECT secretBase64 FROM secret ` ,
` SELECT secretBase64 FROM secret ` ,
) ;
) ;
@ -614,13 +618,19 @@ export async function saveNewIdentity(
"No initial encryption supported. We recommend you clear your data and start over." ,
"No initial encryption supported. We recommend you clear your data and start over." ,
) ;
) ;
}
}
const secretBase64 = secrets . values [ 0 ] [ 0 ] as string ;
const secretBase64 = secrets . values [ 0 ] [ 0 ] as string ;
console . log ( "💾 DEBUG: Found secret, encrypting identity" ) ;
const secret = base64ToArrayBuffer ( secretBase64 ) ;
const secret = base64ToArrayBuffer ( secretBase64 ) ;
const identityStr = JSON . stringify ( identity ) ;
const identityStr = JSON . stringify ( identity ) ;
const encryptedIdentity = await simpleEncrypt ( identityStr , secret ) ;
const encryptedIdentity = await simpleEncrypt ( identityStr , secret ) ;
const encryptedMnemonic = await simpleEncrypt ( mnemonic , secret ) ;
const encryptedMnemonic = await simpleEncrypt ( mnemonic , secret ) ;
const encryptedIdentityBase64 = arrayBufferToBase64 ( encryptedIdentity ) ;
const encryptedIdentityBase64 = arrayBufferToBase64 ( encryptedIdentity ) ;
const encryptedMnemonicBase64 = arrayBufferToBase64 ( encryptedMnemonic ) ;
const encryptedMnemonicBase64 = arrayBufferToBase64 ( encryptedMnemonic ) ;
console . log ( "💾 DEBUG: Inserting account into database" ) ;
const sql = ` INSERT INTO accounts (dateCreated, derivationPath, did, identityEncrBase64, mnemonicEncrBase64, publicKeyHex)
const sql = ` INSERT INTO accounts (dateCreated, derivationPath, did, identityEncrBase64, mnemonicEncrBase64, publicKeyHex)
VALUES ( ? , ? , ? , ? , ? , ? ) ` ;
VALUES ( ? , ? , ? , ? , ? , ? ) ` ;
const params = [
const params = [
@ -632,9 +642,22 @@ export async function saveNewIdentity(
identity . keys [ 0 ] . publicKeyHex ,
identity . keys [ 0 ] . publicKeyHex ,
] ;
] ;
await platformService . dbExec ( sql , params ) ;
await platformService . dbExec ( sql , params ) ;
console . log ( "💾 DEBUG: Account inserted successfully" ) ;
console . log ( "💾 DEBUG: Updating default settings with activeDid" ) ;
await databaseUtil . updateDefaultSettings ( { activeDid : identity.did } ) ;
await databaseUtil . updateDefaultSettings ( { activeDid : identity.did } ) ;
console . log ( "💾 DEBUG: Default settings updated" ) ;
console . log ( "💾 DEBUG: Inserting DID-specific settings" ) ;
await databaseUtil . insertDidSpecificSettings ( identity . did ) ;
await databaseUtil . insertDidSpecificSettings ( identity . did ) ;
console . log ( "💾 DEBUG: DID-specific settings inserted" ) ;
// Check what was actually created
const createdSettings =
await databaseUtil . retrieveSettingsForActiveAccount ( ) ;
console . log ( "💾 DEBUG: Created settings:" , createdSettings ) ;
} catch ( error ) {
} catch ( error ) {
console . log ( "💾 DEBUG: saveNewIdentity error:" , error ) ;
logger . error ( "Failed to update default settings:" , error ) ;
logger . error ( "Failed to update default settings:" , error ) ;
throw new Error (
throw new Error (
"Failed to set default settings. Please try again or restart the app." ,
"Failed to set default settings. Please try again or restart the app." ,
@ -911,22 +934,104 @@ export async function importFromMnemonic(
derivationPath : string = DEFAULT_ROOT_DERIVATION_PATH ,
derivationPath : string = DEFAULT_ROOT_DERIVATION_PATH ,
shouldErase : boolean = false ,
shouldErase : boolean = false ,
) : Promise < void > {
) : Promise < void > {
console . log ( "📥 DEBUG: importFromMnemonic called with:" , {
mnemonicLength : mnemonic.split ( " " ) . length ,
derivationPath ,
shouldErase ,
} ) ;
const mne : string = mnemonic . trim ( ) . toLowerCase ( ) ;
const mne : string = mnemonic . trim ( ) . toLowerCase ( ) ;
console . log ( "📥 DEBUG: Normalized mnemonic length:" , mne . split ( " " ) . length ) ;
// Check if this is Test User #0
const TEST_USER_0_MNEMONIC =
"rigid shrug mobile smart veteran half all pond toilet brave review universe ship congress found yard skate elite apology jar uniform subway slender luggage" ;
const isTestUser0 = mne === TEST_USER_0_MNEMONIC ;
console . log ( "📥 DEBUG: Is Test User #0:" , isTestUser0 ) ;
// Derive address and keys from mnemonic
// Derive address and keys from mnemonic
const [ address , privateHex , publicHex ] = deriveAddress ( mne , derivationPath ) ;
const [ address , privateHex , publicHex ] = deriveAddress ( mne , derivationPath ) ;
console . log ( "📥 DEBUG: Derived address:" , address ) ;
console . log ( "📥 DEBUG: Derived DID:" , ` did:ethr: ${ address } ` ) ;
// Create new identifier
// Create new identifier
const newId = newIdentifier ( address , publicHex , privateHex , derivationPath ) ;
const newId = newIdentifier ( address , publicHex , privateHex , derivationPath ) ;
console . log ( "📥 DEBUG: Created new identifier:" , {
did : newId.did ,
keysLength : newId.keys.length ,
} ) ;
// Handle erasures
// Handle erasures
if ( shouldErase ) {
if ( shouldErase ) {
console . log ( "📥 DEBUG: Erasing existing accounts" ) ;
const platformService = PlatformServiceFactory . getInstance ( ) ;
const platformService = PlatformServiceFactory . getInstance ( ) ;
await platformService . dbExec ( "DELETE FROM accounts" ) ;
await platformService . dbExec ( "DELETE FROM accounts" ) ;
}
}
// Save the new identity
// Save the new identity
console . log ( "📥 DEBUG: Calling saveNewIdentity" ) ;
await saveNewIdentity ( newId , mne , derivationPath ) ;
await saveNewIdentity ( newId , mne , derivationPath ) ;
console . log ( "📥 DEBUG: saveNewIdentity completed" ) ;
// Set up Test User #0 specific settings
if ( isTestUser0 ) {
console . log ( "📥 DEBUG: Setting up Test User #0 specific settings" ) ;
// First, let's see what's currently in the database
const platformService = PlatformServiceFactory . getInstance ( ) ;
const existingResult = await platformService . dbQuery (
"SELECT * FROM settings WHERE accountDid = ?" ,
[ newId . did ] ,
) ;
console . log ( "📥 DEBUG: Existing settings before update:" , existingResult ) ;
// Let's also see the actual data by mapping it
if ( existingResult ? . values ? . length ) {
const existingData = databaseUtil . mapColumnsToValues (
existingResult . columns ,
existingResult . values ,
) [ 0 ] ;
console . log ( "📥 DEBUG: Existing settings mapped:" , existingData ) ;
}
// Let's also check what's in the master settings
const masterResult = await platformService . dbQuery (
"SELECT * FROM settings WHERE id = ?" ,
[ "MASTER" ] ,
) ;
console . log ( "📥 DEBUG: Master settings:" , masterResult ) ;
// Now try the UPDATE with better debugging
const updateResult = await databaseUtil . updateDidSpecificSettings (
newId . did ,
{
firstName : "User Zero" ,
isRegistered : true ,
} ,
) ;
console . log ( "📥 DEBUG: Test User #0 settings update result:" , updateResult ) ;
// Verify the settings were saved
const verifyResult = await platformService . dbQuery (
"SELECT * FROM settings WHERE accountDid = ?" ,
[ newId . did ] ,
) ;
console . log ( "📥 DEBUG: Settings after update attempt:" , verifyResult ) ;
if ( verifyResult ? . values ? . length ) {
const verifiedData = databaseUtil . mapColumnsToValues (
verifyResult . columns ,
verifyResult . values ,
) [ 0 ] ;
console . log ( "📥 DEBUG: Settings after update mapped:" , verifiedData ) ;
}
console . log ( "📥 DEBUG: Test User #0 settings applied" ) ;
}
// Check what settings were created
const settings = await databaseUtil . retrieveSettingsForActiveAccount ( ) ;
console . log ( "📥 DEBUG: Final settings after import:" , settings ) ;
}
}
/ * *
/ * *