Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:WalletConnect/WalletConnectSwift…
Browse files Browse the repository at this point in the history
…V2 into #376-auth-request-service
  • Loading branch information
llbartekll committed Aug 2, 2022
2 parents 7a00520 + 21b6598 commit cc10b94
Show file tree
Hide file tree
Showing 21 changed files with 637 additions and 276 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/intake.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# This workflow moves issues to the Swift board
# when they receive the "accepted" label
# When WalletConnect Org members create issues they
# are automatically "accepted".
# Else they need to manually receive that label during intake.
name: intake

on:
issues:
types: [opened, labeled]
pull_request:
types: [opened, labeled]

jobs:
add-to-project:
name: Add issue to board
if: github.event.action == 'labeled' && github.event.label.name == 'accepted'
runs-on: ubuntu-latest
steps:
- uses: actions/[email protected]
with:
project-url: https://github.com/orgs/WalletConnect/projects/5
github-token: ${{ secrets.ASSIGN_TO_PROJECT_GITHUB_TOKEN }}
labeled: accepted
label-operator: OR
auto-promote:
name: auto-promote
if: github.event.action == 'opened'
runs-on: ubuntu-latest
steps:
- name: Check if organization member
id: is_organization_member
if: github.event.action == 'opened'
uses: JamesSingleton/[email protected]
with:
organization: WalletConnect
username: ${{ github.event_name != 'pull_request' && github.event.issue.user.login || github.event.sender.login }}
token: ${{ secrets.ASSIGN_TO_PROJECT_GITHUB_TOKEN }}
- name: Label issues
uses: andymckay/labeler@e6c4322d0397f3240f0e7e30a33b5c5df2d39e90
with:
add-labels: "accepted"
repo-token: ${{ secrets.ASSIGN_TO_PROJECT_GITHUB_TOKEN }}
6 changes: 3 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ let package = Package(
dependencies: ["WalletConnectUtils"]),
.target(
name: "WalletConnectUtils",
dependencies: ["Commons"]),
dependencies: ["Commons", "JSONRPC"]),
.target(
name: "JSONRPC",
dependencies: ["Commons"]),
Expand All @@ -71,11 +71,11 @@ let package = Package(
dependencies: ["WalletConnectKMS", "WalletConnectUtils", "TestingUtils"]),
.target(
name: "TestingUtils",
dependencies: ["WalletConnectUtils", "WalletConnectKMS"],
dependencies: ["WalletConnectUtils", "WalletConnectKMS", "JSONRPC"],
path: "Tests/TestingUtils"),
.testTarget(
name: "WalletConnectUtilsTests",
dependencies: ["WalletConnectUtils"]),
dependencies: ["WalletConnectUtils", "TestingUtils"]),
.testTarget(
name: "JSONRPCTests",
dependencies: ["JSONRPC", "TestingUtils"]),
Expand Down
12 changes: 12 additions & 0 deletions Sources/Commons/Either.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,15 @@ extension Either: Codable where L: Codable, R: Codable {
}
}
}

extension Either: CustomStringConvertible {

public var description: String {
switch self {
case let .left(left):
return "\(left)"
case let .right(right):
return "\(right)"
}
}
}
4 changes: 2 additions & 2 deletions Sources/JSONRPC/RPCRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ public struct RPCRequest: Equatable {

extension RPCRequest {

static func notification<C>(method: String, params: C) -> RPCRequest where C: Codable {
public static func notification<C>(method: String, params: C) -> RPCRequest where C: Codable {
return RPCRequest(method: method, params: AnyCodable(params), id: nil)
}

static func notification(method: String) -> RPCRequest {
public static func notification(method: String) -> RPCRequest {
return RPCRequest(method: method, params: nil, id: nil)
}

Expand Down
10 changes: 9 additions & 1 deletion Sources/JSONRPC/RPCResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,22 @@ public struct RPCResponse: Equatable {
return nil
}

private let outcome: Result<AnyCodable, JSONRPCError>
public let outcome: Result<AnyCodable, JSONRPCError>

internal init(id: RPCID?, outcome: Result<AnyCodable, JSONRPCError>) {
self.jsonrpc = "2.0"
self.id = id
self.outcome = outcome
}

public init<C>(matchingRequest: RPCRequest, result: C) where C: Codable {
self.init(id: matchingRequest.id, outcome: .success(AnyCodable(result)))
}

public init(matchingRequest: RPCRequest, error: JSONRPCError) {
self.init(id: matchingRequest.id, outcome: .failure(error))
}

public init<C>(id: Int, result: C) where C: Codable {
self.init(id: RPCID(id), outcome: .success(AnyCodable(result)))
}
Expand Down
65 changes: 65 additions & 0 deletions Sources/WalletConnectRelay/RPC/Methods.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
struct Subscribe: RelayRPC {

struct Params: Codable {
let topic: String
}

let params: Params

var method: String {
"subscribe"
}
}

struct Unsubscribe: RelayRPC {

struct Params: Codable {
let id: String
let topic: String
}

let params: Params

var method: String {
"unsubscribe"
}
}

struct Publish: RelayRPC {

struct Params: Codable {
let topic: String
let message: String
let ttl: Int
let prompt: Bool?
let tag: Int?
}

let params: Params

var method: String {
"publish"
}
}

struct Subscription: RelayRPC {

struct Params: Codable {
struct Contents: Codable {
let topic: String
let message: String
}
let id: String
let data: Contents
}

let params: Params

var method: String {
"subscription"
}

init(id: String, topic: String, message: String) {
self.params = Params(id: id, data: Params.Contents(topic: topic, message: message))
}
}
5 changes: 5 additions & 0 deletions Sources/WalletConnectRelay/RPC/RPCMethod.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
protocol RPCMethod {
associatedtype Parameters
var method: String { get }
var params: Parameters { get }
}
38 changes: 38 additions & 0 deletions Sources/WalletConnectRelay/RPC/RelayRPC.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import JSONRPC

protocol RelayRPC: RPCMethod {}

extension RelayRPC where Parameters: Codable {

var idGenerator: IdentifierGenerator {
return WalletConnectRPCID()
}

func wrapToIridium() -> PrefixDecorator<Self> {
return PrefixDecorator(rpcMethod: self, prefix: "iridium")
}

func wrapToIRN() -> PrefixDecorator<Self> {
return PrefixDecorator(rpcMethod: self, prefix: "irn")
}

func asRPCRequest() -> RPCRequest {
RPCRequest(method: self.method, params: self.params, idGenerator: self.idGenerator)
}
}

struct PrefixDecorator<T>: RelayRPC where T: RelayRPC {

typealias Parameters = T.Parameters

let rpcMethod: T
let prefix: String

var method: String {
"\(prefix)_\(rpcMethod.method)"
}

var params: Parameters {
rpcMethod.params
}
}
11 changes: 11 additions & 0 deletions Sources/WalletConnectRelay/RPC/WalletConnectRPCID.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Foundation
import JSONRPC

struct WalletConnectRPCID: IdentifierGenerator {

func next() -> RPCID {
let timestamp = Int64(Date().timeIntervalSince1970 * 1000) * 1000
let random = Int64.random(in: 0..<1000)
return .right(Int(timestamp + random))
}
}
Loading

0 comments on commit cc10b94

Please sign in to comment.