Skip to content

Commit

Permalink
refactor: Run prettier format
Browse files Browse the repository at this point in the history
  • Loading branch information
ankurdotb committed Dec 20, 2023
1 parent 307fd67 commit 7700bf4
Show file tree
Hide file tree
Showing 34 changed files with 913 additions and 852 deletions.
4 changes: 2 additions & 2 deletions src/controllers/issuer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -740,10 +740,10 @@ export class IssuerController {
const did = request.params.did
? await new IdentityServiceStrategySetup(response.locals.customer.customerId).agent.resolveDid(
request.params.did
)
)
: await new IdentityServiceStrategySetup(response.locals.customer.customerId).agent.listDids(
response.locals.customer
);
);

return response.status(StatusCodes.OK).json(did);
} catch (error) {
Expand Down
6 changes: 3 additions & 3 deletions src/controllers/presentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,11 @@ export class PresentationController {
}

if (makeFeePayment) {
const setResult = await cheqdPresentation.trySetStatusList2021(identityServiceStrategySetup.agent)
const setResult = await cheqdPresentation.trySetStatusList2021(identityServiceStrategySetup.agent);
if (setResult.error) {
return response.status(setResult.status).send({
error: setResult.error
})
error: setResult.error,
});
}
if (cheqdPresentation.isPaymentNeeded()) {
const feePaymentResult = await cheqdPresentation.makeFeePayment(
Expand Down
2 changes: 1 addition & 1 deletion src/database/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export class Postgres implements AbstractDatabase {
ssl: config.ssl
? {
ca: EXTERNAL_DB_CERT,
}
}
: false,
migrations: [
...migrations,
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export function isValidService(didDocument: DIDDocument): boolean {
return didDocument.service
? didDocument?.service?.every((s) => {
return s?.serviceEndpoint && s?.id && s?.type;
})
})
: true;
}

Expand Down
232 changes: 113 additions & 119 deletions src/services/connectors/verida.ts
Original file line number Diff line number Diff line change
@@ -1,136 +1,130 @@
import type { EnvironmentType } from '@verida/types'
import { Context, Network } from '@verida/client-ts'
import { AutoAccount } from '@verida/account-node'
import type { EnvironmentType } from '@verida/types';
import { Context, Network } from '@verida/client-ts';
import { AutoAccount } from '@verida/account-node';

import type { CredentialDataRecord, DataRecord } from '../../types/verida.js'
import { VERIDA_APP_NAME, VERIDA_CREDENTIAL_RECORD_SCHEMA } from '../../types/constants.js'
import type { CredentialDataRecord, DataRecord } from '../../types/verida.js';
import { VERIDA_APP_NAME, VERIDA_CREDENTIAL_RECORD_SCHEMA } from '../../types/constants.js';

import * as dotenv from 'dotenv'
import type { VerifiableCredential } from '@veramo/core'
dotenv.config()
import * as dotenv from 'dotenv';
import type { VerifiableCredential } from '@veramo/core';
dotenv.config();

const { VERIDA_NETWORK, POLYGON_RPC_URL, VERIDA_PRIVATE_KEY, POLYGON_PRIVATE_KEY } = process.env
const { VERIDA_NETWORK, POLYGON_RPC_URL, VERIDA_PRIVATE_KEY, POLYGON_PRIVATE_KEY } = process.env;

/**
* Helper class for the Verida protocol.
*
* Run the init method before running any other method.
*/
export class VeridaService {
private context?: Context
private account?: AutoAccount
private context?: Context;
private account?: AutoAccount;

static instance = new VeridaService()
static instance = new VeridaService();

/**
* Initialise the Verida account and context.
*
* @param environment The Verida environment.
* @param contextName The Context name of the application.
* @param accountPrivateKey The private key of the account
*/
async init(
environment: EnvironmentType,
contextName: string,
accountPrivateKey: string,
polygonPrivateKey: string
) {
if(this.context && this.account) {
return
}
this.account = new AutoAccount({
privateKey: accountPrivateKey,
environment,
didClientConfig: {
callType: 'web3',
web3Config: {
rpcUrl: POLYGON_RPC_URL,
privateKey: polygonPrivateKey, // Polygon private key for creating DID, not needed in our case but required in the current version of the config.
},
},
})
try {
this.context = await Network.connect({
client: {
environment,
},
context: {
name: contextName,
},
account: this.account,
})
} catch (error) {
throw new Error(`Error: ${error}`)
}
}
/**
* Initialise the Verida account and context.
*
* @param environment The Verida environment.
* @param contextName The Context name of the application.
* @param accountPrivateKey The private key of the account
*/
async init(
environment: EnvironmentType,
contextName: string,
accountPrivateKey: string,
polygonPrivateKey: string
) {
if (this.context && this.account) {
return;
}
this.account = new AutoAccount({
privateKey: accountPrivateKey,
environment,
didClientConfig: {
callType: 'web3',
web3Config: {
rpcUrl: POLYGON_RPC_URL,
privateKey: polygonPrivateKey, // Polygon private key for creating DID, not needed in our case but required in the current version of the config.
},
},
});
try {
this.context = await Network.connect({
client: {
environment,
},
context: {
name: contextName,
},
account: this.account,
});
} catch (error) {
throw new Error(`Error: ${error}`);
}
}

/**
* Send data to a DID via the Verida protocol.
*
* @param recipientDid The DID of the recipient.
* @param subject The subject of the message (similar to an email subject).
* @param data The data to be sent.
*/
async sendData(recipientDid: string, subject: string, data: DataRecord) {
try {
if(!this.context) {
await VeridaService.instance.init(
VERIDA_NETWORK,
VERIDA_APP_NAME,
VERIDA_PRIVATE_KEY,
POLYGON_PRIVATE_KEY
)
}
/**
* Send data to a DID via the Verida protocol.
*
* @param recipientDid The DID of the recipient.
* @param subject The subject of the message (similar to an email subject).
* @param data The data to be sent.
*/
async sendData(recipientDid: string, subject: string, data: DataRecord) {
try {
if (!this.context) {
await VeridaService.instance.init(
VERIDA_NETWORK,
VERIDA_APP_NAME,
VERIDA_PRIVATE_KEY,
POLYGON_PRIVATE_KEY
);
}

const messagingClient = await this.context?.getMessaging()
const messagingClient = await this.context?.getMessaging();

const messageType = 'inbox/type/dataSend' // There are different types of message, here we are sending some data.
const messageData = {
data: [data],
}
const messageConfig = {
recipientContextName: 'Verida: Vault', // The inbox of a DID is on the 'Verida: Vault' context. This context is the private space of this DID.
did: recipientDid,
}
const messageType = 'inbox/type/dataSend'; // There are different types of message, here we are sending some data.
const messageData = {
data: [data],
};
const messageConfig = {
recipientContextName: 'Verida: Vault', // The inbox of a DID is on the 'Verida: Vault' context. This context is the private space of this DID.
did: recipientDid,
};

await messagingClient?.send(
recipientDid,
messageType,
messageData,
subject,
messageConfig
)
} catch (error) {
throw new Error(`Error sending data ${error}`)
}
}
await messagingClient?.send(recipientDid, messageType, messageData, subject, messageConfig);
} catch (error) {
throw new Error(`Error sending data ${error}`);
}
}

/**
* Send a Verifiable Credential to a DID via the Verida protocol.
*
* @param recipientDid The DID of the recipient.
* @param messageSubject The subject of the message in which the Credential will be sent to the recipient (similar to an email subject).
* @param credential The credential itself.
* @param credentialName The name of the credential. For instance, will be displayed in the Verida Wallet UI.
* @param credentialSummary A summary of the credential. For instance, will be displayed in the Verida Wallet UI.
*/
async sendCredential(
recipientDid: string,
messageSubject: string,
credential: VerifiableCredential,
credentialName: string,
credentialSchema: string,
credentialSummary?: string
) {
// The Credential record is how Verida wrap the credential to store it on the Network. Check the JSdoc of the type and each property. They are following the Verida Credential Record schema.
const credentialRecord: CredentialDataRecord = {
name: credentialName,
summary: credentialSummary,
schema: VERIDA_CREDENTIAL_RECORD_SCHEMA,
didJwtVc: credential.proof.jwt,
credentialSchema,
credentialData: credential,
}
await this.sendData(recipientDid, messageSubject, credentialRecord)
}
}
/**
* Send a Verifiable Credential to a DID via the Verida protocol.
*
* @param recipientDid The DID of the recipient.
* @param messageSubject The subject of the message in which the Credential will be sent to the recipient (similar to an email subject).
* @param credential The credential itself.
* @param credentialName The name of the credential. For instance, will be displayed in the Verida Wallet UI.
* @param credentialSummary A summary of the credential. For instance, will be displayed in the Verida Wallet UI.
*/
async sendCredential(
recipientDid: string,
messageSubject: string,
credential: VerifiableCredential,
credentialName: string,
credentialSchema: string,
credentialSummary?: string
) {
// The Credential record is how Verida wrap the credential to store it on the Network. Check the JSdoc of the type and each property. They are following the Verida Credential Record schema.
const credentialRecord: CredentialDataRecord = {
name: credentialName,
summary: credentialSummary,
schema: VERIDA_CREDENTIAL_RECORD_SCHEMA,
didJwtVc: credential.proof.jwt,
credentialSchema,
credentialData: credential,
};
await this.sendData(recipientDid, messageSubject, credentialRecord);
}
}
12 changes: 6 additions & 6 deletions src/services/identity/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,11 +290,11 @@ export class Veramo {
? await agent.cheqdIssueRevocableCredentialWithStatusList2021({
issuanceOptions,
statusOptions: statusListOptions as RevocationStatusOptions,
})
})
: await agent.cheqdIssueSuspendableCredentialWithStatusList2021({
issuanceOptions,
statusOptions: statusListOptions as SuspensionStatusOptions,
});
});
} else {
verifiable_credential = await agent.createVerifiableCredential(issuanceOptions);
}
Expand Down Expand Up @@ -468,7 +468,7 @@ export class Veramo {
feePaymentAmount: `${toMinimalDenom(condition.feePaymentAmount)}${MINIMAL_DENOM}`,
intervalInSeconds: condition.feePaymentWindow * 60,
};
})
})
: (function () {
// validate relevant components - case: feePaymentAddress
if (!statusOptions.feePaymentAddress)
Expand All @@ -490,7 +490,7 @@ export class Veramo {
intervalInSeconds: statusOptions.feePaymentWindow * 60,
},
];
})()
})()
) satisfies PaymentCondition[];

