Skip to content

Commit

Permalink
fix: check if wallet is importable (#15)
Browse files Browse the repository at this point in the history
Signed-off-by: Sai Ranjit Tummalapalli <[email protected]>
  • Loading branch information
sairanjit authored Nov 10, 2023
1 parent 627deea commit a57f1d7
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions packages/ssi/src/wallet/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,39 @@ export const exportWallet = async (agent: Agent, exportConfig: WalletExportImpor
await agent.wallet.export(exportConfig)
}

/**
* Checks if a wallet can be imported successfully with the given configuration.
*
* @param walletConfig The configuration for the wallet.
* @param importConfig The configuration for importing the wallet.
* @returns A Promise that resolves to a boolean indicating whether the wallet can be imported successfully.
*/
export const isWalletImportable = async (
walletConfig: WalletConfig,
importConfig: WalletExportImportConfig
): Promise<boolean> => {
const fileSystem = new agentDependencies.FileSystem()
try {
const tempImportPath = fileSystem.tempPath + '/importTemp'
// Add temp path to wallet config
walletConfig.storage = {
type: 'sqlite',
path: tempImportPath
}
// NOTE: a custom wallet is used to check if the wallet passphrase is correct and can be imported successfully.
const askarWallet = new AskarWallet(new ConsoleLogger(LogLevel.debug), fileSystem, new SigningProviderRegistry([]))
await askarWallet.import(walletConfig, importConfig)

await fileSystem.delete(importConfig.path)
return true
} catch (e) {
// eslint-disable-next-line no-console
console.log('Error importing wallet', e)
await fileSystem.delete(importConfig.path)
return false
}
}

/**
* Imports a wallet with an agent.
*
Expand Down

0 comments on commit a57f1d7

Please sign in to comment.