Class: Mpp::Methods::Tempo::Account
- Inherits:
-
Object
- Object
- Mpp::Methods::Tempo::Account
- Defined in:
- lib/mpp/methods/tempo/account.rb
Overview
Wrapper around the eth gem for signing. Requires the ‘eth` gem to be installed.
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
Class Method Summary collapse
-
.from_env(var = "TEMPO_PRIVATE_KEY") ⇒ Object
Load from environment variable.
-
.from_key(private_key) ⇒ Object
Load from hex private key (0x-prefixed).
Instance Method Summary collapse
-
#address ⇒ Object
Get the account’s Ethereum address (checksummed).
-
#initialize(key) ⇒ Account
constructor
A new instance of Account.
-
#private_key ⇒ Object
Get the private key as hex string.
-
#sign_hash(msg_hash) ⇒ Object
Sign a 32-byte hash, return 65-byte signature (r || s || v).
Constructor Details
#initialize(key) ⇒ Account
Returns a new instance of Account.
12 13 14 |
# File 'lib/mpp/methods/tempo/account.rb', line 12 def initialize(key) @key = key end |
Instance Attribute Details
#key ⇒ Object (readonly)
Returns the value of attribute key.
10 11 12 |
# File 'lib/mpp/methods/tempo/account.rb', line 10 def key @key end |
Class Method Details
.from_env(var = "TEMPO_PRIVATE_KEY") ⇒ Object
Load from environment variable.
23 24 25 26 27 28 |
# File 'lib/mpp/methods/tempo/account.rb', line 23 def self.from_env(var = "TEMPO_PRIVATE_KEY") key = ENV.fetch(var, nil) raise ArgumentError, "$#{var} not set" unless key && !key.empty? from_key(key) end |
.from_key(private_key) ⇒ Object
Load from hex private key (0x-prefixed).
17 18 19 20 |
# File 'lib/mpp/methods/tempo/account.rb', line 17 def self.from_key(private_key) require "eth" new(Eth::Key.new(priv: private_key.delete_prefix("0x"))) end |
Instance Method Details
#address ⇒ Object
Get the account’s Ethereum address (checksummed).
31 32 33 |
# File 'lib/mpp/methods/tempo/account.rb', line 31 def address @key.address.to_s end |
#private_key ⇒ Object
Get the private key as hex string.
36 37 38 |
# File 'lib/mpp/methods/tempo/account.rb', line 36 def private_key "0x#{@key.private_hex}" end |
#sign_hash(msg_hash) ⇒ Object
Sign a 32-byte hash, return 65-byte signature (r || s || v).
41 42 43 44 45 46 47 48 |
# File 'lib/mpp/methods/tempo/account.rb', line 41 def sign_hash(msg_hash) raise ArgumentError, "msg_hash must be 32 bytes, got #{msg_hash.bytesize}" unless msg_hash.bytesize == 32 sig = @key.sign(msg_hash) # eth gem returns hex signature, parse r, s, v sig_hex = sig.delete_prefix("0x") [sig_hex].pack("H*") end |