Commit 04b41c22 authored by Jeremy Bokobza's avatar Jeremy Bokobza

Completed the GetNetwork method

parent ede7dece
using System;
using System.Collections.Generic;
using System.Text;
using Breeze.Wallet.Helpers;
using NBitcoin;
using Xunit;
namespace Breeze.Api.Tests
{
public class WalletHelpersTest
{
[Fact]
public void GetMainNetworkRetuirnsNetworkMain()
{
Network network = WalletHelpers.GetNetwork("main");
Assert.Equal(Network.Main, network);
}
[Fact]
public void GetMainNetNetworkRetuirnsNetworkMain()
{
Network network = WalletHelpers.GetNetwork("mainnet");
Assert.Equal(Network.Main, network);
}
[Fact]
public void GetTestNetworkRetuirnsNetworkTest()
{
Network network = WalletHelpers.GetNetwork("test");
Assert.Equal(Network.TestNet, network);
}
[Fact]
public void GetTestNetNetworkRetuirnsNetworkTest()
{
Network network = WalletHelpers.GetNetwork("testnet");
Assert.Equal(Network.TestNet, network);
}
[Fact]
public void GetNetworkIsCaseInsensitive()
{
Network testNetwork = WalletHelpers.GetNetwork("Test");
Assert.Equal(Network.TestNet, testNetwork);
Network mainNetwork = WalletHelpers.GetNetwork("MainNet");
Assert.Equal(Network.Main, mainNetwork);
}
[Fact]
public void WrongNetworkThrowsArgumentException()
{
var exception = Record.Exception(() => WalletHelpers.GetNetwork("myNetwork"));
Assert.NotNull(exception);
Assert.IsType<ArgumentException>(exception);
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using NBitcoin;
using Stratis.Bitcoin.Utilities;
namespace Breeze.Wallet.Helpers
{
/// <summary>
/// Contains a collection of helpers methods.
/// </summary>
public static class WalletHelpers
{
/// <summary>
/// Get the network on which to operate.
/// </summary>
/// <param name="network">The network</param>
/// <returns>A <see cref="Network"/> object.</returns>
public static Network GetNetwork(string network)
{
Guard.NotEmpty(network, nameof(network));
switch (network.ToLowerInvariant())
{
case "main":
case "mainnet":
return Network.Main;
case "test":
case "testnet":
return Network.TestNet;
default:
throw new ArgumentException($"Network '{network}' is not a valid network.");
}
}
}
}
using System.IO;
using System;
using System.IO;
using System.Linq;
using Breeze.Wallet.Helpers;
using Breeze.Wallet.Models;
using HBitcoin.KeyManagement;
using NBitcoin;
......@@ -22,7 +24,7 @@ namespace Breeze.Wallet.Wrappers
public string Create(string password, string folderPath, string name, string network)
{
Mnemonic mnemonic;
Safe wallet = Safe.Create(out mnemonic, password, Path.Combine(folderPath, $"{name}.json"), this.GetNetwork(network));
Safe wallet = Safe.Create(out mnemonic, password, Path.Combine(folderPath, $"{name}.json"), WalletHelpers.GetNetwork(network));
return mnemonic.ToString();
}
......@@ -57,7 +59,7 @@ namespace Breeze.Wallet.Wrappers
/// <returns></returns>
public WalletModel Recover(string password, string folderPath, string name, string network, string mnemonic)
{
Safe wallet = Safe.Recover(new Mnemonic(mnemonic), password, Path.Combine(folderPath, $"{name}.json"), this.GetNetwork(network));
Safe wallet = Safe.Recover(new Mnemonic(mnemonic), password, Path.Combine(folderPath, $"{name}.json"), WalletHelpers.GetNetwork(network));
//TODO review here which data should be returned
return new WalletModel
......@@ -68,19 +70,6 @@ namespace Breeze.Wallet.Wrappers
};
}
private Network GetNetwork(string network)
{
// any network different than MainNet will default to TestNet
switch (network.ToLowerInvariant())
{
case "main":
case "mainnet":
return Network.Main;
default:
return Network.TestNet;
}
}
public WalletInfoModel GetInfo(string name)
{
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