Skip to content

Commit 334a861

Browse files
Merge pull request #24 from Web3Auth/feat/sapphire-updates
Feat/sapphire updates
2 parents 5e3342b + ae2c203 commit 334a861

File tree

11 files changed

+348
-74
lines changed

11 files changed

+348
-74
lines changed

Assets/Plugins/Web3AuthSDK/Api/Web3AuthApi.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,24 @@ public IEnumerator logout(LogoutApiRequest logoutApiRequest, Action<JObject> cal
5050
else
5151
callback(null);
5252
}
53+
54+
public IEnumerator createSession(LogoutApiRequest logoutApiRequest, Action<JObject> callback)
55+
{
56+
WWWForm data = new WWWForm();
57+
data.AddField("key", logoutApiRequest.key);
58+
data.AddField("data", logoutApiRequest.data);
59+
data.AddField("signature", logoutApiRequest.signature);
60+
data.AddField("timeout", logoutApiRequest.timeout.ToString());
61+
62+
var request = UnityWebRequest.Post($"{baseAddress}/store/set", data);
63+
yield return request.SendWebRequest();
64+
65+
if (request.result == UnityWebRequest.Result.Success)
66+
{
67+
string result = request.downloadHandler.text;
68+
callback(Newtonsoft.Json.JsonConvert.DeserializeObject<JObject>(result));
69+
}
70+
else
71+
callback(null);
72+
}
5373
}

Assets/Plugins/Web3AuthSDK/Keystore/KeyStoreManagerUtils.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
using Org.BouncyCastle.Math;
77
using Org.BouncyCastle.Utilities.Encoders;
88
using System.Runtime.InteropServices;
9+
using Org.BouncyCastle.Security;
10+
using Org.BouncyCastle.OpenSsl;
11+
using Org.BouncyCastle.Crypto;
12+
using System.Text;
913

1014
public class KeyStoreManagerUtils
1115
{
@@ -76,6 +80,36 @@ public static void deletePreferencesData(string key)
7680
#endif
7781
}
7882

83+
public static AsymmetricCipherKeyPair generateECKeyPair()
84+
{
85+
var secureRandom = new SecureRandom();
86+
var curve = SecNamedCurves.GetByName("secp256k1");
87+
var domainParams = new ECDomainParameters(curve.Curve, curve.G, curve.N, curve.H, curve.GetSeed());
88+
89+
var keyGenParam = new ECKeyGenerationParameters(domainParams, secureRandom);
90+
var generator = GeneratorUtilities.GetKeyPairGenerator("ECDSA");
91+
generator.Init(keyGenParam);
92+
93+
return generator.GenerateKeyPair();
94+
}
95+
96+
public static string generateRandomSessionKey() {
97+
var keyPair = generateECKeyPair();
98+
var privateKey = (ECPrivateKeyParameters)keyPair.Private;
99+
var publicKey = (ECPublicKeyParameters)keyPair.Public;
100+
101+
string privateKeyHex = privateKey.D.ToString(16).PadLeft(64, '0');
102+
return privateKeyHex;
103+
}
104+
105+
public static byte[] generateRandomBytes()
106+
{
107+
var secureRandom = new SecureRandom();
108+
byte[] bytes = new byte[16];
109+
secureRandom.NextBytes(bytes);
110+
return bytes;
111+
}
112+
79113
public static string getECDSASignature(string privateKey, string data){
80114
var curve = SecNamedCurves.GetByName("secp256k1");
81115
var domain = new ECDomainParameters(curve.Curve, curve.G, curve.N, curve.H);
@@ -108,4 +142,15 @@ public static string getECDSASignature(string privateKey, string data){
108142

109143
return Hex.ToHexString(derSignature);
110144
}
145+
146+
public static string convertByteToHexadecimal(byte[] byteArray)
147+
{
148+
string hex = "";
149+
// Iterating through each byte in the array
150+
foreach (byte b in byteArray)
151+
{
152+
hex += $"{b:X2}";
153+
}
154+
return hex.ToLowerInvariant();
155+
}
111156
}

