@ayanworks/polygon-did-registry-contract 2.0.1-alpha.3 → 2.0.1-alpha.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  //SPDX-License-Identifier: Apache 2.0
2
- pragma solidity ^0.8.16;
2
+ pragma solidity 0.8.16;
3
3
 
4
4
  /**
5
5
  *@title PolygonDidRegistry
@@ -8,7 +8,6 @@ pragma solidity ^0.8.16;
8
8
  contract PolygonDidRegistry {
9
9
  uint256 totalDIDs;
10
10
  address owner;
11
- uint256 deletedDID;
12
11
  struct PolyDID {
13
12
  address controller;
14
13
  uint256 created;
@@ -16,17 +15,11 @@ contract PolygonDidRegistry {
16
15
  string didDoc;
17
16
  }
18
17
 
19
- modifier onlyController(address _id) {
20
- require(
21
- polyDIDs[_id].controller == msg.sender,
22
- 'message sender is not the controller of the DID Doc'
23
- );
24
- _;
25
- }
26
18
  mapping(address => PolyDID) polyDIDs;
27
19
  mapping(uint256 => address) activeDIDs;
28
20
  mapping(address => uint256) activeAddress;
29
-
21
+ mapping(address => mapping(string => string)) resourceData;
22
+ mapping(address => string[]) private keysById;
30
23
  event DIDCreated(address id, string doc);
31
24
  event DIDUpdated(address id, string doc);
32
25
  event TransferOwnership(address newOwner);
@@ -39,17 +32,22 @@ contract PolygonDidRegistry {
39
32
  bool private initialized;
40
33
 
41
34
  /**
42
- *@dev initializes the ownership of contract
35
+ *@dev modifiers for access control
43
36
  **/
44
37
 
45
- mapping(address => string[]) private keysById;
46
- mapping(address => mapping(string => string)) resourceData;
38
+ modifier onlyController(address _id) {
39
+ require(
40
+ polyDIDs[_id].controller == msg.sender,
41
+ 'message sender is not the controller of the DID Doc'
42
+ );
43
+ _;
44
+ }
47
45
 
