Skip to content

Commit c7a4502

Browse files
author
Marcus Arnett
committed
Fix nonce generation to url b64
1 parent bc76c9a commit c7a4502

File tree

6 files changed

+38
-16
lines changed

6 files changed

+38
-16
lines changed

Sources/SuiKit/Extensions/Data.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,14 @@ public extension Data {
8484
}
8585
return UInt16(s % 65535)
8686
}
87+
88+
func base64urlEncodedString() -> String {
89+
var result = self.base64EncodedString()
90+
result = result.replacingOccurrences(of: "+", with: "-")
91+
result = result.replacingOccurrences(of: "/", with: "_")
92+
result = result.replacingOccurrences(of: "=", with: "")
93+
return result
94+
}
8795
}
8896

8997
public extension Data {

Sources/SuiKit/Utils/zkLogin/zkLoginNonce.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public struct zkLoginNonce {
4646
let ephPublicKey1 = publicKeyBytes % BigInt(2).power(128)
4747
let bigNum = try PoseidonUtilities.poseidonHash(inputs: [ephPublicKey0, ephPublicKey1, BigInt(maxEpoch), BigInt(randomness, radix: 10)!])
4848
let z = zkLoginUtilities.toBigEndianBytes(num: bigNum, width: 20)
49-
let nonce = Data(z).base64EncodedString()
49+
let nonce = Data(z).base64urlEncodedString()
5050
guard nonce.count == Self.nonceLength else { throw SuiError.notImplemented }
5151
return nonce
5252
}

Sources/SuiKit/Utils/zkLogin/zkLoginSignatureInputsClaim.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ public struct zkLoginSignatureInputsClaim: KeyProtocol, Equatable, Codable {
2929
public var value: String
3030
public var indexMod4: UInt8
3131

32+
public init(value: String, indexMod4: UInt8) {
33+
self.value = value
34+
self.indexMod4 = indexMod4
35+
}
36+
3237
public func serialize(_ serializer: Serializer) throws {
3338
try Serializer.str(serializer, self.value)
3439
try Serializer.u8(serializer, self.indexMod4)

Sources/SuiKit/Utils/zkLogin/zkLoginSignatureInputsProofPoints.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ public struct zkLoginSignatureInputsProofPoints: KeyProtocol, Equatable, Codable
3030
public var b: [[String]]
3131
public var c: [String]
3232

33+
public init(a: [String], b: [[String]], c: [String]) {
34+
self.a = a
35+
self.b = b
36+
self.c = c
37+
}
38+
3339
public func serialize(_ serializer: Serializer) throws {
3440
try serializer.sequence(self.a, Serializer.str)
3541
try serializer.uleb128(UInt(self.b.count))

Sources/SuiKit/Utils/zkLogin/zkLoginUtilities.swift

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,15 @@ public struct zkLoginUtilities {
5959
}
6060

6161
public static func toPaddedBigEndianBytes(num: BigInt, width: Int) -> [UInt8] {
62-
let hex = String(num, radix: 16, uppercase: false)
63-
64-
// Padding and Slicing
65-
let paddedHex = String(hex).leftPad(toLength: width * 2, withPad: "0")
66-
67-
// Convert Hex String to Bytes
68-
var bytes = [UInt8]()
69-
for i in stride(from: 0, to: paddedHex.count, by: 2) {
70-
let start = paddedHex.index(paddedHex.startIndex, offsetBy: i)
71-
let end = paddedHex.index(paddedHex.startIndex, offsetBy: i + 2)
72-
let byteString = paddedHex[start..<end]
73-
if let byte = UInt8(byteString, radix: 16) {
74-
bytes.append(byte)
75-
}
62+
let hex = String(num, radix: 16)
63+
let paddedHex = String(repeating: "0", count: max(0, width * 2 - hex.count)) + hex
64+
let finalHex = paddedHex.suffix(width * 2)
65+
return stride(from: 0, to: width * 2, by: 2).map {
66+
let startIndex = finalHex.index(finalHex.startIndex, offsetBy: $0)
67+
let endIndex = finalHex.index(startIndex, offsetBy: 2)
68+
let byteString = finalHex[startIndex..<endIndex]
69+
return UInt8(byteString, radix: 16)!
7670
}
77-
return bytes
7871
}
7972

8073
public static func findFirstNonZeroIndex(bytes: [UInt8]) -> Int {

Tests/SuiKitTests/Unit/zkLogin/JWTUtilsTest.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,14 @@ final class JWTUtilsTest: XCTestCase {
3838
)
3939
XCTAssertEqual(extractedValue, "https://accounts.google.com")
4040
}
41+
42+
func testThatGeneratingNonceWorksAsIntended() throws {
43+
let pk = try ED25519PublicKey(value: "dkUcNsSSYV2cFz+L/WAlyxINuXHpah/MJnYZ57/GtKY=")
44+
let nonce = try zkLoginNonce.generateNonce(
45+
publicKey: pk,
46+
maxEpoch: 954,
47+
randomness: "176720613486626510701195520524108477720"
48+
)
49+
XCTAssertEqual("NN9BV-W7MlsscmY042AddYkO1N8", nonce)
50+
}
4151
}

0 commit comments

Comments
 (0)