Class: Codex32::Share
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
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#index ⇒ Object
readonly
Returns the value of attribute index.
-
#payload ⇒ Object
readonly
Returns the value of attribute payload.
-
#threshold ⇒ Object
readonly
Returns the value of attribute threshold.
Instance Method Summary collapse
-
#checksum ⇒ String
Calculate checksum.
-
#data ⇒ String
Return decoded payload.
-
#initialize(id, threshold, index, payload) ⇒ Share
constructor
A new instance of Share.
-
#to_s ⇒ String
Return bech32 string.
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.
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
#id ⇒ Object (readonly)
Returns the value of attribute id.
7 8 9 |
# File 'lib/codex32/share.rb', line 7 def id @id end |
#index ⇒ Object (readonly)
Returns the value of attribute index.
7 8 9 |
# File 'lib/codex32/share.rb', line 7 def index @index end |
#payload ⇒ Object (readonly)
Returns the value of attribute payload.
7 8 9 |
# File 'lib/codex32/share.rb', line 7 def payload @payload end |
#threshold ⇒ Object (readonly)
Returns the value of attribute threshold.
7 8 9 |
# File 'lib/codex32/share.rb', line 7 def threshold @threshold end |
Instance Method Details
#checksum ⇒ String
Calculate checksum.
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 |
#data ⇒ String
Return 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 |