Class: Merkle::Config
Overview
Merkle tree configuration class.
Constant Summary collapse
- HASH_TYPES =
Supported Hash type.
[:sha256, :double_sha256]
Instance Attribute Summary collapse
-
#branch_tag ⇒ Object
readonly
Returns the value of attribute branch_tag.
-
#hash_type ⇒ Object
readonly
Returns the value of attribute hash_type.
-
#sort_hashes ⇒ Object
readonly
Returns the value of attribute sort_hashes.
Class Method Summary collapse
-
.bitcoin ⇒ Merkle::Config
Bitcoin configuration.
-
.taptree ⇒ Merkle::Config
Taptree configuration.
Instance Method Summary collapse
-
#initialize(hash_type: :sha256, branch_tag: '', sort_hashes: true) ⇒ Config
constructor
Constructor If you enable this, Merkle::Proof’s directions are not required.
-
#tagged_hash(data, tag = branch_tag) ⇒ String
Generate tagged hash.
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.
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_tag ⇒ Object (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_type ⇒ Object (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_hashes ⇒ Object (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
.bitcoin ⇒ Merkle::Config
Bitcoin configuration.
28 29 30 |
# File 'lib/merkle/config.rb', line 28 def self.bitcoin Config.new(hash_type: :double_sha256, sort_hashes: false) end |
.taptree ⇒ Merkle::Config
Taptree configuration.
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.
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 |