Assets/Plugins/Web3AuthSDK/Samples/Web3AuthSample.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,19 @@ void Start()
4646
{
4747
verifier = "your_verifierid_from_web3auth_dashboard",
4848
typeOfLogin = TypeOfLogin.GOOGLE,
49-
clientId = "your_clientid_from_google_or_etc"
49+
clientId = "your_clientId_from_web3auth_dashboard"
5050
};
5151

5252
web3Auth = GetComponent<Web3Auth>();
5353
web3Auth.setOptions(new Web3AuthOptions()
5454
{
5555
whiteLabel = new WhiteLabelData()
5656
{
57-
name = "Web3Auth Sample App",
57+
appName = "Web3Auth Sample App",
5858
logoLight = null,
5959
logoDark = null,
60-
defaultLanguage = "en",
61-
dark = true,
60+
defaultLanguage = Language.en,
61+
mode = ThemeModes.dark,
6262
theme = new Dictionary<string, string>
6363
{
6464
{ "primary", "#123456" }
@@ -72,7 +72,10 @@ void Start()
7272
{"CUSTOM_VERIFIER", loginConfigItem}
7373
}
7474
*/
75-
network = Web3Auth.Network.TESTNET
75+
clientId = "BG4pe3aBso5SjVbpotFQGnXVHgxhgOxnqnNBKyjfEJ3izFvIVWUaMIzoCrAfYag8O6t6a6AOvdLcS4JR2sQMjR4",
76+
buildEnv = BuildEnv.PRODUCTION,
77+
redirectUrl = new Uri("torusapp://com.torus.Web3AuthUnity/auth"),
78+
network = Web3Auth.Network.SAPPHIRE_DEVNET
7679
});
7780
web3Auth.onLogin += onLogin;
7881
web3Auth.onLogout += onLogout;

Assets/Plugins/Web3AuthSDK/Samples/Web3AuthSample.unity

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ RenderSettings:
3838
m_ReflectionIntensity: 1
3939
m_CustomReflection: {fileID: 0}
4040
m_Sun: {fileID: 0}
41-
m_IndirectSpecularColor: {r: 0.4439372, g: 0.49315345, b: 0.5721989, a: 1}
41+
m_IndirectSpecularColor: {r: 0.44657844, g: 0.49641222, b: 0.57481676, a: 1}
4242
m_UseRadianceAmbientProbe: 0
4343
--- !u!157 &3
4444
LightmapSettings:
@@ -896,7 +896,7 @@ MonoBehaviour:
896896
m_Script: {fileID: 11500000, guid: 3eab2a0bf902d6e4b9c2e968ad89f528, type: 3}
897897
m_Name:
898898
m_EditorClassIdentifier:
899-
clientId: BJ6l3_kIQiy6YVL7zDlCcEAvGpGukwFgp-C_0WvNI_fAEeIaoVRLDrV5OjtbZr_zJxbyXFsXMT-yhQiUNYvZWpo
899+
clientId: BG4pe3aBso5SjVbpotFQGnXVHgxhgOxnqnNBKyjfEJ3izFvIVWUaMIzoCrAfYag8O6t6a6AOvdLcS4JR2sQMjR4
900900
redirectUri: torusapp://com.torus.Web3AuthUnity/auth
901901
network: 1
902902
--- !u!1 &529774071

Assets/Plugins/Web3AuthSDK/Types/AES256CBC.cs

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,19 @@
44
using Org.BouncyCastle.Math;
55
using Org.BouncyCastle.Security;
66
using System.Security.Cryptography;
7+
using System.IO;
8+
using System;
9+
using System.Text;
710

811
public class AES256CBC
912
{
1013
private static string TRANSFORMATION = "AES/CBC/PKCS7PADDING";
1114
private byte[] AES_ENCRYPTION_KEY;
1215
private byte[] ENCRYPTION_IV;
1316

17+
private byte[] MAC_KEY;
18+
private byte[] ENCRYPTION_EPHEM_KEY;
19+
1420
public AES256CBC(string privateKeyHex, string ephemPublicKeyHex, string encryptionIvHex)
1521
{
1622
using (SHA512 shaM = new SHA512Managed())
@@ -24,35 +30,39 @@ public AES256CBC(string privateKeyHex, string ephemPublicKeyHex, string encrypti
2430
System.Array.Copy(hash, encKeyBytes, 32);
2531

2632
AES_ENCRYPTION_KEY = encKeyBytes;
27-
ENCRYPTION_IV = toByteArray(encryptionIvHex);
2833

34+
MAC_KEY = new byte[hash.Length - 32];
35+
System.Array.Copy(hash, 32, MAC_KEY, 0, MAC_KEY.Length);
36+
37+
ENCRYPTION_IV = toByteArray(encryptionIvHex);
38+
ENCRYPTION_EPHEM_KEY = toByteArray(ephemPublicKeyHex);
2939
}
3040
}
3141

32-
public string encrypt(byte[] src)
42+
public byte[] encrypt(byte[] src)
3343
{
3444
var key = ParameterUtilities.CreateKeyParameter("AES", AES_ENCRYPTION_KEY);
3545
var parametersWithIv = new ParametersWithIV(key, ENCRYPTION_IV);
3646

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

40-
return System.Text.Encoding.UTF8.GetString(
41-
cipher.DoFinal(src)
42-
);
50+
return cipher.DoFinal(src);
4351
}
4452

45-
public string decrypt(byte[] src)
53+
public byte[] decrypt(byte[] src, string mac)
4654
{
55+
if (!hmacSha256Verify(MAC_KEY, getCombinedData(src), mac))
56+
{
57+
throw new SystemException("Bad MAC error during decrypt");
58+
}
4759
var key = ParameterUtilities.CreateKeyParameter("AES", AES_ENCRYPTION_KEY);
4860
var parametersWithIv = new ParametersWithIV(key, ENCRYPTION_IV);
4961

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

53-
return System.Text.Encoding.UTF8.GetString(
54-
cipher.DoFinal(src)
55-
);
65+
return cipher.DoFinal(src);
5666
}
5767

5868

@@ -97,4 +107,35 @@ public static byte[] toByteArray(BigInteger bi)
97107
}
98108
return b;
99109
}
110+
111+
public byte[] getCombinedData(byte[] cipherTextBytes)
112+
{
113+
using (MemoryStream outputStream = new MemoryStream())
114+
{
115+
outputStream.Write(ENCRYPTION_IV, 0, ENCRYPTION_IV.Length);
116+
outputStream.Write(ENCRYPTION_EPHEM_KEY, 0, ENCRYPTION_EPHEM_KEY.Length);
117+
outputStream.Write(cipherTextBytes, 0, cipherTextBytes.Length);
118+
return outputStream.ToArray();
119+
}
120+
}
121+
122+
public byte[] getMac(byte[] cipherTextBytes)
123+
{
124+
return hmacSha256Sign(MAC_KEY, getCombinedData(cipherTextBytes));
125+
}
126+
127+
public byte[] hmacSha256Sign(byte[] key, byte[] data)
128+
{
129+
using (HMACSHA256 hmac = new HMACSHA256(key))
130+
{
131+
return hmac.ComputeHash(data);
132+
}
133+
}
134+
135+
public bool hmacSha256Verify(byte[] key, byte[] data, string sig)
136+
{
137+
byte[] expectedSig = hmacSha256Sign(key, data);
138+
string expectedSigHex = BitConverter.ToString(expectedSig).Replace("-", "").ToLower();
139+
return expectedSigHex.Equals(sig);
140+
}
100141
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System.Collections.Generic;
2+
3+
public class MfaSetting
4+
{
5+
public bool enable { get; set; }
6+
public int? priority { get; set; }
7+
public bool? mandatory { get; set; }
8+
9+
// Constructor
10+
public MfaSetting(bool enable, int? priority, bool? mandatory)
11+
{
12+
enable = enable;
13+
priority = priority;
14+
mandatory = mandatory;
15+
}
16+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
public class MfaSettings
2+
{
3+
private MfaSetting deviceShareFactor;
4+
private MfaSetting backUpShareFactor;
5+
private MfaSetting socialBackupFactor;
6+
private MfaSetting passwordFactor;
7+
8+
public MfaSetting DeviceShareFactor
9+
{
10+
get { return deviceShareFactor; }
11+
set { deviceShareFactor = value; }
12+
}
13+
14+
public MfaSetting BackUpShareFactor
15+
{
16+
get { return backUpShareFactor; }
17+
set { backUpShareFactor = value; }
18+
}
19+
20+
public MfaSetting SocialBackupFactor
21+
{
22+
get { return socialBackupFactor; }
23+
set { socialBackupFactor = value; }
24+
}
25+
26+
public MfaSetting PasswordFactor
27+
{
28+
get { return passwordFactor; }
29+
set { passwordFactor = value; }
30+
}
31+
32+
// Constructors
33+
public MfaSettings(
34+
MfaSetting deviceShareFactor = null,
35+
MfaSetting backUpShareFactor = null,
36+
MfaSetting socialBackupFactor = null,
37+
MfaSetting passwordFactor = null)
38+
{
39+
deviceShareFactor = deviceShareFactor;
40+
backUpShareFactor = backUpShareFactor;
41+
socialBackupFactor = socialBackupFactor;
42+
passwordFactor = passwordFactor;
43+
}
44+
}

Assets/Plugins/Web3AuthSDK/Types/UserInfo.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@
1111
public string idToken { get; set; }
1212
public string oAuthIdToken { get; set; }
1313
public string oAuthAccessToken { get; set; }
14+
public bool isMfaEnabled { get; set; }
1415
}

