Class: Ciri::Key

Inherits:
Object
  • Object
show all
Defined in:
lib/ciri/key.rb

Overview

Ciri::Key represent private/public key pair, it support several encryption methods used in Ethereum

Examples:

key = Ciri::Key.random
key.ecdsa_signature(data)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ec_key: nil, raw_public_key: nil, raw_private_key: nil) ⇒ Key

initialized from ec_key or raw keys ec_key is a OpenSSL::PKey::EC object, raw keys is bytes presented keys



56
57
58
# File 'lib/ciri/key.rb', line 56

def initialize(ec_key: nil, raw_public_key: nil, raw_private_key: nil)
  @ec_key = ec_key || Ciri::Utils.create_ec_pk(raw_privkey: raw_private_key, raw_pubkey: raw_public_key)
end

Instance Attribute Details

#ec_keyObject (readonly)

Returns the value of attribute ec_key.



52
53
54
# File 'lib/ciri/key.rb', line 52

def ec_key
  @ec_key
end

Class Method Details

.ecdsa_recover(msg, signature) ⇒ Object



40
41
42
43
# File 'lib/ciri/key.rb', line 40

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

.randomObject



45
46
47
48
49
# File 'lib/ciri/key.rb', line 45

def random
  ec_key = OpenSSL::PKey::EC.new('secp256k1')
  ec_key.generate_key
  Ciri::Key.new(ec_key: ec_key)
end

Instance Method Details

#ecdsa_signature(data) ⇒ Object



65
66
67
# File 'lib/ciri/key.rb', line 65

def ecdsa_signature(data)
  Crypto.ecdsa_signature(secp256k1_key, data)
end

#ecies_decrypt(data, shared_mac_data = '') ⇒ Object



73
74
75
# File 'lib/ciri/key.rb', line 73

def ecies_decrypt(data, shared_mac_data = '')
  Crypto.ecies_decrypt(data, ec_key, shared_mac_data)
end

#ecies_encrypt(message, shared_mac_data = '') ⇒ Object



69
70
71
# File 'lib/ciri/key.rb', line 69

def ecies_encrypt(message, shared_mac_data = '')
  Crypto.ecies_encrypt(message, ec_key, shared_mac_data)
end

#raw_public_keyObject

raw public key



61
62
63
# File 'lib/ciri/key.rb', line 61

def raw_public_key
  @raw_public_key ||= ec_key.public_key.to_bn.to_s(2)
end