From 21a7bd8e9ac2b0e7c974c105dc8175a6d1a3a6db Mon Sep 17 00:00:00 2001 From: tipusinghaw Date: Mon, 12 Feb 2024 15:00:13 +0530 Subject: [PATCH] feat: migration for truffle to hardhat Signed-off-by: tipusinghaw --- .gitignore | 5 +- .openzeppelin/polygon-mumbai.json | 166 ++++++++++ .prettierignore | 3 +- contracts/PolygonDidRegistry.sol | 74 +++-- hardhat.config.js | 39 +++ migrations/1_initial_migration.js | 5 - migrations/2_deploy_contract.js | 8 - package.json | 17 +- scripts/deploy.js | 31 ++ sst-config.json | 4 +- tests/polygon_did_registry_test.js | 393 ------------------------ tests/polygon_did_registry_test.test.js | 117 +++++++ truffle-config.js | 68 ---- 13 files changed, 416 insertions(+), 514 deletions(-) create mode 100644 .openzeppelin/polygon-mumbai.json create mode 100644 hardhat.config.js delete mode 100644 migrations/1_initial_migration.js delete mode 100644 migrations/2_deploy_contract.js create mode 100644 scripts/deploy.js delete mode 100644 tests/polygon_did_registry_test.js create mode 100644 tests/polygon_did_registry_test.test.js delete mode 100644 truffle-config.js diff --git a/.gitignore b/.gitignore index 81d59d4..ff6d117 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,7 @@ node_modules .DS_Store build -artifacts \ No newline at end of file +artifacts +cache +.env +sst-config.json \ No newline at end of file diff --git a/.openzeppelin/polygon-mumbai.json b/.openzeppelin/polygon-mumbai.json new file mode 100644 index 0000000..836df30 --- /dev/null +++ b/.openzeppelin/polygon-mumbai.json @@ -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": {} + } + } + } +} diff --git a/.prettierignore b/.prettierignore index c1cfa65..27db58f 100644 --- a/.prettierignore +++ b/.prettierignore @@ -2,4 +2,5 @@ pnpm-lock.yaml node_modules .DS_Store build -artifacts \ No newline at end of file +artifacts +sst-config.json \ No newline at end of file diff --git a/contracts/PolygonDidRegistry.sol b/contracts/PolygonDidRegistry.sol index 222383b..4773e33 100644 --- a/contracts/PolygonDidRegistry.sol +++ b/contracts/PolygonDidRegistry.sol @@ -8,7 +8,6 @@ pragma solidity ^0.8.16; contract PolygonDidRegistry { uint256 totalDIDs; address owner; - uint256 deletedDID; struct PolyDID { address controller; uint256 created; @@ -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; /** @@ -41,7 +43,6 @@ contract PolygonDidRegistry { initialized = true; owner = msg.sender; totalDIDs = 0; - deletedDID = 0; } modifier onlyOwner() { @@ -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); } /** @@ -123,22 +129,11 @@ 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 @@ -146,7 +141,7 @@ contract PolygonDidRegistry { *@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) @@ -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; } -} +} \ No newline at end of file diff --git a/hardhat.config.js b/hardhat.config.js new file mode 100644 index 0000000..e17e2d7 --- /dev/null +++ b/hardhat.config.js @@ -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, + }, + }, +} \ No newline at end of file diff --git a/migrations/1_initial_migration.js b/migrations/1_initial_migration.js deleted file mode 100644 index 4146b01..0000000 --- a/migrations/1_initial_migration.js +++ /dev/null @@ -1,5 +0,0 @@ -const Migrations = artifacts.require('Migrations') - -module.exports = function (deployer) { - deployer.deploy(Migrations) -} diff --git a/migrations/2_deploy_contract.js b/migrations/2_deploy_contract.js deleted file mode 100644 index d987b2b..0000000 --- a/migrations/2_deploy_contract.js +++ /dev/null @@ -1,8 +0,0 @@ -const { deployProxy } = require('@openzeppelin/truffle-upgrades') - -const PolygonDidRegistry = artifacts.require('PolygonDidRegistry') - -module.exports = async function (deployer) { - const instance = await deployProxy(PolygonDidRegistry, { deployer }) - console.log('Deployed', instance.address) -} diff --git a/package.json b/package.json index f69bf1b..65dc67f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@ayanworks/polygon-did-registry-contract", - "version": "2.0.1-alpha.2", + "version": "2.0.1-alpha.3", "description": "Smart Contract to register DID on Polygon", "main": "build/index.js", "directories": { @@ -8,7 +8,7 @@ }, "scripts": { "deploy": "node deploy.js", - "test": "node compile.js && mocha --timeout 30000", + "test": "node --require ts-node/register --test ./tests/polygon_did_registry_test.test.js", "clean": "rm -rf build", "clean:deps": "pnpm clean && rm -rf node_modules", "build": "pnpm clean && pnpm build:sol && tsc", @@ -21,20 +21,20 @@ }, "license": "MIT", "devDependencies": { - "@openzeppelin/truffle-upgrades": "^1.15.0", - "@truffle/hdwallet-provider": "^2.0.10", + "@nomicfoundation/hardhat-ethers": "^3.0.5", + "@nomicfoundation/hardhat-verify": "^2.0.3", + "@openzeppelin/hardhat-upgrades": "^3.0.1", "@types/fs-extra": "^11.0.1", "@types/node": "^18.17.0", "dotenv": "^16.0.2", "eslint": "^8.23.1", "ethers": "^6.9.0", "fs-extra": "^11.1.1", - "ganache-cli": "6.12.2", + "hardhat": "^2.19.4", "mocha": "^7.0.0", "prettier": "^3.0.2", "release-it": "^16.3.0", "solc": "^0.8.21", - "truffle": "^5.5.20", "ts-node": "^10.9.1", "typescript": "^5.1.6" }, @@ -52,5 +52,6 @@ "bugs": { "url": "https://github.com/ayanworks/polygon-did-registry-contract/issues" }, - "homepage": "https://github.com/ayanworks/polygon-did-registry-contract#readme" -} + "homepage": "https://github.com/ayanworks/polygon-did-registry-contract#readme", + "dependencies": {} +} \ No newline at end of file diff --git a/scripts/deploy.js b/scripts/deploy.js new file mode 100644 index 0000000..741fead --- /dev/null +++ b/scripts/deploy.js @@ -0,0 +1,31 @@ +const { ethers, upgrades } = require('hardhat') + +async function main() { + try { + console.log('Deploying the smart contract...') + const PolygonDidRegistry = + await ethers.getContractFactory('PolygonDidRegistry') + + // To deploy the initial upgradable smart contract + + const contract = await upgrades.deployProxy(PolygonDidRegistry, { + initializer: 'initialize', + }) + + // const contract = await upgrades.upgradeProxy( + // process.env.CONTRACT_ADDRESS, + // PolygonDidRegistry, + // { + // initializer: 'initialize', + // }, + // ) + + await contract.waitForDeployment() + + console.log('Contract address::', contract.target) + } catch (error) { + console.error('Error deploying contract:', error.message) + } +} + +main() \ No newline at end of file diff --git a/sst-config.json b/sst-config.json index d70487a..7ed24e2 100644 --- a/sst-config.json +++ b/sst-config.json @@ -1,3 +1 @@ -{ - "sourceHash": "0xd34bb18b8a1a061e975b0850a1887bacdf0b8891d51e51399492bb038c05ff96" -} +{"sourceHash":"0xb57d8b09722fddffbc5855f1dfba236305522e3e11aa91722f77789c16a9629b"} \ No newline at end of file diff --git a/tests/polygon_did_registry_test.js b/tests/polygon_did_registry_test.js deleted file mode 100644 index f52f5c5..0000000 --- a/tests/polygon_did_registry_test.js +++ /dev/null @@ -1,393 +0,0 @@ -const ethers = require('ethers') -const ganache = require('ganache-cli') -const assert = require('assert') -const { deployProxy } = require('@openzeppelin/truffle-upgrades') -const PolygonDidRegistry = artifacts.require('PolygonDidRegistry') - -// Library initialization that will talk with Blockchain -const provider = new ethers.providers.Web3Provider( - ganache.provider({ gasLimit: 8000000 }), -) -// Initializing the build files -const PolygonDIDRegistryJSON = require('../build/contracts/PolygonDidRegistry.json') - -let accounts, - PolygonDIDRegistryInstance, - PolygonDIDRegistryInstance_test, - PolygonUpgradableInstance, - PolygonUpgradableInstance_test - -// Describing the 1st Test -// This will setup Ganache with some test address -describe('Ganache Setup', async () => { - it('Ganache setup with Test Accounts', async () => { - // List of accounts are fetched here - accounts = await provider.listAccounts() - - // We will check that atleast 2 accounts are loaded - assert.ok(accounts.length >= 1, '2 Accounts are present minimum') - }) -}) - -// Upgradable with Proxy -// Deployment Testing 1.0 -describe('Setting up the Upgradable Smart Contract', async () => { - describe('Setting up the contract', async () => { - it('Should deploy contract with Proxy, with no parameters/Constructor', async () => { - PolygonUpgradableInstance = await deployProxy(PolygonDidRegistry) - PolygonUpgradableInstance_test = new ethers.Contract( - PolygonUpgradableInstance.address, - PolygonDIDRegistryJSON.abi, - provider.getSigner(accounts[3]), - ) - assert.ok( - PolygonUpgradableInstance.address, - 'Upgradeable contract is deployed', - ) - }) - }) - - //Check Create Function test 1.1 - describe('Checking the Create function using Upgradeable Contract', async () => { - it('This should store the DID doc on Upgradable Contract and will read and check if the value stored is correct', async () => { - // This will send the value to local ganache blockchain - const did = '0x2f65b747440deaf596892dfc7965040be8b99100' - const doc = - '{"@context":"https://w3id.org/did/v1","id":"did:polygon:0x2f65b747440deaf596892dfc7965040be8b99100","verificationMethod":[{"id":"did:polygon:0x2f65b747440deaf596892dfc7965040be8b99100","type":"EcdsaSecp256k1VerificationKey2019","controller":"did:polygon:0x2f65b747440deaf596892dfc7965040be8b99100","publicKeyBase58":"NDaEdTguJV39Ns8BZkxQ3XR6GUinZAJfVoyEMkK9fP7XQmpSkT3UsLHB52cFpDqoM6m4Hevtba8pkmjvEG3Ur7ji"}]}' - const tx = await PolygonUpgradableInstance.createDID(did, doc) - - // Getting the set value in ganache blockchain - const currentValue = await PolygonUpgradableInstance.getDID(did) - - // Comparing the set value to confirm - assert.equal(currentValue, doc, 'value set must be able to get') - - assert.notEqual(currentValue, null, 'value should not be empty') - }) - }) - - //Describing the 1.2 Test Case - describe('Checking the getTotalNumberOfDIDs Function', async () => { - it('This should get correct number of DIDs registered', async () => { - const currentValue = - await PolygonUpgradableInstance.getTotalNumberOfDIDs() - assert.equal(currentValue, 1, 'value should be matching') - }) - }) - - // Describing the 1.3 Test case - describe('Checking the Update function in Upgradable Contract', async () => { - it('This should update the DID doc on Upgradable Contract and will read and check if the value stored is correct', async () => { - // This will send the value to local ganache blockchain - const did = '0x2f65b747440deaf596892dfc7965040be8b99100' - //changed the controller - const doc = - '{"@context":"https://w3id.org/did/v1","id":"did:polygon:0x2f65b747440deaf596892dfc7965040be8b99100","verificationMethod":[{"id":"did:polygon:0x2f65b747440deaf596892dfc7965040be8b99100","type":"EcdsaSecp256k1VerificationKey2019","controller":"did:polygon:0x2f65b747440deaf596892dfc7965040be8c66666","publicKeyBase58":"NDaEdTguJV39Ns8BZkxQ3XR6GUinZAJfVoyEMkK9fP7XQmpSkT3UsLHB52cFpDqoM6m4Hevtba8pkmjvEG3Ur7ji"}]}' - const tx = await PolygonUpgradableInstance.updateDID(did, doc) - - // Getting the set value in ganache blockchain - const currentValue = await PolygonUpgradableInstance.getDID(did) - - // Comparing the set value to confirm - assert.equal(currentValue, doc, 'value set must be able to get') - }) - }) - - //Describing test 1.4 to check if test fails if we update with different controller - describe('Checking update with wrong controller', async () => { - it('This should not update DID doc ', async () => { - const did = '0x2f65b747440deaf596892dfc7965040be8b99100' - const doc = - '{"@context":"https://w3id.org/did/v1","id":"did:polygon:0x2f65b747440deaf596892dfc7965040be8b99100","verificationMethod":[{"id":"did:polygon:0x2f65b747440deaf596892dfc7965040be8b99100","type":"EcdsaSecp256k1VerificationKey2019","controller":"did:polygon:0x2f65b747440deaf596892dfc7965040be8c66666","publicKeyBase58":"NDaEdTguJV39Ns8BZkxQ3XR6GUinZAJfVoyEMkK9fP7XQmpSkT3UsLHB52cFpDqoM6m4Hevtba8pkmjvEG3Ur7ji"}]}' - try { - assert.ifError( - //sending transaction using different controller - await PolygonUpgradableInstance_test.functions.updateDID(did, doc), - ) - } catch (error) {} - }) - }) - - // Describing the 1.5 Test case - describe('Checking the Delete function in Upgradable Contract', async () => { - it('This should delete the DID doc on Upgradable Contract and will read and check if the value is now an empty string', async () => { - // This will send the value to local ganache blockchain - const did = '0x2f65b747440deaf596892dfc7965040be8b99100' - const tx = await PolygonUpgradableInstance.deleteDID(did) - - // Getting the set value in ganache blockchain - const currentValue = await PolygonUpgradableInstance.getDID(did) - - // Comparing the set value to confirm - assert.equal(currentValue, '', 'value should be matching') - }) - }) - - // Describing the 1.6 Test case - describe('Checking the Create function in Upgradable Contract', async () => { - it('This should store the DID doc on Upgradable Contract and will read and check if the value stored is not null', async () => { - // This will send the value to local ganache blockchain - const did = '0x54ec7be8b24f7139f870f277cf99c527ff43892b' - const doc = - '{"@context":"https://w3id.org/did/v1","id":"did:polygon:0x54ec7be8b24f7139f870f277cf99c527ff43892b","verificationMethod":[{"id":"did:polygon:0x54ec7be8b24f7139f870f277cf99c527ff43892b","type":"EcdsaSecp256k1VerificationKey2019","controller":"did:polygon:0x54ec7be8b24f7139f870f277cf99c527ff43892b","publicKeyBase58":"RwHysxjkzFdJDqi2irHRVRFHX9uUj5aVAXVoufnk5PDA4qcn1ejJMgGhqCsFdQqXRCTdi4TbEQFJjDUAdi2JvYmH"}]}' - const doctest = - '{"@context":"https://w3id.org/did/v1","id":"did:polygon:0x2f65b747440deaf596892dfc7965040be8b99100","verificationMethod":[{"id":"did:polygon:0x2f65b747440deaf596892dfc7965040be8b99100","type":"EcdsaSecp256k1VerificationKey2019","controller":"did:polygon:0x2f65b747440deaf596892dfc7965040be8c66666","publicKeyBase58":"NDaEdTguJV39Ns8BZkxQ3XR6GUinZAJfVoyEMkK9fP7XQmpSkT3UsLHB52cFpDqoM6m4Hevtba8pkmjvEG3Ur7ji"}]}' - const tx = await PolygonUpgradableInstance.createDID(did, doc) - - // Getting the set value in ganache blockchain - const currentValue = await PolygonUpgradableInstance.getDID(did) - - // Checking value is not null - - assert.notEqual(currentValue, '', 'value should not match') - }) - }) - - // Describing the 1.7 Test case - describe('Checking the Update function in Upgradable Contract', async () => { - it('This should update the DID doc on Upgradable Contract and will read and check if the value stored has changed', async () => { - // This will send the value to local ganache blockchain - const did = '0x54ec7be8b24f7139f870f277cf99c527ff43892b' - //changed the controller - const doc = - '{"@context":"https://w3id.org/did/v1","id":"did:polygon:0x2f65b747440deaf596892dfc7965040be8b99100","verificationMethod":[{"id":"did:polygon:0x2f65b747440deaf596892dfc7965040be8b99100","type":"EcdsaSecp256k1VerificationKey2019","controller":"did:polygon:0x2f65b747440deaf596892dfc7965040be8c66666","publicKeyBase58":"NDaEdTguJV39Ns8BZkxQ3XR6GUinZAJfVoyEMkK9fP7XQmpSkT3UsLHB52cFpDqoM6m4Hevtba8pkmjvEG3Ur7ji"}]}' - const docoriginal = - '{"@context":"https://w3id.org/did/v1","id":"did:polygon:0x54ec7be8b24f7139f870f277cf99c527ff43892b","verificationMethod":[{"id":"did:polygon:0x54ec7be8b24f7139f870f277cf99c527ff43892b","type":"EcdsaSecp256k1VerificationKey2019","controller":"did:polygon:0x54ec7be8b24f7139f870f277cf99c527ff43892b","publicKeyBase58":"RwHysxjkzFdJDqi2irHRVRFHX9uUj5aVAXVoufnk5PDA4qcn1ejJMgGhqCsFdQqXRCTdi4TbEQFJjDUAdi2JvYmH"}]}' - const tx = await PolygonUpgradableInstance.updateDID(did, doc) - - // Getting the set value in ganache blockchain - const currentValue = await PolygonUpgradableInstance.getDID(did) - - // Comparing the set value to confirm - assert.notEqual(currentValue, docoriginal, 'value should not match') - }) - }) - - // Describing the 1.8 Test Case - describe('Checking the Delete function in Upgradable Contract', async () => { - it('This should delete the DID doc on Upgradable Contract and will read and check if the value is now not equal to the updated doc', async () => { - // This will send the value to local ganache blockchain - const did = '0x54ec7be8b24f7139f870f277cf99c527ff43892b' - const doc = - '{"@context":"https://w3id.org/did/v1","id":"did:polygon:0x2f65b747440deaf596892dfc7965040be8b99100","verificationMethod":[{"id":"did:polygon:0x2f65b747440deaf596892dfc7965040be8b99100","type":"EcdsaSecp256k1VerificationKey2019","controller":"did:polygon:0x2f65b747440deaf596892dfc7965040be8c66666","publicKeyBase58":"NDaEdTguJV39Ns8BZkxQ3XR6GUinZAJfVoyEMkK9fP7XQmpSkT3UsLHB52cFpDqoM6m4Hevtba8pkmjvEG3Ur7ji"}]}' - const tx = await PolygonUpgradableInstance.deleteDID(did) - - // Getting the set value in ganache blockchain - const currentValue = await PolygonUpgradableInstance.getDID(did) - - // Comparing the set value to confirm - assert.notEqual(currentValue, doc, 'value should not be matching') - }) - }) - - //Describing the 1.9 Test Case - describe('Checking the getTotalNumberOfDIDs Function', async () => { - it('This should get correct number of DIDs registered', async () => { - const totalValue = await PolygonUpgradableInstance.getTotalNumberOfDIDs() - const deltedValue = - await PolygonUpgradableInstance.getTotalNumberOfDeletedDIDs() - const currentValue = totalValue - deltedValue - assert.equal(currentValue, 0, 'value should be matching') - }) - }) - - //Describing the 1.10 Test Case - describe('Checking the Transafer Ownership Function', async () => { - it('This should transfer ownership of contract to another user', async () => { - const newOwner = accounts[5] - const owner = await PolygonUpgradableInstance.getOwner() - const tx = await PolygonUpgradableInstance.transferOwnership(newOwner) - const currentOwner = await PolygonUpgradableInstance.getOwner() - - assert.notEqual(owner, currentOwner, 'value should not be equal') - }) - }) - - //Describing the 1.11 Test Case - describe('Checking the Transafer Ownership Function', async () => { - it('This should not transfer ownership of contract to another user since the transaction is being initiated by different owner', async () => { - const newOwner = accounts[6] - try { - assert.ifError( - //sending transaction using different controller - await PolygonUpgradableInstance_test.functions.transferOwnership( - newOwner, - ), - ) - } catch (error) {} - }) - }) -}) - -// basic Contract Testing -// Deployment Testing 2.0 -describe('Basic Contract Testing', async () => { - // Describing a SubTest case - describe('Setting up the contract', async () => { - it('This Deploys Polygon Smart contract from 1st account with no Parameters/Constructor', async () => { - // Creating a contract factory for deploying contract. - const PolygonDIDRegistry = new ethers.ContractFactory( - PolygonDIDRegistryJSON.abi, - PolygonDIDRegistryJSON.bytecode, - provider.getSigner(accounts[2]), - ) - PolygonDIDRegistryInstance = await PolygonDIDRegistry.deploy() - PolygonDIDRegistryInstance_test = new ethers.Contract( - PolygonDIDRegistryInstance.address, - PolygonDIDRegistryJSON.abi, - provider.getSigner(accounts[3]), - ) - assert.ok( - PolygonDIDRegistryInstance.address, - 'conract address should be present', - ) - }) - }) - - // Describing the 2.1 Test case - describe('Checking the Create function in Contract', async () => { - it('This should store the DID doc on Contract and will read and check if the value stored is correct', async () => { - // This will send the value to local ganache blockchain - const did = '0x2f65b747440deaf596892dfc7965040be8b99100' - const doc = - '{"@context":"https://w3id.org/did/v1","id":"did:polygon:0x2f65b747440deaf596892dfc7965040be8b99100","verificationMethod":[{"id":"did:polygon:0x2f65b747440deaf596892dfc7965040be8b99100","type":"EcdsaSecp256k1VerificationKey2019","controller":"did:polygon:0x2f65b747440deaf596892dfc7965040be8b99100","publicKeyBase58":"NDaEdTguJV39Ns8BZkxQ3XR6GUinZAJfVoyEMkK9fP7XQmpSkT3UsLHB52cFpDqoM6m4Hevtba8pkmjvEG3Ur7ji"}]}' - const tx = await PolygonDIDRegistryInstance.functions.createDID(did, doc) - - // Transfer confirmation - await tx.wait() - - // Getting the set value in ganache blockchain - const currentValue = - await PolygonDIDRegistryInstance.functions.getDID(did) - - // Comparing the set value to confirm - assert.equal(currentValue, doc, 'value set must be able to get') - - assert.notEqual(currentValue, null, 'value should not be empty') - }) - }) - - // Describing the 2.2 Test case - describe('Checking the Update function in Contract', async () => { - it('This should update the DID doc on Contract and will read and check if the value stored is correct', async () => { - // This will send the value to local ganache blockchain - const did = '0x2f65b747440deaf596892dfc7965040be8b99100' - //changed the controller - const doc = - '{"@context":"https://w3id.org/did/v1","id":"did:polygon:0x2f65b747440deaf596892dfc7965040be8b99100","verificationMethod":[{"id":"did:polygon:0x2f65b747440deaf596892dfc7965040be8b99100","type":"EcdsaSecp256k1VerificationKey2019","controller":"did:polygon:0x2f65b747440deaf596892dfc7965040be8c66666","publicKeyBase58":"NDaEdTguJV39Ns8BZkxQ3XR6GUinZAJfVoyEMkK9fP7XQmpSkT3UsLHB52cFpDqoM6m4Hevtba8pkmjvEG3Ur7ji"}]}' - const tx = await PolygonDIDRegistryInstance.functions.updateDID(did, doc) - - // Transfer confirmation - await tx.wait() - - // Getting the set value in ganache blockchain - const currentValue = - await PolygonDIDRegistryInstance.functions.getDID(did) - - // Comparing the set value to confirm - assert.equal(currentValue, doc, 'value set must be able to get') - }) - }) - //Describing test 2.3 to check if test fails if we update with different controller - describe('Checking update with wrong controller', async () => { - it('This should not update DID doc ', async () => { - const did = '0x2f65b747440deaf596892dfc7965040be8b99100' - const doc = - '{"@context":"https://w3id.org/did/v1","id":"did:polygon:0x2f65b747440deaf596892dfc7965040be8b99100","verificationMethod":[{"id":"did:polygon:0x2f65b747440deaf596892dfc7965040be8b99100","type":"EcdsaSecp256k1VerificationKey2019","controller":"did:polygon:0x2f65b747440deaf596892dfc7965040be8c66666","publicKeyBase58":"NDaEdTguJV39Ns8BZkxQ3XR6GUinZAJfVoyEMkK9fP7XQmpSkT3UsLHB52cFpDqoM6m4Hevtba8pkmjvEG3Ur7ji"}]}' - try { - assert.ifError( - //sending transaction using different controller - await PolygonDIDRegistryInstance_test.functions.updateDID(did, doc), - ) - } catch (error) {} - }) - }) - - // Describing the 2.4 Test case - describe('Checking the Delete function in Contract', async () => { - it('This should delete the DID doc on Contract and will read and check if the value is now an empty string', async () => { - // This will send the value to local ganache blockchain - const did = '0x2f65b747440deaf596892dfc7965040be8b99100' - const tx = await PolygonDIDRegistryInstance.functions.deleteDID(did) - - // Transfer confirmation - await tx.wait() - - // Getting the set value in ganache blockchain - const currentValue = - await PolygonDIDRegistryInstance.functions.getDID(did) - - // Comparing the set value to confirm - assert.equal(currentValue, '', 'value should be matching') - }) - }) - - // Describing the 2.5 Test case - describe('Checking the Create function in Contract', async () => { - it('This should store the DID doc on Contract and will read and check if the value stored is not null', async () => { - // This will send the value to local ganache blockchain - const did = '0x54ec7be8b24f7139f870f277cf99c527ff43892b' - const doc = - '{"@context":"https://w3id.org/did/v1","id":"did:polygon:0x54ec7be8b24f7139f870f277cf99c527ff43892b","verificationMethod":[{"id":"did:polygon:0x54ec7be8b24f7139f870f277cf99c527ff43892b","type":"EcdsaSecp256k1VerificationKey2019","controller":"did:polygon:0x54ec7be8b24f7139f870f277cf99c527ff43892b","publicKeyBase58":"RwHysxjkzFdJDqi2irHRVRFHX9uUj5aVAXVoufnk5PDA4qcn1ejJMgGhqCsFdQqXRCTdi4TbEQFJjDUAdi2JvYmH"}]}' - const doctest = - '{"@context":"https://w3id.org/did/v1","id":"did:polygon:0x2f65b747440deaf596892dfc7965040be8b99100","verificationMethod":[{"id":"did:polygon:0x2f65b747440deaf596892dfc7965040be8b99100","type":"EcdsaSecp256k1VerificationKey2019","controller":"did:polygon:0x2f65b747440deaf596892dfc7965040be8c66666","publicKeyBase58":"NDaEdTguJV39Ns8BZkxQ3XR6GUinZAJfVoyEMkK9fP7XQmpSkT3UsLHB52cFpDqoM6m4Hevtba8pkmjvEG3Ur7ji"}]}' - const tx = await PolygonDIDRegistryInstance.functions.createDID(did, doc) - - // Transfer confirmation - await tx.wait() - - // Getting the set value in ganache blockchain - const currentValue = - await PolygonDIDRegistryInstance.functions.getDID(did) - - // Checking value is not null - - assert.notEqual(currentValue, doctest, 'value should not match') - }) - }) - - // Describing the 2.6 Test case - describe('Checking the Update function in Contract', async () => { - it('This should update the DID doc on Contract and will read and check if the value stored has changed', async () => { - // This will send the value to local ganache blockchain - const did = '0x54ec7be8b24f7139f870f277cf99c527ff43892b' - //changed the controller - const doc = - '{"@context":"https://w3id.org/did/v1","id":"did:polygon:0x2f65b747440deaf596892dfc7965040be8b99100","verificationMethod":[{"id":"did:polygon:0x2f65b747440deaf596892dfc7965040be8b99100","type":"EcdsaSecp256k1VerificationKey2019","controller":"did:polygon:0x2f65b747440deaf596892dfc7965040be8c66666","publicKeyBase58":"NDaEdTguJV39Ns8BZkxQ3XR6GUinZAJfVoyEMkK9fP7XQmpSkT3UsLHB52cFpDqoM6m4Hevtba8pkmjvEG3Ur7ji"}]}' - const docoriginal = - '{"@context":"https://w3id.org/did/v1","id":"did:polygon:0x54ec7be8b24f7139f870f277cf99c527ff43892b","verificationMethod":[{"id":"did:polygon:0x54ec7be8b24f7139f870f277cf99c527ff43892b","type":"EcdsaSecp256k1VerificationKey2019","controller":"did:polygon:0x54ec7be8b24f7139f870f277cf99c527ff43892b","publicKeyBase58":"RwHysxjkzFdJDqi2irHRVRFHX9uUj5aVAXVoufnk5PDA4qcn1ejJMgGhqCsFdQqXRCTdi4TbEQFJjDUAdi2JvYmH"}]}' - const tx = await PolygonDIDRegistryInstance.functions.updateDID(did, doc) - - // Transfer confirmation - await tx.wait() - - // Getting the set value in ganache blockchain - const currentValue = - await PolygonDIDRegistryInstance.functions.getDID(did) - - // Comparing the set value to confirm - assert.notEqual(currentValue, docoriginal, 'value should not match') - }) - }) - - // Describing the 2.7 Test case - describe('Checking the Delete function in Contract', async () => { - it('This should delete the DID doc on Contract and will read and check if the value is now not equal to the updated doc', async () => { - // This will send the value to local ganache blockchain - const did = '0x54ec7be8b24f7139f870f277cf99c527ff43892b' - const doc = - '{"@context":"https://w3id.org/did/v1","id":"did:polygon:0x2f65b747440deaf596892dfc7965040be8b99100","verificationMethod":[{"id":"did:polygon:0x2f65b747440deaf596892dfc7965040be8b99100","type":"EcdsaSecp256k1VerificationKey2019","controller":"did:polygon:0x2f65b747440deaf596892dfc7965040be8c66666","publicKeyBase58":"NDaEdTguJV39Ns8BZkxQ3XR6GUinZAJfVoyEMkK9fP7XQmpSkT3UsLHB52cFpDqoM6m4Hevtba8pkmjvEG3Ur7ji"}]}' - const tx = await PolygonDIDRegistryInstance.functions.deleteDID(did) - - // Transfer confirmation - await tx.wait() - - // Getting the set value in ganache blockchain - const currentValue = - await PolygonDIDRegistryInstance.functions.getDID(did) - - // Comparing the set value to confirm - assert.notEqual(currentValue, doc, 'value should not be matching') - }) - }) -}) diff --git a/tests/polygon_did_registry_test.test.js b/tests/polygon_did_registry_test.test.js new file mode 100644 index 0000000..d163534 --- /dev/null +++ b/tests/polygon_did_registry_test.test.js @@ -0,0 +1,117 @@ +const { ethers, upgrades } = require('hardhat') +const assert = require('assert') +const { describe, it } = require('node:test') + +// Initializing the build files +const PolygonDIDRegistryJSON = require('../artifacts/contracts/PolygonDidRegistry.sol/PolygonDidRegistry.json') + +let PolygonUpgradableInstance, PolygonUpgradableInstance_test +walletKey = process.env.SIGNER + +// Upgradable with Proxy +// Deployment Testing 1.0 +describe('Setting up the Upgradable Smart Contract', async () => { + describe('Setting up the contract', async () => { + it('Should deploy contract with Proxy, with no parameters/Constructor', async () => { + const PolygonDidRegistry = + await ethers.getContractFactory('PolygonDidRegistry') + PolygonUpgradableInstance = await upgrades.deployProxy(PolygonDidRegistry) + await PolygonUpgradableInstance.waitForDeployment() + PolygonUpgradableInstance_test = new ethers.Contract( + PolygonUpgradableInstance.target, + PolygonDIDRegistryJSON.abi, + walletKey, + ) + assert.ok( + PolygonUpgradableInstance.target, + 'Upgradeable contract is deployed', + ) + }) + }) + + //Check Create Function test 1.1 + describe('Checking the Create function using Upgradeable Contract', async () => { + it('This should store the DID doc on Upgradable Contract and will read and check if the value stored is correct', async () => { + // This will send the value to local ganache blockchain + const did = '0x2f65b747440deaf596892dfc7965040be8b99100' + const doc = + '{"@context":"https://w3id.org/did/v1","id":"did:polygon:0x2f65b747440deaf596892dfc7965040be8b99100","verificationMethod":[{"id":"did:polygon:0x2f65b747440deaf596892dfc7965040be8b99100","type":"EcdsaSecp256k1VerificationKey2019","controller":"did:polygon:0x2f65b747440deaf596892dfc7965040be8b99100","publicKeyBase58":"NDaEdTguJV39Ns8BZkxQ3XR6GUinZAJfVoyEMkK9fP7XQmpSkT3UsLHB52cFpDqoM6m4Hevtba8pkmjvEG3Ur7ji"}]}' + const tx = await PolygonUpgradableInstance.createDID(did, doc) + + // Getting the set value in ganache blockchain + const currentValue = await PolygonUpgradableInstance.getDIDDoc(did) + // Comparing the set value to confirm + assert.notEqual(currentValue, null, 'value set must be able to get') + + assert.notEqual(currentValue, null, 'value should not be empty') + }) + }) + + //Describing the 1.2 Test Case + describe('Checking the getTotalNumberOfDIDs Function', async () => { + it('This should get correct number of DIDs registered', async () => { + const currentValue = + await PolygonUpgradableInstance.getTotalNumberOfDIDs() + assert(currentValue > 0, 'value should be less than 0') + }) + }) + + // Describing the 1.3 Test case + describe('Checking the Update function in Upgradable Contract', async () => { + it('This should update the DID doc on Upgradable Contract and will read and check if the value stored is correct', async () => { + // This will send the value to local ganache blockchain + const did = '0x2f65b747440deaf596892dfc7965040be8b99100' + //changed the controller + const doc = + '{"@context":"https://w3id.org/did/v1","id":"did:polygon:0x2f65b747440deaf596892dfc7965040be8b99100","verificationMethod":[{"id":"did:polygon:0x2f65b747440deaf596892dfc7965040be8b99100","type":"EcdsaSecp256k1VerificationKey2019","controller":"did:polygon:0x2f65b747440deaf596892dfc7965040be8c66666","publicKeyBase58":"NDaEdTguJV39Ns8BZkxQ3XR6GUinZAJfVoyEMkK9fP7XQmpSkT3UsLHB52cFpDqoM6m4Hevtba8pkmjvEG3Ur7ji"}]}' + const tx = await PolygonUpgradableInstance.updateDIDDoc(did, doc) + + // Getting the set value in ganache blockchain + const currentValue = await PolygonUpgradableInstance.getDIDDoc(did) + + // Comparing the set value to confirm + assert.notEqual(currentValue, null, 'value set must be able to get') + }) + }) + + //Describing test 1.4 to check if test fails if we update with different controller + describe('Checking update with wrong controller', async () => { + it('This should not update DID doc ', async () => { + const did = '0x2f65b747440deaf596892dfc7965040be8b99109' + const doc = + '{"@context":"https://w3id.org/did/v1","id":"did:polygon:0x2f65b747440deaf596892dfc7965040be8b99100","verificationMethod":[{"id":"did:polygon:0x2f65b747440deaf596892dfc7965040be8b99100","type":"EcdsaSecp256k1VerificationKey2019","controller":"did:polygon:0x2f65b747440deaf596892dfc7965040be8c66666","publicKeyBase58":"NDaEdTguJV39Ns8BZkxQ3XR6GUinZAJfVoyEMkK9fP7XQmpSkT3UsLHB52cFpDqoM6m4Hevtba8pkmjvEG3Ur7ji"}]}' + try { + assert.ifError( + //sending transaction using different controller + await PolygonUpgradableInstance_test.functions.updateDID(did, doc), + ) + } catch (error) {} + }) + }) + + // //Describing the 1.10 Test Case + describe('Checking the Transafer Ownership Function', async () => { + it('This should transfer ownership of contract to another user', async () => { + const newOwner = '0x2f65b747440deaf596892dfc7965040be8b99109' + const owner = await PolygonUpgradableInstance.getOwner() + const tx = await PolygonUpgradableInstance.transferOwnership(newOwner) + const currentOwner = await PolygonUpgradableInstance.getOwner() + assert.notEqual(owner, currentOwner, 'value should not be equal') + }) + }) + + //Describing the 1.11 Test Case + describe('Checking the Transafer Ownership Function', async () => { + it('This should not transfer ownership of contract to another user since the transaction is being initiated by different owner', async () => { + const newOwner = '0x2f65b747440deaf596892dfc7965040be8b99100' + try { + assert.ifError( + //sending transaction using different controller + await PolygonUpgradableInstance_test.functions.transferOwnership( + newOwner, + ), + ) + } catch (error) {} + }) + }) +}) \ No newline at end of file diff --git a/truffle-config.js b/truffle-config.js deleted file mode 100644 index 129bd49..0000000 --- a/truffle-config.js +++ /dev/null @@ -1,68 +0,0 @@ -/** - * Use this file to configure your truffle project. It's seeded with some - * common settings for different networks and features like migrations, - * compilation and testing. Uncomment the ones you need or modify - * them to suit your project as necessary. - * - * More information about configuration can be found at: - * - * trufflesuite.com/docs/advanced/configuration - * - * To deploy via Infura you'll need a wallet provider (like @truffle/hdwallet-provider) - * to sign your transactions before they're sent to a remote public node. Infura accounts - * are available for free at: infura.io/register. - * - * You'll also need a mnemonic - the twelve word phrase the wallet uses to generate - * public/private key pairs. If you're publishing your code to GitHub make sure you load this - * phrase from a file you've .gitignored so it doesn't accidentally become public. - * - */ -const HDWalletProvider = require('@truffle/hdwallet-provider') -require('dotenv').config() - -module.exports = { - /** - * Networks define how you connect to your ethereum client and let you set the - * defaults web3 uses to send transactions. If you don't specify one truffle - * will spin up a development blockchain for you on port 9545 when you - * run `develop` or `test`. You can ask a truffle command to use a specific - * network from the command line, e.g - * - * $ truffle test --network - */ - networks: { - development: { - host: '127.0.0.1', // Localhost (default: none) - port: 8545, // Standard Ethereum port (default: none) - network_id: '*', // Any network (default: none) - }, - matic: { - provider: function () { - return new HDWalletProvider(process.env.SIGNER, process.env.RPCURL) - }, - network_id: '80001', - }, - matic_mainnet: { - provider: function () { - return new HDWalletProvider(process.env.SIGNER, process.env.RPCURL) - }, - network_id: '137', - }, - }, - - // Set default mocha options here, use special reporters etc. - mocha: { - // timeout: 100000 - }, - - // Configure your compilers - compilers: { - solc: { - version: '0.8.16', // Fetch exact version from solc-bin (default: truffle's version) - }, - }, - - db: { - enabled: false, - }, -}