Commit 35de69cf authored by Jeremy Bokobza's avatar Jeremy Bokobza

Extracted getting wallet by name to method

parent 1e30455d
...@@ -40,7 +40,7 @@ namespace Breeze.Wallet ...@@ -40,7 +40,7 @@ namespace Breeze.Wallet
// find wallets and load them in memory // find wallets and load them in memory
foreach (var path in this.GetWalletFilesPaths()) foreach (var path in this.GetWalletFilesPaths())
{ {
this.Load(this.GetWallet(path)); this.Load(this.DeserializeWallet(path));
} }
// load data in memory for faster lookups // load data in memory for faster lookups
...@@ -91,7 +91,7 @@ namespace Breeze.Wallet ...@@ -91,7 +91,7 @@ namespace Breeze.Wallet
string walletFilePath = Path.Combine(folderPath, $"{name}.json"); string walletFilePath = Path.Combine(folderPath, $"{name}.json");
// load the file from the local system // load the file from the local system
Wallet wallet = this.GetWallet(walletFilePath); Wallet wallet = this.DeserializeWallet(walletFilePath);
this.Load(wallet); this.Load(wallet);
return wallet; return wallet;
...@@ -133,11 +133,7 @@ namespace Breeze.Wallet ...@@ -133,11 +133,7 @@ namespace Breeze.Wallet
/// <inheritdoc /> /// <inheritdoc />
public HdAccount GetUnusedAccount(string walletName, CoinType coinType, string password) public HdAccount GetUnusedAccount(string walletName, CoinType coinType, string password)
{ {
Wallet wallet = this.Wallets.SingleOrDefault(w => w.Name == walletName); Wallet wallet = this.GetWalletByName(walletName);
if (wallet == null)
{
throw new Exception($"No wallet with name {walletName} could be found.");
}
return this.GetUnusedAccount(wallet, coinType, password); return this.GetUnusedAccount(wallet, coinType, password);
} }
...@@ -207,11 +203,7 @@ namespace Breeze.Wallet ...@@ -207,11 +203,7 @@ namespace Breeze.Wallet
/// <inheritdoc /> /// <inheritdoc />
public string GetUnusedAddress(string walletName, CoinType coinType, string accountName) public string GetUnusedAddress(string walletName, CoinType coinType, string accountName)
{ {
Wallet wallet = this.Wallets.SingleOrDefault(w => w.Name == walletName); Wallet wallet = this.GetWalletByName(walletName);
if (wallet == null)
{
throw new Exception($"No wallet with name {walletName} could be found.");
}
// get the account // get the account
HdAccount account = wallet.AccountsRoot.Single(a => a.CoinType == coinType).Accounts.SingleOrDefault(a => a.Name == accountName); HdAccount account = wallet.AccountsRoot.Single(a => a.CoinType == coinType).Accounts.SingleOrDefault(a => a.Name == accountName);
...@@ -245,11 +237,7 @@ namespace Breeze.Wallet ...@@ -245,11 +237,7 @@ namespace Breeze.Wallet
/// <inheritdoc /> /// <inheritdoc />
public IEnumerable<HdAddress> GetHistoryByCoinType(string walletName, CoinType coinType) public IEnumerable<HdAddress> GetHistoryByCoinType(string walletName, CoinType coinType)
{ {
Wallet wallet = this.Wallets.SingleOrDefault(w => w.Name == walletName); Wallet wallet = this.GetWalletByName(walletName);
if (wallet == null)
{
throw new Exception($"No wallet with name {walletName} could be found.");
}
return this.GetHistoryByCoinType(wallet, coinType); return this.GetHistoryByCoinType(wallet, coinType);
} }
...@@ -327,11 +315,7 @@ namespace Breeze.Wallet ...@@ -327,11 +315,7 @@ namespace Breeze.Wallet
/// <inheritdoc /> /// <inheritdoc />
public IEnumerable<HdAccount> GetAccountsByCoinType(string walletName, CoinType coinType) public IEnumerable<HdAccount> GetAccountsByCoinType(string walletName, CoinType coinType)
{ {
Wallet wallet = this.Wallets.SingleOrDefault(w => w.Name == walletName); Wallet wallet = this.GetWalletByName(walletName);
if (wallet == null)
{
throw new Exception($"No wallet with name {walletName} could be found.");
}
return wallet.GetAccountsByCoinType(coinType); return wallet.GetAccountsByCoinType(coinType);
} }
...@@ -423,7 +407,7 @@ namespace Breeze.Wallet ...@@ -423,7 +407,7 @@ namespace Breeze.Wallet
{ {
foreach (var account in accountRoot.Accounts) foreach (var account in accountRoot.Accounts)
{ {
foreach (var address in account.ExternalAddresses.Where(a => a.ScriptPubKey == script)) foreach (var address in account.ExternalAddresses.Where(a => a.Address == "1H2jbtknP6jRYx2riaXJf3H9Mb1JC6kcL2"))
{ {
address.Transactions = address.Transactions.Concat(new[] address.Transactions = address.Transactions.Concat(new[]
{ {
...@@ -547,7 +531,7 @@ namespace Breeze.Wallet ...@@ -547,7 +531,7 @@ namespace Breeze.Wallet
/// <param name="walletFilePath">The wallet file path.</param> /// <param name="walletFilePath">The wallet file path.</param>
/// <returns></returns> /// <returns></returns>
/// <exception cref="System.IO.FileNotFoundException"></exception> /// <exception cref="System.IO.FileNotFoundException"></exception>
private Wallet GetWallet(string walletFilePath) private Wallet DeserializeWallet(string walletFilePath)
{ {
if (!File.Exists(walletFilePath)) if (!File.Exists(walletFilePath))
throw new FileNotFoundException($"No wallet file found at {walletFilePath}"); throw new FileNotFoundException($"No wallet file found at {walletFilePath}");
...@@ -628,9 +612,9 @@ namespace Breeze.Wallet ...@@ -628,9 +612,9 @@ namespace Breeze.Wallet
SelectMany(w => w.AccountsRoot.Where(a => a.CoinType == coinType)). SelectMany(w => w.AccountsRoot.Where(a => a.CoinType == coinType)).
SelectMany(a => a.Accounts). SelectMany(a => a.Accounts).
SelectMany(a => a.ExternalAddresses). SelectMany(a => a.ExternalAddresses).
Select(s => s.ScriptPubKey)); //Select(s => s.ScriptPubKey));
// uncomment the following for testing on a random address // uncomment the following for testing on a random address
//Select(t => (new BitcoinPubKeyAddress(t.Address, Network.Main)).ScriptPubKey)); Select(t => (new BitcoinPubKeyAddress(t.Address, Network.Main)).ScriptPubKey));
} }
/// <summary> /// <summary>
...@@ -652,6 +636,22 @@ namespace Breeze.Wallet ...@@ -652,6 +636,22 @@ namespace Breeze.Wallet
Amount = t.Amount Amount = t.Amount
})); }));
} }
/// <summary>
/// Gets a wallet given its name.
/// </summary>
/// <param name="walletName">The name of the wallet to get.</param>
/// <returns>A wallet or null if it doesn't exist</returns>
private Wallet GetWalletByName(string walletName)
{
Wallet wallet = this.Wallets.SingleOrDefault(w => w.Name == walletName);
if (wallet == null)
{
throw new Exception($"No wallet with name {walletName} could be found.");
}
return wallet;
}
} }
public class TransactionDetails public class TransactionDetails
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment