+bool\r
+MatrixClientSetUserId(\r
+ MatrixClient * client,\r
+ const char * userId)\r
+{\r
+ int userIdLen = strlen(userId);\r
+\r
+ if (userIdLen > USER_ID_SIZE - 1)\r
+ return false;\r
+\r
+ for (int i = 0; i < userIdLen; i++)\r
+ client->userId[i] = userId[i];\r
+\r
+ return true;\r
+}\r
+\r
+bool\r
+MatrixClientGenerateOnetimeKeys(\r
+ MatrixClient * client,\r
+ int numberOfKeys)\r
+{\r
+ static uint8_t random[OLM_ONETIME_KEYS_RANDOM_SIZE];\r
+ Randomize(random, OLM_ONETIME_KEYS_RANDOM_SIZE);\r
+\r
+ size_t res =\r
+ olm_account_generate_one_time_keys(client->olmAccount.account,\r
+ numberOfKeys, random, OLM_ONETIME_KEYS_RANDOM_SIZE);\r
+\r
+ return res != olm_error();\r
+}\r
+\r
+bool\r
+MatrixClientUploadOnetimeKeys(\r
+ MatrixClient * client)\r
+{\r
+ static char requestBuffer[UPLOAD_KEYS_REQUEST_SIZE];\r
+\r
+ mjson_snprintf(requestBuffer, UPLOAD_KEYS_REQUEST_SIZE,\r
+ "{\"one_time_keys\":{");\r
+\r
+ static char onetimeKeysBuffer[1024];\r
+ olm_account_one_time_keys(client->olmAccount.account,\r
+ onetimeKeysBuffer, 1024);\r
+\r
+ const char *keys;\r
+ int keysLen;\r
+ mjson_find(onetimeKeysBuffer, strlen(onetimeKeysBuffer), "$.curve25519", &keys, &keysLen);\r
+\r
+ int koff, klen, voff, vlen, vtype, off = 0;\r
+ while ((off = mjson_next(keys, keysLen, off, &koff, &klen, &voff, &vlen, &vtype)) != 0) {\r
+ static char keyJson[JSON_ONETIME_KEY_SIZE];\r
+ \r
+ snprintf(keyJson, JSON_ONETIME_KEY_SIZE,\r
+ "{\"key\":\"%.*s\"}",\r
+ vlen-2, keys + voff+1);\r
+\r
+ static char keyJsonSigned[JSON_ONETIME_KEY_SIGNED_SIZE];\r
+\r
+ JsonSign(client,\r
+ keyJson, JSON_ONETIME_KEY_SIZE,\r
+ keyJsonSigned, JSON_ONETIME_KEY_SIGNED_SIZE);\r
+ \r
+ mjson_snprintf(requestBuffer+strlen(requestBuffer), UPLOAD_KEYS_REQUEST_SIZE-strlen(requestBuffer),\r
+ "\"signed_curve25519:%.*s\":%s,",\r
+ klen-2, keys + koff+1,\r
+ keyJsonSigned);\r
+ }\r
+\r
+ mjson_snprintf(requestBuffer+strlen(requestBuffer), UPLOAD_KEYS_REQUEST_SIZE-strlen(requestBuffer),\r
+ "}}");\r
+\r
+ printf("%s\n", requestBuffer);\r
+\r
+ return true;\r
+}\r
+\r
+bool\r
+MatrixClientUploadDeviceKeys(\r
+ MatrixClient * client)\r
+{\r
+ static char deviceKeysBuffer[UPLOAD_KEYS_REQUEST_SIZE];\r
+\r
+ mjson_snprintf(deviceKeysBuffer, UPLOAD_KEYS_REQUEST_SIZE,\r
+ "{\"device_keys\":{"\r
+ "\"algorithms\":[\"m.olm.v1.curve25519-aes-sha2\",\"m.megolm.v1.aes-sha2\"],"\r
+ "\"device_id\":\"%s\","\r
+ "\"keys\":{"\r
+ "\"curve25519:%s\":\"%s\","\r
+ "\"ed25519:%s\":\"%s\""\r
+ "},"\r
+ "\"user_id\":\"%s\""\r
+ "}}",\r
+ client->deviceId,\r
+ client->deviceId, client->deviceKey,\r
+ client->deviceId, client->signingKey,\r
+ client->userId);\r
+\r
+ static char deviceKeysSignedBuffer[UPLOAD_KEYS_REQUEST_SIGNED_SIZE];\r
+ JsonSign(client,\r
+ deviceKeysBuffer, UPLOAD_KEYS_REQUEST_SIZE,\r
+ deviceKeysSignedBuffer, UPLOAD_KEYS_REQUEST_SIZE);\r
+\r
+ printf("%s\n", deviceKeysSignedBuffer);\r
+\r
+ return true;\r
+}\r
+\r