Class: MoneyTree::Master

Inherits:
Node
  • Object
show all
Defined in:
lib/money-tree/node.rb

Defined Under Namespace

Modules: SeedGeneration

Constant Summary collapse

HD_WALLET_BASE_KEY =
"Bitcoin seed"
RANDOM_SEED_SIZE =
32

Constants included from Support

Support::BASE58_CHARS, Support::INT32_MAX, Support::INT64_MAX

Instance Attribute Summary collapse

Attributes inherited from Node

#chain_code, #depth, #index, #is_private, #parent, #private_key, #public_key

Instance Method Summary collapse

Methods inherited from Node

#chain_code_hex, #depth_hex, #derive_private_key, #derive_public_key, from_bip32, from_serialized_address, #i_as_bytes, #index_hex, #left_from_hash, #node_for_path, #parent_fingerprint, #parse_index, parse_out_key, #private_derivation_message, #public_derivation_message, #right_from_hash, #strip_private_info!, #subnode, #to_address, #to_bip32, #to_fingerprint, #to_identifier, #to_serialized_address, #to_serialized_hex

Methods included from Support

#base58_to_int, #bytes_to_hex, #bytes_to_int, #decode_base58, #decode_base64, #digestify, #encode_base58, #encode_base64, #from_serialized_base58, #hex_to_bytes, #hex_to_int, #hmac_sha512, #hmac_sha512_hex, #int_to_base58, #int_to_bytes, #int_to_hex, #ripemd160, #sha256, #to_serialized_base58

Constructor Details

#initialize(opts = {}) ⇒ Master

Returns a new instance of Master.



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/money-tree/node.rb', line 244

def initialize(opts = {})
  @depth = 0
  @index = 0
  opts[:seed] = [opts[:seed_hex]].pack("H*") if opts[:seed_hex]
  if opts[:seed]
    @seed = opts[:seed]
    @seed_hash = generate_seed_hash(@seed)
    raise SeedGeneration::ImportError unless seed_valid?(@seed_hash)
    set_seeded_keys
  elsif opts[:private_key] || opts[:public_key]
    raise ImportError, 'chain code required' unless opts[:chain_code]
    @chain_code = opts[:chain_code]
    if opts[:private_key]
      @private_key = opts[:private_key]
      @public_key = MoneyTree::PublicKey.new @private_key
    else opts[:public_key]
      @public_key = if opts[:public_key].is_a?(MoneyTree::PublicKey)
        opts[:public_key]
      else
        MoneyTree::PublicKey.new(opts[:public_key])
      end
    end
  else
    generate_seed
    set_seeded_keys
  end
end

Instance Attribute Details

#seedObject (readonly)

Returns the value of attribute seed.



242
243
244
# File 'lib/money-tree/node.rb', line 242

def seed
  @seed
end

#seed_hashObject (readonly)

Returns the value of attribute seed_hash.



242
243
244
# File 'lib/money-tree/node.rb', line 242

def seed_hash
  @seed_hash
end

Instance Method Details

#generate_seedObject



276
277
278
279
280
# File 'lib/money-tree/node.rb', line 276

def generate_seed
  @seed = OpenSSL::Random.random_bytes(32)
  @seed_hash = generate_seed_hash(@seed)
  raise SeedGeneration::ValidityError unless seed_valid?(@seed_hash)
end

#generate_seed_hash(seed) ⇒ Object



282
283
284
# File 'lib/money-tree/node.rb', line 282

def generate_seed_hash(seed)
  hmac_sha512 HD_WALLET_BASE_KEY, seed
end

#is_private?Boolean

Returns:

  • (Boolean)


272
273
274
# File 'lib/money-tree/node.rb', line 272

def is_private?
  true
end

#seed_hexObject



298
299
300
# File 'lib/money-tree/node.rb', line 298

def seed_hex
  bytes_to_hex(seed)
end

#seed_valid?(seed_hash) ⇒ Boolean

Returns:

  • (Boolean)


286
287
288
289
290
# File 'lib/money-tree/node.rb', line 286

def seed_valid?(seed_hash)
  return false unless seed_hash.bytesize == 64
  master_key = left_from_hash(seed_hash)
  !master_key.zero? && master_key < MoneyTree::Key::ORDER
end

#set_seeded_keysObject



292
293
294
295
296
# File 'lib/money-tree/node.rb', line 292

def set_seeded_keys
  @private_key = MoneyTree::PrivateKey.new key: left_from_hash(seed_hash)
  @chain_code = right_from_hash(seed_hash)
  @public_key = MoneyTree::PublicKey.new @private_key
end