-
Notifications
You must be signed in to change notification settings - Fork 204
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 module with import export #652
Merged
TimoGlastra
merged 4 commits into
openwallet-foundation:main
from
TimoGlastra:feature/wallet-module
Mar 10, 2022
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7f89960
feat: add wallet module with import export
TimoGlastra 2576b0b
Merge branch 'main' into feature/wallet-module
TimoGlastra 6e198e4
Merge remote-tracking branch 'upstream/main' into feature/wallet-module
TimoGlastra fac0027
fix: resolve feedback
TimoGlastra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import type { Logger } from '../logger' | ||
import type { WalletConfig, WalletExportImportConfig } from '../types' | ||
import type { WalletCreateConfig } from './Wallet' | ||
|
||
import { inject, Lifecycle, scoped } from 'tsyringe' | ||
|
||
import { AgentConfig } from '../agent/AgentConfig' | ||
import { InjectionSymbols } from '../constants' | ||
|
||
import { Wallet } from './Wallet' | ||
import { WalletError } from './error/WalletError' | ||
import { WalletNotFoundError } from './error/WalletNotFoundError' | ||
|
||
@scoped(Lifecycle.ContainerScoped) | ||
export class WalletModule { | ||
private wallet: Wallet | ||
private logger: Logger | ||
|
||
public constructor(@inject(InjectionSymbols.Wallet) wallet: Wallet, agentConfig: AgentConfig) { | ||
this.wallet = wallet | ||
this.logger = agentConfig.logger | ||
} | ||
|
||
public get isInitialized() { | ||
return this.wallet.isInitialized | ||
} | ||
|
||
public get isProvisioned() { | ||
return this.wallet.isProvisioned | ||
} | ||
|
||
public async initialize(walletConfig: WalletConfig): Promise<void> { | ||
this.logger.info(`Initializing wallet '${walletConfig.id}'`, walletConfig) | ||
|
||
if (this.isInitialized) { | ||
throw new WalletError( | ||
'Wallet instance already initialized. Close the currently opened wallet before re-initializing the wallet' | ||
) | ||
} | ||
|
||
// Open wallet, creating if it doesn't exist yet | ||
try { | ||
await this.open(walletConfig) | ||
} catch (error) { | ||
// If the wallet does not exist yet, create it and try to open again | ||
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 }) | ||
} else { | ||
throw error | ||
} | ||
} | ||
} | ||
|
||
public async create(walletConfig: WalletCreateConfig): Promise<void> { | ||
await this.wallet.create(walletConfig) | ||
} | ||
|
||
public async open(walletConfig: WalletConfig): Promise<void> { | ||
await this.wallet.open(walletConfig) | ||
} | ||
|
||
public async close(): Promise<void> { | ||
await this.wallet.close() | ||
} | ||
|
||
public async delete(): Promise<void> { | ||
await this.wallet.delete() | ||
} | ||
|
||
public async export(exportConfig: WalletExportImportConfig): Promise<void> { | ||
await this.wallet.export(exportConfig) | ||
} | ||
|
||
public async import(walletConfig: WalletConfig, importConfig: WalletExportImportConfig): Promise<void> { | ||
await this.wallet.import(walletConfig, importConfig) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems a bit weird it's named
walletService
although it's not a service.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if we expose
initPublicDid
andpublicDid
via thewallet
"module"?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. Do you have any suggestions of the naming?
I would rather not do that at the moment. With the work happening on other did methods and the did module, I'd like to move away from the
initPublicDid
andpublicDid
. With supporting multiple types of dids, it'll be more common to have different kind of public dids, sopublicDid
andinitPublicDid
won't make a lot of sense anymore. So I'd rather not expose that API right now, but rather keep it as is currently (wallet api has only been exposed after 0.1.0 release, so not a breaking change to not make them public).We can then start looking at how to handle public dids in general through the dids module. The wallet can then focus more on providing the actual crypto instead of dealing with DIDs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. Yes, dids module is a better place for this functionality. That can help with
walletService
naming issue. We wouldn't need to have a class member referring toWallet
instance.