Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Siwe #1369

Merged
merged 4 commits into from
Jun 14, 2024
Merged

Siwe #1369

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/WalletConnectSign/Auth/Types/AuthPayload.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public struct AuthPayload: Codable, Equatable {
}


init(requestParams: AuthRequestParams, iat: String) {
public init(requestParams: AuthRequestParams, iat: String) {
self.type = "eip4361"
self.chains = requestParams.chains
self.domain = requestParams.domain
Expand Down
22 changes: 16 additions & 6 deletions Sources/WalletConnectSign/Sign/SignClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ public final class SignClient: SignClientProtocol {
private let linkSessionRequestSubscriber: LinkSessionRequestSubscriber
private let sessionResponderDispatcher: SessionResponderDispatcher
private let linkSessionRequestResponseSubscriber: LinkSessionRequestResponseSubscriber
private let messageVerifier: MessageVerifier

private var publishers = Set<AnyCancellable>()

Expand Down Expand Up @@ -231,7 +232,8 @@ public final class SignClient: SignClientProtocol {
linkSessionRequestSubscriber: LinkSessionRequestSubscriber,
sessionResponderDispatcher: SessionResponderDispatcher,
linkSessionRequestResponseSubscriber: LinkSessionRequestResponseSubscriber,
authenticateTransportTypeSwitcher: AuthenticateTransportTypeSwitcher
authenticateTransportTypeSwitcher: AuthenticateTransportTypeSwitcher,
messageVerifier: MessageVerifier
) {
self.logger = logger
self.networkingClient = networkingClient
Expand Down Expand Up @@ -267,6 +269,7 @@ public final class SignClient: SignClientProtocol {
self.sessionResponderDispatcher = sessionResponderDispatcher
self.linkSessionRequestResponseSubscriber = linkSessionRequestResponseSubscriber
self.authenticateTransportTypeSwitcher = authenticateTransportTypeSwitcher
self.messageVerifier = messageVerifier

setUpConnectionObserving()
setUpEnginesCallbacks()
Expand Down Expand Up @@ -374,11 +377,6 @@ public final class SignClient: SignClientProtocol {
return try pendingRequestsProvider.getPendingRequests()
}

public func formatAuthMessage(payload: AuthPayload, account: Account) throws -> String {
let cacaoPayload = try CacaoPayloadBuilder.makeCacaoPayload(authPayload: payload, account: account)
return try SIWEFromCacaoPayloadFormatter().formatMessage(from: cacaoPayload)
}

public func buildSignedAuthObject(authPayload: AuthPayload, signature: CacaoSignature, account: Account) throws -> AuthObject {
try CacaosBuilder.makeCacao(authPayload: authPayload, signature: signature, account: account)
}
Expand All @@ -387,6 +385,17 @@ public final class SignClient: SignClientProtocol {
try AuthPayloadBuilder.build(payload: payload, supportedEVMChains: supportedEVMChains, supportedMethods: supportedMethods)
}

// MARK: - SIWE

public func formatAuthMessage(payload: AuthPayload, account: Account) throws -> String {
let cacaoPayload = try CacaoPayloadBuilder.makeCacaoPayload(authPayload: payload, account: account)
return try SIWEFromCacaoPayloadFormatter().formatMessage(from: cacaoPayload)
}

public func verifySIWE(signature: String, message: String, address: String, chainId: String) async throws {
try await messageVerifier.verify(signature: signature, message: message, address: address, chainId: chainId)
}

//-----------------------------------------------------------------------------------

/// For a wallet to approve a session proposal.
Expand Down Expand Up @@ -590,3 +599,4 @@ public final class SignClient: SignClientProtocol {
}.store(in: &publishers)
}
}

3 changes: 2 additions & 1 deletion Sources/WalletConnectSign/Sign/SignClientFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ public struct SignClientFactory {
linkSessionRequestSubscriber: linkSessionRequestSubscriber,
sessionResponderDispatcher: sessionResponderDispatcher,
linkSessionRequestResponseSubscriber: linkSessionRequestResponseSubscriber,
authenticateTransportTypeSwitcher: authenticateTransportTypeSwitcher
authenticateTransportTypeSwitcher: authenticateTransportTypeSwitcher,
messageVerifier: signatureVerifier
)
return client
}
Expand Down
40 changes: 35 additions & 5 deletions Sources/WalletConnectSigner/Verifier/MessageVerifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public struct MessageVerifier {
}

public func verify(signature: CacaoSignature,
message: String,
account: Account
message: String,
account: Account
) async throws {
try await self.verify(
signature: signature,
Expand All @@ -27,9 +27,9 @@ public struct MessageVerifier {
}

public func verify(signature: CacaoSignature,
message: String,
address: String,
chainId: String
message: String,
address: String,
chainId: String
) async throws {

guard let messageData = message.data(using: .utf8) else {
Expand All @@ -54,4 +54,34 @@ public struct MessageVerifier {
)
}
}

public func verify(signature: String,
message: String,
address: String,
chainId: String
) async throws {

guard let messageData = message.data(using: .utf8) else {
throw Errors.utf8EncodingFailed
}
let signatureData = Data(hex: signature)

let prefixedMessage = messageData.prefixed

do {
try await eip191Verifier.verify(
signature: signatureData,
message: prefixedMessage,
address: address
)
} catch {
// If eip191 verification fails, try eip1271 verification
try await eip1271Verifier.verify(
signature: signatureData,
message: prefixedMessage,
address: address,
chainId: chainId
)
}
}
}
Loading