Skip to content

Commit

Permalink
Merge pull request #34 from brunobar79/fix-payload-ids
Browse files Browse the repository at this point in the history
Fix payload ids
  • Loading branch information
Dmitry Bespalov authored Nov 4, 2020
2 parents aae79d5 + 6368166 commit f961eda
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Sources/PublicInterface/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
31 changes: 27 additions & 4 deletions Sources/PublicInterface/Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,44 @@ 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<T: Encodable>(url: WCURL, method: Method, params: [T], id: RequestID? = UUID().uuidString) throws {
public convenience init<T: Encodable>(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)
let payload = JSONRPC_2_0.Request(method: method, params: parameters, id: JSONRPC_2_0.IDType(id))
self.init(payload: payload, url: url)
}

public convenience init<T: Encodable>(url: WCURL, method: Method, namedParams params: T, id: RequestID? = UUID().uuidString) throws {
public convenience init<T: Encodable>(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)
Expand Down

0 comments on commit f961eda

Please sign in to comment.