diff --git a/Assets/Plugins/Web3AuthSDK/Api/Models/ProjectConfigResponse.cs b/Assets/Plugins/Web3AuthSDK/Api/Models/ProjectConfigResponse.cs new file mode 100644 index 0000000..5627de5 --- /dev/null +++ b/Assets/Plugins/Web3AuthSDK/Api/Models/ProjectConfigResponse.cs @@ -0,0 +1,16 @@ +using System.Collections.Generic; + +public class WhitelistResponse +{ + public List urls { get; set; } + public Dictionary signed_urls { get; set; } +} + +public class ProjectConfigResponse +{ + public WhiteLabelData whitelabel { get; set; } + public bool sms_otp_enabled { get; set; } + public bool wallet_connect_enabled { get; set; } + public string wallet_connect_project_id { get; set; } + public WhitelistResponse whitelist { get; set; } +} \ No newline at end of file diff --git a/Assets/Plugins/Web3AuthSDK/Api/Models/ProjectConfigResponse.cs.meta b/Assets/Plugins/Web3AuthSDK/Api/Models/ProjectConfigResponse.cs.meta new file mode 100644 index 0000000..3b2b739 --- /dev/null +++ b/Assets/Plugins/Web3AuthSDK/Api/Models/ProjectConfigResponse.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 3a1917453fdd945f0967a0f999ccdb58 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Plugins/Web3AuthSDK/Api/Web3AuthApi.cs b/Assets/Plugins/Web3AuthSDK/Api/Web3AuthApi.cs index 5872b89..987809f 100644 --- a/Assets/Plugins/Web3AuthSDK/Api/Web3AuthApi.cs +++ b/Assets/Plugins/Web3AuthSDK/Api/Web3AuthApi.cs @@ -4,6 +4,7 @@ using Newtonsoft.Json; using UnityEngine.Networking; using UnityEngine; +using System.Collections.Generic; public class Web3AuthApi { @@ -29,7 +30,7 @@ public IEnumerator authorizeSession(string key, Action callbac yield return request.SendWebRequest(); // Debug.Log("baseAddress =>" + baseAddress); // Debug.Log("key =>" + key); - // //Debug.Log("request URL =>"+ requestURL); + // Debug.Log("request URL =>"+ requestURL); // Debug.Log("request.isNetworkError =>" + request.isNetworkError); // Debug.Log("request.isHttpError =>" + request.isHttpError); // Debug.Log("request.isHttpError =>" + request.error); @@ -93,4 +94,32 @@ public IEnumerator createSession(LogoutApiRequest logoutApiRequest, Action callback) + { + //Debug.Log("network =>" + network); + string baseUrl = SIGNER_MAP[network]; + var requestURL = $"{baseUrl}/api/configuration?project_id={project_id}&network={network}&whitelist=true"; + var request = UnityWebRequest.Get(requestURL); + + yield return request.SendWebRequest(); + + if (request.result == UnityWebRequest.Result.Success) + { + string result = request.downloadHandler.text; + callback(Newtonsoft.Json.JsonConvert.DeserializeObject(result)); + } + else + callback(null); + } + + public static Dictionary SIGNER_MAP = new Dictionary() + { + { "mainnet", "https://signer.web3auth.io" }, + { "testnet", "https://signer.web3auth.io" }, + { "cyan", "https://signer-polygon.web3auth.io" }, + { "aqua", "https://signer-polygon.web3auth.io" }, + { "sapphire_mainnet", "https://signer.web3auth.io" }, + { "sapphire_devnet", "https://signer.web3auth.io" } + }; } diff --git a/Assets/Plugins/Web3AuthSDK/Api/WhiteLabelDataExtensions.cs b/Assets/Plugins/Web3AuthSDK/Api/WhiteLabelDataExtensions.cs new file mode 100644 index 0000000..3c69506 --- /dev/null +++ b/Assets/Plugins/Web3AuthSDK/Api/WhiteLabelDataExtensions.cs @@ -0,0 +1,46 @@ +using System.Collections.Generic; + +public static class WhiteLabelDataExtensions +{ + public static WhiteLabelData merge(this WhiteLabelData target, WhiteLabelData other) + { + if (target == null) + return other; + if (other == null) + return target; + + return new WhiteLabelData + { + appName = target.appName ?? other.appName, + appUrl = target.appUrl ?? other.appUrl, + logoLight = target.logoLight ?? other.logoLight, + logoDark = target.logoDark ?? other.logoDark, + defaultLanguage = target.defaultLanguage ?? other.defaultLanguage, + mode = target.mode ?? other.mode, + useLogoLoader = target.useLogoLoader ?? other.useLogoLoader, + theme = mergeThemes(target.theme, other.theme) + }; + } + + private static Dictionary mergeThemes(Dictionary targetTheme, Dictionary otherTheme) + { + if (otherTheme == null || otherTheme.Count == 0) + return targetTheme; + if (targetTheme == null || targetTheme.Count == 0) + return otherTheme; + + var mergedTheme = new Dictionary(targetTheme); + foreach (var kvp in targetTheme) + { + mergedTheme[kvp.Key] = kvp.Value; + } + foreach (var kvp in otherTheme) + { + if (!mergedTheme.ContainsKey(kvp.Key)) + { + mergedTheme.Add(kvp.Key, kvp.Value); + } + } + return mergedTheme; + } +} \ No newline at end of file diff --git a/Assets/Plugins/Web3AuthSDK/Api/WhiteLabelDataExtensions.cs.meta b/Assets/Plugins/Web3AuthSDK/Api/WhiteLabelDataExtensions.cs.meta new file mode 100644 index 0000000..568dd4e --- /dev/null +++ b/Assets/Plugins/Web3AuthSDK/Api/WhiteLabelDataExtensions.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0e20464404da941b8aabc89fc8e0e03c +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Plugins/Web3AuthSDK/Samples/Web3AuthSample.cs b/Assets/Plugins/Web3AuthSDK/Samples/Web3AuthSample.cs index c3ec31a..ea22279 100644 --- a/Assets/Plugins/Web3AuthSDK/Samples/Web3AuthSample.cs +++ b/Assets/Plugins/Web3AuthSDK/Samples/Web3AuthSample.cs @@ -97,7 +97,7 @@ void Start() clientId = "BPi5PB_UiIZ-cPz1GtV5i1I2iOSOHuimiXBI0e-Oe_u6X3oVAbCiAZOTEBtTXw4tsluTITPqA8zMsfxIKMjiqNQ", buildEnv = BuildEnv.TESTING, redirectUrl = new Uri("torusapp://com.torus.Web3AuthUnity/auth"), - network = Web3Auth.Network.SAPPHIRE_MAINNET, + network = Web3Auth.Network.SAPPHIRE_DEVNET, sessionTime = 86400 }); web3Auth.onLogin += onLogin; diff --git a/Assets/Plugins/Web3AuthSDK/Types/DictionaryExtensions.cs b/Assets/Plugins/Web3AuthSDK/Types/DictionaryExtensions.cs new file mode 100644 index 0000000..1f594dd --- /dev/null +++ b/Assets/Plugins/Web3AuthSDK/Types/DictionaryExtensions.cs @@ -0,0 +1,28 @@ +using System.Collections.Generic; + +public static class DictionaryExtensions +{ + public static Dictionary mergeMaps(this Dictionary source, Dictionary other) + { + if (source == null && other == null) + { + return null; + } + else if (source == null) + { + return new Dictionary(other); + } + else if (other == null) + { + return new Dictionary(source); + } + + var mergedMap = new Dictionary(source); + foreach (var entry in other) + { + mergedMap[entry.Key] = entry.Value; + } + + return mergedMap; + } +} diff --git a/Assets/Plugins/Web3AuthSDK/Types/DictionaryExtensions.cs.meta b/Assets/Plugins/Web3AuthSDK/Types/DictionaryExtensions.cs.meta new file mode 100644 index 0000000..5919e77 --- /dev/null +++ b/Assets/Plugins/Web3AuthSDK/Types/DictionaryExtensions.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 87aab444af88148c4ab86b07a9c5eff3 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Plugins/Web3AuthSDK/Types/Web3AuthOptions.cs b/Assets/Plugins/Web3AuthSDK/Types/Web3AuthOptions.cs index 2fb8d67..1b32f20 100644 --- a/Assets/Plugins/Web3AuthSDK/Types/Web3AuthOptions.cs +++ b/Assets/Plugins/Web3AuthSDK/Types/Web3AuthOptions.cs @@ -38,4 +38,5 @@ public string walletSdkUrl { public MfaSettings? mfaSettings { get; set; } = null; public int sessionTime { get; set; } = 86400; public ChainConfig? chainConfig { get; set; } + public Dictionary originData { get; set; } = null; } \ No newline at end of file diff --git a/Assets/Plugins/Web3AuthSDK/Web3Auth.cs b/Assets/Plugins/Web3AuthSDK/Web3Auth.cs index c37f7e9..ca99b81 100644 --- a/Assets/Plugins/Web3AuthSDK/Web3Auth.cs +++ b/Assets/Plugins/Web3AuthSDK/Web3Auth.cs @@ -100,47 +100,56 @@ public void Awake() // this.setResultUrl(new Uri($"http://localhost#{code}")); // } #endif - authorizeSession(""); } - public void setOptions(Web3AuthOptions web3AuthOptions) + public async void setOptions(Web3AuthOptions web3AuthOptions) { - JsonSerializerSettings settings = new JsonSerializerSettings + this.web3AuthOptions = web3AuthOptions; + + bool isfetchConfigSuccess = await fetchProjectConfig(); + + if (!isfetchConfigSuccess) { - Converters = new List { new StringEnumConverter() }, - Formatting = Formatting.Indented - }; + throw new Exception("Failed to fetch project config. Please try again later."); + } else { + authorizeSession(""); - this.web3AuthOptions = web3AuthOptions; + JsonSerializerSettings settings = new JsonSerializerSettings + { + Converters = new List { new StringEnumConverter() }, + Formatting = Formatting.Indented + }; + + if (this.web3AuthOptions.redirectUrl != null) + this.initParams["redirectUrl"] = this.web3AuthOptions.redirectUrl; - if (this.web3AuthOptions.redirectUrl != null) - this.initParams["redirectUrl"] = this.web3AuthOptions.redirectUrl; + if (this.web3AuthOptions.whiteLabel != null) + this.initParams["whiteLabel"] = JsonConvert.SerializeObject(this.web3AuthOptions.whiteLabel, settings); - if (this.web3AuthOptions.whiteLabel != null) - this.initParams["whiteLabel"] = JsonConvert.SerializeObject(this.web3AuthOptions.whiteLabel, settings); + if (this.web3AuthOptions.loginConfig != null) + this.initParams["loginConfig"] = JsonConvert.SerializeObject(this.web3AuthOptions.loginConfig, settings); - if (this.web3AuthOptions.loginConfig != null) - this.initParams["loginConfig"] = JsonConvert.SerializeObject(this.web3AuthOptions.loginConfig, settings); + if (this.web3AuthOptions.clientId != null) + this.initParams["clientId"] = this.web3AuthOptions.clientId; - if (this.web3AuthOptions.clientId != null) - this.initParams["clientId"] = this.web3AuthOptions.clientId; + if (this.web3AuthOptions.buildEnv != null) + this.initParams["buildEnv"] = this.web3AuthOptions.buildEnv.ToString().ToLower(); - if (this.web3AuthOptions.buildEnv != null) - this.initParams["buildEnv"] = this.web3AuthOptions.buildEnv.ToString().ToLowerInvariant(); + this.initParams["network"] = this.web3AuthOptions.network.ToString().ToLower(); - this.initParams["network"] = this.web3AuthOptions.network.ToString().ToLowerInvariant(); + if (this.web3AuthOptions.useCoreKitKey.HasValue) + this.initParams["useCoreKitKey"] = this.web3AuthOptions.useCoreKitKey.Value; - if (this.web3AuthOptions.useCoreKitKey.HasValue) - this.initParams["useCoreKitKey"] = this.web3AuthOptions.useCoreKitKey.Value; + if (this.web3AuthOptions.chainNamespace != null) + this.initParams["chainNamespace"] = this.web3AuthOptions.chainNamespace; - if (this.web3AuthOptions.chainNamespace != null) - this.initParams["chainNamespace"] = this.web3AuthOptions.chainNamespace; + if (this.web3AuthOptions.mfaSettings != null) + this.initParams["mfaSettings"] = JsonConvert.SerializeObject(this.web3AuthOptions.mfaSettings, settings); - if (this.web3AuthOptions.mfaSettings != null) - this.initParams["mfaSettings"] = JsonConvert.SerializeObject(this.web3AuthOptions.mfaSettings, settings); + if (this.web3AuthOptions.sessionTime != null) + this.initParams["sessionTime"] = this.web3AuthOptions.sessionTime; - if (this.web3AuthOptions.sessionTime != null) - this.initParams["sessionTime"] = this.web3AuthOptions.sessionTime; + } } private void onDeepLinkActivated(string url) @@ -777,6 +786,49 @@ private async Task createSession(string data, long sessionTime) return await createSessionResponse.Task; } + private async Task fetchProjectConfig() + { + TaskCompletionSource fetchProjectConfigResponse = new TaskCompletionSource(); + StartCoroutine(Web3AuthApi.getInstance().fetchProjectConfig(this.web3AuthOptions.clientId, this.web3AuthOptions.network.ToString().ToLower(), (response => + { + if (response != null) + { + this.web3AuthOptions.originData = this.web3AuthOptions.originData.mergeMaps(response.whitelist?.signed_urls); + if (response?.whitelabel != null) + { + if (this.web3AuthOptions.whiteLabel == null) + { + this.web3AuthOptions.whiteLabel = response.whitelabel; + } + else + { + this.web3AuthOptions.whiteLabel = this.web3AuthOptions.whiteLabel?.merge(response.whitelabel); + } + } + //Debug.Log("this.web3AuthOptions: =>" + JsonConvert.SerializeObject(this.web3AuthOptions)); + + JsonSerializerSettings settings = new JsonSerializerSettings + { + Converters = new List { new StringEnumConverter() }, + Formatting = Formatting.Indented + }; + if (this.web3AuthOptions.whiteLabel != null) + this.initParams["whiteLabel"] = JsonConvert.SerializeObject(this.web3AuthOptions.whiteLabel, settings); + + if(this.web3AuthOptions.originData != null) + this.initParams["originData"] = JsonConvert.SerializeObject(this.web3AuthOptions.originData, settings); + + fetchProjectConfigResponse.SetResult(true); + } + else + { + Debug.Log("configResponse API error:"); + fetchProjectConfigResponse.SetResult(false); + } + }))); + return await fetchProjectConfigResponse.Task; + } + public string getPrivKey() { if (web3AuthResponse == null)