2019-11-05 13:40:06 -06:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
2019-11-07 17:06:16 -06:00
|
|
|
|
using System.Globalization;
|
2019-11-05 13:40:06 -06:00
|
|
|
|
using System.IO;
|
2019-11-07 17:06:16 -06:00
|
|
|
|
using System.Threading.Tasks;
|
2019-11-05 13:40:06 -06:00
|
|
|
|
using Newtonsoft.Json;
|
2019-11-04 22:04:35 -06:00
|
|
|
|
|
|
|
|
|
namespace BTCPayServer.Models.ServerViewModels
|
|
|
|
|
{
|
|
|
|
|
public class LndSeedBackupViewModel
|
|
|
|
|
{
|
|
|
|
|
public bool IsWalletUnlockPresent { get; set; }
|
|
|
|
|
|
|
|
|
|
public string WalletPassword { get; set; }
|
|
|
|
|
|
2019-11-15 17:55:55 +09:00
|
|
|
|
public string Seed { get; set; }
|
2019-11-07 17:06:16 -06:00
|
|
|
|
|
2019-11-15 17:55:55 +09:00
|
|
|
|
public bool Removed { get; set; }
|
2019-11-07 17:06:16 -06:00
|
|
|
|
public async Task<bool> RemoveSeedAndWrite(string lndSeedFilePath)
|
|
|
|
|
{
|
|
|
|
|
var removedDate = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ssZ", CultureInfo.InvariantCulture);
|
|
|
|
|
var seedFile = new LndSeedFile
|
|
|
|
|
{
|
|
|
|
|
wallet_password = WalletPassword,
|
|
|
|
|
cipher_seed_mnemonic = new List<string> { $"Seed removed on {removedDate}" }
|
|
|
|
|
};
|
|
|
|
|
var json = JsonConvert.SerializeObject(seedFile);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await File.WriteAllTextAsync(lndSeedFilePath, json);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
// file access exception and such
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-11-05 13:40:06 -06:00
|
|
|
|
public static LndSeedBackupViewModel Parse(string lndSeedFilePath)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (!String.IsNullOrEmpty(lndSeedFilePath) && File.Exists(lndSeedFilePath))
|
|
|
|
|
{
|
|
|
|
|
var unlockFileContents = File.ReadAllText(lndSeedFilePath);
|
|
|
|
|
var unlockFile = JsonConvert.DeserializeObject<LndSeedFile>(unlockFileContents);
|
|
|
|
|
|
|
|
|
|
if (!String.IsNullOrEmpty(unlockFile.wallet_password))
|
|
|
|
|
{
|
|
|
|
|
return new LndSeedBackupViewModel
|
|
|
|
|
{
|
|
|
|
|
WalletPassword = unlockFile.wallet_password,
|
2019-11-15 17:55:55 +09:00
|
|
|
|
Seed = string.Join(' ', unlockFile.cipher_seed_mnemonic),
|
2019-11-05 13:40:06 -06:00
|
|
|
|
IsWalletUnlockPresent = true,
|
2019-11-15 17:55:55 +09:00
|
|
|
|
Removed = unlockFile.cipher_seed_mnemonic.Count == 1
|
2019-11-05 13:40:06 -06:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new LndSeedBackupViewModel();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class LndSeedFile
|
|
|
|
|
{
|
|
|
|
|
public string wallet_password { get; set; }
|
|
|
|
|
public List<string> cipher_seed_mnemonic { get; set; }
|
|
|
|
|
}
|
2019-11-04 22:04:35 -06:00
|
|
|
|
}
|
|
|
|
|
}
|