Skip to content

Commit

Permalink
feat: read and write of new roledefinition smart contract
Browse files Browse the repository at this point in the history
- This is done using classes from @energyweb/iam-contracts repo
- Also using definition interfaces from iam-contracts so that they can be shared with cache-server
- Removed references to @ensdomains package as this is handled by iam-contracts
  • Loading branch information
jrhender committed May 25, 2021
1 parent b0d9446 commit dc632c8
Show file tree
Hide file tree
Showing 14 changed files with 106 additions and 125 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Expand Up @@ -18,6 +18,7 @@
"rules": {
"semi": "error",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/explicit-module-boundary-types": "off"
"@typescript-eslint/explicit-module-boundary-types": "off",
"quotes": ["warn", "double"]
}
}
36 changes: 36 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -107,4 +107,4 @@
"typedoc-plugin-markdown": "~3.0.11",
"typescript": "^4.0.2"
}
}
}
43 changes: 1 addition & 42 deletions src/cacheServerClient/cacheServerClient.types.ts
@@ -1,46 +1,5 @@
import { IDIDDocument } from "@ew-did-registry/did-resolver-interface";
import { PreconditionTypes } from "../utils/constants";

export interface IRoleDefinition {
version: string;
roleType: string;
roleName: string;
fields: {
fieldType: string;
label: string;
required?: boolean;
minLength?: number;
maxLength?: number;
pattern?: string;
minValue?: number;
maxValue?: number;
minDate?: Date;
maxDate?: Date;
}[];
metadata: Record<string, unknown> | Record<string, unknown>[];
issuer: {
issuerType?: string;
did?: string[];
roleName?: string;
};
enrolmentPreconditions?: { type: PreconditionTypes; conditions: string[] }[];
}

export interface IAppDefinition {
appName: string;
logoUrl?: string;
websiteUrl?: string;
description?: string;
others?: Record<string, unknown>;
}

export interface IOrganizationDefinition {
orgName: string;
logoUrl?: string;
websiteUrl?: string;
description?: string;
others?: Record<string, unknown>;
}
import { IRoleDefinition, IAppDefinition, IOrganizationDefinition } from "@energyweb/iam-contracts";

