Skip to content

Commit

Permalink
fix: resolve feedback
Browse files Browse the repository at this point in the history
Signed-off-by: Timo Glastra <[email protected]>
  • Loading branch information
TimoGlastra committed Mar 9, 2022
1 parent 6e198e4 commit fac0027
Show file tree
Hide file tree
Showing 12 changed files with 31 additions and 28 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/crypto/__tests__/JwsService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('JwsService', () => {
const config = getAgentConfig('JwsService')
wallet = new IndyWallet(config)
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
await wallet.create({ ...config.walletConfig!, keepOpenAfterCreate: true })
await wallet.createAndOpen(config.walletConfig!)

jwsService = new JwsService(wallet)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('Decorators | Signature | SignatureDecoratorUtils', () => {
const config = getAgentConfig('SignatureDecoratorUtilsTest')
wallet = new IndyWallet(config)
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
await wallet.create({ ...config.walletConfig!, keepOpenAfterCreate: true })
await wallet.createAndOpen(config.walletConfig!)
})

afterAll(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('BasicMessageService', () => {
agentConfig = getAgentConfig('BasicMessageServiceTest')
wallet = new IndyWallet(agentConfig)
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
await wallet.create({ ...agentConfig.walletConfig!, keepOpenAfterCreate: true })
await wallet.createAndOpen(agentConfig.walletConfig!)
storageService = new IndyStorageService(wallet, agentConfig)
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('ConnectionService', () => {
beforeAll(async () => {
wallet = new IndyWallet(config)
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
await wallet.create({ ...config.walletConfig!, keepOpenAfterCreate: true })
await wallet.createAndOpen(config.walletConfig!)
})

afterAll(async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/modules/dids/__tests__/peer-did.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('peer dids', () => {
beforeEach(async () => {
wallet = new IndyWallet(config)
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
await wallet.create({ ...config.walletConfig!, keepOpenAfterCreate: true })
await wallet.createAndOpen(config.walletConfig!)

const storageService = new IndyStorageService<DidRecord>(wallet, config)
didRepository = new DidRepository(storageService)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe('IndyLedgerService', () => {
beforeAll(async () => {
wallet = new IndyWallet(config)
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
await wallet.create({ ...config.walletConfig!, keepOpenAfterCreate: true })
await wallet.createAndOpen(config.walletConfig!)
})

afterAll(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('IndyStorageService', () => {
indy = config.agentDependencies.indy
wallet = new IndyWallet(config)
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
await wallet.create({ ...config.walletConfig!, keepOpenAfterCreate: true })
await wallet.createAndOpen(config.walletConfig!)
storageService = new IndyStorageService<TestRecord>(wallet, config)
})

Expand Down
24 changes: 14 additions & 10 deletions packages/core/src/wallet/IndyWallet.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Logger } from '../logger'
import type { EncryptedMessage, DecryptedMessageContext, WalletConfig, WalletExportImportConfig } from '../types'
import type { Buffer } from '../utils/buffer'
import type { Wallet, DidInfo, DidConfig, WalletCreateConfig } from './Wallet'
import type { Wallet, DidInfo, DidConfig } from './Wallet'
import type { default as Indy } from 'indy-sdk'

import { Lifecycle, scoped } from 'tsyringe'
Expand Down Expand Up @@ -64,11 +64,17 @@ export class IndyWallet implements Wallet {
* @throws {WalletDuplicateError} if the wallet already exists
* @throws {WalletError} if another error occurs
*/
public async create(walletConfig: WalletCreateConfig): Promise<void> {
this.logger.debug(`Creating wallet '${walletConfig.id}' using SQLite storage`)
public async create(walletConfig: WalletConfig): Promise<void> {
await this.createAndOpen(walletConfig)
await this.close()
}

// Close wallet by default after creating it
const keepOpenAfterCreate = walletConfig.keepOpenAfterCreate ?? false
/**
* @throws {WalletDuplicateError} if the wallet already exists
* @throws {WalletError} if another error occurs
*/
public async createAndOpen(walletConfig: WalletConfig): Promise<void> {
this.logger.debug(`Creating wallet '${walletConfig.id}' using SQLite storage`)

try {
await this.indy.createWallet(
Expand All @@ -83,12 +89,10 @@ export class IndyWallet implements Wallet {

// We need to open wallet before creating master secret because we need wallet handle here.
await this.createMasterSecret(this.handle, walletConfig.id)

// We opened wallet just to create master secret. Close it if desired
if (!keepOpenAfterCreate) {
await this.close()
}
} catch (error) {
// If an error ocurred while creating the master secret, we should close the wallet
if (this.isInitialized) await this.close()

if (isIndyError(error, 'WalletAlreadyExistsError')) {
const errorMessage = `Wallet '${walletConfig.id}' already exists`
this.logger.debug(errorMessage)
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/wallet/Wallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('Wallet', () => {

test('initialize public did', async () => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
await wallet.create({ ...config.walletConfig!, keepOpenAfterCreate: true })
await wallet.createAndOpen(config.walletConfig!)

await wallet.initPublicDid({ seed: '00000000000000000000000Forward01' })

Expand Down
7 changes: 2 additions & 5 deletions packages/core/src/wallet/Wallet.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import type { EncryptedMessage, DecryptedMessageContext, WalletConfig, WalletExportImportConfig } from '../types'
import type { Buffer } from '../utils/buffer'

export interface WalletCreateConfig extends WalletConfig {
keepOpenAfterCreate?: boolean
}

export interface Wallet {
publicDid: DidInfo | undefined
isInitialized: boolean
isProvisioned: boolean

create(walletConfig: WalletCreateConfig): Promise<void>
create(walletConfig: WalletConfig): Promise<void>
createAndOpen(walletConfig: WalletConfig): Promise<void>
open(walletConfig: WalletConfig): Promise<void>
close(): Promise<void>
delete(): Promise<void>
Expand Down
9 changes: 6 additions & 3 deletions packages/core/src/wallet/WalletModule.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { Logger } from '../logger'
import type { WalletConfig, WalletExportImportConfig } from '../types'
import type { WalletCreateConfig } from './Wallet'

import { inject, Lifecycle, scoped } from 'tsyringe'

Expand Down Expand Up @@ -46,14 +45,18 @@ export class WalletModule {
if (error instanceof WalletNotFoundError) {
// Keep the wallet open after creating it, this saves an extra round trip of closing/opening
// the wallet, which can save quite some time.
await this.create({ ...walletConfig, keepOpenAfterCreate: true })
await this.createAndOpen(walletConfig)
} else {
throw error
}
}
}

public async create(walletConfig: WalletCreateConfig): Promise<void> {
public async createAndOpen(walletConfig: WalletConfig): Promise<void> {
await this.wallet.createAndOpen(walletConfig)
}

public async create(walletConfig: WalletConfig): Promise<void> {
await this.wallet.create(walletConfig)
}

Expand Down
3 changes: 1 addition & 2 deletions packages/core/tests/wallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ describe('wallet', () => {
keyDerivationMethod: KeyDerivationMethod.Argon2IInt,
}

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

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

0 comments on commit fac0027

Please sign in to comment.