Assets/Plugins/Web3AuthSDK/Types/Web3AuthOptions.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,25 @@
44
public class Web3AuthOptions {
55
public string clientId { get; set; }
66
public Web3Auth.Network network { get; set; }
7+
8+
public Web3Auth.BuildEnv buildEnv { get; set; } = Web3Auth.BuildEnv.PRODUCTION;
79
public Uri redirectUrl { get; set; }
810
public string sdkUrl {
911
get {
10-
if (network == Web3Auth.Network.TESTNET)
11-
return "https://dev-sdk.openlogin.com";
12-
else
13-
return "https://sdk.openlogin.com";
12+
if (buildEnv == Web3Auth.BuildEnv.STAGING)
13+
return "https://staging-auth.web3auth.io/{openLoginVersion}";
14+
else if (buildEnv == Web3Auth.BuildEnv.TESTING)
15+
return "https://develop-auth.web3auth.io";
16+
else
17+
return "https://auth.web3auth.io/{openLoginVersion}";
1418
}
1519
set { }
1620
}
21+
public const string openLoginVersion = "v5";
1722

18-
public WhiteLabelData whiteLabel { get; set; }
19-
public Dictionary<string, LoginConfigItem> loginConfig { get; set; }
23+
public WhiteLabelData? whiteLabel { get; set; }
24+
public Dictionary<string, LoginConfigItem>? loginConfig { get; set; }
2025
public bool? useCoreKitKey { get; set; } = false;
2126
public Web3Auth.ChainNamespace? chainNamespace { get; set; } = Web3Auth.ChainNamespace.EIP155;
27+
public MfaSettings? mfaSettings { get; set; } = null;
2228
}
Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
using System.Collections.Generic;
2+
using Newtonsoft.Json;
23

3-
public class WhiteLabelData {
4-
public string name { get; set; }
5-
public string logoLight { get; set; }
6-
public string logoDark { get; set; }
7-
public string defaultLanguage { get; set; } = "en";
8-
public bool dark { get; set; } = false;
9-
public Dictionary<string, string> theme { get; set; }
4+
public class WhiteLabelData {
5+
public string? appName { get; set; }
6+
public string? logoLight { get; set; }
7+
public string? logoDark { get; set; }
8+
public Web3Auth.Language? defaultLanguage { get; set; } = Web3Auth.Language.en;
9+
public Web3Auth.ThemeModes? mode { get; set; } = Web3Auth.ThemeModes.light;
10+
public Dictionary<string, string>? theme { get; set; }
11+
public string? appUrl { get; set; }
12+
public bool? useLogoLoader { get; set; } = false;
1013
}

0 commit comments

Comments
 (0)