Class: Merkle::Config

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/merkle/config.rb

Overview

Merkle tree configuration class.

Constant Summary collapse

HASH_TYPES =

Supported Hash type.

[:sha256, :double_sha256]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

#bin_to_hex, #combine_sorted, #hex_string?, #hex_to_bin

Constructor Details

#initialize(hash_type: :sha256, branch_tag: '', sort_hashes: true) ⇒ Config

Constructor If you enable this, Merkle::Proof’s directions are not required.

Parameters:

  • hash_type (Symbol) (defaults to: :sha256)

    The hashing algorithm used to hash the internal nodes.

  • branch_tag (String) (defaults to: '')

    Tags to use when hashing internal nodes.

  • sort_hashes (Boolean) (defaults to: true)

    Whether to sort internal nodes in lexicographical order and hash them.

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
# File 'lib/merkle/config.rb', line 17

def initialize(hash_type: :sha256, branch_tag: '', sort_hashes: true)
  raise ArgumentError, "hash_type #{hash_type} does not supported." unless HASH_TYPES.include?(hash_type)
  raise ArgumentError, "internal_tag must be string." unless branch_tag.is_a?(String)
  raise ArgumentError, "sort_hashes must be boolean." unless sort_hashes.is_a?(TrueClass) || sort_hashes.is_a?(FalseClass)
  @hash_type = hash_type
  @branch_tag = branch_tag
  @sort_hashes = sort_hashes
end

Instance Attribute Details

#branch_tagObject (readonly)

Returns the value of attribute branch_tag.



9
10
11
# File 'lib/merkle/config.rb', line 9

def branch_tag
  @branch_tag
end

#hash_typeObject (readonly)

Returns the value of attribute hash_type.



9
10
11
# File 'lib/merkle/config.rb', line 9

def hash_type
  @hash_type
end

#sort_hashesObject (readonly)

Returns the value of attribute sort_hashes.



9
10
11
# File 'lib/merkle/config.rb', line 9

def sort_hashes
  @sort_hashes
end

Class Method Details

.bitcoinMerkle::Config

Bitcoin configuration.

Returns:



28
29
30
# File 'lib/merkle/config.rb', line 28

def self.bitcoin
  Config.new(hash_type: :double_sha256, sort_hashes: false)
end

.taptreeMerkle::Config

Taptree configuration.

Returns:



34
35
36
# File 'lib/merkle/config.rb', line 34

def self.taptree
  Config.new(branch_tag: 'TapBranch')
end

Instance Method Details

#tagged_hash(data, tag = branch_tag) ⇒ String

Generate tagged hash.

Parameters:

  • data (String)

    The data to be hashed.

  • tag (String) (defaults to: branch_tag)

    Tag string used tagging.

Returns:

  • (String)

    Tagged hash value.

Raises:

  • (ArgumentError)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/merkle/config.rb', line 42

def tagged_hash(data, tag = branch_tag)
  raise ArgumentError, "data must be string." unless data.is_a?(String)
  raise ArgumentError, "tag must be a String." unless tag.is_a?(String)

  data_bin = hex_to_bin(data).b

  unless tag.empty?
    tag_bin = Digest::SHA256.digest(tag).b
    data_bin = tag_bin + tag_bin + data_bin
  end

  case hash_type
  when :sha256
    Digest::SHA256.digest(data_bin)
  when :double_sha256
    Digest::SHA256.digest(Digest::SHA256.digest(data_bin))
  end
end