-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: find existing connection based on invitation did (#698)
* Extract creation of did doc from services * Create peer did from service and store it as invitation did Signed-off-by: Jakub Koci <[email protected]>
- Loading branch information
Showing
12 changed files
with
284 additions
and
110 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
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
61 changes: 61 additions & 0 deletions
61
packages/core/src/modules/dids/domain/createPeerDidFromServices.ts
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,61 @@ | ||
import type { DidCommService } from '.' | ||
|
||
import { convertPublicKeyToX25519 } from '@stablelib/ed25519' | ||
|
||
import { KeyType } from '../../../crypto' | ||
import { uuid } from '../../../utils/uuid' | ||
|
||
import { DidDocumentBuilder } from './DidDocumentBuilder' | ||
import { Key } from './Key' | ||
import { getEd25519VerificationMethod } from './key-type/ed25519' | ||
import { getX25519VerificationMethod } from './key-type/x25519' | ||
|
||
export function createDidDocumentFromServices(services: DidCommService[]) { | ||
const didDocumentBuilder = new DidDocumentBuilder('') | ||
|
||
// We need to all reciepient and routing keys from all services but we don't want to duplicated items | ||
const recipientKeys = new Set(services.map((s) => s.recipientKeys).reduce((acc, curr) => acc.concat(curr), [])) | ||
const routingKeys = new Set( | ||
services | ||
.map((s) => s.routingKeys) | ||
.filter((r): r is string[] => r !== undefined) | ||
.reduce((acc, curr) => acc.concat(curr), []) | ||
) | ||
|
||
for (const recipientKey of recipientKeys) { | ||
const publicKeyBase58 = recipientKey | ||
const ed25519Key = Key.fromPublicKeyBase58(publicKeyBase58, KeyType.Ed25519) | ||
const x25519Key = Key.fromPublicKey(convertPublicKeyToX25519(ed25519Key.publicKey), KeyType.X25519) | ||
|
||
const ed25519VerificationMethod = getEd25519VerificationMethod({ | ||
id: uuid(), | ||
key: ed25519Key, | ||
controller: '#id', | ||
}) | ||
const x25519VerificationMethod = getX25519VerificationMethod({ | ||
id: uuid(), | ||
key: x25519Key, | ||
controller: '#id', | ||
}) | ||
|
||
// We should not add duplicated keys for services | ||
didDocumentBuilder.addAuthentication(ed25519VerificationMethod).addKeyAgreement(x25519VerificationMethod) | ||
} | ||
|
||
for (const routingKey of routingKeys) { | ||
const publicKeyBase58 = routingKey | ||
const ed25519Key = Key.fromPublicKeyBase58(publicKeyBase58, KeyType.Ed25519) | ||
const verificationMethod = getEd25519VerificationMethod({ | ||
id: uuid(), | ||
key: ed25519Key, | ||
controller: '#id', | ||
}) | ||
didDocumentBuilder.addVerificationMethod(verificationMethod) | ||
} | ||
|
||
services.forEach((service) => { | ||
didDocumentBuilder.addService(service) | ||
}) | ||
|
||
return didDocumentBuilder.build() | ||
} |
117 changes: 117 additions & 0 deletions
117
.../DidPeer.test.ts~9399a710 (feat: find existing connection based on invitation did (#698))
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,117 @@ | ||
import { JsonTransformer } from '../../../../../utils' | ||
import didKeyBls12381g1 from '../../../__tests__/__fixtures__/didKeyBls12381g1.json' | ||
import didKeyBls12381g1g2 from '../../../__tests__/__fixtures__/didKeyBls12381g1g2.json' | ||
import didKeyBls12381g2 from '../../../__tests__/__fixtures__/didKeyBls12381g2.json' | ||
import didKeyEd25519 from '../../../__tests__/__fixtures__/didKeyEd25519.json' | ||
import didKeyX25519 from '../../../__tests__/__fixtures__/didKeyX25519.json' | ||
import { DidDocument, Key } from '../../../domain' | ||
import { DidPeer, PeerDidNumAlgo } from '../DidPeer' | ||
import { serviceToNumAlgo2Did } from '../peerDidNumAlgo2' | ||
|
||
import didPeer1zQmRDidCommServices from './__fixtures__/didPeer1zQmR-did-comm-service.json' | ||
import didPeer1zQmR from './__fixtures__/didPeer1zQmR.json' | ||
import didPeer1zQmZ from './__fixtures__/didPeer1zQmZ.json' | ||
import didPeer2Ez6L from './__fixtures__/didPeer2Ez6L.json' | ||
|
||
describe('DidPeer', () => { | ||
test('transforms a key correctly into a peer did method 0 did document', async () => { | ||
const didDocuments = [didKeyEd25519, didKeyBls12381g1, didKeyX25519, didKeyBls12381g1g2, didKeyBls12381g2] | ||
|
||
for (const didDocument of didDocuments) { | ||
const key = Key.fromFingerprint(didDocument.id.split(':')[2]) | ||
|
||
const didPeer = DidPeer.fromKey(key) | ||
const expectedDidPeerDocument = JSON.parse( | ||
JSON.stringify(didDocument).replace(new RegExp('did:key:', 'g'), 'did:peer:0') | ||
) | ||
|
||
expect(didPeer.didDocument.toJSON()).toMatchObject(expectedDidPeerDocument) | ||
} | ||
}) | ||
|
||
test('transforms a method 2 did correctly into a did document', () => { | ||
expect(DidPeer.fromDid(didPeer2Ez6L.id).didDocument.toJSON()).toMatchObject(didPeer2Ez6L) | ||
}) | ||
|
||
test('transforms a method 0 did correctly into a did document', () => { | ||
const didDocuments = [didKeyEd25519, didKeyBls12381g1, didKeyX25519, didKeyBls12381g1g2, didKeyBls12381g2] | ||
|
||
for (const didDocument of didDocuments) { | ||
const didPeer = DidPeer.fromDid(didDocument.id.replace('did:key:', 'did:peer:0')) | ||
const expectedDidPeerDocument = JSON.parse( | ||
JSON.stringify(didDocument).replace(new RegExp('did:key:', 'g'), 'did:peer:0') | ||
) | ||
|
||
expect(didPeer.didDocument.toJSON()).toMatchObject(expectedDidPeerDocument) | ||
} | ||
}) | ||
|
||
test('transforms a did document into a valid method 2 did', () => { | ||
const didPeer2 = DidPeer.fromDidDocument( | ||
JsonTransformer.fromJSON(didPeer2Ez6L, DidDocument), | ||
PeerDidNumAlgo.MultipleInceptionKeyWithoutDoc | ||
) | ||
|
||
expect(didPeer2.did).toBe(didPeer2Ez6L.id) | ||
}) | ||
|
||
test('transforms a did comm service into a valid method 2 did', () => { | ||
const didDocument = JsonTransformer.fromJSON(didPeer1zQmRDidCommServices, DidDocument) | ||
const peerDid = serviceToNumAlgo2Did(didDocument.didCommServices[0]) | ||
const peerDidInstance = DidPeer.fromDid(peerDid) | ||
|
||
// TODO the following `console.log` statement throws an error "TypeError: Cannot read property 'toLowerCase' | ||
// of undefined" because of this: | ||
// | ||
// `service.id = `${did}#${service.type.toLowerCase()}-${serviceIndex++}`` | ||
|
||
// console.log(peerDidInstance.didDocument) | ||
|
||
expect(peerDid).toBe( | ||
'did:peer:2.Ez6MkpTHR8VNsBxYAAWHut2Geadd9jSwuBV8xRoAnwWsdvktH.SeyJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludCJ9' | ||
) | ||
expect(peerDid).toBe(peerDidInstance.did) | ||
}) | ||
|
||
test('transforms a did document into a valid method 1 did', () => { | ||
const didPeer1 = DidPeer.fromDidDocument( | ||
JsonTransformer.fromJSON(didPeer1zQmR, DidDocument), | ||
PeerDidNumAlgo.GenesisDoc | ||
) | ||
|
||
expect(didPeer1.did).toBe(didPeer1zQmR.id) | ||
}) | ||
|
||
// FIXME: we need some input data from AFGO for this test to succeed (we create a hash of the document, so any inconsistency is fatal) | ||
xtest('transforms a did document from aries-framework-go into a valid method 1 did', () => { | ||
const didPeer1 = DidPeer.fromDidDocument( | ||
JsonTransformer.fromJSON(didPeer1zQmZ, DidDocument), | ||
PeerDidNumAlgo.GenesisDoc | ||
) | ||
|
||
expect(didPeer1.did).toBe(didPeer1zQmZ.id) | ||
}) | ||
|
||
test('extracts the numAlgo from the peer did', async () => { | ||
// NumAlgo 0 | ||
const key = Key.fromFingerprint(didKeyEd25519.id.split(':')[2]) | ||
const didPeerNumAlgo0 = DidPeer.fromKey(key) | ||
|
||
expect(didPeerNumAlgo0.numAlgo).toBe(PeerDidNumAlgo.InceptionKeyWithoutDoc) | ||
expect(DidPeer.fromDid('did:peer:0z6Mkk7yqnGF3YwTrLpqrW6PGsKci7dNqh1CjnvMbzrMerSeL').numAlgo).toBe( | ||
PeerDidNumAlgo.InceptionKeyWithoutDoc | ||
) | ||
|
||
// NumAlgo 1 | ||
const peerDidNumAlgo1 = 'did:peer:1zQmZMygzYqNwU6Uhmewx5Xepf2VLp5S4HLSwwgf2aiKZuwa' | ||
expect(DidPeer.fromDid(peerDidNumAlgo1).numAlgo).toBe(PeerDidNumAlgo.GenesisDoc) | ||
|
||
// NumAlgo 2 | ||
const peerDidNumAlgo2 = | ||
'did:peer:2.Ez6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc.Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V.Vz6MkgoLTnTypo3tDRwCkZXSccTPHRLhF4ZnjhueYAFpEX6vg.SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludCIsInIiOlsiZGlkOmV4YW1wbGU6c29tZW1lZGlhdG9yI3NvbWVrZXkiXSwiYSI6WyJkaWRjb21tL3YyIiwiZGlkY29tbS9haXAyO2Vudj1yZmM1ODciXX0' | ||
expect(DidPeer.fromDid(peerDidNumAlgo2).numAlgo).toBe(PeerDidNumAlgo.MultipleInceptionKeyWithoutDoc) | ||
expect(DidPeer.fromDidDocument(JsonTransformer.fromJSON(didPeer2Ez6L, DidDocument)).numAlgo).toBe( | ||
PeerDidNumAlgo.MultipleInceptionKeyWithoutDoc | ||
) | ||
}) | ||
}) |
21 changes: 21 additions & 0 deletions
21
...e/src/modules/dids/methods/peer/__tests__/__fixtures__/didPeer1zQmR-did-comm-service.json
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,21 @@ | ||
{ | ||
"@context": ["https://w3id.org/did/v1"], | ||
"id": "did:peer:1zQmRYBx1pL86DrsxoJ2ZD3w42d7Ng92ErPgFsCSqg8Q1h4i", | ||
"keyAgreement": [ | ||
{ | ||
"id": "#6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V", | ||
"type": "Ed25519VerificationKey2018", | ||
"publicKeyBase58": "ByHnpUCFb1vAfh9CFZ8ZkmUZguURW8nSw889hy6rD8L7" | ||
} | ||
], | ||
"service": [ | ||
{ | ||
"id": "#service-0", | ||
"type": "did-communication", | ||
"serviceEndpoint": "https://example.com/endpoint", | ||
"recipientKeys": ["did:key:z6MkpTHR8VNsBxYAAWHut2Geadd9jSwuBV8xRoAnwWsdvktH"], | ||
"routingKeys": ["ByHnpUCFb1vAfh9CFZ8ZkmUZguURW8nSw889hy6rD8L7"], | ||
"accept": ["didcomm/v2", "didcomm/aip2;env=rfc587"] | ||
} | ||
] | ||
} |
Oops, something went wrong.