Class: Eth::Key

Inherits:
Object
  • Object
show all
Defined in:
lib/eth/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/eth/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/eth/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/eth/key.rb', line 4

def public_key
  @public_key
end

Instance Method Details

#addressObject Also known as: to_address



23
24
25
# File 'lib/eth/key.rb', line 23

def address
  Utils.public_key_to_address public_hex
end

#private_hexObject



11
12
13
# File 'lib/eth/key.rb', line 11

def private_hex
  private_key.to_hex
end

#public_bytesObject



15
16
17
# File 'lib/eth/key.rb', line 15

def public_bytes
  public_key.to_bytes
end

#public_hexObject



19
20
21
# File 'lib/eth/key.rb', line 19

def public_hex
  public_key.to_hex
end

#sign(message) ⇒ Object



28
29
30
# File 'lib/eth/key.rb', line 28

def sign(message)
  sign_hash message_hash(message)
end

#sign_hash(hash) ⇒ Object



32
33
34
35
36
37
# File 'lib/eth/key.rb', line 32

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

#verify_signature(message, signature) ⇒ Object



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

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