From 1185ac00dd1dc75f9dc0a682db4e12f61f0d5cd8 Mon Sep 17 00:00:00 2001 From: tipusinghaw Date: Fri, 19 Jan 2024 18:58:04 +0530 Subject: [PATCH] fix: removed delete method and veriable Signed-off-by: tipusinghaw --- contracts/PolygonDidRegistry.sol | 93 +++--- hardhat.config.js | 6 + package.json | 3 +- pnpm-lock.yaml | 80 ++++- scripts/deploy.js | 20 +- tests/polygon_did_registry_test.js | 393 ------------------------ tests/polygon_did_registry_test.test.js | 117 +++++++ 7 files changed, 250 insertions(+), 462 deletions(-) delete mode 100644 tests/polygon_did_registry_test.js create mode 100644 tests/polygon_did_registry_test.test.js diff --git a/contracts/PolygonDidRegistry.sol b/contracts/PolygonDidRegistry.sol index 0297114..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; @@ -19,22 +18,19 @@ contract PolygonDidRegistry { modifier onlyController(address _id) { require( polyDIDs[_id].controller == msg.sender, - 'message sender is not the controller of the DID Doc' + "message sender is not the controller of the DID Doc" ); _; } 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 TransferOwnership(address newOwner); - event ResourceAdded( - address _id, - string _resourceId, - string _resourcePayload - ); + event ResourceAdded(address _id, string _resourceId, string _resourcePayload); bool private initialized; @@ -42,18 +38,15 @@ contract PolygonDidRegistry { *@dev initializes the ownership of contract **/ - mapping(address => string[]) private keysById; - mapping(address => mapping(string => string)) resourceData; - function initialize() public { - require(!initialized, 'Contract instance has already been initialized'); + require(!initialized, "Contract instance has already been initialized"); initialized = true; owner = msg.sender; totalDIDs = 0; } modifier onlyOwner() { - require(msg.sender == owner, 'message sender is not the owner'); + require(msg.sender == owner, "message sender is not the owner"); _; } @@ -62,15 +55,17 @@ contract PolygonDidRegistry { *@param _newOwner - Address of the new owner to whom the ownership needs to be passed **/ - function transferOwnership( - address _newOwner - ) public onlyOwner returns (string memory) { + function transferOwnership(address _newOwner) + public + onlyOwner + returns (string memory) + { if (owner != _newOwner) { owner = _newOwner; emit TransferOwnership(owner); - return ('Ownership transferred successfully'); + return ("Ownership transferred successfully"); } else { - return ('Ownership cannot be transferred to the same account'); + return ("Ownership cannot be transferred to the same account"); } } @@ -88,10 +83,7 @@ contract PolygonDidRegistry { *@param _doc - A string object that holds the DID Doc */ - function createDID( - address _id, - string memory _doc - ) + function createDID(address _id, string memory _doc) public returns ( address controller, @@ -121,9 +113,7 @@ contract PolygonDidRegistry { *@param _id - Address that refers to the DID doc position */ - function getDIDDoc( - address _id - ) public view returns (string memory, string[] memory) { + 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++) { @@ -136,19 +126,26 @@ contract PolygonDidRegistry { *@dev Reads total number of DIDs and total number of active DIDs from Chain */ - function getTotalNumberOfDIDs() public view returns (uint256 _totalDIDs) { + function getTotalNumberOfDIDs() + public + view + returns (uint256 _totalDIDs) + { return (totalDIDs); } + /** *@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 - ) public view returns (string memory) { + function getDIDDocByIndex(uint256 _index) + public + view + returns (string memory) + { return polyDIDs[activeDIDs[_index]].didDoc; } @@ -158,10 +155,7 @@ contract PolygonDidRegistry { *@param _doc - A String that holds the DID doc */ - function updateDIDDoc( - address _id, - string memory _doc - ) + function updateDIDDoc(address _id, string memory _doc) public onlyController(_id) returns ( @@ -187,31 +181,20 @@ contract PolygonDidRegistry { *@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); - } - - /** + 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) { + function getResource(address _id, string memory _resourceId) public view returns (string memory) { return resourceData[_id][_resourceId]; } @@ -220,9 +203,7 @@ contract PolygonDidRegistry { *@param _id - Address that refers to the DID doc */ - function getAllResources( - address _id - ) public view returns (string[] memory) { + 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++) { diff --git a/hardhat.config.js b/hardhat.config.js index d5181bb..04ade1c 100755 --- a/hardhat.config.js +++ b/hardhat.config.js @@ -5,6 +5,7 @@ // import 'hardhat'; require('@nomicfoundation/hardhat-ethers') require('@openzeppelin/hardhat-upgrades') +require("@nomicfoundation/hardhat-verify"); require('dotenv/config') @@ -25,4 +26,9 @@ module.exports = { accounts: [`0x${process.env.SIGNER}`], }, }, + etherscan: { + apiKey: { + polygonMumbai:process.env.VERIFICATION_KEY + } + } } diff --git a/package.json b/package.json index 69a3e69..97b374e 100644 --- a/package.json +++ b/package.json @@ -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", @@ -22,6 +22,7 @@ "license": "MIT", "devDependencies": { "@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", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 4b9b992..0c8bf35 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -8,9 +8,12 @@ devDependencies: '@nomicfoundation/hardhat-ethers': specifier: ^3.0.5 version: 3.0.5(ethers@6.9.2)(hardhat@2.19.4) + '@nomicfoundation/hardhat-verify': + specifier: ^2.0.3 + version: 2.0.3(hardhat@2.19.4) '@openzeppelin/hardhat-upgrades': specifier: ^3.0.1 - version: 3.0.1(@nomicfoundation/hardhat-ethers@3.0.5)(ethers@6.9.2)(hardhat@2.19.4) + version: 3.0.1(@nomicfoundation/hardhat-ethers@3.0.5)(@nomicfoundation/hardhat-verify@2.0.3)(ethers@6.9.2)(hardhat@2.19.4) '@types/fs-extra': specifier: ^11.0.1 version: 11.0.1 @@ -761,6 +764,25 @@ packages: - supports-color dev: true + /@nomicfoundation/hardhat-verify@2.0.3(hardhat@2.19.4): + resolution: {integrity: sha512-ESbRu9by53wu6VvgwtMtm108RSmuNsVqXtzg061D+/4R7jaWh/Wl/8ve+p6SdDX7vA1Z3L02hDO1Q3BY4luLXQ==} + peerDependencies: + hardhat: ^2.0.4 + dependencies: + '@ethersproject/abi': 5.7.0 + '@ethersproject/address': 5.7.0 + cbor: 8.1.0 + chalk: 2.4.2 + debug: 4.3.4(supports-color@8.1.1) + hardhat: 2.19.4(ts-node@10.9.1)(typescript@5.1.6) + lodash.clonedeep: 4.5.0 + semver: 6.3.1 + table: 6.8.1 + undici: 5.28.2 + transitivePeerDependencies: + - supports-color + dev: true + /@nomicfoundation/solidity-analyzer-darwin-arm64@0.1.1: resolution: {integrity: sha512-KcTodaQw8ivDZyF+D76FokN/HdpgGpfjc/gFCImdLUyqB6eSWVaZPazMbeAjmfhx3R0zm/NYVzxwAokFKgrc0w==} engines: {node: '>= 10'} @@ -1040,7 +1062,7 @@ packages: - encoding dev: true - /@openzeppelin/hardhat-upgrades@3.0.1(@nomicfoundation/hardhat-ethers@3.0.5)(ethers@6.9.2)(hardhat@2.19.4): + /@openzeppelin/hardhat-upgrades@3.0.1(@nomicfoundation/hardhat-ethers@3.0.5)(@nomicfoundation/hardhat-verify@2.0.3)(ethers@6.9.2)(hardhat@2.19.4): resolution: {integrity: sha512-NtD2/n2PKNqHBafQy3AM6KCvsDZD0w97po7fFa4wctl0fg/8QwGAg3fB8InkBFEhGn17+IgshRI8G94hUrFPcQ==} hasBin: true peerDependencies: @@ -1053,6 +1075,7 @@ packages: optional: true dependencies: '@nomicfoundation/hardhat-ethers': 3.0.5(ethers@6.9.2)(hardhat@2.19.4) + '@nomicfoundation/hardhat-verify': 2.0.3(hardhat@2.19.4) '@openzeppelin/defender-admin-client': 1.54.1(debug@4.3.4) '@openzeppelin/defender-base-client': 1.54.1(debug@4.3.4) '@openzeppelin/defender-sdk-base-client': 1.8.0 @@ -1403,6 +1426,15 @@ packages: uri-js: 4.4.1 dev: true + /ajv@8.12.0: + resolution: {integrity: sha512-sRu1kpcO9yLtYxBKvqfTeh9KzZEwO3STyX1HT+4CaDzC6HpTGYhIhPIzj9XuKU7KYDwnaeh5hcOwjy1QuJzBPA==} + dependencies: + fast-deep-equal: 3.1.3 + json-schema-traverse: 1.0.0 + require-from-string: 2.0.2 + uri-js: 4.4.1 + dev: true + /amazon-cognito-identity-js@6.3.7: resolution: {integrity: sha512-tSjnM7KyAeOZ7UMah+oOZ6cW4Gf64FFcc7BE2l7MTcp7ekAPrXaCbpcW2xEpH1EiDS4cPcAouHzmCuc2tr72vQ==} dependencies: @@ -1563,6 +1595,11 @@ packages: tslib: 2.6.2 dev: true + /astral-regex@2.0.0: + resolution: {integrity: sha512-Z7tMw1ytTXt5jqMcOP+OQteU1VuNK9Y02uuJtKQ1Sv69jXQKKg5cibLwGJow8yzZP+eAc18EmLGPal0bp36rvQ==} + engines: {node: '>=8'} + dev: true + /async-retry@1.3.3: resolution: {integrity: sha512-wfr/jstw9xNi/0teMHrRW7dsz3Lt5ARhYNZ2ewpadnhaIp5mbALhOAP+EAdsC7t4Z6wqsDVv9+W6gm1Dk9mEyw==} requiresBuild: true @@ -1861,6 +1898,13 @@ packages: requiresBuild: true dev: true + /cbor@8.1.0: + resolution: {integrity: sha512-DwGjNW9omn6EwP70aXsn7FQJx5kO12tX0bZkaTjzdVFM6/7nhA4t0EENocKGx6D2Bch9PE2KzCUf5SceBdeijg==} + engines: {node: '>=12.19'} + dependencies: + nofilter: 3.1.0 + dev: true + /cbor@9.0.1: resolution: {integrity: sha512-/TQOWyamDxvVIv+DY9cOLNuABkoyz8K/F3QE56539pGVYohx0+MEA1f4lChFTX79dBTBS7R1PF6ovH7G+VtBfQ==} engines: {node: '>=16'} @@ -3912,6 +3956,10 @@ packages: resolution: {integrity: sha512-xbbCH5dCYU5T8LcEhhuh7HJ88HXuW3qsI3Y0zOZFKfZEHcpWiHU/Jxzk629Brsab/mMiHQti9wMP+845RPe3Vg==} dev: true + /json-schema-traverse@1.0.0: + resolution: {integrity: sha512-NM8/P9n3XjXhIZn1lLhkFaACTOURQXjWhV4BA/RnOv8xvgqtqpAX9IO4mRQxSx1Rlo4tqzeqb0sOlruaOy3dug==} + dev: true + /json-stable-stringify-without-jsonify@1.0.1: resolution: {integrity: sha512-Bdboy+l7tA3OGW6FjyFHWkP5LuByj1Tk33Ljyq0axyzdk9//JSi2u3fP1QSmd1KNwq6VOKYGlAu87CisVir6Pw==} dev: true @@ -4027,6 +4075,10 @@ packages: resolution: {integrity: sha512-kZzYOKspf8XVX5AvmQF94gQW0lejFVgb80G85bU4ZWzoJ6C03PQg3coYAUpSTpQWelrZELd3XWgHzw4Ck5kaIw==} dev: true + /lodash.clonedeep@4.5.0: + resolution: {integrity: sha512-H5ZhCF25riFd9uB5UCkVKo61m3S/xZk1x4wA6yp/L3RFP6Z/eHH1ymQcGLo7J3GMPfm0V/7m1tryHuGVxpqEBQ==} + dev: true + /lodash.escaperegexp@4.1.2: resolution: {integrity: sha512-TM9YBvyC84ZxE3rgfefxUWiQKLilstD6k7PTGt6wfbtXF8ixIJLOL3VYyV/z+ZiPLsVxAsKAFVwWlWeb2Y8Yyw==} dev: true @@ -4047,6 +4099,10 @@ packages: resolution: {integrity: sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ==} dev: true + /lodash.truncate@4.4.2: + resolution: {integrity: sha512-jttmRe7bRse52OsWIMDLaXxWqRAmtIUccAQ3garviCqJjafXOfNMO0yMfNpdD6zbGaTU0P5Nz7e7gAT6cKmJRw==} + dev: true + /lodash.uniqby@4.7.0: resolution: {integrity: sha512-e/zcLx6CSbmaEgFHCA7BnoQKyCtKMxnuWrJygbwPs/AIn+IMKl66L8/s+wBUn5LRw2pZx3bUHibiV1b6aTWIww==} dev: true @@ -5243,6 +5299,15 @@ packages: engines: {node: '>=12'} dev: true + /slice-ansi@4.0.0: + resolution: {integrity: sha512-qMCMfhY040cVHT43K9BFygqYbUPFZKHOg7K73mtTWJRb8pyP3fzf4Ixd5SzdEJQ6MRUg/WBnOLxghZtKKurENQ==} + engines: {node: '>=10'} + dependencies: + ansi-styles: 4.3.0 + astral-regex: 2.0.0 + is-fullwidth-code-point: 3.0.0 + dev: true + /smart-buffer@4.2.0: resolution: {integrity: sha512-94hK0Hh8rPqQl2xXc3HsaBoOXKV20MToPkcXvwbISWLEs+64sBq5kFgn2kJDHb1Pry9yrP0dxrCI9RRci7RXKg==} engines: {node: '>= 6.0.0', npm: '>= 3.0.0'} @@ -5514,6 +5579,17 @@ packages: engines: {node: '>= 0.4'} dev: true + /table@6.8.1: + resolution: {integrity: sha512-Y4X9zqrCftUhMeH2EptSSERdVKt/nEdijTOacGD/97EKjhQ/Qs8RTlEGABSJNNN8lac9kheH+af7yAkEWlgneA==} + engines: {node: '>=10.0.0'} + dependencies: + ajv: 8.12.0 + lodash.truncate: 4.4.2 + slice-ansi: 4.0.0 + string-width: 4.2.3 + strip-ansi: 6.0.1 + dev: true + /text-table@0.2.0: resolution: {integrity: sha512-N+8UisAXDGk8PFXP4HAzVR9nbfmVJ3zYLAWiTIoqC5v5isinhr+r5uaO8+7r3BMfuNIufIsA7RdpVgacC2cSpw==} dev: true diff --git a/scripts/deploy.js b/scripts/deploy.js index 16c0f9f..d391635 100755 --- a/scripts/deploy.js +++ b/scripts/deploy.js @@ -8,17 +8,17 @@ async function main() { // To deploy the initial upgradable smart contract - // const contract = await upgrades.deployProxy(PolygonDidRegistry, { - // initializer: 'initialize', - // }) + const contract = await upgrades.deployProxy(PolygonDidRegistry, { + initializer: 'initialize', + }) - const contract = await upgrades.upgradeProxy( - process.env.CONTRACT_ADDRESS, - PolygonDidRegistry, - { - initializer: 'initialize', - }, - ) + // const contract = await upgrades.upgradeProxy( + // process.env.CONTRACT_ADDRESS, + // PolygonDidRegistry, + // { + // initializer: 'initialize', + // }, + // ) await contract.waitForDeployment() 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..f78439b --- /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) {} + }) + }) +})