Skip to content

Commit

Permalink
- bug fix torus sdk, missing left padding on public keys from find ad…
Browse files Browse the repository at this point in the history
…dress

- copy their non public string extension into library and call it if necessary
  • Loading branch information
simonmcl committed Jan 23, 2024
1 parent f320547 commit 73d48c4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
10 changes: 10 additions & 0 deletions Sources/KukaiCoreSwift/Extensions/String+extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
}
19 changes: 15 additions & 4 deletions Sources/KukaiCoreSwift/Services/TorusAuthService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 73d48c4

Please sign in to comment.