Skip to content

Commit

Permalink
Merge pull request #24 from Web3Auth/feat/sapphire-updates
Browse files Browse the repository at this point in the history
Feat/sapphire updates
  • Loading branch information
chaitanyapotti authored Oct 12, 2023
2 parents 5e3342b + ae2c203 commit 334a861
Show file tree
Hide file tree
Showing 11 changed files with 348 additions and 74 deletions.
20 changes: 20 additions & 0 deletions Assets/Plugins/Web3AuthSDK/Api/Web3AuthApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,24 @@ public IEnumerator logout(LogoutApiRequest logoutApiRequest, Action<JObject> cal
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());

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

if (request.result == UnityWebRequest.Result.Success)
{
string result = request.downloadHandler.text;
callback(Newtonsoft.Json.JsonConvert.DeserializeObject<JObject>(result));
}
else
callback(null);
}
}
45 changes: 45 additions & 0 deletions Assets/Plugins/Web3AuthSDK/Keystore/KeyStoreManagerUtils.cs
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 @@ -76,6 +80,36 @@ public static void deletePreferencesData(string key)
#endif
}

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);
Expand Down Expand Up @@ -108,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();
}
}
13 changes: 8 additions & 5 deletions Assets/Plugins/Web3AuthSDK/Samples/Web3AuthSample.cs
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 = "BG4pe3aBso5SjVbpotFQGnXVHgxhgOxnqnNBKyjfEJ3izFvIVWUaMIzoCrAfYag8O6t6a6AOvdLcS4JR2sQMjR4",
buildEnv = BuildEnv.PRODUCTION,
redirectUrl = new Uri("torusapp://com.torus.Web3AuthUnity/auth"),
network = Web3Auth.Network.SAPPHIRE_DEVNET
});
web3Auth.onLogin += onLogin;
web3Auth.onLogout += onLogout;
Expand Down
4 changes: 2 additions & 2 deletions Assets/Plugins/Web3AuthSDK/Samples/Web3AuthSample.unity
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ RenderSettings:
m_ReflectionIntensity: 1
m_CustomReflection: {fileID: 0}
m_Sun: {fileID: 0}
m_IndirectSpecularColor: {r: 0.4439372, g: 0.49315345, b: 0.5721989, a: 1}
m_IndirectSpecularColor: {r: 0.44657844, g: 0.49641222, b: 0.57481676, a: 1}
m_UseRadianceAmbientProbe: 0
--- !u!157 &3
LightmapSettings:
Expand Down Expand Up @@ -896,7 +896,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 3eab2a0bf902d6e4b9c2e968ad89f528, type: 3}
m_Name:
m_EditorClassIdentifier:
clientId: BJ6l3_kIQiy6YVL7zDlCcEAvGpGukwFgp-C_0WvNI_fAEeIaoVRLDrV5OjtbZr_zJxbyXFsXMT-yhQiUNYvZWpo
clientId: BG4pe3aBso5SjVbpotFQGnXVHgxhgOxnqnNBKyjfEJ3izFvIVWUaMIzoCrAfYag8O6t6a6AOvdLcS4JR2sQMjR4
redirectUri: torusapp://com.torus.Web3AuthUnity/auth
network: 1
--- !u!1 &529774071
Expand Down
59 changes: 50 additions & 9 deletions Assets/Plugins/Web3AuthSDK/Types/AES256CBC.cs
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);
}
}
16 changes: 16 additions & 0 deletions Assets/Plugins/Web3AuthSDK/Types/MfaSetting.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Collections.Generic;

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)
{
enable = enable;
priority = priority;
mandatory = mandatory;
}
}
44 changes: 44 additions & 0 deletions Assets/Plugins/Web3AuthSDK/Types/MfaSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
public class MfaSettings
{
private MfaSetting deviceShareFactor;
private MfaSetting backUpShareFactor;
private MfaSetting socialBackupFactor;
private MfaSetting passwordFactor;

public MfaSetting DeviceShareFactor
{
get { return deviceShareFactor; }
set { deviceShareFactor = value; }
}

public MfaSetting BackUpShareFactor
{
get { return backUpShareFactor; }
set { backUpShareFactor = value; }
}

public MfaSetting SocialBackupFactor
{
get { return socialBackupFactor; }
set { socialBackupFactor = value; }
}

public MfaSetting PasswordFactor
{
get { return passwordFactor; }
set { passwordFactor = value; }
}

// Constructors
public MfaSettings(
MfaSetting deviceShareFactor = null,
MfaSetting backUpShareFactor = null,
MfaSetting socialBackupFactor = null,
MfaSetting passwordFactor = null)
{
deviceShareFactor = deviceShareFactor;
backUpShareFactor = backUpShareFactor;
socialBackupFactor = socialBackupFactor;
passwordFactor = passwordFactor;
}
}
1 change: 1 addition & 0 deletions Assets/Plugins/Web3AuthSDK/Types/UserInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
public string idToken { get; set; }
public string oAuthIdToken { get; set; }
public string oAuthAccessToken { get; set; }
public bool isMfaEnabled { get; set; }
}
18 changes: 12 additions & 6 deletions Assets/Plugins/Web3AuthSDK/Types/Web3AuthOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,25 @@
public class Web3AuthOptions {
public string clientId { get; set; }
public Web3Auth.Network network { get; set; }

public Web3Auth.BuildEnv buildEnv { get; set; } = Web3Auth.BuildEnv.PRODUCTION;
public Uri redirectUrl { get; set; }
public string sdkUrl {
get {
if (network == Web3Auth.Network.TESTNET)
return "https://dev-sdk.openlogin.com";
else
return "https://sdk.openlogin.com";
if (buildEnv == Web3Auth.BuildEnv.STAGING)
return "https://staging-auth.web3auth.io/{openLoginVersion}";
else if (buildEnv == Web3Auth.BuildEnv.TESTING)
return "https://develop-auth.web3auth.io";
else
return "https://auth.web3auth.io/{openLoginVersion}";
}
set { }
}
public const string openLoginVersion = "v5";

public WhiteLabelData whiteLabel { get; set; }
public Dictionary<string, LoginConfigItem> loginConfig { get; set; }
public WhiteLabelData? whiteLabel { get; set; }
public Dictionary<string, LoginConfigItem>? loginConfig { get; set; }
public bool? useCoreKitKey { get; set; } = false;
public Web3Auth.ChainNamespace? chainNamespace { get; set; } = Web3Auth.ChainNamespace.EIP155;
public MfaSettings? mfaSettings { get; set; } = null;
}
17 changes: 10 additions & 7 deletions Assets/Plugins/Web3AuthSDK/Types/WhiteLabelData.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using System.Collections.Generic;
using Newtonsoft.Json;

public class WhiteLabelData {
public string name { get; set; }
public string logoLight { get; set; }
public string logoDark { get; set; }
public string defaultLanguage { get; set; } = "en";
public bool dark { get; set; } = false;
public Dictionary<string, string> theme { get; set; }
public class WhiteLabelData {
public string? appName { get; set; }
public string? logoLight { get; set; }
public string? logoDark { get; set; }
public Web3Auth.Language? defaultLanguage { get; set; } = Web3Auth.Language.en;
public Web3Auth.ThemeModes? mode { get; set; } = Web3Auth.ThemeModes.light;
public Dictionary<string, string>? theme { get; set; }
public string? appUrl { get; set; }
public bool? useLogoLoader { get; set; } = false;
}
Loading

0 comments on commit 334a861

Please sign in to comment.