Skip to content

Commit

Permalink
Unifying the wallet database (#44)
Browse files Browse the repository at this point in the history
* Unifying the wallet database

* act on review
  • Loading branch information
dangershony authored Feb 7, 2024
1 parent c308535 commit ad3090e
Show file tree
Hide file tree
Showing 14 changed files with 79 additions and 82 deletions.
27 changes: 13 additions & 14 deletions src/Angor/Client/Pages/Browse.razor
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,27 @@
{
<div class="card mb-3">
<div class="card-body">
<h5 class="card-title">@findProject.ProjectIdentifier</h5>
<p class="card-text">Nostr ID: @(NostrPublicKey.FromHex(findProject.NostrPubKey).Bech32)</p>

@if (SessionStorage.IsProjectInStorageById(findProject.ProjectIdentifier))
{
@if (SessionStorage.IsProjectMetadataStorageByPubkey(findProject.NostrPubKey))
var project = SessionStorage.GetProjectById(findProject.ProjectIdentifier);
if (project?.Metadata != null)
{
var metadata = SessionStorage.GetProjectMetadataByPubkey(findProject.NostrPubKey);
<hr />
<h3 class="card-title">@metadata?.Name</h3>
<p class="card-subtitle">@metadata?.About</p>
<hr />
<h3 class="card-title">@project.Metadata.Name</h3>
<p class="card-subtitle">@project.Metadata.About</p>
<hr>
}

//var info = SessionStorage.GetProjectById(findProject.ProjectIdentifier);
<button @onclick="() => ViewProjectDetails(findProject.ProjectIdentifier)" class="btn btn-primary">View</button>
}
else
{
<p class="text-warning-emphasis">Project not found in any relay!</p>
}
<p>
Project ID:@findProject.ProjectIdentifier
<br/>
Nostr ID :@(NostrPublicKey.FromHex(findProject.NostrPubKey).Bech32)
</p>
<button @onclick="() => ViewProjectDetails(findProject.ProjectIdentifier)" class="btn btn-primary card-">View</button>

</div>
</div>
}
Expand Down Expand Up @@ -102,7 +101,7 @@
<div class="card-body">
@if (nostrSearchInProgress)
{
<div class="loader"></div>
<div class="loader-small"></div>
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/Angor/Client/Pages/CheckTransactionCode.razor
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ else
localAccountInfo = GetAccountInfoFromStorage();

ExtPubKey accountExtPubKey = ExtPubKey.Parse(localAccountInfo.ExtPubKey, network);
var walletWords = _walletStorage.GetWallet();
var walletWords = _walletStorage.GetWallet().WalletWords;
var extendedKey = _hdOperations.GetExtendedKey(walletWords.Words, walletWords.Passphrase);

var privateFounderKey = extendedKey.Derive(new KeyPath("m/84'/1'/0'/0/3")); //derive a different path for the founder keys testing
Expand Down
8 changes: 4 additions & 4 deletions src/Angor/Client/Pages/Create.razor
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@
return;
}

var nostrKey = _derivationOperations.DeriveProjectNostrPrivateKey(_walletStorage.GetWallet(), project.ProjectIndex);
var nostrKey = _derivationOperations.DeriveProjectNostrPrivateKey(_walletStorage.GetWallet().WalletWords, project.ProjectIndex);

await _RelayService.CreateNostrProfileAsync(project.Metadata.ToNostrMetadata(),
NBitcoin.DataEncoders.Encoders.Hex.EncodeData(nostrKey.ToBytes()),
Expand Down Expand Up @@ -370,7 +370,7 @@

unsignedTransaction = _founderTransactionActions.CreateNewProjectTransaction(project.ProjectInfo.FounderKey, _derivationOperations.AngorKeyToScript(project.ProjectInfo.ProjectIdentifier), 10000, project.ProjectInfo.NostrPubKey);

signedTransaction = _WalletOperations.AddInputsAndSignTransaction(accountInfo.GetNextChangeReceiveAddress(), unsignedTransaction, _walletStorage.GetWallet(), accountInfo, feeData.SelectedFeeEstimation);
signedTransaction = _WalletOperations.AddInputsAndSignTransaction(accountInfo.GetNextChangeReceiveAddress(), unsignedTransaction, _walletStorage.GetWallet().WalletWords, accountInfo, feeData.SelectedFeeEstimation);

project.ProjectInfo.CreationTransactionId = signedTransaction.GetHash().ToString();

Expand All @@ -394,7 +394,7 @@
var accountInfo = storage.GetAccountInfo(network.Name);
var unconfirmedInfo = _cacheStorage.GetUnconfirmedInboundFunds();

signedTransaction = _WalletOperations.AddInputsAndSignTransaction(accountInfo.GetNextChangeReceiveAddress(), unsignedTransaction, _walletStorage.GetWallet(), accountInfo, feeData.SelectedFeeEstimation);
signedTransaction = _WalletOperations.AddInputsAndSignTransaction(accountInfo.GetNextChangeReceiveAddress(), unsignedTransaction, _walletStorage.GetWallet().WalletWords, accountInfo, feeData.SelectedFeeEstimation);

StateHasChanged();
}
Expand All @@ -406,7 +406,7 @@
{
showCreateModal = false;

var nostrKey = _derivationOperations.DeriveProjectNostrPrivateKey(_walletStorage.GetWallet(), project.ProjectIndex);
var nostrKey = _derivationOperations.DeriveProjectNostrPrivateKey(_walletStorage.GetWallet().WalletWords, project.ProjectIndex);

var nostrKeyHex = NBitcoin.DataEncoders.Encoders.Hex.EncodeData(nostrKey.ToBytes());

Expand Down
14 changes: 8 additions & 6 deletions src/Angor/Client/Pages/Invest.razor
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@
{
var accountInfo = storage.GetAccountInfo(_networkConfiguration.GetNetwork().Name);

var nostrPrivateKey = _derivationOperations.DeriveProjectNostrPrivateKey(_walletStorage.GetWallet(), accountInfo.InvestmentsCount + 1);
var nostrPrivateKey = _derivationOperations.DeriveProjectNostrPrivateKey(_walletStorage.GetWallet().WalletWords, accountInfo.InvestmentsCount + 1);

var nostrPrivateKeyHex = Encoders.Hex.EncodeData(nostrPrivateKey.ToBytes());

Expand Down Expand Up @@ -383,16 +383,18 @@
feeData.FeeEstimations.Fees.AddRange(fetchFees);
feeData.SelectedFeeEstimation = feeData.FeeEstimations.Fees.First();

var investorKey = _derivationOperations.DeriveInvestorKey(_walletStorage.GetWallet(), project.ProjectInfo.FounderKey);
var walletWords = _walletStorage.GetWallet().WalletWords;

var investorKey = _derivationOperations.DeriveInvestorKey(walletWords, project.ProjectInfo.FounderKey);

if (Investment.IsSeeder)
{
var seederHash = _derivationOperations.DeriveSeederSecretHash(_walletStorage.GetWallet(), project.ProjectInfo.FounderKey);
var seederHash = _derivationOperations.DeriveSeederSecretHash(walletWords, project.ProjectInfo.FounderKey);
}

unSignedTransaction = _InvestorTransactionActions.CreateInvestmentTransaction(project.ProjectInfo, investorKey, Money.Coins(Investment.InvestmentAmount).Satoshi);

signedTransaction = _WalletOperations.AddInputsAndSignTransaction(accountInfo.GetNextChangeReceiveAddress(), unSignedTransaction, _walletStorage.GetWallet(), accountInfo, feeData.SelectedFeeEstimation);
signedTransaction = _WalletOperations.AddInputsAndSignTransaction(accountInfo.GetNextChangeReceiveAddress(), unSignedTransaction, walletWords, accountInfo, feeData.SelectedFeeEstimation);

return new OperationResult { Success = true };

Expand Down Expand Up @@ -422,7 +424,7 @@

var accountInfo = storage.GetAccountInfo(network.Name);

signedTransaction = _WalletOperations.AddInputsAndSignTransaction(accountInfo.GetNextChangeReceiveAddress(), unSignedTransaction, _walletStorage.GetWallet(), accountInfo, feeData.SelectedFeeEstimation);
signedTransaction = _WalletOperations.AddInputsAndSignTransaction(accountInfo.GetNextChangeReceiveAddress(), unSignedTransaction, _walletStorage.GetWallet().WalletWords, accountInfo, feeData.SelectedFeeEstimation);

StateHasChanged();
}
Expand Down Expand Up @@ -453,7 +455,7 @@

var accountInfo = storage.GetAccountInfo(network.Name);

var nostrPrivateKey = _derivationOperations.DeriveProjectNostrPrivateKey(_walletStorage.GetWallet(), accountInfo.InvestmentsCount + 1);
var nostrPrivateKey = _derivationOperations.DeriveProjectNostrPrivateKey(_walletStorage.GetWallet().WalletWords, accountInfo.InvestmentsCount + 1);

var nostrPrivateKeyHex = Encoders.Hex.EncodeData(nostrPrivateKey.ToBytes());

Expand Down
15 changes: 8 additions & 7 deletions src/Angor/Client/Pages/Recover.razor
Original file line number Diff line number Diff line change
Expand Up @@ -460,8 +460,9 @@
feeData.SelectedFeeEstimation = feeData.FeeEstimations.Fees.First();

var accountInfo = storage.GetAccountInfo(network.Name);
var walletWords = _walletStorage.GetWallet().WalletWords;

var investorPrivateKey = _derivationOperations.DeriveInvestorPrivateKey(_walletStorage.GetWallet(), project.ProjectInfo.FounderKey);
var investorPrivateKey = _derivationOperations.DeriveInvestorPrivateKey(walletWords, project.ProjectInfo.FounderKey);

unsignedRecoveryTransaction = _InvestorTransactionActions.AddSignaturesToRecoverSeederFundsTransaction(project.ProjectInfo, investmentTransaction, project.SignaturesInfo, Encoders.Hex.EncodeData(investorPrivateKey.ToBytes()));

Expand All @@ -481,7 +482,7 @@
foreach (var txIn in removeTxin) unsignedRecoveryTransaction.Inputs.Remove(txIn);

// add fee to the recovery trx
recoveryTransaction = _WalletOperations.AddFeeAndSignTransaction(accountInfo.GetNextChangeReceiveAddress(), unsignedRecoveryTransaction, _walletStorage.GetWallet(), accountInfo, feeData.SelectedFeeEstimation);
recoveryTransaction = _WalletOperations.AddFeeAndSignTransaction(accountInfo.GetNextChangeReceiveAddress(), unsignedRecoveryTransaction, walletWords, accountInfo, feeData.SelectedFeeEstimation);

Logger.LogInformation($"recoveryTransaction={recoveryTransaction.GetHash().ToString()}");

Expand Down Expand Up @@ -512,7 +513,7 @@

var accountInfo = storage.GetAccountInfo(network.Name);

recoveryTransaction = _WalletOperations.AddFeeAndSignTransaction(accountInfo.GetNextChangeReceiveAddress(), unsignedRecoveryTransaction, _walletStorage.GetWallet(), accountInfo, feeData.SelectedFeeEstimation);
recoveryTransaction = _WalletOperations.AddFeeAndSignTransaction(accountInfo.GetNextChangeReceiveAddress(), unsignedRecoveryTransaction, _walletStorage.GetWallet().WalletWords, accountInfo, feeData.SelectedFeeEstimation);

StateHasChanged();
}
Expand Down Expand Up @@ -564,7 +565,7 @@

var accountInfo = storage.GetAccountInfo(network.Name);

var investorPrivateKey = _derivationOperations.DeriveInvestorPrivateKey(_walletStorage.GetWallet(), project.ProjectInfo.FounderKey);
var investorPrivateKey = _derivationOperations.DeriveInvestorPrivateKey(_walletStorage.GetWallet().WalletWords, project.ProjectInfo.FounderKey);

if (recoveryTransaction == null)
{
Expand Down Expand Up @@ -604,7 +605,7 @@

var accountInfo = storage.GetAccountInfo(network.Name);

var investorPrivateKey = _derivationOperations.DeriveInvestorPrivateKey(_walletStorage.GetWallet(), project.ProjectInfo.FounderKey);
var investorPrivateKey = _derivationOperations.DeriveInvestorPrivateKey(_walletStorage.GetWallet().WalletWords, project.ProjectInfo.FounderKey);

releaseRecoveryTransaction = _InvestorTransactionActions.BuildAndSignRecoverReleaseFundsTransaction(project.ProjectInfo, investmentTransaction, recoveryTransaction,
accountInfo.GetNextChangeReceiveAddress(), feeData.SelectedFeeEstimation, Encoders.Hex.EncodeData(investorPrivateKey.ToBytes()));
Expand Down Expand Up @@ -657,7 +658,7 @@

var accountInfo = storage.GetAccountInfo(network.Name);

var investorPrivateKey = _derivationOperations.DeriveInvestorPrivateKey(_walletStorage.GetWallet(), project.ProjectInfo.FounderKey);
var investorPrivateKey = _derivationOperations.DeriveInvestorPrivateKey(_walletStorage.GetWallet().WalletWords, project.ProjectInfo.FounderKey);

var fromStage = StageInfo.Items.First(f => f.IsSpent == false);

Expand Down Expand Up @@ -693,7 +694,7 @@

var accountInfo = storage.GetAccountInfo(network.Name);

var investorPrivateKey = _derivationOperations.DeriveInvestorPrivateKey(_walletStorage.GetWallet(), project.ProjectInfo.FounderKey);
var investorPrivateKey = _derivationOperations.DeriveInvestorPrivateKey(_walletStorage.GetWallet().WalletWords, project.ProjectInfo.FounderKey);

var fromStage = StageInfo.Items.First(f => f.IsSpent == false);

Expand Down
2 changes: 1 addition & 1 deletion src/Angor/Client/Pages/Settings.razor
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
<br/>
@if (showConfirmWipeMessage)
{
<p class="text-bg-danger">Please tick the box to wipe all storage!</p>
<p class="text-danger-emphasis">Please tick the box to wipe all storage!</p>
}

<button class="btn btn-danger" @onclick="WipteAllData">Wipe Storage</button>
Expand Down
8 changes: 5 additions & 3 deletions src/Angor/Client/Pages/Signatures.razor
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@
return;
}

var nostrPrivateKey = await DerivationOperations.DeriveProjectNostrPrivateKeyAsync(_walletStorage.GetWallet(), FounderProject.ProjectIndex);
var nostrPrivateKey = await DerivationOperations.DeriveProjectNostrPrivateKeyAsync(_walletStorage.GetWallet().WalletWords, FounderProject.ProjectIndex);

var nostrPrivateKeyHex = Encoders.Hex.EncodeData(nostrPrivateKey.ToBytes());

Expand Down Expand Up @@ -278,13 +278,15 @@
{
var operationResult = await notificationComponent.LongOperation(async () =>
{
var key = DerivationOperations.DeriveFounderRecoveryPrivateKey(_walletStorage.GetWallet(), FounderProject.ProjectIndex);
var walletWords = _walletStorage.GetWallet().WalletWords;

var key = DerivationOperations.DeriveFounderRecoveryPrivateKey(walletWords, FounderProject.ProjectIndex);

var signatureInfo = signProject(signature.TransactionHex, FounderProject.ProjectInfo, Encoders.Hex.EncodeData(key.ToBytes()));

var sigJson = JsonSerializer.Serialize(signatureInfo, RelayService.settings);

var nostrPrivateKey = await DerivationOperations.DeriveProjectNostrPrivateKeyAsync(_walletStorage.GetWallet(), FounderProject.ProjectIndex);
var nostrPrivateKey = await DerivationOperations.DeriveProjectNostrPrivateKeyAsync(walletWords, FounderProject.ProjectIndex);

var nostrPrivateKeyHex = Encoders.Hex.EncodeData(nostrPrivateKey.ToBytes());

Expand Down
4 changes: 2 additions & 2 deletions src/Angor/Client/Pages/Spend.razor
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@

var keys = _walletStorage.GetFounderKeys().Keys.First(k => k.ProjectIdentifier == project.ProjectIdentifier);

var key = _derivationOperations.DeriveFounderPrivateKey(_walletStorage.GetWallet(), keys.Index);
var key = _derivationOperations.DeriveFounderPrivateKey(_walletStorage.GetWallet().WalletWords, keys.Index);

signedTransaction = _founderTransactionActions.SpendFounderStage(founderContext.ProjectInfo, founderContext.InvestmentTrasnactionsHex, selectedStageId, addressScript, Encoders.Hex.EncodeData(key.ToBytes()), feeData.SelectedFeeEstimation);

Expand Down Expand Up @@ -356,7 +356,7 @@
var addressScript = BitcoinWitPubKeyAddress.Create(address, network).ScriptPubKey;

var keys = _walletStorage.GetFounderKeys().Keys.First(k => k.ProjectIdentifier == project.ProjectIdentifier);
var key = _derivationOperations.DeriveFounderPrivateKey(_walletStorage.GetWallet(), keys.Index);
var key = _derivationOperations.DeriveFounderPrivateKey(_walletStorage.GetWallet().WalletWords, keys.Index);

signedTransaction = _founderTransactionActions.SpendFounderStage(founderContext.ProjectInfo, founderContext.InvestmentTrasnactionsHex, selectedStageId, addressScript, Encoders.Hex.EncodeData(key.ToBytes()), feeData.SelectedFeeEstimation);

Expand Down
2 changes: 1 addition & 1 deletion src/Angor/Client/Pages/View.razor
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@
{
if (project is FounderProject founderProject)
{
var nostrKey = _derivationOperations.DeriveProjectNostrPrivateKey(_walletStorage.GetWallet(), founderProject.ProjectIndex);
var nostrKey = _derivationOperations.DeriveProjectNostrPrivateKey(_walletStorage.GetWallet().WalletWords, founderProject.ProjectIndex);
var nsec = NBitcoin.DataEncoders.Encoders.Hex.EncodeData(nostrKey.ToBytes());
await _clipboardService.WriteTextAsync(nsec);
StateHasChanged();
Expand Down
12 changes: 5 additions & 7 deletions src/Angor/Client/Pages/Wallet.razor
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@

@if (accountBalanceInfo.TotalBalanceReserved > 0)
{
<div class="mb-3 text-primary">
<div class="mb-3 text-success">
<span class="fs-6">Reserved balance: </span>
<span class="fs-5"> @Money.Satoshis(accountBalanceInfo.TotalBalanceReserved).ToUnit(MoneyUnit.BTC) @network.CoinTicker</span>
</div>
Expand Down Expand Up @@ -501,8 +501,8 @@
WalletWords data = new WalletWords { Words = newWalletWords, Passphrase = newWalletWordsPassphrase };
var accountInfo = _walletOperations.BuildAccountInfoForWalletWords(data);
await _walletOperations.UpdateAccountInfoWithNewAddressesAsync(accountInfo);
_walletStorage.SaveWalletWords(data);

_walletStorage.SaveWalletWords(new Angor.Shared.Models.Wallet { WalletWords = data });
storage.SetAccountInfo(network.Name,accountInfo);
accountBalanceInfo.UpdateAccountBalanceInfo(accountInfo, new List<UtxoData>());

Expand All @@ -525,7 +525,7 @@

private void ShowWords()
{
var data = _walletStorage.GetWallet();
var data = _walletStorage.GetWallet().WalletWords;
walletWords = data.Words;
walletWordsPassphrase = data.Passphrase;
StateHasChanged();
Expand Down Expand Up @@ -561,9 +561,7 @@
walletWordsModal = false;
storage.DeleteAccountInfo(network.Name);
_cacheStorage.DeleteUnconfirmedInfo();
_walletStorage.DeleteWalletPubkey();
_walletStorage.DeleteWallet();
_walletStorage.DeleteFounderKeys();
hasWallet = _walletStorage.HasWallet();
ClearWalletWords();
StateHasChanged();
Expand Down Expand Up @@ -676,7 +674,7 @@
var accountInfo = storage.GetAccountInfo(network.Name);
var unconfirmedInfo = _cacheStorage.GetUnconfirmedInboundFunds();

var res = await _walletOperations.SendAmountToAddress(wallet, _sendInfo);
var res = await _walletOperations.SendAmountToAddress(wallet.WalletWords, _sendInfo);

if (res.Success)
{
Expand Down
7 changes: 2 additions & 5 deletions src/Angor/Client/Storage/IWalletStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@ namespace Angor.Client.Storage;
public interface IWalletStorage
{
bool HasWallet();
void SaveWalletWords(WalletWords walletWords);
void SaveWalletWords(Wallet wallet);
void DeleteWallet();
WalletWords GetWallet();
Wallet GetWallet();


void SetFounderKeys(FounderKeyCollection founderPubKeys);
FounderKeyCollection GetFounderKeys();
void DeleteFounderKeys();
string? GetWalletPubkey();
void DeleteWalletPubkey();
}
Loading

0 comments on commit ad3090e

Please sign in to comment.