class Ciri::Key
Ciri::Key
represent private/public key pair, it support several encryption methods used in Ethereum
Examples:
key = Ciri::Key.random key.ecdsa_signature(data)
Public Class Methods
ecdsa_recover(msg, signature)
click to toggle source
# File lib/ciri/key.rb, line 44 def ecdsa_recover(msg, signature) raw_public_key = Crypto.ecdsa_recover(msg, signature, return_raw_key: true) Ciri::Key.new(raw_public_key: raw_public_key) end
new(ec_key: nil, raw_public_key: nil, raw_private_key: nil)
click to toggle source
random()
click to toggle source
# File lib/ciri/key.rb, line 49 def random ec_key = OpenSSL::PKey::EC.new('secp256k1') ec_key.generate_key while (raw_priv_key = ec_key.private_key.to_s(2).size) != 32 warn "generated privkey is not 32 bytes, bytes: #{raw_priv_key.size} privkey: #{Utils.to_hex raw_priv_key} -> regenerate it..." ec_key.generate_key end Ciri::Key.new(ec_key: ec_key) end
Public Instance Methods
ec_key()
click to toggle source
# File lib/ciri/key.rb, line 96 def ec_key @ec_key ||= Ciri::Utils.create_ec_pk(raw_privkey: @raw_private_key, raw_pubkey: @raw_public_key) end
ecdsa_signature(data)
click to toggle source
# File lib/ciri/key.rb, line 73 def ecdsa_signature(data) Crypto.ecdsa_signature(secp256k1_key, data) end
ecies_decrypt(data, shared_mac_data = '')
click to toggle source
# File lib/ciri/key.rb, line 81 def ecies_decrypt(data, shared_mac_data = '') Crypto.ecies_decrypt(data, ec_key, shared_mac_data) end
ecies_encrypt(message, shared_mac_data = '')
click to toggle source
# File lib/ciri/key.rb, line 77 def ecies_encrypt(message, shared_mac_data = '') Crypto.ecies_encrypt(message, ec_key, shared_mac_data) end
raw_public_key()
click to toggle source
raw public key
# File lib/ciri/key.rb, line 69 def raw_public_key @raw_public_key ||= ec_key.public_key.to_bn.to_s(2) end
regenerate_ec_key()
click to toggle source
regenerate ec_key
from raw_public_key
and raw_private_key can used to validate the public_key
# File lib/ciri/key.rb, line 91 def regenerate_ec_key @ec_key = nil ec_key end
to_address()
click to toggle source
# File lib/ciri/key.rb, line 85 def to_address Types::Address.new(Utils.keccak(public_key)[-20..-1]) end
Private Instance Methods
public_key()
click to toggle source
public key
# File lib/ciri/key.rb, line 103 def public_key raw_public_key[1..-1] end
secp256k1_key()
click to toggle source
# File lib/ciri/key.rb, line 107 def secp256k1_key privkey = ec_key.private_key.to_s(2) # some times below error will occurs, raise error with more detail unless privkey.instance_of?(String) && privkey.size == 32 raise ArgumentError, "privkey must be composed of 32 bytes, bytes: #{privkey.size} privkey: #{Utils.to_hex privkey}" end @secp256k1_key ||= Crypto.ensure_secp256k1_key(privkey: privkey) end