mirror of
https://gitlab.com/veilid/veilidchat.git
synced 2025-12-10 14:06:38 -05:00
account work
This commit is contained in:
parent
f44fdb8eeb
commit
90fc2e5f85
9 changed files with 422 additions and 25 deletions
|
|
@ -4,6 +4,7 @@ import 'dart:typed_data';
|
|||
import 'package:fast_immutable_collections/fast_immutable_collections.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
import 'package:veilid/veilid.dart';
|
||||
import 'package:veilidchat/tools/tools.dart';
|
||||
|
||||
import '../entities/entities.dart';
|
||||
import '../entities/proto.dart' as proto;
|
||||
|
|
@ -80,7 +81,7 @@ class LocalAccountManager {
|
|||
Uint8List.fromList(utf8.encode(jsonEncode(identityMaster)));
|
||||
await dhtctx.setDHTValue(masterRecordKey, 0, identityMasterBytes);
|
||||
|
||||
// Write empty identity to account map
|
||||
// Write empty identity to identity dht key
|
||||
const identity = Identity(accountKeyPairs: {});
|
||||
final identityBytes =
|
||||
Uint8List.fromList(utf8.encode(jsonEncode(identity)));
|
||||
|
|
@ -101,12 +102,32 @@ class LocalAccountManager {
|
|||
}
|
||||
}
|
||||
|
||||
Future<void> updateIdentityKey(
|
||||
VeilidRoutingContext dhtctx,
|
||||
TypedKey identityRecordKey,
|
||||
TypedKey accountRecordKey,
|
||||
KeyPair accountRecordOwner) async {
|
||||
// Get existing identity key
|
||||
ValueData? identityValueData =
|
||||
await dhtctx.getDHTValue(identityRecordKey, 0, true);
|
||||
if (identityValueData == null) {
|
||||
throw const FormatException("Identity does not exist");
|
||||
}
|
||||
var identity = identityValueData.readJsonData(Identity.fromJson);
|
||||
xxx make back to bytes function and do update loop and maybe make that a tool function too (consistentUpdate)
|
||||
// Update identity key to include account
|
||||
const identity = Identity(accountKeyPairs: {});
|
||||
final identityBytes = Uint8List.fromList(utf8.encode(jsonEncode(identity)));
|
||||
await dhtctx.setDHTValue(identityRec.key, 0, identityBytes);
|
||||
}
|
||||
|
||||
/// Creates a new account associated with master identity
|
||||
Future<LocalAccount> newAccount(
|
||||
IdentityMaster identityMaster,
|
||||
SecretKey identitySecret,
|
||||
EncryptionKeyType encryptionKeyType,
|
||||
String encryptionKey) async {
|
||||
String encryptionKey,
|
||||
proto.Account account) async {
|
||||
// Encrypt identitySecret with key
|
||||
final cs = await Veilid.instance.bestCryptoSystem();
|
||||
final ekbytes = Uint8List.fromList(utf8.encode(encryptionKey));
|
||||
|
|
@ -126,7 +147,39 @@ class LocalAccountManager {
|
|||
hiddenAccount: false,
|
||||
);
|
||||
|
||||
// Push
|
||||
// Add account with profile to DHT
|
||||
final dhtctx = (await Veilid.instance.routingContext())
|
||||
.withPrivacy()
|
||||
.withSequencing(Sequencing.ensureOrdered);
|
||||
DHTRecordDescriptor? identityRec;
|
||||
DHTRecordDescriptor? accountRec;
|
||||
try {
|
||||
identityRec = await dhtctx.openDHTRecord(identityMaster.identityRecordKey,
|
||||
identityMaster.identityWriter(identitySecret));
|
||||
accountRec = await dhtctx.createDHTRecord(const DHTSchema.dflt(oCnt: 1));
|
||||
final crypto = await Veilid.instance.bestCryptoSystem();
|
||||
assert(identityRec.key.kind == crypto.kind());
|
||||
assert(accountRec.key.kind == crypto.kind());
|
||||
|
||||
// Write account key
|
||||
assert(await dhtctx.setDHTValue(
|
||||
accountRec.key, 0, account.writeToBuffer()) ==
|
||||
null);
|
||||
|
||||
// Update identity key to include account
|
||||
await updateIdentityKey(dhtctx, identityRec.key, accountRec.key,
|
||||
KeyPair(key: accountRec.owner, secret: accountRec.ownerSecret!));
|
||||
} catch (e) {
|
||||
if (identityRec != null) {
|
||||
await dhtctx.closeDHTRecord(identityRec.key);
|
||||
}
|
||||
if (accountRec != null) {
|
||||
await dhtctx.deleteDHTRecord(accountRec.key);
|
||||
}
|
||||
rethrow;
|
||||
}
|
||||
|
||||
// Add local account object to internal store
|
||||
|
||||
// Return local account object
|
||||
return localAccount;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue