1 | 'use strict'
|
2 |
|
3 | import {jsonStableSerialize} from './util';
|
4 |
|
5 | import {
|
6 | MUTABLE_DATUM_DIR_TYPE,
|
7 | MUTABLE_DATUM_FILE_TYPE,
|
8 | MUTABLE_DATUM_INODE_HEADER_SCHEMA,
|
9 | URI_RECORD_SCHEMA,
|
10 | MUTABLE_DATUM_DIR_IDATA_SCHEMA,
|
11 | } from './schemas';
|
12 |
|
13 | const assert = require('assert');
|
14 | const crypto = require('crypto');
|
15 | const EC = require('elliptic').ec;
|
16 | const ec = EC('secp256k1');
|
17 | const Ajv = require('ajv');
|
18 |
|
19 | const BLOCKSTACK_STORAGE_PROTO_VERSION = 1;
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | export function hashDataPayload( payload_buffer ) {
|
30 | const hash = crypto.createHash('sha256');
|
31 |
|
32 | hash.update(`${payload_buffer.length}:`);
|
33 | hash.update(payload_buffer);
|
34 | hash.update(',');
|
35 |
|
36 | return hash.digest('hex');
|
37 | }
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | export function hashRawData( payload_buffer ) {
|
46 | const hash = crypto.createHash('sha256');
|
47 |
|
48 | hash.update(payload_buffer);
|
49 |
|
50 | return hash.digest('hex');
|
51 | }
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | function decodeHexString( hex ) {
|
62 | const bytes = [];
|
63 | for(let i=0; i< hex.length-1; i+=2) {
|
64 | bytes.push(parseInt(hex.substr(i, 2), 16));
|
65 | }
|
66 | return Buffer.from(bytes)
|
67 | }
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 | export function decodePrivateKey( privatekey_hex ) {
|
79 | if( privatekey_hex.length === 66 && privatekey_hex.slice(64, 66) === '01' ) {
|
80 |
|
81 | privatekey_hex = privatekey_hex.slice(0, 64);
|
82 | }
|
83 | return decodeHexString(privatekey_hex);
|
84 | }
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 | export function signRawData( payload_buffer, privkey_hex, hash ) {
|
97 |
|
98 | const privkey = decodePrivateKey(privkey_hex);
|
99 |
|
100 | if( !hash ) {
|
101 | hash = hashRawData(payload_buffer);
|
102 | }
|
103 |
|
104 | const sig = ec.sign(hash, privkey, {canonical: true});
|
105 |
|
106 |
|
107 | let r_array = sig.r.toArray();
|
108 | let s_array = sig.s.toArray();
|
109 | let r_buf = Buffer.from(r_array).toString('hex');
|
110 | let s_buf = Buffer.from(s_array).toString('hex');
|
111 |
|
112 | if(r_buf.length < 64) {
|
113 | while(r_buf.length < 64) {
|
114 | r_buf = "0" + r_buf;
|
115 | }
|
116 | }
|
117 |
|
118 | if( s_buf.length < 64) {
|
119 | while(s_buf.length < 64) {
|
120 | s_buf = "0" + s_buf;
|
121 | }
|
122 | }
|
123 |
|
124 | const sig_buf_hex = r_buf + s_buf;
|
125 |
|
126 | assert(sig_buf_hex.length == 128);
|
127 |
|
128 | const sigb64 = Buffer.from(sig_buf_hex, 'hex').toString('base64');
|
129 | return sigb64;
|
130 | }
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 | export function signDataPayload( payload_string, privkey_hex ) {
|
143 | return signRawData( Buffer.concat( [Buffer.from(`${payload_string.length}:`), Buffer.from(payload_string), Buffer.from(',')] ), privkey_hex );
|
144 | }
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 | export function makeFullyQualifiedDataId( device_id, data_id ) {
|
157 | return escape(`${device_id}:${data_id}`.replace('/', '\\x2f'));
|
158 | }
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 | export function makeMutableDataInfo( data_id, data_payload, device_id, version ) {
|
172 | const fq_data_id = makeFullyQualifiedDataId( device_id, data_id );
|
173 | const timestamp = new Date().getTime();
|
174 |
|
175 | const ret = {
|
176 | 'fq_data_id': fq_data_id,
|
177 | 'data': data_payload,
|
178 | 'version': version,
|
179 | 'timestamp': timestamp,
|
180 | };
|
181 |
|
182 | return ret
|
183 | }
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 | export function makeDataTombstone( tombstone_payload ) {
|
194 | const now = parseInt(new Date().getTime() / 1000);
|
195 | return `delete-${now}:${tombstone_payload}`;
|
196 | }
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 | export function makeMutableDataTombstones( device_ids, data_id ) {
|
208 | const ts = [];
|
209 | for (let device_id of device_ids) {
|
210 | ts.push( makeDataTombstone( makeFullyQualifiedDataId(device_id, data_id) ));
|
211 | }
|
212 | return ts;
|
213 | }
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 | export function makeInodeTombstones( datastore_id, inode_uuid, device_ids ) {
|
226 | assert(device_ids.length > 0);
|
227 |
|
228 | const header_id = `${datastore_id}.${inode_uuid}.hdr`;
|
229 | const header_tombstones = makeMutableDataTombstones( device_ids, header_id );
|
230 |
|
231 | const idata_id = `${datastore_id}.${inode_uuid}`;
|
232 | const idata_tombstones = makeMutableDataTombstones( device_ids, idata_id );
|
233 |
|
234 | return header_tombstones.concat(idata_tombstones);
|
235 | }
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 | export function signDataTombstone( tombstone, privkey ) {
|
247 | const sigb64 = signRawData( tombstone, privkey );
|
248 | return `${tombstone}:${sigb64}`;
|
249 | }
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 | export function signMutableDataTombstones( tombstones, privkey ) {
|
261 | const sts = [];
|
262 | for (let ts of tombstones) {
|
263 | sts.push( signDataTombstone(ts, privkey) );
|
264 | };
|
265 | return sts;
|
266 | }
|
267 |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 |
|
275 |
|
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 |
|
282 | export function makeInodeHeaderBlob( datastore_id, inode_type, owner_id, inode_uuid, data_hash, device_id, version ) {
|
283 |
|
284 | const header = {
|
285 | 'type': inode_type,
|
286 | 'owner': owner_id,
|
287 | 'uuid': inode_uuid,
|
288 | 'readers': [],
|
289 | 'data_hash': data_hash,
|
290 | 'version': version,
|
291 | 'proto_version': BLOCKSTACK_STORAGE_PROTO_VERSION,
|
292 | };
|
293 |
|
294 | let valid = null;
|
295 | const ajv = new Ajv();
|
296 | try {
|
297 | valid = ajv.validate(MUTABLE_DATUM_INODE_HEADER_SCHEMA, header);
|
298 | assert(valid);
|
299 | }
|
300 | catch(e) {
|
301 | console.log('header: ' + JSON.stringify(header));
|
302 | console.log('schema: ' + JSON.stringify(MUTABLE_DATUM_INODE_HEADER_SCHEMA));
|
303 | console.log(e.stack);
|
304 | throw e;
|
305 | }
|
306 |
|
307 | const inode_data_id = `${datastore_id}.${inode_uuid}.hdr`;
|
308 | const inode_data_payload = jsonStableSerialize(header);
|
309 | const inode_header_blob = makeMutableDataInfo( inode_data_id, inode_data_payload, device_id, version );
|
310 | return jsonStableSerialize(inode_header_blob);
|
311 | }
|
312 |
|
313 |
|
314 |
|
315 |
|
316 |
|
317 |
|
318 |
|
319 |
|
320 |
|
321 |
|
322 |
|
323 |
|
324 |
|
325 | export function makeDirInodeBlob( datastore_id, owner_id, inode_uuid, dir_listing, device_id, version ) {
|
326 |
|
327 | const ajv = new Ajv();
|
328 | let valid = null;
|
329 | try {
|
330 | valid = ajv.validate(MUTABLE_DATUM_DIR_IDATA_SCHEMA.properties.children, dir_listing);
|
331 | assert(valid);
|
332 | }
|
333 | catch(e) {
|
334 | console.log('dir listing: ' + JSON.stringify(dir_listing));
|
335 | console.log('schema: ' + JSON.stringify(MUTABLE_DATUM_DIR_IDATA_SCHEMA));
|
336 | throw e;
|
337 | }
|
338 |
|
339 | if(!version) {
|
340 | version = 1;
|
341 | }
|
342 |
|
343 | const empty_hash = '0000000000000000000000000000000000000000000000000000000000000000';
|
344 | const internal_header_blob = makeInodeHeaderBlob( datastore_id, MUTABLE_DATUM_DIR_TYPE, owner_id, inode_uuid, empty_hash, device_id, version );
|
345 |
|
346 |
|
347 | const internal_header = JSON.parse( JSON.parse(internal_header_blob).data );
|
348 | const idata_payload = {
|
349 | children: dir_listing,
|
350 | header: internal_header,
|
351 | };
|
352 |
|
353 | const idata_payload_str = jsonStableSerialize(idata_payload);
|
354 | const idata_hash = hashDataPayload(idata_payload_str);
|
355 |
|
356 | const header_blob = makeInodeHeaderBlob( datastore_id, MUTABLE_DATUM_DIR_TYPE, owner_id, inode_uuid, idata_hash, device_id, version );
|
357 | return {'header': header_blob, 'idata': idata_payload_str};
|
358 | }
|
359 |
|
360 |
|
361 |
|
362 |
|
363 |
|
364 |
|
365 |
|
366 |
|
367 |
|
368 |
|
369 |
|
370 |
|
371 |
|
372 | export function makeFileInodeBlob( datastore_id, owner_id, inode_uuid, data_hash, device_id, version ) {
|
373 |
|
374 | const header_blob = makeInodeHeaderBlob( datastore_id, MUTABLE_DATUM_FILE_TYPE, owner_id, inode_uuid, data_hash, device_id, version );
|
375 | return {'header': header_blob}
|
376 | }
|
377 |
|
378 |
|
379 |
|
380 |
|
381 |
|
382 |
|
383 |
|
384 |
|
385 |
|
386 | export function getChildVersion(parent_dir, child_name) {
|
387 | assert(parent_dir['idata']['children'][child_name]);
|
388 | return parent_dir['idata']['children'][child_name].version;
|
389 | }
|
390 |
|
391 |
|
392 |
|
393 |
|
394 |
|
395 |
|
396 |
|
397 |
|
398 |
|
399 |
|
400 |
|
401 |
|
402 |
|
403 | export function inodeDirLink( parent_dir, child_type, child_name, child_uuid, exists ) {
|
404 |
|
405 | assert(parent_dir['type'] === MUTABLE_DATUM_DIR_TYPE);
|
406 | assert(parent_dir['idata']);
|
407 | assert(parent_dir['idata']['children']);
|
408 |
|
409 | if( !exists ) {
|
410 | assert(!Object.keys(parent_dir['idata']['children']).includes(child_name));
|
411 | }
|
412 |
|
413 | const new_dirent = {
|
414 | uuid: child_uuid,
|
415 | type: child_type,
|
416 | version: 1,
|
417 | };
|
418 |
|
419 | if(parent_dir['idata']['children']['version']) {
|
420 | new_dirent.version = parent_dir['idata']['children']['version'] + 1;
|
421 | }
|
422 |
|
423 | parent_dir['idata']['children'][child_name] = new_dirent;
|
424 | parent_dir['version'] += 1;
|
425 | return parent_dir;
|
426 | }
|
427 |
|
428 |
|
429 |
|
430 |
|
431 |
|
432 |
|
433 |
|
434 |
|
435 |
|
436 |
|
437 | export function inodeDirUnlink( parent_dir, child_name ) {
|
438 |
|
439 | assert(parent_dir['type'] === MUTABLE_DATUM_DIR_TYPE);
|
440 | assert(parent_dir['idata']);
|
441 | assert(parent_dir['idata']['children']);
|
442 |
|
443 | assert(Object.keys(parent_dir['idata']['children']).includes(child_name));
|
444 |
|
445 | delete parent_dir['idata']['children'][child_name];
|
446 | parent_dir['version'] += 1;
|
447 | return parent_dir;
|
448 | }
|
449 |
|