Class: Mpp::Methods::Tempo::Account

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#keyObject (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.

Raises:

  • (ArgumentError)


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

#addressObject

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_keyObject

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).

Raises:

  • (ArgumentError)


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