Skip to content

Commit

Permalink
Merge pull request #936 from WalletConnect/aprove-session-properties
Browse files Browse the repository at this point in the history
Aprove session properties
  • Loading branch information
Alexander Lisovik authored Jul 3, 2023
2 parents 57b56f9 + 40397a9 commit dfd5be0
Show file tree
Hide file tree
Showing 16 changed files with 86 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,41 @@ struct ConnectionDetailsView: View {
.padding(.top, 30)
}

if let sessionProperties = presenter.session.sessionProperties {
VStack(alignment: .leading) {
Text("sessionProperties")
.font(.system(size: 15, weight: .semibold, design: .rounded))
.foregroundColor(.whiteBackground)
.padding(.horizontal, 8)
.padding(.vertical, 5)
.background(Color.grey70)
.cornerRadius(28, corners: .allCorners)
.padding(.leading, 15)
.padding(.top, 9)

VStack(spacing: 0) {
TagsView(items: sessionProperties.map { "\($0): \($1)" }) {
Text($0)
.foregroundColor(.cyanBackround)
.font(.system(size: 13, weight: .semibold, design: .rounded))
.padding(.horizontal, 8)
.padding(.vertical, 3)
.background(Color.cyanBackround.opacity(0.2))
.cornerRadius(10, corners: .allCorners)
}
.padding(10)
}
.background(Color.whiteBackground)
.cornerRadius(20, corners: .allCorners)
.padding(.horizontal, 5)
.padding(.bottom, 5)
}
.background(Color("grey95"))
.cornerRadius(25, corners: .allCorners)
.padding(.horizontal, 20)
.padding(.top, 30)
}

