Skip to content

Commit

Permalink
Check for locked wallets
Browse files Browse the repository at this point in the history
  • Loading branch information
michielpost committed Oct 21, 2024
1 parent 9ba2cfb commit b9ea0d6
Show file tree
Hide file tree
Showing 17 changed files with 232 additions and 109 deletions.
6 changes: 6 additions & 0 deletions src/aoWebWallet/Pages/ActionPage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@
Wallet? ownerWallet = BindingContext.WalletList.Data?.Where(x => x.Address == wallet.OwnerAddress).FirstOrDefault();

started = true;

if(wallet.NeedsUnlock || (ownerWallet?.NeedsUnlock ?? false))
{
///TODO: Trigger Unlock
}

await transactionService.SendAction(wallet, ownerWallet, AoAction);
}

Expand Down
5 changes: 5 additions & 0 deletions src/aoWebWallet/Pages/CreateTokenPage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@
if (wallet == null)
return;

if (wallet.NeedsUnlock)
{
//TODO: Trigger unlock
}

//Run preview if not run yet
if (data == null)
data = CreateTokenService.GetTokenProcessCode(wallet.Address, tokenModel);
Expand Down
40 changes: 9 additions & 31 deletions src/aoWebWallet/Pages/GenerateWalletPage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,7 @@
}
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="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>
<SetSecretKeyComponent />
}
}
else
Expand All @@ -60,4 +30,12 @@
new BreadcrumbItem("Home", href: "/"),
new BreadcrumbItem("Generate Wallet", href: null, disabled: true)
};

protected override void OnInitialized()
{
WatchDataLoaderVM(BindingContext.WalletList);
WatchProp(nameof(BindingContext.SecretKey));

base.OnInitialized();
}
}
58 changes: 1 addition & 57 deletions src/aoWebWallet/Pages/GenerateWalletPage.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,62 +6,6 @@ namespace aoWebWallet.Pages
{
public partial class GenerateWalletPage : MvvmComponentBase<MainViewModel>
{
private string Password { get; set; } = string.Empty;
private string ConfirmPassword { get; set; } = string.Empty;
private bool PasswordVisible { get; set; } = false;
private InputType PasswordInput { get; set; } = InputType.Password;
private string PasswordInputIcon { get; set; } = Icons.Material.Filled.VisibilityOff;
private string PasswordError { get; set; } = string.Empty;
private string ConfirmPasswordError { get; set; } = string.Empty;

private void TogglePasswordVisibility()
{
if (PasswordVisible)
{
PasswordVisible = false;
PasswordInputIcon = Icons.Material.Filled.VisibilityOff;
PasswordInput = InputType.Password;
}
else
{
PasswordVisible = true;
PasswordInputIcon = Icons.Material.Filled.Visibility;
PasswordInput = InputType.Text;
}
}

private void GenerateWallet()
{
PasswordError = string.Empty;
ConfirmPasswordError = string.Empty;

if (string.IsNullOrWhiteSpace(Password) || string.IsNullOrWhiteSpace(ConfirmPassword))
{
PasswordError = "Please enter and confirm your password.";
return;
}

if (Password != ConfirmPassword)
{
ConfirmPasswordError = "Passwords do not match.";
return;
}

if (Password.Length < 6)
{
PasswordError = "Password must be at least 6 characters long.";
return;
}

if(BindingContext.WalletList.Data?.Any(x => x.NeedsUnlock) ?? true)
{
PasswordError = "Unable to set a new password. Please unlock your wallet first.";
return;
}

BindingContext.SecretKey = Password;

Snackbar.Add("Password set successfully!", Severity.Success);
}

}
}
41 changes: 41 additions & 0 deletions src/aoWebWallet/Pages/ImportWalletPage.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
@page "/import-wallet"
@inherits MvvmComponentBase<MainViewModel>
@inject ISnackbar Snackbar

<PageTitle>Import Wallet - @Program.PageTitlePostFix</PageTitle>

<MudContainer Class="mt-2 px-8" MaxWidth="MaxWidth.False">
<MudBreadcrumbs Class="breadcrumbs-aoww" Items="_items"></MudBreadcrumbs>

@if (BindingContext.SecretKey == null)
{
if (BindingContext.CheckNeedsUnlock())
{
<UnlockWalletComponent />
}
else
{
<SetSecretKeyComponent />
}
}
else
{
<AddUploadWalletComponent></AddUploadWalletComponent>
}
</MudContainer>

@code {
private List<BreadcrumbItem> _items = new List<BreadcrumbItem>
{
new BreadcrumbItem("Home", href: "/"),
new BreadcrumbItem("Import Wallet", href: null, disabled: true)
};

protected override void OnInitialized()
{
WatchDataLoaderVM(BindingContext.WalletList);
WatchProp(nameof(BindingContext.SecretKey));

base.OnInitialized();
}
}
11 changes: 11 additions & 0 deletions src/aoWebWallet/Pages/ImportWalletPage.razor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using aoWebWallet.ViewModels;
using Microsoft.AspNetCore.Components;
using MudBlazor;

namespace aoWebWallet.Pages
{
public partial class GenerateWalletPage : MvvmComponentBase<MainViewModel>
{

}
}
1 change: 1 addition & 0 deletions src/aoWebWallet/Pages/Start.razor
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@

<AddWalletComponent />


2 changes: 2 additions & 0 deletions src/aoWebWallet/Services/CreateTokenService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public string GetTokenProcessCode(string address, CreateTokenModel tokenModel)
var address = wallet.Address;
string? jwk = wallet.GetJwkSecret();
if (wallet.NeedsUnlock)
throw new Exception("Wallet is locked");
string newProcessId = await CreateEmptyProcess(tokenModel.Name, jwk);
Expand Down
12 changes: 10 additions & 2 deletions src/aoWebWallet/Services/TransactionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,21 @@ static string RemoveColorCodes(string? input)
}

if (!string.IsNullOrEmpty(wallet.OwnerAddress) && ownerWallet?.Address == wallet.OwnerAddress
&& !string.IsNullOrEmpty(ownerWallet?.GetJwkSecret()))
&& (!string.IsNullOrEmpty(ownerWallet?.GetJwkSecret()) || (ownerWallet?.NeedsUnlock ?? false)))
{
if(ownerWallet.NeedsUnlock)
throw new Exception("Wallet is locked");

return await SendActionWithEval(ownerWallet.GetJwkSecret(), wallet.Address, action);
}

if (!string.IsNullOrEmpty(wallet.GetJwkSecret()))
if (!string.IsNullOrEmpty(wallet.GetJwkSecret()) || wallet.NeedsUnlock)
{
if (wallet.NeedsUnlock)
throw new Exception("Wallet is locked");

return await SendActionWithJwk(wallet.GetJwkSecret(), action);
}

//Console.WriteLine("No Wallet to send");
return null;
Expand Down
9 changes: 8 additions & 1 deletion src/aoWebWallet/Shared/AddUploadWalletComponent.razor
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@using aoWebWallet.Models
@using aoww.Services
@inherits MvvmComponentBase<MainViewModel>
@inject ArweaveService ArweaveService
@inject ISnackbar Snackbar
Expand Down Expand Up @@ -120,12 +121,18 @@
{
Address = address,
Name = name,
Jwk = jwk,
JwkSecret = jwk,
Source = WalletTypes.Imported,
IsReadOnly = false,
AddedDate = DateTimeOffset.UtcNow
};

if (!string.IsNullOrEmpty(BindingContext.SecretKey))
{
var jwkEncrypted = EncryptionService.EncryptWallet(BindingContext.SecretKey, jwk);
wallet.JwkEncrypted = jwkEncrypted;
}

_fileNames.Add(wallet);
}
catch
Expand Down
8 changes: 7 additions & 1 deletion src/aoWebWallet/Shared/AddWalletComponent.razor
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@
<MudImage Src="images/json-logo.svg" Alt="hello" Elevation="25" Class="ww-image-start ar-logo-setup" />
</div>
</div>
<AddUploadWalletComponent></AddUploadWalletComponent>
<div Class="d-w-100 d-flex justify-center mt-2">
<MudButton Href="/import-wallet"
Variant="Variant.Filled"
Color="Color.Primary">
Import wallet
</MudButton>
</div>
</MudPaper>
</MudItem>
</MudGrid>
Expand Down
98 changes: 98 additions & 0 deletions src/aoWebWallet/Shared/SetSecretKeyComponent.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
@using aoWebWallet.Models
@inherits MvvmComponentBase<MainViewModel>
@inject ISnackbar Snackbar

<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="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="SetSecretKey"
Class="mt-4">
Generate Wallet
</MudButton>
</MudPaper>



@code {

private string Password { get; set; } = string.Empty;
private string ConfirmPassword { get; set; } = string.Empty;
private bool PasswordVisible { get; set; } = false;
private InputType PasswordInput { get; set; } = InputType.Password;
private string PasswordInputIcon { get; set; } = Icons.Material.Filled.VisibilityOff;
private string PasswordError { get; set; } = string.Empty;
private string ConfirmPasswordError { get; set; } = string.Empty;

private void TogglePasswordVisibility()
{
if (PasswordVisible)
{
PasswordVisible = false;
PasswordInputIcon = Icons.Material.Filled.VisibilityOff;
PasswordInput = InputType.Password;
}
else
{
PasswordVisible = true;
PasswordInputIcon = Icons.Material.Filled.Visibility;
PasswordInput = InputType.Text;
}
}

private void SetSecretKey()
{
PasswordError = string.Empty;
ConfirmPasswordError = string.Empty;

if (string.IsNullOrWhiteSpace(Password) || string.IsNullOrWhiteSpace(ConfirmPassword))
{
PasswordError = "Please enter and confirm your password.";
return;
}

if (Password != ConfirmPassword)
{
ConfirmPasswordError = "Passwords do not match.";
return;
}

if (Password.Length < 6)
{
PasswordError = "Password must be at least 6 characters long.";
return;
}

if (BindingContext.WalletList.Data?.Any(x => x.NeedsUnlock) ?? true)
{
PasswordError = "Unable to set a new password. Please unlock your wallet first.";
return;
}

BindingContext.SecretKey = Password;

Snackbar.Add("Password set successfully!", Severity.Success);
}
}
2 changes: 2 additions & 0 deletions src/aoWebWallet/Shared/UnlockWalletComponent.razor
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
{
await BindingContext.TriggerUnlock();

this.StateHasChanged();

return true;
}
}
Loading

0 comments on commit b9ea0d6

Please sign in to comment.