-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdeweb-interface.ts
More file actions
348 lines (286 loc) · 11 KB
/
deweb-interface.ts
File metadata and controls
348 lines (286 loc) · 11 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import {
balance,
Context,
generateEvent,
setBytecode,
Storage,
transferCoins,
} from '@massalabs/massa-as-sdk';
import {
_onlyOwner,
_setOwner,
} from '@massalabs/sc-standards/assembly/contracts/utils/ownership-internal';
import { Args, stringToBytes } from '@massalabs/as-types';
import { FileChunkPost } from './serializable/FileChunkPost';
import { FileDelete } from './serializable/FileDelete';
import { Metadata } from './serializable/Metadata';
import {
_editFileMetadata,
_editGlobalMetadata,
_removeFileMetadata,
_removeGlobalMetadata,
} from './internals/metadata';
import {
_setFileChunk,
_getTotalChunk,
_deleteFile,
_removeChunksRange,
} from './internals/chunks';
import { _fileInit } from './internals/fileInit';
import { FileInit } from './serializable/FileInit';
import { DEWEB_VERSION_TAG } from './internals/storageKeys/tags';
export { setOwner } from '@massalabs/sc-standards/assembly/contracts/utils/ownership';
const DEWEB_VERSION = '2';
/**
* Initializes the smart contract.
* Sets the contract deployer as the owner.
*/
export function constructor(_: StaticArray<u8>): void {
if (!Context.isDeployingContract()) return;
Storage.set(DEWEB_VERSION_TAG, stringToBytes(DEWEB_VERSION));
_setOwner(Context.caller().toString());
}
/* -------------------------------------------------------------------------- */
/* FILE INITIALIZATION */
/* -------------------------------------------------------------------------- */
/**
* Initializes the contract storage with the given files and metadata.
* @param _binaryArgs - Serialized arguments containing arrays of FileInit objects,
* files to delete, and global metadata.
* @throws If the files are invalid or if the caller is not the owner.
*/
export function filesInit(_binaryArgs: StaticArray<u8>): void {
_onlyOwner();
const args = new Args(_binaryArgs);
const files = args
.nextSerializableObjectArray<FileInit>()
.expect('Invalid files to initialize');
const filesToDelete = args
.nextSerializableObjectArray<FileDelete>()
.expect('Invalid files to delete');
const globalMetadata = args
.nextSerializableObjectArray<Metadata>()
.expect('Invalid global metadata');
const globalMetadataToDelete = args
.nextSerializableObjectArray<Metadata>()
.expect('Invalid global metadata to delete');
for (let i = 0; i < globalMetadataToDelete.length; i++) {
_removeGlobalMetadata(stringToBytes(globalMetadataToDelete[i].key));
}
for (let i = 0; i < filesToDelete.length; i++) {
_deleteFile(filesToDelete[i].hashLocation);
}
for (let i = 0; i < globalMetadata.length; i++) {
_editGlobalMetadata(
stringToBytes(globalMetadata[i].key),
stringToBytes(globalMetadata[i].value),
);
}
for (let i = 0; i < files.length; i++) {
_fileInit(files[i].location, files[i].totalChunk, files[i].metadata);
}
// Send the freed coins back to the caller
transferCoins(Context.caller(), balance());
}
/* -------------------------------------------------------------------------- */
/* FILES */
/* -------------------------------------------------------------------------- */
/**
* Uploads chunks of a file to the contract storage.
* Only the contract owner can call this function.
* @param _binaryArgs - Serialized arguments containing an array of FileChunkPost objects.
* @throws If the chunks are invalid or if the caller is not the owner.
*/
export function uploadFileChunks(_binaryArgs: StaticArray<u8>): void {
_onlyOwner();
const args = new Args(_binaryArgs);
const chunks = args
.nextSerializableObjectArray<FileChunkPost>()
.expect('Invalid chunks');
for (let i = 0; i < chunks.length; i++) {
_setFileChunk(chunks[i].location, chunks[i].index, chunks[i].data);
}
// Send the freed coins back to the caller
transferCoins(Context.caller(), balance());
}
export function removeFileChunkRange(_binaryArgs: StaticArray<u8>): void {
_onlyOwner();
const args = new Args(_binaryArgs);
const hashLocation = args
.next<StaticArray<u8>>()
.expect('Invalid hashLocation');
const start = args.next<u32>().expect('Invalid start');
const end = args.next<u32>().expect('Invalid end');
_removeChunksRange(hashLocation, start, end);
// Send the freed coins back to the caller
transferCoins(Context.caller(), balance());
}
/**
* Deletes files from the contract storage.
* Only the contract owner can call this function.
* @param _binaryArgs - Serialized arguments containing an array of FileDelete objects.
* @throws If the files are invalid or if the caller is not the owner.
*/
export function deleteFiles(_binaryArgs: StaticArray<u8>): void {
_onlyOwner();
const args = new Args(_binaryArgs);
const files = args
.nextSerializableObjectArray<FileDelete>()
.expect('Invalid files');
for (let i = 0; i < files.length; i++) {
assert(_getTotalChunk(files[i].hashLocation) > 0, 'File does not exist');
_deleteFile(files[i].hashLocation);
}
// Send the freed coins back to the caller
transferCoins(Context.caller(), balance());
}
/* -------------------------------------------------------------------------- */
/* GLOBAL METADATA */
/* -------------------------------------------------------------------------- */
/**
* Sets the global metadata of the contract.
* Only the contract owner can call this function.
* @param _binaryArgs - Serialized arguments containing an array of Metadata objects.
* @throws If the caller is not the owner.
*/
export function setMetadataGlobal(_binaryArgs: StaticArray<u8>): void {
_onlyOwner();
const args = new Args(_binaryArgs);
const metadata = args
.nextSerializableObjectArray<Metadata>()
.expect('Invalid metadata');
for (let i = 0; i < metadata.length; i++) {
_editGlobalMetadata(
stringToBytes(metadata[i].key),
stringToBytes(metadata[i].value),
);
}
// Send the freed coins back to the caller
transferCoins(Context.caller(), balance());
}
/**
* Removes specific global metadata entries.
* Only the contract owner can call this function.
* @param _binaryArgs - Serialized arguments containing an array of metadata keys to remove.
* @throws If the caller is not the owner or if the metadata request is invalid.
*/
export function removeMetadataGlobal(_binaryArgs: StaticArray<u8>): void {
_onlyOwner();
const args = new Args(_binaryArgs);
const key = args.next<string[]>().expect('Invalid key');
for (let i = 0; i < key.length; i++) {
_removeGlobalMetadata(stringToBytes(key[i]));
}
// Send the freed coins back to the caller
transferCoins(Context.caller(), balance());
}
/* -------------------------------------------------------------------------- */
/* FILE METADATA */
/* -------------------------------------------------------------------------- */
/**
* Sets the metadata for a specific file.
* Only the contract owner can call this function.
* @param _binaryArgs - Serialized arguments containing the file's hashLocation and an array of Metadata objects.
* @throws If the caller is not the owner.
*/
export function setMetadataFile(_binaryArgs: StaticArray<u8>): void {
_onlyOwner();
const args = new Args(_binaryArgs);
const hashLocation = args
.next<StaticArray<u8>>()
.expect('Invalid hashLocation');
assert(
hashLocation.length == 32,
'Invalid filpath sha256 hash. should be 32 bytes',
);
const metadata = args
.nextSerializableObjectArray<Metadata>()
.expect('Invalid metadata');
for (let i = 0; i < metadata.length; i++) {
_editFileMetadata(
stringToBytes(metadata[i].key),
stringToBytes(metadata[i].value),
hashLocation,
);
}
// Send the freed coins back to the caller
transferCoins(Context.caller(), balance());
}
/**
* Removes specific metadata entries for a file.
* Only the contract owner can call this function.
* @param _binaryArgs - Serialized arguments containing the file's hashLocation and an array of MetadataDelete objects.
* @throws If the caller is not the owner.
*/
export function removeMetadataFile(_binaryArgs: StaticArray<u8>): void {
_onlyOwner();
const args = new Args(_binaryArgs);
const hashLocation = args
.next<StaticArray<u8>>()
.expect('Invalid hashLocation');
assert(
hashLocation.length == 32,
'Invalid filpath sha256 hash. should be 32 bytes',
);
const metadata = args.next<string[]>().expect('Invalid key');
for (let i = 0; i < metadata.length; i++) {
_removeFileMetadata(stringToBytes(metadata[i]), hashLocation);
}
// Send the freed coins back to the caller
transferCoins(Context.caller(), balance());
}
/* -------------------------------------------------------------------------- */
/* COINS MANAGEMENT */
/* -------------------------------------------------------------------------- */
/**
* Allows the owner to withdraw funds from the contract balance.
* Only the contract owner can call this function.
* @param binaryArgs - Serialized amount to withdraw.
* @throws If the caller is not the owner, the amount is invalid, or the contract has insufficient balance.
*/
export function withdrawCoins(binaryArgs: StaticArray<u8>): void {
_onlyOwner();
const args = new Args(binaryArgs);
const amount = args.next<u64>().expect('Invalid amount');
assert(amount > 0, 'Invalid amount');
assert(balance() >= amount, 'Contract has insufficient balance');
transferCoins(Context.caller(), amount);
}
export function receiveCoins(): void {
generateEvent('CoinsReceived: ' + Context.transferredCoins().toString());
}
/* -------------------------------------------------------------------------- */
/* UPGRADE SC */
/* -------------------------------------------------------------------------- */
/**
* Upgrades the smart contract bytecode.
* Only the contract owner can call this function.
* @param args - The new bytecode to set.
* @throws If the caller is not the owner.
*/
export function upgradeSC(args: StaticArray<u8>): void {
_onlyOwner();
setBytecode(args);
// Send the freed coins back to the caller
transferCoins(Context.caller(), balance());
}
/* -------------------------------------------------------------------------- */
/* PURGE SC */
/* -------------------------------------------------------------------------- */
/**
* Deletes all the contract storage and bytecode.
* Sends back the freed coins to the caller.
* @throws If the caller is not the owner.
*/
export function purge(_: StaticArray<u8>): void {
_onlyOwner();
// Delete all datastore entries
const keys = Storage.getKeys([]);
for (let i: u32 = 0; i < u32(keys.length); i++) {
Storage.del(keys[i]);
}
// Empty the bytecode
setBytecode([]);
// Send the freed coins back to the caller
transferCoins(Context.caller(), balance());
}