Class: HyperledgerCli::Key

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

Instance Method Summary collapse

Constructor Details

#initialize(public_key = nil) ⇒ Key

Returns a new instance of Key.



6
7
8
9
10
11
12
# File 'lib/hyperledger_cli/key.rb', line 6

def initialize(public_key = nil)
  @key = if public_key
      PKey::EC.new(File.open(path(public_key)).read)
    else
      PKey::EC.new("secp256k1").generate_key
    end
end

Instance Method Details

#path(public_key = nil) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/hyperledger_cli/key.rb', line 14

def path(public_key = nil)
  root = "#{ENV['HOME']}/.hyperledger"
  if public_key
    "#{root}/#{public_key}.pem"
  else
    root
  end
end

#public_keyObject



30
31
32
# File 'lib/hyperledger_cli/key.rb', line 30

def public_key
  @key.public_key.to_bn.to_s(16)
end

#sign(message) ⇒ Object



34
35
36
37
38
# File 'lib/hyperledger_cli/key.rb', line 34

def sign(message)
  digest = Digest::SHA256.digest(message)
  signature = @key.dsa_sign_asn1(digest)
  signature.unpack("H*").first.upcase
end

#writeObject



23
24
25
26
27
28
# File 'lib/hyperledger_cli/key.rb', line 23

def write
  FileUtils.mkdir_p path
  open path(public_key), 'w' do |io|
    io.write @key.to_pem
  end
end