class MoacEth::Tx

Attributes

signature[W]

Public Class Methods

decode(data) click to toggle source
# File lib/moac_eth/tx.rb, line 24
def self.decode(data)
  data = Utils.hex_to_bin(data) if data.match(/\A(?:0x)?\h+\Z/)
  deserialize(RLP.decode data)
end
new(params) click to toggle source
# File lib/moac_eth/tx.rb, line 29
def initialize(params)

  fields = params.merge({v: MoacEth.chain_id, r: 0, s: 0})
  fields[:to] = Utils.normalize_address(fields[:to])

  if params[:data]
    self.data = params.delete(:data)
    fields[:data_bin] = data_bin
  end
  serializable_initialize fields

  check_transaction_validity
end

Public Instance Methods

data() click to toggle source
# File lib/moac_eth/tx.rb, line 105
def data
  MoacEth.tx_data_hex? ? data_hex : data_bin
end
data=(string) click to toggle source
# File lib/moac_eth/tx.rb, line 109
def data=(string)
  MoacEth.tx_data_hex? ? self.data_hex=(string) : self.data_bin=(string)
end
data_hex() click to toggle source
# File lib/moac_eth/tx.rb, line 97
def data_hex
  Utils.bin_to_prefixed_hex data_bin
end
data_hex=(hex) click to toggle source
# File lib/moac_eth/tx.rb, line 101
def data_hex=(hex)
  self.data_bin = Utils.hex_to_bin(hex)
end
encoded() click to toggle source
# File lib/moac_eth/tx.rb, line 51
def encoded
  RLP.encode self
end
from() click to toggle source
# File lib/moac_eth/tx.rb, line 76
def from
  if signature
    public_key = OpenSsl.recover_compact(signature_hash, signature)
    Utils.public_key_to_address(public_key) if public_key
  end
end
hash() click to toggle source
# File lib/moac_eth/tx.rb, line 92
def hash
  "0x#{Utils.bin_to_hex Utils.keccak256_rlp(self)}"
end
Also aliased as: id
hex() click to toggle source
# File lib/moac_eth/tx.rb, line 55
def hex
  Utils.bin_to_prefixed_hex encoded
end
id()
Alias for: hash
sign(key) click to toggle source
# File lib/moac_eth/tx.rb, line 59
def sign(key)
  self.signature = key.sign(unsigned_encoded)
  vrs = Utils.v_r_s_for signature
  self.v = vrs[0]
  self.r = vrs[1]
  self.s = vrs[2]

  self
end
signature() click to toggle source
# File lib/moac_eth/tx.rb, line 83
def signature
  return @signature if @signature
  self.signature = [
    Utils.int_to_base256(v),
    Utils.zpad_int(r),
    Utils.zpad_int(s),
  ].join if [v, r, s].all?
end
signing_data() click to toggle source
# File lib/moac_eth/tx.rb, line 47
def signing_data
  Utils.bin_to_prefixed_hex unsigned_encoded
end
to_h() click to toggle source
# File lib/moac_eth/tx.rb, line 69
def to_h
  hash_keys.inject({}) do |hash, field|
    hash[field] = send field
    hash
  end
end
unsigned_encoded() click to toggle source
# File lib/moac_eth/tx.rb, line 43
def unsigned_encoded
  RLP.encode(unsigned, sedes: sedes)
end

Private Instance Methods

check_transaction_validity() click to toggle source
# File lib/moac_eth/tx.rb, line 122
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
hash_keys() click to toggle source
# File lib/moac_eth/tx.rb, line 116
def hash_keys
  keys = self.class.serializable_fields.keys
  keys.delete(:data_bin)
  keys + [:data]
end
intrinsic_gas_used() click to toggle source
# File lib/moac_eth/tx.rb, line 130
def intrinsic_gas_used
  num_zero_bytes = data_bin.count(BYTE_ZERO)
  num_non_zero_bytes = data_bin.size - num_zero_bytes

  Gas::GTXCOST +
    Gas::GTXDATAZERO * num_zero_bytes +
    Gas::GTXDATANONZERO * num_non_zero_bytes
end
sedes() click to toggle source
# File lib/moac_eth/tx.rb, line 147
def sedes
  if MoacEth.prevent_replays? && !(MoacEth.replayable_v? v)
    self.class
  else
    UnsignedTx
  end
end
signature_hash() click to toggle source
# File lib/moac_eth/tx.rb, line 139
def signature_hash
  Utils.keccak256 unsigned_encoded
end
unsigned() click to toggle source
# File lib/moac_eth/tx.rb, line 143
def unsigned
  Tx.new to_h.merge(v: MoacEth.chain_id, r: 0, s: 0)
end