Class: Murky::Proof

Inherits:
Object
  • Object
show all
Defined in:
lib/murky/proof.rb

Constant Summary collapse

FILE_HEADER =
"#{?= * 20} BEGIN MURKY PROOF #{?= * 20} "
"#{?= * 20} BEGIN MURKY PROOF #{?= * 20} "
INPUT_REGEXP =
%r{#{FILE_HEADER}\n(.*?)\n#{FILE_FOOTER}}m

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root:, siblings:, signature:, digest:) ⇒ Proof

Returns a new instance of Proof.



9
10
11
12
13
14
15
16
17
# File 'lib/murky/proof.rb', line 9

def initialize(root:, siblings:, signature:, digest:)
  @digest    = digest
  @root      = root
  @siblings  = siblings
  @signature = signature
  @valid = root == siblings.reduce(signature) do |mem, sibling|
    sign Murky.xor(mem,sibling)
  end
end

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



3
4
5
# File 'lib/murky/proof.rb', line 3

def root
  @root
end

#siblingsObject (readonly)

Returns the value of attribute siblings.



3
4
5
# File 'lib/murky/proof.rb', line 3

def siblings
  @siblings
end

#signatureObject (readonly)

Returns the value of attribute signature.



3
4
5
# File 'lib/murky/proof.rb', line 3

def signature
  @signature
end

Class Method Details

.base64_decode(value) ⇒ Object



31
32
33
# File 'lib/murky/proof.rb', line 31

def self.base64_decode(value)
  Base64.decode64(value)
end

.base64_encode(value) ⇒ Object



27
28
29
# File 'lib/murky/proof.rb', line 27

def self.base64_encode(value)
  Base64.encode64(value)
end

.from_file(filename, digest: Digest::SHA256) ⇒ Object



48
49
50
51
# File 'lib/murky/proof.rb', line 48

def self.from_file(filename, digest: Digest::SHA256)
  root, *siblings, signature = IO.read(filename)[INPUT_REGEXP, 1].split("\n").map(&method(:base64_decode))
  Murky::Proof.new(root: root, siblings: siblings, signature: signature, digest: digest)
end

Instance Method Details

#output(filename) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/murky/proof.rb', line 35

def output(filename)
  IO.write(
    filename,
    "#{FILE_HEADER}\n" <<
    (
      [self.class.base64_encode(root)] +
      siblings.map(&self.class.method(:base64_encode)) +
      [self.class.base64_encode(@signature)]
    ).join <<
    "#{FILE_FOOTER}"
  )
end

#sign(value) ⇒ Object



19
20
21
# File 'lib/murky/proof.rb', line 19

def sign(value)
  @digest.digest(value.to_s)
end

#valid?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/murky/proof.rb', line 23

def valid?
  @valid
end