Skip to content

Commit bc77c45

Browse files
authoredAug 26, 2024
Merge pull request #35 from Web3Auth/feat/config_api
Feat/config api
2 parents 66617f2 + 1fa30ef commit bc77c45

10 files changed

+233
-28
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System.Collections.Generic;
2+
3+
public class WhitelistResponse
4+
{
5+
public List<string> urls { get; set; }
6+
public Dictionary<string, string> signed_urls { get; set; }
7+
}
8+
9+
public class ProjectConfigResponse
10+
{
11+
public WhiteLabelData whitelabel { get; set; }
12+
public bool sms_otp_enabled { get; set; }
13+
public bool wallet_connect_enabled { get; set; }
14+
public string wallet_connect_project_id { get; set; }
15+
public WhitelistResponse whitelist { get; set; }
16+
}

‎Assets/Plugins/Web3AuthSDK/Api/Models/ProjectConfigResponse.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Assets/Plugins/Web3AuthSDK/Api/Web3AuthApi.cs

+30-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Newtonsoft.Json;
55
using UnityEngine.Networking;
66
using UnityEngine;
7+
using System.Collections.Generic;
78

89
public class Web3AuthApi
910
{
@@ -29,7 +30,7 @@ public IEnumerator authorizeSession(string key, Action<StoreApiResponse> callbac
2930
yield return request.SendWebRequest();
3031
// Debug.Log("baseAddress =>" + baseAddress);
3132
// Debug.Log("key =>" + key);
32-
// //Debug.Log("request URL =>"+ requestURL);
33+
// Debug.Log("request URL =>"+ requestURL);
3334
// Debug.Log("request.isNetworkError =>" + request.isNetworkError);
3435
// Debug.Log("request.isHttpError =>" + request.isHttpError);
3536
// Debug.Log("request.isHttpError =>" + request.error);
@@ -93,4 +94,32 @@ public IEnumerator createSession(LogoutApiRequest logoutApiRequest, Action<JObje
9394
else
9495
callback(null);
9596
}
97+
98+
public IEnumerator fetchProjectConfig(string project_id, string network, Action<ProjectConfigResponse> callback)
99+
{
100+
//Debug.Log("network =>" + network);
101+
string baseUrl = SIGNER_MAP[network];
102+
var requestURL = $"{baseUrl}/api/configuration?project_id={project_id}&network={network}&whitelist=true";
103+
var request = UnityWebRequest.Get(requestURL);
104+
105+
yield return request.SendWebRequest();
106+
107+
if (request.result == UnityWebRequest.Result.Success)
108+
{
109+
string result = request.downloadHandler.text;
110+
callback(Newtonsoft.Json.JsonConvert.DeserializeObject<ProjectConfigResponse>(result));
111+
}
112+
else
113+
callback(null);
114+
}
115+
116+
public static Dictionary<string, string> SIGNER_MAP = new Dictionary<string, string>()
117+
{
118+
{ "mainnet", "https://signer.web3auth.io" },
119+
{ "testnet", "https://signer.web3auth.io" },
120+
{ "cyan", "https://signer-polygon.web3auth.io" },
121+
{ "aqua", "https://signer-polygon.web3auth.io" },
122+
{ "sapphire_mainnet", "https://signer.web3auth.io" },
123+
{ "sapphire_devnet", "https://signer.web3auth.io" }
124+
};
96125
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System.Collections.Generic;
2+
3+
public static class WhiteLabelDataExtensions
4+
{
5+
public static WhiteLabelData merge(this WhiteLabelData target, WhiteLabelData other)
6+
{
7+
if (target == null)
8+
return other;
9+
if (other == null)
10+
return target;
11+
12+
return new WhiteLabelData
13+
{
14+
appName = target.appName ?? other.appName,
15+
appUrl = target.appUrl ?? other.appUrl,
16+
logoLight = target.logoLight ?? other.logoLight,
17+
logoDark = target.logoDark ?? other.logoDark,
18+
defaultLanguage = target.defaultLanguage ?? other.defaultLanguage,
19+
mode = target.mode ?? other.mode,
20+
useLogoLoader = target.useLogoLoader ?? other.useLogoLoader,
21+
theme = mergeThemes(target.theme, other.theme)
22+
};
23+
}
24+
25+
private static Dictionary<string, string> mergeThemes(Dictionary<string, string> targetTheme, Dictionary<string, string> otherTheme)
26+
{
27+
if (otherTheme == null || otherTheme.Count == 0)
28+
return targetTheme;
29+
if (targetTheme == null || targetTheme.Count == 0)
30+
return otherTheme;
31+
32+
var mergedTheme = new Dictionary<string, string>(targetTheme);
33+
foreach (var kvp in targetTheme)
34+
{
35+
mergedTheme[kvp.Key] = kvp.Value;
36+
}
37+
foreach (var kvp in otherTheme)
38+
{
39+
if (!mergedTheme.ContainsKey(kvp.Key))
40+
{
41+
mergedTheme.Add(kvp.Key, kvp.Value);
42+
}
43+
}
44+
return mergedTheme;
45+
}
46+
}

‎Assets/Plugins/Web3AuthSDK/Api/WhiteLabelDataExtensions.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Assets/Plugins/Web3AuthSDK/Samples/Web3AuthSample.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ void Start()
9797
clientId = "BPi5PB_UiIZ-cPz1GtV5i1I2iOSOHuimiXBI0e-Oe_u6X3oVAbCiAZOTEBtTXw4tsluTITPqA8zMsfxIKMjiqNQ",
9898
buildEnv = BuildEnv.TESTING,
9999
redirectUrl = new Uri("torusapp://com.torus.Web3AuthUnity/auth"),
100-
network = Web3Auth.Network.SAPPHIRE_MAINNET,
100+
network = Web3Auth.Network.SAPPHIRE_DEVNET,
101101
sessionTime = 86400
102102
});
103103
web3Auth.onLogin += onLogin;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System.Collections.Generic;
2+
3+
public static class DictionaryExtensions
4+
{
5+
public static Dictionary<string, string> mergeMaps(this Dictionary<string, string> source, Dictionary<string, string> other)
6+
{
7+
if (source == null && other == null)
8+
{
9+
return null;
10+
}
11+
else if (source == null)
12+
{
13+
return new Dictionary<string, string>(other);
14+
}
15+
else if (other == null)
16+
{
17+
return new Dictionary<string, string>(source);
18+
}
19+
20+
var mergedMap = new Dictionary<string, string>(source);
21+
foreach (var entry in other)
22+
{
23+
mergedMap[entry.Key] = entry.Value;
24+
}
25+
26+
return mergedMap;
27+
}
28+
}

