Can sign and export arbitrary PSBT

This commit is contained in:
nicolas.dorier
2019-05-11 20:26:31 +09:00
parent 541c748ecb
commit 5f1aa619cd
2 changed files with 53 additions and 9 deletions

View File

@@ -233,25 +233,19 @@ namespace BTCPayServer.Controllers
if (command == "ledger")
{
return View("WalletSendLedger", new WalletSendLedgerModel()
{
PSBT = psbt.PSBT.ToBase64(),
HintChange = psbt.ChangeAddress?.ToString(),
WebsocketPath = this.Url.Action(nameof(LedgerConnection)),
SuccessPath = this.Url.Action(nameof(WalletSendLedgerSuccess))
});
return ViewWalletSendLedger(psbt.PSBT, psbt.ChangeAddress);
}
else
{
try
{
{
if (command == "analyze-psbt")
return View(nameof(WalletPSBT), new WalletPSBTViewModel()
{
Decoded = psbt.PSBT.ToString(),
PSBT = psbt.PSBT.ToBase64()
});
return File(psbt.PSBT.ToBytes(), "application/octet-stream", $"Send-{vm.Amount.Value}-{network.CryptoCode}-to-{destination[0].ToString()}.psbt");
return FilePSBT(psbt.PSBT, $"Send-{vm.Amount.Value}-{network.CryptoCode}-to-{destination[0].ToString()}.psbt");
}
catch (NBXplorerException ex)
{
@@ -266,6 +260,22 @@ namespace BTCPayServer.Controllers
}
}
private IActionResult FilePSBT(PSBT psbt, string fileName)
{
return File(psbt.ToBytes(), "application/octet-stream", fileName);
}
private ViewResult ViewWalletSendLedger(PSBT psbt, BitcoinAddress hintChange = null)
{
return View("WalletSendLedger", new WalletSendLedgerModel()
{
PSBT = psbt.ToBase64(),
HintChange = hintChange?.ToString(),
WebsocketPath = this.Url.Action(nameof(LedgerConnection)),
SuccessPath = this.Url.Action(nameof(WalletSendLedgerSuccess))
});
}
[NonAction]
public async Task<CreatePSBTResponse> CreatePSBT(BTCPayNetwork network, DerivationSchemeSettings derivationSettings, WalletSendModel sendModel, CancellationToken cancellationToken)
{
@@ -342,6 +352,26 @@ namespace BTCPayServer.Controllers
return View(vm);
}
[HttpPost]
[Route("{walletId}/psbt/sign")]
public IActionResult WalletPSBTSign(
[ModelBinder(typeof(WalletIdModelBinder))]
WalletId walletId,
WalletPSBTViewModel vm,
string command = null
)
{
var psbt = PSBT.Parse(vm.PSBT, NetworkProvider.GetNetwork(walletId.CryptoCode).NBitcoinNetwork);
if (command == "ledger")
{
return ViewWalletSendLedger(psbt);
}
else
{
return FilePSBT(psbt, "psbt-export.psbt");
}
}
[HttpGet]
[Route("{walletId}/rescan")]
public async Task<IActionResult> WalletRescan(

View File

@@ -10,6 +10,20 @@
{
<h3>Decoded PSBT</h3>
<pre><code class="json">@Model.Decoded</code></pre>
<div class="form-group">
<div class="dropdown" style="margin-top:16px;">
<button class="btn btn-primary dropdown-toggle" type="button" id="SendMenu" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Sign with...
</button>
<form method="post" asp-action="WalletPSBTSign">
<input type="hidden" asp-for="PSBT" />
<div class="dropdown-menu" aria-labelledby="SendMenu">
<button name="command" type="submit" class="dropdown-item" value="ledger">... your Ledger Wallet device</button>
<button name="command" type="submit" class="dropdown-item" value="save-psbt">... a wallet supporting PSBT files</button>
</div>
</form>
</div>
</div>
}
<h3>PSBT to decode</h3>
<form class="form-group" method="post" asp-action="WalletPSBT">