2018-04-06 14:52:10 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2018 Status Research & Development GmbH
|
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
2018-01-17 12:57:50 +00:00
|
|
|
import
|
2021-03-31 10:14:02 +00:00
|
|
|
./constants, ./errors, eth/[common, keys], ./utils,
|
2021-03-31 16:53:15 +00:00
|
|
|
./vm_types2, ./vm_gas_costs
|
2018-01-17 12:57:50 +00:00
|
|
|
|
2019-08-13 08:13:22 +00:00
|
|
|
import eth/common/transaction as common_transaction
|
|
|
|
export common_transaction
|
2018-11-20 17:35:11 +00:00
|
|
|
|
2019-11-11 05:20:46 +00:00
|
|
|
func intrinsicGas*(data: openarray[byte], fork: Fork): GasInt =
|
|
|
|
result = gasFees[fork][GasTransaction]
|
2018-12-03 19:47:20 +00:00
|
|
|
for i in data:
|
|
|
|
if i == 0:
|
2019-11-11 05:20:46 +00:00
|
|
|
result += gasFees[fork][GasTXDataZero]
|
2018-12-03 19:47:20 +00:00
|
|
|
else:
|
2019-11-11 05:20:46 +00:00
|
|
|
result += gasFees[fork][GasTXDataNonZero]
|
2018-12-03 19:47:20 +00:00
|
|
|
|
2019-08-26 15:19:18 +00:00
|
|
|
proc intrinsicGas*(tx: Transaction, fork: Fork): GasInt =
|
2018-01-17 12:57:50 +00:00
|
|
|
# Compute the baseline gas cost for this transaction. This is the amount
|
|
|
|
# of gas needed to send this transaction (but that is not actually used
|
|
|
|
# for computation)
|
2019-11-11 05:20:46 +00:00
|
|
|
result = tx.payload.intrinsicGas(fork)
|
2018-01-17 12:57:50 +00:00
|
|
|
|
2019-08-26 15:19:18 +00:00
|
|
|
if tx.isContractCreation:
|
2019-08-26 16:18:56 +00:00
|
|
|
result = result + gasFees[fork][GasTXCreate]
|
2019-08-26 15:19:18 +00:00
|
|
|
|
2018-09-10 08:44:07 +00:00
|
|
|
proc getSignature*(transaction: Transaction, output: var Signature): bool =
|
2018-08-24 15:46:48 +00:00
|
|
|
var bytes: array[65, byte]
|
|
|
|
bytes[0..31] = transaction.R.toByteArrayBE()
|
|
|
|
bytes[32..63] = transaction.S.toByteArrayBE()
|
2019-04-18 08:33:17 +00:00
|
|
|
|
2018-08-24 15:46:48 +00:00
|
|
|
# TODO: V will become a byte or range soon.
|
2019-04-18 08:33:17 +00:00
|
|
|
var v = transaction.V.int
|
|
|
|
if v >= EIP155_CHAIN_ID_OFFSET:
|
|
|
|
v = 28 - (v and 0x01)
|
2018-09-10 08:44:07 +00:00
|
|
|
elif v == 27 or v == 28:
|
2019-04-18 08:33:17 +00:00
|
|
|
discard
|
2018-09-10 08:44:07 +00:00
|
|
|
else:
|
|
|
|
return false
|
|
|
|
|
2019-04-18 08:33:17 +00:00
|
|
|
bytes[64] = byte(v - 27)
|
2020-04-05 13:12:48 +00:00
|
|
|
let sig = Signature.fromRaw(bytes)
|
|
|
|
if sig.isOk:
|
|
|
|
output = sig[]
|
|
|
|
return true
|
|
|
|
return false
|
2018-09-10 08:44:07 +00:00
|
|
|
|
|
|
|
proc toSignature*(transaction: Transaction): Signature =
|
|
|
|
if not getSignature(transaction, result):
|
2019-08-29 12:57:01 +00:00
|
|
|
raise newException(Exception, "Invalid signature")
|
2018-08-24 15:46:48 +00:00
|
|
|
|
|
|
|
proc getSender*(transaction: Transaction, output: var EthAddress): bool =
|
|
|
|
## Find the address the transaction was sent from.
|
2018-09-10 08:44:07 +00:00
|
|
|
var sig: Signature
|
|
|
|
if transaction.getSignature(sig):
|
2019-04-18 08:33:17 +00:00
|
|
|
var txHash = transaction.txHashNoSignature
|
2020-07-20 06:50:05 +00:00
|
|
|
let pubkey = recover(sig, SkMessage(txHash.data))
|
2020-04-05 13:12:48 +00:00
|
|
|
if pubkey.isOk:
|
|
|
|
output = pubkey[].toCanonicalAddress()
|
2018-09-10 08:44:07 +00:00
|
|
|
result = true
|
2018-08-24 15:46:48 +00:00
|
|
|
|
2018-08-29 15:52:12 +00:00
|
|
|
proc getSender*(transaction: Transaction): EthAddress =
|
|
|
|
## Raises error on failure to recover public key
|
|
|
|
if not transaction.getSender(result):
|
|
|
|
raise newException(ValidationError, "Could not derive sender address from transaction")
|
2018-09-10 08:44:07 +00:00
|
|
|
|
2021-05-04 06:27:18 +00:00
|
|
|
proc getRecipient*(tx: Transaction, sender: EthAddress): EthAddress =
|
2019-02-26 07:04:12 +00:00
|
|
|
if tx.isContractCreation:
|
|
|
|
result = generateAddress(sender, tx.accountNonce)
|
|
|
|
else:
|
|
|
|
result = tx.to
|
2019-08-29 12:57:01 +00:00
|
|
|
|
|
|
|
proc validate*(tx: Transaction, fork: Fork) =
|
|
|
|
# Hook called during instantiation to ensure that all transaction
|
|
|
|
# parameters pass validation rules
|
|
|
|
if tx.intrinsicGas(fork) > tx.gasLimit:
|
|
|
|
raise newException(ValidationError, "Insufficient gas")
|
|
|
|
|
|
|
|
# check signature validity
|
|
|
|
var sender: EthAddress
|
|
|
|
if not tx.getSender(sender):
|
|
|
|
raise newException(ValidationError, "Invalid signature or failed message verification")
|
|
|
|
|
2019-08-29 13:44:54 +00:00
|
|
|
var
|
|
|
|
vMin = 27
|
|
|
|
vMax = 28
|
|
|
|
|
|
|
|
if tx.V.int >= EIP155_CHAIN_ID_OFFSET:
|
|
|
|
let chainId = (tx.V.int - EIP155_CHAIN_ID_OFFSET) div 2
|
|
|
|
vMin = 35 + (2 * chainId)
|
|
|
|
vMax = vMin + 1
|
|
|
|
|
|
|
|
var isValid = tx.R >= Uint256.one
|
|
|
|
isValid = isValid and tx.S >= Uint256.one
|
|
|
|
isValid = isValid and tx.V.int >= vMin
|
|
|
|
isValid = isValid and tx.V.int <= vMax
|
|
|
|
isValid = isValid and tx.S < SECPK1_N
|
|
|
|
isValid = isValid and tx.R < SECPK1_N
|
|
|
|
|
|
|
|
if fork >= FkHomestead:
|
|
|
|
isValid = isValid and tx.S < SECPK1_N div 2
|
|
|
|
|
|
|
|
if not isValid:
|
|
|
|
raise newException(ValidationError, "Invalid transaction")
|
|
|
|
|