class MoacEth::Key
Attributes
private_key[R]
public_key[R]
Public Class Methods
decrypt(data, password)
click to toggle source
# 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)
click to toggle source
# 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
new(priv: nil)
click to toggle source
# 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
Public Instance Methods
address()
click to toggle source
# File lib/moac_eth/key.rb, line 37 def address Utils.public_key_to_address public_hex end
Also aliased as: to_address
private_hex()
click to toggle source
# File lib/moac_eth/key.rb, line 25 def private_hex private_key.to_hex end
public_bytes()
click to toggle source
# File lib/moac_eth/key.rb, line 29 def public_bytes public_key.to_bytes end
public_hex()
click to toggle source
# File lib/moac_eth/key.rb, line 33 def public_hex public_key.to_hex end
sign(message)
click to toggle source
# File lib/moac_eth/key.rb, line 42 def sign(message) sign_hash message_hash(message) end
sign_hash(hash)
click to toggle source
# 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)
click to toggle source
# 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
Private Instance Methods
message_hash(message)
click to toggle source
# File lib/moac_eth/key.rb, line 61 def message_hash(message) Utils.keccak256 message end
valid_s?(signature)
click to toggle source
# File lib/moac_eth/key.rb, line 65 def valid_s?(signature) s_value = Utils.v_r_s_for(signature).last s_value <= Secp256k1::N/2 && s_value != 0 end