Skip to content

Commit

Permalink
Add updates to ZKLogin Signature and sample Auth manager. #119
Browse files Browse the repository at this point in the history
  • Loading branch information
kPatch committed Nov 16, 2024
1 parent c26dda0 commit 663a6a2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Assets/Sui-Unity-SDK/Code/Sui.ZKLogin/Signature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
51 changes: 45 additions & 6 deletions Assets/Sui-Unity-SDK/SDK-Examples/FirebaseAuthManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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 + " -- "
Expand All @@ -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)
{
Expand Down Expand Up @@ -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(
//);
});
});
}
Expand Down

0 comments on commit 663a6a2

Please sign in to comment.