From e479522d9f1343429d58886d2786d4828c7d3bc1 Mon Sep 17 00:00:00 2001 From: Nicolas Dorier Date: Wed, 15 Jan 2025 23:06:17 +0900 Subject: [PATCH] Fix: Migration bug on old instances (Could not cast or convert from System.String to BTCPayServer.DerivationSchemeSettings) (#6551) --- BTCPayServer/Hosting/MigrationStartupTask.cs | 31 ++++++++++++++++++++ BTCPayServer/Services/MigrationSettings.cs | 3 +- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/BTCPayServer/Hosting/MigrationStartupTask.cs b/BTCPayServer/Hosting/MigrationStartupTask.cs index 7092b76b8..c094054a7 100644 --- a/BTCPayServer/Hosting/MigrationStartupTask.cs +++ b/BTCPayServer/Hosting/MigrationStartupTask.cs @@ -224,6 +224,12 @@ namespace BTCPayServer.Hosting settings.MigrateStoreExcludedPaymentMethods = true; await _Settings.UpdateSetting(settings); } + if (!settings.MigrateOldDerivationSchemes) + { + await MigrateOldDerivationSchemes(); + settings.MigrateOldDerivationSchemes = true; + await _Settings.UpdateSetting(settings); + } } catch (Exception ex) { @@ -232,6 +238,31 @@ namespace BTCPayServer.Hosting } } + private async Task MigrateOldDerivationSchemes() + { + await using var ctx = _DBContextFactory.CreateContext(); + var stores = await ctx.Stores.ToArrayAsync(); + foreach (var store in stores) + { + if (string.IsNullOrEmpty(store.DerivationStrategies)) + continue; + JObject strategies = JObject.Parse(store.DerivationStrategies); + if (strategies is null) + continue; + foreach (var prop in strategies.Properties()) + { + var h = _handlers.TryGet(new PaymentMethodId(prop.Name)); + if (h is not BitcoinLikePaymentHandler bh || + prop.Value is not JValue { Type: JTokenType.String, Value: string v }) + continue; + var settings = DerivationSchemeSettings.Parse(v, bh.Network); + prop.Value = JToken.FromObject(settings, bh.Serializer); + store.DerivationStrategies = strategies.ToString(); + } + } + await ctx.SaveChangesAsync(); + } + private async Task MigrateStoreExcludedPaymentMethods() { await using var ctx = _DBContextFactory.CreateContext(); diff --git a/BTCPayServer/Services/MigrationSettings.cs b/BTCPayServer/Services/MigrationSettings.cs index f9742bb3d..3ec657626 100644 --- a/BTCPayServer/Services/MigrationSettings.cs +++ b/BTCPayServer/Services/MigrationSettings.cs @@ -32,6 +32,7 @@ namespace BTCPayServer.Services public bool MigrateAppYmlToJson { get; set; } public bool MigrateToStoreConfig { get; set; } public bool MigrateBlockExplorerLinks { get; set; } - public bool MigrateStoreExcludedPaymentMethods { get; internal set; } + public bool MigrateStoreExcludedPaymentMethods { get; set; } + public bool MigrateOldDerivationSchemes { get; set; } } }