This commit is contained in:
Christien Rioux 2023-07-20 20:57:36 -04:00
parent fb93e07ef2
commit fe49beba9f
3 changed files with 25 additions and 6 deletions

View File

@ -7,22 +7,29 @@ class DHTRecord {
final VeilidRoutingContext _dhtctx; final VeilidRoutingContext _dhtctx;
final DHTRecordDescriptor _recordDescriptor; final DHTRecordDescriptor _recordDescriptor;
final int _defaultSubkey; final int _defaultSubkey;
final KeyPair? _writer;
late final DHTRecordEncryption _encryption;
static Future<DHTRecord> create(VeilidRoutingContext dhtctx, static Future<DHTRecord> create(VeilidRoutingContext dhtctx,
{DHTSchema schema = const DHTSchema.dflt(oCnt: 1), {DHTSchema schema = const DHTSchema.dflt(oCnt: 1),
int defaultSubkey = 0, int defaultSubkey = 0,
DHTRecordEncryption encrypt = DHTRecordEncryption.private}) async { KeyPair? writer,
DHTRecordEncryptionFactory crypto = DHTRecordEncryption.private}) async {
DHTRecordDescriptor recordDescriptor = await dhtctx.createDHTRecord(schema); DHTRecordDescriptor recordDescriptor = await dhtctx.createDHTRecord(schema);
return DHTRecord(
final rec = DHTRecord(
dhtctx: dhtctx, dhtctx: dhtctx,
recordDescriptor: recordDescriptor, recordDescriptor: recordDescriptor,
defaultSubkey: defaultSubkey); defaultSubkey: defaultSubkey,
writer: writer);
final encryption = crypto)
} }
static Future<DHTRecord> open( static Future<DHTRecord> open(
VeilidRoutingContext dhtctx, TypedKey recordKey, KeyPair? writer, VeilidRoutingContext dhtctx, TypedKey recordKey, KeyPair? writer,
{int defaultSubkey = 0, {int defaultSubkey = 0,
DHTRecordEncryption encrypt = DHTRecordEncryption.private}) async { KeyPair? writer,
DHTRecordEncryptionFactory encrypt = DHTRecordEncryption.private}) async {
DHTRecordDescriptor recordDescriptor = DHTRecordDescriptor recordDescriptor =
await dhtctx.openDHTRecord(recordKey, writer); await dhtctx.openDHTRecord(recordKey, writer);
return DHTRecord( return DHTRecord(
@ -34,10 +41,11 @@ class DHTRecord {
DHTRecord( DHTRecord(
{required VeilidRoutingContext dhtctx, {required VeilidRoutingContext dhtctx,
required DHTRecordDescriptor recordDescriptor, required DHTRecordDescriptor recordDescriptor,
int defaultSubkey = 0}) int defaultSubkey = 0, KeyPair? writer})
: _dhtctx = dhtctx, : _dhtctx = dhtctx,
_recordDescriptor = recordDescriptor, _recordDescriptor = recordDescriptor,
_defaultSubkey = defaultSubkey; _defaultSubkey = defaultSubkey,
_writer = writer;
int _subkey(int subkey) => (subkey == -1) ? _defaultSubkey : subkey; int _subkey(int subkey) => (subkey == -1) ? _defaultSubkey : subkey;
@ -57,6 +65,10 @@ class DHTRecord {
return KeyPair(key: _recordDescriptor.owner, secret: ownerSecret); return KeyPair(key: _recordDescriptor.owner, secret: ownerSecret);
} }
KeyPair? writer() {
return _writer;
}
Future<void> close() async { Future<void> close() async {
await _dhtctx.closeDHTRecord(_recordDescriptor.key); await _dhtctx.closeDHTRecord(_recordDescriptor.key);
} }

View File

@ -4,6 +4,8 @@ import 'package:veilid/veilid.dart';
import 'dart:typed_data'; import 'dart:typed_data';
import 'tools.dart'; import 'tools.dart';
typedef DHTRecordEncryptionFactory = DHTRecordEncryption Function();
abstract class DHTRecordEncryption { abstract class DHTRecordEncryption {
factory DHTRecordEncryption.private() { factory DHTRecordEncryption.private() {
return DHTRecordEncryptionPrivate(); return DHTRecordEncryptionPrivate();
@ -16,6 +18,8 @@ abstract class DHTRecordEncryption {
FutureOr<Uint8List> decrypt(Uint8List data); FutureOr<Uint8List> decrypt(Uint8List data);
} }
////////////////////////////////////
/// Private DHT Record: Encrypted with the owner's secret key
class DHTRecordEncryptionPrivate implements DHTRecordEncryption { class DHTRecordEncryptionPrivate implements DHTRecordEncryption {
DHTRecordEncryptionPrivate() { DHTRecordEncryptionPrivate() {
// //
@ -32,6 +36,8 @@ class DHTRecordEncryptionPrivate implements DHTRecordEncryption {
} }
} }
////////////////////////////////////
/// Public DHT Record: No encryption
class DHTRecordEncryptionPublic implements DHTRecordEncryption { class DHTRecordEncryptionPublic implements DHTRecordEncryption {
DHTRecordEncryptionPublic() { DHTRecordEncryptionPublic() {
// //

View File

@ -1,5 +1,6 @@
export 'external_stream_state.dart'; export 'external_stream_state.dart';
export 'dht_record.dart'; export 'dht_record.dart';
export 'dht_record_encryption.dart';
export 'json_tools.dart'; export 'json_tools.dart';
export 'phono_byte.dart'; export 'phono_byte.dart';
export 'protobuf_tools.dart'; export 'protobuf_tools.dart';