Skip to content

Commit

Permalink
Setup for unlock dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
michielpost committed Oct 18, 2024
1 parent a80aa4b commit 9ba2cfb
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 30 deletions.
2 changes: 2 additions & 0 deletions src/aoWebWallet/Models/Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ public class Wallet
public DateTimeOffset AddedDate { get; set; }
public DateTimeOffset LastUsedDate { get; set; }

[JsonIgnore]
public bool NeedsUnlock => JwkEncrypted != null && JwkSecret == null;

[JsonIgnore]
public bool NeedsBackup => Source == WalletTypes.Generated && !string.IsNullOrEmpty(Jwk) && !LastBackedUpDate.HasValue;

public string? GetJwkSecret()
Expand Down
65 changes: 35 additions & 30 deletions src/aoWebWallet/Pages/GenerateWalletPage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,44 @@

@if (BindingContext.SecretKey == null)
{
<MudPaper Class="pa-4 mt-4" Elevation="3">
<MudText Typo="Typo.h4" Class="mb-4">Generate Wallet</MudText>
<MudText Typo="Typo.body1" Class="mb-4">Enter a password to encrypt your wallet. The password must be at least 6 characters long.</MudText>
if (BindingContext.CheckNeedsUnlock())
{
<UnlockWalletComponent />
}
else
{
<MudPaper Class="pa-4 mt-4" Elevation="3">
<MudText Typo="Typo.h4" Class="mb-4">Generate Wallet</MudText>
<MudText Typo="Typo.body1" Class="mb-4">Enter a password to encrypt your wallet. The password must be at least 6 characters long.</MudText>

<MudTextField @bind-Value="Password"
Label="Password"
Variant="Variant.Outlined"
InputType="@PasswordInput"
Adornment="Adornment.End"
AdornmentIcon="@PasswordInputIcon"
OnAdornmentClick="TogglePasswordVisibility"
Error="@(!string.IsNullOrEmpty(PasswordError))"
ErrorText="@PasswordError"
/>
<MudTextField @bind-Value="Password"
Label="Password"
Variant="Variant.Outlined"
InputType="@PasswordInput"
Adornment="Adornment.End"
AdornmentIcon="@PasswordInputIcon"
OnAdornmentClick="TogglePasswordVisibility"
Error="@(!string.IsNullOrEmpty(PasswordError))"
ErrorText="@PasswordError" />

<MudTextField @bind-Value="ConfirmPassword"
Label="Confirm Password"
Variant="Variant.Outlined"
InputType="@PasswordInput"
Adornment="Adornment.End"
AdornmentIcon="@PasswordInputIcon"
OnAdornmentClick="TogglePasswordVisibility"
Error="@(!string.IsNullOrEmpty(ConfirmPasswordError))"
ErrorText="@ConfirmPasswordError"
/>
<MudTextField @bind-Value="ConfirmPassword"
Label="Confirm Password"
Variant="Variant.Outlined"
InputType="@PasswordInput"
Adornment="Adornment.End"
AdornmentIcon="@PasswordInputIcon"
OnAdornmentClick="TogglePasswordVisibility"
Error="@(!string.IsNullOrEmpty(ConfirmPasswordError))"
ErrorText="@ConfirmPasswordError" />

<MudButton Variant="Variant.Filled"
Color="Color.Primary"
OnClick="GenerateWallet"
Class="mt-4">
Generate Wallet
</MudButton>
</MudPaper>
<MudButton Variant="Variant.Filled"
Color="Color.Primary"
OnClick="GenerateWallet"
Class="mt-4">
Generate Wallet
</MudButton>
</MudPaper>
}
}
else
{
Expand Down
30 changes: 30 additions & 0 deletions src/aoWebWallet/Shared/UnlockWalletComponent.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
@using aoWebWallet.Models
@inherits MvvmComponentBase<MainViewModel>
@inject ISnackbar Snackbar

<MudPaper Class="pa-8">
<MudStack Spacing="2">
<MudText Color="Color.Secondary">Wallets are locked. Please unlock the wallets.</MudText>

<div Class="d-w-100 d-flex mt-2">
<MudButton OnClick="Submit" Color="Color.Primary" Variant="Variant.Filled">
Unlock Wallet
</MudButton>
</div>
</MudStack>
</MudPaper>



@code {

public string? Progress { get; set; }


public async Task<bool> Submit()
{
await BindingContext.TriggerUnlock();

return true;
}
}
52 changes: 52 additions & 0 deletions src/aoWebWallet/Shared/UnlockWalletDialog.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
@using aoww.Services
@inject ISnackbar Snackbar
@inherits MvvmComponentBase<MainViewModel>

<MudDialog>
<DialogContent>
Unlock Wallets
<MudFocusTrap DefaultFocus="DefaultFocus.FirstChild">
<MudTextField @bind-Value="Password" Label="Password" Variant="Variant.Text"></MudTextField>
</MudFocusTrap>
<MudText Color="Color.Secondary">@Progress</MudText>
</DialogContent>
<DialogActions>
<MudButton OnClick="Cancel">Cancel</MudButton>
<MudButton Color="Color.Primary" OnClick="Submit">Unlock</MudButton>
</DialogActions>
</MudDialog>
@code {
[CascadingParameter] MudDialogInstance MudDialog { get; set; } = default!;

public string? Password { get; set; }
public string? Progress { get; set; }

public void Submit()
{
if (string.IsNullOrEmpty(Password))
return;

BindingContext.SecretKey = Password;

try
{
foreach(var wallet in BindingContext.WalletList.Data ?? new())
{
if(wallet.NeedsUnlock)
{
if (wallet.JwkEncrypted == null)
continue;

wallet.JwkSecret = EncryptionService.DecryptWallet(Password, wallet.JwkEncrypted);
}
}
}
catch
{
Progress = "Could not unlock wallets.";
}
}

//void Submit() => MudDialog.Close(DialogResult.Ok(true));
void Cancel() => MudDialog.Cancel();
}
25 changes: 25 additions & 0 deletions src/aoWebWallet/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using aoWebWallet.Models;
using aoWebWallet.Pages;
using aoWebWallet.Services;
using aoWebWallet.Shared;
using aoww.Services;
using aoww.Services.Models;
using ArweaveAO;
Expand Down Expand Up @@ -29,6 +30,7 @@ public partial class MainViewModel : ObservableRecipient
private readonly ArweaveService arweaveService;
private readonly GraphqlClient graphqlClient;
private readonly MemoryDataCache memoryDataCache;
private readonly IDialogService dialogService;
private readonly ArweaveConfig arweaveConfig;
private readonly GatewayConfig gatewayConfig;
private readonly GraphqlConfig graphqlConfig;
Expand Down Expand Up @@ -62,6 +64,7 @@ public MainViewModel(TokenDataService dataService,
ArweaveService arweaveService,
GraphqlClient graphqlClient,
MemoryDataCache memoryDataCache,
IDialogService dialogService,
IOptions<GraphqlConfig> graphqlConfig,
IOptions<GatewayConfig> gatewayConfig,
IOptions<ArweaveConfig> arweaveConfig) : base()
Expand All @@ -71,6 +74,7 @@ public MainViewModel(TokenDataService dataService,
this.arweaveService = arweaveService;
this.graphqlClient = graphqlClient;
this.memoryDataCache = memoryDataCache;
this.dialogService = dialogService;
this.arweaveConfig = arweaveConfig.Value;
this.gatewayConfig = gatewayConfig.Value;
this.graphqlConfig = graphqlConfig.Value;
Expand All @@ -81,6 +85,27 @@ public async Task AddToLog(ActivityLogType type, string id)
await storageService.AddToLog(type, id);
}

public bool CheckNeedsUnlock()
{
Console.WriteLine($"Wallets: {WalletList.Data?.Count}");
Console.WriteLine($"Wallets need unlock: {WalletList.Data?.Where(x => x.NeedsUnlock)?.Count()}");

return WalletList.Data?.Any(x => x.NeedsUnlock) ?? false;
}

public Task TriggerUnlock()
{
if (WalletList.Data?.Any(x => x.NeedsUnlock) ?? false)
{

var options = new DialogOptions { CloseOnEscapeKey = true };

return dialogService.ShowAsync<UnlockWalletDialog>("Unlock Wallet", options);
}

return Task.CompletedTask;
}


public Task LoadProcessesDataList() => ProcessesDataList.DataLoader.LoadAsync(async () =>
{
Expand Down

0 comments on commit 9ba2cfb

Please sign in to comment.