Skip to content

Commit

Permalink
Merge pull request #1351 from WalletConnect/wallet-fix-hex-encoded-re…
Browse files Browse the repository at this point in the history
…quest-display

Wallet fix hex encoded request display and signing
  • Loading branch information
llbartekll authored May 6, 2024
2 parents 4169e4b + 6e06989 commit 18a5f20
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
28 changes: 19 additions & 9 deletions Example/Shared/Signer/ETHSigner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import Commons
import Web3

struct ETHSigner {

private let importAccount: ImportAccount

init(importAccount: ImportAccount) {
Expand All @@ -21,8 +20,21 @@ struct ETHSigner {
func personalSign(_ params: AnyCodable) -> AnyCodable {
let params = try! params.get([String].self)
let messageToSign = params[0]
let dataToHash = dataToHash(messageToSign)
let (v, r, s) = try! privateKey.sign(message: .init(hex: dataToHash.toHexString()))

// Determine if the message is hex-encoded or plain text
let dataToSign: Bytes
if messageToSign.hasPrefix("0x") {
// Hex-encoded message, remove "0x" and convert
let messageData = Data(hex: String(messageToSign.dropFirst(2)))
dataToSign = dataToHash(messageData)
} else {
// Plain text message, convert directly to data
let messageData = Data(messageToSign.utf8)
dataToSign = dataToHash(messageData)
}

// Sign the data
let (v, r, s) = try! privateKey.sign(message: .init(Data(dataToSign)))
let result = "0x" + r.toHexString() + s.toHexString() + String(v + 27, radix: 16)
return AnyCodable(result)
}
Expand All @@ -45,12 +57,10 @@ struct ETHSigner {
return AnyCodable(result)
}

private func dataToHash(_ message: String) -> Bytes {
private func dataToHash(_ data: Data) -> Bytes {
let prefix = "\u{19}Ethereum Signed Message:\n"
let messageData = Data(hex: message)
let prefixData = (prefix + String(messageData.count)).data(using: .utf8)!
let prefixedMessageData = prefixData + messageData
let dataToHash: Bytes = .init(hex: prefixedMessageData.toHexString())
return dataToHash
let prefixData = (prefix + String(data.count)).data(using: .utf8)!
let prefixedMessageData = prefixData + data
return .init(hex: prefixedMessageData.toHexString())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,18 @@ final class SessionRequestPresenter: ObservableObject {
let validationStatus: VerifyContext.ValidationStatus?

var message: String {
let message = try? sessionRequest.params.get([String].self)
let decryptedMessage = message.map { String(data: Data(hex: $0.first ?? ""), encoding: .utf8) }
return (decryptedMessage ?? String(describing: sessionRequest.params.value)) ?? String(describing: sessionRequest.params.value)
guard let messages = try? sessionRequest.params.get([String].self),
let firstMessage = messages.first else {
return String(describing: sessionRequest.params.value)
}

// Attempt to decode the message if it's hex-encoded
let decodedMessage = String(data: Data(hex: firstMessage), encoding: .utf8)

// Return the decoded message if available, else return the original message
return decodedMessage?.isEmpty == false ? decodedMessage! : firstMessage
}


@Published var showError = false
@Published var errorMessage = "Error"
Expand Down

0 comments on commit 18a5f20

Please sign in to comment.