From f7b25f024af25982556de91bd75704621d3c0f4f Mon Sep 17 00:00:00 2001 From: metalurgical <97008724+metalurgical@users.noreply.github.com> Date: Fri, 19 Jul 2024 18:25:58 +0200 Subject: [PATCH] cleanup: remove dictionary from getMetadata() parameters --- Sources/TorusUtils/Helpers/MetadataUtils.swift | 17 ++++++++--------- Sources/TorusUtils/Helpers/NodeUtils.swift | 6 +++--- .../jsonRPC/Responses/GetMetadataResponse.swift | 9 +++++++++ .../Interfaces/MetaData/MetadataParams.swift | 10 ++++++++++ Sources/TorusUtils/TorusUtils.swift | 4 ++-- .../BaseTests/HexEncodedString.swift | 2 +- 6 files changed, 33 insertions(+), 15 deletions(-) create mode 100644 Sources/TorusUtils/Helpers/jsonRPC/Responses/GetMetadataResponse.swift diff --git a/Sources/TorusUtils/Helpers/MetadataUtils.swift b/Sources/TorusUtils/Helpers/MetadataUtils.swift index a6c7eed7..95a7e53a 100644 --- a/Sources/TorusUtils/Helpers/MetadataUtils.swift +++ b/Sources/TorusUtils/Helpers/MetadataUtils.swift @@ -65,17 +65,16 @@ internal class MetadataUtils { return .init(pub_key_X: X, pub_key_Y: Y, setData: setData, signature: Data(hex: sigData).base64EncodedString(), keyType: keyType) } - public static func getMetadata(legacyMetadataHost: String, dictionary: [String: String]) async throws -> BigUInt { - let encoded = try JSONSerialization.data(withJSONObject: dictionary, options: [.sortedKeys]) - + public static func getMetadata(legacyMetadataHost: String, params: GetMetadataParams) async throws -> BigUInt { + let encoder = JSONEncoder() + encoder.outputFormatting = .sortedKeys var request = try makeUrlRequest(url: "\(legacyMetadataHost)/get") - request.httpBody = encoded + request.httpBody = try encoder.encode(params) let urlSession = URLSession(configuration: .default) let val = try await urlSession.data(for: request) - let data = try JSONSerialization.jsonObject(with: val.0) as? [String: Any] ?? [:] - os_log("getMetadata: %@", log: getTorusLogger(log: TorusUtilsLogger.network, type: .info), type: .info, data) + let data: GetMetadataResponse = try JSONDecoder().decode(GetMetadataResponse.self, from: val.0) + let msg: String = data.message ?? "0" guard - let msg: String = data["message"] as? String, let ret = BigUInt(msg, radix: 16) else { throw TorusUtilError.decodingFailed("Message value not correct or nil in \(data)") @@ -99,14 +98,14 @@ internal class MetadataUtils { request.httpBody = data let urlSession = URLSession(configuration: .default) let val = try await urlSession.data(for: request) - + let decoded = try JSONDecoder().decode(GetOrSetNonceResult.self, from: val.0) return decoded } public static func getOrSetSapphireMetadataNonce(metadataHost: String, network: TorusNetwork, X: String, Y: String, serverTimeOffset: Int? = nil, privateKey: String? = nil, getOnly: Bool = false, keyType: TorusKeyType = .secp256k1) async throws -> GetOrSetNonceResult { if case .sapphire = network { - return try await getOrSetNonce(legacyMetadataHost: metadataHost, serverTimeOffset: serverTimeOffset ?? Int(trunc(Double((0) + Int(Date().timeIntervalSince1970)))), X: X, Y: Y, privateKey: privateKey, getOnly: getOnly, keyType: keyType) + return try await getOrSetNonce(legacyMetadataHost: metadataHost, serverTimeOffset: serverTimeOffset ?? Int(trunc(Double(0 + Int(Date().timeIntervalSince1970)))), X: X, Y: Y, privateKey: privateKey, getOnly: getOnly, keyType: keyType) } else { throw TorusUtilError.metadataNonceMissing } diff --git a/Sources/TorusUtils/Helpers/NodeUtils.swift b/Sources/TorusUtils/Helpers/NodeUtils.swift index ddec03b2..8a244869 100644 --- a/Sources/TorusUtils/Helpers/NodeUtils.swift +++ b/Sources/TorusUtils/Helpers/NodeUtils.swift @@ -194,7 +194,7 @@ internal class NodeUtils { } var collected = [CommitmentRequestResult]() for try await value in group { - if (value != nil && value?.error == nil) { + if value != nil && value?.error == nil { collected.append(value!.result!) received += 1 if !isImportShareReq { @@ -506,13 +506,13 @@ internal class NodeUtils { finalPubKey = try KeyUtils.combinePublicKeys(keys: [oAuthPublicKey, publicNonce]) } else { typeOfUser = .v1 - metadataNonce = BigInt(try await MetadataUtils.getMetadata(legacyMetadataHost: legacyMetadataHost, dictionary: ["pub_key_X": oAuthPublicKeyX, "pub_key_Y": oAuthPublicKeyY])) + metadataNonce = BigInt(try await MetadataUtils.getMetadata(legacyMetadataHost: legacyMetadataHost, params: GetMetadataParams(pub_key_X: oAuthPublicKeyX, pub_key_Y: oAuthPublicKeyY))) let privateKeyWithNonce = (BigInt(oAuthKey.addLeading0sForLength64(), radix: 16)! + BigInt(metadataNonce)).modulus(KeyUtils.getOrderOfCurve()) finalPubKey = try SecretKey(hex: privateKeyWithNonce.magnitude.serialize().hexString.addLeading0sForLength64()).toPublic().serialize(compressed: false) } } else { typeOfUser = .v1 - metadataNonce = BigInt(try await MetadataUtils.getMetadata(legacyMetadataHost: legacyMetadataHost, dictionary: ["pub_key_X": oAuthPublicKeyX, "pub_key_Y": oAuthPublicKeyY])) + metadataNonce = BigInt(try await MetadataUtils.getMetadata(legacyMetadataHost: legacyMetadataHost, params: GetMetadataParams(pub_key_X: oAuthPublicKeyX, pub_key_Y: oAuthPublicKeyY))) let privateKeyWithNonce = (BigInt(oAuthKey.addLeading0sForLength64(), radix: 16)! + BigInt(metadataNonce)).modulus(KeyUtils.getOrderOfCurve()) finalPubKey = try SecretKey(hex: privateKeyWithNonce.magnitude.serialize().hexString.addLeading0sForLength64()).toPublic().serialize(compressed: false) } diff --git a/Sources/TorusUtils/Helpers/jsonRPC/Responses/GetMetadataResponse.swift b/Sources/TorusUtils/Helpers/jsonRPC/Responses/GetMetadataResponse.swift new file mode 100644 index 00000000..83dae86e --- /dev/null +++ b/Sources/TorusUtils/Helpers/jsonRPC/Responses/GetMetadataResponse.swift @@ -0,0 +1,9 @@ +import Foundation + +internal struct GetMetadataResponse: Codable { + public var message: String? + + public init(message: String) { + self.message = message + } +} diff --git a/Sources/TorusUtils/Interfaces/MetaData/MetadataParams.swift b/Sources/TorusUtils/Interfaces/MetaData/MetadataParams.swift index 9919e6eb..06ff6440 100644 --- a/Sources/TorusUtils/Interfaces/MetaData/MetadataParams.swift +++ b/Sources/TorusUtils/Interfaces/MetaData/MetadataParams.swift @@ -1,5 +1,15 @@ import Foundation +internal struct GetMetadataParams: Codable { + public var pub_key_X: String + public var pub_key_Y: String + + public init(pub_key_X: String, pub_key_Y: String) { + self.pub_key_X = pub_key_X + self.pub_key_Y = pub_key_Y + } +} + internal struct MetadataParams: Codable { public struct SetData: Codable { public var data: String // "getNonce" || "getOrSetNonce" || String diff --git a/Sources/TorusUtils/TorusUtils.swift b/Sources/TorusUtils/TorusUtils.swift index 80cdbbd9..5e49225f 100644 --- a/Sources/TorusUtils/TorusUtils.swift +++ b/Sources/TorusUtils/TorusUtils.swift @@ -311,7 +311,7 @@ public class TorusUtils { if typeOfUser == .v1 { finalPubKey = oAuthPubKey - nonce = try await MetadataUtils.getMetadata(legacyMetadataHost: legacyMetadataHost, dictionary: ["pub_key_X": X, "pub_key_Y": Y]) + nonce = try await MetadataUtils.getMetadata(legacyMetadataHost: legacyMetadataHost, params: GetMetadataParams(pub_key_X: X, pub_key_Y: Y)) if nonce! > BigUInt(0) { let noncePrivateKey = try SecretKey(hex: nonce!.magnitude.serialize().hexString.addLeading0sForLength64()) @@ -328,7 +328,7 @@ public class TorusUtils { } else { typeOfUser = .v1 finalPubKey = oAuthPubKey - nonce = try await MetadataUtils.getMetadata(legacyMetadataHost: legacyMetadataHost, dictionary: ["pub_key_X": X, "pub_key_Y": Y]) + nonce = try await MetadataUtils.getMetadata(legacyMetadataHost: legacyMetadataHost, params: GetMetadataParams(pub_key_X: X, pub_key_Y: Y)) if nonce! > BigUInt(0) { let noncePrivateKey = try SecretKey(hex: nonce!.magnitude.serialize().hexString.addLeading0sForLength64()) diff --git a/Tests/TorusUtilsTests/BaseTests/HexEncodedString.swift b/Tests/TorusUtilsTests/BaseTests/HexEncodedString.swift index 513e9aaf..162a664c 100644 --- a/Tests/TorusUtilsTests/BaseTests/HexEncodedString.swift +++ b/Tests/TorusUtilsTests/BaseTests/HexEncodedString.swift @@ -9,7 +9,7 @@ class HexEndodedTest: XCTestCase { let extra_zero_padded = "06576656E".hexEncodedToString() let double_padded = "00006576656E".hexEncodedToString() let unpadded = "56E".hexEncodedToString() - + XCTAssertEqual(odd, "odd") // 6F 64 64 XCTAssertEqual(even, "even") // 65 76 65 6E XCTAssertEqual(extra_zero_padded, "even") // 00 65 76 65 6E