Skip to content

Commit

Permalink
Merge pull request #13 from michielpost/feature/encryption
Browse files Browse the repository at this point in the history
Lock and Unlock wallets
  • Loading branch information
michielpost authored Dec 28, 2024
2 parents fbfe943 + 06b3937 commit f26bad3
Show file tree
Hide file tree
Showing 28 changed files with 626 additions and 88 deletions.
18 changes: 18 additions & 0 deletions src/aoWebWallet/Models/Wallet.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using aoWebWallet.Pages;
using System.Reflection.Metadata.Ecma335;
using System.Text.Json.Serialization;

namespace aoWebWallet.Models
{
Expand All @@ -8,7 +9,16 @@ public class Wallet
public required string Address { get; set; }
public string? OwnerAddress { get; set; }
public string? Name { get; set; }

/// <summary>
/// Old unencrypted Jwk in browser storage
/// </summary>
public string? Jwk { get; set; }

[JsonIgnore]
public string? JwkSecret { get; set; }

public string? JwkEncrypted { get; set; }
public WalletTypes Source { get; set; }

public bool IsReadOnly { get; set; }
Expand All @@ -17,8 +27,16 @@ 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()
{
return JwkSecret ?? Jwk;
}
}

public enum WalletTypes
Expand Down
4 changes: 4 additions & 0 deletions src/aoWebWallet/Pages/ActionPage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@
Wallet? ownerWallet = BindingContext.WalletList.Data?.Where(x => x.Address == wallet.OwnerAddress).FirstOrDefault();

started = true;

if(wallet.NeedsUnlock || (ownerWallet?.NeedsUnlock ?? false))
return;

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

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

if (wallet.NeedsUnlock)
return;

//Run preview if not run yet
if (data == null)
data = CreateTokenService.GetTokenProcessCode(wallet.Address, tokenModel);
Expand Down
41 changes: 41 additions & 0 deletions src/aoWebWallet/Pages/GenerateWalletPage.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
@page "/generate-wallet"
@inherits MvvmComponentBase<MainViewModel>
@inject ISnackbar Snackbar

<PageTitle>Generate 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
{
<AddGenerateWalletComponent />
}
</MudContainer>

@code {
private List<BreadcrumbItem> _items = new List<BreadcrumbItem>
{
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();
}
}
11 changes: 11 additions & 0 deletions src/aoWebWallet/Pages/GenerateWalletPage.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>
{

}
}
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 />


7 changes: 6 additions & 1 deletion src/aoWebWallet/Pages/WalletDetail.razor
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
</MudContainer>

<MudPaper Class="pa-4 mb-4">
@if (BindingContext.SelectedWallet?.Wallet.NeedsUnlock ?? false)
{
<UnlockWalletComponent />
}

<MudStack Row="true">
<WalletAvatar Address="@BindingContext.SelectedWallet?.Wallet.Address" />
<MudStack style="overflow: hidden;" Justify="Justify.Center" Spacing="0">
Expand Down Expand Up @@ -55,7 +60,7 @@

<MudMenu Icon="@Icons.Material.Filled.MoreVert">
<MudMenuItem>
@if (!string.IsNullOrEmpty(BindingContext.SelectedWallet?.Wallet.Jwk))
@if (!string.IsNullOrEmpty(BindingContext.SelectedWallet?.Wallet.GetJwkSecret()))
{
<MudButton StartIcon="@Icons.Material.Filled.Save" aria-label="backup" OnClick="() => { DownloadWallet(BindingContext.SelectedWallet.Wallet); }">Save</MudButton>
}
Expand Down
1 change: 1 addition & 0 deletions src/aoWebWallet/Pages/WalletDetail.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ protected override void OnInitialized()
//WatchObject(dataService.TokenList);
//WatchObject(BindingContext.BalanceDataList);
WatchObject(dataService.TokenDataLoader);
WatchProp(nameof(BindingContext.SecretKey));

WatchCollection(dataService.TokenList);
WatchCollection(BindingContext.BalanceDataList);
Expand Down
7 changes: 6 additions & 1 deletion src/aoWebWallet/Pages/Wallets.razor
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@

<DataLoaderProgress DataLoader="dataService.TokenDataLoader" Title="tokens" />
<MudStack>
@if (BindingContext.CheckNeedsUnlock())
{
<UnlockWalletComponent />
}

