Class: BlockIo::Key

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

Class Method Summary collapse

Class Method Details

.from_passphrase(passphrase) ⇒ Object

Raises:

  • (Exception)


16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/block_io/key.rb', line 16

def self.from_passphrase(passphrase)
  # ATTENTION: use BlockIo::Key.new to generate new private keys. Using passphrases is not recommended due to lack of / low entropy.
  # create a private/public key pair from a given passphrase
  # use a long, random passphrase. your security depends on the passphrase's entropy.
  
  raise Exception.new('Must provide passphrase at least 8 characters long.') if passphrase.nil? or passphrase.length < 8
  
  hashed_key = Helper.sha256([passphrase].pack('H*')) # must pass bytes to sha256
  
  # modding is for backward compatibility with legacy bitcoinjs
  BlockIo::Key.from_private_key_hex((hashed_key.to_i(16) % ECDSA::Group::Secp256k1.order).to_s(16))
end

.from_private_key_hex(priv_key_hex) ⇒ Object



10
11
12
13
14
# File 'lib/block_io/key.rb', line 10

def self.from_private_key_hex(priv_key_hex)
  # returns Bitcoin::Key (compressed)
  # quirky behavior from bitcoinrb 0.7.0: use IntegerOctetString.encode on private key (integer) first
  Bitcoin::Key.new(:priv_key => ECDSA::Format::IntegerOctetString.encode(priv_key_hex.to_i(16), 32).bth, :key_type => Bitcoin::Key::TYPES[:compressed])
end

.from_wif(wif) ⇒ Object



29
30
31
32
33
34
# File 'lib/block_io/key.rb', line 29

def self.from_wif(wif)
  # returns a new key extracted from the Wallet Import Format provided

  Bitcoin::Key.from_wif(wif)

end

.generateObject



5
6
7
8
# File 'lib/block_io/key.rb', line 5

def self.generate
  # returns a new key
  Bitcoin::Key.generate(Bitcoin::Key::TYPES[:compressed]) # compressed
end