Class: Crypto::Keys::Wif
- Inherits:
-
Object
- Object
- Crypto::Keys::Wif
- Defined in:
- lib/crypto/keys/wif.rb
Class Method Summary collapse
Instance Method Summary collapse
- #address ⇒ Object
- #as_hex ⇒ Object
-
#initialize(wif_hex) ⇒ Wif
constructor
A new instance of Wif.
- #is_valid? ⇒ Boolean
- #network ⇒ Object
- #private_key ⇒ Object
- #public_key ⇒ Object
Constructor Details
#initialize(wif_hex) ⇒ Wif
Returns a new instance of Wif.
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
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
#address ⇒ Object
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_hex ⇒ Object
8 9 10 |
# File 'lib/crypto/keys/wif.rb', line 8 def as_hex @wif_hex end |
#is_valid? ⇒ 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 |
#network ⇒ Object
26 27 28 |
# File 'lib/crypto/keys/wif.rb', line 26 def network KeyUtils.from_wif(self)[:network] end |