@if (BindingContext.WalletList.Data != null)
{
if (BindingContext.WalletList.Data.Where(x => !x.IsReadOnly).Any())
Expand Down Expand Up @@ -62,7 +67,7 @@
</MudStack>
<MudSpacer />
<MudMenu Icon="@Icons.Material.Filled.MoreVert">
@if(!string.IsNullOrEmpty(wallet.Jwk))
@if (!string.IsNullOrEmpty(wallet.GetJwkSecret()))
{
<MudMenuItem>
<MudButton Class="delete-address" StartIcon="@Icons.Material.Filled.Save" aria-label="backup" OnClick="() => { DownloadWallet(wallet); }">Backup</MudButton>
Expand Down
1 change: 1 addition & 0 deletions src/aoWebWallet/Pages/Wallets.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ protected override void OnInitialized()
//WatchDataLoaderVM(BindingContext.TokenList);
WatchDataLoaderVM(BindingContext.WalletList);
WatchDataLoaderVM(BindingContext.ProcessesDataList);
WatchProp(nameof(BindingContext.SecretKey));

base.OnInitialized();
}
Expand Down
4 changes: 3 additions & 1 deletion src/aoWebWallet/Services/CreateTokenService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ public string GetTokenProcessCode(string address, CreateTokenModel tokenModel)
{

var address = wallet.Address;
string? jwk = wallet.Jwk;
string? jwk = wallet.GetJwkSecret();
if (wallet.NeedsUnlock)
throw new Exception("Wallet is locked");

string newProcessId = await CreateEmptyProcess(tokenModel.Name, jwk);

Expand Down
16 changes: 12 additions & 4 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?.Jwk))
&& (!string.IsNullOrEmpty(ownerWallet?.GetJwkSecret()) || (ownerWallet?.NeedsUnlock ?? false)))
{
return await SendActionWithEval(ownerWallet.Jwk, wallet.Address, action);
if(ownerWallet.NeedsUnlock)
throw new Exception("Wallet is locked");

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

if (!string.IsNullOrEmpty(wallet.Jwk))
return await SendActionWithJwk(wallet.Jwk, action);
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
28 changes: 18 additions & 10 deletions src/aoWebWallet/Shared/AddGenerateWalletComponent.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 All @@ -7,16 +8,16 @@
<MudPaper Elevation="0" Class="pa-8 trigger-transparency">
<MudStack Spacing="2">

<MudFocusTrap Disabled="@Disabled" DefaultFocus="@DefaultFocus">
<MudTextField @bind-Value="Name" Label="Wallet Name" Variant="Variant.Text"></MudTextField>
</MudFocusTrap>
<MudFocusTrap Disabled="@Disabled" DefaultFocus="@DefaultFocus">
<MudTextField @bind-Value="Name" Label="Wallet Name" Variant="Variant.Text"></MudTextField>
</MudFocusTrap>

<MudText DefaultFocus="DefaultFocus" Color="Color.Secondary">@Progress</MudText>
<div Class="d-w-100 d-flex justify-center mt-2">
<MudButton Disabled="ButtonDisabled" Class="text-transform-none" Color="Color.Primary" Variant="Variant.Filled" OnClick="Submit">
Create aoWW Wallet
</MudButton>
</div>
<MudText DefaultFocus="DefaultFocus" Color="Color.Secondary">@Progress</MudText>
<div Class="d-w-100 d-flex mt-2">
<MudButton Disabled="ButtonDisabled" Class="text-transform-none" Color="Color.Primary" Variant="Variant.Filled" OnClick="Submit">
Create aoWW Wallet
</MudButton>
</div>
</MudStack>
</MudPaper>

Expand Down Expand Up @@ -52,17 +53,24 @@
var jwk = await ArweaveService.GenerateWallet();
var address = await ArweaveService.GetAddress(jwk);


var wallet = new Wallet
{
Address = address,
Name = Name,
Jwk = jwk,
JwkSecret = jwk,
Source = WalletTypes.Generated,
IsReadOnly = false,
LastBackedUpDate = null,
AddedDate = DateTimeOffset.UtcNow
};

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

await BindingContext.SaveWallet(wallet);

Snackbar.Add($"Wallet added ({address})", Severity.Info);
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
16 changes: 14 additions & 2 deletions src/aoWebWallet/Shared/AddWalletComponent.razor
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@
<MudImage Src="images/ths.svg" Alt="hello" Elevation="25" Class="ww-image-start" />
</div>
</div>
<AddGenerateWalletComponent></AddGenerateWalletComponent>
<div Class="d-w-100 d-flex justify-center mt-2">
<MudButton Href="/generate-wallet"
Variant="Variant.Filled"
Color="Color.Primary">
Generate new wallet
</MudButton>
</div>
</MudPaper>
</MudItem>
<MudItem xs="12" md="12" lg="4">
Expand All @@ -27,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
Loading

0 comments on commit f26bad3

Please sign in to comment.