Class: Codex32::Share

Inherits:
Object
  • Object
show all
Includes:
Codex32
Defined in:
lib/codex32/share.rb

Overview

Codex32 share class.

Constant Summary

Constants included from Codex32

BECH32_INV, CHARSET, HRP, MS32_CONST, MS32_LONG_CONST, SECRET_INDEX, SEPARATOR, VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Codex32

array_to_bech32, bech32_lagrange, bech32_mul, bech32_to_array, convert_bits, from, generate_share, interpolate_at, long_polymod, parse, polymod, valid_checksum?

Constructor Details

#initialize(id, threshold, index, payload) ⇒ Share

Returns a new instance of Share.

Parameters:

  • id (String)

    Identifier of this share.

  • threshold (Integer)

    Threshold.

  • index (String)

    Share index.

  • payload (String)

    Share payload.

Raises:



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/codex32/share.rb', line 13

def initialize(id, threshold, index, payload)
  unless CHARSET.include?(index.downcase)
    raise Codex32::Errors::InvalidBech32Character
  end
  unless threshold.zero? || (threshold > 1 && threshold < 10)
    raise Codex32::Errors::InvalidThreshold
  end
  @id = id.downcase
  @payload = payload.downcase
  @index = index.downcase
  @threshold = threshold

  if threshold.zero? && index != SECRET_INDEX
    raise Codex32::Errors::InvalidShareIndex
  end

  incomplete_group = payload.length * 5 % 8
  return unless incomplete_group > 4
  raise Codex32::Errors::IncompleteGroup,
        "Incomplete group #{incomplete_group}"
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



7
8
9
# File 'lib/codex32/share.rb', line 7

def id
  @id
end

#indexObject (readonly)

Returns the value of attribute index.



7
8
9
# File 'lib/codex32/share.rb', line 7

def index
  @index
end

#payloadObject (readonly)

Returns the value of attribute payload.



7
8
9
# File 'lib/codex32/share.rb', line 7

def payload
  @payload
end

#thresholdObject (readonly)

Returns the value of attribute threshold.



7
8
9
# File 'lib/codex32/share.rb', line 7

def threshold
  @threshold
end

Instance Method Details

#checksumString

Calculate checksum.

Returns:

  • (String)


37
38
39
40
41
42
43
# File 'lib/codex32/share.rb', line 37

def checksum
  data = bech32_to_array(content)
  return create_long_checksum(data) if data.length > 80
  poly_value = polymod(data + [0] * 13) ^ MS32_CONST
  result = 13.times.map { |i| (poly_value >> 5 * (12 - i)) & 31 }
  array_to_bech32(result)
end

#dataString

Return decoded payload.

Returns:

  • (String)

    Decoded payload.



47
48
49
50
51
# File 'lib/codex32/share.rb', line 47

def data
  convert_bits(bech32_to_array(payload), 5, 8, padding: false).pack(
    "C*"
  ).unpack1("H*")
end

#to_sString

Return bech32 string.

Returns:

  • (String)


55
56
57
# File 'lib/codex32/share.rb', line 55

def to_s
  HRP + SEPARATOR + content + checksum
end