mirror of
https://github.com/lil-org/tokenary.git
synced 2024-12-15 23:13:28 +03:00
Update signing messages
This commit is contained in:
parent
82b02997dd
commit
8740b4a7ef
@ -19,19 +19,20 @@ struct Ethereum {
|
|||||||
|
|
||||||
static let shared = Ethereum()
|
static let shared = Ethereum()
|
||||||
|
|
||||||
func sign(message: String, wallet: InkWallet) throws -> String {
|
func sign(data: Data, wallet: InkWallet) throws -> String {
|
||||||
return try sign(message: message, wallet: wallet, addPrefix: false)
|
return try sign(data: data, wallet: wallet, addPrefix: false)
|
||||||
}
|
}
|
||||||
|
|
||||||
func signPersonal(message: String, wallet: InkWallet) throws -> String {
|
func signPersonalMessage(data: Data, wallet: InkWallet) throws -> String {
|
||||||
return try sign(message: message, wallet: wallet, addPrefix: true)
|
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 }
|
guard let ethereumPrivateKey = wallet.ethereumPrivateKey else { throw Error.keyNotFound }
|
||||||
let withPrefixIfNeeded = (addPrefix ? "\u{19}Ethereum Signed Message:\n" + String(message.count) : "") + message
|
let prefixString = addPrefix ? "\u{19}Ethereum Signed Message:\n" + String(data.count) : ""
|
||||||
guard let data = withPrefixIfNeeded.data(using: .utf8) else { throw Error.invalidInputData }
|
guard let prefixData = prefixString.data(using: .utf8) else { throw Error.failedToSign }
|
||||||
guard var signed = ethereumPrivateKey.sign(digest: Hash.keccak256(data: data), curve: CoinType.ethereum.curve) 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
|
signed[64] += 27
|
||||||
return signed.toPrefixedHexString()
|
return signed.toPrefixedHexString()
|
||||||
}
|
}
|
||||||
|
@ -150,10 +150,10 @@ class WalletConnect {
|
|||||||
let title: String
|
let title: String
|
||||||
switch payload {
|
switch payload {
|
||||||
case let .sign(data: data, raw: _):
|
case let .sign(data: data, raw: _):
|
||||||
message = String(data: data, encoding: .utf8)
|
message = String(data: data, encoding: .utf8) ?? data.hexString
|
||||||
title = "Sign Message"
|
title = "Sign Message"
|
||||||
case let .personalSign(data: data, raw: _):
|
case let .personalSign(data: data, raw: _):
|
||||||
message = String(data: data, encoding: .utf8)
|
message = String(data: data, encoding: .utf8) ?? data.hexString
|
||||||
title = "Sign Personal Message"
|
title = "Sign Personal Message"
|
||||||
case let .signTypeData(id: _, data: _, raw: raw):
|
case let .signTypeData(id: _, data: _, raw: raw):
|
||||||
title = "Sign Typed Data"
|
title = "Sign Typed Data"
|
||||||
@ -165,7 +165,7 @@ class WalletConnect {
|
|||||||
let peer = getPeerOfInteractor(interactor)
|
let peer = getPeerOfInteractor(interactor)
|
||||||
Agent.shared.showApprove(title: title, meta: message ?? "", peerMeta: peer) { [weak self, weak interactor] approved in
|
Agent.shared.showApprove(title: title, meta: message ?? "", peerMeta: peer) { [weak self, weak interactor] approved in
|
||||||
if approved {
|
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 {
|
} else {
|
||||||
self?.rejectRequest(id: id, interactor: interactor, message: "Cancelled")
|
self?.rejectRequest(id: id, interactor: interactor, message: "Cancelled")
|
||||||
}
|
}
|
||||||
@ -188,19 +188,20 @@ class WalletConnect {
|
|||||||
interactor?.approveRequest(id: requestId, result: hash).cauterize()
|
interactor?.approveRequest(id: requestId, result: hash).cauterize()
|
||||||
}
|
}
|
||||||
|
|
||||||
private func sign(id: Int64, message: String?, payload: WCEthereumSignPayload, walletId: String, interactor: WCInteractor?) {
|
private func sign(id: Int64, payload: WCEthereumSignPayload, walletId: String, interactor: WCInteractor?) {
|
||||||
guard let message = message, let wallet = walletsManager.getWallet(id: walletId) else {
|
guard let wallet = walletsManager.getWallet(id: walletId) else {
|
||||||
rejectRequest(id: id, interactor: interactor, message: "Something went wrong.")
|
rejectRequest(id: id, interactor: interactor, message: "Something went wrong.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var signed: String?
|
var signed: String?
|
||||||
switch payload {
|
switch payload {
|
||||||
case .personalSign:
|
case let .personalSign(data: data, raw: _):
|
||||||
signed = try? ethereum.signPersonal(message: message, wallet: wallet)
|
signed = try? ethereum.signPersonalMessage(data: data, wallet: wallet)
|
||||||
case .signTypeData:
|
case let .signTypeData(id: _, data: _, raw: raw):
|
||||||
signed = try? ethereum.sign(typedData: message, wallet: wallet)
|
let typedData = raw.count >= 2 ? raw[1] : ""
|
||||||
case .sign:
|
signed = try? ethereum.sign(typedData: typedData, wallet: wallet)
|
||||||
signed = try? ethereum.sign(message: message, wallet: wallet)
|
case let .sign(data: data, raw: _):
|
||||||
|
signed = try? ethereum.sign(data: data, wallet: wallet)
|
||||||
}
|
}
|
||||||
guard let result = signed else {
|
guard let result = signed else {
|
||||||
rejectRequest(id: id, interactor: interactor, message: "Something went wrong.")
|
rejectRequest(id: id, interactor: interactor, message: "Something went wrong.")
|
||||||
|
Loading…
Reference in New Issue
Block a user