Skip to content

Commit

Permalink
fix: Fix test payloads (#553)
Browse files Browse the repository at this point in the history
* fix: Fix test payloads

* Update mainnet payloads

* fix statuslist tests
  • Loading branch information
DaevMithran committed Jul 1, 2024
1 parent 198c9da commit 78b4b84
Show file tree
Hide file tree
Showing 29 changed files with 168 additions and 105 deletions.
7 changes: 4 additions & 3 deletions src/controllers/api/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { EventTracker, eventTracker } from '../../services/track/tracker.js';
import type { ISubmitOperation, ISubmitStripeCustomerCreateData } from '../../services/track/submitter.js';
import * as dotenv from 'dotenv';
import { validate } from '../validator/decorator.js';
import { SupportedKeyTypes } from '@veramo/utils';
dotenv.config();

export class AccountController {
Expand Down Expand Up @@ -249,7 +250,7 @@ export class AccountController {
const accounts = await PaymentAccountService.instance.find({ customer });
if (accounts.length === 0) {
const key = await new IdentityServiceStrategySetup(customer.customerId).agent.createKey(
'Secp256k1',
SupportedKeyTypes.Secp256k1,
customer
);
if (!key) {
Expand Down Expand Up @@ -434,12 +435,12 @@ export class AccountController {
}
}

// 3. Check is paymentAccount exists for the customer
// 3. Check if paymentAccount exists for the customer
const accounts = await PaymentAccountService.instance.find({ customer });
paymentAccount = accounts.find((account) => account.namespace === CheqdNetwork.Testnet) || null;
if (paymentAccount === null) {
const key = await new IdentityServiceStrategySetup(customer.customerId).agent.createKey(
'Secp256k1',
SupportedKeyTypes.Secp256k1,
customer
);
if (!key) {
Expand Down
18 changes: 13 additions & 5 deletions src/controllers/api/did.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import type {
} from '../../types/did.js';
import { check, param } from '../validator/index.js';
import type { IKey, RequireOnly } from '@veramo/core';
import { extractPublicKeyHex } from '@veramo/utils';
import { SupportedKeyTypes, extractPublicKeyHex } from '@veramo/utils';
import type { KeyImport } from '../../types/key.js';
import { eventTracker } from '../../services/track/tracker.js';
import { OperationCategoryNameEnum, OperationNameEnum } from '../../types/constants.js';
Expand Down Expand Up @@ -183,8 +183,12 @@ export class DIDController {
if (options) {
const publicKeyHex =
options.key ||
(await identityServiceStrategySetup.agent.createKey('Ed25519', response.locals.customer))
.publicKeyHex;
(
await identityServiceStrategySetup.agent.createKey(
SupportedKeyTypes.Ed25519,
response.locals.customer
)
).publicKeyHex;
const pkBase64 =
publicKeyHex.length == 43 ? publicKeyHex : toString(fromString(publicKeyHex, 'hex'), 'base64');

Expand All @@ -207,8 +211,12 @@ export class DIDController {
} else if (verificationMethodType) {
const publicKeyHex =
key ||
(await identityServiceStrategySetup.agent.createKey('Ed25519', response.locals.customer))
.publicKeyHex;
(
await identityServiceStrategySetup.agent.createKey(
SupportedKeyTypes.Ed25519,
response.locals.customer
)
).publicKeyHex;
didDocument = generateDidDoc({
verificationMethod: verificationMethodType,
verificationMethodId: 'key-1',
Expand Down
24 changes: 23 additions & 1 deletion src/controllers/api/key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,19 @@ import { eventTracker } from '../../services/track/tracker.js';
import type { IKeyTrack, ITrackOperation } from '../../types/track.js';
import { OperationCategoryNameEnum, OperationNameEnum } from '../../types/constants.js';
import { validate } from '../validator/decorator.js';
import { SupportedKeyTypes } from '@veramo/utils';

// ToDo: Make the format of /key/create and /key/read the same
// ToDo: Add valdiation for /key/import
export class KeyController {
public static keyCreateValidator = [
check('type')
.optional()
.isString()
.isIn([SupportedKeyTypes.Ed25519, SupportedKeyTypes.Secp256k1])
.withMessage('Invalid key type')
.bail(),
];
public static keyGetValidator = [
check('kid')
.exists()
Expand Down Expand Up @@ -56,6 +65,15 @@ export class KeyController {
* tags: [ Key ]
* summary: Create an identity key pair.
* description: This endpoint creates an identity key pair associated with the user's account for custodian-mode clients.
* parameters:
* - name: type
* description: Key type of the identity key pair to create.
* in: query
* schema:
* type: string
* enum:
* - Ed25519
* - Secp256k1
* responses:
* 200:
* description: The request was successful.
Expand Down Expand Up @@ -86,7 +104,11 @@ export class KeyController {
// Get strategy e.g. postgres or local
const identityServiceStrategySetup = new IdentityServiceStrategySetup(response.locals.customer.customerId);
try {
const key = await identityServiceStrategySetup.agent.createKey('Ed25519', response.locals.customer);
const keyType = request.query.type as SupportedKeyTypes | undefined;
const key = await identityServiceStrategySetup.agent.createKey(
keyType || SupportedKeyTypes.Ed25519,
response.locals.customer
);

eventTracker.emit('track', {
name: OperationNameEnum.KEY_CREATE,
Expand Down
6 changes: 5 additions & 1 deletion src/services/api/customer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { PaymentAccountService } from './payment-account.js';
import { CheqdNetwork } from '@cheqd/sdk';
import { v4 as uuidv4 } from 'uuid';
import type { UpdateCustomerEntity } from '../../types/customer.js';
import { SupportedKeyTypes } from '@veramo/utils';
dotenv.config();

export class CustomerService {
Expand All @@ -35,7 +36,10 @@ export class CustomerService {
await this.customerRepository.insert(customerEntity);

// Create a new Cosmos account for the customer and make a link with customer entity;
const key = await new IdentityServiceStrategySetup(name).agent.createKey('Secp256k1', customerEntity);
const key = await new IdentityServiceStrategySetup(name).agent.createKey(
SupportedKeyTypes.Secp256k1,
customerEntity
);
await PaymentAccountService.instance.create(CheqdNetwork.Testnet, true, customerEntity, key);
return {
customerId: customerEntity.customerId,
Expand Down
5 changes: 3 additions & 2 deletions src/services/identity/abstract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import type { CustomerEntity } from '../../database/entities/customer.entity.js'
import type { KeyEntity } from '../../database/entities/key.entity.js';
import type { UserEntity } from '../../database/entities/user.entity';
import type { APIKeyEntity } from '../../database/entities/api.key.entity';
import type { SupportedKeyTypes } from '@veramo/utils';

export abstract class AbstractIdentityService implements IIdentityService {
agent?: VeramoAgent;
Expand All @@ -51,12 +52,12 @@ export abstract class AbstractIdentityService implements IIdentityService {
throw new Error(`Not supported`);
}

createKey(type: 'Ed25519' | 'Secp256k1', customer?: CustomerEntity, keyAlias?: string): Promise<KeyEntity> {
createKey(type: SupportedKeyTypes, customer?: CustomerEntity, keyAlias?: string): Promise<KeyEntity> {
throw new Error(`Not supported`);
}

importKey(
type: 'Ed25519' | 'Secp256k1',
type: SupportedKeyTypes,
privateKeyHex: string,
customer?: CustomerEntity,
keyAlias?: string
Expand Down
9 changes: 7 additions & 2 deletions src/services/identity/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ import { toCoin, toDefaultDkg, toMinimalDenom } from '../../helpers/helpers.js';
import { jwtDecode } from 'jwt-decode';
import type { ICheqdCreateLinkedResourceArgs } from '@cheqd/did-provider-cheqd';
import type { TPublicKeyEd25519 } from '@cheqd/did-provider-cheqd';
import { SupportedKeyTypes } from '@veramo/utils';

// dynamic import to avoid circular dependency
const VeridaResolver =
Expand Down Expand Up @@ -152,15 +153,19 @@ export class Veramo {
return createAgent({ plugins });
}

async createKey(agent: TAgent<IKeyManager>, type: 'Ed25519' | 'Secp256k1' = 'Ed25519') {
async createKey(agent: TAgent<IKeyManager>, type: SupportedKeyTypes = SupportedKeyTypes.Ed25519) {
const [kms] = await agent.keyManagerGetKeyManagementSystems();
return await agent.keyManagerCreate({
type: type || 'Ed25519',
kms,
});
}

async importKey(agent: TAgent<IKeyManager>, type: 'Ed25519' | 'Secp256k1' = 'Ed25519', privateKeyHex: string) {
async importKey(
agent: TAgent<IKeyManager>,
type: SupportedKeyTypes = SupportedKeyTypes.Ed25519,
privateKeyHex: string
) {
const [kms] = await agent.keyManagerGetKeyManagementSystems();
return await agent.keyManagerImport({
type: type || 'Ed25519',
Expand Down
5 changes: 3 additions & 2 deletions src/services/identity/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import type { CustomerEntity } from '../../database/entities/customer.entity.js'
import type { KeyEntity } from '../../database/entities/key.entity.js';
import type { UserEntity } from '../../database/entities/user.entity.js';
import type { APIKeyEntity } from '../../database/entities/api.key.entity.js';
import type { SupportedKeyTypes } from '@veramo/utils';

dotenv.config();

Expand All @@ -59,9 +60,9 @@ export interface IIdentityService {

initAgent(): TAgent<any>;
createAgent?(customer: CustomerEntity): Promise<VeramoAgent>;
createKey(type: 'Ed25519' | 'Secp256k1', customer?: CustomerEntity, keyAlias?: string): Promise<KeyEntity>;
createKey(type: SupportedKeyTypes, customer?: CustomerEntity, keyAlias?: string): Promise<KeyEntity>;
importKey(
type: 'Ed25519' | 'Secp256k1',
type: SupportedKeyTypes,
privateKeyHex: string,
customer?: CustomerEntity,
keyAlias?: string
Expand Down
5 changes: 3 additions & 2 deletions src/services/identity/postgres.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import type { CheqdProviderError } from '@cheqd/did-provider-cheqd';
import type { TPublicKeyEd25519 } from '@cheqd/did-provider-cheqd';
import { toTPublicKeyEd25519 } from '../helpers.js';
import type { APIServiceOptions } from '../../types/admin.js';
import { SupportedKeyTypes } from '@veramo/utils';

dotenv.config();

Expand Down Expand Up @@ -158,15 +159,15 @@ export class PostgresIdentityService extends DefaultIdentityService {
});
}

async createKey(type: 'Ed25519' | 'Secp256k1' = 'Ed25519', customer?: CustomerEntity, keyAlias?: string) {
async createKey(type: SupportedKeyTypes = SupportedKeyTypes.Ed25519, customer?: CustomerEntity, keyAlias?: string) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const key = await Veramo.instance.createKey(this.agent!, type);
// Update our specific key columns
return await KeyService.instance.update(key.kid, customer, keyAlias, new Date());
}

async importKey(
type: 'Ed25519' | 'Secp256k1' = 'Ed25519',
type: SupportedKeyTypes = SupportedKeyTypes.Ed25519,
privateKeyHex: string,
customer?: CustomerEntity,
keyAlias?: string
Expand Down
14 changes: 14 additions & 0 deletions src/static/swagger-api.json
Original file line number Diff line number Diff line change
Expand Up @@ -1155,6 +1155,20 @@
],
"summary": "Create an identity key pair.",
"description": "This endpoint creates an identity key pair associated with the user's account for custodian-mode clients.",
"parameters": [
{
"name": "type",
"description": "Key type of the identity key pair to create.",
"in": "query",
"schema": {
"type": "string",
"enum": [
"Ed25519",
"Secp256k1"
]
}
}
],
"responses": {
"200": {
"description": "The request was successful.",
Expand Down
3 changes: 2 additions & 1 deletion src/types/key.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { ManagedKeyInfo } from '@veramo/core';
import type { KeyEntity } from '../database/entities/key.entity.js';
import type { UnsuccessfulQueryResponseBody, UnsuccessfulResponseBody } from './shared.js';
import type { SupportedKeyTypes } from '@veramo/utils';

// Interfaces

Expand All @@ -10,7 +11,7 @@ export interface KeyImport {
ivHex: string | undefined;
salt: string | undefined;
alias?: string;
type: 'Ed25519' | 'Secp256k1';
type: SupportedKeyTypes.Ed25519 | SupportedKeyTypes.Secp256k1;
}

// Requests
Expand Down
13 changes: 7 additions & 6 deletions tests/e2e/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ export enum CONTENT_TYPE {
export const DID_METHOD = 'cheqd';
export const DID_NOT_FOUND_ERROR = 'notFound';

export const DEFAULT_MAINNET_DID = 'did:cheqd:mainnet:7c950b5d-dbbb-4a12-9d79-6b553ca0c271';
export const DEFAULT_TESTNET_DID = 'did:cheqd:testnet:0c3581f0-011f-4263-b1ca-15ad70d54ede';
export const DEFAULT_TESTNET_DID_IDENTIFIER = '0c3581f0-011f-4263-b1ca-15ad70d54ede';
export const DEFAULT_TESTNET_DID_RESOURCE_ID = '1a79a313-8fc3-421c-af3d-34730fa046d1';
// todo: create a new mainnet did from test account
export const DEFAULT_MAINNET_DID = 'did:cheqd:mainnet:a6ef1d50-a040-4db3-b833-6bb3a0ff1eb6';
export const DEFAULT_TESTNET_DID = 'did:cheqd:testnet:5RpEg66jhhbmASWPXJRWrA';
export const DEFAULT_TESTNET_DID_IDENTIFIER = '5RpEg66jhhbmASWPXJRWrA';

export const DEACTIVATED_TESTNET_DID = 'did:cheqd:testnet:UYBzUsTPHpTEXSnzYGTzUZ';

export const TESTNET_DID_WITH_JSON_RESOURCE = 'did:cheqd:testnet:c69d7867-be90-4dea-8bbf-f4419d3599d8';
export const TESTNET_DID_WITH_JSON_RESOURCE_ID = '3194b5a6-1b73-44a0-8ccf-27dc01509eb2';
Expand All @@ -45,10 +47,9 @@ export const NOT_EXISTENT_TESTNET_DID_IDENTIFIER = 'd4a13003-0bc5-4608-b23a-54ea

// Credential status list names
export const DEFAULT_STATUS_LIST_ENCRYPTED_NAME = 'cheqd-employee-credentials-encrypted';
export const DEFAULT_STATUS_LIST_UNENCRYPTED_NAME = 'cheqd-employee-credentials-unencrypted';
export const DEFAULT_STATUS_LIST_UNENCRYPTED_NAME = 'testingStatusList';
export const DEFAULT_STATUS_LIST_PAYMENT_ADDRESS = 'cheqd1qs0nhyk868c246defezhz5eymlt0dmajna2csg';
export const DEFAULT_STATUS_LIST_INDICES = [10, 3199, 12109, 130999];
export const DEFAULT_STATUS_LIST_NAME = 'cheqd-employee-credentials';

// Credential names
export const DEFAULT_SUBJECT_DID = 'did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK';
Expand Down
4 changes: 2 additions & 2 deletions tests/e2e/credential/issue-verify-flow.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { VerifiableCredential } from '@veramo/core';
import { test, expect } from '@playwright/test';
import { StatusCodes } from 'http-status-codes';
import * as fs from 'fs';
import { CONTENT_TYPE, PAYLOADS_PATH } from '../constants';
import { CONTENT_TYPE, DEACTIVATED_TESTNET_DID, PAYLOADS_PATH } from '../constants';

test.use({ storageState: 'playwright/.auth/user.json' });

Expand Down Expand Up @@ -53,7 +53,7 @@ test(' Issue a jwt credential with a deactivated DID', async ({ request }) => {
const credentialData = JSON.parse(
fs.readFileSync(`${PAYLOADS_PATH.CREDENTIAL}/credential-issue-jwt.json`, 'utf-8')
);
credentialData.issuerDid = 'did:cheqd:testnet:edce6dfb-b59c-493b-a4b8-1d16a6184349';
credentialData.issuerDid = DEACTIVATED_TESTNET_DID;
const response = await request.post(`/credential/issue`, {
data: JSON.stringify(credentialData),
headers: {
Expand Down
3 changes: 2 additions & 1 deletion tests/e2e/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
DEFAULT_STATUS_LIST_ENCRYPTED_NAME,
DEFAULT_STATUS_LIST_INDICES,
DEFAULT_STATUS_LIST_PAYMENT_ADDRESS,
DEFAULT_STATUS_LIST_UNENCRYPTED_NAME,
DEFAULT_SUBJECT_DID,
DEFAULT_TESTNET_DID,
} from './constants';
Expand Down Expand Up @@ -120,7 +121,7 @@ export const buildSimpleIssueCredentialRequest = (
issuerDid = DEFAULT_TESTNET_DID,
subjectDid = DEFAULT_SUBJECT_DID,
statusPurpose = 'revocation',
statusListName = 'employee-credentials'
statusListName = DEFAULT_STATUS_LIST_UNENCRYPTED_NAME
) => {
return {
issuerDid: issuerDid,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"did": "did:cheqd:mainnet:7bf81a20-633c-4cc7-bc4a-5a45801005e0",
"did": "did:cheqd:mainnet:a6ef1d50-a040-4db3-b833-6bb3a0ff1eb6",
"statusListName": "cheqd-employee-credentials-encrypted",
"paymentConditions": [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"did": "did:cheqd:mainnet:7bf81a20-633c-4cc7-bc4a-5a45801005e0",
"did": "did:cheqd:mainnet:a6ef1d50-a040-4db3-b833-6bb3a0ff1eb6",
"statusListName": "cheqd-employee-credentials-encrypted",
"length": 140000,
"encoding": "base64url"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"did": "did:cheqd:mainnet:7bf81a20-633c-4cc7-bc4a-5a45801005e0",
"did": "did:cheqd:mainnet:a6ef1d50-a040-4db3-b833-6bb3a0ff1eb6",
"statusListName": "cheqd-employee-credentials-encrypted",
"paymentConditions": [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"did": "did:cheqd:mainnet:7bf81a20-633c-4cc7-bc4a-5a45801005e0",
"did": "did:cheqd:mainnet:a6ef1d50-a040-4db3-b833-6bb3a0ff1eb6",
"statusListName": "cheqd-employee-credentials-encrypted",
"length": 140000,
"encoding": "base64url"
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/payloads/credential/credential-issue-jsonld.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"issuerDid": "did:cheqd:testnet:4JdgsZ4A8LegKXdsKE3v6X",
"issuerDid": "did:cheqd:testnet:5RpEg66jhhbmASWPXJRWrA",
"subjectDid": "did:key:z6MkqJNR1DHxX2qxqDYx9tNDsXoNRVpaVvJkLPeCYqaARz1n",
"attributes": {
"gender": "male",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"issuerDid": "did:cheqd:testnet:4JdgsZ4A8LegKXdsKE3v6X",
"issuerDid": "did:cheqd:testnet:5RpEg66jhhbmASWPXJRWrA",
"subjectDid": "did:key:z6MkqJNR1DHxX2qxqDYx9tNDsXoNRVpaVvJkLPeCYqaARz1n",
"attributes": {
"gender": "male",
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/payloads/credential/credential-issue-jwt.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"issuerDid": "did:cheqd:testnet:4JdgsZ4A8LegKXdsKE3v6X",
"issuerDid": "did:cheqd:testnet:5RpEg66jhhbmASWPXJRWrA",
"subjectDid": "did:key:z6MkqJNR1DHxX2qxqDYx9tNDsXoNRVpaVvJkLPeCYqaARz1n",
"attributes": {
"gender": "male",
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/payloads/credential/credential-issue-vda.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
},
"credentialSchema": "https://common.schemas.verida.io/identity/kyc/FinClusive/individual-basic/v0.1.0/schema.json",
"format": "jwt",
"issuerDid": "did:cheqd:testnet:4JdgsZ4A8LegKXdsKE3v6X",
"issuerDid": "did:cheqd:testnet:5RpEg66jhhbmASWPXJRWrA",
"subjectDid": "did:vda:mainnet:0x503DeeB3d41F835E22e2fa64940b18Eeb607759a"
}
Loading

0 comments on commit 78b4b84

Please sign in to comment.