Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for mainnet #61

Merged
merged 2 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 66 additions & 55 deletions src/Angor/Client/NetworkConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,75 @@ namespace Angor.Client;
public class NetworkConfiguration : INetworkConfiguration
{
public static string AngorTestKey = "tpubD8JfN1evVWPoJmLgVg6Usq2HEW9tLqm6CyECAADnH5tyQosrL6NuhpL9X1cQCbSmndVrgLSGGdbRqLfUbE6cRqUbrHtDJgSyQEY2Uu7WwTL";
public static string AngorMainKey = "todo: add an Angor key here";

private Network currentNetwork;

public void SetNetwork(Network network)
{
currentNetwork = network;
}

public Network GetNetwork()
{
return new BitcoinSignet();
if (currentNetwork == null)
{
throw new ApplicationException("Network not set");
}

return currentNetwork;
}

public SettingsUrl GetIndexerUrl()
{
return new SettingsUrl { Name = "", Url = "https://tbtc.indexer.angor.io/api" };
if (currentNetwork.NetworkType == NetworkType.Testnet)
{
return new SettingsUrl { Name = "", Url = "https://tbtc.indexer.angor.io/api" };
}

if (currentNetwork.NetworkType == NetworkType.Mainnet)
{
return new SettingsUrl { Name = "", Url = "https://btc.indexer.angor.io/api" };
}

throw new ApplicationException("Network not set");
}

public SettingsUrl GetExplorerUrl()
{
if (currentNetwork.NetworkType == NetworkType.Testnet)
{
return new SettingsUrl { Name = "", Url = "https://explorer.angor.io/tbtc/explorer" };
}

if (currentNetwork.NetworkType == NetworkType.Mainnet)
{
return new SettingsUrl { Name = "", Url = "https://explorer.angor.io/btc/explorer" };
}

return new SettingsUrl { Name = "", Url = "https://explorer.angor.io/tbtc/explorer" };
throw new ApplicationException("Network not set");
}

public List<SettingsUrl> GetDefaultIndexerUrls()
{
return new List<SettingsUrl>
if (currentNetwork.NetworkType == NetworkType.Testnet)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just replace the subdomain in the URL if the rest stays the same?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it may not always be the case, it is just simpler this way.

{
new SettingsUrl { Name = "", Url = "https://tbtc.indexer.angor.io", IsPrimary = true },
};
return new List<SettingsUrl>
{
new SettingsUrl { Name = "", Url = "https://tbtc.indexer.angor.io", IsPrimary = true },
};
}

if (currentNetwork.NetworkType == NetworkType.Mainnet)
{
return new List<SettingsUrl>
{
new SettingsUrl { Name = "", Url = "https://btc.indexer.angor.io", IsPrimary = true },
};
}

throw new ApplicationException("Network not set");

}

public List<SettingsUrl> GetDefaultRelayUrls()
Expand All @@ -45,58 +91,23 @@ public List<SettingsUrl> GetDefaultRelayUrls()

public List<SettingsUrl> GetDefaultExplorerUrl()
{
return new List<SettingsUrl>
if (currentNetwork.NetworkType == NetworkType.Testnet)
{
new SettingsUrl { Name = "", Url = "https://explorer.angor.io/tbtc/explorer", IsPrimary = true },
};
}
return new List<SettingsUrl>
{
new SettingsUrl { Name = "", Url = "https://explorer.angor.io/tbtc/explorer", IsPrimary = true },
};
}

