Skip to content

Commit

Permalink
feat: add wallet key derivation method option (#650)
Browse files Browse the repository at this point in the history
Signed-off-by: Timo Glastra <[email protected]>
  • Loading branch information
TimoGlastra authored Mar 2, 2022
1 parent 810d7bb commit 8386506
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 12 deletions.
10 changes: 10 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,19 @@ import type { IndyPoolConfig } from './modules/ledger/IndyPool'
import type { AutoAcceptProof } from './modules/proofs'
import type { MediatorPickupStrategy } from './modules/routing'

export const enum KeyDerivationMethod {
/** default value in indy-sdk. Will be used when no value is provided */
Argon2IMod = 'ARGON2I_MOD',
/** less secure, but faster */
Argon2IInt = 'ARGON2I_INT',
/** raw wallet master key */
Raw = 'RAW',
}

export interface WalletConfig {
id: string
key: string
keyDerivationMethod?: KeyDerivationMethod
}

export type EncryptedMessage = {
Expand Down
27 changes: 15 additions & 12 deletions packages/core/src/wallet/IndyWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ export class IndyWallet implements Wallet {
this.logger.debug(`Creating wallet '${walletConfig.id}' using SQLite storage`)

try {
await this.indy.createWallet({ id: walletConfig.id }, { key: walletConfig.key })
await this.indy.createWallet(
{ id: walletConfig.id },
{ key: walletConfig.key, key_derivation_method: walletConfig.keyDerivationMethod }
)

this.walletConfig = {
id: walletConfig.id,
key: walletConfig.key,
}
this.walletConfig = walletConfig

// We usually want to create master secret only once, therefore, we can to do so when creating a wallet.
await this.open(walletConfig)
Expand Down Expand Up @@ -141,11 +141,11 @@ export class IndyWallet implements Wallet {
}

try {
this.walletHandle = await this.indy.openWallet({ id: walletConfig.id }, { key: walletConfig.key })
this.walletConfig = {
id: walletConfig.id,
key: walletConfig.key,
}
this.walletHandle = await this.indy.openWallet(
{ id: walletConfig.id },
{ key: walletConfig.key, key_derivation_method: walletConfig.keyDerivationMethod }
)
this.walletConfig = walletConfig
} catch (error) {
if (isIndyError(error, 'WalletNotFoundError')) {
const errorMessage = `Wallet '${walletConfig.id}' not found`
Expand Down Expand Up @@ -192,7 +192,10 @@ export class IndyWallet implements Wallet {
}

try {
await this.indy.deleteWallet({ id: this.walletConfig.id }, { key: this.walletConfig.key })
await this.indy.deleteWallet(
{ id: this.walletConfig.id },
{ key: this.walletConfig.key, key_derivation_method: this.walletConfig.keyDerivationMethod }
)
} catch (error) {
if (isIndyError(error, 'WalletNotFoundError')) {
const errorMessage = `Error deleting wallet: wallet '${this.walletConfig.id}' not found`
Expand All @@ -219,7 +222,7 @@ export class IndyWallet implements Wallet {
*/
public async close(): Promise<void> {
if (!this.walletHandle) {
throw new WalletError('Wallet is in inavlid state, you are trying to close wallet that has no `walletHandle`.')
throw new WalletError('Wallet is in invalid state, you are trying to close wallet that has no `walletHandle`.')
}

try {
Expand Down
14 changes: 14 additions & 0 deletions packages/core/tests/wallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Subject } from 'rxjs'
import { SubjectInboundTransport } from '../../../tests/transport/SubjectInboundTransport'
import { SubjectOutboundTransport } from '../../../tests/transport/SubjectOutboundTransport'
import { Agent } from '../src/agent/Agent'
import { KeyDerivationMethod } from '../src/types'

import { getBaseConfig } from './helpers'

Expand Down Expand Up @@ -101,4 +102,17 @@ describe('=== wallet', () => {

await expect(aliceAgent.wallet.open(walletConfig)).resolves.toBeUndefined()
})

test('create wallet with custom key derivation method', async () => {
const walletConfig = {
id: 'mywallet',
key: 'mysecretwalletkey',
keyDerivationMethod: KeyDerivationMethod.Argon2IInt,
}

await aliceAgent.wallet.create(walletConfig)
await aliceAgent.wallet.open(walletConfig)

expect(aliceAgent.wallet.isInitialized).toBe(true)
})
})

0 comments on commit 8386506

Please sign in to comment.