using SteamKit2; namespace CVRManager.Services; public static class BranchCodeService { public static async Task> ResolveAsync(string code, string account, string refreshToken, CancellationToken ct) { await using var link = await SteamLink.OpenAsync(account, refreshToken, ct); return await ResolveAsync(link, code); } public static async Task> ResolveAsync(SteamLink link, string code) { var call = link.Apps.CheckAppBetaPassword((uint)Paths.AppId, code).ToTask(); if (await Task.WhenAny(call, Task.Delay(TimeSpan.FromSeconds(25))) != call) throw new TimeoutException("Steam did not answer the code check. It limits how often codes can be checked; try again in a few minutes."); var reply = await call; if (reply.Result == EResult.OK) return new Dictionary(reply.BetaPasswords); throw new InvalidOperationException(reply.Result switch { EResult.InvalidPassword => "Steam did not accept that access code.", EResult.RateLimitExceeded => "Steam is rate limiting code checks. Wait a few minutes and try again.", _ => $"Steam could not check the code ({reply.Result}){(link.IsAnonymous ? "; sign in to Steam in Settings and try again" : "")}.", }); } public static string ProtectKey(byte[] key) => TokenVault.Protect(Convert.ToBase64String(key)); public static byte[]? UnprotectKey(string stored) { var text = TokenVault.Unprotect(stored); try { return text.Length == 0 ? null : Convert.FromBase64String(text); } catch (FormatException) { return null; } } }