class Eth::Key
The ‘Eth::Key` class to handle Secp256k1 private/public key-pairs.
def address
-
(Eth::Address)
- compressed address as packed hex prefixed string.
def address Util.public_key_to_address public_bytes end
def initialize(priv: nil)
-
priv
(String
) -- binary string of private key data (optional).
def initialize(priv: nil) # Creates a new, randomized libsecp256k1 context. ctx = Secp256k1::Context.new context_randomization_bytes: SecureRandom.random_bytes(32) # Creates a new random key pair (public, private). key = ctx.generate_key_pair unless priv.nil? # Converts hex private keys to binary strings. priv = Util.hex_to_bin priv if Util.is_hex? priv # Creates a keypair from existing private key data. key = ctx.key_pair_from_private_key priv end # Sets the attributes. @private_key = key.private_key @public_key = key.public_key end
def personal_sign(message, chain_id = nil)
-
(String)
- an EIP-191 conform, hexa-decimal signature.
Parameters:
-
chain_id
(Integer
) -- the chain id the signature should be generated on. -
message
(String
) -- the message string to be prefixed and signed.
def personal_sign(message, chain_id = nil) prefixed_message = Signature.prefix_message message hashed_message = Util.keccak256 prefixed_message sign hashed_message, chain_id end
def private_bytes
-
(String)
- private key as packed byte-string.
def private_bytes @private_key.data end
def private_hex
-
(String)
- private key as hexa-decimal string.
def private_hex Util.bin_to_hex @private_key.data end
def public_bytes
-
(String)
- uncompressed public key as packed byte-string.
def public_bytes @public_key.uncompressed end
def public_bytes_compressed
-
(String)
- compressed public key as packed byte-string.
def public_bytes_compressed @public_key.compressed end
def public_hex
-
(String)
- public key as uncompressed hexa-decimal string.
def public_hex Util.bin_to_hex @public_key.uncompressed end
def public_hex_compressed
-
(String)
- public key as compressed hexa-decimal string.
def public_hex_compressed Util.bin_to_hex @public_key.compressed end
def sign(blob, chain_id = nil)
-
(String)
- a hexa-decimal signature.
Parameters:
-
chain_id
(Integer
) -- the chain id the signature should be generated on. -
blob
(String
) -- that arbitrary data to be signed.
def sign(blob, chain_id = nil) context = Secp256k1::Context.new compact, recovery_id = context.sign_recoverable(@private_key, blob).compact signature = compact.bytes v = Chain.to_v recovery_id, chain_id is_leading_zero = true [v].pack("N").unpack("C*").each do |byte| is_leading_zero = false if byte > 0 and is_leading_zero signature.append byte unless is_leading_zero and byte === 0 end Util.bin_to_hex signature.pack "c*" end
def sign_typed_data(typed_data, chain_id = nil)
-
(String)
- an EIP-712 conform, hexa-decimal signature.
Parameters:
-
chain_id
(Integer
) -- the chain id the signature should be generated on. -
typed_data
(Array
) -- all the data in the typed data structure to be signed.
def sign_typed_data(typed_data, chain_id = nil) hash_to_sign = Eip712.hash typed_data sign hash_to_sign, chain_id end