-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: Init verida connector * feat: Add verida packages
- Loading branch information
1 parent
5d61791
commit c1ac128
Showing
10 changed files
with
2,797 additions
and
165 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
import { EnvironmentType } from '@verida/types' | ||
import { Context, Network } from '@verida/client-ts' | ||
import { AutoAccount } from '@verida/account-node' | ||
|
||
import { Credential } from '../../types/types.js' | ||
import { CredentialDataRecord, DataRecord } from '../../types/verida.js' | ||
import { POLYGON_RPC_URL, VERIDA_CREDENTIAL_RECORD_SCHEMA } from '../../types/constants.js' | ||
|
||
import * as dotenv from 'dotenv' | ||
dotenv.config() | ||
|
||
const { VERIDA_NETWORK, VERIDA_APP_NAME, ISSUER_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 | ||
|
||
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) { | ||
console.log(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, | ||
ISSUER_VERIDA_PRIVATE_KEY, | ||
POLYGON_PRIVATE_KEY | ||
) | ||
} | ||
|
||
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, | ||
} | ||
|
||
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: Credential, | ||
credentialName: 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: credential['@context'][0], | ||
credentialData: credential, | ||
} | ||
await this.sendData(recipientDid, messageSubject, credentialRecord) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** Structure of the record stored on the Verida Network */ | ||
export interface DataRecord { | ||
/** Name/Title of the record, for instance used while browsing the UI. Optional. */ | ||
name?: string; | ||
/** A summary of the data, could be displayed in the UI. Optional. */ | ||
summary?: string; | ||
/** The schema of the record, For Credential data, it will be the Credential schema. Required. */ | ||
schema: string; | ||
/** Any specific attributes of the record. These are following the schema mentioned above. */ | ||
[key: string]: unknown; | ||
} | ||
|
||
/** Structure of a Credential record stored on the Verida Network. */ | ||
export interface CredentialDataRecord extends DataRecord { | ||
/** Name is mandatory */ | ||
name: string; | ||
/** DID JWT of this credential */ | ||
didJwtVc: string; | ||
/** Schema of the DID-JWT Verifiable Credential */ | ||
credentialSchema: string; | ||
/** Data included in the DID-JWT Verifiable Credential */ | ||
credentialData: object; | ||
} |