-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[Auth] #462 test message compromised
- Loading branch information
Showing
8 changed files
with
120 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: [] | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: [] | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters