Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
using System.Collections.Generic;
using UnityEngine;

public class StoreApiResponse
{
public class StoreApiResponse {
public string message { get; set; }
public bool success { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,22 @@ public static Web3AuthApi getInstance()

public IEnumerator authorizeSession(string key, Action<StoreApiResponse> callback)
{
var request = UnityWebRequest.Get($"{baseAddress}/store/get?key={key}");
yield return request.SendWebRequest();
//var requestURL = $"{baseAddress}/store/get?key={key}";
//var request = UnityWebRequest.Get(requestURL);
WWWForm data = new WWWForm();
data.AddField("key", key);

var request = UnityWebRequest.Post($"{baseAddress}/store/get", data);

yield return request.SendWebRequest();
// Debug.Log("baseAddress =>" + baseAddress);
// Debug.Log("key =>" + key);
// //Debug.Log("request URL =>"+ requestURL);
// Debug.Log("request.isNetworkError =>" + request.isNetworkError);
// Debug.Log("request.isHttpError =>" + request.isHttpError);
// Debug.Log("request.isHttpError =>" + request.error);
// Debug.Log("request.result =>" + request.result);
// Debug.Log("request.downloadHandler.text =>" + request.downloadHandler.text);
if (request.result == UnityWebRequest.Result.Success)
{
string result = request.downloadHandler.text;
Expand All @@ -38,7 +51,37 @@ public IEnumerator logout(LogoutApiRequest logoutApiRequest, Action<JObject> cal
data.AddField("data", logoutApiRequest.data);
data.AddField("signature", logoutApiRequest.signature);
data.AddField("timeout", logoutApiRequest.timeout.ToString());
// Debug.Log("key during logout session =>" + logoutApiRequest.key);

var request = UnityWebRequest.Post($"{baseAddress}/store/set", data);
yield return request.SendWebRequest();

// Debug.Log("baseAddress =>" + baseAddress);
// Debug.Log("key =>" + logoutApiRequest.key);
// Debug.Log("request URL =>"+ requestURL);
// Debug.Log("request.isNetworkError =>" + request.isNetworkError);
// Debug.Log("request.isHttpError =>" + request.isHttpError);
// Debug.Log("request.isHttpError =>" + request.error);
// Debug.Log("request.result =>" + request.result);
// Debug.Log("request.downloadHandler.text =>" + request.downloadHandler.text);

if (request.result == UnityWebRequest.Result.Success)
{
string result = request.downloadHandler.text;
callback(Newtonsoft.Json.JsonConvert.DeserializeObject<JObject>(result));
}
else
callback(null);
}

public IEnumerator createSession(LogoutApiRequest logoutApiRequest, Action<JObject> callback)
{
WWWForm data = new WWWForm();
data.AddField("key", logoutApiRequest.key);
data.AddField("data", logoutApiRequest.data);
data.AddField("signature", logoutApiRequest.signature);
data.AddField("timeout", logoutApiRequest.timeout.ToString());
// Debug.Log("key during create session =>" + logoutApiRequest.key);
var request = UnityWebRequest.Post($"{baseAddress}/store/set", data);
yield return request.SendWebRequest();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class Web3AuthDebug : EditorWindow

[SerializeField] public int index;

[MenuItem("Window/ChainSafe SDK/Web3Auth/Deep Linking Debug")]
[MenuItem("Window/Web3Auth/Deep Linking Debug")]
public static void ShowExample()
{
Web3AuthDebug wnd = GetWindow<Web3AuthDebug>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Utilities.Encoders;
using System.Runtime.InteropServices;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.OpenSsl;
using Org.BouncyCastle.Crypto;
using System.Text;

public class KeyStoreManagerUtils
{
Expand Down Expand Up @@ -36,8 +40,7 @@ public static string getPubKey(string sessionId)
var q = new ECPublicKeyParameters("EC", domain.G.Multiply(key.D), parameters).Q;

return Hex.ToHexString(domain.Curve.CreatePoint(q.XCoord.ToBigInteger(), q.YCoord.ToBigInteger()).GetEncoded(false));
}
catch (System.Exception ex)
} catch (System.Exception ex)
{
UnityEngine.Debug.Log(ex);
return "";
Expand Down Expand Up @@ -77,8 +80,37 @@ public static void deletePreferencesData(string key)
#endif
}

public static string getECDSASignature(string privateKey, string data)
public static AsymmetricCipherKeyPair generateECKeyPair()
{
var secureRandom = new SecureRandom();
var curve = SecNamedCurves.GetByName("secp256k1");
var domainParams = new ECDomainParameters(curve.Curve, curve.G, curve.N, curve.H, curve.GetSeed());

var keyGenParam = new ECKeyGenerationParameters(domainParams, secureRandom);
var generator = GeneratorUtilities.GetKeyPairGenerator("ECDSA");
generator.Init(keyGenParam);

return generator.GenerateKeyPair();
}

public static string generateRandomSessionKey() {
var keyPair = generateECKeyPair();
var privateKey = (ECPrivateKeyParameters)keyPair.Private;
var publicKey = (ECPublicKeyParameters)keyPair.Public;

string privateKeyHex = privateKey.D.ToString(16).PadLeft(64, '0');
return privateKeyHex;
}

public static byte[] generateRandomBytes()
{
var secureRandom = new SecureRandom();
byte[] bytes = new byte[16];
secureRandom.NextBytes(bytes);
return bytes;
}

public static string getECDSASignature(string privateKey, string data){
var curve = SecNamedCurves.GetByName("secp256k1");
var domain = new ECDomainParameters(curve.Curve, curve.G, curve.N, curve.H);
var keyParameters = new ECPrivateKeyParameters(new BigInteger(privateKey, 16), domain);
Expand Down Expand Up @@ -110,4 +142,15 @@ public static string getECDSASignature(string privateKey, string data)

return Hex.ToHexString(derSignature);
}

public static string convertByteToHexadecimal(byte[] byteArray)
{
string hex = "";
// Iterating through each byte in the array
foreach (byte b in byteArray)
{
hex += $"{b:X2}";
}
return hex.ToLowerInvariant();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
public class LoginVerifier
{
public class LoginVerifier {
public string name { get; set; }
public Provider loginProvider { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,19 @@ void Start()
{
verifier = "your_verifierid_from_web3auth_dashboard",
typeOfLogin = TypeOfLogin.GOOGLE,
clientId = "your_clientid_from_google_or_etc"
clientId = "your_clientId_from_web3auth_dashboard"
};

web3Auth = GetComponent<Web3Auth>();
web3Auth.setOptions(new Web3AuthOptions()
{
whiteLabel = new WhiteLabelData()
{
name = "Web3Auth Sample App",
appName = "Web3Auth Sample App",
logoLight = null,
logoDark = null,
defaultLanguage = "en",
dark = true,
defaultLanguage = Language.en,
mode = ThemeModes.dark,
theme = new Dictionary<string, string>
{
{ "primary", "#123456" }
Expand All @@ -72,7 +72,10 @@ void Start()
{"CUSTOM_VERIFIER", loginConfigItem}
}
*/
network = Web3Auth.Network.TESTNET
clientId = "BPi5PB_UiIZ-cPz1GtV5i1I2iOSOHuimiXBI0e-Oe_u6X3oVAbCiAZOTEBtTXw4tsluTITPqA8zMsfxIKMjiqNQ",
buildEnv = BuildEnv.PRODUCTION,
redirectUrl = new Uri("torusapp://com.torus.Web3AuthUnity/auth"),
network = Web3Auth.Network.SAPPHIRE_MAINNET
});
web3Auth.onLogin += onLogin;
web3Auth.onLogout += onLogout;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -896,9 +896,9 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 3eab2a0bf902d6e4b9c2e968ad89f528, type: 3}
m_Name:
m_EditorClassIdentifier:
clientId: BJ6l3_kIQiy6YVL7zDlCcEAvGpGukwFgp-C_0WvNI_fAEeIaoVRLDrV5OjtbZr_zJxbyXFsXMT-yhQiUNYvZWpo
clientId: BPi5PB_UiIZ-cPz1GtV5i1I2iOSOHuimiXBI0e-Oe_u6X3oVAbCiAZOTEBtTXw4tsluTITPqA8zMsfxIKMjiqNQ
redirectUri: torusapp://com.torus.Web3AuthUnity/auth
network: 1
network: 5
--- !u!1 &529774071
GameObject:
m_ObjectHideFlags: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Security;
using System.Security.Cryptography;
using System.IO;
using System;
using System.Text;

public class AES256CBC
{
private static string TRANSFORMATION = "AES/CBC/PKCS7PADDING";
private byte[] AES_ENCRYPTION_KEY;
private byte[] ENCRYPTION_IV;

private byte[] MAC_KEY;
private byte[] ENCRYPTION_EPHEM_KEY;

public AES256CBC(string privateKeyHex, string ephemPublicKeyHex, string encryptionIvHex)
{
using (SHA512 shaM = new SHA512Managed())
Expand All @@ -24,35 +30,39 @@ public AES256CBC(string privateKeyHex, string ephemPublicKeyHex, string encrypti
System.Array.Copy(hash, encKeyBytes, 32);

AES_ENCRYPTION_KEY = encKeyBytes;
ENCRYPTION_IV = toByteArray(encryptionIvHex);

MAC_KEY = new byte[hash.Length - 32];
System.Array.Copy(hash, 32, MAC_KEY, 0, MAC_KEY.Length);

ENCRYPTION_IV = toByteArray(encryptionIvHex);
ENCRYPTION_EPHEM_KEY = toByteArray(ephemPublicKeyHex);
}
}

public string encrypt(byte[] src)
public byte[] encrypt(byte[] src)
{
var key = ParameterUtilities.CreateKeyParameter("AES", AES_ENCRYPTION_KEY);
var parametersWithIv = new ParametersWithIV(key, ENCRYPTION_IV);

var cipher = CipherUtilities.GetCipher(TRANSFORMATION);
cipher.Init(true, parametersWithIv);

return System.Text.Encoding.UTF8.GetString(
cipher.DoFinal(src)
);
return cipher.DoFinal(src);
}

public string decrypt(byte[] src)
public byte[] decrypt(byte[] src, string mac)
{
if (!hmacSha256Verify(MAC_KEY, getCombinedData(src), mac))
{
throw new SystemException("Bad MAC error during decrypt");
}
var key = ParameterUtilities.CreateKeyParameter("AES", AES_ENCRYPTION_KEY);
var parametersWithIv = new ParametersWithIV(key, ENCRYPTION_IV);

var cipher = CipherUtilities.GetCipher(TRANSFORMATION);
cipher.Init(false, parametersWithIv);

return System.Text.Encoding.UTF8.GetString(
cipher.DoFinal(src)
);
return cipher.DoFinal(src);
}


Expand Down Expand Up @@ -97,4 +107,35 @@ public static byte[] toByteArray(BigInteger bi)
}
return b;
}

public byte[] getCombinedData(byte[] cipherTextBytes)
{
using (MemoryStream outputStream = new MemoryStream())
{
outputStream.Write(ENCRYPTION_IV, 0, ENCRYPTION_IV.Length);
outputStream.Write(ENCRYPTION_EPHEM_KEY, 0, ENCRYPTION_EPHEM_KEY.Length);
outputStream.Write(cipherTextBytes, 0, cipherTextBytes.Length);
return outputStream.ToArray();
}
}

public byte[] getMac(byte[] cipherTextBytes)
{
return hmacSha256Sign(MAC_KEY, getCombinedData(cipherTextBytes));
}

public byte[] hmacSha256Sign(byte[] key, byte[] data)
{
using (HMACSHA256 hmac = new HMACSHA256(key))
{
return hmac.ComputeHash(data);
}
}

public bool hmacSha256Verify(byte[] key, byte[] data, string sig)
{
byte[] expectedSig = hmacSha256Sign(key, data);
string expectedSigHex = BitConverter.ToString(expectedSig).Replace("-", "").ToLower();
return expectedSigHex.Equals(sig);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Collections.Generic;

public class ExtraLoginOptions
{
public class ExtraLoginOptions {
public Dictionary<string, string> additionalParams { get; set; }
public string domain { get; set; }
public string client_id { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
public class LoginConfigItem
{
public class LoginConfigItem {
public string verifier { get; set; }
public TypeOfLogin typeOfLogin { get; set; }
public string name { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Collections.Generic;
#nullable enable
public class MfaSetting
{
public bool enable { get; set; }
public int? priority { get; set; }
public bool? mandatory { get; set; }

// Constructor
public MfaSetting(bool enable, int? priority, bool? mandatory)
{
this.enable = enable;
this.priority = priority;
this.mandatory = mandatory;
}
}

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
@@ -0,0 +1,20 @@
public class MfaSettings
{
public MfaSetting? deviceShareFactor { get; set; }
public MfaSetting? backUpShareFactor { get; set; }
public MfaSetting? socialBackupFactor { get; set; }
public MfaSetting? passwordFactor { get; set; }

// Constructors
public MfaSettings(
MfaSetting? deviceShareFactor,
MfaSetting? backUpShareFactor,
MfaSetting? socialBackupFactor,
MfaSetting? passwordFactor)
{
this.deviceShareFactor = deviceShareFactor;
this.backUpShareFactor = backUpShareFactor;
this.socialBackupFactor = socialBackupFactor;
this.passwordFactor = passwordFactor;
}
}

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

Loading