Class: Zilliqa::Crypto::KeyTool

Inherits:
Object
  • Object
show all
Includes:
BitcoinSecp256k1
Defined in:
lib/zilliqa/crypto/key_tool.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(private_key) ⇒ KeyTool

Returns a new instance of KeyTool.



8
9
10
11
12
# File 'lib/zilliqa/crypto/key_tool.rb', line 8

def initialize(private_key)
  is_raw = private_key.length == 32

  @pk = PrivateKey.new(privkey: private_key, raw: is_raw)
end

Class Method Details

.generate_private_keyObject



14
15
16
# File 'lib/zilliqa/crypto/key_tool.rb', line 14

def self.generate_private_key
  Util.encode_hex KeyTool.generate_random_bytes(32)
end

.generate_random_bytes(size) ⇒ Object



18
19
20
# File 'lib/zilliqa/crypto/key_tool.rb', line 18

def self.generate_random_bytes(size)
  SecureRandom.random_bytes(size)
end

.get_address_from_private_key(private_key) ⇒ Object

getAddressFromPrivateKey

takes a hex-encoded string (private key) and returns its corresponding 20-byte hex-encoded address.

Parameters:

  • privateKey (string)


44
45
46
47
# File 'lib/zilliqa/crypto/key_tool.rb', line 44

def self.get_address_from_private_key(private_key)
  public_key = KeyTool.get_public_key_from_private_key(private_key)
  KeyTool.get_address_from_public_key(public_key)
end

.get_address_from_public_key(public_key) ⇒ Object

getAddressFromPublicKey

takes hex-encoded string and returns the corresponding address

Parameters:

  • public_key (string)


55
56
57
58
# File 'lib/zilliqa/crypto/key_tool.rb', line 55

def self.get_address_from_public_key(public_key)
  orig_address = Digest::SHA256.hexdigest Util.decode_hex public_key
  Util::Bech32.to_bech32(orig_address[24..-1].downcase)
end

.get_public_key_from_private_key(private_key, is_compressed = true) ⇒ Object

getPubKeyFromPrivateKey

takes a hex-encoded string (private key) and returns its corresponding hex-encoded 33-byte public key.

Parameters:

  • privateKey (string)


29
30
31
32
33
34
35
# File 'lib/zilliqa/crypto/key_tool.rb', line 29

def self.get_public_key_from_private_key(private_key, is_compressed = true)
  is_raw = private_key.length == 32

  pk = PrivateKey.new(privkey: private_key, raw: is_raw)

  (Util.encode_hex pk.pubkey.serialize(compressed: is_compressed)).downcase
end