hashtastic 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 93a14f1cad9f8acd97b774aff0a17a9645fbe4c58e9ff436b558aaee2bb3ee7c
4
- data.tar.gz: d503ccccdfe7902945b342e9c95c3d6b84eb1dbc23b9a7087b17904011846eab
3
+ metadata.gz: a898d7fa1b1f5188d55a073cc4db58dc2b462fabad86d9798abd2e29620f9600
4
+ data.tar.gz: bb55e244d752b738d09e72205c680efb9799aacf52123c899acba5d51ab022ea
5
5
  SHA512:
6
- metadata.gz: 212807d96c812105679a1c0f6325a38598202d98c9e2ce2f0ceeafce54a59e41fdb050cc8d864507c09f80719bb120e878aa92c03df3f6cd93a55887ab863890
7
- data.tar.gz: 809e52a76d93323944578e19db72f9f48a1195438e1bb18ec97cb701c67973e8fadd41ab2bc339df13f7bb38e3ee3fbfb41b4da2653657a43656f15131b16f58
6
+ metadata.gz: e949ae465b4ae7e0cb59d2b1d6aa077dcc386707f115efeb66ef62220bd7fbf255007722b7173f6e6890f0c4f179a3252d27a262bd674d4c8107a2a044159a6a
7
+ data.tar.gz: ef1fd8c18bfb65ce033bf7da0c97ba98a1cc3691ee02136d3d3194acf6d8848d47f7165202054ede2095278760ee9c8176759b6dc47cf68c181aa2af4e9ac0de
@@ -4,6 +4,15 @@ All notable changes to this project will be documented in this file.
4
4
  The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
5
5
  and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## [0.3.1] - 2019-08-05
8
+ ### Added:
9
+ - Validate hex input
10
+
11
+ ### Fixed:
12
+ - Prefix '0x' with hexdigest
13
+
14
+ ### Changed:
15
+ - Move verify to class method of MerkleTree
7
16
 
8
17
  ## [0.3.0] - 2019-08-05
9
18
  ### Added:
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- hashtastic (0.3.0)
4
+ hashtastic (0.3.1)
5
5
  activesupport (~> 5.0)
6
6
  digest-sha3 (~> 1.1)
7
7
 
@@ -5,6 +5,7 @@ require 'active_support/all'
5
5
  require 'hashtastic/utils'
6
6
  require 'hashtastic/list_hasher'
7
7
  require 'hashtastic/dictionary_values_hasher'
8
+ require 'hashtastic/merkle_tree/invalid_hex_format_error'
8
9
  require 'hashtastic/ethereum_sha3'
9
10
  require 'hashtastic/dictionary_hasher'
10
11
  require 'hashtastic/digester'
@@ -10,7 +10,7 @@ module Hashtastic
10
10
 
11
11
  hasher.update(encoded_message)
12
12
 
13
- hasher.hexdigest
13
+ "0x#{hasher.hexdigest}"
14
14
  end
15
15
 
16
16
  private
@@ -11,7 +11,7 @@ module Hashtastic
11
11
  hasher.update(value.to_s)
12
12
  end
13
13
 
14
- hasher.hexdigest
14
+ "0x#{hasher.hexdigest}"
15
15
  end
16
16
  end
17
17
  end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class InvalidHexFormat < StandardError
4
+ def initialize(leaf)
5
+ super("invalid hex format, prefix '0x' is required or input is not a hex value: #{leaf}")
6
+ end
7
+ end
@@ -2,9 +2,29 @@
2
2
 
3
3
  module Hashtastic
4
4
  class MerkleTree
5
+ class << self
6
+ def verify(proof:, root:, leaf:, hashfunc: EthereumSHA3)
7
+ proof.each { |hex| validate_hex(hex) }
8
+
9
+ computed_hash =
10
+ proof.reduce(leaf) do |acc, element|
11
+ combined = [acc, element].map { |h| Utils.hex_to_ascii(h) }.sort.join
12
+
13
+ hashfunc.call(combined)
14
+ end
15
+
16
+ computed_hash == root
17
+ end
18
+
19
+ def validate_hex(hex)
20
+ raise ArgumentError, hex unless Utils.hex?(hex)
21
+ end
22
+ end
23
+
5
24
  attr_reader :layers, :leafs
6
25
 
7
26
  def initialize(leafs, hashfunc = EthereumSHA3)
27
+ leafs.each { |leaf| self.class.validate_hex(leaf) }
8
28
  @hashfunc = hashfunc
9
29
  @leafs = leafs.sort.uniq
10
30
  @layers = LayersCreator.call(@leafs, hashfunc)
@@ -27,21 +47,12 @@ module Hashtastic
27
47
  end
28
48
  end
29
49
 
50
+ private
51
+
30
52
  def pair_element(index, layer)
31
53
  pair_index = index.even? ? index + 1 : index - 1
32
54
 
33
55
  return layer[pair_index] if pair_index < layer.length
34
56
  end
35
-
36
- def verify(proof:, root:, leaf:)
37
- computed_hash =
38
- proof.reduce(leaf) do |acc, element|
39
- combined = [acc, element].map { |h| Utils.hex_to_ascii(h) }.sort.join
40
-
41
- @hashfunc.call(combined)
42
- end
43
-
44
- computed_hash == root
45
- end
46
57
  end
47
58
  end
@@ -8,6 +8,13 @@ module Hashtastic
8
8
 
9
9
  [value].pack('H*')
10
10
  end
11
+
12
+ def hex?(value)
13
+ pattern = /^[[:xdigit:]]+$/
14
+ return false if value[0..1] != '0x'
15
+
16
+ return value[2..-1].match?(pattern)
17
+ end
11
18
  end
12
19
  end
13
20
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Hashtastic
4
- VERSION = '0.3.0'
4
+ VERSION = '0.3.1'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hashtastic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - lucidity
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-08-05 00:00:00.000000000 Z
11
+ date: 2019-08-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -133,6 +133,7 @@ files:
133
133
  - lib/hashtastic/digester.rb
134
134
  - lib/hashtastic/ethereum_sha3.rb
135
135
  - lib/hashtastic/list_hasher.rb
136
+ - lib/hashtastic/merkle_tree/invalid_hex_format_error.rb
136
137
  - lib/hashtastic/merkle_tree/layers_creator.rb
137
138
  - lib/hashtastic/merkle_tree/leaf_not_found_error.rb
138
139
  - lib/hashtastic/merkle_tree/merkle_tree.rb