diff --git a/Encrypted Ink/Ethereum/Ethereum.swift b/Encrypted Ink/Ethereum/Ethereum.swift index b50e7c4a..676d8717 100644 --- a/Encrypted Ink/Ethereum/Ethereum.swift +++ b/Encrypted Ink/Ethereum/Ethereum.swift @@ -19,19 +19,20 @@ struct Ethereum { static let shared = Ethereum() - func sign(message: String, wallet: InkWallet) throws -> String { - return try sign(message: message, wallet: wallet, addPrefix: false) + func sign(data: Data, wallet: InkWallet) throws -> String { + return try sign(data: data, wallet: wallet, addPrefix: false) } - func signPersonal(message: String, wallet: InkWallet) throws -> String { - return try sign(message: message, wallet: wallet, addPrefix: true) + func signPersonalMessage(data: Data, wallet: InkWallet) throws -> String { + return try sign(data: data, wallet: wallet, addPrefix: true) } - private func sign(message: String, wallet: InkWallet, addPrefix: Bool) throws -> String { + private func sign(data: Data, wallet: InkWallet, addPrefix: Bool) throws -> String { guard let ethereumPrivateKey = wallet.ethereumPrivateKey else { throw Error.keyNotFound } - let withPrefixIfNeeded = (addPrefix ? "\u{19}Ethereum Signed Message:\n" + String(message.count) : "") + message - guard let data = withPrefixIfNeeded.data(using: .utf8) else { throw Error.invalidInputData } - guard var signed = ethereumPrivateKey.sign(digest: Hash.keccak256(data: data), curve: CoinType.ethereum.curve) else { throw Error.failedToSign } + let prefixString = addPrefix ? "\u{19}Ethereum Signed Message:\n" + String(data.count) : "" + guard let prefixData = prefixString.data(using: .utf8) else { throw Error.failedToSign } + let digest = Hash.keccak256(data: prefixData + data) + guard var signed = ethereumPrivateKey.sign(digest: digest, curve: CoinType.ethereum.curve) else { throw Error.failedToSign } signed[64] += 27 return signed.toPrefixedHexString() } diff --git a/Encrypted Ink/WalletConnect.swift b/Encrypted Ink/WalletConnect.swift index 788d2951..7e3cb9a7 100644 --- a/Encrypted Ink/WalletConnect.swift +++ b/Encrypted Ink/WalletConnect.swift @@ -150,10 +150,10 @@ class WalletConnect { let title: String switch payload { case let .sign(data: data, raw: _): - message = String(data: data, encoding: .utf8) + message = String(data: data, encoding: .utf8) ?? data.hexString title = "Sign Message" case let .personalSign(data: data, raw: _): - message = String(data: data, encoding: .utf8) + message = String(data: data, encoding: .utf8) ?? data.hexString title = "Sign Personal Message" case let .signTypeData(id: _, data: _, raw: raw): title = "Sign Typed Data" @@ -165,7 +165,7 @@ class WalletConnect { let peer = getPeerOfInteractor(interactor) Agent.shared.showApprove(title: title, meta: message ?? "", peerMeta: peer) { [weak self, weak interactor] approved in if approved { - self?.sign(id: id, message: message, payload: payload, walletId: walletId, interactor: interactor) + self?.sign(id: id, payload: payload, walletId: walletId, interactor: interactor) } else { self?.rejectRequest(id: id, interactor: interactor, message: "Cancelled") } @@ -188,19 +188,20 @@ class WalletConnect { interactor?.approveRequest(id: requestId, result: hash).cauterize() } - private func sign(id: Int64, message: String?, payload: WCEthereumSignPayload, walletId: String, interactor: WCInteractor?) { - guard let message = message, let wallet = walletsManager.getWallet(id: walletId) else { + private func sign(id: Int64, payload: WCEthereumSignPayload, walletId: String, interactor: WCInteractor?) { + guard let wallet = walletsManager.getWallet(id: walletId) else { rejectRequest(id: id, interactor: interactor, message: "Something went wrong.") return } var signed: String? switch payload { - case .personalSign: - signed = try? ethereum.signPersonal(message: message, wallet: wallet) - case .signTypeData: - signed = try? ethereum.sign(typedData: message, wallet: wallet) - case .sign: - signed = try? ethereum.sign(message: message, wallet: wallet) + case let .personalSign(data: data, raw: _): + signed = try? ethereum.signPersonalMessage(data: data, wallet: wallet) + case let .signTypeData(id: _, data: _, raw: raw): + let typedData = raw.count >= 2 ? raw[1] : "" + signed = try? ethereum.sign(typedData: typedData, wallet: wallet) + case let .sign(data: data, raw: _): + signed = try? ethereum.sign(data: data, wallet: wallet) } guard let result = signed else { rejectRequest(id: id, interactor: interactor, message: "Something went wrong.")