btcpayserver/BTCPayServer/Models/WalletViewModels/WalletPSBTViewModel.cs

76 lines
2.3 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; }
public async Task<PSBT> GetPSBT(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();
}
}
if (SigningContext != null && !string.IsNullOrEmpty(SigningContext.PSBT))
{
PSBT = SigningContext.PSBT;
}
if (!string.IsNullOrEmpty(PSBT))
2019-05-12 13:13:52 +09:00
{
try
{
return NBitcoin.PSBT.Parse(PSBT, network);
}
catch
{ }
2019-05-12 13:13:52 +09:00
}
return null;
}
2019-05-11 00:29:29 +09:00
}
}