Skip to content

Commit

Permalink
fix: optional fields in did document (#726)
Browse files Browse the repository at this point in the history
Signed-off-by: Timo Glastra <timo@animo.id>
  • Loading branch information
TimoGlastra committed May 4, 2022
1 parent b281673 commit 2da845d
Show file tree
Hide file tree
Showing 29 changed files with 380 additions and 397 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{
"@context": ["https://w3id.org/did/v1"],
"controller": [],
"alsoKnownAs": [],
"id": "did:key:z3tEFALUKUzzCAvytMHX8X4SnsNsq6T5tC5Zb18oQEt1FqNcJXqJ3AA9umgzA9yoqPBeWA",
"verificationMethod": [
{
Expand All @@ -22,7 +20,5 @@
],
"capabilityInvocation": [
"did:key:z3tEFALUKUzzCAvytMHX8X4SnsNsq6T5tC5Zb18oQEt1FqNcJXqJ3AA9umgzA9yoqPBeWA#z3tEFALUKUzzCAvytMHX8X4SnsNsq6T5tC5Zb18oQEt1FqNcJXqJ3AA9umgzA9yoqPBeWA"
],
"keyAgreement": [],
"service": []
]
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{
"@context": ["https://w3id.org/did/v1"],
"controller": [],
"alsoKnownAs": [],
"id": "did:key:z5TcESXuYUE9aZWYwSdrUEGK1HNQFHyTt4aVpaCTVZcDXQmUheFwfNZmRksaAbBneNm5KyE52SdJeRCN1g6PJmF31GsHWwFiqUDujvasK3wTiDr3vvkYwEJHt7H5RGEKYEp1ErtQtcEBgsgY2DA9JZkHj1J9HZ8MRDTguAhoFtR4aTBQhgnkP4SwVbxDYMEZoF2TMYn3s",
"verificationMethod": [
{
Expand Down Expand Up @@ -32,7 +30,5 @@
"capabilityInvocation": [
"did:key:z5TcESXuYUE9aZWYwSdrUEGK1HNQFHyTt4aVpaCTVZcDXQmUheFwfNZmRksaAbBneNm5KyE52SdJeRCN1g6PJmF31GsHWwFiqUDujvasK3wTiDr3vvkYwEJHt7H5RGEKYEp1ErtQtcEBgsgY2DA9JZkHj1J9HZ8MRDTguAhoFtR4aTBQhgnkP4SwVbxDYMEZoF2TMYn3s#z3tEG5qmJZX29jJSX5kyhDR5YJNnefJFdwTxRqk6zbEPv4Pf2xF12BpmXv9NExxSRFGfxd",
"did:key:z5TcESXuYUE9aZWYwSdrUEGK1HNQFHyTt4aVpaCTVZcDXQmUheFwfNZmRksaAbBneNm5KyE52SdJeRCN1g6PJmF31GsHWwFiqUDujvasK3wTiDr3vvkYwEJHt7H5RGEKYEp1ErtQtcEBgsgY2DA9JZkHj1J9HZ8MRDTguAhoFtR4aTBQhgnkP4SwVbxDYMEZoF2TMYn3s#zUC7LTa4hWtaE9YKyDsMVGiRNqPMN3s4rjBdB3MFi6PcVWReNfR72y3oGW2NhNcaKNVhMobh7aHp8oZB3qdJCs7RebM2xsodrSm8MmePbN25NTGcpjkJMwKbcWfYDX7eHCJjPGM"
],
"keyAgreement": [],
"service": []
]
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{
"@context": ["https://w3id.org/did/v1"],
"controller": [],
"alsoKnownAs": [],
"id": "did:key:zUC71nmwvy83x1UzNKbZbS7N9QZx8rqpQx3Ee3jGfKiEkZngTKzsRoqobX6wZdZF5F93pSGYYco3gpK9tc53ruWUo2tkBB9bxPCFBUjq2th8FbtT4xih6y6Q1K9EL4Th86NiCGT",
"verificationMethod": [
{
Expand All @@ -22,7 +20,5 @@
],
"capabilityInvocation": [
"did:key:zUC71nmwvy83x1UzNKbZbS7N9QZx8rqpQx3Ee3jGfKiEkZngTKzsRoqobX6wZdZF5F93pSGYYco3gpK9tc53ruWUo2tkBB9bxPCFBUjq2th8FbtT4xih6y6Q1K9EL4Th86NiCGT#zUC71nmwvy83x1UzNKbZbS7N9QZx8rqpQx3Ee3jGfKiEkZngTKzsRoqobX6wZdZF5F93pSGYYco3gpK9tc53ruWUo2tkBB9bxPCFBUjq2th8FbtT4xih6y6Q1K9EL4Th86NiCGT"
],
"keyAgreement": [],
"service": []
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
"https://w3id.org/security/suites/ed25519-2018/v1",
"https://w3id.org/security/suites/x25519-2019/v1"
],
"controller": [],
"alsoKnownAs": [],
"id": "did:key:z6MkmjY8GnV5i9YTDtPETC2uUAW6ejw3nk5mXF5yci5ab7th",
"verificationMethod": [
{
Expand Down Expand Up @@ -34,6 +32,5 @@
"controller": "did:key:z6MkmjY8GnV5i9YTDtPETC2uUAW6ejw3nk5mXF5yci5ab7th",
"publicKeyBase58": "79CXkde3j8TNuMXxPdV7nLUrT2g7JAEjH5TreyVY7GEZ"
}
],
"service": []
]
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
{
"@context": ["https://w3id.org/did/v1"],
"controller": [],
"alsoKnownAs": [],
"id": "did:key:z6LShLeXRTzevtwcfehaGEzCMyL3bNsAeKCwcqwJxyCo63yE",
"verificationMethod": [],
"authentication": [],
"assertionMethod": [],
"capabilityDelegation": [],
"capabilityInvocation": [],
"keyAgreement": [
{
"id": "did:key:z6LShLeXRTzevtwcfehaGEzCMyL3bNsAeKCwcqwJxyCo63yE#z6LShLeXRTzevtwcfehaGEzCMyL3bNsAeKCwcqwJxyCo63yE",
"type": "X25519KeyAgreementKey2019",
"controller": "did:key:z6LShLeXRTzevtwcfehaGEzCMyL3bNsAeKCwcqwJxyCo63yE",
"publicKeyBase58": "6fUMuABnqSDsaGKojbUF3P7ZkEL3wi2njsDdUWZGNgCU"
}
],
"service": []
]
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
{
"@context": ["https://w3id.org/did/v1"],
"id": "did:peer:1zQmYtsAsQhwEjjFkcJ2zpbHuE1ESuDkTEwm6KQd65HRNtAq",
"alsoKnownAs": [],
"controller": [],
"verificationMethod": [],
"id": "did:peer:1zQmchWGXSsHohSMrgts5oxG76zAfG49RkMZbhrYqPJeVXc1",
"service": [
{
"id": "#service-0",
Expand All @@ -25,15 +22,12 @@
"publicKeyBase58": "CQZzRfoJMRzoESU2VtWrgx3rTsk9yjrjqXL2UdxWjX2q"
}
],
"assertionMethod": [],
"keyAgreement": [
{
"id": "#08673492-3c44-47fe-baa4-a1780c585d75",
"type": "X25519KeyAgreementKey2019",
"controller": "#id",
"publicKeyBase58": "7SbWSgJgjSvSTc7ZAKHJiaZbTBwNM9TdFUAU1UyZfJn8"
}
],
"capabilityInvocation": [],
"capabilityDelegation": []
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
"https://w3id.org/security/suites/x25519-2019/v1"
],
"id": "did:sov:R1xKJw17sUoXhejEpugMYJ",
"alsoKnownAs": [],
"controller": [],
"verificationMethod": [
{
"type": "Ed25519VerificationKey2018",
Expand All @@ -24,8 +22,6 @@
"authentication": ["did:sov:R1xKJw17sUoXhejEpugMYJ#key-1"],
"assertionMethod": ["did:sov:R1xKJw17sUoXhejEpugMYJ#key-1"],
"keyAgreement": ["did:sov:R1xKJw17sUoXhejEpugMYJ#key-agreement-1"],
"capabilityDelegation": [],
"capabilityInvocation": [],
"service": [
{
"id": "did:sov:R1xKJw17sUoXhejEpugMYJ#endpoint",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
"https://w3id.org/security/suites/x25519-2019/v1",
"https://didcomm.org/messaging/contexts/v2"
],
"alsoKnownAs": [],
"controller": [],
"id": "did:sov:WJz9mHyW9BZksioQnRsrAo",
"verificationMethod": [
{
Expand All @@ -22,8 +20,6 @@
"publicKeyBase58": "S3AQEEKkGYrrszT9D55ozVVX2XixYp8uynqVm4okbud"
}
],
"capabilityDelegation": [],
"capabilityInvocation": [],
"authentication": ["did:sov:WJz9mHyW9BZksioQnRsrAo#key-1"],
"assertionMethod": ["did:sov:WJz9mHyW9BZksioQnRsrAo#key-1"],
"keyAgreement": ["did:sov:WJz9mHyW9BZksioQnRsrAo#key-agreement-1"],
Expand Down
36 changes: 21 additions & 15 deletions packages/core/src/modules/dids/__tests__/peer-did.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import { DidDocumentRole } from '../domain/DidDocumentRole'
import { convertPublicKeyToX25519, getEd25519VerificationMethod } from '../domain/key-type/ed25519'
import { getX25519VerificationMethod } from '../domain/key-type/x25519'
import { DidKey } from '../methods/key'
import { DidPeer, PeerDidNumAlgo } from '../methods/peer/DidPeer'
import { getNumAlgoFromPeerDid, PeerDidNumAlgo } from '../methods/peer/didPeer'
import { didDocumentJsonToNumAlgo1Did } from '../methods/peer/peerDidNumAlgo1'
import { DidRecord, DidRepository } from '../repository'
import { DidResolverService } from '../services'

Expand Down Expand Up @@ -96,22 +97,27 @@ describe('peer dids', () => {
.addService(service)
.build()

const peerDid = DidPeer.fromDidDocument(didDocument, PeerDidNumAlgo.GenesisDoc)
const didDocumentJson = didDocument.toJSON()
const did = didDocumentJsonToNumAlgo1Did(didDocumentJson)

expect(peerDid.did).toBe(didPeer1zQmY.id)
expect(peerDid.didDocument).toMatchObject(didPeer1zQmY)
expect(did).toBe(didPeer1zQmY.id)

// Set did after generating it
didDocument.id = did

expect(didDocument.toJSON()).toMatchObject(didPeer1zQmY)

// Save the record to storage
const didDocumentRecord = new DidRecord({
id: didPeer1zQmY.id,
role: DidDocumentRole.Created,
// It is important to take the did document from the PeerDid class
// as it will have the id property
didDocument: peerDid.didDocument,
didDocument: didDocument,
tags: {
// We need to save the recipientKeys, so we can find the associated did
// of a key when we receive a message from another connection.
recipientKeys: peerDid.didDocument.recipientKeys,
recipientKeys: didDocument.recipientKeys,
},
})

Expand All @@ -122,33 +128,33 @@ describe('peer dids', () => {
// This flow assumes peer dids. When implementing for did exchange other did methods could be used

// We receive the did and did document from the did exchange message (request or response)
// It is important to not parse the did document to a DidDocument class yet as we need the raw json
// to consistently verify the hash of the did document
const did = didPeer1zQmY.id
const numAlgo = getNumAlgoFromPeerDid(did)

// Note that the did document could be undefined (if inlined did:peer or public did)
const didDocument = JsonTransformer.fromJSON(didPeer1zQmY, DidDocument)

// Create a did peer instance from the did document document, or only the did if no did document provided
const didPeer = didDocument ? DidPeer.fromDidDocument(didDocument) : DidPeer.fromDid(did)

// make sure the dids are valid by matching them against our encoded variants
expect(didPeer.did).toBe(did)
expect(didDocumentJsonToNumAlgo1Did(didPeer1zQmY)).toBe(did)

// If a did document was provided, we match it against the did document of the peer did
// This validates whether we get the same did document
if (didDocument) {
expect(didPeer.didDocument.toJSON()).toMatchObject(didPeer1zQmY)
expect(didDocument.toJSON()).toMatchObject(didPeer1zQmY)
}

const didDocumentRecord = new DidRecord({
id: didPeer.did,
id: did,
role: DidDocumentRole.Received,
// If the method is a genesis doc (did:peer:1) we should store the document
// Otherwise we only need to store the did itself (as the did can be generated)
didDocument: didPeer.numAlgo === PeerDidNumAlgo.GenesisDoc ? didPeer.didDocument : undefined,
didDocument: numAlgo === PeerDidNumAlgo.GenesisDoc ? didDocument : undefined,
tags: {
// We need to save the recipientKeys, so we can find the associated did
// of a key when we receive a message from another connection.
recipientKeys: didPeer.didDocument.recipientKeys,
recipientKeys: didDocument.recipientKeys,
},
})

Expand All @@ -158,7 +164,7 @@ describe('peer dids', () => {
// connectionRecord.theirDid = didPeer.did

// Then when we want to send a message we can resolve the did document
const { didDocument: resolvedDidDocument } = await didResolverService.resolve(didPeer.did)
const { didDocument: resolvedDidDocument } = await didResolverService.resolve(did)
expect(resolvedDidDocument).toBeInstanceOf(DidDocument)
expect(resolvedDidDocument?.toJSON()).toMatchObject(didPeer1zQmY)
})
Expand Down
Loading

0 comments on commit 2da845d

Please sign in to comment.