nimbus-eth1/nimbus/transaction.nim

86 lines
3.0 KiB
Nim
Raw Normal View History

# 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.
import
constants, errors, eth/[common, rlp, keys], nimcrypto, utils,
./vm/interpreter/[vm_forks, gas_costs]
import eth/common/transaction as common_transaction
export common_transaction
2018-11-20 17:35:11 +00:00
func intrinsicGas*(data: openarray[byte]): GasInt =
result = 21_000 # GasTransaction
for i in data:
if i == 0:
result += 4 # GasTXDataZero
else:
result += 68 # GasTXDataNonZero
proc intrinsicGas*(tx: Transaction, fork: Fork): GasInt =
# 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)
result = tx.payload.intrinsicGas
if tx.isContractCreation:
2019-08-26 16:18:56 +00:00
result = result + gasFees[fork][GasTXCreate]
2018-09-10 08:44:07 +00:00
proc getSignature*(transaction: Transaction, output: var Signature): bool =
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
# 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)
2018-09-10 08:44:07 +00:00
result = recoverSignature(bytes, output) == EthKeysStatus.Success
proc toSignature*(transaction: Transaction): Signature =
if not getSignature(transaction, result):
2019-08-29 12:57:01 +00:00
raise newException(Exception, "Invalid signature")
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):
var pubKey: PublicKey
2019-04-18 08:33:17 +00:00
var txHash = transaction.txHashNoSignature
2018-09-10 08:44:07 +00:00
if recoverSignatureKey(sig, txHash.data, pubKey) == EthKeysStatus.Success:
output = pubKey.toCanonicalAddress()
result = true
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
2019-02-26 07:04:12 +00:00
proc getRecipient*(tx: Transaction): EthAddress =
if tx.isContractCreation:
let sender = tx.getSender()
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")