return await agent.cheqdCreateStatusList2021({
Expand Down Expand Up @@ -671,7 +671,7 @@ export class Veramo {
intervalInSeconds: condition.feePaymentWindow * 60,
type: 'timelockPayment',
};
})
})
: (function () {
// validate relevant components
if (
Expand Down Expand Up @@ -701,7 +701,7 @@ export class Veramo {
type: 'timelockPayment',
},
];
})()
})()
) satisfies PaymentCondition[] | undefined;

switch (statusOptions.statusAction) {
Expand Down
6 changes: 2 additions & 4 deletions src/services/w3c_credential.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import { StatusCodes } from 'http-status-codes';
import type { ICommonErrorResponse } from '../types/authentication.js';
import type {
Expand All @@ -18,7 +17,6 @@ import { CommonReturn, type FeePaymentOptions } from '../types/shared.js';
import { JWT_PROOF_TYPE } from '../types/constants.js';
import type { StatusList2021Revocation, StatusList2021Suspension } from '@cheqd/did-provider-cheqd';


export interface ICheqdCredential extends UnsignedCredential {
proof: {
type: string;
Expand Down Expand Up @@ -135,9 +133,9 @@ export class CheqdW3CVerifiableCredential extends CommonReturn implements ICheqd

public isPaymentNeeded(): boolean {
if (!this.statusList) {
return false
return false;
}
return this.statusList.metadata.encrypted
return this.statusList.metadata.encrypted;
}

public async makeFeePayment(agent: IIdentityService, customer: CustomerEntity): Promise<ICommonErrorResponse> {
Expand Down
Loading

0 comments on commit 7700bf4

Please sign in to comment.