Class: Eth::Tx

Inherits:
Object
  • Object
show all
Extended by:
Sedes
Includes:
RLP::Sedes::Serializable
Defined in:
lib/eth/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(params) ⇒ Tx

Returns a new instance of Tx.



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/eth/tx.rb', line 26

def initialize(params)
  fields = {v: 0, r: 0, s: 0}.merge params
  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

Instance Attribute Details

#signatureObject



79
80
81
82
83
84
85
86
# File 'lib/eth/tx.rb', line 79

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

Class Method Details

.decode(data) ⇒ Object



21
22
23
24
# File 'lib/eth/tx.rb', line 21

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

Instance Method Details

#dataObject



101
102
103
# File 'lib/eth/tx.rb', line 101

def data
  Eth.tx_data_hex? ? data_hex : data_bin
end

#data=(string) ⇒ Object



105
106
107
# File 'lib/eth/tx.rb', line 105

def data=(string)
  Eth.tx_data_hex? ? self.data_hex=(string) : self.data_bin=(string)
end

#data_hexObject



93
94
95
# File 'lib/eth/tx.rb', line 93

def data_hex
  Utils.bin_to_prefixed_hex data_bin
end

#data_hex=(hex) ⇒ Object



97
98
99
# File 'lib/eth/tx.rb', line 97

def data_hex=(hex)
  self.data_bin = Utils.hex_to_bin(hex)
end

#encodedObject



47
48
49
# File 'lib/eth/tx.rb', line 47

def encoded
  RLP.encode self
end

#fromObject



72
73
74
75
76
77
# File 'lib/eth/tx.rb', line 72

def from
  if signature
    public_key = OpenSsl.recover_compact(signature_hash, signature)
    Utils.public_key_to_address(public_key) if public_key
  end
end

#hashObject Also known as: id



88
89
90
# File 'lib/eth/tx.rb', line 88

def hash
  "0x#{Utils.bin_to_hex Utils.keccak256_rlp(self)}"
end

#hexObject



51
52
53
# File 'lib/eth/tx.rb', line 51

def hex
  Utils.bin_to_prefixed_hex encoded
end

#sign(key) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/eth/tx.rb', line 55

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

#signing_dataObject



43
44
45
# File 'lib/eth/tx.rb', line 43

def signing_data
  Utils.bin_to_prefixed_hex unsigned_encoded
end

#to_hObject



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

def to_h
  hash_keys.inject({}) do |hash, field|
    hash[field] = send field
    hash
  end
end

#unsigned_encodedObject



39
40
41
# File 'lib/eth/tx.rb', line 39

def unsigned_encoded
  RLP.encode(unsigned, sedes: sedes)
end