Class: BSV::Primitives::KeyShares
- Inherits:
-
Object
- Object
- BSV::Primitives::KeyShares
- Defined in:
- lib/bsv/primitives/key_shares.rb
Overview
A set of Shamir’s Secret Sharing shares derived from a private key.
Holds the evaluation points (shares), a reconstruction threshold, and an integrity string for verifying that recombined shares produce the correct key. Supports serialisation to and from a human-readable backup format.
Backup format per share: “Base58(x).Base58(y).threshold.integrity”
This format is compatible with the Go and TypeScript reference SDKs.
Instance Attribute Summary collapse
-
#integrity ⇒ String
readonly
First 8 hex characters of the Hash160 of the compressed public key.
-
#points ⇒ Array<PointInFiniteField>
readonly
The share points.
-
#threshold ⇒ Integer
readonly
The minimum number of shares required to reconstruct the key.
Class Method Summary collapse
-
.from_backup_format(shares) ⇒ KeyShares
Deserialise shares from backup format strings.
Instance Method Summary collapse
-
#initialize(points, threshold, integrity) ⇒ KeyShares
constructor
A new instance of KeyShares.
-
#to_backup_format ⇒ Array<String>
Serialise shares to backup format strings.
Constructor Details
#initialize(points, threshold, integrity) ⇒ KeyShares
Returns a new instance of KeyShares.
33 34 35 36 37 |
# File 'lib/bsv/primitives/key_shares.rb', line 33 def initialize(points, threshold, integrity) @points = points @threshold = threshold @integrity = integrity end |
Instance Attribute Details
#integrity ⇒ String (readonly)
Returns first 8 hex characters of the Hash160 of the compressed public key.
28 29 30 |
# File 'lib/bsv/primitives/key_shares.rb', line 28 def integrity @integrity end |
#points ⇒ Array<PointInFiniteField> (readonly)
Returns the share points.
22 23 24 |
# File 'lib/bsv/primitives/key_shares.rb', line 22 def points @points end |
#threshold ⇒ Integer (readonly)
Returns the minimum number of shares required to reconstruct the key.
25 26 27 |
# File 'lib/bsv/primitives/key_shares.rb', line 25 def threshold @threshold end |
Class Method Details
.from_backup_format(shares) ⇒ KeyShares
Deserialise shares from backup format strings.
Each string must have the form “Base58(x).Base58(y).threshold.integrity”. All shares must agree on threshold and integrity.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/bsv/primitives/key_shares.rb', line 47 def self.from_backup_format(shares) threshold = 0 integrity = '' points = shares.each_with_index.map do |share, idx| parts = share.split('.', -1) raise ArgumentError, "invalid share format in share #{idx}: expected 4 dot-separated parts, got #{share.inspect}" unless parts.length == 4 x_str, y_str, t_str, i_str = parts raise ArgumentError, "threshold not found in share #{idx}" if t_str.empty? raise ArgumentError, "integrity not found in share #{idx}" if i_str.empty? t_int = Integer(t_str) if idx != 0 raise ArgumentError, "threshold mismatch in share #{idx}" unless threshold == t_int raise ArgumentError, "integrity mismatch in share #{idx}" unless integrity == i_str end threshold = t_int integrity = i_str PointInFiniteField.from_string("#{x_str}.#{y_str}") end new(points, threshold, integrity) end |
Instance Method Details
#to_backup_format ⇒ Array<String>
Serialise shares to backup format strings.
78 79 80 |
# File 'lib/bsv/primitives/key_shares.rb', line 78 def to_backup_format @points.map { |point| "#{point}.#{@threshold}.#{@integrity}" } end |