Skip to content

Commit dff7b0f

Browse files
committed
feat: add encryption key support for ethr-did-documents
fixes decentralized-identity#52
1 parent 1d3d326 commit dff7b0f

File tree

3 files changed

+88
-7
lines changed

3 files changed

+88
-7
lines changed

README.md

+41-6
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,25 @@ While any attribute can be stored. For the DID document we currently support add
145145

146146
The name of the attribute should follow this format:
147147

148-
`did/pub/(Secp256k1|RSA|Ed25519)/(veriKey|sigAuth)/(hex|base64)`
148+
`did/pub/(Secp256k1|RSA|Ed25519|X25519)/(veriKey|sigAuth|enc)/(hex|base64)`
149149

150-
#### Hex encoded Secp256k1 Verification Key
150+
(Essentially `did/pub/<key algorithm>/<key purpose>/<encoding>`)
151+
152+
#### Key purposes
153+
154+
- `veriKey` adds a `<key algorithm>VerificationKey2018` to the `publicKey` section of document
155+
- `sigAuth` adds a `<key algorithm>SignatureAuthentication2018` to the `publicKey` section of document. An entry is also added to the `authentication` section of document.
156+
- `enc` adds a `<key algorithm>KeyAgreementKey2019` to the `publicKey` section.
157+
This is used to perform a Diffie-Hellman key exchange and derive a secret key for encrypting messages to the DID that lists such a key.
158+
159+
> **Note** The `<encoding>` only refers to the key encoding in the resolved DID document.
160+
> Attribute values should always be hex encoded.
151161
152-
A `DIDAttributeChanged` event for the identity `0xf3beac30c498d9e26865f34fcaa57dbb935b0d74` with the name `did/pub/Secp256k1/veriKey/hex` and the value of `0x02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71` generates a `PublicKey` entry like this:
162+
#### Example hex encoded Secp256k1 Verification Key
163+
164+
A `DIDAttributeChanged` event for the identity `0xf3beac30c498d9e26865f34fcaa57dbb935b0d74` with the name
165+
`did/pub/Secp256k1/veriKey/hex` and the value of `0x02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71`
166+
generates a `PublicKey` entry like this:
153167

154168
```javascript
155169
{
@@ -162,7 +176,9 @@ A `DIDAttributeChanged` event for the identity `0xf3beac30c498d9e26865f34fcaa57d
162176

163177
#### Base64 encoded Ed25519 Verification Key
164178

165-
A `DIDAttributeChanged` event for the identity `0xf3beac30c498d9e26865f34fcaa57dbb935b0d74` with the name `did/pub/Ed25519/veriKey/base64` and the value of `0xb97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71` generates a `PublicKey` entry like this:
179+
A `DIDAttributeChanged` event for the identity `0xf3beac30c498d9e26865f34fcaa57dbb935b0d74` with the name
180+
`did/pub/Ed25519/veriKey/base64` and the value of
181+
`0xb97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71` generates a `PublicKey` entry like this:
166182

167183
```javascript
168184
{
@@ -173,6 +189,22 @@ A `DIDAttributeChanged` event for the identity `0xf3beac30c498d9e26865f34fcaa57d
173189
}
174190
```
175191

192+
#### Base64 encoded X25519 Encryption Key
193+
194+
A `DIDAttributeChanged` event for the identity `0xf3beac30c498d9e26865f34fcaa57dbb935b0d74` with the name
195+
`did/pub/X25519/enc/base64` and the value of
196+
`0x302a300506032b656e032100118557777ffb078774371a52b00fed75561dcf975e61c47553e664a617661052`
197+
generates a `PublicKey` entry like this:
198+
199+
```javascript
200+
{
201+
id: "did:ethr:0xf3beac30c498d9e26865f34fcaa57dbb935b0d74#delegate-1",
202+
type: "X25519KeyAgreementKey2019",
203+
owner: "did:ethr:0xf3beac30c498d9e26865f34fcaa57dbb935b0d74",
204+
publicKeyBase64: "MCowBQYDK2VuAyEAEYVXd3/7B4d0NxpSsA/tdVYdz5deYcR1U+ZkphdmEFI="
205+
}
206+
```
207+
176208
We are looking for people to submit support for `pem`, `base58` and `jwk` key formats as well.
177209

178210
### Service Endpoints
@@ -183,7 +215,9 @@ The name of the attribute should follow this format:
183215

184216
#### Hex encoded Secp256k1 Verification Key
185217

186-
A `DIDAttributeChanged` event for the identity `0xf3beac30c498d9e26865f34fcaa57dbb935b0d74` with the name `did/svc/HubService` and value of the url `https://hubs.uport.me` hex encoded as `0x68747470733a2f2f687562732e75706f72742e6d65` generates a `Service` entry like this:
218+
A `DIDAttributeChanged` event for the identity `0xf3beac30c498d9e26865f34fcaa57dbb935b0d74` with the name
219+
`did/svc/HubService` and value of the url `https://hubs.uport.me` hex encoded as
220+
`0x68747470733a2f2f687562732e75706f72742e6d65` generates a `Service` entry like this:
187221

