Skip to content

Commit

Permalink
feat: add wallet import and check wallet pin methods
Browse files Browse the repository at this point in the history
Signed-off-by: Sai Ranjit Tummalapalli <sairanjit.tummalapalli@ayanworks.com>
  • Loading branch information
sairanjit committed Oct 5, 2023
1 parent c30a499 commit f9a7187
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 3 deletions.
5 changes: 4 additions & 1 deletion packages/ssi/src/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ import { anoncreds } from '@hyperledger/anoncreds-react-native'
import { ariesAskar } from '@hyperledger/aries-askar-react-native'
import { indyVdr } from '@hyperledger/indy-vdr-react-native'

const getAgentModules = (mediatorInvitationUrl: string, indyNetworks: [IndyVdrPoolConfig, ...IndyVdrPoolConfig[]]) => {
export const getAgentModules = (
mediatorInvitationUrl: string,
indyNetworks: [IndyVdrPoolConfig, ...IndyVdrPoolConfig[]]
) => {
return {
askar: new AskarModule({
ariesAskar
Expand Down
5 changes: 3 additions & 2 deletions packages/ssi/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ import { LogLevel, ConsoleLogger } from '@aries-framework/core'
// Indy VDR
import { IndyVdrPoolConfig } from '@aries-framework/indy-vdr'

export { initializeAgent, AdeyaAgent } from './agent'
export { LogLevel, ConsoleLogger, InitConfig, IndyVdrPoolConfig }
export * from './providers'
export * from './hooks'
export * from './wallet'
export { initializeAgent, AdeyaAgent } from './agent'
export { LogLevel, ConsoleLogger, InitConfig, IndyVdrPoolConfig }
export {
V1RequestPresentationMessage,
AnonCredsCredentialOffer,
Expand Down
3 changes: 3 additions & 0 deletions packages/ssi/src/wallet/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { importWalletWithAgent, isWalletImportable, isWalletPinCorrect } from './wallet'

export { importWalletWithAgent, isWalletImportable, isWalletPinCorrect }
109 changes: 109 additions & 0 deletions packages/ssi/src/wallet/wallet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// NOTE: We need to import these to be able to use the AskarWallet in this file.
import '@hyperledger/aries-askar-react-native'

import type { InitConfig } from '@aries-framework/core'
import type { IndyVdrPoolConfig } from '@aries-framework/indy-vdr'

import { AskarWallet } from '@aries-framework/askar'
import {
Agent,
ConsoleLogger,
HttpOutboundTransport,
LogLevel,
SigningProviderRegistry,
WsOutboundTransport,
type WalletConfig,
type WalletExportImportConfig
} from '@aries-framework/core'
import { agentDependencies } from '@aries-framework/react-native'

import { getAgentModules } from '../agent'

interface WalletImportConfigWithAgent {
agentConfig: InitConfig
importConfig: WalletExportImportConfig
mediatorInvitationUrl: string
indyNetworks: [IndyVdrPoolConfig, ...IndyVdrPoolConfig[]]
}

export const isWalletPinCorrect = async (walletConfig: WalletConfig) => {
try {
// NOTE: a custom wallet is used to check if the wallet key is correct. This is different from the wallet used in the rest of the app.
// We create an AskarWallet instance and open the wallet with the given secret.
const askarWallet = new AskarWallet(
new ConsoleLogger(LogLevel.off),
new agentDependencies.FileSystem(),
new SigningProviderRegistry([])
)
await askarWallet.open(walletConfig)

await askarWallet.close()
return true
} catch (e) {
// eslint-disable-next-line no-console
console.log('Error opening wallet', e)
return false
}
}

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.off), 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
}
}

export const importWalletWithAgent = async ({
importConfig,
agentConfig,
mediatorInvitationUrl,
indyNetworks
}: WalletImportConfigWithAgent) => {
if (!agentConfig.walletConfig?.id || !agentConfig.walletConfig.key) {
// Cannot find wallet id/key in agent config, so we cannot import the wallet
return
}

if (!importConfig.key || !importConfig.path) {
throw new Error('Please enter a valid passphrase')
}

const agent = new Agent({
dependencies: agentDependencies,
config: {
autoUpdateStorageOnStartup: true,
...agentConfig
},
modules: getAgentModules(mediatorInvitationUrl, indyNetworks)
})

agent.registerOutboundTransport(new HttpOutboundTransport())
agent.registerOutboundTransport(new WsOutboundTransport())

await agent.wallet.import(agentConfig.walletConfig, importConfig)

await agent.wallet.initialize(agentConfig.walletConfig)

await agent.initialize()

return agent
}

0 comments on commit f9a7187

Please sign in to comment.