diff --git a/README.md b/README.md new file mode 100644 index 0000000..ed44aac --- /dev/null +++ b/README.md @@ -0,0 +1,45 @@ +# Polygon DID Modules + +[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) +[![Node.js](https://img.shields.io/badge/Node.js-22%2B-green.svg)](https://nodejs.org/) +[![TypeScript](https://img.shields.io/badge/TypeScript-Ready-blue.svg)](https://www.typescriptlang.org/) + +A comprehensive suite of modules for implementing Polygon-based Decentralized Identifiers (DIDs) with W3C compliance, enabling secure identity management on the Polygon blockchain network. + + +## Packages + +This monorepo contains the following packages: + +| Package | Description | Version | +|---------|-------------|---------| +| `credo-module` | Credo framework integration module | ![npm](https://img.shields.io/npm/v/@ayanworks/credo-polygon-w3c-module) | +| `did-resolver` | Polygon DID resolver implementation | ![npm](https://img.shields.io/npm/v/@ayanworks/polygon-did-resolver) | +| `did-registrar` | DID registration and management | ![npm](https://img.shields.io/npm/v/@ayanworks/polygon-did-registrar) | +| `schema-manager` | W3C schema management utilities | ![npm](https://img.shields.io/npm/v/@ayanworks/polygon-schema-manager) | +| `did-registry-contract` | Smart contract for DID registry | ![npm](https://img.shields.io/npm/v/@ayanworks/polygon-did-registry-contract) | + + +### Setup + +```bash +# Clone the repository +git clone https://github.com/ayanworks/polygon-did-modules.git + +# Install dependencies +pnpm install + +# Build all packages +pnpm build + +# Run tests +pnpm test + +# Type checking +pnpm types:check + +# Code formatting +pnpm style:fix +``` + + diff --git a/packages/credo-module/README.md b/packages/credo-module/README.md new file mode 100644 index 0000000..3ebc6f3 --- /dev/null +++ b/packages/credo-module/README.md @@ -0,0 +1,25 @@ +# Credo did:polygon W3C Module + +- W3C did:polygon method registry for [credo-ts](https://github.com/openwallet-foundation/credo-ts). + +## Usage + +```ts +import { PolygonDidResolver, PolygonDidRegistrar, PolygonModule } from 'afj-polygon-w3c-module' + +const agent = new Agent({ + config: { + /* agent config */ + }, + dependencies, + modules: { + /* ... */ + dids: new DidsModule({ + resolvers: [ /* ... */, new PolygonDidResolver()], + registrars: [ /* ... */, new PolygonDidRegistrar()], + }), + /* ... */ + polygon: new PolygonModule({ + rpcUrl: 'rpcUrl' // polygon rpc url, + didContractAddress: 'didContractAddress' // polygon did contract address, + fileServerToken: 'fileServerToken' // polygon file server token to store schema json, \ No newline at end of file diff --git a/packages/did-registrar/README.md b/packages/did-registrar/README.md new file mode 100644 index 0000000..7ff7893 --- /dev/null +++ b/packages/did-registrar/README.md @@ -0,0 +1,128 @@ +# Polygon DID Method + +The polygon DID method library uses Ethereum based addresses as fully functional DID’s or Decentralized identifiers, on the Polygon network. The following allows one to create a key Pair based and facilitates its storage on the registry smart contract, deployed on Polygon chain. +Third party users can use this to create polygon DID identities. It allows the controller to perform actions like resolve, update and delete by encapsulating polygonDID registry and PolygonDID resolver. +The DID identifier allows the controller to resolve DID document for usage in different scenarios. + +### Example of polygon DID document resolved using PolygonDIDResolver: + +```json +{ + "@context": "https://w3id.org/did/v1", + "id": "did:polygon:0x794b781493AeD65b9ceBD680716fec257e118993", + "verificationMethod": [ + { + "id": "did:polygon:0x794b781493AeD65b9ceBD680716fec257e118993", + "type": "EcdsaSecp256k1VerificationKey2019", + "controller": ["did:polygon:0x794b781493AeD65b9ceBD680716fec257e118993"], + "publicKeyBase58": "7Lnm1ZnseKDkH1baAb1opREfAU4MPY7zCdUDSrWSm9NxNTQmy4neU9brFUYnEcyy7CwFKjD11ikyP9J8cf6zEaAKrEzzp" + } + ] +} +``` + +# DID Method or DID schema + +The DID method is a specific implementation of a DID scheme that will be identified by method name. For this case the method name is “polygon”, and the identifier is an Ethereum address. + +## The DID for Polygon looks like: + +### On Polygon mainnet + +``` +did:polygon:0xdce5306fb5f9ba6797546dcd2e11eb5c5201bfeb +``` + +### On Polygon testnet + +``` +did:polygon:testnet:0xdce5306fb5f9ba6797546dcd2e11eb5c5201bfeb +``` + +## DID On-Chain + +Every DID on chain has the same structure, defined as: + +Where, + +- controller : the address of the person who creates and manages the DID +- created : holds the timestamp of the block when DID was created +- updated : initially holds the timestamp of when the DID was created, but is updated if the controller updates the DID on chain, and +- doc : holds the entire DID document in form of string. + +# DID Operations + +## Create + +Creating a createKeyPair refers to generation of a DID uri, based on a newly generated wallet. + +```js +import { createKeyPair } from 'polygon-did-registrar' +const keys = await createKeyPair(network) +``` + +The function returns address, privateKey, publicKeyBase58, did + +## Register + +Register of DID is done by logging the transaction on the polygon-register smart contract, by invoking + +```js +import { create } from 'polygon-did-registrar' +const txHash = await create(did, didDoc) +``` + +The function returns a txnHash and DID and didDoc on successful execution. + +## Update + +The DID controller requests for the update functionality, if the controller wishes to edit the did doc store on the ledger using : + +```js +import { update } from 'polygon-did-registrar' +const txHash = await update(did, didDoc) +``` + +## Add Resource + +Add DID-linked resource for the DID-Doc. + +```js +import { addResource } from 'polygon-did-registrar' +const txHash = await addResource(did, resourcePayload) +``` + +The function returns a txhash, DID, and resourceId on successful execution. + +## Update Resource + +Update DID-linked resource for the DID-Doc. + +```js +import { updateResource } from 'polygon-did-registrar' +const txHash = await updateResource(did, resourceId, resourcePayload) +``` + +The function returns a txhash, DID, and resourceId on successful execution. + +## Fetch Resource + +Get a DID-linked resource for a specific DID. + +```js +import { getResourceByDidAndResourceId } from 'polygon-did-registrar' +const txHash = await getResourceByDidAndResourceId(did, resourceId) +``` + +The function returns DID-linked resource and DID uri on successful execution. + +## Fetch all Resources + +Get all DID-linked resources for a specific DID. + +```js +import { getResourcesByDid } from 'polygon-did-registrar' +const txHash = await getResourcesByDid(did) +``` + +The function returns the list of DID-linked resources and DID on successful execution. \ No newline at end of file diff --git a/packages/did-registry-contract/README.md b/packages/did-registry-contract/README.md new file mode 100644 index 0000000..449d187 --- /dev/null +++ b/packages/did-registry-contract/README.md @@ -0,0 +1,93 @@ +# Polygon DID Registry Contract + +This library is an implementation of a registry contract that supports the Polygon DID Method. + +## Overview + +The Polygon registry contract acts as a public ledger, where the Polygon-Identity specified Decentralised Identifiers will be logged. The specifications related to polygon DID method are mentioned in the document. A DID generated using the Polygon DID generator, can be stored and managed on the ledger using this contract library. + +## Contract Deployment + +| Network | ChainId | Registry Address | +| :--------------------: | :-----: | :----------------------------------------: | +| Polygon Mainnet | 137 | 0x0C16958c4246271622201101C83B9F0Fc7180d15 | +| Polygon Testnet (amoy) | 80002 | 0xcB80F37eDD2bE3570c6C9D5B0888614E04E1e49E | + +## Methods + +- `createDID(address, string)` : The method createDID is used to create and log a new DID on the polygon chain. The parameter of address type, will act as the reference key, to refer the did document stored on the chain. The string type variable will contain the did document, that will be stored on the matic chain. + +- `updateDIDDoc(address, string)` : The method updateDID is included in contract, which will facilitate the controller, and only the controller of the did, to update the document if need arises. Though the Polygon DID method, defines how the DID doc is defined as per standards, and that can be resolved. + +- `getDIDDoc(address)` : The method getDID helps to resolve the DID document. + +- `transferOwnership(address)` : The method transferOwnership, helps in transferring the ownership of contract to a new owner. Only the current owner can access this function. + +- `getOwner()` : the method getOwner helps one to fetch the current owner of the contract. + +- `addResource(address, string, string)` : The addResource method allows the controller of a DID to add a linked resource to the DID document. This method ensures that only authorized controllers can add resources by requiring the caller to be the controller of the DID. + +- `getResource(address, string)` : The getResource method fetches a specific linked resource from the blockchain that is associated with a given DID document. + +- `getAllResources(address)` : The getAllResources method retrieves all resources linked to a specific DID document. This provides a comprehensive list of all resources associated with a given DID. + +## Example ethers code + +Using ethers, the following illustrates how one can interact with PolygonRegistry contract, from client side application. + +## Loading the Contract + +``` +const ethers = require('ethers'); +const url = https://rpc-amoy.polygon.technology; // For amoy testnet +const DID_ADDRESS = ``; +const provider = new ethers.providers.JsonRpcProvider(url); + +let wallet = new ethers.Wallet(``, provider); +let registry = new ethers.Contract(DID_ADDRESS, , wallet); +``` + +# Deploying the Contract on Matic network + +Pre-requisites + +- NodeJS - https://nodejs.org/en/download/ +- Hardhat - https://hardhat.org +- A wallet connected to polygon network, with Matic token in it. One can receive the Matic Test Tokens from their faucet. + +## Deployment + +Clone the repository + +``` +git clone https://github.com/ayanworks/polygon-did-registry-contract.git +``` + +Install Dependencies + +``` +pnpm i +``` + +Update your and RPC URL in .env file. + +``` +MAINNET_RPCURL="" +AMOY_RPCURL="" +SIGNER_TESTNET="" +SIGNER_MAINNET="" +``` + +On a new console window run + +``` +npx hardhat run deploy --network +``` + +## Testing + +For Testing use the command + +``` +pnpm test +``` diff --git a/packages/did-resolver/REAMDE.md b/packages/did-resolver/REAMDE.md new file mode 100644 index 0000000..7553097 --- /dev/null +++ b/packages/did-resolver/REAMDE.md @@ -0,0 +1,28 @@ +# Polygon DID Resolver + +The polygon resolver library is used for resolving DID’s in Polygon Method Space. The module is supposed to be used as an integration to polygon library. + +## Install + +``` +pnpm install +``` + +## Usage + +In combination with the DID-Resolver: + +```js +import { resolveDID } from 'polygon-did-resolver' +const didDocument = await resolveDID(did) +``` + +The function returns a DID Document. + +## Testing + +For testing use the command + +``` +pnpm run test +``` diff --git a/packages/schema-manager/README.md b/packages/schema-manager/README.md new file mode 100644 index 0000000..cecdc19 --- /dev/null +++ b/packages/schema-manager/README.md @@ -0,0 +1,85 @@ +# Polygon schema registrar + +This GitHub repository is dedicated to creating W3C-compliant schemas for JSON-LD credentials, facilitating interoperability and standardization in digital credentialing. +Methods + +## Contract Deployment + +| Network | ChainId | Contract Address | +| :--------------------: | :-----: | :----------------------------------------: | +| Polygon Mainnet | 137 | 0x4B16719E73949a62E9A7306F352ec73F1B143c27 | +| Polygon Testnet (amoy) | 80002 | 0x4742d43C2dFCa5a1d4238240Afa8547Daf87Ee7a | + +### Example of Polygon JSON-LD Schema: + +```json +{ + "resourceURI": "did:polygon:testnet:0x13cd23928Ae515b86592C630f56C138aE4c7B79a/resources/398cee0a-efac-4643-9f4c-74c48c72a14b", + "resourceCollectionId": "55dbc8bf-fba3-4117-855c-1e0dc1d3bb47", + "resourceId": "398cee0a-efac-4643-9f4c-74c48c72a14b", + "resourceName": "PANCARD", + "resourceType": "W3C-schema", + "mediaType": "txt", + "created": "2022-11-17T08:10:36Z", + "checksum": "a95380f460e63ad939541a57aecbfd795fcd37c6d78ee86c885340e33a91b559", + "previousVersionId": null, + "nextVersionId": null +} +``` + +# Schema Operations + +## Create Schema + +Create a new JSON-LD credential schema. This method allows users to define the structure and properties of the credential schema. + +```js +import { createSchema } from 'polygon-schema-manager' +const txDetails = await createSchema(did, schemaName, schema) +``` + +The function returns, did, schemaId,and txnReceipt. + +## Get Schema by ID + +Retrieves schema details by its unique ID. + +```js +import { getSchemaById } from 'polygon-schema-manager' +const schemaDetail = await getSchemaById(did, schemaId) +``` + +The function returns Schema details including resourceURI, resourceCollectionId, etc.. + +## Get All Schemas by DID + +Retrieves all schemas associated with a specific DID. + +```js +import { getSchemaById } from 'polygon-schema-manager' +const schemaDetails = await getSchemaById(did) +``` + +The function returns Array of schema objects with essential keys.. + +## Estimate Transaction + +Estimates transaction fees for schema-related transactions. + +```js +import { estimateTxFee } from 'polygon-schema-manager' +const transactionDetails = await getSchemaById(did) +``` + +The function returns transaction details including transactionFee, gasLimit, etc. + +## Validate Schema Object + +Validates the JSON schema object to ensure its correctness. + +```js +import { estimateTxFee } from 'polygon-schema-manager' +const transactionDetails = await getSchemaById(did) +``` + +The function returns boolean indicating whether the schema is valid. \ No newline at end of file