Class: MoacEth::Key

Inherits:
Object
  • Object
show all
Defined in:
lib/moac_eth/key.rb

Defined Under Namespace

Classes: Decrypter, Encrypter

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(priv: nil) ⇒ Key

Returns a new instance of Key.



20
21
22
23
# File 'lib/moac_eth/key.rb', line 20

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.



6
7
8
# File 'lib/moac_eth/key.rb', line 6

def private_key
  @private_key
end

#public_keyObject (readonly)

Returns the value of attribute public_key.



6
7
8
# File 'lib/moac_eth/key.rb', line 6

def public_key
  @public_key
end

Class Method Details

.decrypt(data, password) ⇒ Object



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

def self.decrypt(data, password)
  priv = Decrypter.perform data, password
  new priv: priv
end

.encrypt(key, password) ⇒ Object



8
9
10
11
12
# File 'lib/moac_eth/key.rb', line 8

def self.encrypt(key, password)
  key = new(priv: key) unless key.is_a?(Key)

  Encrypter.perform key.private_hex, password
end

Instance Method Details

#addressObject Also known as: to_address



37
38
39
# File 'lib/moac_eth/key.rb', line 37

def address
  Utils.public_key_to_address public_hex
end

#private_hexObject



25
26
27
# File 'lib/moac_eth/key.rb', line 25

def private_hex
  private_key.to_hex
end

#public_bytesObject



29
30
31
# File 'lib/moac_eth/key.rb', line 29

def public_bytes
  public_key.to_bytes
end

#public_hexObject



33
34
35
# File 'lib/moac_eth/key.rb', line 33

def public_hex
  public_key.to_hex
end

#sign(message) ⇒ Object



42
43
44
# File 'lib/moac_eth/key.rb', line 42

def sign(message)
  sign_hash message_hash(message)
end

#sign_hash(hash) ⇒ Object



46
47
48
49
50
51
# File 'lib/moac_eth/key.rb', line 46

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



53
54
55
56
# File 'lib/moac_eth/key.rb', line 53

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