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

feat: test vp exchange with ew role vc #154

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions apps/vc-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@
"dependencies": {
"@energyweb/ssi-did": "0.0.1",
"@energyweb/w3c-ccg-webkms": "0.0.1",
"@ethersproject/providers": "5.7.2",
"@nestjs/common": "^9.0.0",
"@nestjs/config": "^2.0.0",
"@nestjs/core": "^9.0.0",
"@nestjs/platform-express": "^9.0.0",
"@nestjs/typeorm": "^9.0.0",
"ethers": "5.7.2",
"jose": "^4.0.0",
"reflect-metadata": "^0.1.0",
"rimraf": "^3.0.0",
Expand All @@ -50,6 +52,7 @@
"@nestjs/serve-static": "^3.0.0"
},
"devDependencies": {
"@ew-did-registry/credentials-interface": "0.8.0",
"@nestjs/cli": "^9.0.0",
"@nestjs/schematics": "^9.0.0",
"@nestjs/testing": "^9.0.0",
Expand All @@ -59,9 +62,11 @@
"@types/supertest": "^2.0.0",
"@typescript-eslint/eslint-plugin": "^5.0.0",
"@typescript-eslint/parser": "^5.0.0",
"axios": "1.2.1",
"eslint": "^8.0.0",
"eslint-config-prettier": "^8.0.0",
"eslint-plugin-prettier": "^4.0.0",
"iam-client-lib": "^7.0.0",
"jest": "^29.0.0",
"prettier": "^2.0.0",
"supertest": "^6.0.0",
Expand Down
2 changes: 2 additions & 0 deletions apps/vc-api/test/app.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { AppModule } from '../src/app.module';
import { WalletClient } from './wallet-client';
import { didSuite } from './did/did.e2e-suite';
import { residentCardExchangeSuite } from './vc-api/exchanges/resident-card/resident-card.e2e-suite';
import { energywebRoleVCExchangeSure } from './vc-api/exchanges/ew-role/ew-role-vc-exchange.e2e-suite';
import { VpRequestDto } from '../src/vc-api/exchanges/dtos/vp-request.dto';
import { rebeamExchangeSuite } from './vc-api/exchanges/rebeam/rebeam.e2e-suite';
import { vcApiSuite } from './vc-api/credentials/vc-api.e2e-suite';
Expand Down Expand Up @@ -62,6 +63,7 @@ describe('App (e2e)', () => {
describe('VC-API (e2e)', vcApiSuite);
describe('Resident Card (e2e)', residentCardExchangeSuite);
describe('Rebeam (e2e)', rebeamExchangeSuite);
describe('EW Role VC (e2e)', energywebRoleVCExchangeSure);
});

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/**
* Copyright 2021, 2022 Energy Web Foundation
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import {
VerifiableCredentialsServiceBase,
getVerifiableCredentialsService,
IssuerFields,
fromPrivateKey,
CacheClient,
RoleCredentialSubject
} from 'iam-client-lib';
import {
Credential,
CredentialStatusPurpose,
StatusListEntryType
} from '@ew-did-registry/credentials-interface';
import { Wallet, providers, utils } from 'ethers';
import axios from 'axios';
import { error } from 'console';
// import {} from '../../../../../../common/temp/node_modules/iam-client-lib/dist/src/modules/cache-client/cache-client.service'

const { id } = utils;

jest.mock('axios');
const getClaimsBySubject = jest.fn();
jest.mock('iam-client-lib/dist/src/modules/cache-client/cache-client.service', () => {
Copy link
Contributor Author

@nichonien nichonien Dec 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jrhender @JGiter @artursudnik Is there any way we can mock the CacheClient module such that we have access to intermediate results (credential argument of addStatusToCredential method) ? js.mock() only takes path of the module and iam-client-lib only has Dist build but not the src directory.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would try something like

const addStatusToCredential = jest.fn();
jest.mock('iam-client-lib', () => {
   return jest.fn().mockImplementation(() => {
    return {CacheClient: {addStatusToCredential}};
  });
})

Copy link
Contributor Author

@nichonien nichonien Dec 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried this, it breaks other functions coming from iam-client-lib . So my assumption is that it mocks whole iam-client-lib not just the function as jest.mock() is for modules. - https://jestjs.io/docs/mock-functions#mocking-modules

return {
CacheClient: jest.fn().mockImplementation(() => {
return {
getClaimsBySubject,
addStatusToCredential: (
credential: Credential<RoleCredentialSubject>
): Credential<RoleCredentialSubject> => {
return {
...credential,
credentialStatus: {
id: `https://energyweb.org/credential/${id(JSON.stringify(credential))}#list`,
type: StatusListEntryType.Entry2021,
statusPurpose: CredentialStatusPurpose.REVOCATION,
statusListIndex: '1',
statusListCredential: `https://identitycache.org/v1/status-list/${credential.id}`
}
};
}
};
})
};
});

export const energywebRoleVCExchangeSure = () => {
it('should be able to present Energy Web Role based VC', async () => {
// As an holder, obtain EW Role verifiable credential

const rpcUrl = 'https://volta-rpc.energyweb.org/';
let provider = new providers.JsonRpcProvider(rpcUrl);

const roleNamespace = 'test.ew.role.credential';

let verifiableCredentialsService: VerifiableCredentialsServiceBase;
let holderDID: string;
const holderWallet = Wallet.createRandom();
const holderAddress = holderWallet.address;

const signerService = await fromPrivateKey(holderWallet.privateKey, rpcUrl);
await signerService.publicKeyAndIdentityToken();
holderDID = signerService.didHex;

const cacheClient = new CacheClient(signerService);
verifiableCredentialsService = await getVerifiableCredentialsService(signerService, cacheClient);

const createExampleSignedCredential = async (issuerFields: IssuerFields[], expirationDate?: Date) => {
return await verifiableCredentialsService.createRoleVC({
id: holderDID,
namespace: roleNamespace,
version: '1',
issuerFields,
expirationDate
});
};
const verifiableCredential = await createExampleSignedCredential([]);
error('xxxxxxxxxxxxxx + vc creation');
const verifiablePresentation = await verifiableCredentialsService.createVerifiablePresentation([
verifiableCredential
]);
console.log(verifiablePresentation);
});
};
88 changes: 88 additions & 0 deletions apps/vc-api/test/vc-api/exchanges/ew-role/ew-role-vc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Source: https://github.com/spruceid/ssi/blob/3745b7dfdd18e1b27c6135f8036470efde596f35/did-pkh/tests/vc-eth-eip712sig.jsonld

export const validEnergyWebRoleVC = {
'@context': ['https://www.w3.org/2018/credentials/v1'],
type: ['VerifiableCredential'],
credentialSubject: {
id: 'did:example:foo'
},
issuer: 'did:pkh:eth:0x2fbf1be19d90a29aea9363f4ef0b6bf1c4ff0758',
issuanceDate: '2021-03-18T16:38:25Z',
proof: {
'@context': 'https://demo.spruceid.com/ld/eip712sig-2021/v0.1.jsonld',
type: 'EthereumEip712Signature2021',
proofPurpose: 'assertionMethod',
proofValue:
'0x9abee96d684a146aa0b30498d8799ee9a4f8f54488c73d4a4fba3a6fb94eca8764af54f15a24deba0dd9ee2f460d1f6bd174a4ca7504a72d6b1fe9b739d613fe1b',
verificationMethod: 'did:pkh:eth:0x2fbf1be19d90a29aea9363f4ef0b6bf1c4ff0758#Recovery2020',
created: '2021-06-17T17:16:39.791Z',
eip712Domain: {
domain: {
name: 'EthereumEip712Signature2021'
},
messageSchema: {
CredentialSubject: [
{
name: 'id',
type: 'string'
}
],
EIP712Domain: [
{
name: 'name',
type: 'string'
}
],
Proof: [
{
name: '@context',
type: 'string'
},
{
name: 'verificationMethod',
type: 'string'
},
{
name: 'created',
type: 'string'
},
{
name: 'proofPurpose',
type: 'string'
},
{
name: 'type',
type: 'string'
}
],
VerifiableCredential: [
{
name: '@context',
type: 'string[]'
},
{
name: 'type',
type: 'string[]'
},
{
name: 'issuer',
type: 'string'
},
{
name: 'issuanceDate',
type: 'string'
},
{
name: 'credentialSubject',
type: 'CredentialSubject'
},
{
name: 'proof',
type: 'Proof'
}
]
},
primaryType: 'VerifiableCredential'
}
}
};
Loading