uploadKeys method
Publishes end-to-end encryption keys for the device.
deviceKeys
Identity keys for the device. May be absent if no new
identity keys are required.
fallbackKeys
The public key which should be used if the device's one-time keys
are exhausted. The fallback key is not deleted once used, but should
be replaced when additional one-time keys are being uploaded. The
server will notify the client of the fallback key being used through
/sync
.
There can only be at most one key per algorithm uploaded, and the server will only persist one key per algorithm.
When uploading a signed key, an additional fallback: true
key should
be included to denote that the key is a fallback key.
May be absent if a new fallback key is not required.
oneTimeKeys
One-time public keys for "pre-key" messages. The names of
the properties should be in the format
<algorithm>:<key_id>
. The format of the key is determined
by the key algorithm.
May be absent if no new one-time keys are required.
returns one_time_key_counts
:
For each key algorithm, the number of unclaimed one-time keys
of that type currently held on the server for this device.
If an algorithm is not listed, the count for that algorithm
is to be assumed zero.
Implementation
Future<Map<String, int>> uploadKeys({
MatrixDeviceKeys? deviceKeys,
Map<String, Object?>? fallbackKeys,
Map<String, Object?>? oneTimeKeys,
}) async {
final requestUri = Uri(path: '_matrix/client/v3/keys/upload');
final request = Request('POST', baseUri!.resolveUri(requestUri));
request.headers['authorization'] = 'Bearer ${bearerToken!}';
request.headers['content-type'] = 'application/json';
request.bodyBytes = utf8.encode(
jsonEncode({
if (deviceKeys != null) 'device_keys': deviceKeys.toJson(),
if (fallbackKeys != null) 'fallback_keys': fallbackKeys,
if (oneTimeKeys != null) 'one_time_keys': oneTimeKeys,
}),
);
final response = await httpClient.send(request);
final responseBody = await response.stream.toBytes();
if (response.statusCode != 200) unexpectedResponse(response, responseBody);
final responseString = utf8.decode(responseBody);
final json = jsonDecode(responseString);
return (json['one_time_key_counts'] as Map<String, Object?>)
.map((k, v) => MapEntry(k, v as int));
}