Handle "pin already prompted" error. (Fix #1209)

This commit is contained in:
nicolas.dorier
2019-12-03 13:53:50 +09:00
parent f94daed06d
commit 71a8166027
3 changed files with 20 additions and 8 deletions

View File

@@ -64,7 +64,7 @@ namespace BTCPayServer.Controllers
HwiEnumerateEntry deviceEntry = null; HwiEnumerateEntry deviceEntry = null;
HDFingerprint? fingerprint = null; HDFingerprint? fingerprint = null;
string password = null; string password = null;
int? pin = null; bool pinProvided = false;
var websocketHelper = new WebSocketHelper(websocket); var websocketHelper = new WebSocketHelper(websocket);
async Task<bool> RequireDeviceUnlocking() async Task<bool> RequireDeviceUnlocking()
@@ -80,7 +80,7 @@ namespace BTCPayServer.Controllers
return true; return true;
} }
if ((deviceEntry.Code is HwiErrorCode.DeviceNotReady || deviceEntry.NeedsPinSent is true) if ((deviceEntry.Code is HwiErrorCode.DeviceNotReady || deviceEntry.NeedsPinSent is true)
&& pin is null && !pinProvided
// Trezor T always show the pin on screen // Trezor T always show the pin on screen
&& (deviceEntry.Model != HardwareWalletModels.Trezor_T || deviceEntry.Model != HardwareWalletModels.Trezor_T_Simulator)) && (deviceEntry.Model != HardwareWalletModels.Trezor_T || deviceEntry.Model != HardwareWalletModels.Trezor_T_Simulator))
{ {
@@ -157,11 +157,21 @@ namespace BTCPayServer.Controllers
await websocketHelper.Send("{ \"error\": \"need-device\"}", cancellationToken); await websocketHelper.Send("{ \"error\": \"need-device\"}", cancellationToken);
continue; continue;
} }
await device.PromptPinAsync(cancellationToken); try
await websocketHelper.Send("{ \"info\": \"prompted, please input the pin\"}", cancellationToken);
pin = int.Parse(await websocketHelper.NextMessageAsync(cancellationToken), CultureInfo.InvariantCulture);
if (await device.SendPinAsync(pin.Value, cancellationToken))
{ {
await device.PromptPinAsync(cancellationToken);
}
catch (HwiException ex) when (ex.ErrorCode == HwiErrorCode.DeviceAlreadyUnlocked)
{
pinProvided = true;
await websocketHelper.Send("{ \"error\": \"device-already-unlocked\"}", cancellationToken);
continue;
}
await websocketHelper.Send("{ \"info\": \"prompted, please input the pin\"}", cancellationToken);
var pin = int.Parse(await websocketHelper.NextMessageAsync(cancellationToken), CultureInfo.InvariantCulture);
if (await device.SendPinAsync(pin, cancellationToken))
{
pinProvided = true;
await websocketHelper.Send("{ \"info\": \"the pin is correct\"}", cancellationToken); await websocketHelper.Send("{ \"info\": \"the pin is correct\"}", cancellationToken);
} }
else else
@@ -207,7 +217,7 @@ namespace BTCPayServer.Controllers
break; break;
case "ask-device": case "ask-device":
password = null; password = null;
pin = null; pinProvided = false;
deviceEntry = null; deviceEntry = null;
device = null; device = null;
var entries = (await hwi.EnumerateEntriesAsync(cancellationToken)).ToList(); var entries = (await hwi.EnumerateEntriesAsync(cancellationToken)).ToList();

View File

@@ -728,7 +728,7 @@ namespace BTCPayServer.Controllers
{ {
try try
{ {
return (await wallet.GetBalance(derivationStrategy, cts.Token)).ToString(); return (await wallet.GetBalance(derivationStrategy, cts.Token)).ToString(CultureInfo.InvariantCulture);
} }
catch catch
{ {

View File

@@ -256,6 +256,8 @@ var vaultui = (function () {
self.bridge.socket.send("ask-pin"); self.bridge.socket.send("ask-pin");
var json = await self.bridge.waitBackendMessage(); var json = await self.bridge.waitBackendMessage();
if (json.hasOwnProperty("error")) { if (json.hasOwnProperty("error")) {
if (json.error == "device-already-unlocked")
return true;
if (await needRetry(json)) if (await needRetry(json))
return await self.askForPin(); return await self.askForPin();
return false; return false;