From 1e11f9ea48697cf9cc58dafa402c32561035110f Mon Sep 17 00:00:00 2001 From: Bruno Barbieri Date: Wed, 4 Nov 2020 11:59:35 -0500 Subject: [PATCH 1/2] Generate same payloadId as JS SDK --- Sources/PublicInterface/Request.swift | 31 +++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/Sources/PublicInterface/Request.swift b/Sources/PublicInterface/Request.swift index 5cad5072c..2fd4a383a 100644 --- a/Sources/PublicInterface/Request.swift +++ b/Sources/PublicInterface/Request.swift @@ -30,13 +30,36 @@ public class Request { self.payload = payload self.url = url } - - public convenience init(url: WCURL, method: Method, id: RequestID? = UUID().uuidString) { + + /// Generates new ID for a request that is compatible with JS clients. + /// - Returns: new ID + public static func payloadId() -> RequestID { + // Port of the payloadId() from the JS library to make sure that + // generated IDs fit into JS's number size (max integer size is + // 2^53 - 1). + // + // NOTE: new Date().getTime() is time in milliseconds in JS, however, + // in iOS it is time in seconds. So instead of 1000, we multiply by 1 million + // + // NOTE: the id may still be greater than JS max number, but + // this would happen if the date is in year 2255 + // + // JS code: + // const datePart: number = new Date().getTime() * Math.pow(10, 3) + // const extraPart: number = Math.floor(Math.random() * Math.pow(10, 3)) + // const id: number = datePart + extraPart + let datePart = Int(Date().timeIntervalSince1970 * 1000_000) + let extraPart = Int.random(in: 0..<1000) + let id = datePart + extraPart + return id + } + + public convenience init(url: WCURL, method: Method, id: RequestID? = payloadId()) { let payload = JSONRPC_2_0.Request(method: method, params: nil, id: JSONRPC_2_0.IDType(id)) self.init(payload: payload, url: url) } - public convenience init(url: WCURL, method: Method, params: [T], id: RequestID? = UUID().uuidString) throws { + public convenience init(url: WCURL, method: Method, params: [T], id: RequestID? = payloadId()) throws { let data = try JSONEncoder.encoder().encode(params) let values = try JSONDecoder().decode([JSONRPC_2_0.ValueType].self, from: data) let parameters = JSONRPC_2_0.Request.Params.positional(values) @@ -44,7 +67,7 @@ public class Request { self.init(payload: payload, url: url) } - public convenience init(url: WCURL, method: Method, namedParams params: T, id: RequestID? = UUID().uuidString) throws { + public convenience init(url: WCURL, method: Method, namedParams params: T, id: RequestID? = payloadId()) throws { let data = try JSONEncoder.encoder().encode(params) let values = try JSONDecoder().decode([String: JSONRPC_2_0.ValueType].self, from: data) let parameters = JSONRPC_2_0.Request.Params.named(values) From 63681669fbe18f3d0ab19dacd32cbd3f7a53bea6 Mon Sep 17 00:00:00 2001 From: Bruno Barbieri Date: Wed, 4 Nov 2020 12:00:06 -0500 Subject: [PATCH 2/2] use Request.payloadId to generate ids --- Sources/PublicInterface/Client.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/PublicInterface/Client.swift b/Sources/PublicInterface/Client.swift index 7bfed1fa2..ff0b9e1d4 100644 --- a/Sources/PublicInterface/Client.swift +++ b/Sources/PublicInterface/Client.swift @@ -184,7 +184,7 @@ public class Client: WalletConnect { delegate.client(self, didConnect: existingSession) } else { // establishing new connection, handshake in process communicator.subscribe(on: dAppInfo.peerId, url: url) - let request = try! Request(url: url, method: "wc_sessionRequest", params: [dAppInfo], id: UUID().hashValue) + let request = try! Request(url: url, method: "wc_sessionRequest", params: [dAppInfo], id: Request.payloadId()) let requestID = request.internalID! responses.add(requestID: requestID) { [unowned self] response in self.handleHandshakeResponse(response)