From ecf4a77f1eb387d0fd048f1431f5f3189e793977 Mon Sep 17 00:00:00 2001 From: metalurgical <97008724+metalurgical@users.noreply.github.com> Date: Tue, 30 Jul 2024 08:30:19 +0200 Subject: [PATCH] fix: token could be hex or base64 --- Sources/TorusUtils/Helpers/NodeUtils.swift | 6 +++++- Tests/TorusUtilsTests/SapphireDevnetTest.swift | 4 ++-- Tests/TorusUtilsTests/SapphireMainnetTests.swift | 3 +-- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/Sources/TorusUtils/Helpers/NodeUtils.swift b/Sources/TorusUtils/Helpers/NodeUtils.swift index 1db1e08f..d823d0a4 100644 --- a/Sources/TorusUtils/Helpers/NodeUtils.swift +++ b/Sources/TorusUtils/Helpers/NodeUtils.swift @@ -462,7 +462,11 @@ internal class NodeUtils { if item == nil { sessionTokenDatas.append(nil) } else { - sessionTokenDatas.append(SessionToken(token: Data(hexString: item!)!.base64EncodedString(), signature: sessionTokenSigs[i]!.data(using: .utf8)!.hexString, node_pubx: shareResponses[i].nodePubX, node_puby: shareResponses[i].nodePubY)) + if Data(hexString: item!) != nil { + sessionTokenDatas.append(SessionToken(token: Data(hexString: item!)!.base64EncodedString(), signature: sessionTokenSigs[i]!.data(using: .utf8)!.hexString, node_pubx: shareResponses[i].nodePubX, node_puby: shareResponses[i].nodePubY)) + } else { + sessionTokenDatas.append(SessionToken(token: item!, signature: sessionTokenSigs[i]!.data(using: .utf8)!.hexString, node_pubx: shareResponses[i].nodePubX, node_puby: shareResponses[i].nodePubY)) + } } } diff --git a/Tests/TorusUtilsTests/SapphireDevnetTest.swift b/Tests/TorusUtilsTests/SapphireDevnetTest.swift index 900c2b3b..fabd4144 100644 --- a/Tests/TorusUtilsTests/SapphireDevnetTest.swift +++ b/Tests/TorusUtilsTests/SapphireDevnetTest.swift @@ -291,12 +291,12 @@ final class SapphireDevnetTest: XCTestCase { let signatures = data.sessionData.sessionTokenData.map({ $0!.token }) let parsedSignatures = signatures.map({ let data = Data(base64Encoded: $0.data(using: .utf8)!)! - let hexString = String(data: data, encoding: .utf8)! - let json = hexString.hexEncodedToString() + let json = String(data: data, encoding: .utf8)! return json }) let now = Int(Date().timeIntervalSince1970) for item in parsedSignatures { + let json = try JSONSerialization.jsonObject(with: item.data(using: .utf8)!) as? [String: Any] let exp = json!["exp"] as! Int let sessionTime = exp - now diff --git a/Tests/TorusUtilsTests/SapphireMainnetTests.swift b/Tests/TorusUtilsTests/SapphireMainnetTests.swift index ac29a202..5bfbf28b 100644 --- a/Tests/TorusUtilsTests/SapphireMainnetTests.swift +++ b/Tests/TorusUtilsTests/SapphireMainnetTests.swift @@ -270,8 +270,7 @@ class SapphireMainnetTests: XCTestCase { let signatures = data.sessionData.sessionTokenData.map({ $0!.token }) let parsedSignatures = signatures.map({ let data = Data(base64Encoded: $0.data(using: .utf8)!)! - let hexString = String(data: data, encoding: .utf8)! - let json = hexString.hexEncodedToString() + let json = String(data: data, encoding: .utf8)! return json }) let now = Int(Date().timeIntervalSince1970)