188222
```javascript
189223
{
@@ -195,7 +229,8 @@ A `DIDAttributeChanged` event for the identity `0xf3beac30c498d9e26865f34fcaa57d
195229
## Resolving a DID document
196230

197231
The library presents a `resolver()` function that returns a ES6 Promise returning the DID document.
198-
It is not meant to be used directly but through the [`did-resolver`](https://github.com/decentralized-identity/did-resolver) aggregator.
232+
It is not meant to be used directly but through the
233+
[`did-resolver`](https://github.com/decentralized-identity/did-resolver) aggregator.
199234
You can use the `getResolver(conf)` method to produce an entry that can be used with the `Resolver`
200235
constructor.
201236

src/__tests__/ethr-did-resolver-test.js

+45
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,51 @@ describe('ethrResolver', () => {
507507
})
508508
})
509509
})
510+
511+
describe('X25519KeyAgreementKey2019', () => {
512+
let identity1, did1
513+
514+
beforeAll(async () => {
515+
const accounts = await getAccounts()
516+
identity1 = accounts[5]
517+
did1 = `did:ethr:${identity1}`
518+
519+
await registry.setAttribute(
520+
identity1,
521+
stringToBytes32('did/pub/X25519/enc/base64'),
522+
`0x${Buffer.from('MCowBQYDK2VuAyEAEYVXd3/7B4d0NxpSsA/tdVYdz5deYcR1U+ZkphdmEFI=', 'base64').toString('hex')}`,
523+
86400,
524+
{ from: identity1 }
525+
)
526+
})
527+
528+
it('resolves document', () => {
529+
return expect(didResolver.resolve(did1)).resolves.toEqual({
530+
'@context': 'https://w3id.org/did/v1',
531+
id: did1,
532+
publicKey: [
533+
{
534+
id: `${did1}#owner`,
535+
type: 'Secp256k1VerificationKey2018',
536+
owner: did1,
537+
ethereumAddress: identity1
538+
},
539+
{
540+
id: `${did1}#delegate-1`,
541+
type: 'X25519KeyAgreementKey2019',
542+
owner: did1,
543+
publicKeyBase64: 'MCowBQYDK2VuAyEAEYVXd3/7B4d0NxpSsA/tdVYdz5deYcR1U+ZkphdmEFI='
544+
}
545+
],
546+
authentication: [
547+
{
548+
type: 'Secp256k1SignatureAuthentication2018',
549+
publicKey: `${did1}#owner`
550+
}
551+
]
552+
})
553+
})
554+
})
510555
})
511556

512557
describe('add service endpoints', () => {

src/ethr-did-resolver.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ const delegateTypes = {
2929

3030
const attrTypes = {
3131
sigAuth: 'SignatureAuthentication2018',
32-
veriKey: 'VerificationKey2018'
32+
veriKey: 'VerificationKey2018',
33+
enc: 'KeyAgreementKey2019'
3334
}
3435

3536
function wrapDidDocument (did, owner, history) {

0 commit comments

Comments
 (0)