Skip to content

Commit

Permalink
feat: possibility to set masterSecretId inside of WalletConfig (#1043)
Browse files Browse the repository at this point in the history
Signed-off-by: Andrii Uhryn <[email protected]>
  • Loading branch information
an-uhryn authored Oct 7, 2022
1 parent 9dd95e8 commit 8a89ad2
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export interface WalletConfig {
type: string
[key: string]: unknown
}
masterSecretId?: string
}

export interface WalletConfigRekey {
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/wallet/IndyWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ export class IndyWallet implements Wallet {
}

public get masterSecretId() {
if (!this.isInitialized || !this.walletConfig?.id) {
if (!this.isInitialized || !(this.walletConfig?.id || this.walletConfig?.masterSecretId)) {
throw new AriesFrameworkError(
'Wallet has not been initialized yet. Make sure to await agent.initialize() before using the agent.'
)
}

return this.walletConfig.id
return this.walletConfig?.masterSecretId ?? this.walletConfig.id
}

private walletStorageConfig(walletConfig: WalletConfig): Indy.WalletConfig {
Expand Down Expand Up @@ -124,7 +124,7 @@ export class IndyWallet implements Wallet {
await this.open(walletConfig)

// We need to open wallet before creating master secret because we need wallet handle here.
await this.createMasterSecret(this.handle, walletConfig.id)
await this.createMasterSecret(this.handle, this.masterSecretId)
} catch (error) {
// If an error ocurred while creating the master secret, we should close the wallet
if (this.isInitialized) await this.close()
Expand Down
38 changes: 35 additions & 3 deletions packages/core/src/wallet/Wallet.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import type { AgentConfig } from '../../src'

import { getAgentConfig } from '../../tests/helpers'

import { IndyWallet } from './IndyWallet'

describe('Wallet', () => {
const config = getAgentConfig('WalletTest')
const wallet = new IndyWallet(config)
let wallet: IndyWallet
let config: AgentConfig
let configWithMasterSecretId: AgentConfig

beforeEach(() => {
config = getAgentConfig('WalletTest')
configWithMasterSecretId = getAgentConfig('WalletTestWithMasterSecretId', {
walletConfig: {
id: `Wallet: WalletTestWithMasterSecretId`,
key: `Key: WalletTestWithMasterSecretId`,
masterSecretId: 'customMasterSecretId',
},
})
})

test('initialize public did', async () => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
wallet = new IndyWallet(config)

await wallet.createAndOpen(config.walletConfig!)

await wallet.initPublicDid({ seed: '00000000000000000000000Forward01' })
Expand All @@ -18,6 +34,22 @@ describe('Wallet', () => {
})
})

test('masterSecretId is equal to wallet ID by default', async () => {
wallet = new IndyWallet(config)

await wallet.createAndOpen(config.walletConfig!)

expect(wallet.masterSecretId).toEqual(config.walletConfig!.id)
})

test('masterSecretId is set by config', async () => {
wallet = new IndyWallet(configWithMasterSecretId)

await wallet.createAndOpen(configWithMasterSecretId.walletConfig!)

expect(wallet.masterSecretId).toEqual(configWithMasterSecretId.walletConfig!.masterSecretId)
})

afterEach(async () => {
await wallet.delete()
})
Expand Down

0 comments on commit 8a89ad2

Please sign in to comment.