@ayanworks/polygon-did-registry-contract 1.1.1 → 1.1.2
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.
- package/.env.sample +2 -2
- package/LICENSE +201 -0
- package/README.md +33 -15
- package/build/contracts/Migrations.json +32 -4
- package/build/contracts/PolygonDidRegistry.json +4860 -2744
- package/contracts/PolygonDidRegistry.sol +122 -44
- package/package.json +3 -1
- package/test/polygon_did_registry_test.js +37 -11
- package/truffle-config.js +4 -4
|
@@ -1,68 +1,78 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
//SPDX-License-Identifier: MIT
|
|
6
|
-
pragma solidity ^0.8.15;
|
|
7
|
-
// import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
|
|
1
|
+
//SPDX-License-Identifier: Apache 2.0
|
|
2
|
+
pragma solidity ^0.8.16;
|
|
8
3
|
|
|
9
4
|
/**
|
|
10
5
|
*@title PolygonDidRegistry
|
|
11
6
|
*@dev Smart Contract for Polygon DID Method
|
|
12
7
|
*/
|
|
13
8
|
contract PolygonDidRegistry {
|
|
9
|
+
uint256 totalDIDs;
|
|
14
10
|
address owner;
|
|
11
|
+
uint256 deletedDID;
|
|
15
12
|
struct PolyDID {
|
|
16
13
|
address controller;
|
|
17
14
|
uint256 created;
|
|
18
15
|
uint256 updated;
|
|
19
|
-
string
|
|
16
|
+
string didDoc;
|
|
20
17
|
}
|
|
21
18
|
|
|
22
19
|
modifier onlyController(address _id) {
|
|
23
20
|
require(
|
|
24
|
-
|
|
21
|
+
polyDIDs[_id].controller == msg.sender,
|
|
22
|
+
"message sender is not the controller of the DID Doc"
|
|
25
23
|
);
|
|
26
24
|
_;
|
|
27
25
|
}
|
|
28
|
-
|
|
29
|
-
mapping(
|
|
30
|
-
|
|
31
|
-
event
|
|
32
|
-
event
|
|
26
|
+
mapping(address => PolyDID) polyDIDs;
|
|
27
|
+
mapping(uint256 => address) activeDIDs;
|
|
28
|
+
mapping(address => uint256) activeAddress;
|
|
29
|
+
event DIDCreated(address id, string doc);
|
|
30
|
+
event DIDUpdated(address id, string doc);
|
|
31
|
+
event DIDDeleted(address id);
|
|
33
32
|
event TransferOwnership(address newOwner);
|
|
34
|
-
|
|
33
|
+
bool private initialized;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
*@dev initializes the ownership of contract
|
|
37
|
+
**/
|
|
35
38
|
|
|
36
39
|
function initialize() public {
|
|
37
40
|
require(!initialized, "Contract instance has already been initialized");
|
|
38
41
|
initialized = true;
|
|
39
42
|
owner = msg.sender;
|
|
43
|
+
totalDIDs = 0;
|
|
44
|
+
deletedDID = 0;
|
|
40
45
|
}
|
|
41
46
|
|
|
42
|
-
modifier onlyOwner(){
|
|
43
|
-
require(
|
|
47
|
+
modifier onlyOwner() {
|
|
48
|
+
require(msg.sender == owner, "message sender is not the owner");
|
|
44
49
|
_;
|
|
45
50
|
}
|
|
46
51
|
|
|
47
52
|
/**
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
+
*@dev transfer the ownership of contract
|
|
54
|
+
*@param _newOwner - Address of the new owner to whom the ownership needs to be passed
|
|
55
|
+
**/
|
|
56
|
+
|
|
57
|
+
function transferOwnership(address _newOwner)
|
|
58
|
+
public
|
|
59
|
+
onlyOwner
|
|
60
|
+
returns (string memory)
|
|
61
|
+
{
|
|
62
|
+
if (owner != _newOwner) {
|
|
53
63
|
owner = _newOwner;
|
|
54
64
|
emit TransferOwnership(owner);
|
|
55
65
|
return ("Ownership transferred successfully");
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
return ("New Owner address is equal to original owner address");
|
|
66
|
+
} else {
|
|
67
|
+
return ("Ownership cannot be transferred to the same account");
|
|
59
68
|
}
|
|
60
69
|
}
|
|
61
70
|
|
|
62
71
|
/**
|
|
63
72
|
*@dev Reads contract owner from chain
|
|
64
73
|
*/
|
|
65
|
-
|
|
74
|
+
|
|
75
|
+
function getOwner() public view returns (address _owner) {
|
|
66
76
|
return owner;
|
|
67
77
|
}
|
|
68
78
|
|
|
@@ -71,24 +81,77 @@ contract PolygonDidRegistry {
|
|
|
71
81
|
*@param _id - Address that will refer the DID doc
|
|
72
82
|
*@param _doc - A string object that holds the DID Doc
|
|
73
83
|
*/
|
|
84
|
+
|
|
74
85
|
function createDID(address _id, string memory _doc)
|
|
75
86
|
public
|
|
76
|
-
returns (
|
|
87
|
+
returns (
|
|
88
|
+
address controller,
|
|
89
|
+
uint256 created,
|
|
90
|
+
uint256 updated,
|
|
91
|
+
string memory didDoc
|
|
92
|
+
)
|
|
77
93
|
{
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
94
|
+
polyDIDs[_id].controller = msg.sender;
|
|
95
|
+
polyDIDs[_id].created = block.timestamp;
|
|
96
|
+
polyDIDs[_id].updated = block.timestamp;
|
|
97
|
+
polyDIDs[_id].didDoc = _doc;
|
|
98
|
+
activeDIDs[totalDIDs] = msg.sender;
|
|
99
|
+
activeAddress[_id] = totalDIDs;
|
|
100
|
+
++totalDIDs;
|
|
101
|
+
emit DIDCreated(_id, _doc);
|
|
102
|
+
return (
|
|
103
|
+
polyDIDs[_id].controller,
|
|
104
|
+
polyDIDs[_id].created,
|
|
105
|
+
polyDIDs[_id].updated,
|
|
106
|
+
polyDIDs[_id].didDoc
|
|
107
|
+
);
|
|
84
108
|
}
|
|
85
109
|
|
|
86
110
|
/**
|
|
87
111
|
*@dev Reads DID Doc from Chain
|
|
88
112
|
*@param _id - Address that refers to the DID doc position
|
|
89
113
|
*/
|
|
90
|
-
|
|
91
|
-
|
|
114
|
+
|
|
115
|
+
function getDIDDoc(address _id) public view returns (string memory) {
|
|
116
|
+
return polyDIDs[_id].didDoc;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
*@dev Reads total number of DIDs and total number of active DIDs from Chain
|
|
121
|
+
*/
|
|
122
|
+
|
|
123
|
+
function getTotalNumberOfDIDs()
|
|
124
|
+
public
|
|
125
|
+
view
|
|
126
|
+
returns (uint256 _totalDIDs, uint256 _activeDIDs)
|
|
127
|
+
{
|
|
128
|
+
return (totalDIDs, (totalDIDs - deletedDID));
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
*@dev Reads total number of DIDs deleted from Chain
|
|
133
|
+
*/
|
|
134
|
+
|
|
135
|
+
function getTotalNumberOfDeletedDIDs()
|
|
136
|
+
public
|
|
137
|
+
view
|
|
138
|
+
returns (uint256 _deletedDID)
|
|
139
|
+
{
|
|
140
|
+
return deletedDID;
|
|
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(uint256 _index)
|
|
150
|
+
public
|
|
151
|
+
view
|
|
152
|
+
returns (string memory)
|
|
153
|
+
{
|
|
154
|
+
return polyDIDs[activeDIDs[_index]].didDoc;
|
|
92
155
|
}
|
|
93
156
|
|
|
94
157
|
/**
|
|
@@ -96,22 +159,37 @@ contract PolygonDidRegistry {
|
|
|
96
159
|
*@param _id - Address that refers to the DID doc
|
|
97
160
|
*@param _doc - A String that holds the DID doc
|
|
98
161
|
*/
|
|
99
|
-
|
|
162
|
+
|
|
163
|
+
function updateDIDDoc(address _id, string memory _doc)
|
|
100
164
|
public
|
|
101
|
-
onlyController(_id)
|
|
165
|
+
onlyController(_id)
|
|
166
|
+
returns (
|
|
167
|
+
address controller,
|
|
168
|
+
uint256 created,
|
|
169
|
+
uint256 updated,
|
|
170
|
+
string memory didDoc
|
|
171
|
+
)
|
|
102
172
|
{
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
emit
|
|
106
|
-
return (
|
|
173
|
+
polyDIDs[_id].didDoc = _doc;
|
|
174
|
+
polyDIDs[_id].updated = block.timestamp;
|
|
175
|
+
emit DIDUpdated(_id, _doc);
|
|
176
|
+
return (
|
|
177
|
+
polyDIDs[_id].controller,
|
|
178
|
+
polyDIDs[_id].created,
|
|
179
|
+
polyDIDs[_id].updated,
|
|
180
|
+
polyDIDs[_id].didDoc
|
|
181
|
+
);
|
|
107
182
|
}
|
|
108
183
|
|
|
109
184
|
/**
|
|
110
185
|
*@dev To delete a DID from chain
|
|
111
186
|
*@param _id - Address that refers to the DID doc that need to be deleted
|
|
112
187
|
*/
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
188
|
+
|
|
189
|
+
function deleteDIDDoc(address _id) public onlyController(_id) {
|
|
190
|
+
delete polyDIDs[_id];
|
|
191
|
+
delete activeDIDs[activeAddress[_id]];
|
|
192
|
+
++deletedDID;
|
|
193
|
+
emit DIDDeleted(_id);
|
|
116
194
|
}
|
|
117
195
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ayanworks/polygon-did-registry-contract",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.2",
|
|
4
4
|
"description": "Smart Contract to register DID on Polygon",
|
|
5
5
|
"main": "build/contracts/PolygonDidRegistry.json",
|
|
6
6
|
"directories": {
|
|
@@ -15,6 +15,8 @@
|
|
|
15
15
|
"dependencies": {
|
|
16
16
|
"@openzeppelin/contracts": "^4.7.0",
|
|
17
17
|
"@truffle/hdwallet-provider": "^2.0.10",
|
|
18
|
+
"dotenv": "^16.0.2",
|
|
19
|
+
"eslint": "^8.23.1",
|
|
18
20
|
"fs-extra": "8.1.0",
|
|
19
21
|
"ganache-cli": "6.12.2",
|
|
20
22
|
"mocha": "^7.0.0",
|
|
@@ -66,7 +66,21 @@ describe('Setting up the Upgradable Smart Contract', async () => {
|
|
|
66
66
|
});
|
|
67
67
|
});
|
|
68
68
|
|
|
69
|
-
//
|
|
69
|
+
//Describing the 1.2 Test Case
|
|
70
|
+
describe('Checking the getTotalNumberOfDIDs Function', async () => {
|
|
71
|
+
it('This should get correct number of DIDs registered', async () => {
|
|
72
|
+
|
|
73
|
+
const currentValue = await PolygonUpgradableInstance.getTotalNumberOfDIDs();
|
|
74
|
+
assert.equal(
|
|
75
|
+
currentValue,
|
|
76
|
+
1,
|
|
77
|
+
'value should be matching'
|
|
78
|
+
);
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
// Describing the 1.3 Test case
|
|
70
84
|
describe('Checking the Update function in Upgradable Contract', async () => {
|
|
71
85
|
|
|
72
86
|
it('This should update the DID doc on Upgradable Contract and will read and check if the value stored is correct', async () => {
|
|
@@ -89,7 +103,7 @@ describe('Setting up the Upgradable Smart Contract', async () => {
|
|
|
89
103
|
});
|
|
90
104
|
});
|
|
91
105
|
|
|
92
|
-
//Describing test 1.
|
|
106
|
+
//Describing test 1.4 to check if test fails if we update with different controller
|
|
93
107
|
describe('Checking update with wrong controller', async () => {
|
|
94
108
|
|
|
95
109
|
it('This should not update DID doc ', async () => {
|
|
@@ -106,7 +120,7 @@ describe('Setting up the Upgradable Smart Contract', async () => {
|
|
|
106
120
|
});
|
|
107
121
|
});
|
|
108
122
|
|
|
109
|
-
// Describing the 1.
|
|
123
|
+
// Describing the 1.5 Test case
|
|
110
124
|
describe('Checking the Delete function in Upgradable Contract', async () => {
|
|
111
125
|
|
|
112
126
|
it('This should delete the DID doc on Upgradable Contract and will read and check if the value is now an empty string', async () => {
|
|
@@ -127,7 +141,7 @@ describe('Setting up the Upgradable Smart Contract', async () => {
|
|
|
127
141
|
});
|
|
128
142
|
});
|
|
129
143
|
|
|
130
|
-
// Describing the 1.
|
|
144
|
+
// Describing the 1.6 Test case
|
|
131
145
|
describe('Checking the Create function in Upgradable Contract', async () => {
|
|
132
146
|
|
|
133
147
|
it('This should store the DID doc on Upgradable Contract and will read and check if the value stored is not null', async () => {
|
|
@@ -151,7 +165,7 @@ describe('Setting up the Upgradable Smart Contract', async () => {
|
|
|
151
165
|
});
|
|
152
166
|
});
|
|
153
167
|
|
|
154
|
-
// Describing the 1.
|
|
168
|
+
// Describing the 1.7 Test case
|
|
155
169
|
describe('Checking the Update function in Upgradable Contract', async () => {
|
|
156
170
|
|
|
157
171
|
it('This should update the DID doc on Upgradable Contract and will read and check if the value stored has changed', async () => {
|
|
@@ -176,7 +190,7 @@ describe('Setting up the Upgradable Smart Contract', async () => {
|
|
|
176
190
|
});
|
|
177
191
|
|
|
178
192
|
|
|
179
|
-
// Describing the 1.
|
|
193
|
+
// Describing the 1.8 Test Case
|
|
180
194
|
describe('Checking the Delete function in Upgradable Contract', async () => {
|
|
181
195
|
|
|
182
196
|
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 () => {
|
|
@@ -198,8 +212,22 @@ describe('Setting up the Upgradable Smart Contract', async () => {
|
|
|
198
212
|
});
|
|
199
213
|
});
|
|
200
214
|
|
|
215
|
+
//Describing the 1.9 Test Case
|
|
216
|
+
describe('Checking the getTotalNumberOfDIDs Function', async () => {
|
|
217
|
+
it('This should get correct number of DIDs registered', async () => {
|
|
218
|
+
|
|
219
|
+
const totalValue = await PolygonUpgradableInstance.getTotalNumberOfDIDs();
|
|
220
|
+
const deltedValue = await PolygonUpgradableInstance.getTotalNumberOfDeletedDIDs();
|
|
221
|
+
const currentValue = totalValue - deltedValue;
|
|
222
|
+
assert.equal(
|
|
223
|
+
currentValue,
|
|
224
|
+
0,
|
|
225
|
+
'value should be matching'
|
|
226
|
+
);
|
|
227
|
+
});
|
|
228
|
+
});
|
|
201
229
|
|
|
202
|
-
//Describing the 1.
|
|
230
|
+
//Describing the 1.10 Test Case
|
|
203
231
|
describe('Checking the Transafer Ownership Function', async () => {
|
|
204
232
|
it('This should transfer ownership of contract to another user', async () => {
|
|
205
233
|
|
|
@@ -216,7 +244,7 @@ describe('Setting up the Upgradable Smart Contract', async () => {
|
|
|
216
244
|
});
|
|
217
245
|
});
|
|
218
246
|
|
|
219
|
-
//Describing the 1.
|
|
247
|
+
//Describing the 1.11 Test Case
|
|
220
248
|
describe('Checking the Transafer Ownership Function', async () => {
|
|
221
249
|
it('This should not transfer ownership of contract to another user since the transaction is being initiated by different owner', async () => {
|
|
222
250
|
|
|
@@ -229,9 +257,7 @@ describe('Setting up the Upgradable Smart Contract', async () => {
|
|
|
229
257
|
}
|
|
230
258
|
});
|
|
231
259
|
});
|
|
232
|
-
|
|
233
|
-
})
|
|
234
|
-
|
|
260
|
+
})
|
|
235
261
|
|
|
236
262
|
|
|
237
263
|
// basic Contract Testing
|
package/truffle-config.js
CHANGED
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
*
|
|
19
19
|
*/
|
|
20
20
|
const HDWalletProvider = require("@truffle/hdwallet-provider");
|
|
21
|
-
|
|
21
|
+
require('dotenv').config()
|
|
22
22
|
|
|
23
23
|
module.exports = {
|
|
24
24
|
/**
|
|
@@ -39,13 +39,13 @@
|
|
|
39
39
|
},
|
|
40
40
|
matic: {
|
|
41
41
|
provider: function () {
|
|
42
|
-
return new HDWalletProvider(
|
|
42
|
+
return new HDWalletProvider(process.env.SIGNER, process.env.RPCURL);
|
|
43
43
|
},
|
|
44
44
|
network_id: '80001',
|
|
45
45
|
},
|
|
46
46
|
matic_mainnet: {
|
|
47
47
|
provider: function () {
|
|
48
|
-
return new HDWalletProvider(
|
|
48
|
+
return new HDWalletProvider(process.env.SIGNER, process.env.RPCURL);
|
|
49
49
|
},
|
|
50
50
|
network_id: '137',
|
|
51
51
|
}
|
|
@@ -59,7 +59,7 @@
|
|
|
59
59
|
// Configure your compilers
|
|
60
60
|
compilers: {
|
|
61
61
|
solc: {
|
|
62
|
-
version: "0.8.
|
|
62
|
+
version: "0.8.16", // Fetch exact version from solc-bin (default: truffle's version)
|
|
63
63
|
}
|
|
64
64
|
},
|
|
65
65
|
|