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 all 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
7 changes: 7 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,10 +62,14 @@
"@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",
"jwk-to-pem": "2.0.5",
"openssl-nodejs": "1.0.5",
"prettier": "^2.0.0",
"supertest": "^6.0.0",
"ts-jest": "^29.0.0-next.0",
Expand Down
10 changes: 6 additions & 4 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 { energywebRoleVCExchangeSuite } 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 @@ -57,11 +58,12 @@ describe('App (e2e)', () => {
await exchangeRepository.clear();
});

describe('DID (e2e)', didSuite);
describe('Key (e2e)', keySuite);
describe('VC-API (e2e)', vcApiSuite);
// describe('DID (e2e)', didSuite);
// describe('Key (e2e)', keySuite);
// describe('VC-API (e2e)', vcApiSuite);
describe('Resident Card (e2e)', residentCardExchangeSuite);
describe('Rebeam (e2e)', rebeamExchangeSuite);
// describe('Rebeam (e2e)', rebeamExchangeSuite);
describe('EW Role VC (e2e)', energywebRoleVCExchangeSuite);
});

/**
Expand Down
158 changes: 158 additions & 0 deletions apps/vc-api/test/vc-api/exchanges/ew-role/ew-role-issuance-exchange.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/**
* 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 { plainToClass } from 'class-transformer';
import { WalletClient } from '../../../wallet-client';
import { VerifiablePresentationDto } from '../../../../src/vc-api/credentials/dtos/verifiable-presentation.dto';
import { CredentialDto } from '../../../../src/vc-api/credentials/dtos/credential.dto';
import { Presentation } from '../../../../src/vc-api/exchanges/types/presentation';
import { ExchangeDefinitionDto } from '../../../../src/vc-api/exchanges/dtos/exchange-definition.dto';
import { VpRequestInteractServiceType } from '../../../../src/vc-api/exchanges/types/vp-request-interact-service-type';
import { VpRequestQueryType } from '../../../../src/vc-api/exchanges/types/vp-request-query-type';
import { ProvePresentationOptionsDto } from '../../../../src/vc-api/credentials/dtos/prove-presentation-options.dto';
import { completeIssueCredential, prepareIssueCredential } from '@spruceid/didkit-wasm-node';
import {
CredentialStatusPurpose,
CredentialType,
StatusListEntryType,
VerifiableCredential
} from '@ew-did-registry/credentials-interface';
import { proofOptions, keyType } from './ew-role-vc';
import { error } from 'console';
import { fromPrivateKey, RoleCredentialSubject, SignerService } from 'iam-client-lib';

export class ResidentCardIssuance {
#exchangeId = 'ew-role-issuance';
#callbackUrl: string;
queryType = VpRequestQueryType.didAuth;

constructor(callbackUrl: string) {
this.#callbackUrl = callbackUrl;
}

getExchangeId(): string {
return this.#exchangeId;
}

getExchangeDefinition(): ExchangeDefinitionDto {
const exchangeDefinition: ExchangeDefinitionDto = {
exchangeId: this.#exchangeId,
query: [
{
type: this.queryType,
credentialQuery: []
}
],
interactServices: [
{
type: VpRequestInteractServiceType.mediatedPresentation
}
],
isOneTime: false,
callback: [
{
url: this.#callbackUrl
}
]
};
return plainToClass(ExchangeDefinitionDto, exchangeDefinition);
}

/**
*
* TODO: get and approve presentation review
* @param vp
* @param walletClient
* @returns
*/
async issueCredential(vp: VerifiablePresentationDto, walletClient: WalletClient) {
let signerService = await fromPrivateKey(
'22dc144318961ebe99d19c12f44ea10439a942585e6e7d93c0b4a06397dcd138',
'https://volta-rpc.energyweb.org/'
);
if (!vp.holder) {
return { errors: ['holder of vp not provided'] };
}
const issuingDID = await walletClient.createDID('key');
const credentialObject = this.createCredentialObject(issuingDID.id, vp.holder);
const stringifyCredential = JSON.stringify(credentialObject);
let proofOptionsObject = proofOptions;
proofOptionsObject.verificationMethod = `${vp.holder}#controller`;
const preparedVC = await prepareIssueCredential(
stringifyCredential,
JSON.stringify(proofOptionsObject),
JSON.stringify(keyType)
);

const preparation = JSON.parse(preparedVC);

const typedData = preparation.signingInput;
delete typedData.types['EIP712Domain'];

const signature = await signerService.signTypedData(typedData.domain, typedData.types, typedData.message);

const signedCredential = await completeIssueCredential(stringifyCredential, preparedVC, signature);

const verifiableCredential = JSON.parse(signedCredential) as VerifiableCredential<RoleCredentialSubject>;

const presentation: Presentation = {
'@context': ['https://www.w3.org/2018/credentials/v1'],
type: ['VerifiablePresentation'],
verifiableCredential: [verifiableCredential]
};
const verificationMethodURI = issuingDID?.verificationMethod[0]?.id;
if (!verificationMethodURI) {
return { errors: ['verification method for issuance not available'] };
}
const presentationOptions: ProvePresentationOptionsDto = {
verificationMethod: verificationMethodURI
};
const provePresentationDto = {
options: presentationOptions,
presentation
};
const returnVp = await walletClient.provePresentation(provePresentationDto);
return {
errors: [],
vp: returnVp
};
}

