Class: Ethereum::Tx::Key

Inherits:
Object
  • Object
show all
Defined in:
lib/ethereum-tx/key.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(priv: nil) ⇒ Key

Returns a new instance of Key.



6
7
8
9
# File 'lib/ethereum-tx/key.rb', line 6

def initialize(priv: nil)
  @private_key = MoneyTree::PrivateKey.new key: priv
  @public_key = MoneyTree::PublicKey.new private_key, compressed: false
end

Instance Attribute Details

#private_keyObject (readonly)

Returns the value of attribute private_key.



4
5
6
# File 'lib/ethereum-tx/key.rb', line 4

def private_key
  @private_key
end

#public_keyObject (readonly)

Returns the value of attribute public_key.



4
5
6
# File 'lib/ethereum-tx/key.rb', line 4

def public_key
  @public_key
end

Instance Method Details

#private_hexObject



11
12
13
# File 'lib/ethereum-tx/key.rb', line 11

def private_hex
  private_key.to_hex
end

#public_bytesObject



15
16
17
# File 'lib/ethereum-tx/key.rb', line 15

def public_bytes
  public_key.to_bytes
end

#public_hexObject



19
20
21
# File 'lib/ethereum-tx/key.rb', line 19

def public_hex
  public_key.to_hex
end

#sign(message) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/ethereum-tx/key.rb', line 27

def sign(message)
  hash = message_hash(message)
  loop do
    signature = OpenSsl.sign_compact hash, private_hex, public_hex
    return signature if valid_s? signature
  end
end

#to_addressObject



23
24
25
# File 'lib/ethereum-tx/key.rb', line 23

def to_address
  Utils.bin_to_hex(Utils.keccak256(public_bytes[1..-1])[-20..-1])
end

#verify_signature(message, signature) ⇒ Object



35
36
37
38
# File 'lib/ethereum-tx/key.rb', line 35

def verify_signature(message, signature)
  hash = message_hash(message)
  public_hex == OpenSsl.recover_compact(hash, signature)
end