From 521ad4c3661d4e6c2e62a8f3be08bb4d7768e483 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francisco=20Javier=20Ribo=CC=81=20Labrador?= Date: Tue, 2 Jan 2024 14:35:53 +0100 Subject: [PATCH] feat!: Move PeerDID outside of Domain. BREAKING CHANGE: If you have imported Domain.PeerDID in your typescript application, you will need to re-import from PeerDID directly --- src/domain/buildingBlocks/Pluto.ts | 2 +- src/domain/models/PeerDID.ts | 21 ---------- src/domain/models/index.ts | 1 - src/index.ts | 2 +- src/mercury/didcomm/SecretsResolver.ts | 3 +- src/peer-did/PeerDID.ts | 54 ++++++++++++++++---------- tests/castor/PeerDID.test.ts | 1 - 7 files changed, 37 insertions(+), 47 deletions(-) delete mode 100644 src/domain/models/PeerDID.ts diff --git a/src/domain/buildingBlocks/Pluto.ts b/src/domain/buildingBlocks/Pluto.ts index 1c0b33c29..91d848cc8 100644 --- a/src/domain/buildingBlocks/Pluto.ts +++ b/src/domain/buildingBlocks/Pluto.ts @@ -3,10 +3,10 @@ import { DIDPair } from "../models/DIDPair"; import { PrivateKey } from "../models"; import { Mediator } from "../models/Mediator"; import { Message } from "../models/Message"; -import { PeerDID } from "../models/PeerDID"; import { PrismDIDInfo } from "../models/PrismDIDInfo"; import { Credential } from "../models/Credential"; import { Anoncreds } from "../models/Anoncreds"; +import { PeerDID } from "../../peer-did/PeerDID"; /** * Pluto is a storage interface describing storage requirements of the edge agents diff --git a/src/domain/models/PeerDID.ts b/src/domain/models/PeerDID.ts deleted file mode 100644 index 2e0ff4cdc..000000000 --- a/src/domain/models/PeerDID.ts +++ /dev/null @@ -1,21 +0,0 @@ -import type { DID, KeyCurve } from "."; - -export class PeerDID { - constructor( - public readonly did: DID, - public readonly privateKeys: Array<{ - /** - * Instance of a KeyCurve - * - * @type {KeyCurve} - */ - keyCurve: KeyCurve; - /** - * Value as Uint8Array, buffer like - * - * @type {Uint8Array} - */ - value: Uint8Array; - }> - ) {} -} diff --git a/src/domain/models/index.ts b/src/domain/models/index.ts index c08ba5005..accf342b4 100644 --- a/src/domain/models/index.ts +++ b/src/domain/models/index.ts @@ -9,7 +9,6 @@ export * from "./KeyPair"; export * from "./Mediator"; export * from "./Message"; export * from "./MessageAttachment"; -export * from "./PeerDID"; export * from "./PrismDIDInfo"; export * from "./PrismDIDMethodId"; export * from "./keyManagement"; diff --git a/src/index.ts b/src/index.ts index 107b31c95..edcfca25c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -16,7 +16,7 @@ export * from "./prism-agent/mediator/PlutoMediatorStore"; export * from "./mercury/didcomm/Wrapper"; export * from "./prism-agent/helpers/ApiImpl"; export { ListenerKey } from "./prism-agent/types"; - +export * from './peer-did/PeerDID'; export type { MediatorHandler, ConnectionsManager as ConnectionsManagerInterface, diff --git a/src/mercury/didcomm/SecretsResolver.ts b/src/mercury/didcomm/SecretsResolver.ts index 04dc2085c..84b87b3f7 100644 --- a/src/mercury/didcomm/SecretsResolver.ts +++ b/src/mercury/didcomm/SecretsResolver.ts @@ -1,6 +1,7 @@ import type { Secret, SecretsResolver } from "didcomm-node"; import * as Domain from "../../domain"; import * as DIDURLParser from "../../castor/parser/DIDUrlParser"; +import { PeerDID } from "../../peer-did/PeerDID"; export class DIDCommSecretsResolver implements SecretsResolver { constructor( @@ -51,7 +52,7 @@ export class DIDCommSecretsResolver implements SecretsResolver { } private mapToSecret( - peerDid: Domain.PeerDID, + peerDid: PeerDID, publicKeyJWK: Domain.PublicKeyJWK ): Secret { const privateKeyBuffer = peerDid.privateKeys.find( diff --git a/src/peer-did/PeerDID.ts b/src/peer-did/PeerDID.ts index 2e0675384..f82bd45e7 100644 --- a/src/peer-did/PeerDID.ts +++ b/src/peer-did/PeerDID.ts @@ -1,5 +1,22 @@ -import { DID } from "../domain/models/DID"; -import { CastorError } from "../domain/models/Errors"; +import { CastorError, type DID, type KeyCurve } from "../domain"; + +export namespace PeerDID { + // Q: why is this a custom shape instead of a Domain.PrivateKey? + export interface PrivateKey { + /** + * Instance of a KeyCurve + * + * @type {KeyCurve} + */ + keyCurve: KeyCurve; + /** + * Value as Uint8Array, buffer like + * + * @type {Uint8Array} + */ + value: Uint8Array; + } +} export interface PeerDIDEncoded { t: string; @@ -8,6 +25,20 @@ export interface PeerDIDEncoded { a: string[]; } +export class PeerDID { + constructor( + public readonly did: DID, + public readonly privateKeys: PeerDID.PrivateKey[] = [] + ) { + const regex = /(([01](z)([1-9a-km-zA-HJ-NP-Z]{46,47}))|(2((\.[AEVID](z)([1-9a-km-zA-HJ-NP-Z]{46,47}))+(\.(S)[0-9a-zA-Z=]*)?)))$/; + const isValid = did.schema === "did" && did.method === "peer" && regex.test(did.methodId); + + if (isValid === false) { + throw new CastorError.InvalidPeerDIDError(); + } + } +} + /** * Provides functionality to transfrom peerDIDServices from our interfaces into DIDComm module ones */ @@ -62,22 +93,3 @@ export class PeerDIDService { ); } } - -export class PeerDID { - readonly did: DID; - - constructor(did: DID) { - const regex = - /(([01](z)([1-9a-km-zA-HJ-NP-Z]{46,47}))|(2((\.[AEVID](z)([1-9a-km-zA-HJ-NP-Z]{46,47}))+(\.(S)[0-9a-zA-Z=]*)?)))$/; - if ( - !( - did.schema === "did" && - did.method === "peer" && - regex.test(did.methodId) - ) - ) { - throw new CastorError.InvalidPeerDIDError(); - } - this.did = did; - } -} diff --git a/tests/castor/PeerDID.test.ts b/tests/castor/PeerDID.test.ts index 6b77e18a7..94a02a163 100644 --- a/tests/castor/PeerDID.test.ts +++ b/tests/castor/PeerDID.test.ts @@ -1,7 +1,6 @@ import { expect } from "chai"; import { - KeyPair, Service, DID, ServiceEndpoint,