Class: Bitcoin::Key

Inherits:
Object
  • Object
show all
Defined in:
lib/block_io/extended_bitcoinrb.rb

Instance Method Summary collapse

Constructor Details

#initialize(priv_key: nil, pubkey: nil, key_type: nil, compressed: true, allow_hybrid: false) ⇒ Key

Returns a new instance of Key.

Raises:

  • (ArgumentError)


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/block_io/extended_bitcoinrb.rb', line 76

def initialize(priv_key: nil, pubkey: nil, key_type: nil, compressed: true, allow_hybrid: false)
  # override so enforce compressed keys
  
  raise "key_type must always be Bitcoin::KEY::TYPES[:compressed]" unless key_type == TYPES[:compressed]
  puts '[Warning] Use key_type parameter instead of compressed. compressed parameter removed in the future.' if key_type.nil? && !compressed.nil? && pubkey.nil?
  if key_type
    @key_type = key_type
    compressed = @key_type != TYPES[:uncompressed]
  else
    @key_type = compressed ? TYPES[:compressed] : TYPES[:uncompressed]
  end
  @secp256k1_module =  Bitcoin.secp_impl
  @priv_key = priv_key
  if @priv_key
    raise ArgumentError, Errors::Messages::INVALID_PRIV_KEY unless validate_private_key_range(@priv_key)
  end
  if pubkey
    @pubkey = pubkey
  else
    @pubkey = generate_pubkey(priv_key, compressed: compressed) if priv_key
  end
  raise ArgumentError, Errors::Messages::INVALID_PUBLIC_KEY unless fully_valid_pubkey?(allow_hybrid)
end

Instance Method Details

#private_key_hexObject



104
105
106
# File 'lib/block_io/extended_bitcoinrb.rb', line 104

def private_key_hex
  @priv_key
end

#public_key_hexObject



100
101
102
# File 'lib/block_io/extended_bitcoinrb.rb', line 100

def public_key_hex
  @pubkey
end