class Crypto::Keys::Wif

Public Class Methods

from(private_key, network = MAINNET) click to toggle source
# 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
new(wif_hex) click to toggle source
# 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

Public Instance Methods

address() click to toggle source
# 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_hex() click to toggle source
# File lib/crypto/keys/wif.rb, line 8
def as_hex
    @wif_hex
end
is_valid?() click to toggle source
# 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
network() click to toggle source
# File lib/crypto/keys/wif.rb, line 26
def network
    KeyUtils.from_wif(self)[:network]
end
private_key() click to toggle source
# File lib/crypto/keys/wif.rb, line 18
def private_key
    KeyUtils.from_wif(self)[:private_key]
end
public_key() click to toggle source
# File lib/crypto/keys/wif.rb, line 22
def public_key
    KeyUtils.from_wif(self)[:private_key].public_key
end