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: support wallet key rotation #672

Merged
merged 12 commits into from
Apr 4, 2022
Merged
Show file tree
Hide file tree
Changes from 9 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
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"@multiformats/base-x": "^4.0.1",
"@stablelib/ed25519": "^1.0.2",
"@stablelib/sha256": "^1.0.1",
"@types/indy-sdk": "^1.16.12",
"@types/indy-sdk": "^1.16.15",
"@types/node-fetch": "^2.5.10",
"@types/ws": "^7.4.4",
"abort-controller": "^3.0.0",
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ export interface WalletConfig {
keyDerivationMethod?: KeyDerivationMethod
}

export interface WalletConfigRekey {
MosCD3 marked this conversation as resolved.
Show resolved Hide resolved
id: string
key: string
rekey: string
keyDerivationMethod?: KeyDerivationMethod
rekeyDerivationMethod?: KeyDerivationMethod
}

export interface WalletExportImportConfig {
key: string
path: string
Expand Down
49 changes: 46 additions & 3 deletions packages/core/src/wallet/IndyWallet.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import type { Logger } from '../logger'
import type { EncryptedMessage, DecryptedMessageContext, WalletConfig, WalletExportImportConfig } from '../types'
import type {
EncryptedMessage,
DecryptedMessageContext,
WalletConfig,
WalletExportImportConfig,
WalletConfigRekey,
KeyDerivationMethod,
} from '../types'
import type { Buffer } from '../utils/buffer'
import type { Wallet, DidInfo, DidConfig } from './Wallet'
import type { default as Indy } from 'indy-sdk'
Expand Down Expand Up @@ -120,6 +127,33 @@ export class IndyWallet implements Wallet {
* @throws {WalletError} if another error occurs
*/
public async open(walletConfig: WalletConfig): Promise<void> {
await this._open(walletConfig)
}

/**
* @throws {WalletNotFoundError} if the wallet does not exist
* @throws {WalletError} if another error occurs
*/
public async rotateKey(walletConfig: WalletConfigRekey): Promise<void> {
if (!walletConfig.rekey) {
throw new WalletError('Wallet rekey undefined!. Please specify the new wallet key')
}
await this._open(
{ id: walletConfig.id, key: walletConfig.key, keyDerivationMethod: walletConfig.keyDerivationMethod },
walletConfig.rekey,
walletConfig.rekeyDerivationMethod
)
}

/**
* @throws {WalletNotFoundError} if the wallet does not exist
* @throws {WalletError} if another error occurs
*/
private async _open(
walletConfig: WalletConfig,
rekey?: string,
rekeyDerivation?: KeyDerivationMethod
): Promise<void> {
if (this.walletHandle) {
throw new WalletError(
'Wallet instance already opened. Close the currently opened wallet before re-opening the wallet'
Expand All @@ -129,9 +163,18 @@ export class IndyWallet implements Wallet {
try {
this.walletHandle = await this.indy.openWallet(
{ id: walletConfig.id },
{ key: walletConfig.key, key_derivation_method: walletConfig.keyDerivationMethod }
{
key: walletConfig.key,
rekey: rekey,
key_derivation_method: walletConfig.keyDerivationMethod,
rekey_derivation_method: rekeyDerivation,
}
)
this.walletConfig = walletConfig
if (rekey) {
this.walletConfig = { ...walletConfig, key: rekey, keyDerivationMethod: rekeyDerivation }
} else {
this.walletConfig = walletConfig
}
} catch (error) {
if (isIndyError(error, 'WalletNotFoundError')) {
const errorMessage = `Wallet '${walletConfig.id}' not found`
Expand Down
9 changes: 8 additions & 1 deletion packages/core/src/wallet/Wallet.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import type { EncryptedMessage, DecryptedMessageContext, WalletConfig, WalletExportImportConfig } from '../types'
import type {
EncryptedMessage,
DecryptedMessageContext,
WalletConfig,
WalletExportImportConfig,
WalletConfigRekey,
} from '../types'
import type { Buffer } from '../utils/buffer'

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

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

Expand Down Expand Up @@ -68,6 +68,10 @@ export class WalletModule {
await this.wallet.close()
}

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

public async delete(): Promise<void> {
await this.wallet.delete()
}
Expand Down
23 changes: 23 additions & 0 deletions packages/core/tests/wallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,27 @@ describe('wallet', () => {
// Expect same basic message record to exist in new wallet
expect(await bobBasicMessageRepository.getById(basicMessageRecord.id)).toMatchObject(basicMessageRecord)
})

test('changing wallet key', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is using the same key to rekey the wallet. Would be good to add a test that does the following:

  • create and open the wallet
  • close the wallet
  • rotate the wallet key to a new key
  • open the wallet (using the agent.wallet so you can provide the new key directly -> more explicit)

const walletConfig = {
id: 'mywallet',
key: 'mysecretwalletkey',
}

await aliceAgent.wallet.createAndOpen(walletConfig)
await aliceAgent.initialize()

//Close agent
const walletConfigRekey = {
id: 'mywallet',
key: 'mysecretwalletkey',
rekey: '123',
}

await aliceAgent.shutdown()
await aliceAgent.wallet.rotateKey(walletConfigRekey)
await aliceAgent.initialize()

expect(aliceAgent.isInitialized).toBe(true)
})
})
2 changes: 1 addition & 1 deletion packages/react-native/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"events": "^3.3.0"
},
"devDependencies": {
"@types/indy-sdk-react-native": "npm:@types/indy-sdk@^1.16.12",
"@types/indy-sdk-react-native": "npm:@types/indy-sdk@^1.16.15",
"@types/react-native": "^0.64.10",
"indy-sdk-react-native": "^0.1.16",
"react": "17.0.1",
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2242,10 +2242,10 @@
dependencies:
"@types/node" "*"

"@types/indy-sdk-react-native@npm:@types/indy-sdk@^1.16.12", "@types/indy-sdk@^1.16.12":
version "1.16.12"
resolved "https://registry.npmjs.org/@types/indy-sdk/-/indy-sdk-1.16.12.tgz#7b6ad4e4ebf11125bd77f0ef98cf727d0262c4b7"
integrity sha512-6uyHSSAoM+eKQD4XF+KohAjbkDN6D9DnriYWlGi/pLCWkd74kCcEMlm7/REqfMkAgxL52wh7Cyzir+cnIi342g==
"@types/indy-sdk-react-native@npm:@types/indy-sdk@^1.16.15", "@types/indy-sdk@^1.16.15":
version "1.16.16"
resolved "https://registry.yarnpkg.com/@types/indy-sdk/-/indy-sdk-1.16.16.tgz#dc9e51c61c266eff616991fa7df88309975d9f1e"
integrity sha512-bSWG56bAFvjTTLHJoGEXylbbN533g9XEtOTPS9G1EvIv5AsVcy/OIZOkXXElbzvYdZ1Q+qCxZDD5T0OqMYNmjg==
dependencies:
buffer "^6.0.0"

Expand Down