Refactor sending transaction

This commit is contained in:
Ivan Grachyov 2021-06-20 16:17:11 +03:00
parent 405869fb9d
commit d5dd9887cb
5 changed files with 42 additions and 34 deletions

View File

@ -39,6 +39,8 @@
2C8A09EE2675965F00993638 /* WaitingViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C8A09ED2675965F00993638 /* WaitingViewController.swift */; };
2C917429267D2A6E00049075 /* Keychain.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C917428267D2A6E00049075 /* Keychain.swift */; };
2CDAB3722675B3F0009F8B97 /* PasswordViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2CDAB3712675B3F0009F8B97 /* PasswordViewController.swift */; };
2CE3D012267F73C00032A62E /* Transaction.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2CE3D011267F73C00032A62E /* Transaction.swift */; };
2CE3D015267F73E80032A62E /* Account.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2CE3D014267F73E80032A62E /* Account.swift */; };
88745B0F4DEE1F60AD0F02C3 /* Pods_Encrypted_Ink.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3704D7F64179CCDCE2E8C783 /* Pods_Encrypted_Ink.framework */; };
/* End PBXBuildFile section */
@ -80,6 +82,8 @@
2C8A09ED2675965F00993638 /* WaitingViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WaitingViewController.swift; sourceTree = "<group>"; };
2C917428267D2A6E00049075 /* Keychain.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Keychain.swift; sourceTree = "<group>"; };
2CDAB3712675B3F0009F8B97 /* PasswordViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PasswordViewController.swift; sourceTree = "<group>"; };
2CE3D011267F73C00032A62E /* Transaction.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Transaction.swift; sourceTree = "<group>"; };
2CE3D014267F73E80032A62E /* Account.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Account.swift; sourceTree = "<group>"; };
3704D7F64179CCDCE2E8C783 /* Pods_Encrypted_Ink.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Encrypted_Ink.framework; sourceTree = BUILT_PRODUCTS_DIR; };
/* End PBXFileReference section */
@ -181,6 +185,8 @@
isa = PBXGroup;
children = (
2C1995552674D0F300A8E370 /* Ethereum.swift */,
2CE3D014267F73E80032A62E /* Account.swift */,
2CE3D011267F73C00032A62E /* Transaction.swift */,
0DB729012674E2DB0011F7A1 /* EIP712 */,
);
path = Ethereum;
@ -383,6 +389,8 @@
2CDAB3722675B3F0009F8B97 /* PasswordViewController.swift in Sources */,
2C1995402674C4B900A8E370 /* AppDelegate.swift in Sources */,
0DB729142674E2DB0011F7A1 /* EIP712Parameter.swift in Sources */,
2CE3D015267F73E80032A62E /* Account.swift in Sources */,
2CE3D012267F73C00032A62E /* Transaction.swift in Sources */,
0DB7291B2674E2DB0011F7A1 /* EIP712Hash.swift in Sources */,
0DB729172674E2DB0011F7A1 /* EIP712StructType.swift in Sources */,
0DB7291F2674E2DB0011F7A1 /* EIP712Representable.swift in Sources */,

View File

@ -0,0 +1,8 @@
// Copyright © 2021 Encrypted Ink. All rights reserved.
import Foundation
struct Account: Codable {
let privateKey: String
let address: String
}

View File

@ -4,20 +4,6 @@ import Foundation
import Web3Swift
import CryptoSwift
struct Transaction {
let transactionsCount: String?
let gasPrice: String?
let gasEstimate: String?
let recipientAddress: String
let weiAmount: String
let contractCall: String
}
struct Account: Codable {
let privateKey: String
let address: String
}
struct Ethereum {
enum Errors: Error {
@ -63,14 +49,6 @@ struct Ethereum {
return try signer.signatureData(hash: hash).toPrefixedHexString()
}
static func sign(transaction: Transaction, account: Account) throws -> String {
guard transaction.transactionsCount != nil, transaction.gasPrice != nil, transaction.gasEstimate != nil else {
throw Errors.invalidInputData
}
let bytes = signedTransactionBytes(transaction: transaction, account: account)
return try bytes.value().toPrefixedHexString()
}
static func send(transaction: Transaction, account: Account) throws -> String {
let bytes = signedTransactionBytes(transaction: transaction, account: account)
let response = try SendRawTransactionProcedure(network: network, transactionBytes: bytes).call()
@ -82,14 +60,19 @@ struct Ethereum {
private static func signedTransactionBytes(transaction: Transaction, account: Account) -> EthContractCallBytes {
let senderKey = EthPrivateKey(hex: account.privateKey)
let contractAddress = EthAddress(hex: transaction.recipientAddress)
let weiAmount = EthNumber(hex: transaction.weiAmount)
let functionCall = BytesFromHexString(hex: transaction.contractCall)
let contractAddress = EthAddress(hex: transaction.to)
let weiAmount: EthNumber
if let value = transaction.value {
weiAmount = EthNumber(hex: value)
} else {
weiAmount = EthNumber(value: 0)
}
let functionCall = BytesFromHexString(hex: transaction.data)
let bytes: EthContractCallBytes
if let gasPriceString = transaction.gasPrice {
let gasPrice = EthNumber(hex: gasPriceString)
if let gasEstimateString = transaction.gasEstimate, let transctionCountString = transaction.transactionsCount {
if let gasEstimateString = transaction.gas,
let transctionCountString = transaction.nonce {
let gasEstimate = EthNumber(hex: gasEstimateString)
let transactionCount = EthNumber(hex: transctionCountString)

View File

@ -0,0 +1,13 @@
// Copyright © 2021 Encrypted Ink. All rights reserved.
import Foundation
struct Transaction {
let from: String
let to: String
let nonce: String?
let gasPrice: String?
let gas: String?
let value: String?
let data: String
}

View File

@ -93,16 +93,12 @@ class WalletConnect {
}
func sendTransaction(id: Int64, wct: WCEthereumTransaction, address: String, interactor: WCInteractor?) {
guard let account = AccountsService.getAccountForAddress(address) else {
guard let account = AccountsService.getAccountForAddress(address), let to = wct.to else {
// TODO: display error message
rejectRequest(id: id, interactor: interactor, message: "Failed for some reason")
return
}
let transaction = Transaction(transactionsCount: wct.nonce,
gasPrice: wct.gasPrice,
gasEstimate: wct.gasLimit,
recipientAddress: wct.to ?? "",
weiAmount: wct.value ?? "",
contractCall: wct.data)
let transaction = Transaction(from: wct.from, to: to, nonce: wct.nonce, gasPrice: wct.gasPrice, gas: wct.gas, value: wct.value, data: wct.data)
guard let hash = try? Ethereum.send(transaction: transaction, account: account) else {
rejectRequest(id: id, interactor: interactor, message: "Failed to send")
return