Skip to content

Commit

Permalink
fix: adds method to parse did
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmedolaibrahim committed Jun 22, 2021
1 parent 1831949 commit 8107ce8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
12 changes: 5 additions & 7 deletions src/iam.ts
Expand Up @@ -60,7 +60,7 @@ import { AxiosError } from "axios";
import { DIDDocumentFull } from "@ew-did-registry/did-document";
import { Methods } from "@ew-did-registry/did";
import { addressOf } from "@ew-did-registry/did-ethr-resolver";
import { isValidDID } from "./utils/did";
import { isValidDID, parseDID } from "./utils/did";
import { chainConfigs } from "./iam/chainConfig";
import { canonizeSig } from "./utils/enrollment";

Expand Down Expand Up @@ -138,10 +138,6 @@ export class IAM extends IAMBase {
*/

getDid(): string | undefined {
const didRegex = new RegExp(`^did:${Methods.Erc1056}:`);
if (this._did && didRegex.test(this._did) === true) {
this._did = this._did.split(":")[2];
}
return this._did;
}

Expand Down Expand Up @@ -775,13 +771,14 @@ export class IAM extends IAMBase {
*/
async changeOrgOwnership({
namespace,
newOwner,
newOrgOwner,
returnSteps = false
}: {
namespace: string;
newOwner: string;
newOrgOwner: string;
returnSteps?: boolean;
}) {
const newOwner = parseDID(newOrgOwner);
const orgNamespaces = [
`${ENSNamespaceTypes.Roles}.${namespace}`,
`${ENSNamespaceTypes.Application}.${namespace}`,
Expand Down Expand Up @@ -1481,6 +1478,7 @@ export class IAM extends IAMBase {
if (!subject) {
subject = this._did;
}
this._did = parseDID(this._did);
const { claimType: role, claimTypeVersion: version } = claim;
const token = await this.createPublicClaim({ data: claim, subject });

Expand Down
8 changes: 8 additions & 0 deletions src/utils/did.ts
Expand Up @@ -17,4 +17,12 @@ export function addSupportedDID(
validators.set(method, validator);
}

export function parseDID(did: string) {
const didRegex = new RegExp(`^did:${Methods.Erc1056}:`);
if (did && didRegex.test(did) === true) {
return did.split(":")[2];
}
return did;
}

addSupportedDID(Methods.Erc1056, isValidErc1056);
17 changes: 17 additions & 0 deletions test/utils/did.test.ts
@@ -0,0 +1,17 @@

import { parseDID } from "../../src/utils/did";


describe("removeDIDFromAddress", () => {
it("should remove DID from address if present", async () => {
const didAddress = 'did:ethr:0x395569900f9e60819fd5521a9aC044a1B2a849DC8'
const address = parseDID(didAddress);
expect(address).toStrictEqual(didAddress.split(':')[2]);
});

it("should not attempt to remove did from address if it is not present", async () => {
const didAddress = '0x395569900f9e60819fd5521a9aC044a1B2a849DC8'
const address = parseDID(didAddress);
expect(address).toStrictEqual(didAddress);
});
})

0 comments on commit 8107ce8

Please sign in to comment.