Class: Eth::Key
- Inherits:
-
Object
- Object
- Eth::Key
- Defined in:
- lib/eth/key.rb
Instance Attribute Summary collapse
-
#private_key ⇒ Object
readonly
Returns the value of attribute private_key.
-
#public_key ⇒ Object
readonly
Returns the value of attribute public_key.
Instance Method Summary collapse
- #address ⇒ Object
-
#initialize(priv: nil) ⇒ Key
constructor
A new instance of Key.
- #private_hex ⇒ Object
- #public_bytes ⇒ Object
- #public_hex ⇒ Object
- #sign(message) ⇒ Object
- #sign_hash(hash) ⇒ Object
- #verify_signature(message, signature) ⇒ Object
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_key ⇒ Object (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_key ⇒ Object (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
#address ⇒ Object
23 24 25 |
# File 'lib/eth/key.rb', line 23 def address Utils.bin_to_hex(Utils.keccak256(public_bytes[1..-1])[-20..-1]) end |
#private_hex ⇒ Object
11 12 13 |
# File 'lib/eth/key.rb', line 11 def private_hex private_key.to_hex end |
#public_bytes ⇒ Object
15 16 17 |
# File 'lib/eth/key.rb', line 15 def public_bytes public_key.to_bytes end |
#public_hex ⇒ Object
19 20 21 |
# File 'lib/eth/key.rb', line 19 def public_hex public_key.to_hex end |
#sign(message) ⇒ Object
27 28 29 |
# File 'lib/eth/key.rb', line 27 def sign() sign_hash () end |
#sign_hash(hash) ⇒ Object
31 32 33 34 35 36 |
# File 'lib/eth/key.rb', line 31 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
38 39 40 41 |
# File 'lib/eth/key.rb', line 38 def verify_signature(, signature) hash = () public_hex == OpenSsl.recover_compact(hash, signature) end |