Class: Ethereum::Tx

Inherits:
Object
  • Object
show all
Extended by:
Sedes
Includes:
RLP::Sedes::Serializable
Defined in:
lib/ethereum/tx.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Sedes

address, big_endian_int, binary, hash32, int20, int256, int32, trie_root

Constructor Details

#initialize(*args) ⇒ Tx

Returns a new instance of Tx.



29
30
31
32
33
34
35
36
# File 'lib/ethereum/tx.rb', line 29

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

Instance Attribute Details

#signatureObject



65
66
67
68
69
70
# File 'lib/ethereum/tx.rb', line 65

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

Class Method Details

.decode(data) ⇒ Object



24
25
26
27
# File 'lib/ethereum/tx.rb', line 24

def self.decode(data)
  data = Utils.hex_to_bin(data) if data.match(/\A\h+\Z/)
  deserialize(RLP.decode data)
end

Instance Method Details

#encodedObject



42
43
44
# File 'lib/ethereum/tx.rb', line 42

def encoded
  RLP.encode self
end

#fromObject



60
61
62
63
# File 'lib/ethereum/tx.rb', line 60

def from
  return @from if @from
  @from ||= OpenSsl.recover_compact(signature_hash, signature) if signature
end

#sign(key) ⇒ Object



46
47
48
49
50
51
# File 'lib/ethereum/tx.rb', line 46

def sign(key)
  self.signature = key.sign(unsigned_encoded)
  self.vrs = Utils.v_r_s_for signature

  self
end

#to_hObject



53
54
55
56
57
58
# File 'lib/ethereum/tx.rb', line 53

def to_h
  self.class.serializable_fields.keys.inject({}) do |hash, field|
    hash[field] = send field
    hash
  end
end

#unsigned_encodedObject



38
39
40
# File 'lib/ethereum/tx.rb', line 38

def unsigned_encoded
  RLP.encode self, sedes: Tx.exclude([:v, :r, :s])
end