Skip to content

Commit

Permalink
feat: issue on-chain role
Browse files Browse the repository at this point in the history
  • Loading branch information
JGiter committed Jun 11, 2021
1 parent 48c25ea commit db0d42a
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 16 deletions.
6 changes: 3 additions & 3 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 @@ -49,7 +49,7 @@
},
"dependencies": {
"@babel/runtime": "^7.12.5",
"@energyweb/iam-contracts": "1.11.0",
"@energyweb/iam-contracts": "1.11.2",
"@ensdomains/ens": "^0.4.5",
"@ew-did-registry/claims": "0.5.2-alpha.1045.0",
"@ew-did-registry/did": "0.5.2-alpha.1045.0",
Expand Down
87 changes: 75 additions & 12 deletions src/iam.ts
Expand Up @@ -22,7 +22,8 @@ import {
IOrganizationDefinition,
PreconditionType,
EncodedCall,
DomainReader
DomainReader,
ClaimManager__factory
} from "@energyweb/iam-contracts";
import {
Algorithms,
Expand All @@ -34,7 +35,6 @@ import {
} from "@ew-did-registry/did-resolver-interface";
import { hashes, IProofData, ISaltedFields } from "@ew-did-registry/claims";
import { ProxyOperator } from "@ew-did-registry/proxyidentity";
import { namehash } from "./utils/ENS_hash";
import { v4 as uuid } from "uuid";
import { IAMBase } from "./iam/iam-base";
import {
Expand All @@ -55,7 +55,7 @@ import {
} from "./cacheServerClient/cacheServerClient.types";
import detectEthereumProvider from "@metamask/detect-provider";
import { WalletProvider } from "./types/WalletProvider";
import { emptyAddress, NATS_EXCHANGE_TOPIC } from "./utils/constants";
import { emptyAddress, erc712_type_hash, NATS_EXCHANGE_TOPIC, proof_type_hash, typedMsgPrefix } from "./utils/constants";
import { Subscription } from "nats.ws";
import { AxiosError } from "axios";
import { DIDDocumentFull } from "@ew-did-registry/did-document";
Expand All @@ -64,7 +64,7 @@ import { addressOf } from "@ew-did-registry/did-ethr-resolver";
import { isValidDID } from "./utils/did";
import { chainConfigs } from "./iam/chainConfig";

const { id, keccak256, defaultAbiCoder, solidityKeccak256, arrayify } = utils;
const { id, keccak256, defaultAbiCoder, solidityKeccak256, arrayify, namehash } = utils;

export type InitializeData = {
did: string | undefined;
Expand All @@ -88,7 +88,8 @@ export interface IClaimRequest extends IMessage {
}

export interface IClaimIssuance extends IMessage {
issuedToken: string;
issuedToken?: string;
onChainProof?: string;
acceptedBy: string;
}

Expand Down Expand Up @@ -1395,7 +1396,6 @@ export class IAM extends IAMBase {
)
);

const typedMsgPrefix = "1901";
const messageId = Buffer.from(typedMsgPrefix, "hex");

const agreementHash = solidityKeccak256(
Expand All @@ -1415,6 +1415,45 @@ export class IAM extends IAMBase {
));
}

async createOnChainProof(role: string, version: number, expiry: number, subject: string): Promise<string> {
if (!this._did) {
throw new Error(ERROR_MESSAGES.USER_NOT_LOGGED_IN);
}
if (!this._signer) {
throw new Error(ERROR_MESSAGES.SIGNER_NOT_INITIALIZED);
}
const messageId = Buffer.from(typedMsgPrefix, "hex");

const domainSeparator = utils.keccak256(
defaultAbiCoder.encode(
["bytes32", "bytes32", "bytes32", "uint256", "address"],
[
erc712_type_hash,
utils.id("Claim Manager"),
utils.id("1.0"),
(await this._provider.getNetwork()).chainId,
this._claimManager.address
]
)
);

const proofHash = solidityKeccak256(
["bytes", "bytes32", "bytes32"],
[
messageId,
domainSeparator,
utils.keccak256(defaultAbiCoder.encode(
["bytes32", "address", "bytes32", "uint", "uint", "address"],
[proof_type_hash, subject, utils.namehash(role), version, expiry, this._address]
))
]
);

return this._signer.signMessage(arrayify(
proofHash
));
}

async createClaimRequest({
issuer,
claim,
Expand Down Expand Up @@ -1472,28 +1511,52 @@ export class IAM extends IAMBase {
requester,
token,
id,
subjectAgreement,
registrationTypes
}: {
requester: string;
token: string;
id: string;
subjectAgreement: string;
registrationTypes: RegistrationTypes[]
}) {
if (!this._did) {
throw new Error(ERROR_MESSAGES.USER_NOT_LOGGED_IN);
}
if (!this._jwt) {
throw new Error(ERROR_MESSAGES.JWT_NOT_INITIALIZED);
}
const { claimData, sub } = this._jwt.decode(token) as { claimData: { claimType: string; claimTypeVersion: string }; sub: string };
const issuedToken = await this.issuePublicClaim({
token: await this._jwt.sign({ claimData }, { subject: sub, issuer: this._did })
});
if (!this._signer) {
throw new Error(ERROR_MESSAGES.SIGNER_NOT_INITIALIZED);
}
const { claimData, sub } = this._jwt.decode(token) as
{ claimData: { claimType: string; claimTypeVersion: number, expiry: number }; sub: string };
const message: IClaimIssuance = {
id,
issuedToken,
requester: requester,
requester,
claimIssuer: [this._did],
acceptedBy: this._did
};
if (registrationTypes.includes(RegistrationTypes.OffChain)) {
message.issuedToken = await this.issuePublicClaim({
token: await this._jwt.sign({ claimData }, { subject: sub, issuer: this._did })
});
}
if (registrationTypes.includes(RegistrationTypes.OnChain)) {
const { claimType: role, claimTypeVersion: version, expiry } = claimData;
const claimManager = ClaimManager__factory.connect(this._claimManager, this._signer);
const onChainProof = await this.createOnChainProof(role, version, expiry, sub);
await claimManager.register(
addressOf(sub),
namehash(role),
version,
expiry,
addressOf(this._did),
subjectAgreement,
onChainProof
);
message.onChainProof = onChainProof;
}

if (!this._natsConnection) {
if (this._cacheClient) {
Expand Down
7 changes: 7 additions & 0 deletions src/utils/constants.ts
@@ -1,3 +1,5 @@
import { utils } from "ethers";

export const emptyAddress = "0x0000000000000000000000000000000000000000";
export const WALLET_PROVIDER = "WalletProvider";
export const PUBLIC_KEY = "PublicKey";
Expand All @@ -9,3 +11,8 @@ export enum MessagingMethod {
WebRTC = "webRTC",
SmartContractStorage = "smartContractStorage"
}

export const typedMsgPrefix = "1901";
export const erc712_type_hash = utils.id("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)");
export const agreement_type_hash = utils.id("Agreement(address subject,bytes32 role,uint256 version)");
export const proof_type_hash = utils.id("Proof(address subject,bytes32 role,uint256 version,uint256 expiry,address issuer)");

0 comments on commit db0d42a

Please sign in to comment.