Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add wallet key derivation method option #650

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})
})