‎Assets/Plugins/Web3AuthSDK/Types/DictionaryExtensions.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Assets/Plugins/Web3AuthSDK/Types/Web3AuthOptions.cs

+1
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,5 @@ public string walletSdkUrl {
3838
public MfaSettings? mfaSettings { get; set; } = null;
3939
public int sessionTime { get; set; } = 86400;
4040
public ChainConfig? chainConfig { get; set; }
41+
public Dictionary<string, string> originData { get; set; } = null;
4142
}

‎Assets/Plugins/Web3AuthSDK/Web3Auth.cs

+78-26
Original file line numberDiff line numberDiff line change
@@ -100,47 +100,56 @@ public void Awake()
100100
// this.setResultUrl(new Uri($"http://localhost#{code}"));
101101
// }
102102
#endif
103-
authorizeSession("");
104103
}
105104

106-
public void setOptions(Web3AuthOptions web3AuthOptions)
105+
public async void setOptions(Web3AuthOptions web3AuthOptions)
107106
{
108-
JsonSerializerSettings settings = new JsonSerializerSettings
107+
this.web3AuthOptions = web3AuthOptions;
108+
109+
bool isfetchConfigSuccess = await fetchProjectConfig();
110+
111+
if (!isfetchConfigSuccess)
109112
{
110-
Converters = new List<JsonConverter> { new StringEnumConverter() },
111-
Formatting = Formatting.Indented
112-
};
113+
throw new Exception("Failed to fetch project config. Please try again later.");
114+
} else {
115+
authorizeSession("");
113116

114-
this.web3AuthOptions = web3AuthOptions;
117+
JsonSerializerSettings settings = new JsonSerializerSettings
118+
{
119+
Converters = new List<JsonConverter> { new StringEnumConverter() },
120+
Formatting = Formatting.Indented
121+
};
122+
123+
if (this.web3AuthOptions.redirectUrl != null)
124+
this.initParams["redirectUrl"] = this.web3AuthOptions.redirectUrl;
115125

116-
if (this.web3AuthOptions.redirectUrl != null)
117-
this.initParams["redirectUrl"] = this.web3AuthOptions.redirectUrl;
126+
if (this.web3AuthOptions.whiteLabel != null)
127+
this.initParams["whiteLabel"] = JsonConvert.SerializeObject(this.web3AuthOptions.whiteLabel, settings);
118128

119-
if (this.web3AuthOptions.whiteLabel != null)
120-
this.initParams["whiteLabel"] = JsonConvert.SerializeObject(this.web3AuthOptions.whiteLabel, settings);
129+
if (this.web3AuthOptions.loginConfig != null)
130+
this.initParams["loginConfig"] = JsonConvert.SerializeObject(this.web3AuthOptions.loginConfig, settings);
121131

122-
if (this.web3AuthOptions.loginConfig != null)
123-
this.initParams["loginConfig"] = JsonConvert.SerializeObject(this.web3AuthOptions.loginConfig, settings);
132+
if (this.web3AuthOptions.clientId != null)
133+
this.initParams["clientId"] = this.web3AuthOptions.clientId;
124134

125-
if (this.web3AuthOptions.clientId != null)
126-
this.initParams["clientId"] = this.web3AuthOptions.clientId;
135+
if (this.web3AuthOptions.buildEnv != null)
136+
this.initParams["buildEnv"] = this.web3AuthOptions.buildEnv.ToString().ToLower();
127137

128-
if (this.web3AuthOptions.buildEnv != null)
129-
this.initParams["buildEnv"] = this.web3AuthOptions.buildEnv.ToString().ToLowerInvariant();
138+
this.initParams["network"] = this.web3AuthOptions.network.ToString().ToLower();
130139

131-
this.initParams["network"] = this.web3AuthOptions.network.ToString().ToLowerInvariant();
140+
if (this.web3AuthOptions.useCoreKitKey.HasValue)
141+
this.initParams["useCoreKitKey"] = this.web3AuthOptions.useCoreKitKey.Value;
132142

133-
if (this.web3AuthOptions.useCoreKitKey.HasValue)
134-
this.initParams["useCoreKitKey"] = this.web3AuthOptions.useCoreKitKey.Value;
143+
if (this.web3AuthOptions.chainNamespace != null)
144+
this.initParams["chainNamespace"] = this.web3AuthOptions.chainNamespace;
135145

136-
if (this.web3AuthOptions.chainNamespace != null)
137-
this.initParams["chainNamespace"] = this.web3AuthOptions.chainNamespace;
146+
if (this.web3AuthOptions.mfaSettings != null)
147+
this.initParams["mfaSettings"] = JsonConvert.SerializeObject(this.web3AuthOptions.mfaSettings, settings);
138148

139-
if (this.web3AuthOptions.mfaSettings != null)
140-
this.initParams["mfaSettings"] = JsonConvert.SerializeObject(this.web3AuthOptions.mfaSettings, settings);
149+
if (this.web3AuthOptions.sessionTime != null)
150+
this.initParams["sessionTime"] = this.web3AuthOptions.sessionTime;
141151

142-
if (this.web3AuthOptions.sessionTime != null)
143-
this.initParams["sessionTime"] = this.web3AuthOptions.sessionTime;
152+
}
144153
}
145154

