forked from autocontracts/lightstreams
-
Notifications
You must be signed in to change notification settings - Fork 8
/
permissioned.sol
217 lines (173 loc) · 4.87 KB
/
permissioned.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
contract Permissioned {
address public _creator;
address public _owner;
address public _counterparty;
address public _endorser;
string public _publicStateAddress;
string public _privateStateAddress;
string public _proposedStateAddress;
string public ownerDetailsAddress;
uint public price;
bool _stateInitialized;
bool _stateDirty;
bool _signed;
struct Permission {
bool exists;
bool read;
bool write;
string publicKey;
}
struct Purchase {
bool purchased;
uint price;
}
mapping(address => Permission) _permissions;
mapping(address => Purchase) _purchases;
event StateUpdated(string stateAddress);
function Permissioned(address owner,
address counterparty,
address endorser,
string ownerPublicKey) {
_creator = msg.sender;
_owner = owner;
_counterparty = counterparty;
_endorser = endorser;
_permissions[owner] = Permission({
exists: true,
read: true,
write: true,
publicKey: ownerPublicKey
});
}
function setOwnerDetailsAddress(string _ownerDetailsAddress) {
if ( msg.sender != _owner ) {
throw;
}
ownerDetailsAddress = _ownerDetailsAddress;
}
function initPrivateState(string stateAddress) {
if ( msg.sender != _owner ) {
throw;
}
if (_stateInitialized) {
throw;
}
_stateInitialized = true;
_privateStateAddress = stateAddress;
}
function setPublicState(string publicStateAddress) {
if( msg.sender != _owner ) {
throw;
}
_publicStateAddress = publicStateAddress;
}
function sign() {
if( msg.sender != _counterparty ) {
throw;
}
_stateInitialized = true;
_signed = true;
}
function updatePrivateStateAddress(string stateAddress) {
if( !_permissions[msg.sender].write ) {
throw;
}
_stateDirty = true;
_proposedStateAddress = stateAddress;
}
function endorse(string endorseStateAddress) {
if( msg.sender != _endorser ) {
throw;
}
if (!stringsEquals(_proposedStateAddress, endorseStateAddress)) {
throw;
}
_privateStateAddress = _proposedStateAddress;
_stateDirty = false;
StateUpdated(_privateStateAddress);
}
function setPrice(uint _price) {
if( msg.sender != _owner ) {
throw;
}
price = _price;
}
function purchaseContent(string publicKey) payable {
if (msg.value != price) {
throw;
}
_purchases[msg.sender] = Purchase({
purchased: true,
price: msg.value
});
_permissions[msg.sender] = Permission({
exists: true,
read: true,
write: false,
publicKey: publicKey
});
_owner.transfer(msg.value);
}
function hasPurchased(address account) returns (bool) {
return _purchases[account].purchased;
}
function getPublicKey(address account) returns (string) {
return _permissions[account].publicKey;
}
function grantRead(address account, string publicKey) {
if( msg.sender != _owner ) {
throw;
}
_permissions[account] = Permission({
exists: true,
read: true,
write: false,
publicKey: publicKey
});
}
function grantWrite(address account, string publicKey) {
if( msg.sender != _owner ) {
throw;
}
_permissions[account] = Permission({
exists: true,
read: true,
write: true,
publicKey: publicKey
});
}
function revoke(address account) {
if( msg.sender != _owner ) {
throw;
}
_permissions[account] = Permission({
exists: false,
read: false,
write: false,
publicKey: ""
});
}
function hasWrite(address account) returns (bool) {
if (_permissions[account].write) {
return true;
}
return false;
}
function hasRead(address account) returns (bool) {
if (_permissions[account].read) {
return true;
}
return false;
}
function stringsEquals(string storage _a, string memory _b) internal returns (bool) {
bytes storage a = bytes(_a);
bytes memory b = bytes(_b);
if (a.length != b.length)
return false;
for (uint i = 0; i < a.length; i ++) {
if (a[i] != b[i])
return false;
}
return true;
}
}