veilidchat/lib/tools/dht_record.dart

202 lines
5.9 KiB
Dart
Raw Normal View History

2023-07-19 00:09:57 -04:00
import 'package:protobuf/protobuf.dart';
2023-07-17 22:39:33 -04:00
import 'package:veilid/veilid.dart';
import 'dart:typed_data';
import 'tools.dart';
class DHTRecord {
final VeilidRoutingContext _dhtctx;
final DHTRecordDescriptor _recordDescriptor;
final int _defaultSubkey;
2023-07-20 20:57:36 -04:00
final KeyPair? _writer;
late final DHTRecordEncryption _encryption;
2023-07-17 22:39:33 -04:00
static Future<DHTRecord> create(VeilidRoutingContext dhtctx,
{DHTSchema schema = const DHTSchema.dflt(oCnt: 1),
2023-07-20 15:48:55 -04:00
int defaultSubkey = 0,
2023-07-20 20:57:36 -04:00
DHTRecordEncryptionFactory crypto = DHTRecordEncryption.private}) async {
2023-07-17 22:39:33 -04:00
DHTRecordDescriptor recordDescriptor = await dhtctx.createDHTRecord(schema);
2023-07-20 20:57:36 -04:00
final rec = DHTRecord(
2023-07-17 22:39:33 -04:00
dhtctx: dhtctx,
recordDescriptor: recordDescriptor,
2023-07-20 20:57:36 -04:00
defaultSubkey: defaultSubkey,
2023-07-20 23:35:02 -04:00
writer: recordDescriptor.ownerKeyPair());
rec._encryption = crypto(rec);
return rec;
2023-07-17 22:39:33 -04:00
}
2023-07-20 23:35:02 -04:00
static Future<DHTRecord> openRead(
VeilidRoutingContext dhtctx, TypedKey recordKey,
2023-07-20 15:48:55 -04:00
{int defaultSubkey = 0,
2023-07-20 23:35:02 -04:00
DHTRecordEncryptionFactory crypto = DHTRecordEncryption.private}) async {
DHTRecordDescriptor recordDescriptor =
await dhtctx.openDHTRecord(recordKey, null);
final rec = DHTRecord(
dhtctx: dhtctx,
recordDescriptor: recordDescriptor,
defaultSubkey: defaultSubkey,
writer: null);
rec._encryption = crypto(rec);
return rec;
}
static Future<DHTRecord> openWrite(
VeilidRoutingContext dhtctx, TypedKey recordKey, KeyPair writer,
{int defaultSubkey = 0,
DHTRecordEncryptionFactory crypto = DHTRecordEncryption.private}) async {
2023-07-17 22:39:33 -04:00
DHTRecordDescriptor recordDescriptor =
await dhtctx.openDHTRecord(recordKey, writer);
2023-07-20 23:35:02 -04:00
final rec = DHTRecord(
2023-07-17 22:39:33 -04:00
dhtctx: dhtctx,
recordDescriptor: recordDescriptor,
2023-07-20 23:35:02 -04:00
defaultSubkey: defaultSubkey,
writer: writer);
rec._encryption = crypto(rec);
return rec;
2023-07-17 22:39:33 -04:00
}
DHTRecord(
{required VeilidRoutingContext dhtctx,
required DHTRecordDescriptor recordDescriptor,
2023-07-20 23:35:02 -04:00
int defaultSubkey = 0,
KeyPair? writer})
2023-07-17 22:39:33 -04:00
: _dhtctx = dhtctx,
_recordDescriptor = recordDescriptor,
2023-07-20 20:57:36 -04:00
_defaultSubkey = defaultSubkey,
_writer = writer;
2023-07-17 22:39:33 -04:00
2023-07-20 23:35:02 -04:00
int subkeyOrDefault(int subkey) => (subkey == -1) ? _defaultSubkey : subkey;
2023-07-17 22:39:33 -04:00
2023-07-19 00:09:57 -04:00
TypedKey key() {
return _recordDescriptor.key;
}
PublicKey owner() {
return _recordDescriptor.owner;
}
KeyPair? ownerKeyPair() {
2023-07-20 23:35:02 -04:00
return _recordDescriptor.ownerKeyPair();
2023-07-19 00:09:57 -04:00
}
2023-07-20 20:57:36 -04:00
KeyPair? writer() {
return _writer;
}
2023-07-19 00:09:57 -04:00
Future<void> close() async {
await _dhtctx.closeDHTRecord(_recordDescriptor.key);
}
Future<void> delete() async {
await _dhtctx.deleteDHTRecord(_recordDescriptor.key);
}
Future<T> scope<T>(Future<T> Function(DHTRecord) scopeFunction) async {
try {
return await scopeFunction(this);
} finally {
close();
}
}
Future<T> deleteScope<T>(Future<T> Function(DHTRecord) scopeFunction) async {
try {
return await scopeFunction(this);
} catch (_) {
delete();
rethrow;
} finally {
close();
}
}
2023-07-17 22:39:33 -04:00
Future<Uint8List?> get({int subkey = -1, bool forceRefresh = false}) async {
2023-07-20 23:35:02 -04:00
subkey = subkeyOrDefault(subkey);
ValueData? valueData =
await _dhtctx.getDHTValue(_recordDescriptor.key, subkey, false);
2023-07-17 22:39:33 -04:00
if (valueData == null) {
return null;
}
2023-07-20 23:35:02 -04:00
return _encryption.decrypt(valueData.data, subkey);
2023-07-17 22:39:33 -04:00
}
Future<T?> getJson<T>(T Function(Map<String, dynamic>) fromJson,
{int subkey = -1, bool forceRefresh = false}) async {
2023-07-20 23:35:02 -04:00
final data = await get(subkey: subkey, forceRefresh: forceRefresh);
if (data == null) {
2023-07-17 22:39:33 -04:00
return null;
}
2023-07-20 23:35:02 -04:00
return jsonDecodeBytes(fromJson, data);
2023-07-17 22:39:33 -04:00
}
Future<void> eventualWriteBytes(Uint8List newValue, {int subkey = -1}) async {
2023-07-20 23:35:02 -04:00
subkey = subkeyOrDefault(subkey);
newValue = await _encryption.encrypt(newValue, subkey);
2023-07-17 22:39:33 -04:00
// Get existing identity key
ValueData? valueData;
do {
// Ensure it exists already
if (valueData == null) {
throw const FormatException("value does not exist");
}
// Set the new data
2023-07-20 23:35:02 -04:00
valueData =
await _dhtctx.setDHTValue(_recordDescriptor.key, subkey, newValue);
2023-07-17 22:39:33 -04:00
// Repeat if newer data on the network was found
} while (valueData != null);
}
Future<void> eventualUpdateBytes(
Future<Uint8List> Function(Uint8List oldValue) update,
{int subkey = -1}) async {
2023-07-20 23:35:02 -04:00
subkey = subkeyOrDefault(subkey);
2023-07-17 22:39:33 -04:00
// Get existing identity key
2023-07-20 23:35:02 -04:00
ValueData? valueData =
await _dhtctx.getDHTValue(_recordDescriptor.key, subkey, false);
2023-07-17 22:39:33 -04:00
do {
// Ensure it exists already
if (valueData == null) {
throw const FormatException("value does not exist");
}
// Update the data
2023-07-20 23:35:02 -04:00
final oldData = await _encryption.decrypt(valueData.data, subkey);
final updatedData = await update(oldData);
final newData = await _encryption.encrypt(updatedData, subkey);
2023-07-17 22:39:33 -04:00
// Set it back
2023-07-20 23:35:02 -04:00
valueData =
await _dhtctx.setDHTValue(_recordDescriptor.key, subkey, newData);
2023-07-17 22:39:33 -04:00
// Repeat if newer data on the network was found
} while (valueData != null);
}
Future<void> eventualWriteJson<T>(T newValue, {int subkey = -1}) {
return eventualWriteBytes(jsonEncodeBytes(newValue), subkey: subkey);
}
2023-07-19 00:09:57 -04:00
Future<void> eventualWriteProtobuf<T extends GeneratedMessage>(T newValue,
{int subkey = -1}) {
return eventualWriteBytes(newValue.writeToBuffer(), subkey: subkey);
}
2023-07-17 22:39:33 -04:00
Future<void> eventualUpdateJson<T>(
T Function(Map<String, dynamic>) fromJson, Future<T> Function(T) update,
{int subkey = -1}) {
return eventualUpdateBytes(jsonUpdate(fromJson, update), subkey: subkey);
}
2023-07-19 00:09:57 -04:00
Future<void> eventualUpdateProtobuf<T extends GeneratedMessage>(
T Function(List<int>) fromBuffer, Future<T> Function(T) update,
{int subkey = -1}) {
return eventualUpdateBytes(protobufUpdate(fromBuffer, update),
subkey: subkey);
}
2023-07-17 22:39:33 -04:00
}