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

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,14 +1,12 @@
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
6
6
  *@dev Smart Contract for Polygon DID Method
7
7
  */
8
8
  contract PolygonDidRegistry {
9
- uint256 totalDIDs;
10
9
  address owner;
11
- uint256 deletedDID;
12
10
  struct PolyDID {
13
11
  address controller;
14
12
  uint256 created;
@@ -16,17 +14,9 @@ contract PolygonDidRegistry {
16
14
  string didDoc;
17
15
  }
18
16
 
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
17
  mapping(address => PolyDID) polyDIDs;
27
- mapping(uint256 => address) activeDIDs;
28
- mapping(address => uint256) activeAddress;
29
-
18
+ mapping(address => mapping(string => string)) resourceData;
19
+ mapping(address => string[]) private keysById;
30
20
  event DIDCreated(address id, string doc);
31
21
  event DIDUpdated(address id, string doc);
32
22
  event TransferOwnership(address newOwner);
@@ -39,17 +29,21 @@ contract PolygonDidRegistry {
39
29
  bool private initialized;
40
30
 
41
31
  /**
42
- *@dev initializes the ownership of contract
32
+ *@dev modifiers for access control
43
33
  **/
44
34
 
45
- mapping(address => string[]) private keysById;
46
- mapping(address => mapping(string => string)) resourceData;
35
+ modifier onlyController(address _id) {
36
+ require(
37
+ polyDIDs[_id].controller == msg.sender,
38
+ 'message sender is not the controller of the DID Doc'
39
+ );
40
+ _;
41
+ }
47
42
 
48
- function initialize() public {
43
+ modifier nonReentrant() {
49
44
  require(!initialized, 'Contract instance has already been initialized');
50
45
  initialized = true;
51
- owner = msg.sender;
52
- totalDIDs = 0;
46
+ _;
53
47
  }
54
48
 
55
49
  modifier onlyOwner() {
@@ -57,6 +51,15 @@ contract PolygonDidRegistry {
57
51
  _;
58
52
  }
59
53
 
54
+ /**
55
+ *@dev initializes the ownership of contract
56
+ **/
57
+
58
+ function initialize() external nonReentrant {
59
+ require(msg.sender != address(0), 'Invalid owner address');
60
+ owner = msg.sender;
61
+ }
62
+
60
63
  /**
61
64
  *@dev transfer the ownership of contract
62
65
  *@param _newOwner - Address of the new owner to whom the ownership needs to be passed
@@ -64,14 +67,15 @@ contract PolygonDidRegistry {
64
67
 
65
68
  function transferOwnership(
66
69
  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
- }
70
+ ) external onlyOwner returns (string memory) {
71
+ require(_newOwner != address(0), 'Invalid owner address');
72
+ require(
73
+ owner != _newOwner,
74
+ 'Ownership cannot be transferred to the same account'
75
+ );
76
+ owner = _newOwner;
77
+ emit TransferOwnership(owner);
78
+ return ('Ownership transferred successfully');
75
79
  }
76
80
 
77
81
  /**
@@ -92,7 +96,7 @@ contract PolygonDidRegistry {
92
96
  address _id,
93
97
  string memory _doc
94
98
  )
95
- public
99
+ external
96
100
  returns (
97
101
  address controller,
98
102
  uint256 created,
@@ -100,13 +104,12 @@ contract PolygonDidRegistry {
100
104
  string memory didDoc
101
105
  )
102
106
  {
103
- polyDIDs[_id].controller = msg.sender;
107
+ require(_id != address(0), 'Invalid address provided');
108
+ require(polyDIDs[_id].controller != _id, 'DID already exist');
109
+ polyDIDs[_id].controller = _id;
104
110
  polyDIDs[_id].created = block.timestamp;
105
111
  polyDIDs[_id].updated = block.timestamp;
106
112
  polyDIDs[_id].didDoc = _doc;
107
- activeDIDs[totalDIDs] = msg.sender;
108
- activeAddress[_id] = totalDIDs;
109
- ++totalDIDs;
110
113
  emit DIDCreated(_id, _doc);
111
114
  return (
112
115
  polyDIDs[_id].controller,
@@ -132,26 +135,6 @@ contract PolygonDidRegistry {
132
135
  return (polyDIDs[_id].didDoc, result);
133
136
  }
134
137
 
135
- /**
136
- *@dev Reads total number of DIDs and total number of active DIDs from Chain
137
- */
138
-
139
- function getTotalNumberOfDIDs() public view returns (uint256 _totalDIDs) {
140
- return (totalDIDs);
141
- }
142
-
143
- /**
144
- *@dev Reads one DID at a time from Chain based on index
145
- *@param _index - Uint256 type variable that refers to the DID position
146
- *@return _did - returns the DID Doc assciated with the index. Returns null if the DID Doc is deleted.
147
- */
148
-
149
- function getDIDDocByIndex(
150
- uint256 _index
151
- ) public view returns (string memory) {
152
- return polyDIDs[activeDIDs[_index]].didDoc;
153
- }
154
-
155
138
  /**
156
139
  *@dev To Update the DID doc
157
140
  *@param _id - Address that refers to the DID doc
@@ -162,7 +145,7 @@ contract PolygonDidRegistry {
162
145
  address _id,
163
146
  string memory _doc
164
147
  )
165
- public
148
+ external
166
149
  onlyController(_id)
167
150
  returns (
168
151
  address controller,
@@ -192,10 +175,11 @@ contract PolygonDidRegistry {
192
175
  string memory _resourceId,
193
176
  string memory _resourcePayload
194
177
  )
195
- public
178
+ external
196
179
  onlyController(_id)
197
180
  returns (address, string memory, string memory)
198
181
  {
182
+ require(_id != address(0), 'Invalid address provided');
199
183
  resourceData[_id][_resourceId] = _resourcePayload;
200
184
  keysById[_id].push(_resourceId);
201
185
  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.5",
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":"0x7cfa8ab1c0bc24dcb13a69e00eca9da0d9850c96acb083ce64a7a18ebdca959f"}
@@ -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
+ })