Skip to content
Merged
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
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
```


25 changes: 25 additions & 0 deletions packages/credo-module/README.md
Original file line number Diff line number Diff line change
@@ -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,
128 changes: 128 additions & 0 deletions packages/did-registrar/README.md
Original file line number Diff line number Diff line change
@@ -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.
93 changes: 93 additions & 0 deletions packages/did-registry-contract/README.md
Original file line number Diff line number Diff line change
@@ -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 = `<Contract Address>`;
const provider = new ethers.providers.JsonRpcProvider(url);

let wallet = new ethers.Wallet(`<Signer Key/Private Key>`, provider);
let registry = new ethers.Contract(DID_ADDRESS, <Contract ABI>, 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="<Place your Mainnet RPC URL here>"
AMOY_RPCURL="<Place your Amoy RPC URL here>"
SIGNER_TESTNET="<Place your Testnet Signer Key here>"
SIGNER_MAINNET="<Place your Mainnet Signer Key here>"
```

On a new console window run

```
npx hardhat run deploy --network <network name>
```

## Testing

For Testing use the command

```
pnpm test
```
28 changes: 28 additions & 0 deletions packages/did-resolver/REAMDE.md
Original file line number Diff line number Diff line change
@@ -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
```
Loading
Loading