Learn how to create atKeys for your chosen platform
AtKey
Please note that any reference to the word "AtKey" in this document is not associated with cryptographic keys. The atKey is the "key" of the key-value pair that makes up an atRecord.
This article explains how to create an atKey in the atSDK.
If you are unfamiliar with atKeys please read this first.
Flutter / Dart
Package Installation
The at_commons package contains common elements used in a number of Atsign's Flutter and Dart packages. This package needs to be included in the application in order to create atKeys.
First add the package to your project:
flutter pub add at_commons
Usage
See below for how to create the various types of atKeys.
Public atKey
To create a public atKey, first use the AtKey.public builder to configure it, then call .build to create it.
Public atKey holds public (and non-encrypted) data, available for any atSign to get from you.
Self atKey holds self encrypted data, only available for your own atSign to get.
Shared atKey holds encrypted data that is only decipherable by you and the intended recipient.
Every atKey has metadata, which is free for you to also control (to an extent). Not all metadata should be handled by the developer. Some metadata is managed by the SDK itself. Check out our documentation on metadata to find out which metadata is worth your time handling.
Before running any of the examples, be sure to include atkey.h
Don't forget to call atclient_atkey_free at the end of the intended life-time of your atkey struct.
atclient_atkey_free(&my_public_atkey);
Example Application
#include<atclient/atclient.h>#include<atclient/atclient_utils.h>#include<atlogger/atlogger.h>#include<stdlib.h>#defineATSIGN"@soccer99"intmain(){int exit_code =-1; /* * Create an atkey struct */ atclient_atkey my_public_atkey;atclient_atkey_init(&my_public_atkey);constchar*atkey_key ="phone";constchar*atkey_shared_by = ATSIGN;constchar*atkey_namespace ="wavi"; /* * Use the dandy atkey_create_public_key function to populate the struct for you with your desired values. */if((exit_code =atclient_atkey_create_public_key(&my_public_atkey, atkey_key, atkey_shared_by, atkey_namespace)) !=0) {goto exit; } exit_code =0;exit:{atclient_atkey_free(&my_public_atkey);return exit_code;}}
Don't forget to call atclient_atkey_free at the end of the intended life-time of your atkey struct.
atclient_atkey_free(&my_public_atkey);
Example Application
#include<atclient/atclient.h>#include<atclient/atclient_utils.h>#include<atlogger/atlogger.h>#include<stdlib.h>#defineATSIGN"@soccer99"intmain(){int exit_code =-1; /* * Create an atkey struct */ atclient_atkey my_self_atkey;atclient_atkey_init(&my_self_atkey);constchar*atkey_key ="phone";constchar*atkey_shared_by = ATSIGN;constchar*atkey_namespace ="wavi"; /* * Use the dandy atkey_create_self_key function to populate the struct for you with your desired values. */if((exit_code =atclient_atkey_create_self_key(&my_self_atkey, atkey_key, atkey_shared_by, atkey_namespace)) !=0) {goto exit; } exit_code =0;exit:{atclient_atkey_free(&my_self_atkey);return exit_code;}}
Don't forget to call atclient_atkey_free at the end of the intended life-time of your atkey struct.
atclient_atkey_free(&my_public_atkey);
Example Application
#include<atclient/atclient.h>#include<atclient/atclient_utils.h>#include<atlogger/atlogger.h>#include<stdlib.h>#defineATSIGN"@soccer99"intmain(){int exit_code =-1; /* * Create an atkey struct */ atclient_atkey my_shared_atkey;atclient_atkey_init(&my_shared_atkey);constchar*atkey_key ="phone";constchar*atkey_shared_by = ATSIGN;constchar*atkey_shared_with ="@soccer99";constchar*atkey_namespace ="wavi"; /* * Use the dandy atkey_create_shared_key function to populate the struct for you with your desired values. */if((exit_code =atclient_atkey_create_shared_key(&my_shared_atkey, atkey_key, atkey_shared_by, atkey_shared_with, atkey_namespace)) !=0) {goto exit; } exit_code =0;exit:{atclient_atkey_free(&my_shared_atkey);return exit_code;}}
Metadata
This is how you modify the metadata of any atKey. For the sake of this example, we will modify the metadata of a Shared atKey.
Next, let's create a pointer to the atKey's metadata. We can feel safe reading the atclient_atkey's inner metadata field because we have previously called atclient_atkey_init.
Call a atclient_atkey_metadata_set_* function. For the sake of this example, we will call atclient_atkey_metadata_set_ttl which sets the atKey's lifespan, specified in milliseconds.
if (atclient_atkey_metadata_set_ttl(metadata,1*1000) !=0){// an error occurred}
We can check if that was set and is safe to read by using the atclient_atkey_metadata_is_ttl_initialized(metadata) function. It is important to check that ttl was initialized. If this function returns true, we can feel safe knowing that the the internal initialized bit was set to true previously and that we are not reading garbage data.
if(atclient_atkey_metadata_is_ttl_initialized(metadata)) {// metadata->ttl is safe to read and we know it is populated.printf("metadata->ttl: %d\n",metadata->ttl);}
Example Application
#include<atclient/atclient.h>#include<atclient/atclient_utils.h>#include<atlogger/atlogger.h>#include<stdlib.h>#defineATSIGN"@soccer99"#defineATSERVER_HOST"root.atsign.org"#defineATSERVER_PORT64intmain(){int exit_code =-1;atlogger_set_logging_level(ATLOGGER_LOGGING_LEVEL_DEBUG); atclient_atkey my_shared_atkey;atclient_atkey_init(&my_shared_atkey);constchar*atkey_key ="phone";constchar*atkey_shared_by = ATSIGN;constchar*atkey_shared_with ="@soccer99";constchar*atkey_namespace ="wavi";if ((exit_code =atclient_atkey_create_shared_key(&my_shared_atkey, atkey_key, atkey_shared_by, atkey_shared_with, atkey_namespace)) !=0) {goto exit; } /* * Now that the AtKey is properly set up, we can set the metadata of that AtKey like this. * Since we called the `atclient_atkey_init` function earlier, we can feel safe knowing that the metadata is also already initialized. */ atclient_atkey_metadata *metadata =&(my_shared_atkey.metadata); /* * Set the ttl (time to live) of the AtKey. Once this AtKey is put into the atServer, it will only live for 1000 milliseconds. */if ((exit_code =atclient_atkey_metadata_set_ttl(metadata,1*1000)) !=0) {goto exit; } /* * Set the ttr (time to refresh) of the AtKey. Once this AtKey is put into the atServer, the recipient of the AtKey will have it refreshed every 1000 * milliseconds, in case there are any value changes. */if ((exit_code =atclient_atkey_metadata_set_ttr(metadata,1*1000)) !=0) {goto exit; }if(atclient_atkey_metadata_is_ttl_initialized(metadata)) {// metadata->ttl is safe to read and we know it is populated.atlogger_log("main", ATLOGGER_LOGGING_LEVEL_DEBUG,"metadata->ttl: %d\n",metadata->ttl); // [DEBG] 2024-08-15 01:52:09.596055 | main | metadata->ttl: 1000 } exit_code =0;exit:{atclient_atkey_free(&my_shared_atkey); // this _free command will automatically free the metadata as wellreturn exit_code;}}