2020-06-29 04:44:35 +02:00
|
|
|
using System;
|
2018-12-20 08:52:04 +01:00
|
|
|
using System.IO;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
namespace BTCPayServer.Controllers
|
|
|
|
{
|
|
|
|
public class Macaroons
|
|
|
|
{
|
|
|
|
public class Macaroon
|
|
|
|
{
|
|
|
|
public Macaroon(byte[] bytes)
|
|
|
|
{
|
|
|
|
Bytes = bytes;
|
|
|
|
Hex = NBitcoin.DataEncoders.Encoders.Hex.EncodeData(bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
public string Hex { get; set; }
|
|
|
|
public byte[] Bytes { get; set; }
|
|
|
|
}
|
|
|
|
public static async Task<Macaroons> GetFromDirectoryAsync(string directoryPath)
|
|
|
|
{
|
2021-12-28 09:39:54 +01:00
|
|
|
ArgumentNullException.ThrowIfNull(directoryPath);
|
2018-12-20 08:52:04 +01:00
|
|
|
Macaroons macaroons = new Macaroons();
|
|
|
|
if (!Directory.Exists(directoryPath))
|
2019-03-01 06:33:32 +01:00
|
|
|
throw new DirectoryNotFoundException("Macaroons directory not found");
|
2020-06-28 10:55:27 +02:00
|
|
|
foreach (var file in Directory.GetFiles(directoryPath, "*.macaroon"))
|
2018-12-20 08:52:04 +01:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
switch (Path.GetFileName(file))
|
|
|
|
{
|
|
|
|
case "admin.macaroon":
|
|
|
|
macaroons.AdminMacaroon = new Macaroon(await File.ReadAllBytesAsync(file));
|
|
|
|
break;
|
|
|
|
case "readonly.macaroon":
|
|
|
|
macaroons.ReadonlyMacaroon = new Macaroon(await File.ReadAllBytesAsync(file));
|
|
|
|
break;
|
|
|
|
case "invoice.macaroon":
|
|
|
|
macaroons.InvoiceMacaroon = new Macaroon(await File.ReadAllBytesAsync(file));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch { }
|
|
|
|
}
|
|
|
|
return macaroons;
|
|
|
|
}
|
2019-03-01 05:20:21 +01:00
|
|
|
|
|
|
|
public Macaroons Clone()
|
|
|
|
{
|
|
|
|
return new Macaroons()
|
|
|
|
{
|
|
|
|
AdminMacaroon = AdminMacaroon,
|
|
|
|
InvoiceMacaroon = InvoiceMacaroon,
|
|
|
|
ReadonlyMacaroon = ReadonlyMacaroon
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-12-20 08:52:04 +01:00
|
|
|
public Macaroon ReadonlyMacaroon { get; set; }
|
|
|
|
|
|
|
|
public Macaroon InvoiceMacaroon { get; set; }
|
|
|
|
public Macaroon AdminMacaroon { get; set; }
|
|
|
|
}
|
|
|
|
}
|