Class: Platon::Tx

Inherits:
Object
  • Object
show all
Extended by:
Sedes
Includes:
RLP::Sedes::Serializable
Defined in:
lib/platon/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.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/platon/tx.rb', line 30

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

  self.chain_id = (params[:chain_id]) ? params.delete(:chain_id) : nil

  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



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

def signature
  return @signature if @signature
  @signature = { v: v, r: r, s: s } if [v, r, s].all? && (v > 0)
end

Class Method Details

.decode(data) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/platon/tx.rb', line 21

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

  txh[:chain_id] = Platon.chain_id_from_signature(txh)

  self.new txh
end

Instance Method Details

#chain_idObject



130
131
132
# File 'lib/platon/tx.rb', line 130

def chain_id
  @chain_id
end

#chain_id=(cid) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
# File 'lib/platon/tx.rb', line 134

def chain_id=(cid)
  if cid != @chain_id
    self.v = 0
    self.r = 0
    self.s = 0

    clear_signature
  end

  @chain_id = (cid == 0) ? nil : cid
end

#dataObject



118
119
120
121
122
# File 'lib/platon/tx.rb', line 118

def data
  true ? data_hex : data_bin
  # Platon.tx_data_hex? ? data_hex : data_bin

end

#data=(string) ⇒ Object



124
125
126
127
128
# File 'lib/platon/tx.rb', line 124

def data=(string)
  true ? self.data_hex=(string) : self.data_bin=(string)
  # Platon.tx_data_hex? ? self.data_hex=(string) : self.data_bin=(string)

end

#data_hexObject



110
111
112
# File 'lib/platon/tx.rb', line 110

def data_hex
  Utils.bin_to_prefixed_hex data_bin
end

#data_hex=(hex) ⇒ Object



114
115
116
# File 'lib/platon/tx.rb', line 114

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

#ecdsa_signatureObject



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/platon/tx.rb', line 92

def ecdsa_signature
  return @ecdsa_signature if @ecdsa_signature

  if [v, r, s].all? && (v > 0)
    s_v = (self.chain_id) ? (v - (self.chain_id * 2) - 8) : v
    @ecdsa_signature = [
      Utils.int_to_base256(s_v),
      Utils.zpad_int(r),
      Utils.zpad_int(s),
    ].join
  end
end

#encodedObject



54
55
56
# File 'lib/platon/tx.rb', line 54

def encoded
  RLP.encode self
end

#fromObject



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

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

#hashObject Also known as: id



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

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

#hexObject



58
59
60
# File 'lib/platon/tx.rb', line 58

def hex
  Utils.bin_to_prefixed_hex encoded
end

#prevent_replays?Boolean

Returns:

  • (Boolean)


146
147
148
# File 'lib/platon/tx.rb', line 146

def prevent_replays?
  !self.chain_id.nil?
end

#sign(key) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/platon/tx.rb', line 62

def sign(key)
  sig = key.sign(unsigned_encoded)
  vrs = Utils.v_r_s_for sig
  self.v = self.chain_id ? ((self.chain_id * 2) + vrs[0] + 8) : vrs[0]
  self.r = vrs[1]
  self.s = vrs[2]

  clear_signature
  self
end

#signing_dataObject



50
51
52
# File 'lib/platon/tx.rb', line 50

def signing_data
  Utils.bin_to_prefixed_hex unsigned_encoded
end

#to_hObject



73
74
75
76
77
78
# File 'lib/platon/tx.rb', line 73

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

#unsigned_encodedObject



45
46
47
48
# File 'lib/platon/tx.rb', line 45

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