2022-07-04 06:20:08 +02:00
|
|
|
using System;
|
2019-05-12 13:13:52 +09:00
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2024-05-21 10:52:55 +09:00
|
|
|
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
2019-05-12 13:13:52 +09:00
|
|
|
using NBitcoin;
|
|
|
|
|
|
|
|
namespace BTCPayServer.Models.WalletViewModels
|
|
|
|
{
|
|
|
|
public class WalletPSBTCombineViewModel
|
|
|
|
{
|
|
|
|
public string OtherPSBT { get; set; }
|
2024-07-24 20:16:20 +09:00
|
|
|
[Display(Name = "PSBT to combine with…")]
|
2019-05-12 13:13:52 +09:00
|
|
|
public string PSBT { get; set; }
|
2024-07-24 20:16:20 +09:00
|
|
|
[Display(Name = "Upload PSBT from file…")]
|
2019-05-12 13:13:52 +09:00
|
|
|
public IFormFile UploadedPSBTFile { get; set; }
|
2023-01-06 14:18:07 +01:00
|
|
|
|
2022-07-04 06:20:08 +02:00
|
|
|
public string BackUrl { get; set; }
|
|
|
|
public string ReturnUrl { get; set; }
|
2019-05-12 13:13:52 +09:00
|
|
|
|
2024-05-21 10:52:55 +09:00
|
|
|
public PSBT GetSourcePSBT(Network network, ModelStateDictionary modelState)
|
2019-05-12 13:13:52 +09:00
|
|
|
{
|
|
|
|
if (!string.IsNullOrEmpty(OtherPSBT))
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return NBitcoin.PSBT.Parse(OtherPSBT, network);
|
|
|
|
}
|
2024-05-21 10:52:55 +09:00
|
|
|
catch (Exception ex)
|
|
|
|
{ modelState.AddModelError(nameof(OtherPSBT), ex.Message); }
|
2019-05-12 13:13:52 +09:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2024-05-21 10:52:55 +09:00
|
|
|
public async Task<PSBT> GetPSBT(Network network, ModelStateDictionary modelState)
|
2019-05-12 13:13:52 +09:00
|
|
|
{
|
|
|
|
if (UploadedPSBTFile != null)
|
|
|
|
{
|
|
|
|
if (UploadedPSBTFile.Length > 500 * 1024)
|
|
|
|
return null;
|
|
|
|
byte[] bytes = new byte[UploadedPSBTFile.Length];
|
|
|
|
using (var stream = UploadedPSBTFile.OpenReadStream())
|
|
|
|
{
|
|
|
|
await stream.ReadAsync(bytes, 0, (int)UploadedPSBTFile.Length);
|
|
|
|
}
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return NBitcoin.PSBT.Load(bytes, network);
|
|
|
|
}
|
2024-05-21 10:52:55 +09:00
|
|
|
catch (FormatException ex)
|
2019-05-12 13:13:52 +09:00
|
|
|
{
|
2024-05-21 10:52:55 +09:00
|
|
|
modelState.AddModelError(nameof(UploadedPSBTFile), ex.Message);
|
2019-05-12 13:13:52 +09:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(PSBT))
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return NBitcoin.PSBT.Parse(PSBT, network);
|
|
|
|
}
|
2024-05-21 10:52:55 +09:00
|
|
|
catch (FormatException ex)
|
|
|
|
{
|
|
|
|
modelState.AddModelError(nameof(UploadedPSBTFile), ex.Message);
|
|
|
|
}
|
2019-05-12 13:13:52 +09:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|