Class: BitPay::KeyUtils

Inherits:
Object
  • Object
show all
Defined in:
lib/bitpay/key_utils.rb

Class Method Summary collapse

Class Method Details

.create_key(pem) ⇒ Object



20
21
22
# File 'lib/bitpay/key_utils.rb', line 20

def create_key pem
  OpenSSL::PKey::EC.new(pem)
end

.create_new_keyObject



24
25
26
27
28
# File 'lib/bitpay/key_utils.rb', line 24

def create_new_key
  key = OpenSSL::PKey::EC.new("secp256k1")
  key.generate_key
  key
end

.generate_pemObject



14
15
16
17
18
# File 'lib/bitpay/key_utils.rb', line 14

def generate_pem
  key = OpenSSL::PKey::EC.new("secp256k1")
  key.generate_key
  key.to_pem
end

.generate_sin_from_pem(pem) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/bitpay/key_utils.rb', line 51

def generate_sin_from_pem pem
  #http://blog.bitpay.com/2014/07/01/bitauth-for-decentralized-authentication.html
  #https://en.bitcoin.it/wiki/Identity_protocol_v1

  # NOTE:  All Digests are calculated against the binary representation, 
  # hence the requirement to use [].pack("H*") to convert to binary for each step

  #Generate Private Key
  key = OpenSSL::PKey::EC.new pem
  key.public_key.group.point_conversion_form = :compressed
  public_key = key.public_key.to_bn.to_s(2)
  step_one = Digest::SHA256.hexdigest(public_key)
  step_two = Digest::RMD160.hexdigest([step_one].pack("H*")) 
  step_three = "0F02" + step_two
  step_four_a = Digest::SHA256.hexdigest([step_three].pack("H*"))
  step_four = Digest::SHA256.hexdigest([step_four_a].pack("H*"))
  step_five = step_four[0..7]
  step_six = step_three + step_five
  encode_base58(step_six)
end

.get_private_key(key) ⇒ Object



30
31
32
# File 'lib/bitpay/key_utils.rb', line 30

def get_private_key key
  key.private_key.to_int.to_s(16)
end

.get_private_key_from_pem(pem) ⇒ Object

Raises:



39
40
41
42
43
# File 'lib/bitpay/key_utils.rb', line 39

def get_private_key_from_pem pem
  raise BitPayError, MISSING_PEM unless pem
  key = OpenSSL::PKey::EC.new(pem)
  get_private_key key
end

.get_public_key(key) ⇒ Object



34
35
36
37
# File 'lib/bitpay/key_utils.rb', line 34

def get_public_key key 
  key.public_key.group.point_conversion_form = :compressed
  key.public_key.to_bn.to_s(16).downcase
end

.get_public_key_from_pem(pem) ⇒ Object

Raises:



45
46
47
48
49
# File 'lib/bitpay/key_utils.rb', line 45

def get_public_key_from_pem pem
  raise BitPayError, MISSING_PEM unless pem
  key = OpenSSL::PKey::EC.new(pem)
  get_public_key key
end

.sign(message, privkey) ⇒ Object

Generate ECDSA signature

This is the last method that requires the ecdsa gem, which we would like to replace


76
77
78
79
80
81
82
83
84
85
# File 'lib/bitpay/key_utils.rb', line 76

def sign(message, privkey)
  group = ECDSA::Group::Secp256k1
  digest = Digest::SHA256.digest(message)
  signature = nil
  while signature.nil?
    temp_key = 1 + SecureRandom.random_number(group.order - 1)
    signature = ECDSA.sign(group, privkey.to_i(16), digest, temp_key)
    return ECDSA::Format::SignatureDerString.encode(signature).unpack("H*").first
  end
end

.sign_with_pem(pem, message) ⇒ Object



87
88
89
90
# File 'lib/bitpay/key_utils.rb', line 87

def sign_with_pem pem, message
  priv = get_private_key_from_pem pem
  sign(message, priv)
end