Class: Eth::RpcSigner

Inherits:
Object
  • Object
show all
Defined in:
lib/eth/rpc_signer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ RpcSigner

Returns a new instance of RpcSigner.



6
7
8
# File 'lib/eth/rpc_signer.rb', line 6

def initialize(key)
  @key = key
end

Instance Attribute Details

#keyObject

Returns the value of attribute key.



4
5
6
# File 'lib/eth/rpc_signer.rb', line 4

def key
  @key
end

Class Method Details

.ecrecover_address(message_hash_hex, signature_hex) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/eth/rpc_signer.rb', line 24

def ecrecover_address(message_hash_hex, signature_hex)
  message_hash = Eth::Utils.hex_to_bin(message_hash_hex)
  signature = Eth::Utils.hex_to_bin(signature_hex)
  public_key = Eth::OpenSsl.recover_compact(message_hash, signature) 
  signer = Eth::Utils.public_key_to_address(public_key)
  return signer
end

Instance Method Details

#sign_message(message) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/eth/rpc_signer.rb', line 10

def sign_message(message)
  payload = Eth::Signature.new(message)
  payload.signer = @key.address
  payload.padded_message = Eth::Utils.prefix_message(message)
  payload.hash = Eth::Utils.keccak256(payload.padded_message)
  payload.signature = @key.sign_hash(payload.hash)
  payload.v, payload.r, payload.s = Eth::Utils.v_r_s_for(payload.signature)
  payload.rpc_signature = Eth::Utils.zpad_int(payload.r, 32) + Eth::Utils.zpad_int(payload.s, 32) + [(payload.v - 27)].pack('C')
  return payload
end