Skip to content

Commit

Permalink
Add GetTorusKey
Browse files Browse the repository at this point in the history
  • Loading branch information
phuctm97 committed Jul 15, 2021
1 parent 44de4a1 commit 22e0386
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 0 deletions.
Binary file modified Assets/Plugins/Torus/Android/TorusDirect.aar
Binary file not shown.
38 changes: 38 additions & 0 deletions Assets/Plugins/Torus/Classes/TorusDirect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,44 @@ public static void TriggerLogin(TorusCallback callback, TorusTypeOfLogin typeOfL
}
}

[Serializable]
public class GetTorusKeyParams
{
public string verifier;
public string verifierId;
public TorusVerifierParams verifierParams;
public string idToken;
}

public static void GetTorusKey(TorusCallback callback, string verifier, string verifierId, string idToken, TorusVerifierParams verifierParams = null)
{
if (Application.platform == RuntimePlatform.Android)
{
using (AndroidJavaClass cls = new AndroidJavaClass("org.torusresearch.unity.torusdirect.Plugin"))
{
using (AndroidJavaObject plugin = cls.CallStatic<AndroidJavaObject>("getInstance"))
{
TorusVerifierParams mergedVerifierParams = verifierParams != null ? verifierParams : new TorusVerifierParams();
if (string.IsNullOrEmpty(mergedVerifierParams.verifier_id)) mergedVerifierParams.verifier_id = verifierId;

plugin.Call("getTorusKey", callback.gameObject.name, callback.method,
JsonUtility.ToJson(new GetTorusKeyParams
{
verifier = verifier,
verifierId = verifierId,
verifierParams = mergedVerifierParams,
idToken = idToken
})
);
}
}
}
else
{
Debug.LogWarning("TorusDirect: Unsupported platform");
}
}

public static TorusCredentials ResumeAuth(string message)
{
TorusResponse response = JsonUtility.FromJson<TorusResponse>(message);
Expand Down
12 changes: 12 additions & 0 deletions Assets/Plugins/Torus/Scripts/TorusDirectMonoBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,18 @@ public void TriggerLogin(TorusTypeOfLogin typeOfLogin, string verifier, string c
);
}

public void GetTorusKey(TorusCallback callback, string verifier, string verifierId, string idToken, TorusVerifierParams verifierParams = null)
{
__OnPreLogin__();
TorusDirect.GetTorusKey(
callback: TorusDirect.Callback(gameObject, "__onPostLogin__"),
verifier: verifier,
verifierId: verifierId,
verifierParams: verifierParams,
idToken: idToken
);
}

public void LoginWithGoogle()
{
TriggerLogin(TorusTypeOfLogin.Google, loginWithGoogle.verifier, loginWithGoogle.clientID);
Expand Down
10 changes: 10 additions & 0 deletions Assets/Plugins/Torus/Scripts/TorusVerifierParams.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;

namespace Torus
{
[Serializable]
public class TorusVerifierParams
{
public string verifier_id;
}
}
11 changes: 11 additions & 0 deletions Assets/Plugins/Torus/Scripts/TorusVerifierParams.cs.meta

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

Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,75 @@ class TorusDirectPlugin internal constructor() {
}
}

fun getTorusKey(
callbackGameObject: String,
callbackMethod: String,
paramsString: String
) {
if (args == null) throw Exception("TorusDirect.init must be called before getTorusKey.")

val activity = UnityPlayer.currentActivity
Log.d(
"${tag}#getTorusKey", arrayOf(
"callbackGameObject=$callbackGameObject",
"callbackMethod=$callbackMethod",
"params=$paramsString",
"activity=${activity::class.qualifiedName}"
).joinToString(" ")
)

val paramsJSON = JSONObject(paramsString)
CoroutineScope(Dispatchers.Default).launch {
try {
val sdk = TorusDirectSdk(args, activity)
Log.d("${tag}#getTorusKey", "Initialized TorusDirect SDK successfully")

val verifierParamsMap = hashMapOf<String, Any>()
val verifierParamsJSON = paramsJSON.getJSONObject("verifierParams")
for (key in verifierParamsJSON.keys())
verifierParamsMap[key] = verifierParamsJSON.get(key)

val result = sdk.getTorusKey(
paramsJSON.getString("verifier"),
paramsJSON.getString("verifierId"),
verifierParamsMap,
paramsJSON.getString("idToken")
).join()
Log.d("${tag}#getTorusKey", "Got Torus key successfully")
launch(Dispatchers.Main) {
val json = JSONObject()
json.put("status", "fulfilled")
val value = JSONObject()
value.put("privateKey", result.privateKey)
value.put("publicAddress", result.publicAddress)
json.put("value", value)
UnityPlayer.UnitySendMessage(
callbackGameObject,
callbackMethod,
json.toString()
)
}
} catch (completionException: Throwable) {
val e = Helpers.unwrapCompletionException(completionException)
Log.e("${tag}#triggerLogin", "Login failed - ${e}")
Log.e("${tag}#triggerLogin", "Login stacktrace - ${e.stackTraceToString()}")
launch(Dispatchers.Main) {
val json = JSONObject()
json.put("status", "rejected")
val reason = JSONObject()
reason.put("name", e::class.simpleName)
reason.put("message", e.message)
json.put("reason", reason)
UnityPlayer.UnitySendMessage(
callbackGameObject,
callbackMethod,
json.toString()
)
}
}
}
}

private fun mapJwtParams(jwtParams: JSONObject?): Auth0ClientOptions? {
if (jwtParams == null || !jwtParams.has("domain")) {
return Auth0ClientOptions.Auth0ClientOptionsBuilder("").build()
Expand Down

0 comments on commit 22e0386

Please sign in to comment.