Commit bbec71c8 authored by Jeremy Bokobza's avatar Jeremy Bokobza

Removed the WalletWrapper as we don't have Hbitcoin anymore

parent e6f7cf8f
...@@ -8,6 +8,7 @@ using Breeze.Wallet.Errors; ...@@ -8,6 +8,7 @@ using Breeze.Wallet.Errors;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Breeze.Wallet.Models; using Breeze.Wallet.Models;
using Breeze.Wallet.Wrappers; using Breeze.Wallet.Wrappers;
using NBitcoin;
namespace Breeze.Wallet.Controllers namespace Breeze.Wallet.Controllers
{ {
...@@ -17,11 +18,11 @@ namespace Breeze.Wallet.Controllers ...@@ -17,11 +18,11 @@ namespace Breeze.Wallet.Controllers
[Route("api/v{version:apiVersion}/[controller]")] [Route("api/v{version:apiVersion}/[controller]")]
public class WalletController : Controller public class WalletController : Controller
{ {
private readonly IWalletWrapper walletWrapper; private readonly IWalletManager walletManager;
public WalletController(IWalletWrapper walletWrapper) public WalletController(IWalletManager walletManager)
{ {
this.walletWrapper = walletWrapper; this.walletManager = walletManager;
} }
/// <summary> /// <summary>
...@@ -44,9 +45,9 @@ namespace Breeze.Wallet.Controllers ...@@ -44,9 +45,9 @@ namespace Breeze.Wallet.Controllers
{ {
// get the wallet folder // get the wallet folder
DirectoryInfo walletFolder = GetWalletFolder(request.FolderPath); DirectoryInfo walletFolder = GetWalletFolder(request.FolderPath);
Mnemonic mnemonic = this.walletManager.CreateWallet(request.Password, walletFolder.FullName, request.Name, request.Network);
var mnemonic = this.walletWrapper.Create(request.Password, walletFolder.FullName, request.Name, request.Network); return this.Json(mnemonic.ToString());
return this.Json(mnemonic);
} }
catch (InvalidOperationException e) catch (InvalidOperationException e)
{ {
...@@ -76,9 +77,13 @@ namespace Breeze.Wallet.Controllers ...@@ -76,9 +77,13 @@ namespace Breeze.Wallet.Controllers
// get the wallet folder // get the wallet folder
DirectoryInfo walletFolder = GetWalletFolder(request.FolderPath); DirectoryInfo walletFolder = GetWalletFolder(request.FolderPath);
var wallet = this.walletWrapper.Load(request.Password, walletFolder.FullName, request.Name); Wallet wallet = this.walletManager.LoadWallet(request.Password, walletFolder.FullName, request.Name);
return this.Json(wallet); return this.Json(new WalletModel
{
Network = wallet.Network.Name,
// Addresses = wallet.GetFirstNAddresses(10).Select(a => a.ToWif()),
FileName = wallet.WalletFilePath
});
} }
catch (FileNotFoundException e) catch (FileNotFoundException e)
{ {
...@@ -116,8 +121,13 @@ namespace Breeze.Wallet.Controllers ...@@ -116,8 +121,13 @@ namespace Breeze.Wallet.Controllers
// get the wallet folder // get the wallet folder
DirectoryInfo walletFolder = GetWalletFolder(request.FolderPath); DirectoryInfo walletFolder = GetWalletFolder(request.FolderPath);
var wallet = this.walletWrapper.Recover(request.Password, walletFolder.FullName, request.Name, request.Network, request.Mnemonic); Wallet wallet = this.walletManager.RecoverWallet(request.Password, walletFolder.FullName, request.Name, request.Network, request.Mnemonic);
return this.Json(wallet); return this.Json(new WalletModel
{
Network = wallet.Network.Name,
// Addresses = wallet.GetFirstNAddresses(10).Select(a => a.ToWif()),
FileName = wallet.WalletFilePath
});
} }
catch (InvalidOperationException e) catch (InvalidOperationException e)
{ {
...@@ -153,7 +163,7 @@ namespace Breeze.Wallet.Controllers ...@@ -153,7 +163,7 @@ namespace Breeze.Wallet.Controllers
try try
{ {
return this.Json(this.walletWrapper.GetGeneralInfo(model.Name)); return this.Json(this.walletManager.GetGeneralInfo(model.Name));
} }
catch (Exception e) catch (Exception e)
...@@ -180,7 +190,7 @@ namespace Breeze.Wallet.Controllers ...@@ -180,7 +190,7 @@ namespace Breeze.Wallet.Controllers
try try
{ {
return this.Json(this.walletWrapper.GetHistory(model.Name)); return this.Json(this.walletManager.GetHistory(model.Name));
} }
catch (Exception e) catch (Exception e)
...@@ -207,7 +217,7 @@ namespace Breeze.Wallet.Controllers ...@@ -207,7 +217,7 @@ namespace Breeze.Wallet.Controllers
try try
{ {
return this.Json(this.walletWrapper.GetBalance(model.Name)); return this.Json(this.walletManager.GetBalance(model.Name));
} }
catch (Exception e) catch (Exception e)
...@@ -234,7 +244,7 @@ namespace Breeze.Wallet.Controllers ...@@ -234,7 +244,7 @@ namespace Breeze.Wallet.Controllers
try try
{ {
return this.Json(this.walletWrapper.BuildTransaction(request.Password, request.Address, request.Amount, request.FeeType, request.AllowUnconfirmed)); return this.Json(this.walletManager.BuildTransaction(request.Password, request.Address, request.Amount, request.FeeType, request.AllowUnconfirmed));
} }
catch (Exception e) catch (Exception e)
...@@ -261,7 +271,7 @@ namespace Breeze.Wallet.Controllers ...@@ -261,7 +271,7 @@ namespace Breeze.Wallet.Controllers
try try
{ {
var result = this.walletWrapper.SendTransaction(request.Hex); var result = this.walletManager.SendTransaction(request.Hex);
if (result) if (result)
{ {
return this.Ok(); return this.Ok();
......
using System; using System;
using Breeze.Wallet.Models;
using NBitcoin; using NBitcoin;
namespace Breeze.Wallet namespace Breeze.Wallet
...@@ -12,36 +13,49 @@ namespace Breeze.Wallet ...@@ -12,36 +13,49 @@ namespace Breeze.Wallet
/// Creates a wallet and persist it as a file on the local system. /// Creates a wallet and persist it as a file on the local system.
/// </summary> /// </summary>
/// <param name="password">The password used to encrypt sensitive info.</param> /// <param name="password">The password used to encrypt sensitive info.</param>
/// <param name="passphrase">The passphrase used in the seed.</param> /// <param name="folderPath">The folder where the wallet will be saved.</param>
/// <param name="walletFilePath">The path where the wallet file will be created.</param> /// <param name="name">The name of the wallet.</param>
/// <param name="network">The network this wallet is for.</param> /// <param name="network">The network this wallet is for.</param>
/// <param name="passphrase">The passphrase used in the seed.</param>
/// <returns>A mnemonic defining the wallet's seed used to generate addresses.</returns> /// <returns>A mnemonic defining the wallet's seed used to generate addresses.</returns>
Mnemonic CreateWallet(string password, string walletFilePath, Network network, string passphrase = null); Mnemonic CreateWallet(string password, string folderPath, string name, string network, string passphrase = null);
/// <summary> /// <summary>
/// Loads a wallet from a file. /// Loads a wallet from a file.
/// </summary> /// </summary>
/// <param name="password">The password used to encrypt sensitive info.</param> /// <param name="password">The user's password.</param>
/// <param name="walletFilePath">The location of the wallet file.</param> /// <param name="folderPath">The folder where the wallet will be loaded.</param>
/// <param name="name">The name of the wallet.</param>
/// <returns>The wallet.</returns> /// <returns>The wallet.</returns>
Wallet LoadWallet(string password, string walletFilePath); Wallet LoadWallet(string password, string folderPath, string name);
/// <summary> /// <summary>
/// Recovers a wallet. /// Recovers a wallet.
/// </summary> /// </summary>
/// <param name="mnemonic">A mnemonic defining the wallet's seed used to generate addresses.</param> /// <param name="password">The user's password.</param>
/// <param name="password">The password used to encrypt sensitive info.</param> /// <param name="folderPath">The folder where the wallet will be loaded.</param>
/// <param name="walletFilePath">The location of the wallet file.</param> /// <param name="name">The name of the wallet.</param>
/// <param name="network">The network this wallet is for.</param> /// <param name="network">The network in which to creae this wallet</param>
/// <param name="mnemonic">The user's mnemonic for the wallet.</param>
/// <param name="passphrase">The passphrase used in the seed.</param> /// <param name="passphrase">The passphrase used in the seed.</param>
/// <param name="creationTime">The time this wallet was created.</param> /// <param name="creationTime">The time this wallet was created.</param>
/// <returns>The recovered wallet.</returns> /// <returns>The recovered wallet.</returns>
Wallet RecoverWallet(Mnemonic mnemonic, string password, string walletFilePath, Network network, string passphrase = null, DateTimeOffset? creationTime = null); Wallet RecoverWallet(string password, string folderPath, string name, string network, string mnemonic, string passphrase = null, DateTimeOffset? creationTime = null);
/// <summary> /// <summary>
/// Deleted a wallet. /// Deleted a wallet.
/// </summary> /// </summary>
/// <param name="walletFilePath">The location of the wallet file.</param> /// <param name="walletFilePath">The location of the wallet file.</param>
void DeleteWallet(string walletFilePath); void DeleteWallet(string walletFilePath);
WalletGeneralInfoModel GetGeneralInfo(string walletName);
WalletBalanceModel GetBalance(string walletName);
WalletHistoryModel GetHistory(string walletName);
WalletBuildTransactionModel BuildTransaction(string password, string address, Money amount, string feeType, bool allowUnconfirmed);
bool SendTransaction(string transactionHex);
} }
} }
...@@ -41,7 +41,6 @@ namespace Breeze.Wallet ...@@ -41,7 +41,6 @@ namespace Breeze.Wallet
.AddFeature<WalletFeature>() .AddFeature<WalletFeature>()
.FeatureServices(services => .FeatureServices(services =>
{ {
services.AddTransient<IWalletWrapper, WalletWrapper>();
services.AddSingleton<ITrackerWrapper, TrackerWrapper>(); services.AddSingleton<ITrackerWrapper, TrackerWrapper>();
services.AddSingleton<IWalletManager, WalletManager>(); services.AddSingleton<IWalletManager, WalletManager>();
services.AddSingleton<WalletController>(); services.AddSingleton<WalletController>();
......
using System; using System;
using System.IO; using System.IO;
using Breeze.Wallet.Helpers;
using Breeze.Wallet.Models;
using NBitcoin; using NBitcoin;
using Newtonsoft.Json; using Newtonsoft.Json;
...@@ -11,21 +13,31 @@ namespace Breeze.Wallet ...@@ -11,21 +13,31 @@ namespace Breeze.Wallet
public class WalletManager : IWalletManager public class WalletManager : IWalletManager
{ {
/// <inheritdoc /> /// <inheritdoc />
public Mnemonic CreateWallet(string password, string walletFilePath, Network network, string passphrase = null) public Mnemonic CreateWallet(string password, string folderPath, string name, string network, string passphrase = null)
{ {
string walletFilePath = Path.Combine(folderPath, $"{name}.json");
// for now the passphrase is set to be the password by default.
if (passphrase == null)
{
passphrase = password;
}
// generate the root seed used to generate keys from a mnemonic picked at random // generate the root seed used to generate keys from a mnemonic picked at random
// and a passphrase optionally provided by the user // and a passphrase optionally provided by the user
Mnemonic mnemonic = new Mnemonic(Wordlist.English, WordCount.Twelve); Mnemonic mnemonic = new Mnemonic(Wordlist.English, WordCount.Twelve);
ExtKey extendedKey = mnemonic.DeriveExtKey(passphrase); ExtKey extendedKey = mnemonic.DeriveExtKey(passphrase);
// create a wallet file // create a wallet file
Wallet wallet = this.GenerateWalletFile(password, walletFilePath, network, extendedKey); Wallet wallet = this.GenerateWalletFile(password, walletFilePath, WalletHelpers.GetNetwork(network), extendedKey);
return mnemonic; return mnemonic;
} }
/// <inheritdoc /> /// <inheritdoc />
public Wallet LoadWallet(string password, string walletFilePath) public Wallet LoadWallet(string password, string folderPath, string name)
{ {
string walletFilePath = Path.Combine(folderPath, $"{name}.json");
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}");
...@@ -35,16 +47,48 @@ namespace Breeze.Wallet ...@@ -35,16 +47,48 @@ namespace Breeze.Wallet
} }
/// <inheritdoc /> /// <inheritdoc />
public Wallet RecoverWallet(Mnemonic mnemonic, string password, string walletFilePath, Network network, string passphrase = null, DateTimeOffset? creationTime = null) public Wallet RecoverWallet(string password, string folderPath, string name, string network, string mnemonic, string passphrase = null, DateTimeOffset? creationTime = null)
{
// for now the passphrase is set to be the password by default.
if (passphrase == null)
{ {
passphrase = password;
}
// generate the root seed used to generate keys // generate the root seed used to generate keys
ExtKey extendedKey = mnemonic.DeriveExtKey(passphrase); ExtKey extendedKey = (new Mnemonic(mnemonic)).DeriveExtKey(passphrase);
// create a wallet file // create a wallet file
Wallet wallet = this.GenerateWalletFile(password, walletFilePath, network, extendedKey, creationTime); Wallet wallet = this.GenerateWalletFile(password, Path.Combine(folderPath, $"{name}.json"), WalletHelpers.GetNetwork(network), extendedKey, creationTime);
return wallet; return wallet;
} }
public WalletGeneralInfoModel GetGeneralInfo(string name)
{
throw new System.NotImplementedException();
}
public WalletBalanceModel GetBalance(string walletName)
{
throw new System.NotImplementedException();
}
public WalletHistoryModel GetHistory(string walletName)
{
throw new System.NotImplementedException();
}
public WalletBuildTransactionModel BuildTransaction(string password, string address, Money amount, string feeType,
bool allowUnconfirmed)
{
throw new System.NotImplementedException();
}
public bool SendTransaction(string transactionHex)
{
throw new System.NotImplementedException();
}
/// <inheritdoc /> /// <inheritdoc />
public void DeleteWallet(string walletFilePath) public void DeleteWallet(string walletFilePath)
{ {
......
using Breeze.Wallet.Models;
using NBitcoin;
namespace Breeze.Wallet.Wrappers
{
/// <summary>
/// An interface enabling wallet operations.
/// </summary>
public interface IWalletWrapper
{
string Create(string password, string folderPath, string name, string network);
WalletModel Load(string password, string folderPath, string name);
WalletModel Recover(string password, string folderPath, string name, string network, string mnemonic);
WalletGeneralInfoModel GetGeneralInfo(string walletName);
WalletBalanceModel GetBalance(string walletName);
WalletHistoryModel GetHistory(string walletName);
WalletBuildTransactionModel BuildTransaction(string password, string address, Money amount, string feeType, bool allowUnconfirmed);
bool SendTransaction(string transactionHex);
}
}
using System;
using System.IO;
using System.Linq;
using Breeze.Wallet.Helpers;
using Breeze.Wallet.Models;
using NBitcoin;
namespace Breeze.Wallet.Wrappers
{
/// <summary>
/// An implementation of the <see cref="IWalletWrapper"/> interface.
/// </summary>
public class WalletWrapper : IWalletWrapper
{
private readonly IWalletManager walletManager;
public WalletWrapper(IWalletManager walletManager)
{
this.walletManager = walletManager;
}
/// <summary>
/// Creates a wallet on the local device.
/// </summary>
/// <param name="password">The user's password.</param>
/// <param name="folderPath">The folder where the wallet will be saved.</param>
/// <param name="name">The name of the wallet.</param>
/// <param name="network">The network for which to create a wallet.</param>
/// <returns>A mnemonic allowing recovery of the wallet.</returns>
public string Create(string password, string folderPath, string name, string network)
{
Mnemonic mnemonic = this.walletManager.CreateWallet(password, Path.Combine(folderPath, $"{name}.json"), WalletHelpers.GetNetwork(network), password);
return mnemonic.ToString();
}
/// <summary>
/// Loads a wallet from the local device.
/// </summary>
/// <param name="password">The user's password.</param>
/// <param name="folderPath">The folder where the wallet will be loaded.</param>
/// <param name="name">The name of the wallet.</param>
/// <returns>The wallet loaded from the local device</returns>
public WalletModel Load(string password, string folderPath, string name)
{
Wallet wallet = this.walletManager.LoadWallet(password, Path.Combine(folderPath, $"{name}.json"));
//TODO review here which data should be returned
return new WalletModel
{
Network = wallet.Network.Name,
// Addresses = wallet.GetFirstNAddresses(10).Select(a => a.ToWif()),
FileName = wallet.WalletFilePath
};
}
/// <summary>
/// Recovers a wallet from the local device.
/// </summary>
/// <param name="password">The user's password.</param>
/// <param name="folderPath">The folder where the wallet will be loaded.</param>
/// <param name="name">The name of the wallet.</param>
/// <param name="network">The network in which to creae this wallet</param>
/// <param name="mnemonic">The user's mnemonic for the wallet.</param>
/// <returns></returns>
public WalletModel Recover(string password, string folderPath, string name, string network, string mnemonic)
{
Wallet wallet = this.walletManager.RecoverWallet(new Mnemonic(mnemonic), password, Path.Combine(folderPath, $"{name}.json"), WalletHelpers.GetNetwork(network), password);
//TODO review here which data should be returned
return new WalletModel
{
Network = wallet.Network.Name,
// Addresses = wallet.GetFirstNAddresses(10).Select(a => a.ToWif()),
FileName = wallet.WalletFilePath
};
}
public WalletGeneralInfoModel GetGeneralInfo(string name)
{
throw new System.NotImplementedException();
}
public WalletBalanceModel GetBalance(string walletName)
{
throw new System.NotImplementedException();
}
public WalletHistoryModel GetHistory(string walletName)
{
throw new System.NotImplementedException();
}
public WalletBuildTransactionModel BuildTransaction(string password, string address, Money amount, string feeType,
bool allowUnconfirmed)
{
throw new System.NotImplementedException();
}
public bool SendTransaction(string transactionHex)
{
throw new System.NotImplementedException();
}
}
}
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