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

Wallet fix hex encoded request display and signing #1351

Merged
merged 3 commits into from
May 6, 2024
Merged
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
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
Loading