@@ -134,7 +134,12 @@ export class Vault {
134134 } ) ;
135135 }
136136
137- static async spawnFromMidenClient ( password : string , mnemonic : string , walletAccounts : WalletAccount [ ] ) {
137+ static async spawnFromMidenClient (
138+ password : string ,
139+ mnemonic : string ,
140+ walletAccounts : WalletAccount [ ] ,
141+ skForImportedAccounts : Record < string , string >
142+ ) {
138143 return withError ( 'Failed to spawn from miden client' , async ( ) => {
139144 await clearStorage ( false ) ;
140145
@@ -160,9 +165,18 @@ export class Vault {
160165 if ( ! walletAccount ) {
161166 throw new PublicError ( 'Account from Miden Client not found in provided wallet accounts' ) ;
162167 }
163- const walletSeed = deriveClientSeed ( walletAccount . type , mnemonic , walletAccount . hdIndex ) ;
164- const secretKey = SecretKey . rpoFalconWithRNG ( walletSeed ) ;
165- await midenClient . webClient . addAccountSecretKeyToWebStore ( secretKey ) ;
168+ if ( walletAccount . hdIndex === - 1 ) {
169+ const skHex = skForImportedAccounts [ walletAccount . publicKey ] ;
170+ if ( ! skHex ) {
171+ throw new PublicError ( 'Secret key for imported account not found' ) ;
172+ }
173+ const sk = SecretKey . deserialize ( new Uint8Array ( Buffer . from ( skHex , 'hex' ) ) ) ;
174+ await midenClient . webClient . addAccountSecretKeyToWebStore ( sk ) ;
175+ } else {
176+ const walletSeed = deriveClientSeed ( walletAccount . type , mnemonic , walletAccount . hdIndex ) ;
177+ const secretKey = SecretKey . rpoFalconWithRNG ( walletSeed ) ;
178+ await midenClient . webClient . addAccountSecretKeyToWebStore ( secretKey ) ;
179+ }
166180 }
167181 } ) ;
168182
@@ -348,11 +362,16 @@ export class Vault {
348362 static async revealPrivateKey ( accPublicKey : string , password : string ) {
349363 const passKey = await Vault . toValidPassKey ( password ) ;
350364 return await withError ( 'Failed to reveal private key' , async ( ) => {
351- const secretKeyHex = await fetchAndDecryptOneWithLegacyFallBack < string > (
352- accAuthSecretKeyStrgKey ( accPublicKey ) ,
353- passKey
354- ) ;
355- return secretKeyHex ;
365+ try {
366+ const secretKeyHex = await fetchAndDecryptOneWithLegacyFallBack < string > (
367+ accAuthSecretKeyStrgKey ( accPublicKey ) ,
368+ passKey
369+ ) ;
370+ return secretKeyHex ;
371+ } catch ( e ) {
372+ console . error ( 'Error fetching and decrypting private key:' , e ) ;
373+ throw e ;
374+ }
356375 } ) ;
357376 }
358377
@@ -363,12 +382,10 @@ export class Vault {
363382 const pubKeyWord = secretKey . publicKey ( ) . toCommitment ( ) ;
364383
365384 const pubKeyHex = pubKeyWord . toHex ( ) . slice ( 2 ) ; // remove '0x' prefix
366- console . log ( { pubKeyHex } ) ;
367385 const publicKey = await withWasmClientLock ( async ( ) => {
368386 const midenClient = await getMidenClient ( ) ;
369387 return await midenClient . importPublicAccountFromPrivateKey ( secretKey ) ;
370388 } ) ;
371- console . log ( { publicKey } ) ;
372389 const newAccount : WalletAccount = {
373390 publicKey,
374391 name : getNewAccountName ( allAccounts ) ,
0 commit comments