Class: Crypto::Keys::Wif

Inherits:
Object
  • Object
show all
Defined in:
lib/crypto/keys/wif.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(wif_hex) ⇒ Wif

Returns a new instance of Wif.

Raises:



3
4
5
6
# File 'lib/crypto/keys/wif.rb', line 3

def initialize(wif_hex)
    @wif_hex = wif_hex
    raise AxentroError, "invalid wif: #{@wif_hex}" unless is_valid?
end

Class Method Details

.from(private_key, network = MAINNET) ⇒ Object

Raises:



12
13
14
15
16
# File 'lib/crypto/keys/wif.rb', line 12

def self.from(private_key, network = MAINNET)
    wif = KeyUtils.to_wif(private_key, network)
    raise AxentroError, "invalid wif: #{wif.as_hex}" unless wif.is_valid?
    wif
end

Instance Method Details

#addressObject



30
31
32
33
34
35
# File 'lib/crypto/keys/wif.rb', line 30

def address 
    res = KeyUtils.from_wif(self)
    public_key = res[:private_key].public_key
    network = res[:network]
    Address.new(KeyUtils.get_address_from_public_key(public_key), network)
end

#as_hexObject



8
9
10
# File 'lib/crypto/keys/wif.rb', line 8

def as_hex
    @wif_hex
end

#is_valid?Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
45
46
47
# File 'lib/crypto/keys/wif.rb', line 37

def is_valid?
    begin
    decoded_wif = Base64.strict_decode64(@wif_hex)
    network_key = decoded_wif[0..-7]
    hashed_key = Crypto::Hashes.sha256(Crypto::Hashes.sha256(network_key))
    checksum = hashed_key[0..5]
    checksum == decoded_wif[-6..-1]
    rescue 
        false
    end
end

#networkObject



26
27
28
# File 'lib/crypto/keys/wif.rb', line 26

def network
    KeyUtils.from_wif(self)[:network]
end

#private_keyObject



18
19
20
# File 'lib/crypto/keys/wif.rb', line 18

def private_key
    KeyUtils.from_wif(self)[:private_key]
end

#public_keyObject



22
23
24
# File 'lib/crypto/keys/wif.rb', line 22

def public_key
    KeyUtils.from_wif(self)[:private_key].public_key
end