class Ethereum::Tx::Key
Attributes
private_key[R]
public_key[R]
Public Class Methods
new(priv: nil)
click to toggle source
# 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
Public Instance Methods
private_hex()
click to toggle source
# File lib/ethereum-tx/key.rb, line 11 def private_hex private_key.to_hex end
public_bytes()
click to toggle source
# File lib/ethereum-tx/key.rb, line 15 def public_bytes public_key.to_bytes end
public_hex()
click to toggle source
# File lib/ethereum-tx/key.rb, line 19 def public_hex public_key.to_hex end
sign(message)
click to toggle source
# 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_address()
click to toggle source
# 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)
click to toggle source
# 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
Private Instance Methods
message_hash(message)
click to toggle source
# File lib/ethereum-tx/key.rb, line 43 def message_hash(message) Utils.keccak256 message end
valid_s?(signature)
click to toggle source
# File lib/ethereum-tx/key.rb, line 47 def valid_s?(signature) s_value = Utils.v_r_s_for(signature).last s_value <= SECP256K1_N/2 && s_value != 0 end