private createCredentialObject(issuingDID: string, holderDID: string): CredentialDto {
return {
'@context': ['https://www.w3.org/2018/credentials/v1'],
id: 'https://issuer.oidp.uscis.gov/credentials/83627465',
type: [CredentialType.VerifiableCredential, CredentialType.EWFRole],
issuer: issuingDID,
issuanceDate: '2019-12-03T12:19:52Z',
expirationDate: '2029-12-03T12:19:52Z',
credentialSubject: {
id: holderDID,
issuerFields: [],
role: { namespace: 'test.iam.ewc', version: '1' }
},
credentialStatus: {
id: 'https://energyweb.org/credential/0x760b59d40ca5be5d197f82df5f7acb8ecaecd5d03c38f91f909e7456ae464430#list',
type: StatusListEntryType.Entry2021,
statusListIndex: '1',
statusPurpose: CredentialStatusPurpose.REVOCATION,
statusListCredential:
'https://identitycache.org/v1/status-list/urn:uuid:1feb8928-cdad-4f36-9096-e854151cfef4'
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/**
* 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 { plainToClass } from 'class-transformer';
import { ExchangeDefinitionDto } from '../../../../src/vc-api/exchanges/dtos/exchange-definition.dto';
import { VpRequestInteractServiceType } from '../../../../src/vc-api/exchanges/types/vp-request-interact-service-type';
import { VpRequestQueryType } from '../../../../src/vc-api/exchanges/types/vp-request-query-type';

export class EWRolePresentation {
#exchangeId = `b229a18f-db45-4b33-8d36-25d442467bab`;
#callbackUrl: string;
queryType = VpRequestQueryType.presentationDefinition;

constructor(callbackUrl: string) {
this.#callbackUrl = callbackUrl;
}

getExchangeId(): string {
return this.#exchangeId;
}

getExchangeDefinition(): ExchangeDefinitionDto {
const exchangeDefinition: ExchangeDefinitionDto = {
exchangeId: this.#exchangeId,
query: [
{
type: this.queryType,
credentialQuery: [
{
presentationDefinition: {
id: '286bc1e0-f1bd-488a-a873-8d71be3c690e',
input_descriptors: [
{
id: 'ew_role_credential',
name: 'EW Role Credential',
purpose: 'Only allowed to present EW Role Credential',
constraints: {
fields: [
{
path: ['$.type'],
filter: {
type: 'array',
contains: {
type: 'string',
const: 'EWFRole'
}
}
}
]
}
}
]
}
}
]
}
],
interactServices: [
{
type: VpRequestInteractServiceType.unmediatedPresentation
}
],
isOneTime: true,
callback: [
{
url: this.#callbackUrl
}
]
};
return plainToClass(ExchangeDefinitionDto, exchangeDefinition);
}
}
Loading