Skip to content

Commit

Permalink
Add comments and TODOs. #119
Browse files Browse the repository at this point in the history
  • Loading branch information
kPatch committed Nov 20, 2024
1 parent 663a6a2 commit a260eb5
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 49 deletions.
15 changes: 7 additions & 8 deletions Assets/Sui-Unity-SDK/Code/Sui.ZKLogin/AccountAddress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@
//

using System;
using Sui.Utilities;
using System.Text;
using Sui.Cryptography;
using Konscious.Security.Cryptography;

namespace Sui.ZKLogin
{
/// <summary>
/// TODO: Look into where and how this is used in ZK Login TS
/// </summary>
public class AccountAddress : Accounts.AccountAddress
{
/// <summary>
Expand All @@ -39,22 +41,19 @@ public class AccountAddress : Accounts.AccountAddress
/// <param name="addressSeed">The address seed as BigInteger</param>
/// <param name="iss">The issuer string</param>
/// <returns>Normalized Sui address string</returns>
public Sui.Accounts.AccountAddress ComputeZkLoginAddressFromSeed(long addressSeed, string iss)
public Accounts.AccountAddress ComputeZkLoginAddressFromSeed(long addressSeed, string iss)
{
//TS: bytesToHex(blake2b(tmp, { dkLen: 32 })).slice(0, SUI_ADDRESS_LENGTH * 2),
//string hex = BitConverter.ToString(bytes);
// TS: bytesToHex(blake2b(tmp, { dkLen: 32 })).slice(0, SUI_ADDRESS_LENGTH * 2),
// string hex = BitConverter.ToString(bytes);
byte[] addressSeedBytesBigEndian = Utils.ToBigEndianBytes(addressSeed, 32);

// Normalize Google issuer
if (iss == "accounts.google.com")
{
iss = "https://accounts.google.com";
}
iss = "https://accounts.google.com"; //TODO: See / ask about implementation for OAuth providers

byte[] addressParamBytes = Encoding.UTF8.GetBytes(iss);
byte[] tmp = new byte[2 + addressSeedBytesBigEndian.Length + addressParamBytes.Length];


// Set signature scheme flag
tmp[0] = SignatureSchemeToFlag.ZkLogin;

Expand Down
5 changes: 5 additions & 0 deletions Assets/Sui-Unity-SDK/Code/Sui.ZKLogin/Bcs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

namespace Sui.ZKLogin
{
/// <summary>
/// TODO: Complete implementation
/// TODO: See if we can just make the ZKLogin Signature class `ISerializable` to that we can just call `serialize` on it.
/// TODO: See why this signature looks like this" inputs, maxEpochs, userSignature
/// </summary>
[JsonObject]
public class ZkLoginSignatureBCS: ISerializable
{
Expand Down
9 changes: 4 additions & 5 deletions Assets/Sui-Unity-SDK/Code/Sui.ZKLogin/JwtDecoder.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
using UnityEngine;
using System;
using System.Text;
using System.Collections.Generic;

namespace OpenDive.Utils.Jwt
{
using System;
using System.Text;
using UnityEngine;
using Newtonsoft.Json;

/// <summary>
/// A class to decode JWT tokens.
/// TODO: Add unit tests
/// </summary>
public class JWTDecoder
{
/// <summary>
Expand Down
15 changes: 3 additions & 12 deletions Assets/Sui-Unity-SDK/Code/Sui.ZKLogin/JwtUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@

namespace Sui.ZKLogin
{

//public record Claim
//{
// public string Value { get; init; }
// public int IndexMod4 { get; init; }
//}

/// <summary>
/// Represents a claim with a base64URL encoded value and its position indicator
/// </summary>
Expand All @@ -29,6 +22,9 @@ public class Claim
public int indexMod4;
}

/// <summary>
/// TODO: Add tests for JWT Utils
/// </summary>
public class JwtUtils
{
/// <summary>
Expand Down Expand Up @@ -59,11 +55,6 @@ private static int[] Base64UrlCharTo6Bits(string base64UrlChar)
return bits;
}

//private static int[] Base64UrlStringToBitVector(string base64UrlString)
//{
// return base64UrlString.SelectMany(c => Base64UrlCharTo6Bits(c)).ToArray();
//}

/// <summary>
/// Converts a base64URL encoded string to a bit vector
/// </summary>
Expand Down
6 changes: 4 additions & 2 deletions Assets/Sui-Unity-SDK/Code/Sui.ZKLogin/PublicKey.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@

namespace Sui.ZKLogin.SDK
{
using System;
using System.Text;
using System.Numerics;
using System.Threading.Tasks;
using UnityEngine;
using Sui.Cryptography;
using Sui.Cryptography.Ed25519;

/// <summary>
/// TODO: Implement ZKLoginPublicKey.
/// TODO: See how this is used and whether it can just be extended from the core PublicKey cass
/// </summary>
public class ZkLoginPublicKey : PublicKey
{
public ZkLoginPublicKey(byte[] public_key) : base(public_key)
Expand Down
23 changes: 13 additions & 10 deletions Assets/Sui-Unity-SDK/Code/Sui.ZKLogin/SDK/Address.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

namespace Sui.ZKLogin.SDK
{
/// <summary>
/// A utility class used to compute A Sui address from the:
/// UserSalt, and JWT token values (ClaimName, ClaimValue, Aud, ISS).
/// TODO: See how this is implemented / used in ZK Login TS. Can the SDK and outer accout `address` class be reconciled?
/// </summary>
public static class Address
{
public const int MAX_HEADER_LEN_B64 = 248;
Expand Down Expand Up @@ -47,11 +52,9 @@ public static string JwtToAddress(string jwt, string userSalt)
{
LengthChecks(jwt);

//var payload = JwtDecoder.DecodeJwt(jwt);
JWT decodedJWT = JWTDecoder.DecodeJWT(jwt);
JWTPayload payload = decodedJWT.Payload;


if (string.IsNullOrEmpty(payload.Sub) || string.IsNullOrEmpty(payload.Iss) || string.IsNullOrEmpty(payload.Aud))
throw new ArgumentException("Missing jwt data");

Expand All @@ -70,13 +73,20 @@ public static string JwtToAddress(string jwt, string userSalt)
});
}


public static string ComputeZkLoginAddress(ZkLoginAddressOptions options)
{
var seed = GenAddressSeed(options.UserSalt, options.ClaimName, options.ClaimValue, options.Aud);
return ComputeZkLoginAddressFromSeed(seed, options.Iss);
}

/// <summary>
///
/// </summary>
/// <param name="userSalt"></param>
/// <param name="claimName"></param>
/// <param name="claimValue"></param>
/// <param name="aud"></param>
/// <returns></returns>
private static BigInteger GenAddressSeed(string userSalt, string claimName, string claimValue, string aud)
{
using var sha256 = SHA256.Create();
Expand Down Expand Up @@ -114,11 +124,4 @@ public class ZkLoginAddressOptions
public string Iss { get; set; }
public string Aud { get; set; }
}

public class JwtPayload
{
public string Sub { get; set; }
public string Iss { get; set; }
public string Aud { get; set; }
}
}
10 changes: 5 additions & 5 deletions Assets/Sui-Unity-SDK/Code/Sui.ZKLogin/SDK/Nonce.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

namespace Sui.ZKLogin.SDK
{
/// <summary>
/// TODO: See if there are any issues with using RNGCryptoServiceProvider on mobile or WebGL
/// TODO: See how TS implements this. Perhaps we can use a difference source of randomness.
/// </summary>
public static class NonceGenerator
{
public const int NONCE_LENGTH = 27;
Expand All @@ -22,7 +26,7 @@ private static BigInteger ToBigIntBE(byte[] bytes)
public static string GenerateRandomness()
{
byte[] randomBytes = new byte[16];
// IRVIN: See the impact of using this. TypeScript uses `noble/hashes`
// TODO: See the impact of using this. TypeScript uses `noble/hashes`
using (var rng = new RNGCryptoServiceProvider())
{
rng.GetBytes(randomBytes);
Expand Down Expand Up @@ -57,9 +61,7 @@ public static string GenerateNonce(PublicKey publicKey, int maxEpoch, BigInteger
string nonce = Base64UrlEncode(Z);

if (nonce.Length != NONCE_LENGTH)
{
throw new Exception($"Length of nonce {nonce} ({nonce.Length}) is not equal to {NONCE_LENGTH}");
}

return nonce;
}
Expand All @@ -71,9 +73,7 @@ private static byte[] ToPaddedBigEndianBytes(BigInteger value, int length)
Array.Reverse(bytes); // Convert to big-endian

if (bytes.Length > length)
{
throw new ArgumentException($"Value too large for {length} bytes");
}

byte[] paddedBytes = new byte[length];
Array.Copy(bytes, 0, paddedBytes, length - bytes.Length, bytes.Length);
Expand Down
1 change: 0 additions & 1 deletion Assets/Sui-Unity-SDK/Code/Sui.ZKLogin/SDK/Poseidon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public static class PoseidonHasher
Poseidon16.Hash,
};


/// <summary>
/// Runs Poseidon Hash.
/// The inputs can either be a int, long, or string array.
Expand Down
6 changes: 4 additions & 2 deletions Assets/Sui-Unity-SDK/Code/Sui.ZKLogin/Signature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Numerics;
using Unity.Plastic.Newtonsoft.Json;
using OpenDive.BCS;
using Org.BouncyCastle.Asn1.Ocsp;
using Sui.Cryptography;

namespace Sui.ZKLogin
Expand Down Expand Up @@ -33,6 +32,10 @@ namespace Sui.ZKLogin
// }
//}

/// <summary>
/// TODO: Implement ZkLogin Signature.
/// TODO: See if it can be extended from the core Signature class.
/// </summary>
[JsonObject]
public class ZkLoginSignature : ISerializable
{
Expand All @@ -44,7 +47,6 @@ public class ZkLoginSignature : ISerializable
//{
// this.SigInputs = inputs;
// this.MaxEpoch = MaxEpoch;

//}

[JsonProperty("inputs")]
Expand Down
9 changes: 5 additions & 4 deletions Assets/Sui-Unity-SDK/Code/Sui.ZKLogin/Utils.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System;
using System.Linq;
using System.Numerics;
using UnityEngine;

namespace Sui.ZKLogin
{
/// <summary>
/// Utility functions used within ZK Login implementation.
/// TODO: See if we have implemented this in the core Utils class. Does it make sense to move it there? (I think so).
/// </summary>
public static class Utils
{

Expand Down

0 comments on commit a260eb5

Please sign in to comment.