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 inherited from Node

Node::PRIVATE_RANGE_LIMIT

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_parent_node, #derive_private_key, #derive_public_key, from_bip32, #i_as_bytes, #index_hex, #left_from_hash, #negative?, #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_bech32_address, #to_bip32, #to_fingerprint, #to_identifier, #to_p2wpkh_p2sh, #to_serialized_hex

Methods included from Support

#base58_to_int, #bytes_to_hex, #bytes_to_int, #convert_p2wpkh_p2sh, #custom_hash_160, #decode_base58, #decode_base64, #digestify, #encode_base58, #encode_base64, #encode_p2wpkh_p2sh, #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, #to_serialized_bech32

Constructor Details

#initialize(opts = {}) ⇒ Master

Returns a new instance of Master.



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/money-tree/node.rb', line 265

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.



263
264
265
# File 'lib/money-tree/node.rb', line 263

def seed
  @seed
end

#seed_hashObject (readonly)

Returns the value of attribute seed_hash.



263
264
265
# File 'lib/money-tree/node.rb', line 263

def seed_hash
  @seed_hash
end

Instance Method Details

#generate_seedObject



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

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



302
303
304
# File 'lib/money-tree/node.rb', line 302

def generate_seed_hash(seed)
  hmac_sha512 HD_WALLET_BASE_KEY, seed
end

#is_private?Boolean

Returns:

  • (Boolean)


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

def is_private?
  true
end

#seed_hexObject



318
319
320
# File 'lib/money-tree/node.rb', line 318

def seed_hex
  bytes_to_hex(seed)
end

#seed_valid?(seed_hash) ⇒ Boolean

Returns:

  • (Boolean)


306
307
308
309
310
# File 'lib/money-tree/node.rb', line 306

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



312
313
314
315
316
# File 'lib/money-tree/node.rb', line 312

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