Make wallets network agnostic
MODIFY
HiddenWallet.Daemon/Controllers/WalletController.cs
HiddenWallet.Daemon/Controllers/WalletController.cs
@@ -74,7 +74,7 @@ namespace HiddenWallet.Daemon.Controllers
try
{
- await Global.WalletWrapper.LoadAsync(request.Password);
+ await Global.WalletWrapper.LoadAsync(request.Password, Global.Config.Network);
return new ObjectResult(new SuccessResponse());
}

MODIFY
HiddenWallet.Daemon/Wrappers/WalletWrapper.cs
HiddenWallet.Daemon/Wrappers/WalletWrapper.cs
@@ -72,9 +72,9 @@ namespace HiddenWallet.Daemon.Wrappers
};
}
- public async Task LoadAsync(string password)
+ public async Task LoadAsync(string password, Network network)
{
- Safe safe = await Safe.LoadAsync(password, Global.Config.WalletFilePath);
+ Safe safe = await Safe.LoadAsync(password, Global.Config.WalletFilePath, network);
if (safe.Network != Global.Config.Network) throw new NotSupportedException("Network in the config file differs from the network in the wallet file");
if (!_walletJobTask.IsCompleted)

MODIFY
HiddenWallet.FullSpvWallet/HiddenWallet.FullSpvWallet.csproj
HiddenWallet.FullSpvWallet/HiddenWallet.FullSpvWallet.csproj
</PropertyGroup>
<ItemGroup>
- <PackageReference Include="DotNetTor" Version="2.3.0" />
+ <PackageReference Include="DotNetTor" Version="2.3.1" />
<PackageReference Include="QBitNinja.Client" Version="1.0.3.44" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="1.0.0-alpha2-final" /> </ItemGroup>

MODIFY
HiddenWallet.Tests/FullSpvWalletTests.cs
HiddenWallet.Tests/FullSpvWalletTests.cs
@@ -29,7 +29,7 @@ namespace HiddenWallet.Tests
Network network = Network.Main;
string path = $"Wallets/Empty{network}.json";
const string password = "";
- Safe safe = File.Exists(path) ? await Safe.LoadAsync(password, path) : (await Safe.CreateAsync(password, path, network)).Safe;
+ Safe safe = File.Exists(path) ? await Safe.LoadAsync(password, path, network) : (await Safe.CreateAsync(password, path, network)).Safe;
Debug.WriteLine($"Unique Safe ID: {safe.UniqueId}");
@@ -120,7 +120,7 @@ namespace HiddenWallet.Tests
const string password = "";
// I change it because I am using a very old wallet to test
Safe.EarliestPossibleCreationTime = DateTimeOffset.ParseExact("2016-12-18", "yyyy-MM-dd", CultureInfo.InvariantCulture);
- Safe safe = await Safe.LoadAsync(password, path);
+ Safe safe = await Safe.LoadAsync(password, path, network);
Assert.Equal(network, safe.Network);
Debug.WriteLine($"Unique Safe ID: {safe.UniqueId}");

MODIFY
HiddenWallet.Tests/SafeTests.cs
HiddenWallet.Tests/SafeTests.cs
@@ -28,7 +28,7 @@ namespace HiddenWallet.Tests
var result = await Safe.CreateAsync(password, path, network);
var safe = result.Safe;
var mnemonic = result.Mnemonic;
- var loadedSafe = await Safe.LoadAsync(password, path);
+ var loadedSafe = await Safe.LoadAsync(password, path, network);
var wantedCreation = DateTimeOffset.ParseExact("1998-01-01", "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal);
var recoverdSafe = await Safe.RecoverAsync(mnemonic, password, recoveredPath, network, wantedCreation);
@@ -76,7 +76,7 @@ namespace HiddenWallet.Tests
var result = await Safe.CreateAsync(password, path, network);
var safe = result.Safe;
var mnemonic = result.Mnemonic;
- var loadedSafe = await Safe.LoadAsync(password, path);
+ var loadedSafe = await Safe.LoadAsync(password, path, network);
var recoverdSafe = await Safe.RecoverAsync(mnemonic, password, "Wallets/RecoveredTestWallet.json", network, Safe.EarliestPossibleCreationTime);
try

MODIFY
HiddenWallet.Tests/SendTests.cs
HiddenWallet.Tests/SendTests.cs
@@ -24,7 +24,7 @@ namespace HiddenWallet.Tests
SafeAccount account = new SafeAccount(1);
string path = Path.Combine(Helpers.CommittedWalletsFolderPath, $"Sending{network}.json");
const string password = "";
- Safe safe = await Safe.LoadAsync(password, path);
+ Safe safe = await Safe.LoadAsync(password, path, network);
Assert.Equal(network, safe.Network);
Debug.WriteLine($"Unique Safe ID: {safe.UniqueId}");
@@ -428,7 +428,7 @@ namespace HiddenWallet.Tests
SafeAccount account = new SafeAccount(1);
string path = Path.Combine(Helpers.CommittedWalletsFolderPath, $"Sending{network}.json");
const string password = "";
- Safe safe = await Safe.LoadAsync(password, path);
+ Safe safe = await Safe.LoadAsync(password, path, network);
Assert.Equal(network, safe.Network);
Debug.WriteLine($"Unique Safe ID: {safe.UniqueId}");

MODIFY
HiddenWallet/HiddenWallet.csproj
HiddenWallet/HiddenWallet.csproj
<ItemGroup>
<PackageReference Include="ConcurrentHashSet" Version="1.0.2" />
- <PackageReference Include="NBitcoin" Version="4.0.0.47" />
+ <PackageReference Include="NBitcoin" Version="4.0.0.48" />
<PackageReference Include="Nito.AsyncEx.Coordination" Version="1.0.2" />
<PackageReference Include="Portable.BouncyCastle" Version="1.8.1.3" />
</ItemGroup>

MODIFY
HiddenWallet/KeyManagement/Safe.cs
HiddenWallet/KeyManagement/Safe.cs
@@ -172,11 +172,10 @@ namespace HiddenWallet.KeyManagement
walletFilePath,
encryptedBitcoinPrivateKeyString,
chainCodeString,
- networkString,
creationTimeString);
}
- public static async Task<Safe> LoadAsync(string password, string walletFilePath)
+ public static async Task<Safe> LoadAsync(string password, string walletFilePath, Network network)
{
if (!File.Exists(walletFilePath))
throw new ArgumentException($"No wallet file found at {walletFilePath}");
@@ -188,15 +187,26 @@ namespace HiddenWallet.KeyManagement
var chainCode = Convert.FromBase64String(chainCodeString);
- Network network;
- var networkString = walletFileRawContent.Network;
- network = networkString == Network.Main.ToString() ? Network.Main : Network.TestNet;
-
DateTimeOffset creationTime = DateTimeOffset.ParseExact(walletFileRawContent.CreationTime, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal);
var safe = new Safe(password, walletFilePath, network, creationTime);
- var privateKey = Key.Parse(encryptedBitcoinPrivateKeyString, password, safe.Network);
+ Key privateKey = null;
+ foreach (var n in Network.GetNetworks())
+ {
+ try
+ {
+ privateKey = Key.Parse(encryptedBitcoinPrivateKeyString, password, n);
+ break;
+ }
+ catch
+ {
+
+ }
+ }
+ // if private key still null throw the
+ privateKey = privateKey ?? Key.Parse(encryptedBitcoinPrivateKeyString, password, safe.Network);
+
var seedExtKey = new ExtKey(privateKey, chainCode);
safe.SetSeed(seedExtKey);

MODIFY
HiddenWallet/KeyManagement/WalletFileSerializer.cs
HiddenWallet/KeyManagement/WalletFileSerializer.cs
@@ -8,25 +8,22 @@ namespace HiddenWallet.KeyManagement
internal class WalletFileSerializer
{
[JsonConstructor]
- private WalletFileSerializer(string encryptedBitcoinPrivateKey, string chainCode, string network, string creationTime)
+ private WalletFileSerializer(string encryptedBitcoinPrivateKey, string chainCode, string creationTime)
{
EncryptedSeed = encryptedBitcoinPrivateKey;
ChainCode = chainCode;
- Network = network;
CreationTime = creationTime;
}
// KEEP THEM PUBLIC OTHERWISE IT WILL NOT SERIALIZE!
public string EncryptedSeed { get; set; }
public string ChainCode { get; set; }
- public string Network { get; set; }
public string CreationTime { get; set; }
- internal static async Task SerializeAsync(string walletFilePath, string encryptedBitcoinPrivateKey, string chainCode,
- string network, string creationTime)
+ internal static async Task SerializeAsync(string walletFilePath, string encryptedBitcoinPrivateKey, string chainCode, string creationTime)
{
var content =
- JsonConvert.SerializeObject(new WalletFileSerializer(encryptedBitcoinPrivateKey, chainCode, network, creationTime), Formatting.Indented);
+ JsonConvert.SerializeObject(new WalletFileSerializer(encryptedBitcoinPrivateKey, chainCode, creationTime), Formatting.Indented);
if (File.Exists(walletFilePath))
throw new NotSupportedException($"Wallet file already exists at {walletFilePath}");
@@ -45,8 +42,7 @@ namespace HiddenWallet.KeyManagement
var contentString = await File.ReadAllTextAsync(path);
var walletFileSerializer = JsonConvert.DeserializeObject<WalletFileSerializer>(contentString);
- return new WalletFileSerializer(walletFileSerializer.EncryptedSeed, walletFileSerializer.ChainCode,
- walletFileSerializer.Network, walletFileSerializer.CreationTime);
+ return new WalletFileSerializer(walletFileSerializer.EncryptedSeed, walletFileSerializer.ChainCode, walletFileSerializer.CreationTime);
}
}
}

Make wallets network agnostic