btcpayserver/BTCPayServer/Models/WalletViewModels/WalletPSBTViewModel.cs

96 lines
2.9 KiB
C#
Raw Normal View History

2020-07-14 09:23:28 +02:00
using System;
2019-05-11 00:29:29 +09:00
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.IO;
2019-05-11 00:29:29 +09:00
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
2019-05-12 13:13:52 +09:00
using NBitcoin;
2019-05-11 00:29:29 +09:00
namespace BTCPayServer.Models.WalletViewModels
{
2021-07-27 17:01:00 +02:00
public class WalletPSBTViewModel : WalletPSBTReadyViewModel
2019-05-11 00:29:29 +09:00
{
public string CryptoCode { get; set; }
2019-05-11 00:29:29 +09:00
public string Decoded { get; set; }
2019-05-13 08:55:26 +09:00
string _FileName;
public string PSBTHex { get; set; }
2020-01-21 09:33:12 +01:00
public bool NBXSeedAvailable { get; set; }
2019-05-13 08:55:26 +09:00
public string FileName
{
get
{
return string.IsNullOrEmpty(_FileName) ? "psbt-export.psbt" : _FileName;
}
set
{
_FileName = value;
}
}
[Display(Name = "PSBT content")]
2019-05-11 00:29:29 +09:00
public string PSBT { get; set; }
public List<string> Errors { get; set; } = new List<string>();
2019-05-12 13:13:52 +09:00
[Display(Name = "Upload PSBT from file")]
public IFormFile UploadedPSBTFile { get; set; }
2022-02-17 17:58:56 +09:00
public async Task<PSBT> GetPSBT(Network network)
2022-02-17 17:58:56 +09:00
{
var psbt = await GetPSBTCore(network);
if (psbt != null)
{
Decoded = psbt.ToString();
PSBTHex = psbt.ToHex();
PSBT = psbt.ToBase64();
if (SigningContext is null)
SigningContext = new SigningContextModel(psbt);
else
SigningContext.PSBT = psbt.ToBase64();
}
return psbt;
}
public bool InvalidPSBT { get; set; }
2022-02-17 17:58:56 +09:00
async Task<PSBT> GetPSBTCore(Network network)
2019-05-12 13:13:52 +09:00
{
if (UploadedPSBTFile != null)
{
if (UploadedPSBTFile.Length > 500 * 1024)
return null;
2020-07-14 09:23:28 +02:00
try
{
byte[] bytes = new byte[UploadedPSBTFile.Length];
await using (var stream = UploadedPSBTFile.OpenReadStream())
{
await stream.ReadAsync(bytes, 0, (int)UploadedPSBTFile.Length);
}
return NBitcoin.PSBT.Load(bytes, network);
}
catch (Exception)
{
using var stream = new StreamReader(UploadedPSBTFile.OpenReadStream());
PSBT = await stream.ReadToEndAsync();
2022-02-17 17:58:56 +09:00
InvalidPSBT = true;
2020-07-14 09:23:28 +02:00
}
}
if (SigningContext != null && !string.IsNullOrEmpty(SigningContext.PSBT))
{
PSBT = SigningContext.PSBT;
}
if (!string.IsNullOrEmpty(PSBT))
2019-05-12 13:13:52 +09:00
{
try
{
2022-02-17 17:58:56 +09:00
InvalidPSBT = false;
return NBitcoin.PSBT.Parse(PSBT, network);
}
catch
2022-02-17 17:58:56 +09:00
{ InvalidPSBT = true; }
2019-05-12 13:13:52 +09:00
}
return null;
}
2019-05-11 00:29:29 +09:00
}
}