diff --git a/Sources/KukaiCoreSwift/Extensions/String+extensions.swift b/Sources/KukaiCoreSwift/Extensions/String+extensions.swift index 692b3357..a450c29c 100644 --- a/Sources/KukaiCoreSwift/Extensions/String+extensions.swift +++ b/Sources/KukaiCoreSwift/Extensions/String+extensions.swift @@ -34,4 +34,14 @@ public extension String { return nil } } + + /// Pad the left side of a string with characters + func leftPadding(toLength: Int, withPad character: Character) -> String { + let stringLength = count + if stringLength < toLength { + return String(repeatElement(character, count: toLength - stringLength)) + self + } else { + return String(suffix(toLength)) + } + } } diff --git a/Sources/KukaiCoreSwift/Services/TorusAuthService.swift b/Sources/KukaiCoreSwift/Services/TorusAuthService.swift index ab62ac08..282df906 100644 --- a/Sources/KukaiCoreSwift/Services/TorusAuthService.swift +++ b/Sources/KukaiCoreSwift/Services/TorusAuthService.swift @@ -331,10 +331,21 @@ public class TorusAuthService: NSObject { } let data = try await self.torusUtils.getPublicAddress(endpoints: nd.getTorusNodeEndpoints(), torusNodePubs: nd.getTorusNodePub(), verifier: verifierName, verifierId: socialUserId, isExtended: true) - guard let pubX = data.x, - let pubY = data.y, - let bytesX = Sodium.shared.utils.hex2bin(pubX), - let bytesY = Sodium.shared.utils.hex2bin(pubY) else { + var pubX = data.x + var pubY = data.y + + if let x = pubX, x.count > 0 && x.count < 64 { + pubX = pubX?.leftPadding(toLength: (64 - x.count), withPad: "0") + } + + if let y = pubY, y.count > 0 && y.count < 64 { + pubY = pubY?.leftPadding(toLength: (64 - y.count), withPad: "0") + } + + guard let x = pubX, + let y = pubY, + let bytesX = Sodium.shared.utils.hex2bin(x), + let bytesY = Sodium.shared.utils.hex2bin(y) else { Logger.torus.error("Finding address - no valid pub key x and y returned") completion(Result.failure(KukaiError.internalApplicationError(error: TorusAuthError.invalidTorusResponse))) return