veilidchat/lib/veilid_support/identity_master.dart

87 lines
3.1 KiB
Dart
Raw Normal View History

2023-07-25 01:04:34 -04:00
import 'dart:typed_data';
2023-07-21 21:25:27 -04:00
import 'package:fast_immutable_collections/fast_immutable_collections.dart';
import 'package:veilid/veilid.dart';
import '../entities/identity.dart';
import 'veilid_support.dart';
2023-07-25 01:04:34 -04:00
// Identity Master with secrets
// Not freezed because we never persist this class in its entirety
class IdentityMasterWithSecrets {
IdentityMasterWithSecrets(
{required this.identityMaster,
required this.masterSecret,
required this.identitySecret});
2023-07-26 14:20:29 -04:00
IdentityMaster identityMaster;
SecretKey masterSecret;
SecretKey identitySecret;
2023-07-25 01:04:34 -04:00
Future<void> delete() async {
final veilid = await eventualVeilid.future;
final dhtctx = (await veilid.routingContext())
.withPrivacy()
.withSequencing(Sequencing.ensureOrdered);
await dhtctx.deleteDHTRecord(identityMaster.masterRecordKey);
await dhtctx.deleteDHTRecord(identityMaster.identityRecordKey);
}
}
2023-07-21 21:25:27 -04:00
/// Creates a new master identity and returns it with its secrets
Future<IdentityMasterWithSecrets> newIdentityMaster() async {
2023-07-22 23:29:10 -04:00
final veilid = await eventualVeilid.future;
final dhtctx = (await veilid.routingContext())
2023-07-21 21:25:27 -04:00
.withPrivacy()
.withSequencing(Sequencing.ensureOrdered);
// IdentityMaster DHT record is public/unencrypted
return (await DHTRecord.create(dhtctx, crypto: const DHTRecordCryptoPublic()))
.deleteScope((masterRec) async {
// Identity record is private
return (await DHTRecord.create(dhtctx)).deleteScope((identityRec) async {
// Make IdentityMaster
final masterRecordKey = masterRec.key();
final masterOwner = masterRec.ownerKeyPair()!;
2023-07-25 01:04:34 -04:00
final masterSigBuf = BytesBuilder();
masterSigBuf.add(masterRecordKey.decode());
masterSigBuf.add(masterOwner.key.decode());
2023-07-21 21:25:27 -04:00
final identityRecordKey = identityRec.key();
final identityOwner = identityRec.ownerKeyPair()!;
2023-07-25 01:04:34 -04:00
final identitySigBuf = BytesBuilder();
identitySigBuf.add(identityRecordKey.decode());
identitySigBuf.add(identityOwner.key.decode());
assert(masterRecordKey.kind == identityRecordKey.kind);
final crypto = await veilid.getCryptoSystem(masterRecordKey.kind);
2023-07-21 21:25:27 -04:00
final identitySignature =
2023-07-25 01:04:34 -04:00
await crypto.signWithKeyPair(masterOwner, identitySigBuf.toBytes());
2023-07-21 21:25:27 -04:00
final masterSignature =
2023-07-25 01:04:34 -04:00
await crypto.signWithKeyPair(identityOwner, masterSigBuf.toBytes());
2023-07-21 21:25:27 -04:00
final identityMaster = IdentityMaster(
identityRecordKey: identityRecordKey,
identityPublicKey: identityOwner.key,
masterRecordKey: masterRecordKey,
masterPublicKey: masterOwner.key,
identitySignature: identitySignature,
masterSignature: masterSignature);
// Write identity master to master dht key
await masterRec.eventualWriteJson(identityMaster);
// Make empty identity
const identity = Identity(accountRecords: IMapConst({}));
// Write empty identity to identity dht key
await identityRec.eventualWriteJson(identity);
return IdentityMasterWithSecrets(
identityMaster: identityMaster,
masterSecret: masterOwner.secret,
identitySecret: identityOwner.secret);
});
});
}