Skip to content

Commit

Permalink
Merge pull request #35 from Web3Auth/feat/config_api
Browse files Browse the repository at this point in the history
Feat/config api
  • Loading branch information
chaitanyapotti authored Aug 26, 2024
2 parents 66617f2 + 1fa30ef commit bc77c45
Show file tree
Hide file tree
Showing 10 changed files with 233 additions and 28 deletions.
16 changes: 16 additions & 0 deletions Assets/Plugins/Web3AuthSDK/Api/Models/ProjectConfigResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Collections.Generic;

public class WhitelistResponse
{
public List<string> urls { get; set; }
public Dictionary<string, string> 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; }
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 30 additions & 1 deletion Assets/Plugins/Web3AuthSDK/Api/Web3AuthApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Newtonsoft.Json;
using UnityEngine.Networking;
using UnityEngine;
using System.Collections.Generic;

public class Web3AuthApi
{
Expand All @@ -29,7 +30,7 @@ public IEnumerator authorizeSession(string key, Action<StoreApiResponse> 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);
Expand Down Expand Up @@ -93,4 +94,32 @@ public IEnumerator createSession(LogoutApiRequest logoutApiRequest, Action<JObje
else
callback(null);
}

public IEnumerator fetchProjectConfig(string project_id, string network, Action<ProjectConfigResponse> 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<ProjectConfigResponse>(result));
}
else
callback(null);
}

public static Dictionary<string, string> SIGNER_MAP = new Dictionary<string, string>()
{
{ "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" }
};
}
46 changes: 46 additions & 0 deletions Assets/Plugins/Web3AuthSDK/Api/WhiteLabelDataExtensions.cs
Original file line number Diff line number Diff line change
@@ -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<string, string> mergeThemes(Dictionary<string, string> targetTheme, Dictionary<string, string> otherTheme)
{
if (otherTheme == null || otherTheme.Count == 0)
return targetTheme;
if (targetTheme == null || targetTheme.Count == 0)
return otherTheme;

var mergedTheme = new Dictionary<string, string>(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;
}
}
11 changes: 11 additions & 0 deletions Assets/Plugins/Web3AuthSDK/Api/WhiteLabelDataExtensions.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Assets/Plugins/Web3AuthSDK/Samples/Web3AuthSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
28 changes: 28 additions & 0 deletions Assets/Plugins/Web3AuthSDK/Types/DictionaryExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Collections.Generic;

public static class DictionaryExtensions
{
public static Dictionary<string, string> mergeMaps(this Dictionary<string, string> source, Dictionary<string, string> other)
{
if (source == null && other == null)
{
return null;
}
else if (source == null)
{
return new Dictionary<string, string>(other);
}
else if (other == null)
{
return new Dictionary<string, string>(source);
}

var mergedMap = new Dictionary<string, string>(source);
foreach (var entry in other)
{
mergedMap[entry.Key] = entry.Value;
}

return mergedMap;
}
}
11 changes: 11 additions & 0 deletions Assets/Plugins/Web3AuthSDK/Types/DictionaryExtensions.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Assets/Plugins/Web3AuthSDK/Types/Web3AuthOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string> originData { get; set; } = null;
}
104 changes: 78 additions & 26 deletions Assets/Plugins/Web3AuthSDK/Web3Auth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<JsonConverter> { 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<JsonConverter> { 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)
Expand Down Expand Up @@ -777,6 +786,49 @@ private async Task<string> createSession(string data, long sessionTime)
return await createSessionResponse.Task;
}

private async Task<bool> fetchProjectConfig()
{
TaskCompletionSource<bool> fetchProjectConfigResponse = new TaskCompletionSource<bool>();
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<JsonConverter> { 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)
Expand Down

0 comments on commit bc77c45

Please sign in to comment.