Skip to content

Commit

Permalink
feat: add support for exporting DID details to W3C-compliant DID Docu…
Browse files Browse the repository at this point in the history
…ment (#411)

* Merge branch 'develop' into aa-w3c-diddoc-exporter

Co-authored-by: Antonio Antonino <antonio@kilt.io>

* feat: add export to DID Document for light DIDs

* test: add unit tests for light DID to DID Document export

* test: add more tests for different keys for light DID W3C exporter

* test: add edge cases for light DID Document exporter

* test: add unit tests for full DID details exporting

* chore: move exportToDidDocument to utils file

* chore: restructure code to support multiple DID Document formats

* chore: restructure code to support multiple DID Document formats pt. 2

* Merge branch 'develop' into aa-w3c-diddoc-exporter

* fix: fix after merging from develop

* fix: fix unit tests for DID exporter

* chore: refactor imports

* chore: remove unused .toDidDocument()

* fix: unused import

* doc: add README for DIDDocument exporter

* doc: add missing import statement in exporter README

* chore: bump packages version to 0.24.1-rc.1

* fix: do not include empty fields in final DID Document

* fix: update naming to match universal resolver

* fix: revert version to 0.24.0-1

* chore: add comment about allowed values to export a DID Document

* test: add completed DID Document in exporter test cases

* chore: minor refinements

Co-authored-by: Raphael Flechtner <39338561+rflechtner@users.noreply.github.com>
  • Loading branch information
ntn-x2 and rflechtner authored Sep 20, 2021
1 parent 0b05a87 commit 9cf4d99
Show file tree
Hide file tree
Showing 16 changed files with 760 additions and 35 deletions.
37 changes: 18 additions & 19 deletions packages/kilt-did/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ const authenticationKeyPublicDetails = await keystore.generateKeypair({
const lightDID = new LightDidDetails({
authenticationKey: {
publicKey: authenticationKeyPublicDetails.publicKey,
type: DemoKeystore.getKeypairTypeForAlg(
authenticationKeyPublicDetails.alg
)
type: DemoKeystore.getKeypairTypeForAlg(authenticationKeyPublicDetails.alg),
},
})
// Will print `did:kilt:light:014sxSYXakw1ZXBymzT9t3Yw91mUaqKST5bFUEjGEpvkTuckar`.
Expand Down Expand Up @@ -102,15 +100,11 @@ const serviceEndpoints: IServiceDetails[] = [
const lightDID = new LightDidDetails({
authenticationKey: {
publicKey: authenticationKeyPublicDetails.publicKey,
type: DemoKeystore.getKeypairTypeForAlg(
authenticationKeyPublicDetails.alg
),
type: DemoKeystore.getKeypairTypeForAlg(authenticationKeyPublicDetails.alg),
},
encryptionKey: {
publicKey: encryptionKeyPublicDetails.publicKey,
type: DemoKeystore.getKeypairTypeForAlg(
encryptionKeyPublicDetails.alg
),
type: DemoKeystore.getKeypairTypeForAlg(encryptionKeyPublicDetails.alg),
},
services: serviceEndpoints,
})
Expand Down Expand Up @@ -151,11 +145,17 @@ import {
FullDidDetails,
SigningAlgorithms,
} from '@kiltprotocol/did'
import { getDeleteDidExtrinsic, getSetKeyExtrinsic } from '@kiltprotocol/did/src/Did.chain'
import {
getDeleteDidExtrinsic,
getSetKeyExtrinsic,
} from '@kiltprotocol/did/src/Did.chain'
import { KeyRelationship } from '@kiltprotocol/types'

// Configure the resolution promise to wait for transactions to be finalized or simply included in a block depending on the environment.
const resolveOn = process.env.NODE_ENV === 'production' ? BlockchainUtils.IS_FINALIZED : BlockchainUtils.IS_IN_BLOCK
const resolveOn =
process.env.NODE_ENV === 'production'
? BlockchainUtils.IS_FINALIZED
: BlockchainUtils.IS_IN_BLOCK

// Initialise connection to the public KILT test network.
await kiltInit({ address: 'wss://kilt-peregrine-k8s.kilt.io' })
Expand Down Expand Up @@ -185,9 +185,7 @@ const authenticationKeyPublicDetails = await keystore.generateKeypair({
const { extrinsic, did } = await DidUtils.writeDidFromPublicKeys(keystore, {
[KeyRelationship.authentication]: {
publicKey: authenticationKeyPublicDetails.publicKey,
type: DemoKeystore.getKeypairTypeForAlg(
authenticationKeyPublicDetails.alg
),
type: DemoKeystore.getKeypairTypeForAlg(authenticationKeyPublicDetails.alg),
},
})
// Will print `did:kilt:4sxSYXakw1ZXBymzT9t3Yw91mUaqKST5bFUEjGEpvkTuckar`.
Expand All @@ -205,7 +203,10 @@ const fullDid = await DefaultResolver.resolveDoc(did)
If additional keys and external services are to be specified, then they can be included in the DID create operation.

```typescript
const resolveOn = process.env.NODE_ENV === 'production' ? BlockchainUtils.IS_FINALIZED : BlockchainUtils.IS_IN_BLOCK
const resolveOn =
process.env.NODE_ENV === 'production'
? BlockchainUtils.IS_FINALIZED
: BlockchainUtils.IS_IN_BLOCK

await kiltInit({ address: 'wss://kilt-peregrine-k8s.kilt.io' })

Expand Down Expand Up @@ -255,9 +256,7 @@ const { extrinsic, did } = await DidUtils.writeDidFromPublicKeys(
},
[KeyRelationship.keyAgreement]: {
publicKey: encryptionKeyPublicDetails.publicKey,
type: DemoKeystore.getKeypairTypeForAlg(
encryptionKeyPublicDetails.alg
),
type: DemoKeystore.getKeypairTypeForAlg(encryptionKeyPublicDetails.alg),
},
},
serviceEndpoints
Expand Down Expand Up @@ -340,4 +339,4 @@ await BlockchainUtils.signAndSubmitTx(

## Migrating a light DID to a full DID

This feature is a work in progress and this section will be updated when it will be added to the SDK.
This feature is a work in progress and this section will be updated when it will be added to the SDK.
3 changes: 2 additions & 1 deletion packages/kilt-did/src/DidDetails/FullDidDetails.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@

import { KeyRelationship } from '@kiltprotocol/types'
import { BN } from '@polkadot/util'
import type { IDidKeyDetails } from '@kiltprotocol/types'
import { mapCallToKeyRelationship } from './FullDidDetails.utils'
import { FullDidDetails, FullDidDetailsCreationOpts } from './FullDidDetails'

describe('functional tests', () => {
const identifier = '4rp4rcDHP71YrBNvDhcH5iRoM3YzVoQVnCZvQPwPom9bjo2e'
const did = `did:kilt:${identifier}`
const keys = [
const keys: IDidKeyDetails[] = [
{
id: `${did}#1`,
controller: did,
Expand Down
4 changes: 2 additions & 2 deletions packages/kilt-did/src/DidDetails/LightDidDetails.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
* @group unit/did
*/

import { Crypto } from '@kiltprotocol/utils'
import type { IServiceDetails } from '@kiltprotocol/types'
import { hexToU8a } from '@polkadot/util'
import { encodeAddress } from '@polkadot/util-crypto'
import { LightDidDetails, LightDidDetailsCreationOpts } from './LightDidDetails'
import type { INewPublicKey } from '../types'

Expand All @@ -24,7 +24,7 @@ describe('Light DID v1 tests', () => {
const encPublicKey = hexToU8a(
'0xbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'
)
const address = Crypto.encodeAddress(authPublicKey, 38)
const address = encodeAddress(authPublicKey, 38)
const authenticationDidKeyDetails: INewPublicKey = {
publicKey: authPublicKey,
type: 'ed25519',
Expand Down
Loading

0 comments on commit 9cf4d99

Please sign in to comment.