/
DBNS.sol
198 lines (173 loc) · 5.62 KB
/
DBNS.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import {Core} from "./libraries/Core.sol";
/**
* @title DBNS
* @dev Create a decentralized Database NameSpaces
* Where space and subSpaces can get created and anyone can
* create instances inside a space and contribute to the public
* this is possible by integrating tableland SQL in solidity
* FNS system to support a decentralized Namespace of Database spaces
* IPNS and Push protocol for code and space discussions
*/
contract DBNS is Core {
constructor(
address _registry,
address _registrar,
address _publicResolver,
bytes32 _baseNode,
address _gateImplementation,
address _subscriptionImplementation
)
Core(
_registry,
_registrar,
_publicResolver,
_baseNode,
_gateImplementation,
_subscriptionImplementation
)
{}
/**
* @dev Create a new space under the given node
* @param _name The name of the new space
*/
function createDBSpace(string calldata _name) public {
bytes32 _newDBSpace = createSubNode(DBNS_NODE, _name);
isType[_newDBSpace] = Types.SUBNODE;
spaceInsertion(_newDBSpace, DBNS_NODE, _name);
}
/**
* @dev Create a new subnode under the given node
* @param _DBSpace The parent node
* @param _name The name of the new subnode
*/
function createDBSubSpace(
bytes32 _DBSpace,
string calldata _name
) external {
require(
isType[_DBSpace] == Types.SUBNODE,
"DBNS: Node is not a subnode"
);
bytes32 _newDBSubSpace = createSubNode(_DBSpace, _name);
isType[_newDBSubSpace] = Types.SUBNODE;
spaceInsertion(_newDBSubSpace, _DBSpace, _name);
}
/**
* @dev Create a new instance under the given node
* @param _node The parent node
* @param _members The hatID of the new instance
* @param _metadataCID The name of the new instance
* @param _chatID The chatID of the new instance
* @param _IPNS The IPNS of the new instance
*/
function createSpaceInstance(
bytes32 _node,
uint256 _price,
address[] calldata _members,
string calldata _metadataCID,
string calldata _chatID,
string calldata _IPNS,
string calldata _IPNSEncryptedKey
) external {
require(isType[_node] == Types.SUBNODE, "DBNS: Node is not a subnode");
bytes32 _newDBInstance = keccak256(abi.encodePacked(_node, _IPNS));
if (isType[_newDBInstance] != Types.NULL) {
revert InstanceAlreadyExists();
}
address _gatedContract;
if (_members.length > 0) {
_gatedContract = createGatedContract(_members, _newDBInstance);
_insertMembers(_newDBInstance, _members);
}
instances[_newDBInstance] = SpaceInstance(
_gatedContract,
_price,
msg.sender
);
createInstanceType(_newDBInstance, _gatedContract, _price);
instanceInsertion(
_newDBInstance,
uint8(isType[_newDBInstance]),
_node,
_gatedContract,
_price,
_metadataCID,
_chatID,
_IPNS,
_IPNSEncryptedKey
);
}
/**
* @dev Create a new instance under the given node
* @param _instance The parent node
* @param _name The name of the new instance
* @param _about The about of the new instance
* @param _chatID The chatID of the new instance
* @param _codeIPNS The IPNS of the new instance
*/
function createInstanceCode(
bytes32 _instance,
string calldata _name,
string calldata _about,
string calldata _chatID,
string calldata _codeIPNS,
string calldata _IPNSEncryptedKey
) external {
if (!hasMutateAccess(_instance, msg.sender)) {
revert NoInstanceAccess();
}
bytes32 _newDBInstanceCode = keccak256(
abi.encodePacked(_instance, _codeIPNS)
);
if (isType[_newDBInstanceCode] != Types.NULL) {
revert InstanceAlreadyExists();
}
codeOwner[_newDBInstanceCode] = msg.sender;
isType[_newDBInstanceCode] = Types.CODE;
InsertInstanceCode(
_instance,
_newDBInstanceCode,
_name,
_about,
_chatID,
_codeIPNS,
_IPNSEncryptedKey
);
}
function purchaseInstanceSubscription(
bytes32 _instanceID
) external payable {
purchaseSubscription(_instanceID);
insertSubscription(_instanceID, msg.sender, getTime() + MONTH);
}
function extendInstanceSubscription(bytes32 _instanceID) external payable {
uint256 remaining = getRemainingSubscriptionTime(
_instanceID,
msg.sender
);
extendSubscription(_instanceID);
updateSubscription(_instanceID, msg.sender, remaining + MONTH);
}
function updateCode(
bytes32 _codeID,
string calldata _name,
string calldata _about
) external {
if (codeOwner[_codeID] != msg.sender) {
revert NoCodeOwner();
}
updateInstanceCode(_codeID, _name, _about);
}
function updateInstance(
bytes32 _instanceID,
string calldata _metadataCID
) external {
if (instances[_instanceID].creator != msg.sender) {
// revert NoInstanceAccess();
revert("DBNS: No instance access");
}
updateInstanceMetadata(_instanceID, _metadataCID);
}
}