Button {
presenter.onDelete()
} label: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ final class SessionProposalInteractor {
events: Array(supportedEvents),
accounts: supportedAccounts
)
try await Web3Wallet.instance.approve(proposalId: proposal.id, namespaces: sessionNamespaces)
try await Web3Wallet.instance.approve(proposalId: proposal.id, namespaces: sessionNamespaces, sessionProperties: proposal.sessionProperties)
} catch {
print(error)
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Auth/Services/Wallet/PendingRequestsProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class PendingRequestsProvider {
.filter {$0.request.method == "wc_authRequest"}
.compactMap {
guard let params = try? $0.request.params?.get(AuthRequestParams.self) else { return nil }
return AuthRequest(id: $0.request.id!, payload: params.payloadParams)
return AuthRequest(id: $0.request.id!, topic: $0.topic, payload: params.payloadParams)
}
return pendingRequests
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Auth/Services/Wallet/WalletRequestSubscriber.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class WalletRequestSubscriber {
peerMetadata: payload.request.requester.metadata
)

let request = AuthRequest(id: payload.id, payload: payload.request.payloadParams)
let request = AuthRequest(id: payload.id, topic: payload.topic, payload: payload.request.payloadParams)

guard let verifyClient else {
onRequest?((request, nil))
Expand Down
1 change: 1 addition & 0 deletions Sources/Auth/Types/Public/AuthRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ import Foundation

public struct AuthRequest: Equatable, Codable {
public let id: RPCID
public let topic: String
public let payload: AuthPayload
}
26 changes: 19 additions & 7 deletions Sources/WalletConnectSign/Engine/Common/ApproveEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ final class ApproveEngine {
setupResponseErrorSubscriptions()
}

func approveProposal(proposerPubKey: String, validating sessionNamespaces: [String: SessionNamespace]) async throws {

func approveProposal(proposerPubKey: String, validating sessionNamespaces: [String: SessionNamespace], sessionProperties: [String: String]? = nil) async throws {
guard let payload = try proposalPayloadsStore.get(key: proposerPubKey) else {
throw Errors.wrongRequestParams
}
Expand Down Expand Up @@ -87,9 +86,19 @@ final class ApproveEngine {
let result = SessionType.ProposeResponse(relay: relay, responderPublicKey: selfPublicKey.hexRepresentation)
let response = RPCResponse(id: payload.id, result: result)

async let proposeResponse: () = networkingInteractor.respond(topic: payload.topic, response: response, protocolMethod: SessionProposeProtocolMethod())
async let proposeResponse: () = networkingInteractor.respond(
topic: payload.topic,
response: response,
protocolMethod: SessionProposeProtocolMethod()
)

async let settleRequest: () = settle(topic: sessionTopic, proposal: proposal, namespaces: sessionNamespaces, pairingTopic: pairingTopic)
async let settleRequest: () = settle(
topic: sessionTopic,
proposal: proposal,
namespaces: sessionNamespaces,
sessionProperties: sessionProperties,
pairingTopic: pairingTopic
)

_ = try await [proposeResponse, settleRequest]

Expand All @@ -108,7 +117,7 @@ final class ApproveEngine {
// TODO: Delete pairing if inactive
}

func settle(topic: String, proposal: SessionProposal, namespaces: [String: SessionNamespace], pairingTopic: String) async throws {
func settle(topic: String, proposal: SessionProposal, namespaces: [String: SessionNamespace], sessionProperties: [String: String]? = nil, pairingTopic: String) async throws {
guard let agreementKeys = kms.getAgreementSecret(for: topic) else {
throw Errors.agreementMissingOrInvalid
}
Expand All @@ -129,7 +138,9 @@ final class ApproveEngine {
relay: relay,
controller: selfParticipant,
namespaces: namespaces,
expiry: Int64(expiry))
sessionProperties: sessionProperties,
expiry: Int64(expiry)
)

let session = WCSession(
topic: topic,
Expand All @@ -139,7 +150,8 @@ final class ApproveEngine {
peerParticipant: proposal.proposer,
settleParams: settleParams,
requiredNamespaces: proposal.requiredNamespaces,
acknowledged: false)
acknowledged: false
)

logger.debug("Sending session settle request")

Expand Down
2 changes: 2 additions & 0 deletions Sources/WalletConnectSign/Session.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ public struct Session {
public let topic: String
public let pairingTopic: String
public let peer: AppMetadata
public let requiredNamespaces: [String: ProposalNamespace]
public let namespaces: [String: SessionNamespace]
public let sessionProperties: [String: String]?
public let expiryDate: Date
public static var defaultTimeToLive: Int64 {
WCSession.defaultTimeToLive
Expand Down
4 changes: 2 additions & 2 deletions Sources/WalletConnectSign/Sign/SignClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,8 @@ public final class SignClient: SignClientProtocol {
/// - Parameters:
/// - proposalId: Session Proposal id
/// - namespaces: namespaces for given session, needs to contain at least required namespaces proposed by dApp.
public func approve(proposalId: String, namespaces: [String: SessionNamespace]) async throws {
try await approveEngine.approveProposal(proposerPubKey: proposalId, validating: namespaces)
public func approve(proposalId: String, namespaces: [String: SessionNamespace], sessionProperties: [String: String]? = nil) async throws {
try await approveEngine.approveProposal(proposerPubKey: proposalId, validating: namespaces, sessionProperties: sessionProperties)
}

/// For the wallet to reject a session proposal.
Expand Down
2 changes: 1 addition & 1 deletion Sources/WalletConnectSign/Sign/SignClientProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public protocol SignClientProtocol {

func connect(requiredNamespaces: [String: ProposalNamespace], optionalNamespaces: [String: ProposalNamespace]?, sessionProperties: [String: String]?, topic: String) async throws
func request(params: Request) async throws
func approve(proposalId: String, namespaces: [String: SessionNamespace]) async throws
func approve(proposalId: String, namespaces: [String: SessionNamespace], sessionProperties: [String: String]?) async throws
func reject(proposalId: String, reason: RejectionReason) async throws
func update(topic: String, namespaces: [String: SessionNamespace]) async throws
func extend(topic: String) async throws
Expand Down
1 change: 1 addition & 0 deletions Sources/WalletConnectSign/Types/Session/SessionType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ internal enum SessionType {
let relay: RelayProtocolOptions
let controller: Participant
let namespaces: [String: SessionNamespace]
let sessionProperties: [String: String]?
let expiry: Int64
}

Expand Down
13 changes: 11 additions & 2 deletions Sources/WalletConnectSign/Types/Session/WCSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ struct WCSession: SequenceObject, Equatable {
private(set) var timestamp: Date
private(set) var namespaces: [String: SessionNamespace]
private(set) var requiredNamespaces: [String: ProposalNamespace]
private(set) var sessionProperties: [String: String]?

static var defaultTimeToLive: Int64 {
Int64(7*Time.day)
Expand All @@ -43,6 +44,7 @@ struct WCSession: SequenceObject, Equatable {
self.selfParticipant = selfParticipant
self.peerParticipant = peerParticipant
self.namespaces = settleParams.namespaces
self.sessionProperties = settleParams.sessionProperties
self.requiredNamespaces = requiredNamespaces
self.acknowledged = acknowledged
self.expiryDate = Date(timeIntervalSince1970: TimeInterval(settleParams.expiry))
Expand All @@ -58,6 +60,7 @@ struct WCSession: SequenceObject, Equatable {
selfParticipant: Participant,
peerParticipant: Participant,
namespaces: [String: SessionNamespace],
sessionProperties: [String: String],
requiredNamespaces: [String: ProposalNamespace],
events: Set<String>,
accounts: Set<Account>,
Expand All @@ -72,6 +75,7 @@ struct WCSession: SequenceObject, Equatable {
self.selfParticipant = selfParticipant
self.peerParticipant = peerParticipant
self.namespaces = namespaces
self.sessionProperties = sessionProperties
self.requiredNamespaces = requiredNamespaces
self.acknowledged = acknowledged
self.expiryDate = Date(timeIntervalSince1970: TimeInterval(expiry))
Expand Down Expand Up @@ -165,8 +169,11 @@ struct WCSession: SequenceObject, Equatable {
topic: topic,
pairingTopic: pairingTopic,
peer: peerParticipant.metadata,
requiredNamespaces: requiredNamespaces,
namespaces: namespaces,
expiryDate: expiryDate)
sessionProperties: sessionProperties,
expiryDate: expiryDate
)
}
}

Expand All @@ -175,7 +182,7 @@ struct WCSession: SequenceObject, Equatable {
extension WCSession {

enum CodingKeys: String, CodingKey {
case topic, pairingTopic, relay, selfParticipant, peerParticipant, expiryDate, acknowledged, controller, namespaces, timestamp, requiredNamespaces
case topic, pairingTopic, relay, selfParticipant, peerParticipant, expiryDate, acknowledged, controller, namespaces, timestamp, requiredNamespaces, sessionProperties
}

init(from decoder: Decoder) throws {
Expand All @@ -186,6 +193,7 @@ extension WCSession {
self.selfParticipant = try container.decode(Participant.self, forKey: .selfParticipant)
self.peerParticipant = try container.decode(Participant.self, forKey: .peerParticipant)
self.namespaces = try container.decode([String: SessionNamespace].self, forKey: .namespaces)
self.sessionProperties = try container.decode([String: String]?.self, forKey: .sessionProperties)
self.acknowledged = try container.decode(Bool.self, forKey: .acknowledged)
self.expiryDate = try container.decode(Date.self, forKey: .expiryDate)
self.timestamp = try container.decode(Date.self, forKey: .timestamp)
Expand All @@ -202,6 +210,7 @@ extension WCSession {
try container.encode(selfParticipant, forKey: .selfParticipant)
try container.encode(peerParticipant, forKey: .peerParticipant)
try container.encode(namespaces, forKey: .namespaces)
try container.encode(sessionProperties, forKey: .sessionProperties)
try container.encode(acknowledged, forKey: .acknowledged)
try container.encode(expiryDate, forKey: .expiryDate)
try container.encode(timestamp, forKey: .timestamp)
Expand Down
4 changes: 2 additions & 2 deletions Sources/Web3Wallet/Web3WalletClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ public class Web3WalletClient {
/// - Parameters:
/// - proposalId: Session Proposal id
/// - namespaces: namespaces for given session, needs to contain at least required namespaces proposed by dApp.
public func approve(proposalId: String, namespaces: [String: SessionNamespace]) async throws {
try await signClient.approve(proposalId: proposalId, namespaces: namespaces)
public func approve(proposalId: String, namespaces: [String: SessionNamespace], sessionProperties: [String: String]? = nil) async throws {
try await signClient.approve(proposalId: proposalId, namespaces: namespaces, sessionProperties: sessionProperties)
}

/// For the wallet to reject a session proposal.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ final class ModalSheetInteractorMock: ModalSheetInteractor {
}

var sessionSettlePublisher: AnyPublisher<Session, Never> {
Result.Publisher(Session(topic: "", pairingTopic: "", peer: .stub(), namespaces: [:], expiryDate: Date()))
Result.Publisher(Session(topic: "", pairingTopic: "", peer: .stub(), requiredNamespaces: [:], namespaces: [:], sessionProperties: nil, expiryDate: Date()))
.eraseToAnyPublisher()
}

Expand Down
3 changes: 3 additions & 0 deletions Tests/WalletConnectSignTests/Stub/Session+Stub.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ extension WCSession {
expiryDate: Date = Date.distantFuture,
selfPrivateKey: AgreementPrivateKey = AgreementPrivateKey(),
namespaces: [String: SessionNamespace] = [:],
sessionProperties: [String: String] = [:],
requiredNamespaces: [String: ProposalNamespace] = [:],
acknowledged: Bool = true,
timestamp: Date = Date()
Expand All @@ -25,6 +26,7 @@ extension WCSession {
selfParticipant: Participant.stub(publicKey: selfKey),
peerParticipant: Participant.stub(publicKey: peerKey),
namespaces: namespaces,
sessionProperties: sessionProperties,
requiredNamespaces: requiredNamespaces,
events: [],
accounts: Account.stubSet(),
Expand All @@ -45,6 +47,7 @@ extension SessionType.SettleParams {
relay: RelayProtocolOptions.stub(),
controller: Participant.stub(),
namespaces: SessionNamespace.stubDictionary(),
sessionProperties: nil,
expiry: Int64(Date.distantFuture.timeIntervalSince1970))
}
}
1 change: 1 addition & 0 deletions Tests/Web3WalletTests/Mocks/AuthClientMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ final class AuthClientMock: AuthClientProtocol {

return AuthRequest(
id: .left(""),
topic: "",
payload: AuthPayload(requestParams: requestParams, iat: "")
)
}
Expand Down
9 changes: 4 additions & 5 deletions Tests/Web3WalletTests/Mocks/SignClientMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import Combine
@testable import WalletConnectSign

final class SignClientMock: SignClientProtocol {

var approveCalled = false
var rejectCalled = false
var updateCalled = false
Expand Down Expand Up @@ -41,7 +40,7 @@ final class SignClientMock: SignClientProtocol {
}

var sessionsPublisher: AnyPublisher<[WalletConnectSign.Session], Never> {
return Result.Publisher([WalletConnectSign.Session(topic: "", pairingTopic: "", peer: metadata, namespaces: [:], expiryDate: Date())])
return Result.Publisher([WalletConnectSign.Session(topic: "", pairingTopic: "", peer: metadata, requiredNamespaces: [:], namespaces: [:], sessionProperties: nil, expiryDate: Date())])
.eraseToAnyPublisher()
}

Expand All @@ -51,7 +50,7 @@ final class SignClientMock: SignClientProtocol {
}

var sessionSettlePublisher: AnyPublisher<WalletConnectSign.Session, Never> {
return Result.Publisher(Session(topic: "", pairingTopic: "", peer: metadata, namespaces: [:], expiryDate: Date()))
return Result.Publisher(Session(topic: "", pairingTopic: "", peer: metadata, requiredNamespaces: [:], namespaces: [:], sessionProperties: nil, expiryDate: Date()))
.eraseToAnyPublisher()
}

Expand Down Expand Up @@ -80,7 +79,7 @@ final class SignClientMock: SignClientProtocol {
.eraseToAnyPublisher()
}

func approve(proposalId: String, namespaces: [String : WalletConnectSign.SessionNamespace]) async throws {
func approve(proposalId: String, namespaces: [String : WalletConnectSign.SessionNamespace], sessionProperties: [String : String]? = nil) async throws {
approveCalled = true
}

Expand Down Expand Up @@ -113,7 +112,7 @@ final class SignClientMock: SignClientProtocol {
}

func getSessions() -> [WalletConnectSign.Session] {
return [WalletConnectSign.Session(topic: "", pairingTopic: "", peer: metadata, namespaces: [:], expiryDate: Date())]
return [WalletConnectSign.Session(topic: "", pairingTopic: "", peer: metadata, requiredNamespaces: [:], namespaces: [:], sessionProperties: nil, expiryDate: Date())]
}

func getPendingRequests(topic: String?) -> [WalletConnectSign.Request] {
Expand Down

0 comments on commit dfd5be0

Please sign in to comment.