Skip to content

Commit

Permalink
Add ZK Login implementation. #119
Browse files Browse the repository at this point in the history
  • Loading branch information
kPatch committed Nov 15, 2024
1 parent 05cdaa5 commit 7703f28
Show file tree
Hide file tree
Showing 8 changed files with 456 additions and 73 deletions.
56 changes: 0 additions & 56 deletions Assets/Sui-Unity-SDK/Code/Sui.Utilities/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,62 +67,6 @@ public static byte[] HexStringToByteArray(this string input)
return output;
}

/// <summary>
/// Finds the index of the first non-zero byte in a byte array.
/// </summary>
/// <param name="bytes">The byte array to search</param>
/// <returns>The index of the first non-zero byte, or -1 if all bytes are zero</returns>
public static int FindFirstNonZeroIndex(byte[] bytes)
{
for (int i = 0; i < bytes.Length; i++)
{
if (bytes[i] != 0)
{
return i;
}
}

return -1;
}

/// <summary>
/// Converts a BigInteger to a byte array padded to specified width.
/// </summary>
/// <param name="num">The number to convert</param>
/// <param name="width">The desired width in bytes</param>
/// <returns>Byte array padded to specified width</returns>
public static byte[] ToPaddedBigEndianBytes(this BigInteger num, int width)
{
// Convert to hex string and pad
string hex = num.ToString("X");
hex = hex.PadLeft(width * 2, '0');

// Take only the last width*2 characters to match desired byte length
hex = hex.Substring(Math.Max(0, hex.Length - width * 2));

return HexStringToByteArray(hex);
}

/// <summary>
/// Converts a BigInteger to a big-endian byte array, removing leading zeros but ensuring at least one byte is returned.
/// </summary>
/// <param name="num">The number to convert</param>
/// <param name="width">The maximum width in bytes</param>
/// <returns>A byte array representing the number with minimal leading zeros</returns>
public static byte[] ToBigEndianBytes(BigInteger num, int width)
{
byte[] bytes = ToPaddedBigEndianBytes(num, width);

int firstNonZeroIndex = FindFirstNonZeroIndex(bytes);

if (firstNonZeroIndex == -1)
{
return new byte[] { 0 };
}

return bytes.Skip(firstNonZeroIndex).ToArray();
}

/// <summary>
/// Fast check if the string to know if base58 str
/// </summary>
Expand Down
73 changes: 73 additions & 0 deletions Assets/Sui-Unity-SDK/Code/Sui.ZKLogin/Bcs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System.Numerics;
using Newtonsoft.Json;
using OpenDive.BCS;

namespace Sui.ZKLogin
{
[JsonObject]
public class ZkLoginSignature: ISerializable
{
[JsonProperty("inputs")]
public Inputs Inputs { get; set; }

[JsonProperty("maxEpoch")]
public BigInteger MaxEpoch { get; set; }

[JsonProperty("userSignature")]
public BigInteger UserSignature { get; set; }

public void Serialize(Serialization serializer)
{
throw new System.NotImplementedException();
}

public static ISerializable Deserialize(Deserialization deserializer)
{
throw new System.NotImplementedException();
}
}

[JsonObject]
public class Inputs
{
[JsonProperty("proofPoints")]
public ProofPoints proofPoints { get; set; }

[JsonProperty("issBase64Details")]
public ZkLoginSignatureInputsClaim IssBase64Details { get; set; }

[JsonProperty("headerBase64")]
public BigInteger HeaderBase64 { get; set; }

[JsonProperty("addressSeed")]
public BigInteger AddressSeed { get; set; }
}

[JsonObject]
public class ProofPoints
{
// Sequence input = new Sequence(new string[] { "a", "abc", "def", "ghi" }.ToList().Select(str => new BString(str)).ToArray());
[JsonProperty("a")]
public Sequence A { get; set; } // a: bcs.vector(bcs.string())

[JsonProperty("b")]
public Sequence B { get; set; } // b: bcs.vector(bcs.vector(bcs.string())),

[JsonProperty("c")]
public Sequence C { get; set; } // c: bcs.vector(bcs.string()),
}

[JsonObject]
public class ZkLoginSignatureInputsClaim
{
[JsonProperty("value")]
public BigInteger Value { get; set; }

[JsonProperty("indexMod4")]
public BigInteger IndexMod4 { get; set; }
}

public static class Bcs
{
}
}
11 changes: 11 additions & 0 deletions Assets/Sui-Unity-SDK/Code/Sui.ZKLogin/Bcs.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7703f28

Please sign in to comment.