146155
private void onDeepLinkActivated(string url)
@@ -777,6 +786,49 @@ private async Task<string> createSession(string data, long sessionTime)
777786
return await createSessionResponse.Task;
778787
}
779788

789+
private async Task<bool> fetchProjectConfig()
790+
{
791+
TaskCompletionSource<bool> fetchProjectConfigResponse = new TaskCompletionSource<bool>();
792+
StartCoroutine(Web3AuthApi.getInstance().fetchProjectConfig(this.web3AuthOptions.clientId, this.web3AuthOptions.network.ToString().ToLower(), (response =>
793+
{
794+
if (response != null)
795+
{
796+
this.web3AuthOptions.originData = this.web3AuthOptions.originData.mergeMaps(response.whitelist?.signed_urls);
797+
if (response?.whitelabel != null)
798+
{
799+
if (this.web3AuthOptions.whiteLabel == null)
800+
{
801+
this.web3AuthOptions.whiteLabel = response.whitelabel;
802+
}
803+
else
804+
{
805+
this.web3AuthOptions.whiteLabel = this.web3AuthOptions.whiteLabel?.merge(response.whitelabel);
806+
}
807+
}
808+
//Debug.Log("this.web3AuthOptions: =>" + JsonConvert.SerializeObject(this.web3AuthOptions));
809+
810+
JsonSerializerSettings settings = new JsonSerializerSettings
811+
{
812+
Converters = new List<JsonConverter> { new StringEnumConverter() },
813+
Formatting = Formatting.Indented
814+
};
815+
if (this.web3AuthOptions.whiteLabel != null)
816+
this.initParams["whiteLabel"] = JsonConvert.SerializeObject(this.web3AuthOptions.whiteLabel, settings);
817+
818+
if(this.web3AuthOptions.originData != null)
819+
this.initParams["originData"] = JsonConvert.SerializeObject(this.web3AuthOptions.originData, settings);
820+
821+
fetchProjectConfigResponse.SetResult(true);
822+
}
823+
else
824+
{
825+
Debug.Log("configResponse API error:");
826+
fetchProjectConfigResponse.SetResult(false);
827+
}
828+
})));
829+
return await fetchProjectConfigResponse.Task;
830+
}
831+
780832
public string getPrivKey()
781833
{
782834
if (web3AuthResponse == null)

0 commit comments

Comments
 (0)
Please sign in to comment.