-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(did): parse identifier from document id. * feat(did): backup and restore did keys
- Loading branch information
1 parent
4afa955
commit bfb9914
Showing
10 changed files
with
189 additions
and
9 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright 2021-2022 zcloak authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { generateMnemonic } from '@zcloak/crypto'; | ||
import { Keyring } from '@zcloak/keyring'; | ||
|
||
import { createEcdsaFromMnemonic } from '../did/helpers'; | ||
import { backup, getEcdsaIdentifierPair } from './backup'; | ||
import { DEFAULT_DID_KEYS_JSON_VERSION } from './defaults'; | ||
|
||
describe('Backup did', (): void => { | ||
const keyring = new Keyring(); | ||
|
||
it('get ecdsa identifier pair', () => { | ||
const mnemonic = generateMnemonic(12); | ||
const did = createEcdsaFromMnemonic(mnemonic, keyring); | ||
|
||
expect(getEcdsaIdentifierPair(keyring, did)?.publicKey).toEqual( | ||
keyring.createFromMnemonic(mnemonic, undefined, 'ecdsa').publicKey | ||
); | ||
}); | ||
|
||
it('backup did', () => { | ||
const mnemonic = generateMnemonic(12); | ||
const did = createEcdsaFromMnemonic(mnemonic, keyring); | ||
|
||
expect(backup(keyring, did, '1234')).toMatchObject({ | ||
didUrl: did.id, | ||
version: DEFAULT_DID_KEYS_JSON_VERSION, | ||
authentication: Array.from(did.authentication ?? []), | ||
assertionMethod: Array.from(did.assertionMethod ?? []), | ||
keyAgreement: Array.from(did.keyAgreement ?? []), | ||
capabilityInvocation: Array.from(did.capabilityInvocation ?? []), | ||
capabilityDelegation: Array.from(did.capabilityDelegation ?? []) | ||
}); | ||
}); | ||
}); |
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,43 @@ | ||
// Copyright 2021-2022 zcloak authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import type { KeyringPair } from '@zcloak/keyring/types'; | ||
import type { Did } from '../did'; | ||
import type { DidKeys$Json } from './types'; | ||
|
||
import { assert } from '@polkadot/util'; | ||
|
||
import { ethereumEncode } from '@zcloak/crypto'; | ||
import { Keyring } from '@zcloak/keyring'; | ||
|
||
import { DEFAULT_DID_KEYS_JSON_VERSION } from './defaults'; | ||
|
||
export function getEcdsaIdentifierPair(keyring: Keyring, did: Did): KeyringPair | undefined { | ||
const identifierPair = keyring | ||
.getPairs() | ||
.find((pair) => ethereumEncode(pair.publicKey) === did.identifier); | ||
|
||
return identifierPair; | ||
} | ||
|
||
export function backup(keyring: Keyring, did: Did, password: string): DidKeys$Json { | ||
const identifierPair = getEcdsaIdentifierPair(keyring, did); | ||
|
||
assert(identifierPair, 'no identifier pair found'); | ||
|
||
return { | ||
didUrl: did.id, | ||
version: DEFAULT_DID_KEYS_JSON_VERSION, | ||
identifierKey: identifierPair.toJson(password), | ||
keys: Array.from(did.keyRelationship.values()).map(({ publicKey }) => { | ||
const pair = did.getPair(publicKey); | ||
|
||
return pair.toJson(password); | ||
}), | ||
authentication: Array.from(did.authentication ?? []), | ||
assertionMethod: Array.from(did.assertionMethod ?? []), | ||
keyAgreement: Array.from(did.keyAgreement ?? []), | ||
capabilityInvocation: Array.from(did.capabilityInvocation ?? []), | ||
capabilityDelegation: Array.from(did.capabilityDelegation ?? []) | ||
}; | ||
} |
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,6 @@ | ||
// Copyright 2021-2022 zcloak authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { DidKeys$JsonVersion } from './types'; | ||
|
||
export const DEFAULT_DID_KEYS_JSON_VERSION: DidKeys$JsonVersion = '1'; |
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,5 @@ | ||
// Copyright 2021-2022 zcloak authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
export * from './backup'; | ||
export * from './restore'; |
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,22 @@ | ||
// Copyright 2021-2022 zcloak authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { generateMnemonic } from '@zcloak/crypto'; | ||
import { Keyring } from '@zcloak/keyring'; | ||
|
||
import { createEcdsaFromMnemonic } from '../did/helpers'; | ||
import { backup } from './backup'; | ||
import { restore } from './restore'; | ||
|
||
describe('Restore did', (): void => { | ||
const keyring = new Keyring(); | ||
|
||
it('backup did and restore', () => { | ||
const mnemonic = generateMnemonic(12); | ||
const did = createEcdsaFromMnemonic(mnemonic, keyring); | ||
|
||
const json = backup(keyring, did, '1234'); | ||
|
||
expect(restore(keyring, json, '1234')).toMatchObject(did); | ||
}); | ||
}); |
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,47 @@ | ||
// Copyright 2021-2022 zcloak authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import type { DidUrl } from '@zcloak/did-resolver/types'; | ||
import type { Keyring } from '@zcloak/keyring'; | ||
import type { DidKeys$Json } from './types'; | ||
|
||
import { Did } from '../did'; | ||
import { create } from '../did/helpers'; | ||
import { IDidDetails, KeyRelationship } from '../types'; | ||
|
||
export function restore(keyring: Keyring, json: DidKeys$Json, password: string): Did { | ||
const keyRelationship = new Map<DidUrl, KeyRelationship>(); | ||
|
||
json.keys.forEach((key, index) => { | ||
const pair = keyring.addFromJson(key); | ||
|
||
pair.unlock(password); | ||
|
||
const id: DidUrl = `${json.didUrl}#key-${index}`; | ||
const controller: DidUrl[] = [`${json.didUrl}`]; | ||
const publicKey = pair.publicKey; | ||
|
||
keyRelationship.set(id, { | ||
id, | ||
controller, | ||
publicKey | ||
}); | ||
}); | ||
const pair = keyring.addFromJson(json.identifierKey); | ||
|
||
pair.unlock(password); | ||
|
||
const details: IDidDetails = { | ||
id: json.didUrl, | ||
controller: new Set([json.didUrl]), | ||
keyRelationship, | ||
authentication: new Set(json.authentication), | ||
assertionMethod: new Set(json.assertionMethod), | ||
keyAgreement: new Set(json.keyAgreement), | ||
capabilityInvocation: new Set(json.capabilityInvocation), | ||
capabilityDelegation: new Set(json.capabilityDelegation), | ||
service: new Map() | ||
}; | ||
|
||
return create(details, keyring); | ||
} |
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,19 @@ | ||
// Copyright 2021-2022 zcloak authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import type { DidUrl } from '@zcloak/did-resolver/types'; | ||
import type { KeyringPair$Json } from '@zcloak/keyring/types'; | ||
|
||
export type DidKeys$JsonVersion = '1'; | ||
|
||
export interface DidKeys$Json { | ||
didUrl: DidUrl; | ||
version: DidKeys$JsonVersion; | ||
identifierKey: KeyringPair$Json; | ||
keys: KeyringPair$Json[]; | ||
authentication: DidUrl[]; | ||
assertionMethod: DidUrl[]; | ||
keyAgreement: DidUrl[]; | ||
capabilityInvocation: DidUrl[]; | ||
capabilityDelegation: DidUrl[]; | ||
} |
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 |
---|---|---|
@@ -1,15 +1,11 @@ | ||
{ | ||
"extends": "./tsconfig.base.json", | ||
"exclude": [ | ||
"build/**/*", | ||
"**/build/**/*" | ||
], | ||
"compilerOptions": { | ||
"baseUrl": "./packages", | ||
"composite": false | ||
}, | ||
"include": [ | ||
"packages/**/src/**/*", | ||
"packages/**/scripts/**/*", | ||
"packages/**/test/**/*" | ||
], | ||
"exclude": [ | ||
"**/node_modules/**/*" | ||
] | ||
} | ||
} |