public static List<ProjectInfo> CreateFakeProjects()
{
return new List<ProjectInfo>
if (currentNetwork.NetworkType == NetworkType.Mainnet)
{
return new List<SettingsUrl>
{
new ProjectInfo
{
StartDate = DateTime.UtcNow,
PenaltyDays = 100,
ExpiryDate = DateTime.UtcNow,
TargetAmount = 300,
ProjectIdentifier = "angor" + Guid.NewGuid().ToString("N"),
Stages = new List<Stage>
{
new Stage { AmountToRelease = 10, ReleaseDate = DateTime.UtcNow.AddDays(1) },
new Stage { AmountToRelease = 30, ReleaseDate = DateTime.UtcNow.AddDays(2) },
new Stage { AmountToRelease = 60, ReleaseDate = DateTime.UtcNow.AddDays(3) },
}
},
new ProjectInfo
{
StartDate = DateTime.UtcNow,
PenaltyDays = 100,
ExpiryDate = DateTime.UtcNow,
TargetAmount = 200,
ProjectIdentifier = "angor" + Guid.NewGuid().ToString("N"),
Stages = new List<Stage>
{
new Stage { AmountToRelease = 10, ReleaseDate = DateTime.UtcNow.AddDays(1) },
new Stage { AmountToRelease = 30, ReleaseDate = DateTime.UtcNow.AddDays(2) },
new Stage { AmountToRelease = 60, ReleaseDate = DateTime.UtcNow.AddDays(3) },
}
},
new ProjectInfo
{
StartDate = DateTime.UtcNow,
PenaltyDays = 100,
ExpiryDate = DateTime.UtcNow,
TargetAmount = 100,
ProjectIdentifier = "angor" + Guid.NewGuid().ToString("N"),
Stages = new List<Stage>
{
new Stage { AmountToRelease = 10, ReleaseDate = DateTime.UtcNow.AddDays(1) },
new Stage { AmountToRelease = 30, ReleaseDate = DateTime.UtcNow.AddDays(2) },
new Stage { AmountToRelease = 60, ReleaseDate = DateTime.UtcNow.AddDays(3) },
}
},
new SettingsUrl { Name = "", Url = "https://explorer.angor.io/btc/explorer", IsPrimary = true },
};
}

throw new ApplicationException("Network not set");
}

}
20 changes: 20 additions & 0 deletions src/Angor/Client/Pages/Settings.razor
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
@inject IWalletStorage _walletStorage;
@inject NavMenuState NavMenuState
@inject ILogger<Settings> Logger;
@inject NavigationManager _navManager

@inherits BaseComponent

Expand Down Expand Up @@ -58,6 +59,17 @@
{
<p class="text-danger-emphasis">Please tick the box to wipe all storage!</p>
}

<hr />

<!-- Dropdown for network selection -->
<div class="mb-3">
<label for="networkSelection">Select Network:</label>
<select class="form-control" id="networkSelection" @bind="selectedNetwork">
<option value="testnet" selected>Testnet</option>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be data driven from the network configuration class?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can but I think it is not urgent rn, we will for now just have main/test.

<option value="mainnet">Mainnet</option>
</select>
</div>

<button class="btn btn-danger" @onclick="WipteAllData">Wipe Storage</button>
</div>
Expand Down Expand Up @@ -190,6 +202,7 @@
private bool showWipeallModal = false;
private bool confirmWipe = false;
private bool showConfirmWipeMessage = false;
private string selectedNetwork = "testnet"; // Default to "testnet"

private SettingsInfo settingsInfo;

Expand All @@ -201,6 +214,9 @@

networkType = _networkConfiguration.GetNetwork().Name;

if (!networkType.ToLower().Contains("test"))
selectedNetwork = "mainnet";

return base.OnInitializedAsync();
}

Expand Down Expand Up @@ -389,8 +405,12 @@
showWipeallModal = false;
showConfirmWipeMessage = false;

_networkService.CheckAndSetNetwork(_navManager.Uri.ToLower(), selectedNetwork);
_networkService.AddSettingsIfNotExist();

networkType = _networkConfiguration.GetNetwork().Name;
settingsInfo = _clientStorage.GetSettingsInfo();

hasWallet = _walletStorage.HasWallet();
StateHasChanged();

Expand Down
18 changes: 9 additions & 9 deletions src/Angor/Client/Pages/Wallet.razor
Original file line number Diff line number Diff line change
Expand Up @@ -947,15 +947,15 @@

var res = await _httpClient.GetAsync($"/api/faucet/send/{receiveAddress}");

if (res.IsSuccessStatusCode)
{
var trxhex = await res.Content.ReadAsStringAsync();
var trx = network.CreateTransaction(trxhex);
var unconfirmedInboundFunds = _cacheStorage.GetUnconfirmedInboundFunds();
unconfirmedInboundFunds.Add(new UtxoData { PendingSpent = true, address = receiveAddress, value = trx.Outputs.FirstOrDefault()?.Value.Satoshi ?? Money.Coins(50).Satoshi, outpoint = new Outpoint(trx.GetHash().ToString(), 0) });
_cacheStorage.SetUnconfirmedInboundFunds(unconfirmedInboundFunds);
accountBalanceInfo.UpdateAccountBalanceInfo(accountBalanceInfo.AccountInfo, unconfirmedInboundFunds);
}
res.EnsureSuccessStatusCode();

