@ayanworks/polygon-did-registry-contract 3.0.0 → 4.0.0

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/build/index.d.ts DELETED
@@ -1,2 +0,0 @@
1
- import DidRegistryContract from './artifacts/PolygonDidRegistry.json';
2
- export default DidRegistryContract;
package/build/index.js DELETED
@@ -1,8 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const PolygonDidRegistry_json_1 = __importDefault(require("./artifacts/PolygonDidRegistry.json"));
7
- exports.default = PolygonDidRegistry_json_1.default;
8
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";;;;;AAAA,kGAAqE;AAErE,kBAAe,iCAAmB,CAAA"}
package/compile.ts DELETED
@@ -1,115 +0,0 @@
1
- import path from 'path'
2
- import fs from 'fs-extra'
3
- // @ts-ignore
4
- import solc from 'solc'
5
- import * as ethers from 'ethers'
6
-
7
- const sourceFolderPath = path.resolve(__dirname, 'contracts')
8
- const buildFolderPath = path.resolve(__dirname, 'artifacts')
9
- const lastSourceHashFilePath = path.resolve(__dirname, 'sst-config.json')
10
-
11
- const getContractSource = (contractFileName: string) => {
12
- const contractPath = path.resolve(__dirname, 'contracts', contractFileName)
13
- const source = fs.readFileSync(contractPath, 'utf8')
14
- return source
15
- }
16
-
17
- let sources = {}
18
-
19
- fs.readdirSync(sourceFolderPath).forEach((contractFileName: string) => {
20
- sources = {
21
- ...sources,
22
- [contractFileName]: {
23
- content: getContractSource(contractFileName),
24
- },
25
- }
26
- })
27
- // console.log(sources);
28
-
29
- function convertToHex(inputString: string) {
30
- let hex = ''
31
- for (let i = 0; i < inputString.length; i++) {
32
- hex += '' + inputString.charCodeAt(i).toString(16)
33
- }
34
- return hex
35
- }
36
-
37
- const sourceHash = ethers.sha256('0x' + convertToHex(JSON.stringify(sources)))
38
-
39
- console.log('\n'.repeat(process.stdout.rows))
40
-
41
- if (
42
- fs.existsSync(buildFolderPath) &&
43
- fs.existsSync(lastSourceHashFilePath) &&
44
- JSON.parse(fs.readFileSync(lastSourceHashFilePath, 'utf8')).sourceHash ===
45
- sourceHash
46
- ) {
47
- console.log(
48
- 'No changes in .sol files detected... \nSkiping compile script...\n',
49
- )
50
- } else {
51
- // write the source hash there at the end of
52
- // console.log(lastSourceHash,sourceHash);
53
-
54
- const input = {
55
- language: 'Solidity',
56
- sources,
57
- settings: {
58
- outputSelection: {
59
- '*': {
60
- '*': ['*'],
61
- },
62
- },
63
- },
64
- }
65
-
66
- console.log('Compiling contracts...')
67
- const output = JSON.parse(solc.compile(JSON.stringify(input))) as any
68
- console.log('Done')
69
-
70
- let shouldBuild = true
71
-
72
- if (output.errors) {
73
- // console.error(output.errors);
74
- for (const error of output.errors) {
75
- console.log('-'.repeat(process.stdout.columns))
76
- console.group(error.severity.toUpperCase())
77
- console.log(error.formattedMessage)
78
- console.groupEnd()
79
- }
80
- if (Object.values(output.errors).length)
81
- console.log('-'.repeat(process.stdout.columns))
82
- // throw '\nError in compilation please check the contract\n';
83
- for (const error of output.errors) {
84
- if (error.severity === 'error') {
85
- shouldBuild = false
86
- throw 'Error found\n'
87
- break
88
- }
89
- }
90
- }
91
-
92
- if (shouldBuild) {
93
- console.log('\nBuilding please wait...')
94
-
95
- fs.removeSync(buildFolderPath)
96
- fs.ensureDirSync(buildFolderPath)
97
-
98
- let i = 0
99
- for (const contractFile in output.contracts) {
100
- for (const key in output.contracts[contractFile]) {
101
- console.log(key, path.resolve(buildFolderPath, `${key}.json`))
102
- fs.outputJsonSync(
103
- path.resolve(buildFolderPath, `${key}.json`),
104
- output.contracts[contractFile][key],
105
- )
106
- }
107
- i++
108
- }
109
- console.log('Build finished successfully!\n')
110
- } else {
111
- console.log('\nBuild failed\n')
112
- }
113
-
114
- fs.outputJsonSync(path.resolve(lastSourceHashFilePath), { sourceHash })
115
- }
@@ -1,220 +0,0 @@
1
- //SPDX-License-Identifier: Apache 2.0
2
- pragma solidity 0.8.16;
3
-
4
- /**
5
- *@title PolygonDidRegistry
6
- *@dev Smart Contract for Polygon DID Method
7
- */
8
- contract PolygonDidRegistry {
9
- address owner;
10
- struct PolyDID {
11
- address controller;
12
- uint256 created;
13
- uint256 updated;
14
- string didDoc;
15
- }
16
-
17
- mapping(address => PolyDID) polyDIDs;
18
- mapping(address => mapping(string => string)) resourceData;
19
- mapping(address => string[]) private keysById;
20
- event DIDCreated(address id, string doc);
21
- event DIDUpdated(address id, string doc);
22
- event TransferOwnership(address newOwner);
23
- event ResourceAdded(
24
- address _id,
25
- string _resourceId,
26
- string _resourcePayload
27
- );
28
-
29
- bool private initialized;
30
-
31
- /**
32
- *@dev modifiers for access control
33
- **/
34
-
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
- }
42
-
43
- modifier nonReentrant() {
44
- require(!initialized, 'Contract instance has already been initialized');
45
- initialized = true;
46
- _;
47
- }
48
-
49
- modifier onlyOwner() {
50
- require(msg.sender == owner, 'message sender is not the owner');
51
- _;
52
- }
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
-
63
- /**
64
- *@dev transfer the ownership of contract
65
- *@param _newOwner - Address of the new owner to whom the ownership needs to be passed
66
- **/
67
-
68
- function transferOwnership(
69
- address _newOwner
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');
79
- }
80
-
81
- /**
82
- *@dev Reads contract owner from chain
83
- */
84
-
85
- function getOwner() public view returns (address _owner) {
86
- return owner;
87
- }
88
-
89
- /**
90
- *@dev Register a new DID
91
- *@param _id - Address that will refer the DID doc
92
- *@param _doc - A string object that holds the DID Doc
93
- */
94
-
95
- function createDID(
96
- address _id,
97
- string memory _doc
98
- )
99
- external
100
- returns (
101
- address controller,
102
- uint256 created,
103
- uint256 updated,
104
- string memory didDoc
105
- )
106
- {
107
- require(_id != address(0), 'Invalid address provided');
108
- require(polyDIDs[_id].controller != _id, 'DID already exist');
109
- polyDIDs[_id].controller = _id;
110
- polyDIDs[_id].created = block.timestamp;
111
- polyDIDs[_id].updated = block.timestamp;
112
- polyDIDs[_id].didDoc = _doc;
113
- emit DIDCreated(_id, _doc);
114
- return (
115
- polyDIDs[_id].controller,
116
- polyDIDs[_id].created,
117
- polyDIDs[_id].updated,
118
- polyDIDs[_id].didDoc
119
- );
120
- }
121
-
122
- /**
123
- *@dev Reads DID Doc from Chain
124
- *@param _id - Address that refers to the DID doc position
125
- */
126
-
127
- function getDIDDoc(
128
- address _id
129
- ) public view returns (string memory, string[] memory) {
130
- string[] memory result = new string[](keysById[_id].length);
131
-
132
- for (uint256 i = 0; i < keysById[_id].length; i++) {
133
- result[i] = resourceData[_id][keysById[_id][i]];
134
- }
135
- return (polyDIDs[_id].didDoc, result);
136
- }
137
-
138
- /**
139
- *@dev To Update the DID doc
140
- *@param _id - Address that refers to the DID doc
141
- *@param _doc - A String that holds the DID doc
142
- */
143
-
144
- function updateDIDDoc(
145
- address _id,
146
- string memory _doc
147
- )
148
- external
149
- onlyController(_id)
150
- returns (
151
- address controller,
152
- uint256 created,
153
- uint256 updated,
154
- string memory didDoc
155
- )
156
- {
157
- polyDIDs[_id].didDoc = _doc;
158
- polyDIDs[_id].updated = block.timestamp;
159
- emit DIDUpdated(_id, _doc);
160
- return (
161
- polyDIDs[_id].controller,
162
- polyDIDs[_id].created,
163
- polyDIDs[_id].updated,
164
- polyDIDs[_id].didDoc
165
- );
166
- }
167
-
168
- /**
169
- *@dev To add linked resource in the DID doc
170
- *@param _id - Address that refers to the DID doc
171
- *@param _resourceId - Id that refers to the resource
172
- */
173
- function addResource(
174
- address _id,
175
- string memory _resourceId,
176
- string memory _resourcePayload
177
- )
178
- external
179
- onlyController(_id)
180
- returns (address, string memory, string memory)
181
- {
182
- require(_id != address(0), 'Invalid address provided');
183
- if (bytes(resourceData[_id][_resourceId]).length == 0) {
184
- keysById[_id].push(_resourceId);
185
- }
186
- resourceData[_id][_resourceId] = _resourcePayload;
187
- emit ResourceAdded(_id, _resourceId, _resourcePayload);
188
- return (_id, _resourceId, _resourcePayload);
189
- }
190
-
191
- /**
192
- *@dev Reads DID linked resource from Chain
193
- *@param _id - Address that refers to the DID doc
194
- *@param _resourceId - Id that refers to a specific resource
195
- */
196
-
197
- function getResource(
198
- address _id,
199
- string memory _resourceId
200
- ) public view returns (string memory) {
201
- return resourceData[_id][_resourceId];
202
- }
203
-
204
- /**
205
- *@dev Reads all DID linked resource for a specific DID from Chain
206
- *@param _id - Address that refers to the DID doc
207
- */
208
-
209
- function getAllResources(
210
- address _id
211
- ) public view returns (string[] memory) {
212
- string[] memory result = new string[](keysById[_id].length);
213
-
214
- for (uint256 i = 0; i < keysById[_id].length; i++) {
215
- result[i] = resourceData[_id][keysById[_id][i]];
216
- }
217
-
218
- return result;
219
- }
220
- }
package/hardhat.config.js DELETED
@@ -1,39 +0,0 @@
1
- /**
2
- * @type import('hardhat/config').HardhatUserConfig
3
- */
4
-
5
- // import 'hardhat';
6
- require('@nomicfoundation/hardhat-ethers')
7
- require('@openzeppelin/hardhat-upgrades')
8
- require('@nomicfoundation/hardhat-verify')
9
-
10
- require('dotenv/config')
11
-
12
- module.exports = {
13
- solidity: {
14
- version: '0.8.16',
15
- settings: {
16
- optimizer: {
17
- enabled: true,
18
- runs: 200,
19
- },
20
- },
21
- },
22
- networks: {
23
- hardhat: {},
24
- amoy: {
25
- url: process.env.AMOY_RPCURL,
26
- accounts: [`0x${process.env.SIGNER_TESTNET}`],
27
- },
28
- //Use for mainnet deployment
29
- // polygon: {
30
- // url: process.env.MAINNET_RPCURL,
31
- // accounts: [`0x${process.env.SIGNER_MAINNET}`],
32
- // },
33
- },
34
- etherscan: {
35
- apiKey: {
36
- polygonMumbai: process.env.VERIFICATION_KEY,
37
- },
38
- },
39
- }
package/index.ts DELETED
@@ -1,3 +0,0 @@
1
- import DidRegistryContract from './artifacts/PolygonDidRegistry.json'
2
-
3
- export default DidRegistryContract
package/scripts/deploy.js DELETED
@@ -1,31 +0,0 @@
1
- const { ethers, upgrades } = require('hardhat')
2
-
3
- async function main() {
4
- try {
5
- console.log('Deploying the smart contract...')
6
- const PolygonDidRegistry =
7
- await ethers.getContractFactory('PolygonDidRegistry')
8
-
9
- // To deploy the initial upgradable smart contract
10
-
11
- const contract = await upgrades.deployProxy(PolygonDidRegistry, {
12
- initializer: 'initialize',
13
- })
14
-
15
- // const contract = await upgrades.upgradeProxy(
16
- // process.env.CONTRACT_ADDRESS,
17
- // PolygonDidRegistry,
18
- // {
19
- // initializer: 'initialize',
20
- // },
21
- // )
22
-
23
- await contract.waitForDeployment()
24
-
25
- console.log('Contract address::', contract.target)
26
- } catch (error) {
27
- console.error('Error deploying contract:', error.message)
28
- }
29
- }
30
-
31
- main()
package/sst-config.json DELETED
@@ -1 +0,0 @@
1
- {"sourceHash":"0x97a599fbc455201c7d5253480af7bf0b22264a53b3c5933af397217c88ba64d4"}
@@ -1,117 +0,0 @@
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_TESTNET
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
- })
package/tsconfig.json DELETED
@@ -1,14 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "outDir": "build",
4
- "target": "es2016",
5
- "module": "commonjs",
6
- "esModuleInterop": true,
7
- "forceConsistentCasingInFileNames": true,
8
- "strict": true,
9
- "declaration": true,
10
- "sourceMap": true,
11
- "resolveJsonModule": true
12
- },
13
- "exclude": ["tests", "compile.ts"]
14
- }