Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: merging multiple PeerDID classes #130

Merged
merged 1 commit into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/domain/buildingBlocks/Pluto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 0 additions & 21 deletions src/domain/models/PeerDID.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/domain/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion src/mercury/didcomm/SecretsResolver.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand Down Expand Up @@ -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(
Expand Down
54 changes: 33 additions & 21 deletions src/peer-did/PeerDID.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
*/
Expand Down Expand Up @@ -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;
}
}
1 change: 0 additions & 1 deletion tests/castor/PeerDID.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { expect } from "chai";

import {
KeyPair,
Service,
DID,
ServiceEndpoint,
Expand Down
Loading