Class: Ethereum::Tx::Key
- Inherits:
-
Object
- Object
- Ethereum::Tx::Key
- Defined in:
- lib/ethereum-tx/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
-
#initialize(priv: nil) ⇒ Key
constructor
A new instance of Key.
- #private_hex ⇒ Object
- #public_bytes ⇒ Object
- #public_hex ⇒ Object
- #sign(message) ⇒ Object
- #to_address ⇒ Object
- #verify_signature(message, signature) ⇒ Object
Constructor Details
#initialize(priv: nil) ⇒ Key
Returns a new instance of Key.
6 7 8 9 |
# 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 |
Instance Attribute Details
#private_key ⇒ Object (readonly)
Returns the value of attribute private_key.
4 5 6 |
# File 'lib/ethereum-tx/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/ethereum-tx/key.rb', line 4 def public_key @public_key end |
Instance Method Details
#private_hex ⇒ Object
11 12 13 |
# File 'lib/ethereum-tx/key.rb', line 11 def private_hex private_key.to_hex end |
#public_bytes ⇒ Object
15 16 17 |
# File 'lib/ethereum-tx/key.rb', line 15 def public_bytes public_key.to_bytes end |
#public_hex ⇒ Object
19 20 21 |
# File 'lib/ethereum-tx/key.rb', line 19 def public_hex public_key.to_hex end |
#sign(message) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/ethereum-tx/key.rb', line 27 def sign() hash = () loop do signature = OpenSsl.sign_compact hash, private_hex, public_hex return signature if valid_s? signature end end |
#to_address ⇒ Object
23 24 25 |
# 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) ⇒ Object
35 36 37 38 |
# File 'lib/ethereum-tx/key.rb', line 35 def verify_signature(, signature) hash = () public_hex == OpenSsl.recover_compact(hash, signature) end |