fix: code review

This commit is contained in:
Richard Ramos 2021-06-30 08:32:14 -04:00 committed by Iuri Matias
parent 5f7f899c3b
commit 065bd26786
5 changed files with 38 additions and 43 deletions

View File

@ -1,9 +1,8 @@
import algorithm, atomics, sequtils, strformat, strutils, sugar, sequtils, json, parseUtils, std/wrapnils, tables import algorithm, atomics, sequtils, strformat, strutils, sugar, sequtils, json, parseUtils, std/wrapnils, tables
import NimQml, json, sequtils, chronicles, strutils, strformat, json, stint import NimQml, json, sequtils, chronicles, strutils, strformat, json, stint
import web3/ethhexstrings
import import
../../../status/[status, settings, wallet, tokens], ../../../status/[status, settings, wallet, tokens, utils],
../../../status/wallet as status_wallet, ../../../status/wallet as status_wallet,
../../../status/tasks/[qt, task_runner_impl] ../../../status/tasks/[qt, task_runner_impl]
@ -109,17 +108,10 @@ QtObject:
proc sendTransaction*(self: TransactionsView, from_addr: string, to: string, assetAddress: string, value: string, gas: string, gasPrice: string, password: string, uuid: string) {.slot.} = proc sendTransaction*(self: TransactionsView, from_addr: string, to: string, assetAddress: string, value: string, gas: string, gasPrice: string, password: string, uuid: string) {.slot.} =
self.sendTransaction("transactionSent", from_addr, to, assetAddress, value, gas, gasPrice, password, uuid) self.sendTransaction("transactionSent", from_addr, to, assetAddress, value, gas, gasPrice, password, uuid)
proc transferEth*(self: TransactionsView, from_addr: string, to_addr: string, value: string, gas: string, gasPrice: string, password: string, uuid: string): bool {.slot.} = proc transferEth*(self: TransactionsView, from_addr: string, to_addr: string, value: string, gas: string, gasPrice: string, password: string, uuid: string): bool {.slot.} =
try: try:
if not validate(HexDataStr(from_addr)): return false validateTransactionInput(from_addr, to_addr, "", value, gas, gasPrice, "", uuid)
if from_addr.len != 42: return false
if not validate(HexDataStr(to_addr)): return false
if to_addr.len != 42: return false
if parseFloat(value) < 0: return false
if parseInt(gas) < 0: return false
if parseFloat(gasPrice) <= 0: return false
if uuid.isEmptyOrWhitespace(): return false
self.sendTransaction("transactionSent", from_addr, to_addr, ZERO_ADDRESS, value, gas, gasPrice, password, uuid) self.sendTransaction("transactionSent", from_addr, to_addr, ZERO_ADDRESS, value, gas, gasPrice, password, uuid)
except Exception as e: except Exception as e:
error "Error sending eth transfer transaction", msg = e.msg error "Error sending eth transfer transaction", msg = e.msg
@ -128,16 +120,7 @@ QtObject:
proc transferTokens*(self: TransactionsView, from_addr: string, to_addr: string, assetAddress: string, value: string, gas: string, gasPrice: string, password: string, uuid: string): bool {.slot.} = proc transferTokens*(self: TransactionsView, from_addr: string, to_addr: string, assetAddress: string, value: string, gas: string, gasPrice: string, password: string, uuid: string): bool {.slot.} =
try: try:
if not validate(HexDataStr(from_addr)): return false validateTransactionInput(from_addr, to_addr, assetAddress, value, gas, gasPrice, "", uuid)
if from_addr.len != 42: return false
if not validate(HexDataStr(to_addr)): return false
if to_addr.len != 42: return false
if not validate(HexDataStr(assetAddress)): return false
if assetAddress.len != 42: return false
if parseFloat(value) <= 0: return false
if parseInt(gas) <= 0: return false
if parseFloat(gasPrice) <= 0: return false
if uuid.isEmptyOrWhitespace(): return false
self.sendTransaction("transactionSent", from_addr, to_addr, assetAddress, value, gas, gasPrice, password, uuid) self.sendTransaction("transactionSent", from_addr, to_addr, assetAddress, value, gas, gasPrice, password, uuid)
except Exception as e: except Exception as e:
error "Error sending token transfer transaction", msg = e.msg error "Error sending token transfer transaction", msg = e.msg

View File

