Class: Bitcoin::Wallet::KeyGenerator
- Inherits:
-
Object
- Object
- Bitcoin::Wallet::KeyGenerator
- Defined in:
- lib/bitcoin/wallet/keygenerator.rb
Overview
Deterministic key generator as described in bitcointalk.org/index.php?topic=11665.0.
Takes a seed and generates an arbitrary amount of keys. Protects against brute-force attacks by requiring the key hash to fit a difficulty target, much like the block chain.
Constant Summary collapse
- DEFAULT_TARGET =
difficulty target (0x0000FFFF00000000000000000000000000000000000000000000000000000000)
0x0000FFFF00000000000000000000000000000000000000000000000000000000
Instance Attribute Summary collapse
-
#nonce ⇒ Object
Returns the value of attribute nonce.
-
#seed ⇒ Object
Returns the value of attribute seed.
-
#target ⇒ Object
Returns the value of attribute target.
Instance Method Summary collapse
-
#find_nonce ⇒ Object
find a nonce that leads to the privkey satisfying the target.
-
#get_key(n = 0) ⇒ Object
get key number
n
from chain. -
#initialize(seed = nil, nonce = nil, target = nil) ⇒ KeyGenerator
constructor
Initialize key generator with optional
seed
andnonce
andtarget
.
Constructor Details
#initialize(seed = nil, nonce = nil, target = nil) ⇒ KeyGenerator
Initialize key generator with optional seed
and nonce
and target
.
- seed
-
the seed data for the keygenerator (default: random)
- nonce
-
the nonce required to satisfy the target (default: computed)
- target
-
custom difficulty target (default: DEFAULT_TARGET)
Example:
g = KeyGenerator.new # random seed, computed nonce, default target
KeyGenerator.new(g.seed)
KeyGenerator.new(g.seed, g.nonce)
g.get_key(0) #=> <Bitcoin::Key>
Note: When initializing without seed, you should obviously save the seed once it is generated. Saving the nonce is optional; it only saves time.
31 32 33 34 35 |
# File 'lib/bitcoin/wallet/keygenerator.rb', line 31 def initialize seed = nil, nonce = nil, target = nil @seed = seed || OpenSSL::Random.random_bytes(64) @target = target || DEFAULT_TARGET @nonce = check_nonce(nonce) end |
Instance Attribute Details
#nonce ⇒ Object
Returns the value of attribute nonce.
16 17 18 |
# File 'lib/bitcoin/wallet/keygenerator.rb', line 16 def nonce @nonce end |
#seed ⇒ Object
Returns the value of attribute seed.
16 17 18 |
# File 'lib/bitcoin/wallet/keygenerator.rb', line 16 def seed @seed end |
#target ⇒ Object
Returns the value of attribute target.
16 17 18 |
# File 'lib/bitcoin/wallet/keygenerator.rb', line 16 def target @target end |
Instance Method Details
#find_nonce ⇒ Object
find a nonce that leads to the privkey satisfying the target
46 47 48 49 50 |
# File 'lib/bitcoin/wallet/keygenerator.rb', line 46 def find_nonce n = 0 n += 1 while !check_target(get_hash(@seed, n)) n end |