Skip to content

Commit ccdee82

Browse files
Prevent SamRock from crashing on invalid descriptors (#11)
Co-authored-by: FrancisPouliot <FrancisPouliot@users.noreply.github.com>
1 parent 256157d commit ccdee82

2 files changed

Lines changed: 19 additions & 8 deletions

File tree

Plugins/SamRockProtocol/Controllers/ProtocolController.cs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ private async Task<IActionResult> processSamRockProtocolRequest(SamRockProtocolR
152152
var descriptor = setupModel.BTC.Descriptor;
153153

154154
// Extract script type, fingerprint, derivation path, xpub, and address derivation suffix
155-
var match = Regex.Match(descriptor, @"^(\w+)\(\[([a-fA-F0-9]{8})/([^\]]+)\](xpub[^/\)]+)(/[^\)]+)?\)(?:#[a-zA-Z0-9]+)?");
155+
var match = Regex.Match(descriptor, @"^(\w+)\(\[([a-fA-F0-9]{8})/([^\]]+)\](xpub[^/\)]+)(/[^\)]+)?\)(?:#[a-zA-Z0-9]+)?$");
156156
if (!match.Success)
157157
{
158158
result.Results[key] = new SamRockProtocolResponse(false,
@@ -252,7 +252,15 @@ private async Task<IActionResult> processSamRockProtocolRequest(SamRockProtocolR
252252
{
253253
if (string.Equals(setupModel.BTCLN.Type, "Boltz", StringComparison.OrdinalIgnoreCase))
254254
{
255-
await boltzWrapper.SetBoltz(StoreId, setupModel.BTCLN.LBTC.Descriptor, result);
255+
if (string.IsNullOrWhiteSpace(setupModel.BTCLN.LBTC?.Descriptor))
256+
{
257+
result.Results[SamRockProtocolKeys.BTC_LN] = new SamRockProtocolResponse(false,
258+
"Boltz setup requires a Liquid descriptor.", null);
259+
}
260+
else
261+
{
262+
await boltzWrapper.SetBoltz(StoreId, setupModel.BTCLN.LBTC.Descriptor, result);
263+
}
256264
}
257265
else
258266
{
@@ -264,12 +272,11 @@ private async Task<IActionResult> processSamRockProtocolRequest(SamRockProtocolR
264272
// TODO: If both LBTC is set and BtcLn is set, need to generate as many addresses for LiquidChain
265273
// as we have in setupModel.BtcLn.LiquidAddresses.Length to reserve them
266274

267-
var allSuccess = result.Results.Values.All(a => a.Success);
275+
var allSuccess = result.Results.Count > 0 && result.Results.Values.All(a => a.Success);
268276
string errorMessage = null;
269-
if (!allSuccess && result.Results[SamRockProtocolKeys.BTC_LN] != null)
277+
if (!allSuccess && result.Results.TryGetValue(SamRockProtocolKeys.BTC_LN, out var lnResult))
270278
{
271-
var res = result.Results[SamRockProtocolKeys.BTC_LN];
272-
errorMessage = res.Message;
279+
errorMessage = lnResult.Message;
273280
}
274281

275282
samrockProtocolService.OtpUsed(otp, allSuccess, errorMessage);
@@ -278,8 +285,8 @@ private async Task<IActionResult> processSamRockProtocolRequest(SamRockProtocolR
278285

279286
return Ok(new
280287
{
281-
Success = true,
282-
Message = "Wallet setup successfully.",
288+
Success = allSuccess,
289+
Message = allSuccess ? "Wallet setup successfully." : "Wallet setup failed.",
283290
Result = result
284291
});
285292
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
using System;
2+
using Newtonsoft.Json;
23

34
namespace SamRockProtocol.Models;
45

56
public class SamRockProtocolResponse(bool success, string message, Exception exception)
67
{
78
public bool Success { get; set; } = success;
89
public string Message { get; set; } = message;
10+
public string Error { get; set; } = exception?.Message;
11+
12+
[JsonIgnore]
913
public Exception Exception { get; set; } = exception;
1014
}

0 commit comments

Comments
 (0)