Files
Walleby/Wallet/WalletFactory.cs
T
2024-07-02 11:58:07 +01:00

43 lines
1.3 KiB
C#

using Microsoft.Extensions.Hosting;
using System.Text;
using Toolkit.Foundation;
namespace Wallet;
public class WalletFactory(ISecurityKeyFactory securityKeyFactory,
IDecoratorService<SecurityKey> secureKeyStore,
IWalletStoreFactory walletStoreFactory,
IWritableConfiguration<WalletConfiguration> configuration,
IHostEnvironment environment,
IImageWriter imageWriter) :
IWalletFactory
{
public async Task<bool> Create(string name,
string password,
IImageDescriptor? imageDescriptor)
{
if (securityKeyFactory.Create(Encoding.UTF8.GetBytes(password)) is SecurityKey key)
{
secureKeyStore.Set(key);
if (await walletStoreFactory.Create(name, key))
{
configuration.Write(args => args.Key = $"{Convert.ToBase64String(key.Salt)}:" +
$"{Convert.ToBase64String(key.EncryptedKey)}:{Convert.ToBase64String(key.DecryptedKey)}");
if (imageDescriptor is not null)
{
string file = Path.Combine(environment.ContentRootPath, "Thumbnail.png");
using FileStream stream = File.OpenWrite(file);
imageWriter.Write(imageDescriptor, stream);
}
return true;
}
}
return false;
}
}