using System.Text; using Toolkit.Foundation; namespace Wallet; public class OpenWalletHandler(IConfigurationDescriptor descriptor, ISecurityKeyFactory securityKeyFactory, IWalletStorageFactory WalletStorageFactory) : IHandler>, bool> { public async Task Handle(ActivateEventArgs> args, CancellationToken cancellationToken) { if (args.Sender is Wallet Wallet && descriptor.Name is { Length: > 0 } name && Wallet.Sender is { Length: > 0 } password) { WalletConfiguration configuration = descriptor.Value; if (configuration.Key?.Split(':') is { Length: >= 2 } keyPart) { byte[]? salt = Convert.FromBase64String(keyPart[0]); byte[]? encryptedKey = Convert.FromBase64String(keyPart[1]); if (securityKeyFactory.Create(Encoding.UTF8.GetBytes(password), encryptedKey, salt) is SecurityKey key) { if (await WalletStorageFactory.Create(name, key)) { return true; } } } } return false; } }