Skip to content

Commit

Permalink
Merge pull request #291 from energywebfoundation/bug/IS-16_approved_c…
Browse files Browse the repository at this point in the history
…laim_not_verified

bug/IS-16 approved claim not verified
  • Loading branch information
JGiter authored Mar 15, 2022
2 parents 4f66118 + cf1173b commit d6fb85f
Show file tree
Hide file tree
Showing 10 changed files with 956 additions and 1,626 deletions.
9 changes: 3 additions & 6 deletions lib/ClaimVerifier.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import {
OffchainClaim,
DecodedToken,
IRoleDefinition,
} from './LoginStrategy.types';
import { OffchainClaim, DecodedToken } from './LoginStrategy.types';
import * as jwt from 'jsonwebtoken';
import { IRoleDefinition } from '@energyweb/credential-governance';
import { IDIDDocument } from '@ew-did-registry/did-resolver-interface';
import { ProofVerifier } from '@ew-did-registry/claims';

Expand All @@ -12,7 +9,7 @@ export class ClaimVerifier {
private readonly claims: OffchainClaim[],
private readonly getRoleDefinition: (
namespace: string
) => Promise<IRoleDefinition>,
) => Promise<IRoleDefinition | null>,
private readonly getOffchainClaims: (
did: string
) => Promise<OffchainClaim[]>,
Expand Down
62 changes: 39 additions & 23 deletions lib/LoginStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,26 @@ import { providers } from 'ethers';
import {
ethrReg,
Resolver,
VoltaAddress1056,
addressOf,
} from '@ew-did-registry/did-ethr-resolver';

import { isOffchainClaim, lookup, namehash } from './utils';
import {
OffchainClaim,
IRoleDefinition,
ITokenPayload,
} from './LoginStrategy.types';
import { PublicResolver__factory } from '../ethers/factories/PublicResolver__factory';
import { PublicResolver } from '../ethers/PublicResolver';
import { OffchainClaim, ITokenPayload } from './LoginStrategy.types';
import { Methods, getDIDChain, isValidErc1056 } from '@ew-did-registry/did';
import { DidStore } from '@ew-did-registry/did-ipfs-store';
import { CacheServerClient } from './cacheServerClient';
import { ClaimVerifier } from './ClaimVerifier';
import { IDIDDocument } from '@ew-did-registry/did-resolver-interface';
import { ProofVerifier } from '@ew-did-registry/claims';
import { Logger } from './Logger';
//import { Logger } from "@ethersproject/logger";
import {
DomainReader,
ResolverContractType,
IRoleDefinition,
} from '@energyweb/credential-governance';
import { knownResolvers } from './defaultConfig';

const { JsonRpcProvider } = providers;
const { abi: abi1056 } = ethrReg;

export interface LoginStrategyOptions extends StrategyOptions {
Expand All @@ -34,8 +33,13 @@ export interface LoginStrategyOptions extends StrategyOptions {
cacheServerUrl?: string;
privateKey?: string;
numberOfBlocksBack?: number;
ensResolverAddress?: string;
didContractAddress?: string;
ensResolvers?: {
chainId: number;
resolverType: ResolverContractType;
address: string;
}[];
didContractAddress: string;
ensRegistryAddress: string;
ipfsUrl?: string;
acceptedRoles?: string[];
jwtSecret: string | Buffer;
Expand All @@ -48,7 +52,7 @@ export class LoginStrategy extends BaseStrategy {
private readonly jwtSignOptions?: jwt.SignOptions;
private readonly provider: providers.JsonRpcProvider;
private readonly numberOfBlocksBack: number;
private readonly ensResolver: PublicResolver;
private readonly domainReader: DomainReader;
private readonly didResolver: Resolver;
private readonly ipfsStore: DidStore;
private readonly acceptedRoles: Set<string>;
Expand All @@ -63,8 +67,9 @@ export class LoginStrategy extends BaseStrategy {
numberOfBlocksBack = 4,
jwtSecret,
jwtSignOptions,
ensResolverAddress = '0x0a97e07c4Df22e2e31872F20C5BE191D5EFc4680',
didContractAddress = VoltaAddress1056,
ensResolvers = [],
didContractAddress,
ensRegistryAddress,
ipfsUrl = 'https://ipfs.infura.io:5001/api/v0/',
acceptedRoles,
...options
Expand All @@ -75,11 +80,21 @@ export class LoginStrategy extends BaseStrategy {
) {
super(options);
this.claimField = claimField;
this.provider = new providers.JsonRpcProvider(rpcUrl);
this.ensResolver = PublicResolver__factory.connect(
ensResolverAddress,
this.provider
);
this.provider = new JsonRpcProvider(rpcUrl);
this.domainReader = new DomainReader({
ensRegistryAddress,
provider: this.provider,
});

knownResolvers
.concat(ensResolvers)
.forEach(({ chainId, resolverType, address }) =>
this.domainReader.addKnownResolver({
chainId,
address,
type: resolverType,
})
);
if (cacheServerUrl && !privateKey) {
throw new Error(
'You need to provide privateKey of an accepted account to login to cache server'
Expand Down Expand Up @@ -232,14 +247,15 @@ export class LoginStrategy extends BaseStrategy {
);
}

async getRoleDefinition(namespace: string): Promise<IRoleDefinition> {
async getRoleDefinition(namespace: string): Promise<IRoleDefinition | null> {
if (this.cacheServerClient?.isAvailable) {
return this.cacheServerClient.getRoleDefinition({ namespace });
}
const namespaceHash = namehash(namespace);
const definition = await this.ensResolver.text(namespaceHash, 'metadata');

return JSON.parse(definition) as IRoleDefinition;
const definition = await this.domainReader.read({
node: namehash(namespace),
});
return DomainReader.isRoleDefinition(definition) ? definition : null;
}

/**
Expand Down
19 changes: 2 additions & 17 deletions lib/LoginStrategy.types.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,4 @@
export interface IRoleDefinition {
version: string;
roleType: string;
roleName: string;
fields: {
fieldType: string;
label: string;
validation: string;
}[];
metadata: Record<string, unknown> | Record<string, unknown>[];
issuer: {
issuerType?: string;
did?: string[];
roleName?: string;
};
}
import { IRoleDefinition } from '@energyweb/credential-governance';

export interface IRole {
uid: string;
Expand All @@ -25,7 +10,7 @@ export interface IRole {

export interface OffchainClaim {
claimType: string;
claimTypeVersion: string;
claimTypeVersion: number;
issuedToken: string;
iss?: string;
}
Expand Down
3 changes: 2 additions & 1 deletion lib/cacheServerClient.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import axios, { AxiosError, AxiosInstance, AxiosResponse } from 'axios';
import base64url from 'base64url';
import { Signer, Wallet, utils, providers } from 'ethers';
import { OffchainClaim, IRole, IRoleDefinition } from './LoginStrategy.types';
import { OffchainClaim, IRole } from './LoginStrategy.types';
import { Policy } from 'cockatiel';
import { IDIDDocument } from '@ew-did-registry/did-resolver-interface';
import { IRoleDefinition } from '@energyweb/credential-governance';
import { knownChains } from './utils';
import { Logger } from './Logger';

Expand Down
46 changes: 46 additions & 0 deletions lib/defaultConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import {
EWC_CHAIN_ID,
EWC_PUBLIC_RESOLVER_ADDRESS,
EWC_RESOLVER_V2_ADDRESS,
ResolverContractType,
VOLTA_CHAIN_ID,
VOLTA_PUBLIC_RESOLVER_ADDRESS,
VOLTA_RESOLVER_V1_ADDRESS,
VOLTA_RESOLVER_V2_ADDRESS,
} from '@energyweb/credential-governance';

export interface ResolverInfo {
chainId: number;
resolverType: ResolverContractType;
address: string;
}

export const knownResolvers = [
{
chainId: VOLTA_CHAIN_ID,
resolverType: ResolverContractType.PublicResolver,
address: VOLTA_PUBLIC_RESOLVER_ADDRESS,
},

{
chainId: VOLTA_CHAIN_ID,
resolverType: ResolverContractType.RoleDefinitionResolver_v1,
address: VOLTA_RESOLVER_V1_ADDRESS,
},
{
chainId: VOLTA_CHAIN_ID,
resolverType: ResolverContractType.RoleDefinitionResolver_v1,
address: VOLTA_RESOLVER_V2_ADDRESS,
},
{
chainId: EWC_CHAIN_ID,
resolverType: ResolverContractType.PublicResolver,
address: EWC_PUBLIC_RESOLVER_ADDRESS,
},

{
chainId: EWC_CHAIN_ID,
resolverType: ResolverContractType.RoleDefinitionResolver_v2,
address: EWC_RESOLVER_V2_ADDRESS,
},
];
Loading

0 comments on commit d6fb85f

Please sign in to comment.