Class: BSV::Wallet::Substrates::HTTPWalletWire

Inherits:
Object
  • Object
show all
Defined in:
lib/bsv/wallet_interface/substrates/http_wallet_wire.rb

Overview

Binary wire transport that transmits BRC-100 wallet wire messages over HTTP.

Implements the single-method WalletWire interface: given a raw binary frame (as an Array of byte integers), parses the call code, maps it to a URL path, and POSTs the payload to the remote wallet endpoint.

Examples:

wire = BSV::Wallet::Substrates::HTTPWalletWire.new('http://localhost:3301')
response_bytes = wire.transmit_to_wallet(frame_bytes)

Instance Method Summary collapse

Constructor Details

#initialize(base_url, originator: nil, http_client: nil) ⇒ HTTPWalletWire

Returns a new instance of HTTPWalletWire.

Parameters:

  • base_url (String)

    the base URL of the remote wallet (e.g. ‘localhost:3301’)

  • originator (String, nil) (defaults to: nil)

    FQDN of the calling application (sent as Origin header)

  • http_client (#call, nil) (defaults to: nil)

    injectable HTTP client for testing; nil uses Net::HTTP



22
23
24
25
26
# File 'lib/bsv/wallet_interface/substrates/http_wallet_wire.rb', line 22

def initialize(base_url, originator: nil, http_client: nil)
  @base_url = base_url
  @originator = originator
  @http_client = http_client
end

Instance Method Details

#transmit_to_wallet(message) ⇒ Array<Integer>

Transmits a binary wallet wire message to the remote wallet.

Parses the call code from byte 0, reads the originator from the header, and POSTs the remaining payload bytes to the appropriate URL path.

Parameters:

  • message (Array<Integer>)

    raw wire frame as array of byte integers

Returns:

  • (Array<Integer>)

    response body as array of byte integers

Raises:

  • (ArgumentError)

    if the message is empty or contains an unknown call code

  • (RuntimeError)

    if the HTTP response indicates an error (non-2xx)



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/bsv/wallet_interface/substrates/http_wallet_wire.rb', line 37

def transmit_to_wallet(message)
  raise ArgumentError, 'message must not be empty' if message.nil? || message.empty?

  call_code = message[0]
  call_name = BSV::Wallet::Wire::Serializer::METHODS_BY_CODE[call_code]
  raise ArgumentError, "unknown call code: #{call_code}" if call_name.nil?

  originator_length = message[1] || 0
  originator = message[2, originator_length].pack('C*').force_encoding('UTF-8') if originator_length.positive?

  payload_start = 2 + originator_length
  payload = message[payload_start..] || []

  camel_name = BSV::WireFormat.snake_to_camel(call_name.to_s)
  url = "#{@base_url}/#{camel_name}"

  response_body = post_binary(url, payload, originator || @originator)
  response_body.bytes.to_a
end