export interface IRole {
uid: string;
Expand Down
4 changes: 1 addition & 3 deletions src/iam-client-lib.ts
Expand Up @@ -14,6 +14,7 @@
//
// @authors: Kim Honoridez

import { IRoleDefinition, IAppDefinition, IOrganizationDefinition } from "@energyweb/iam-contracts";
import { IAM, ENSNamespaceTypes } from "./iam";
import { ICacheServerClient } from "./cacheServerClient/ICacheServerClient";
import { ERROR_MESSAGES } from "./errors";
Expand All @@ -26,11 +27,8 @@ import {
} from "./utils/constants";
import {
IApp,
IAppDefinition,
IOrganization,
IOrganizationDefinition,
IRole,
IRoleDefinition,
Asset,
AssetHistory,
AssetHistoryEventType,
Expand Down
41 changes: 13 additions & 28 deletions src/iam.ts
Expand Up @@ -16,6 +16,7 @@
// @authors: Daniel Wojno

import { providers, Signer } from "ethers";
import { IRoleDefinition, IAppDefinition, IOrganizationDefinition } from "@energyweb/iam-contracts";
import {
DIDAttribute,
IDIDDocument,
Expand All @@ -39,10 +40,7 @@ import {
} from "./errors";
import {
AssetHistoryEventType, ClaimData,
IAppDefinition,
IOrganization,
IOrganizationDefinition,
IRoleDefinition,
Order
} from "./cacheServerClient/cacheServerClient.types";
import detectEthereumProvider from "@metamask/detect-provider";
Expand Down Expand Up @@ -514,7 +512,7 @@ export class IAM extends IAMBase {
data: IAppDefinition | IOrganizationDefinition | IRoleDefinition;
}) {
await this.send({
calls: [this.setDomainDefinitionTx({ domain, data })],
calls: [this._domainDefinitionTransactionFactory.editDomain({ domain, domainDefinition: data })],
from: await this.getOwner({ namespace: domain })
});
}
Expand Down Expand Up @@ -547,12 +545,8 @@ export class IAM extends IAMBase {
info: "Create organization subdomain"
},
{
tx: this.setDomainNameTx({ domain: orgDomain }),
info: "Register reverse name for organization subdomain"
},
{
tx: this.setDomainDefinitionTx({ domain: orgDomain, data }),
info: "Set definition for organization"
tx: this._domainDefinitionTransactionFactory.newDomain({ domain: orgDomain, domainDefinition: data }),
info: "Register reverse name and set definition for organization subdomain"
},
{
tx: this.createSubdomainTx({
Expand All @@ -563,7 +557,7 @@ export class IAM extends IAMBase {
info: "Create roles subdomain for organization"
},
{
tx: this.setDomainNameTx({ domain: rolesDomain }),
tx: this._domainDefinitionTransactionFactory.setDomainNameTx({ domain: rolesDomain }),
info: "Register reverse name for roles subdomain"
},
{
Expand All @@ -575,7 +569,7 @@ export class IAM extends IAMBase {
info: "Create app subdomain for organization"
},
{
tx: this.setDomainNameTx({ domain: appsDomain }),
tx: this._domainDefinitionTransactionFactory.setDomainNameTx({ domain: appsDomain }),
info: "Register reverse name for app subdomain"
}
].map(step => ({
Expand Down Expand Up @@ -617,12 +611,8 @@ export class IAM extends IAMBase {
info: "Set subdomain for application"
},
{
tx: this.setDomainNameTx({ domain: appDomain }),
info: "Set name for application"
},
{
tx: this.setDomainDefinitionTx({ data, domain: appDomain }),
info: "Set definition for application"
tx: this._domainDefinitionTransactionFactory.newDomain({ domainDefinition: data, domain: appDomain }),
info: "Set name definition for application"
},
{
tx: this.createSubdomainTx({
Expand All @@ -633,7 +623,7 @@ export class IAM extends IAMBase {
info: "Create roles subdomain for application"
},
{
tx: this.setDomainNameTx({ domain: `${ENSNamespaceTypes.Roles}.${appDomain}` }),
tx: this._domainDefinitionTransactionFactory.setDomainNameTx({ domain: `${ENSNamespaceTypes.Roles}.${appDomain}` }),
info: "Set name for roles subdomain for application"
}
].map(step => ({
Expand Down Expand Up @@ -675,12 +665,8 @@ export class IAM extends IAMBase {
info: "Create subdomain for role"
},
{
tx: this.setDomainNameTx({ domain: newDomain }),
info: "Set name for role"
},
{
tx: this.setDomainDefinitionTx({ data, domain: newDomain }),
info: "Set role definition for role"
tx: this._domainDefinitionTransactionFactory.newRole({ domain: newDomain, roleDefinition: data }),
info: "Set name and definition for role"
}
].map(step => ({
...step,
Expand Down Expand Up @@ -1012,10 +998,9 @@ export class IAM extends IAMBase {
}
throw new ENSTypeNotSupportedError();
}
if (this._ensResolver) {
if (this._domainDefinitionReader) {
const roleHash = namehash(namespace);
const metadata = await this._ensResolver.text(roleHash, "metadata");
return JSON.parse(metadata) as IRoleDefinition | IAppDefinition | IOrganizationDefinition;
return await this._domainDefinitionReader.read(roleHash) as IRoleDefinition | IAppDefinition | IOrganizationDefinition;
}
throw new ENSResolverNotInitializedError();
}
Expand Down
4 changes: 4 additions & 0 deletions src/iam/chainConfig.ts
@@ -1,4 +1,5 @@
import { VoltaAddress1056 } from "@ew-did-registry/did-ethr-resolver";
import { setRegistryAddress } from "@energyweb/iam-contracts";
import { CacheServerClientOptions } from "../cacheServerClient/cacheServerClient";
import { MessagingMethod } from "../utils/constants";

Expand Down Expand Up @@ -50,6 +51,9 @@ export const messagingOptions: Record<number, MessagingOptions> = {
*/
export const setChainConfig = function(chainId: number, config: Partial<ChainConfig>) {
chainConfigs[chainId] = { ...chainConfigs[chainId], ...config };
if (config.ensRegistryAddress) {
setRegistryAddress(chainId, config.ensRegistryAddress);
}
};

/**
Expand Down
48 changes: 12 additions & 36 deletions src/iam/iam-base.ts
@@ -1,4 +1,5 @@
import { providers, Signer, utils, errors, Wallet } from "ethers";
import { IRoleDefinition } from "@energyweb/iam-contracts";
import { ethrReg, Operator, Resolver } from "@ew-did-registry/did-ethr-resolver";
import { labelhash, namehash } from "../utils/ENS_hash";
import { IServiceEndpoint, RegistrySettings, KeyTags, IPublicKey } from "@ew-did-registry/did-resolver-interface";
Expand All @@ -7,20 +8,16 @@ import { DIDDocumentFull } from "@ew-did-registry/did-document";
import { ClaimsIssuer, ClaimsUser, ClaimsVerifier } from "@ew-did-registry/claims";
import { DidStore } from "@ew-did-registry/did-ipfs-store";
import { EnsRegistryFactory } from "../../ethers/EnsRegistryFactory";
import { PublicResolverFactory } from "../../ethers/PublicResolverFactory";
import { RoleDefinitionResolverFactory } from "../../ethers/RoleDefinitionResolverFactory";
import { EnsRegistry } from "../../ethers/EnsRegistry";
import { PublicResolver } from "../../ethers/PublicResolver";
import { RoleDefinitionResolver } from "../../ethers/RoleDefinitionResolver";
import { JWT } from "@ew-did-registry/jwt";
import { DomainReader, DomainTransactionFactory } from "@energyweb/iam-contracts";
import { ICacheServerClient } from "../cacheServerClient/ICacheServerClient";
import { isBrowser } from "../utils/isBrowser";
import { connect, NatsConnection, JSONCodec, Codec } from "nats.ws";
import { ERROR_MESSAGES } from "../errors";
import {
ClaimData,
IAppDefinition,
IOrganizationDefinition,
IRoleDefinition
} from "../cacheServerClient/cacheServerClient.types";
import { ClaimData } from "../cacheServerClient/cacheServerClient.types";
import difference from "lodash.difference";
import { TransactionOverrides } from "../../ethers";
import detectMetamask from "@metamask/detect-provider";
Expand All @@ -30,7 +27,6 @@ import { CacheServerClient } from "../cacheServerClient/cacheServerClient";
import {
emptyAddress,
MessagingMethod,
NODE_FIELDS_KEY,
PUBLIC_KEY,
WALLET_PROVIDER
} from "../utils/constants";
Expand Down Expand Up @@ -101,7 +97,8 @@ export class IAMBase {
protected _jwt: JWT | undefined;

protected _ensRegistry: EnsRegistry;
protected _ensResolver: PublicResolver;
protected _ensResolver: RoleDefinitionResolver;
protected _domainDefinitionTransactionFactory: DomainTransactionFactory
protected _ensResolverAddress: string;
protected _ensRegistryAddress: string;

Expand All @@ -115,6 +112,8 @@ export class IAMBase {

private ttl = bigNumberify(0);

protected _domainDefinitionReader: DomainReader;

/**
* IAM Constructor
*
Expand Down Expand Up @@ -488,14 +487,6 @@ export class IAMBase {
};
}

protected setDomainNameTx({ domain }: { domain: string }): EncodedCall {
const namespaceHash = namehash(domain) as string;
return {
to: this._ensResolverAddress,
data: this._ensResolver?.interface.functions.setName.encode([namespaceHash, domain])
};
}

protected changeSubdomainOwnerTx({
newOwner,
label,
Expand Down Expand Up @@ -590,23 +581,6 @@ export class IAMBase {
};
}

protected setDomainDefinitionTx({
domain,
data
}: {
domain: string;
data: IAppDefinition | IOrganizationDefinition | IRoleDefinition;
}): EncodedCall {
return {
to: this._ensResolverAddress,
data: this._ensResolver.interface.functions.setText.encode([
namehash(domain),
NODE_FIELDS_KEY,
JSON.stringify(data)
])
};
}

protected async deleteSubdomain({ namespace }: { namespace: string }) {
if (!this._signer) {
throw new Error(ERROR_MESSAGES.SIGNER_NOT_INITIALIZED);
Expand Down Expand Up @@ -655,7 +629,9 @@ export class IAMBase {
this._ensResolverAddress = ensResolverAddress;
this._assetManager = IdentityManagerFactory.connect(assetManagerAddress, this._signer);
this._ensRegistry = EnsRegistryFactory.connect(ensRegistryAddress, this._provider);
this._ensResolver = PublicResolverFactory.connect(ensResolverAddress, this._provider);
this._ensResolver = RoleDefinitionResolverFactory.connect(ensResolverAddress, this._provider);
this._domainDefinitionReader = new DomainReader(this._provider);
this._domainDefinitionTransactionFactory = new DomainTransactionFactory(this._ensResolver);

const cacheOptions = cacheServerClientOptions[chainId];

Expand Down
4 changes: 2 additions & 2 deletions src/utils/getSubDomains.ts
@@ -1,8 +1,8 @@
import { EventFilter, utils, providers } from "ethers";
import { EnsRegistry } from "../../ethers/EnsRegistry";
import { PublicResolver } from "../../ethers/PublicResolver";
import { abi as ensResolverContract } from "@ensdomains/resolver/build/contracts/PublicResolver.json";
import { abi as ensRegistryContract } from "@ensdomains/resolver/build/contracts/ENS.json";
import { abi as ensResolverContract } from "@energyweb/iam-contracts/build/contracts/PublicResolver.json";
import { abi as ensRegistryContract } from "@energyweb/iam-contracts/build/contracts/ENS.json";

import { namehash } from "./ENS_hash";
import { emptyAddress } from "./constants";
Expand Down

0 comments on commit dc632c8

Please sign in to comment.