class Ethereum::Tx

lifted from github.com/lian/bitcoin-ruby thanks to everyone there for figuring this out TODO: Pull shared code out into a separate library

Constants

BYTE_ZERO
GTXCOST
GTXDATANONZERO
GTXDATAZERO
SECP256K1_N
UINT_MAX
VERSION

Attributes

signature[W]

Public Class Methods

decode(data) click to toggle source
# File lib/ethereum-tx.rb, line 44
def self.decode(data)
  data = Utils.hex_to_bin(data) if data.match(/\A\h+\Z/)
  deserialize(RLP.decode data)
end
new(*args) click to toggle source
# File lib/ethereum-tx.rb, line 49
def initialize(*args)
  fields = {v: 0, r: 0, s: 0}.merge parse_field_args(args)
  fields[:to] = Utils.normalize_address(fields[:to])

  serializable_initialize fields

  check_transaction_validity
end

Public Instance Methods

encoded() click to toggle source
# File lib/ethereum-tx.rb, line 62
def encoded
  RLP.encode self
end
from() click to toggle source
# File lib/ethereum-tx.rb, line 84
def from
  return @from if @from
  @from ||= OpenSsl.recover_compact(signature_hash, signature) if signature
end
hex() click to toggle source
# File lib/ethereum-tx.rb, line 66
def hex
  bin_to_hex encoded
end
sign(key) click to toggle source
# File lib/ethereum-tx.rb, line 70
def sign(key)
  self.signature = key.sign(unsigned_encoded)
  self.vrs = Utils.v_r_s_for signature

  self
end
signature() click to toggle source
# File lib/ethereum-tx.rb, line 89
def signature
  return @signature if @signature
  self.signature = [v, r, s].map do |integer|
    Utils.int_to_base256 integer
  end.join if [v, r, s].all?
end
to_h() click to toggle source
# File lib/ethereum-tx.rb, line 77
def to_h
  self.class.serializable_fields.keys.inject({}) do |hash, field|
    hash[field] = send field
    hash
  end
end
unsigned_encoded() click to toggle source
# File lib/ethereum-tx.rb, line 58
def unsigned_encoded
  RLP.encode self, sedes: Tx.exclude([:v, :r, :s])
end

Private Instance Methods

check_transaction_validity() click to toggle source
# File lib/ethereum-tx.rb, line 99
def check_transaction_validity
  if [gas_price, gas_limit, value, nonce].max > UINT_MAX
    raise InvalidTransaction, "Values way too high!"
  elsif gas_limit < intrinsic_gas_used
    raise InvalidTransaction, "Gas limit too low"
  end
end
intrinsic_gas_used() click to toggle source
# File lib/ethereum-tx.rb, line 113
def intrinsic_gas_used
  num_zero_bytes = data.count(BYTE_ZERO)
  num_non_zero_bytes = data.size - num_zero_bytes

  GTXCOST + GTXDATAZERO * num_zero_bytes +
    GTXDATANONZERO * num_non_zero_bytes
end
signature_hash() click to toggle source
# File lib/ethereum-tx.rb, line 121
def signature_hash
  Utils.keccak256 unsigned_encoded
end
vrs=(vrs) click to toggle source
# File lib/ethereum-tx.rb, line 107
def vrs=(vrs)
  self.v = vrs[0]
  self.r = vrs[1]
  self.s = vrs[2]
end