Fix parsing

pull/148/head
gmbnt 4 years ago
parent ede00b1a02
commit 4f56307d39

@ -178,8 +178,8 @@ internal enum OnionRequestAPI {
// MARK: Internal API // MARK: Internal API
/// Sends an onion request to `snode`. Builds new paths as needed. /// Sends an onion request to `snode`. Builds new paths as needed.
internal static func invoke(_ method: LokiAPITarget.Method, on snode: LokiAPITarget, with parameters: JSON) -> Promise<Data> { internal static func invoke(_ method: LokiAPITarget.Method, on snode: LokiAPITarget, with parameters: JSON) -> Promise<JSON> {
let (promise, seal) = Promise<Data>.pending() let (promise, seal) = Promise<JSON>.pending()
workQueue.async { workQueue.async {
let payload: JSON = [ "method" : method.rawValue, "params" : parameters ] let payload: JSON = [ "method" : method.rawValue, "params" : parameters ]
buildOnion(around: payload, targetedAt: snode).done(on: workQueue) { intermediate in buildOnion(around: payload, targetedAt: snode).done(on: workQueue) { intermediate in
@ -200,8 +200,11 @@ internal enum OnionRequestAPI {
do { do {
let gcm = GCM(iv: iv.bytes, tagLength: Int(gcmTagSize), mode: .combined) let gcm = GCM(iv: iv.bytes, tagLength: Int(gcmTagSize), mode: .combined)
let aes = try AES(key: targetSnodeSymmetricKey.bytes, blockMode: gcm, padding: .noPadding) let aes = try AES(key: targetSnodeSymmetricKey.bytes, blockMode: gcm, padding: .noPadding)
let result = try aes.decrypt(ciphertext.bytes) let data = Data(try aes.decrypt(ciphertext.bytes))
seal.fulfill(Data(bytes: result)) guard let json = try JSONSerialization.jsonObject(with: data, options: []) as? JSON,
let bodyAsString = json["body"] as? String, let bodyAsData = bodyAsString.data(using: .utf8),
let body = try JSONSerialization.jsonObject(with: bodyAsData, options: []) as? JSON else { return seal.reject(HTTP.Error.invalidJSON) }
seal.fulfill(body)
} catch (let error) { } catch (let error) {
seal.reject(error) seal.reject(error)
} }

@ -5,8 +5,6 @@ import XCTest
class OnionRequestAPITests : XCTestCase { class OnionRequestAPITests : XCTestCase {
/// Builds a path and then routes the same request through it several times. Logs the number of successes
/// versus the number of failures.
func testOnionRequestSending() { func testOnionRequestSending() {
let semaphore = DispatchSemaphore(value: 0) let semaphore = DispatchSemaphore(value: 0)
var totalSuccessRate: Double = 0 var totalSuccessRate: Double = 0
@ -20,9 +18,9 @@ class OnionRequestAPITests : XCTestCase {
let mockSessionID = "0582bc30f11e8a9736407adcaca03b049f4acd4af3ae7eb6b6608d30f0b1e6a20e" let mockSessionID = "0582bc30f11e8a9736407adcaca03b049f4acd4af3ae7eb6b6608d30f0b1e6a20e"
let parameters: JSON = [ "pubKey" : mockSessionID ] let parameters: JSON = [ "pubKey" : mockSessionID ]
let (promise, seal) = Promise<Void>.pending() let (promise, seal) = Promise<Void>.pending()
OnionRequestAPI.invoke(.getSwarm, on: snode, with: parameters).done(on: OnionRequestAPI.workQueue) { data in OnionRequestAPI.invoke(.getSwarm, on: snode, with: parameters).done(on: OnionRequestAPI.workQueue) { json in
successCount += 1 successCount += 1
print("[Loki] [Onion Request API] Onion request succeeded with result: \(String(data: data, encoding: .utf8)).") print("[Loki] [Onion Request API] Onion request succeeded with result: \(json.prettifiedDescription).")
seal.fulfill(()) seal.fulfill(())
}.catch(on: OnionRequestAPI.workQueue) { error in }.catch(on: OnionRequestAPI.workQueue) { error in
if case GCM.Error.fail = error { if case GCM.Error.fail = error {

Loading…
Cancel
Save