48
- function initialize() public {
46
+ modifier nonReentrant() {
49
47
  require(!initialized, 'Contract instance has already been initialized');
50
48
  initialized = true;
51
- owner = msg.sender;
52
49
  totalDIDs = 0;
50
+ _;
53
51
  }
54
52
 
55
53
  modifier onlyOwner() {
@@ -57,6 +55,15 @@ contract PolygonDidRegistry {
57
55
  _;
58
56
  }
59
57
 
58
+ /**
59
+ *@dev initializes the ownership of contract
60
+ **/
61
+
62
+ function initialize() external nonReentrant {
63
+ require(msg.sender != address(0), 'Invalid owner address');
64
+ owner = msg.sender;
65
+ }
66
+
60
67
  /**
61
68
  *@dev transfer the ownership of contract
62
69
  *@param _newOwner - Address of the new owner to whom the ownership needs to be passed
@@ -64,14 +71,15 @@ contract PolygonDidRegistry {
64
71
 
65
72
  function transferOwnership(
66
73
  address _newOwner
67
- ) public onlyOwner returns (string memory) {
68
- if (owner != _newOwner) {
69
- owner = _newOwner;
70
- emit TransferOwnership(owner);
71
- return ('Ownership transferred successfully');
72
- } else {
73
- return ('Ownership cannot be transferred to the same account');
74
- }
74
+ ) external onlyOwner returns (string memory) {
75
+ require(_newOwner != address(0), 'Invalid owner address');
76
+ require(
77
+ owner != _newOwner,
78
+ 'Ownership cannot be transferred to the same account'
79
+ );
80
+ owner = _newOwner;
81
+ emit TransferOwnership(owner);
82
+ return ('Ownership transferred successfully');
75
83
  }
76
84
 
77
85
  /**
@@ -92,7 +100,7 @@ contract PolygonDidRegistry {
92
100
  address _id,
93
101
  string memory _doc
94
102
  )
95
- public
103
+ external
96
104
  returns (
97
105
  address controller,
98
106
  uint256 created,
@@ -100,6 +108,7 @@ contract PolygonDidRegistry {
100
108
  string memory didDoc
101
109
  )
102
110
  {
111
+ require(_id != address(0), 'Invalid address provided');
103
112
  polyDIDs[_id].controller = msg.sender;
104
113
  polyDIDs[_id].created = block.timestamp;
105
114
  polyDIDs[_id].updated = block.timestamp;
@@ -162,7 +171,7 @@ contract PolygonDidRegistry {
162
171
  address _id,
163
172
  string memory _doc
164
173
  )
165
- public
174
+ external
166
175
  onlyController(_id)
167
176
  returns (
168
177
  address controller,
@@ -192,10 +201,11 @@ contract PolygonDidRegistry {
192
201
  string memory _resourceId,
193
202
  string memory _resourcePayload
194
203
  )
195
- public
204
+ external
196
205
  onlyController(_id)
197
206
  returns (address, string memory, string memory)
198
207
  {
208
+ require(_id != address(0), 'Invalid address provided');
199
209
  resourceData[_id][_resourceId] = _resourcePayload;
200
210
  keysById[_id].push(_resourceId);
201
211
  emit ResourceAdded(_id, _resourceId, _resourcePayload);
package/hardhat.config.js CHANGED
@@ -5,6 +5,7 @@
5
5
  // import 'hardhat';
6
6
  require('@nomicfoundation/hardhat-ethers')
7
7
  require('@openzeppelin/hardhat-upgrades')
8
+ require('@nomicfoundation/hardhat-verify')
8
9
 
9
10
  require('dotenv/config')
10
11
 
@@ -20,9 +21,19 @@ module.exports = {
20
21
  },
21
22
  networks: {
22
23
  hardhat: {},
23
- mumbai: {
24
- url: process.env.RPCURL, // Update with Mumbai Matic RPC URL
24
+ amoy: {
25
+ url: process.env.AMOY_RPCURL,
25
26
  accounts: [`0x${process.env.SIGNER}`],
26
27
  },
28
+ //Use for mainnet deployment
29
+ // polygon: {
30
+ // url: process.env.RPCURL_POLYON_MAINNET,
31
+ // accounts: [`0x${process.env.SIGNER}`],
32
+ // },
33
+ },
34
+ etherscan: {
35
+ apiKey: {
36
+ polygonMumbai: process.env.VERIFICATION_KEY,
37
+ },
27
38
  },
28
39
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ayanworks/polygon-did-registry-contract",
3
- "version": "2.0.1-alpha.3",
3
+ "version": "2.0.1-alpha.4",
4
4
  "description": "Smart Contract to register DID on Polygon",
5
5
  "main": "build/index.js",
6
6
  "directories": {
@@ -8,7 +8,7 @@
8
8
  },
9
9
  "scripts": {
10
10
  "deploy": "node deploy.js",
11
- "test": "node compile.js && mocha --timeout 30000",
11
+ "test": "node --require ts-node/register --test ./tests/polygon_did_registry_test.test.js",
12
12
  "clean": "rm -rf build",
13
13
  "clean:deps": "pnpm clean && rm -rf node_modules",
14
14
  "build": "pnpm clean && pnpm build:sol && tsc",
@@ -22,6 +22,7 @@
22
22
  "license": "MIT",
23
23
  "devDependencies": {
24
24
  "@nomicfoundation/hardhat-ethers": "^3.0.5",
25
+ "@nomicfoundation/hardhat-verify": "^2.0.3",
25
26
  "@openzeppelin/hardhat-upgrades": "^3.0.1",
26
27
  "@types/fs-extra": "^11.0.1",
27
28
  "@types/node": "^18.17.0",
@@ -33,7 +34,7 @@
33
34
  "mocha": "^7.0.0",
34
35
  "prettier": "^3.0.2",
35
36
  "release-it": "^16.3.0",
36
- "solc": "^0.8.21",
37
+ "solc": "0.8.16",
37
38
  "ts-node": "^10.9.1",
38
39
  "typescript": "^5.1.6"
39
40
  },
package/scripts/deploy.js CHANGED
@@ -8,17 +8,17 @@ async function main() {
8
8
 
9
9
  // To deploy the initial upgradable smart contract
10
10
 
11
- // const contract = await upgrades.deployProxy(PolygonDidRegistry, {
12
- // initializer: 'initialize',
13
- // })
11
+ const contract = await upgrades.deployProxy(PolygonDidRegistry, {
12
+ initializer: 'initialize',
13
+ })
14
14
 
15
- const contract = await upgrades.upgradeProxy(
16
- process.env.CONTRACT_ADDRESS,
17
- PolygonDidRegistry,
18
- {
19
- initializer: 'initialize',
20
- },
21
- )
15
+ // const contract = await upgrades.upgradeProxy(
16
+ // process.env.CONTRACT_ADDRESS,
17
+ // PolygonDidRegistry,
18
+ // {
19
+ // initializer: 'initialize',
20
+ // },
21
+ // )
22
22
 
23
23
  await contract.waitForDeployment()
24
24
 
package/sst-config.json CHANGED
@@ -1 +1 @@
1
- {"sourceHash":"0xb57d8b09722fddffbc5855f1dfba236305522e3e11aa91722f77789c16a9629b"}
1
+ {"sourceHash":"0x99e57955d85cedd34a3d4d094b89b3fbcf3fd704b4c4b67d6dd6544f783afe53"}
@@ -0,0 +1,117 @@
1
+ const { ethers, upgrades } = require('hardhat')
2
+ const assert = require('assert')
3
+ const { describe, it } = require('node:test')
4
+
5
+ // Initializing the build files
6
+ const PolygonDIDRegistryJSON = require('../artifacts/contracts/PolygonDidRegistry.sol/PolygonDidRegistry.json')
7
+
8
+ let PolygonUpgradableInstance, PolygonUpgradableInstance_test
9
+ walletKey = process.env.SIGNER
10
+
11
+ // Upgradable with Proxy
12
+ // Deployment Testing 1.0
13
+ describe('Setting up the Upgradable Smart Contract', async () => {
14
+ describe('Setting up the contract', async () => {
15
+ it('Should deploy contract with Proxy, with no parameters/Constructor', async () => {
16
+ const PolygonDidRegistry =
17
+ await ethers.getContractFactory('PolygonDidRegistry')
18
+ PolygonUpgradableInstance = await upgrades.deployProxy(PolygonDidRegistry)
19
+ await PolygonUpgradableInstance.waitForDeployment()
20
+ PolygonUpgradableInstance_test = new ethers.Contract(
21
+ PolygonUpgradableInstance.target,
22
+ PolygonDIDRegistryJSON.abi,
23
+ walletKey,
24
+ )
25
+ assert.ok(
26
+ PolygonUpgradableInstance.target,
27
+ 'Upgradeable contract is deployed',
28
+ )
29
+ })
30
+ })
31
+
32
+ //Check Create Function test 1.1
33
+ describe('Checking the Create function using Upgradeable Contract', async () => {
34
+ it('This should store the DID doc on Upgradable Contract and will read and check if the value stored is correct', async () => {
35
+ // This will send the value to local ganache blockchain
36
+ const did = '0x2f65b747440deaf596892dfc7965040be8b99100'
37
+ const doc =
38
+ '{"@context":"https://w3id.org/did/v1","id":"did:polygon:0x2f65b747440deaf596892dfc7965040be8b99100","verificationMethod":[{"id":"did:polygon:0x2f65b747440deaf596892dfc7965040be8b99100","type":"EcdsaSecp256k1VerificationKey2019","controller":"did:polygon:0x2f65b747440deaf596892dfc7965040be8b99100","publicKeyBase58":"NDaEdTguJV39Ns8BZkxQ3XR6GUinZAJfVoyEMkK9fP7XQmpSkT3UsLHB52cFpDqoM6m4Hevtba8pkmjvEG3Ur7ji"}]}'
39
+ const tx = await PolygonUpgradableInstance.createDID(did, doc)
40
+
41
+ // Getting the set value in ganache blockchain
42
+ const currentValue = await PolygonUpgradableInstance.getDIDDoc(did)
43
+ // Comparing the set value to confirm
44
+ assert.notEqual(currentValue, null, 'value set must be able to get')
45
+
46
+ assert.notEqual(currentValue, null, 'value should not be empty')
47
+ })
48
+ })
49
+
50
+ //Describing the 1.2 Test Case
51
+ describe('Checking the getTotalNumberOfDIDs Function', async () => {
52
+ it('This should get correct number of DIDs registered', async () => {
53
+ const currentValue =
54
+ await PolygonUpgradableInstance.getTotalNumberOfDIDs()
55
+ assert(currentValue > 0, 'value should be less than 0')
56
+ })
57
+ })
58
+
59
+ // Describing the 1.3 Test case
60
+ describe('Checking the Update function in Upgradable Contract', async () => {
61
+ it('This should update the DID doc on Upgradable Contract and will read and check if the value stored is correct', async () => {
62
+ // This will send the value to local ganache blockchain
63
+ const did = '0x2f65b747440deaf596892dfc7965040be8b99100'
64
+ //changed the controller
65
+ const doc =
66
+ '{"@context":"https://w3id.org/did/v1","id":"did:polygon:0x2f65b747440deaf596892dfc7965040be8b99100","verificationMethod":[{"id":"did:polygon:0x2f65b747440deaf596892dfc7965040be8b99100","type":"EcdsaSecp256k1VerificationKey2019","controller":"did:polygon:0x2f65b747440deaf596892dfc7965040be8c66666","publicKeyBase58":"NDaEdTguJV39Ns8BZkxQ3XR6GUinZAJfVoyEMkK9fP7XQmpSkT3UsLHB52cFpDqoM6m4Hevtba8pkmjvEG3Ur7ji"}]}'
67
+ const tx = await PolygonUpgradableInstance.updateDIDDoc(did, doc)
68
+
69
+ // Getting the set value in ganache blockchain
70
+ const currentValue = await PolygonUpgradableInstance.getDIDDoc(did)
71
+
72
+ // Comparing the set value to confirm
73
+ assert.notEqual(currentValue, null, 'value set must be able to get')
74
+ })
75
+ })
76
+
77
+ //Describing test 1.4 to check if test fails if we update with different controller
78
+ describe('Checking update with wrong controller', async () => {
79
+ it('This should not update DID doc ', async () => {
80
+ const did = '0x2f65b747440deaf596892dfc7965040be8b99109'
81
+ const doc =
82
+ '{"@context":"https://w3id.org/did/v1","id":"did:polygon:0x2f65b747440deaf596892dfc7965040be8b99100","verificationMethod":[{"id":"did:polygon:0x2f65b747440deaf596892dfc7965040be8b99100","type":"EcdsaSecp256k1VerificationKey2019","controller":"did:polygon:0x2f65b747440deaf596892dfc7965040be8c66666","publicKeyBase58":"NDaEdTguJV39Ns8BZkxQ3XR6GUinZAJfVoyEMkK9fP7XQmpSkT3UsLHB52cFpDqoM6m4Hevtba8pkmjvEG3Ur7ji"}]}'
83
+ try {
84
+ assert.ifError(
85
+ //sending transaction using different controller
86
+ await PolygonUpgradableInstance_test.functions.updateDID(did, doc),
87
+ )
88
+ } catch (error) {}
89
+ })
90
+ })
91
+
92
+ // //Describing the 1.10 Test Case
93
+ describe('Checking the Transafer Ownership Function', async () => {
94
+ it('This should transfer ownership of contract to another user', async () => {
95
+ const newOwner = '0x2f65b747440deaf596892dfc7965040be8b99109'
96
+ const owner = await PolygonUpgradableInstance.getOwner()
97
+ const tx = await PolygonUpgradableInstance.transferOwnership(newOwner)
98
+ const currentOwner = await PolygonUpgradableInstance.getOwner()
99
+ assert.notEqual(owner, currentOwner, 'value should not be equal')
100
+ })
101
+ })
102
+
103
+ //Describing the 1.11 Test Case
104
+ describe('Checking the Transafer Ownership Function', async () => {
105
+ it('This should not transfer ownership of contract to another user since the transaction is being initiated by different owner', async () => {
106
+ const newOwner = '0x2f65b747440deaf596892dfc7965040be8b99100'
107
+ try {
108
+ assert.ifError(
109
+ //sending transaction using different controller
110
+ await PolygonUpgradableInstance_test.functions.transferOwnership(
111
+ newOwner,
112
+ ),
113
+ )
114
+ } catch (error) {}
115
+ })
116
+ })
117
+ })