Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implemented did-linked resource methods and replaced truffle config with hardhat #17

Merged
merged 3 commits into from
Feb 12, 2024
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
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
node_modules
.DS_Store
build
artifacts
artifacts
cache
.env
sst-config.json
166 changes: 166 additions & 0 deletions .openzeppelin/polygon-mumbai.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
{
"manifestVersion": "3.2",
"proxies": [
{
"address": "0x59c5d68d6B45dd4bcaC643e8dc73c1f29FC0183d",
"txHash": "0x153de4adc51caceeabfc6d0a6a4b19159470d189421eefce990a512e785d70ee",
"kind": "transparent"
}
],
"impls": {
"dadc513cd81ce3e765b05219a9f956799b5405d497cd25e1342ce5b4b08463df": {
"address": "0x5DF0b04279bbA8082253466b1373243C6c82bb1f",
"txHash": "0x4afb91141018066c147d75ab814c8d676450b077fb403276ab8643c385634e39",
"layout": {
"solcVersion": "0.8.16",
"storage": [
{
"label": "totalDIDs",
"offset": 0,
"slot": "0",
"type": "t_uint256",
"contract": "PolygonDidRegistry",
"src": "contracts/PolygonDidRegistry.sol:9"
},
{
"label": "owner",
"offset": 0,
"slot": "1",
"type": "t_address",
"contract": "PolygonDidRegistry",
"src": "contracts/PolygonDidRegistry.sol:10"
},
{
"label": "polyDIDs",
"offset": 0,
"slot": "2",
"type": "t_mapping(t_address,t_struct(PolyDID)48_storage)",
"contract": "PolygonDidRegistry",
"src": "contracts/PolygonDidRegistry.sol:25"
},
{
"label": "activeDIDs",
"offset": 0,
"slot": "3",
"type": "t_mapping(t_uint256,t_address)",
"contract": "PolygonDidRegistry",
"src": "contracts/PolygonDidRegistry.sol:26"
},
{
"label": "activeAddress",
"offset": 0,
"slot": "4",
"type": "t_mapping(t_address,t_uint256)",
"contract": "PolygonDidRegistry",
"src": "contracts/PolygonDidRegistry.sol:27"
},
{
"label": "resourceData",
"offset": 0,
"slot": "5",
"type": "t_mapping(t_address,t_mapping(t_string_memory_ptr,t_string_storage))",
"contract": "PolygonDidRegistry",
"src": "contracts/PolygonDidRegistry.sol:28"
},
{
"label": "keysById",
"offset": 0,
"slot": "6",
"type": "t_mapping(t_address,t_array(t_string_storage)dyn_storage)",
"contract": "PolygonDidRegistry",
"src": "contracts/PolygonDidRegistry.sol:29"
},
{
"label": "initialized",
"offset": 0,
"slot": "7",
"type": "t_bool",
"contract": "PolygonDidRegistry",
"src": "contracts/PolygonDidRegistry.sol:35"
}
],
"types": {
"t_address": {
"label": "address",
"numberOfBytes": "20"
},
"t_array(t_string_storage)dyn_storage": {
"label": "string[]",
"numberOfBytes": "32"
},
"t_bool": {
"label": "bool",
"numberOfBytes": "1"
},
"t_mapping(t_address,t_array(t_string_storage)dyn_storage)": {
"label": "mapping(address => string[])",
"numberOfBytes": "32"
},
"t_mapping(t_address,t_mapping(t_string_memory_ptr,t_string_storage))": {
"label": "mapping(address => mapping(string => string))",
"numberOfBytes": "32"
},
"t_mapping(t_address,t_struct(PolyDID)48_storage)": {
"label": "mapping(address => struct PolygonDidRegistry.PolyDID)",
"numberOfBytes": "32"
},
"t_mapping(t_address,t_uint256)": {
"label": "mapping(address => uint256)",
"numberOfBytes": "32"
},
"t_mapping(t_string_memory_ptr,t_string_storage)": {
"label": "mapping(string => string)",
"numberOfBytes": "32"
},
"t_mapping(t_uint256,t_address)": {
"label": "mapping(uint256 => address)",
"numberOfBytes": "32"
},
"t_string_memory_ptr": {
"label": "string",
"numberOfBytes": "32"
},
"t_string_storage": {
"label": "string",
"numberOfBytes": "32"
},
"t_struct(PolyDID)48_storage": {
"label": "struct PolygonDidRegistry.PolyDID",
"members": [
{
"label": "controller",
"type": "t_address",
"offset": 0,
"slot": "0"
},
{
"label": "created",
"type": "t_uint256",
"offset": 0,
"slot": "1"
},
{
"label": "updated",
"type": "t_uint256",
"offset": 0,
"slot": "2"
},
{
"label": "didDoc",
"type": "t_string_storage",
"offset": 0,
"slot": "3"
}
],
"numberOfBytes": "128"
},
"t_uint256": {
"label": "uint256",
"numberOfBytes": "32"
}
},
"namespaces": {}
}
}
}
}
3 changes: 2 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ pnpm-lock.yaml
node_modules
.DS_Store
build
artifacts
artifacts
sst-config.json
74 changes: 47 additions & 27 deletions contracts/PolygonDidRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ pragma solidity ^0.8.16;
contract PolygonDidRegistry {
uint256 totalDIDs;
address owner;
uint256 deletedDID;
struct PolyDID {
address controller;
uint256 created;
Expand All @@ -26,10 +25,13 @@ contract PolygonDidRegistry {
mapping(address => PolyDID) polyDIDs;
mapping(uint256 => address) activeDIDs;
mapping(address => uint256) activeAddress;
mapping(address => mapping(string => string)) resourceData;
mapping(address => string[]) private keysById;
event DIDCreated(address id, string doc);
event DIDUpdated(address id, string doc);
event DIDDeleted(address id);
event TransferOwnership(address newOwner);
event ResourceAdded(address _id, string _resourceId, string _resourcePayload);

bool private initialized;

/**
Expand All @@ -41,7 +43,6 @@ contract PolygonDidRegistry {
initialized = true;
owner = msg.sender;
totalDIDs = 0;
deletedDID = 0;
}

modifier onlyOwner() {
Expand Down Expand Up @@ -112,8 +113,13 @@ contract PolygonDidRegistry {
*@param _id - Address that refers to the DID doc position
*/

function getDIDDoc(address _id) public view returns (string memory) {
return polyDIDs[_id].didDoc;
function getDIDDoc(address _id) public view returns (string memory, string[] memory) {
string[] memory result = new string[](keysById[_id].length);

for (uint256 i = 0; i < keysById[_id].length; i++) {
result[i] = resourceData[_id][keysById[_id][i]];
}
return (polyDIDs[_id].didDoc, result);
}

/**
Expand All @@ -123,30 +129,19 @@ contract PolygonDidRegistry {
function getTotalNumberOfDIDs()
public
view
returns (uint256 _totalDIDs, uint256 _activeDIDs)
returns (uint256 _totalDIDs)
{
return (totalDIDs, (totalDIDs - deletedDID));
return (totalDIDs);
}

/**
*@dev Reads total number of DIDs deleted from Chain
*/

function getTotalNumberOfDeletedDIDs()
public
view
returns (uint256 _deletedDID)
{
return deletedDID;
}

/**
*@dev Reads one DID at a time from Chain based on index
*@param _index - Uint256 type variable that refers to the DID position
*@return _did - returns the DID Doc assciated with the index. Returns null if the DID Doc is deleted.
*/

function getDIDDOcByIndex(uint256 _index)
function getDIDDocByIndex(uint256 _index)
public
view
returns (string memory)
Expand Down Expand Up @@ -182,14 +177,39 @@ contract PolygonDidRegistry {
}

/**
*@dev To delete a DID from chain
*@param _id - Address that refers to the DID doc that need to be deleted
*@dev To add linked resource in the DID doc
*@param _id - Address that refers to the DID doc
*@param _resourceId - Id that refers to the resource
*/
function addResource(address _id, string memory _resourceId, string memory _resourcePayload) public onlyController(_id) returns (address, string memory, string memory) {
resourceData[_id][_resourceId] = _resourcePayload;
keysById[_id].push(_resourceId);
emit ResourceAdded(_id, _resourceId, _resourcePayload);
return (_id, _resourceId, _resourcePayload);
}

/**
*@dev Reads DID linked resource from Chain
*@param _id - Address that refers to the DID doc
*@param _resourceId - Id that refers to a specific resource
*/

function getResource(address _id, string memory _resourceId) public view returns (string memory) {
return resourceData[_id][_resourceId];
}

/**
*@dev Reads all DID linked resource for a specific DID from Chain
*@param _id - Address that refers to the DID doc
*/

function deleteDIDDoc(address _id) public onlyController(_id) {
delete polyDIDs[_id];
delete activeDIDs[activeAddress[_id]];
++deletedDID;
emit DIDDeleted(_id);
function getAllResources(address _id) public view returns (string[] memory) {
string[] memory result = new string[](keysById[_id].length);

for (uint256 i = 0; i < keysById[_id].length; i++) {
result[i] = resourceData[_id][keysById[_id][i]];
}

return result;
}
}
}
39 changes: 39 additions & 0 deletions hardhat.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* @type import('hardhat/config').HardhatUserConfig
*/

// import 'hardhat';
require('@nomicfoundation/hardhat-ethers')
require('@openzeppelin/hardhat-upgrades')
require('@nomicfoundation/hardhat-verify')

require('dotenv/config')

module.exports = {
solidity: {
version: '0.8.16',
settings: {
optimizer: {
enabled: true,
runs: 200,
},
},
},
networks: {
hardhat: {},
mumbai: {
url: process.env.RPCURL_POLYGON_MUMBAI,
accounts: [`0x${process.env.SIGNER}`],
},
//Use for mainnet deployment
// polygon: {
// url: process.env.RPCURL_POLYON_MAINNET,
// accounts: [`0x${process.env.SIGNER}`],
// },
},
etherscan: {
apiKey: {
polygonMumbai: process.env.VERIFICATION_KEY,
},
},
}
5 changes: 0 additions & 5 deletions migrations/1_initial_migration.js

This file was deleted.

8 changes: 0 additions & 8 deletions migrations/2_deploy_contract.js

This file was deleted.

Loading
Loading