@ -9,7 +9,6 @@ import chronicles
import nbaser import nbaser
import stew/byteutils import stew/byteutils
from base32 import nil from base32 import nil
import web3/ethhexstrings
const HTTPS_SCHEME = "https" const HTTPS_SCHEME = "https"
const IPFS_GATEWAY = ".infura.status.im" const IPFS_GATEWAY = ".infura.status.im"
@ -92,19 +91,6 @@ proc toAPIRequest(message: string): APIRequest =
hostname: data{"hostname"}.getStr() hostname: data{"hostname"}.getStr()
) )
proc validateInput(from_addr, to_addr, value, gas, gasPrice, data: string): bool =
if not validate(HexDataStr(from_addr)): return false
if from_addr.len != 42: return false
if to_addr != "":
if not validate(HexDataStr(to_addr)): return false
if to_addr.len != 42: return false
if parseFloat(value) < 0: return false
if parseInt(gas) <= 0: return false
if parseFloat(gasPrice) <= 0: return false
if data != "":
if not validate(HexDataStr(data)): return false
return true
proc process(self: ProviderModel, data: Web3SendAsyncReadOnly): string = proc process(self: ProviderModel, data: Web3SendAsyncReadOnly): string =
if AUTH_METHODS.contains(data.payload.rpcMethod) and not self.permissions.hasPermission(data.hostname, Permission.Web3): if AUTH_METHODS.contains(data.payload.rpcMethod) and not self.permissions.hasPermission(data.hostname, Permission.Web3):
return $ %* { return $ %* {
@ -132,11 +118,17 @@ proc process(self: ProviderModel, data: Web3SendAsyncReadOnly): string =
else: else:
"" ""
let validInput = validateInput(fromAddress, to, value, selectedGasLimit, selectedGasPrice, txData)
var success: bool var success: bool
var errorMessage = "" var errorMessage = ""
var response = "" var response = ""
var validInput: bool = true
try:
validateTransactionInput(fromAddress, to, "", value, selectedGasLimit, selectedGasPrice, txData, "dummy")
except Exception as e:
validInput = false
success = false
errorMessage = e.msg
if validInput: if validInput:
# TODO make this async # TODO make this async
@ -148,10 +140,6 @@ proc process(self: ProviderModel, data: Web3SendAsyncReadOnly): string =
response response
else: else:
"" ""
else:
success = false
errorMessage = "Invalid input"
return $ %* { return $ %* {
"type": ResponseTypes.Web3SendAsyncCallback, "type": ResponseTypes.Web3SendAsyncCallback,

View File

@ -4,6 +4,7 @@ import stint
from times import getTime, toUnix, nanosecond from times import getTime, toUnix, nanosecond
import libstatus/accounts/signing_phrases import libstatus/accounts/signing_phrases
from web3 import Address, fromHex from web3 import Address, fromHex
import web3/ethhexstrings
proc getTimelineChatId*(pubKey: string = ""): string = proc getTimelineChatId*(pubKey: string = ""): string =
if pubKey == "": if pubKey == "":
@ -131,6 +132,28 @@ proc find*[T](s: seq[T], pred: proc(x: T): bool {.closure.}, found: var bool): T
proc parseAddress*(strAddress: string): Address = proc parseAddress*(strAddress: string): Address =
fromHex(Address, strAddress) fromHex(Address, strAddress)
proc isAddress*(strAddress: string): bool =
try:
discard parseAddress(strAddress)
except:
return false
return true
proc validateTransactionInput*(from_addr, to_addr, assetAddress, value, gas, gasPrice, data, uuid: string) =
if not isAddress(from_addr): raise newException(ValueError, "from_addr is not a valid ETH address")
if not isAddress(to_addr): raise newException(ValueError, "to_addr is not a valid ETH address")
if parseFloat(value) < 0: raise newException(ValueError, "value should be a number >= 0")
if parseInt(gas) <= 0: raise newException(ValueError, "gas should be a number > 0")
if parseFloat(gasPrice) <= 0: raise newException(ValueError, "gasPrice should be a number > 0")
if uuid.isEmptyOrWhitespace(): raise newException(ValueError, "uuid is required")
if assetAddress != "": # If a token is being used
if not isAddress(assetAddress): raise newException(ValueError, "assetAddress is not a valid ETH address")
if assetAddress == "0x0000000000000000000000000000000000000000": raise newException(ValueError, "assetAddress requires a valid token address")
if data != "": # If data is being used
if not validate(HexDataStr(data)): raise newException(ValueError, "data should contain a valid hex string")
proc hex2Time*(hex: string): Time = proc hex2Time*(hex: string): Time =
# represents the time since 1970-01-01T00:00:00Z # represents the time since 1970-01-01T00:00:00Z
fromUnix(fromHex[int64](hex)) fromUnix(fromHex[int64](hex))

View File

@ -43,7 +43,7 @@ ModalPopup {
if(!success){ if(!success){
sendingError.text = qsTr("Invalid transaction parameters") sendingError.text = qsTr("Invalid transaction parameters")
return sendingError.open() sendingError.open()
} }

View File

@ -51,7 +51,8 @@ ModalPopup {
if(!success){ if(!success){
sendingError.text = qsTr("Invalid transaction parameters") sendingError.text = qsTr("Invalid transaction parameters")
return sendingError.open() sendingError.open()
root.close()
} }
} }