From 663a6a279755d0eef549f8792bebb9ed56b17eb0 Mon Sep 17 00:00:00 2001 From: Irvin Date: Sat, 16 Nov 2024 12:03:41 +0700 Subject: [PATCH] Add updates to ZKLogin Signature and sample Auth manager. #119 --- .../Code/Sui.ZKLogin/Signature.cs | 2 +- .../SDK-Examples/FirebaseAuthManager.cs | 51 ++++++++++++++++--- 2 files changed, 46 insertions(+), 7 deletions(-) diff --git a/Assets/Sui-Unity-SDK/Code/Sui.ZKLogin/Signature.cs b/Assets/Sui-Unity-SDK/Code/Sui.ZKLogin/Signature.cs index 001415f..c87ee5c 100644 --- a/Assets/Sui-Unity-SDK/Code/Sui.ZKLogin/Signature.cs +++ b/Assets/Sui-Unity-SDK/Code/Sui.ZKLogin/Signature.cs @@ -81,7 +81,7 @@ public static byte[] GetZkLoginSignatureBytes(Inputs inputs, ulong maxEpoch, byt return sigBytes; } - public string GetZkLoginSignature(Inputs inputs, ulong maxEpoch, byte[] userSignature) + public static string GetZkLoginSignature(Inputs inputs, ulong maxEpoch, byte[] userSignature) { byte[] bytes = GetZkLoginSignatureBytes(inputs, maxEpoch, userSignature); byte[] signatureBytes = new byte[bytes.Length + 1]; diff --git a/Assets/Sui-Unity-SDK/SDK-Examples/FirebaseAuthManager.cs b/Assets/Sui-Unity-SDK/SDK-Examples/FirebaseAuthManager.cs index 821ac96..305644e 100644 --- a/Assets/Sui-Unity-SDK/SDK-Examples/FirebaseAuthManager.cs +++ b/Assets/Sui-Unity-SDK/SDK-Examples/FirebaseAuthManager.cs @@ -5,9 +5,9 @@ using OpenDive.Utils.Jwt; using UnityEngine; using Firebase.Extensions; -using i5.Toolkit.Core.RocketChatClient; -using System.Net.Mail; -using Newtonsoft.Json.Linq; +using Sui.Cryptography.Ed25519; +using System.Security.Cryptography; +using System.Numerics; public class FirebaseAuthManager : MonoBehaviour { @@ -104,9 +104,9 @@ private void OnLoginCompleted(object sender, EventArgs e) return; } - string idToken = task.Result; + string jwtIdToken = task.Result; - Debug.Log("ID TOKEN: " + idToken); + Debug.Log("ID TOKEN: " + jwtIdToken); //JwtPayload payload = JwtDecoder.DecodeJwt(idToken); //Debug.Log(payload.Aud + " -- " @@ -115,7 +115,7 @@ private void OnLoginCompleted(object sender, EventArgs e) // + payload.Iat + " --- " // + payload.Iss + " ... "); - JWT decodedJWT = JWTDecoder.DecodeJWT(idToken); + JWT decodedJWT = JWTDecoder.DecodeJWT(jwtIdToken); if (decodedJWT != null) { @@ -144,6 +144,45 @@ private void OnLoginCompleted(object sender, EventArgs e) // Send token to your backend via HTTPS // ... + + byte[] byteSalt = new byte[16]; // 16 bytes = 128 bits + using (var rng = RandomNumberGenerator.Create()) + { + rng.GetBytes(byteSalt); // Fill the array with secure random bytes + } + + Debug.Log("BYTE SALT: " + byteSalt); + + string strSalt = BitConverter.ToString(byteSalt); + + Debug.Log("SALT STRING: " + strSalt); + string seed = Sui.ZKLogin.SDK.Address.JwtToAddress(jwtIdToken, strSalt); + + Debug.Log("SEED: " + seed); + + Sui.ZKLogin.AccountAddress accountAddress = new Sui.ZKLogin.AccountAddress(); + //Sui.ZKLogin.AccountAddress zkLoginAddress + // = accountAddress.ComputeZkLoginAddressFromSeed(seed, decodedJWT.Payload.Iss); + + Debug.Log("ACCOUNT ADDRESS: " + accountAddress); + + string private_key_hex = "0x99da9559e15e913ee9ab2e53e3dfad575da33b49be1125bb922e33494f498828"; + PrivateKey ephemeralPk = new PrivateKey(private_key_hex); + PublicKey pubKey = (PublicKey) ephemeralPk.PublicKey(); + + string extPubKey = Sui.ZKLogin.SDK.Utils.GetExtendedEphemeralPublicKey(pubKey); + + // GEN ADDRESS SEED: + BigInteger addressSeed = Sui.ZKLogin.SDK.Utils.GenAddressSeed( + strSalt, + "sub", + decodedJWT.Payload.Sub, + decodedJWT.Payload.Aud + ); + + //https://docs.sui.io/guides/developer/cryptography/zklogin-integration + //string zkLoginSig = Sui.ZKLogin.ZkLoginSignature.GetZkLoginSignature( + //); }); }); }