Module: BSV::Wallet::ProtoWallet::Validators

Included in:
KeyDeriver
Defined in:
lib/bsv/wallet/proto_wallet/validators.rb

Overview

Validation helpers for BRC-100 wallet method parameters.

Provides the subset of validators required by KeyDeriver and ProtoWallet. Raises InvalidParameterError for any invalid input.

Class Method Summary collapse

Class Method Details

.validate_counterparty!(counterparty) ⇒ Object

Validates a counterparty: ‘self’, ‘anyone’, or a 66-char hex pubkey.

Parameters:

  • counterparty (Object)

    the value to validate

Raises:



55
56
57
58
59
# File 'lib/bsv/wallet/proto_wallet/validators.rb', line 55

def validate_counterparty!(counterparty)
  return if %w[self anyone].include?(counterparty)

  validate_pub_key_hex!(counterparty, 'counterparty')
end

.validate_key_id!(key_id) ⇒ Object

Validates a BRC-43 key ID.

Must be a non-empty String of at most 800 bytes.

Parameters:

  • key_id (Object)

    the value to validate

Raises:



44
45
46
47
48
49
# File 'lib/bsv/wallet/proto_wallet/validators.rb', line 44

def validate_key_id!(key_id)
  raise InvalidParameterError.new('key_id', 'a String') unless key_id.is_a?(String)

  byte_length = key_id.bytesize
  raise InvalidParameterError.new('key_id', 'between 1 and 800 bytes') if byte_length < 1 || byte_length > 800
end

.validate_protocol_id!(protocol_id) ⇒ Object

Validates a BRC-43 protocol ID.

Must be an Array of [Integer(0-2), String(5-400 chars)]. The name is normalized (stripped and downcased) before length/content checks.

Parameters:

  • protocol_id (Object)

    the value to validate

Raises:



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/bsv/wallet/proto_wallet/validators.rb', line 20

def validate_protocol_id!(protocol_id)
  unless protocol_id.is_a?(Array) && protocol_id.length == 2
    raise InvalidParameterError.new('protocol_id', 'an Array of [security_level, protocol_name]')
  end

  level, name = protocol_id
  raise InvalidParameterError.new('protocol_id security level', '0, 1, or 2') unless [0, 1, 2].include?(level)
  raise InvalidParameterError.new('protocol_id name', 'a String') unless name.is_a?(String)

  name = name.strip.downcase
  max_length = name.start_with?('specific linkage revelation') ? 430 : 400
  raise InvalidParameterError.new('protocol_id name', "between 5 and #{max_length} characters") if name.length < 5 || name.length > max_length

  raise InvalidParameterError.new('protocol_id name', 'lowercase letters, numbers, and spaces only') unless name.match?(/\A[a-z0-9 ]+\z/)

  raise InvalidParameterError.new('protocol_id name', 'free of consecutive spaces') if name.include?('  ')
end

.validate_pub_key_hex!(value, name = 'public_key') ⇒ Object

Validates a compressed public key in hex form (66 chars, 02/03/04 prefix).

Parameters:

  • value (Object)

    the value to validate

  • name (String) (defaults to: 'public_key')

    parameter name for error messages

Raises:



66
67
68
69
70
# File 'lib/bsv/wallet/proto_wallet/validators.rb', line 66

def validate_pub_key_hex!(value, name = 'public_key')
  raise InvalidParameterError.new(name, 'a String') unless value.is_a?(String)

  raise InvalidParameterError.new(name, 'a 66-character hex string (compressed public key)') unless value.match?(/\A[0-9a-f]{66}\z/)
end