var trxhex = await res.Content.ReadAsStringAsync();
var trx = network.CreateTransaction(trxhex);
var unconfirmedInboundFunds = _cacheStorage.GetUnconfirmedInboundFunds();
unconfirmedInboundFunds.Add(new UtxoData { PendingSpent = true, address = receiveAddress, value = trx.Outputs.FirstOrDefault()?.Value.Satoshi ?? Money.Coins(50).Satoshi, outpoint = new Outpoint(trx.GetHash().ToString(), 0) });
_cacheStorage.SetUnconfirmedInboundFunds(unconfirmedInboundFunds);
accountBalanceInfo.UpdateAccountBalanceInfo(accountBalanceInfo.AccountInfo, unconfirmedInboundFunds);

}
catch (Exception e)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Angor/Client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
builder.Services.AddBlazoredLocalStorage();
builder.Services.AddBlazoredSessionStorage();

builder.Services.AddTransient<INetworkConfiguration, NetworkConfiguration>();
builder.Services.AddSingleton<INetworkConfiguration, NetworkConfiguration>();
builder.Services.AddTransient<IHdOperations, HdOperations>();
builder.Services.AddTransient <IClientStorage, ClientStorage>();
builder.Services.AddTransient<INetworkStorage, ClientStorage>();
Expand Down
20 changes: 17 additions & 3 deletions src/Angor/Client/Shared/MainLayout.razor
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
@using Angor.Client.Storage
@using System.Reflection
@using Angor.Shared
@using Angor.Shared.Networks
@using Angor.Shared.Services
@using Blockcore.Networks
@inherits LayoutComponentBase
@inject IJSRuntime Js;
@inject INetworkConfiguration _networkConfiguration
@inject INetworkService _networkService
@inject NavigationManager _navManager

<aside class="sidenav navbar navbar-vertical navbar-expand-xs border-0 border-radius-xl my-3 fixed-start ms-3 b-shadow bg-gradient-dark"
id="sidenav-main">
Expand All @@ -16,7 +23,7 @@
<nav>
<div class="mb-0 pb-0 pt-1 px-0 me-sm-6 me-5">
<div class="text-sm">
<h6 class="font-weight-bolder" title="Alpha app - only use test wallets.">Only use test wallets.</h6>
<h6 class="font-weight-bolder" title="Alpha app - only use test wallets.">Only use test wallets. @networkText</h6>
</div>
</div>
</nav>
Expand Down Expand Up @@ -77,6 +84,8 @@
ElementReference contentRef;
ElementReference footerRef;

string networkText = string.Empty;

private async Task InstallApp()
{
try
Expand All @@ -100,6 +109,13 @@

if (version != null) _softwareVersion = version.ToString();

_networkService.CheckAndSetNetwork(_navManager.Uri.ToLower());

networkText = "(mainnet)";
if (_networkConfiguration.GetNetwork().NetworkType != NetworkType.Mainnet)
{
networkText = "(testnet)";
}
}

/// <summary>
Expand All @@ -108,9 +124,7 @@
/// <param name="firstRender"></param>
protected override async Task OnAfterRenderAsync(bool firstRender)
{

await Js.InvokeVoidAsync("angor.setTheme");

}

}
11 changes: 11 additions & 0 deletions src/Angor/Client/Storage/ClientStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,15 @@ public void WipeStorage()
{
_storage.Clear();
}

public void SetNetwork(string network)
{
_storage.SetItem("network", network);

}

public string GetNetwork()
{
return _storage.GetItem<string>("network");
}
}
5 changes: 5 additions & 0 deletions src/Angor/Server/FaucetController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,10 @@ public void HandleException(Exception exception)
{
throw exception;
}

public void CheckAndSetNetwork(string url, string? setNetwork = null)
{
throw new NotImplementedException();
}
}
}
1 change: 1 addition & 0 deletions src/Angor/Shared/INetworkConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace Angor.Shared;
public interface INetworkConfiguration
{
Network GetNetwork();
void SetNetwork(Network network);
SettingsUrl GetIndexerUrl();
SettingsUrl GetExplorerUrl();
List<SettingsUrl> GetDefaultIndexerUrls();
Expand Down
4 changes: 4 additions & 0 deletions src/Angor/Shared/INetworkStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ public interface INetworkStorage
{
SettingsInfo GetSettings();
void SetSettings(SettingsInfo settingsInfo);

public void SetNetwork(string network);
public string GetNetwork();

}
1 change: 1 addition & 0 deletions src/Angor/Shared/Services/INetworkService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ public interface INetworkService
List<SettingsUrl> GetRelays();
void CheckAndHandleError(HttpResponseMessage httpResponseMessage);
void HandleException(Exception exception);
void CheckAndSetNetwork(string url, string? setNetwork = null);
}
Loading
Loading