Skip to content

Commit

Permalink
Merge pull request #463 from WalletConnect/#462-test-message-compromised
Browse files Browse the repository at this point in the history
[Auth] #462 test message compromised
  • Loading branch information
llbartekll authored Aug 24, 2022
2 parents 6aad276 + ecc8f8a commit 8cddac9
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 12 deletions.
67 changes: 67 additions & 0 deletions Tests/AuthTests/AppRespondSubscriberTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import Foundation
import XCTest
@testable import Auth
import WalletConnectUtils
@testable import WalletConnectKMS
@testable import TestingUtils
import JSONRPC

class AppRespondSubscriberTests: XCTestCase {
var networkingInteractor: NetworkingInteractorMock!
var sut: AppRespondSubscriber!
var messageFormatter: SIWEMessageFormatter!
var rpcHistory: RPCHistory!
let defaultTimeout: TimeInterval = 0.01
let walletAccount = Account(chainIdentifier: "eip155:1", address: "0x724d0D2DaD3fbB0C168f947B87Fa5DBe36F1A8bf")!
let prvKey = Data(hex: "462c1dad6832d7d96ccf87bd6a686a4110e114aaaebd5512e552c0e3a87b480f")
var messageSigner: MessageSigner!

override func setUp() {
networkingInteractor = NetworkingInteractorMock()
messageFormatter = SIWEMessageFormatter()
messageSigner = MessageSigner()
let historyStorage = CodableStore<RPCHistory.Record>(defaults: RuntimeKeyValueStorage(), identifier: StorageDomainIdentifiers.jsonRpcHistory.rawValue)
rpcHistory = RPCHistory(keyValueStore: historyStorage)
sut = AppRespondSubscriber(
networkingInteractor: networkingInteractor,
logger: ConsoleLoggerMock(),
rpcHistory: rpcHistory,
signatureVerifier: messageSigner,
messageFormatter: messageFormatter)
}

func testMessageCompromisedFailure() {
let messageExpectation = expectation(description: "receives response")

// set history record for a request
let topic = "topic"
let requestId: RPCID = RPCID(1234)
let request = RPCRequest(method: "wc_authRequest", params: AuthRequestParams.stub(), id: requestId.right!)
try! rpcHistory.set(request, forTopic: topic, emmitedBy: .local)

var messageId: RPCID!
var result: Result<Cacao, AuthError>!
sut.onResponse = { id, r in
messageId = id
result = r
messageExpectation.fulfill()
}

// subscribe on compromised cacao
let header = CacaoHeader(t: "eip4361")
let payload = CacaoPayload(params: AuthPayload.stub(nonce: "compromised nonce"), didpkh: DIDPKH(account: walletAccount))

let message = try! messageFormatter.formatMessage(from: payload)
let signature = try! messageSigner.sign(message: message, privateKey: prvKey)
let cacaoSignature = CacaoSignature(t: "eip191", s: signature)

let cacao = Cacao(header: header, payload: payload, signature: cacaoSignature)

let response = RPCResponse(id: requestId, result: cacao)
networkingInteractor.responsePublisherSubject.send(ResponseSubscriptionPayload(topic: topic, response: response))

wait(for: [messageExpectation], timeout: defaultTimeout)
XCTAssertEqual(result, .failure(AuthError.messageCompromised))
XCTAssertEqual(messageId, requestId)
}
}
14 changes: 14 additions & 0 deletions Tests/AuthTests/Mocks/AppMetadata.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

import Foundation
import WalletConnectPairing

public extension AppMetadata {
static func stub() -> AppMetadata {
AppMetadata(
name: "Wallet Connect",
description: "A protocol to connect blockchain wallets to dapps.",
url: "https://walletconnect.com/",
icons: []
)
}
}
2 changes: 1 addition & 1 deletion Tests/AuthTests/Mocks/NetworkingInteractorMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ struct NetworkingInteractorMock: NetworkInteracting {
var responsePublisher: AnyPublisher<ResponseSubscriptionPayload, Never> {
responsePublisherSubject.eraseToAnyPublisher()
}
private let responsePublisherSubject = PassthroughSubject<ResponseSubscriptionPayload, Never>()
let responsePublisherSubject = PassthroughSubject<ResponseSubscriptionPayload, Never>()

let requestPublisherSubject = PassthroughSubject<RequestSubscriptionPayload, Never>()
var requestPublisher: AnyPublisher<RequestSubscriptionPayload, Never> {
Expand Down
7 changes: 7 additions & 0 deletions Tests/AuthTests/Stubs/AuthPayload.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,10 @@ extension AuthPayload {
AuthPayload(requestParams: requestParams, iat: "2021-09-30T16:25:24Z")
}
}

extension AuthPayload {
static func stub(nonce: String) -> AuthPayload {
let issueAt = ISO8601DateFormatter().string(from: Date())
return AuthPayload(requestParams: RequestParams.stub(nonce: nonce), iat: issueAt)
}
}
16 changes: 16 additions & 0 deletions Tests/AuthTests/Stubs/AuthRequestParams.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@testable import Auth
import Foundation
import WalletConnectPairing

extension AuthRequestParams {
static func stub(nonce: String = "32891756") -> AuthRequestParams {
let payload = AuthPayload.stub(nonce: nonce)
return AuthRequestParams(requester: Requester.stub(), payloadParams: payload)
}
}

extension AuthRequestParams.Requester {
static func stub() -> AuthRequestParams.Requester {
AuthRequestParams.Requester(publicKey: "", metadata: AppMetadata.stub())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import WalletConnectUtils
@testable import TestingUtils
import JSONRPC

class AuthRequstSubscriberTests: XCTestCase {
class WalletRequestSubscriberTests: XCTestCase {
var networkingInteractor: NetworkingInteractorMock!
var sut: WalletRequestSubscriber!
var messageFormatter: SIWEMessageFormatterMock!
Expand Down
14 changes: 14 additions & 0 deletions Tests/WalletConnectSignTests/Mocks/AppMetadata.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

import Foundation
import WalletConnectPairing

public extension AppMetadata {
static func stub() -> AppMetadata {
AppMetadata(
name: "Wallet Connect",
description: "A protocol to connect blockchain wallets to dapps.",
url: "https://walletconnect.com/",
icons: []
)
}
}
10 changes: 0 additions & 10 deletions Tests/WalletConnectSignTests/Stub/Stubs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,6 @@ import WalletConnectUtils
import TestingUtils
import WalletConnectPairing

extension AppMetadata {
static func stub() -> AppMetadata {
AppMetadata(
name: "Wallet Connect",
description: "A protocol to connect blockchain wallets to dapps.",
url: "https://walletconnect.com/",
icons: []
)
}
}

extension Pairing {
static func stub(expiryDate: Date = Date(timeIntervalSinceNow: 10000), topic: String = String.generateTopic()) -> Pairing {
Expand Down

0 comments on commit 8cddac9

Please sign in to comment.