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


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

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


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

def ec_key
  @ec_key
end

Class Method Details

.ecdsa_recover(msg, signature) ⇒ Object


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

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


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

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


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

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

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


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

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

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


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

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

#raw_public_keyObject

raw public key


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

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