diff options
author | Tristan Schwieren <tristan.schwieren@tum.de> | 2021-12-14 23:25:40 +0100 |
---|---|---|
committer | Tristan Schwieren <tristan.schwieren@tum.de> | 2021-12-14 23:25:40 +0100 |
commit | 3586f9360aac1f6ba580cacc00accce9aab00893 (patch) | |
tree | 47633fce033ea97aab5a96ba25e0dc620160b4fb | |
parent | 8e685158f11e8c8d57b30248a694c66119066d0c (diff) | |
download | gnunet-3586f9360aac1f6ba580cacc00accce9aab00893.tar.gz gnunet-3586f9360aac1f6ba580cacc00accce9aab00893.zip |
-did working replace, generalized remove, refactor
-rw-r--r-- | src/did/gnunet-did.c | 213 |
1 files changed, 136 insertions, 77 deletions
diff --git a/src/did/gnunet-did.c b/src/did/gnunet-did.c index f5dfc4f43..2efc116cd 100644 --- a/src/did/gnunet-did.c +++ b/src/did/gnunet-did.c | |||
@@ -88,8 +88,8 @@ const static struct GNUNET_CONFIGURATION_Handle * my_cfg; | |||
88 | // TODO | 88 | // TODO |
89 | // static void replace_did_document(); - use remove_did_document and add_did_document | 89 | // static void replace_did_document(); - use remove_did_document and add_did_document |
90 | // eddsa only | 90 | // eddsa only |
91 | // welche properties? | 91 | // Set the duration for the didd record |
92 | 92 | // safe delete the didd record - look for other with same sub | |
93 | // Add a data DID Document type | 93 | // Add a data DID Document type |
94 | 94 | ||
95 | /** | 95 | /** |
@@ -114,6 +114,10 @@ cleanup(void * cls) | |||
114 | static void | 114 | static void |
115 | get_did_for_ego_lookup_cb(void *cls, struct GNUNET_IDENTITY_Ego * ego) | 115 | get_did_for_ego_lookup_cb(void *cls, struct GNUNET_IDENTITY_Ego * ego) |
116 | { | 116 | { |
117 | struct GNUNET_IDENTITY_PublicKey pkey; // Get Public key | ||
118 | const char * pkey_str; | ||
119 | char did_str[71]; // 58 + 12 + 1= 71 | ||
120 | |||
117 | if(ego == NULL) { | 121 | if(ego == NULL) { |
118 | printf("EGO not found\n"); | 122 | printf("EGO not found\n"); |
119 | GNUNET_SCHEDULER_add_now(&cleanup, NULL); | 123 | GNUNET_SCHEDULER_add_now(&cleanup, NULL); |
@@ -121,11 +125,9 @@ get_did_for_ego_lookup_cb(void *cls, struct GNUNET_IDENTITY_Ego * ego) | |||
121 | return; | 125 | return; |
122 | } | 126 | } |
123 | 127 | ||
124 | struct GNUNET_IDENTITY_PublicKey pkey; // Get Public key | ||
125 | GNUNET_IDENTITY_ego_get_public_key(ego, &pkey); | 128 | GNUNET_IDENTITY_ego_get_public_key(ego, &pkey); |
126 | 129 | ||
127 | const char * pkey_str = GNUNET_IDENTITY_public_key_to_string(&pkey); // Convert public key to string | 130 | pkey_str = GNUNET_IDENTITY_public_key_to_string(&pkey); // Convert public key to string |
128 | char did_str[71]; // 58 + 12 + 1= 71 | ||
129 | sprintf(did_str, "did:reclaim:%s", pkey_str); // Convert the public key to a DID str | 131 | sprintf(did_str, "did:reclaim:%s", pkey_str); // Convert the public key to a DID str |
130 | 132 | ||
131 | printf("%s\n", did_str); | 133 | printf("%s\n", did_str); |
@@ -212,6 +214,8 @@ print_did_document( | |||
212 | static void | 214 | static void |
213 | resolve_did_document() | 215 | resolve_did_document() |
214 | { | 216 | { |
217 | struct GNUNET_IDENTITY_PublicKey pkey; | ||
218 | |||
215 | if (attr_did == NULL) { | 219 | if (attr_did == NULL) { |
216 | printf("Set DID option to resolve DID\n"); | 220 | printf("Set DID option to resolve DID\n"); |
217 | GNUNET_SCHEDULER_add_now(cleanup, NULL); | 221 | GNUNET_SCHEDULER_add_now(cleanup, NULL); |
@@ -219,7 +223,6 @@ resolve_did_document() | |||
219 | return; | 223 | return; |
220 | } | 224 | } |
221 | 225 | ||
222 | struct GNUNET_IDENTITY_PublicKey pkey; | ||
223 | get_pkey_from_attr_did(&pkey); | 226 | get_pkey_from_attr_did(&pkey); |
224 | 227 | ||
225 | // TODO: Check the type of returned records | 228 | // TODO: Check the type of returned records |
@@ -237,15 +240,43 @@ struct event { | |||
237 | }; | 240 | }; |
238 | 241 | ||
239 | /** | 242 | /** |
240 | * @brief Callback after the DID has been removed | 243 | * @brief Implements the GNUNET_NAMESTORE_ContinuationWithStatus |
244 | * Calls the callback function and cls in the event struct | ||
245 | * | ||
246 | * @param cls closure containing the event struct | ||
247 | * @param success | ||
248 | * @param emgs | ||
241 | */ | 249 | */ |
242 | static void | 250 | static void |
243 | remove_did_cb(void * arg){ | 251 | remove_did_document_namestore_cb(void * cls, int32_t success, const char *emgs){ |
244 | // Test if record was removed from Namestore | 252 | struct event * blob; |
245 | printf("DID Document has been removed\n"); | 253 | |
246 | GNUNET_SCHEDULER_add_now(cleanup, NULL); | 254 | if(success == GNUNET_YES){ |
247 | ret = 0; | 255 | printf("DID Document has been removed\n"); |
248 | return; | 256 | |
257 | blob = (struct event *) cls; | ||
258 | |||
259 | if(blob->cont != NULL) | ||
260 | { | ||
261 | blob->cont(blob->cls); | ||
262 | free(blob); | ||
263 | } else { | ||
264 | free(blob); | ||
265 | GNUNET_SCHEDULER_add_now(cleanup, NULL); | ||
266 | ret = 0; | ||
267 | return; | ||
268 | } | ||
269 | } else { | ||
270 | printf("Something went wrong when deleting the DID Document\n"); | ||
271 | |||
272 | if(emgs != NULL) { | ||
273 | printf("%s\n", emgs); | ||
274 | } | ||
275 | |||
276 | GNUNET_SCHEDULER_add_now(cleanup, NULL); | ||
277 | ret = 0; | ||
278 | return; | ||
279 | } | ||
249 | } | 280 | } |
250 | 281 | ||
251 | /** | 282 | /** |
@@ -255,28 +286,25 @@ remove_did_cb(void * arg){ | |||
255 | * @param ego the ego returned by the identity service | 286 | * @param ego the ego returned by the identity service |
256 | */ | 287 | */ |
257 | static void | 288 | static void |
258 | remove_did_ego_lookup_cb(void * cls, struct GNUNET_IDENTITY_Ego * ego){ | 289 | remove_did_document_ego_lookup_cb(void * cls, struct GNUNET_IDENTITY_Ego * ego){ |
259 | //const struct GNUNET_IDENTITY_PrivateKey * skey = GNUNET_IDENTITY_ego_get_private_key(ego); | 290 | const struct GNUNET_IDENTITY_PrivateKey * skey = GNUNET_IDENTITY_ego_get_private_key(ego); |
260 | //const int emp[0]; | 291 | const int emp[0]; |
261 | //struct GNUNET_GNSRECORD_Data rd = { | 292 | struct GNUNET_GNSRECORD_Data rd = { |
262 | // .data = &emp, | 293 | .data = &emp, |
263 | // .expiration_time = 0, | 294 | .expiration_time = 0, |
264 | // .data_size = 0, | 295 | .data_size = 0, |
265 | // .record_type = 0, | 296 | .record_type = 0, |
266 | // .flags = GNUNET_GNSRECORD_RF_NONE | 297 | .flags = GNUNET_GNSRECORD_RF_NONE |
267 | //}; | 298 | }; |
268 | |||
269 | printf("2: %d\n", * (int *) cls); | ||
270 | |||
271 | //GNUNET_NAMESTORE_records_store (namestore_handle, | ||
272 | // skey, | ||
273 | // "didd", | ||
274 | // 0, | ||
275 | // &rd, | ||
276 | // &remove_did_cb, | ||
277 | // NULL); | ||
278 | } | ||
279 | 299 | ||
300 | GNUNET_NAMESTORE_records_store (namestore_handle, | ||
301 | skey, | ||
302 | "didd", | ||
303 | 0, | ||
304 | &rd, | ||
305 | &remove_did_document_namestore_cb, | ||
306 | cls); | ||
307 | } | ||
280 | 308 | ||
281 | /** | 309 | /** |
282 | * @brief Remove a DID Document | 310 | * @brief Remove a DID Document |
@@ -284,29 +312,22 @@ remove_did_ego_lookup_cb(void * cls, struct GNUNET_IDENTITY_Ego * ego){ | |||
284 | static void | 312 | static void |
285 | remove_did_document(remove_did_document_callback cont, void * cls) | 313 | remove_did_document(remove_did_document_callback cont, void * cls) |
286 | { | 314 | { |
315 | struct event * blob; | ||
316 | |||
287 | if(attr_ego == NULL) { | 317 | if(attr_ego == NULL) { |
288 | printf("Remove requieres an ego option\n"); | 318 | printf("Remove requieres an ego option\n"); |
289 | GNUNET_SCHEDULER_add_now(cleanup, NULL); | 319 | GNUNET_SCHEDULER_add_now(cleanup, NULL); |
290 | ret = 1; | 320 | ret = 1; |
291 | return; | 321 | return; |
292 | } else { | 322 | } else { |
293 | //struct remove_did_document_cont_cls * blob = malloc(sizeof(* blob)); | 323 | blob = malloc(sizeof(* blob)); |
294 | //blob->cont = (remove_did_document_callback *) malloc(sizeof(*cont)); | 324 | blob->cont = cont; |
295 | //memcpy(blob->cont, cont, sizeof(*cont)); | 325 | blob->cls = cls; |
296 | |||
297 | struct event * blob = malloc(sizeof(struct event)); | ||
298 | //blob->cont = malloc(sizeof(remove_did_document_callback)); | ||
299 | blob->cls = malloc(sizeof(*cls)); | ||
300 | |||
301 | //memcpy(blob->cont, cont, sizeof(*cont)); | ||
302 | memcpy(blob->cls, cls, sizeof(*cls)); | ||
303 | |||
304 | printf("1: %d\n", * (int *) blob->cls); | ||
305 | 326 | ||
306 | GNUNET_IDENTITY_ego_lookup(my_cfg, | 327 | GNUNET_IDENTITY_ego_lookup(my_cfg, |
307 | attr_ego, | 328 | attr_ego, |
308 | &remove_did_ego_lookup_cb, | 329 | &remove_did_document_ego_lookup_cb, |
309 | blob); | 330 | (void *) blob); |
310 | } | 331 | } |
311 | } | 332 | } |
312 | 333 | ||
@@ -320,38 +341,51 @@ remove_did_document(remove_did_document_callback cont, void * cls) | |||
320 | char * | 341 | char * |
321 | create_did_generate(struct GNUNET_IDENTITY_PublicKey pkey) | 342 | create_did_generate(struct GNUNET_IDENTITY_PublicKey pkey) |
322 | { | 343 | { |
323 | const char * pkey_str = GNUNET_IDENTITY_public_key_to_string(&pkey); // Convert public key to string | 344 | char * pkey_str; // Convert public key to string |
324 | char did_str[71]; // 58 + 12 + 1= 71 | 345 | char did_str[71]; // 58 + 12 + 1= 71 |
346 | char * didd_str; | ||
325 | char pkey_multibase_str[60]; // 58 + 1 + 1 = 60 | 347 | char pkey_multibase_str[60]; // 58 + 1 + 1 = 60 |
348 | |||
349 | json_t * did_json; | ||
350 | json_t * pkey_multibase_json; | ||
351 | json_t * context_1_json; | ||
352 | json_t * context_2_json; | ||
353 | json_t * auth_type_json; | ||
354 | json_t * context_json; | ||
355 | json_t * auth_json; | ||
356 | json_t * auth_1_json; | ||
357 | json_t * didd; | ||
358 | |||
359 | pkey_str = GNUNET_IDENTITY_public_key_to_string(&pkey); // Convert public key to string | ||
326 | sprintf(did_str, "did:reclaim:%s", pkey_str); // Convert the public key to a DID str | 360 | sprintf(did_str, "did:reclaim:%s", pkey_str); // Convert the public key to a DID str |
327 | sprintf(pkey_multibase_str, "V%s", pkey_str); // Convert the public key to MultiBase data format | 361 | sprintf(pkey_multibase_str, "V%s", pkey_str); // Convert the public key to MultiBase data format |
328 | 362 | ||
329 | // Create DID Document | 363 | // Create DID Document |
330 | json_t * did_json = json_string(did_str); | 364 | did_json = json_string(did_str); |
331 | json_t * pkey_multibase_json = json_string(pkey_multibase_str); | 365 | pkey_multibase_json = json_string(pkey_multibase_str); |
332 | json_t * context_1_json = json_string("https://www.w3.org/ns/did/v1"); | 366 | context_1_json = json_string("https://www.w3.org/ns/did/v1"); |
333 | json_t * context_2_json = json_string("https://w3id.org/security/suites/ed25519-2020/v1"); | 367 | context_2_json = json_string("https://w3id.org/security/suites/ed25519-2020/v1"); |
334 | json_t * auth_type_json = json_string("Ed25519VerificationKey2020"); | 368 | auth_type_json = json_string("Ed25519VerificationKey2020"); |
335 | 369 | ||
336 | json_t * context_json = json_array(); | 370 | context_json = json_array(); |
337 | json_array_append(context_json, context_1_json); | 371 | json_array_append(context_json, context_1_json); |
338 | json_array_append(context_json, context_2_json); | 372 | json_array_append(context_json, context_2_json); |
339 | 373 | ||
340 | json_t * auth_json = json_array(); | 374 | auth_json = json_array(); |
341 | json_t * auth_1_json = json_object(); | 375 | auth_1_json = json_object(); |
342 | json_object_set(auth_1_json, "id", did_json); | 376 | json_object_set(auth_1_json, "id", did_json); |
343 | json_object_set(auth_1_json, "type", auth_type_json); | 377 | json_object_set(auth_1_json, "type", auth_type_json); |
344 | json_object_set(auth_1_json, "controller", did_json); | 378 | json_object_set(auth_1_json, "controller", did_json); |
345 | json_object_set(auth_1_json, "publicKeyMultiBase", pkey_multibase_json); | 379 | json_object_set(auth_1_json, "publicKeyMultiBase", pkey_multibase_json); |
346 | json_array_append(auth_json, auth_1_json); | 380 | json_array_append(auth_json, auth_1_json); |
347 | 381 | ||
348 | json_t * didd = json_object(); | 382 | didd = json_object(); |
349 | json_object_set(didd, "@context", context_json); | 383 | json_object_set(didd, "@context", context_json); |
350 | json_object_set(didd, "id", did_json); | 384 | json_object_set(didd, "id", did_json); |
351 | json_object_set(didd, "authentication", auth_json); | 385 | json_object_set(didd, "authentication", auth_json); |
352 | 386 | ||
353 | // Encode DID Document as JSON string | 387 | // Encode DID Document as JSON string |
354 | char * didd_str = json_dumps(didd, JSON_INDENT(2)); | 388 | didd_str = json_dumps(didd, JSON_INDENT(2)); |
355 | if(didd_str == NULL) | 389 | if(didd_str == NULL) |
356 | { | 390 | { |
357 | printf("DID Document could not be encoded"); | 391 | printf("DID Document could not be encoded"); |
@@ -420,6 +454,9 @@ create_did_store(char * didd_str, struct GNUNET_IDENTITY_Ego * ego) | |||
420 | static void | 454 | static void |
421 | create_did_ego_lockup_cb(void *cls, struct GNUNET_IDENTITY_Ego * ego) | 455 | create_did_ego_lockup_cb(void *cls, struct GNUNET_IDENTITY_Ego * ego) |
422 | { | 456 | { |
457 | struct GNUNET_IDENTITY_PublicKey pkey; | ||
458 | char * didd_str; | ||
459 | |||
423 | if(ego == NULL) | 460 | if(ego == NULL) |
424 | { | 461 | { |
425 | printf("EGO not found\n"); | 462 | printf("EGO not found\n"); |
@@ -428,7 +465,6 @@ create_did_ego_lockup_cb(void *cls, struct GNUNET_IDENTITY_Ego * ego) | |||
428 | return; | 465 | return; |
429 | } | 466 | } |
430 | 467 | ||
431 | struct GNUNET_IDENTITY_PublicKey pkey; // Get Public key | ||
432 | GNUNET_IDENTITY_ego_get_public_key(ego, &pkey); | 468 | GNUNET_IDENTITY_ego_get_public_key(ego, &pkey); |
433 | 469 | ||
434 | printf("DEBUG: Key type: %d\n", pkey.type); | 470 | printf("DEBUG: Key type: %d\n", pkey.type); |
@@ -445,8 +481,6 @@ create_did_ego_lockup_cb(void *cls, struct GNUNET_IDENTITY_Ego * ego) | |||
445 | return; | 481 | return; |
446 | } | 482 | } |
447 | 483 | ||
448 | char * didd_str; | ||
449 | |||
450 | if(attr_didd != NULL) | 484 | if(attr_didd != NULL) |
451 | { | 485 | { |
452 | // TODO: Check if given DIDD is valid | 486 | // TODO: Check if given DIDD is valid |
@@ -475,6 +509,8 @@ create_did_document_ego_create_cb(void *cls, | |||
475 | const struct GNUNET_IDENTITY_PrivateKey *pk, | 509 | const struct GNUNET_IDENTITY_PrivateKey *pk, |
476 | const char *emsg) | 510 | const char *emsg) |
477 | { | 511 | { |
512 | const char * ego_name; | ||
513 | |||
478 | if (emsg != NULL){ | 514 | if (emsg != NULL){ |
479 | printf("Something went wrong during the creation of a new identity\n"); | 515 | printf("Something went wrong during the creation of a new identity\n"); |
480 | printf("%s\n", emsg); | 516 | printf("%s\n", emsg); |
@@ -483,7 +519,7 @@ create_did_document_ego_create_cb(void *cls, | |||
483 | return; | 519 | return; |
484 | } | 520 | } |
485 | 521 | ||
486 | const char * ego_name = (char *) cls; | 522 | ego_name = (char *) cls; |
487 | 523 | ||
488 | GNUNET_IDENTITY_ego_lookup(my_cfg, | 524 | GNUNET_IDENTITY_ego_lookup(my_cfg, |
489 | ego_name, | 525 | ego_name, |
@@ -514,26 +550,49 @@ create_did_document() | |||
514 | } | 550 | } |
515 | } | 551 | } |
516 | 552 | ||
553 | |||
554 | /** | ||
555 | * @brief Replace a DID Docuemnt. Callback function after ego lockup | ||
556 | * | ||
557 | * @param cls | ||
558 | * @param ego | ||
559 | */ | ||
517 | static void | 560 | static void |
518 | hello_world(void * arg) | 561 | replace_did_document_ego_lookup_cb(void *cls, struct GNUNET_IDENTITY_Ego * ego) |
519 | { | 562 | { |
520 | printf("arg: %d\n", * (int *) arg); | 563 | create_did_store(attr_didd, ego); |
521 | printf("Hello World!\n"); | 564 | } |
522 | GNUNET_SCHEDULER_add_now(&cleanup, NULL); | 565 | |
523 | ret = 1; | 566 | /** |
524 | return; | 567 | * @brief Replace a DID Document. Callback functiona after remove |
568 | * | ||
569 | * @param cls | ||
570 | */ | ||
571 | static void | ||
572 | replace_did_document_remove_cb(void * cls) | ||
573 | { | ||
574 | GNUNET_IDENTITY_ego_lookup(my_cfg, | ||
575 | attr_ego, | ||
576 | &replace_did_document_ego_lookup_cb, | ||
577 | NULL); | ||
525 | } | 578 | } |
526 | 579 | ||
580 | /** | ||
581 | * @brief Replace a DID Docuemnt | ||
582 | * | ||
583 | */ | ||
527 | static void | 584 | static void |
528 | replace_did_document() | 585 | replace_did_document() |
529 | { | 586 | { |
530 | // Do remove | 587 | if (attr_didd != NULL) |
531 | // Change remove to use coustome cb | 588 | { |
532 | // use create_did_store | 589 | remove_did_document(&replace_did_document_remove_cb, NULL); |
533 | 590 | } else { | |
534 | int var = 42; | 591 | printf("Set the DID Document argument to repalce the DID Document\n"); |
535 | 592 | GNUNET_SCHEDULER_add_now(&cleanup, NULL); | |
536 | remove_did_document(&hello_world, (void *) &var); | 593 | ret = 1; |
594 | return; | ||
595 | } | ||
537 | } | 596 | } |
538 | 597 | ||
539 | 598 | ||
@@ -571,7 +630,7 @@ run (void *cls, | |||
571 | } else if (1 == attr_get) { | 630 | } else if (1 == attr_get) { |
572 | resolve_did_document(); | 631 | resolve_did_document(); |
573 | } else if (1 == attr_remove) { | 632 | } else if (1 == attr_remove) { |
574 | remove_did_document(&remove_did_cb, NULL); | 633 | remove_did_document(NULL, NULL); |
575 | } else if (1 == attr_create) { | 634 | } else if (1 == attr_create) { |
576 | create_did_document(); | 635 | create_did_document(); |
577 | } else if (1 == attr_show) { | 636 | } else if (1 == attr_show) { |