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

Inherits:
Object
  • Object
show all
Includes:
WalletWire
Defined in:
lib/bsv/wallet/substrates/http_wallet_wire.rb

Overview

Concrete WalletWire implementation over HTTP/HTTPS.

Transmits binary BRC-103 request frames via HTTP POST to ‘base_url/wallet` using `Content-Type: application/octet-stream`, and returns the raw binary response body.

The optional ‘http_client:` parameter accepts any object responding to `#request(uri, net_http_request)` — matching the injectable client convention used throughout this SDK. When omitted, `Net::HTTP` is used directly.

Examples:

Direct wire usage

wire   = BSV::Wallet::Substrates::HTTPWalletWire.new(base_url: 'https://wallet.example')
client = BSV::Wallet::WalletWireTransceiver.new(wire)
client.get_network  #=> { network: :mainnet }

Convenience factory

client = BSV::Wallet::Substrates::HTTPWalletWire.client(base_url: 'https://wallet.example')
client.get_network  #=> { network: :mainnet }

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url:, http_client: nil, headers: {}) ⇒ HTTPWalletWire

Returns a new instance of HTTPWalletWire.

Parameters:

  • base_url (String)

    wallet base URL (e.g. ‘wallet.example’)

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

    injectable HTTP client for testing; must respond to #request(uri, net_http_request). Defaults to Net::HTTP.

  • headers (Hash) (defaults to: {})

    additional headers merged into every request



35
36
37
38
39
# File 'lib/bsv/wallet/substrates/http_wallet_wire.rb', line 35

def initialize(base_url:, http_client: nil, headers: {})
  @uri         = build_uri(base_url)
  @http_client = http_client
  @headers     = headers.transform_keys(&:to_s)
end

Class Method Details

.client(base_url:, http_client: nil, headers: {}) ⇒ WalletWireTransceiver

Convenience factory: wraps a new BSV::Wallet::Substrates::HTTPWalletWire in a WalletWireTransceiver.

Parameters:

  • base_url (String)
  • http_client (#request, nil) (defaults to: nil)
  • headers (Hash) (defaults to: {})

Returns:



47
48
49
# File 'lib/bsv/wallet/substrates/http_wallet_wire.rb', line 47

def self.client(base_url:, http_client: nil, headers: {})
  WalletWireTransceiver.new(new(base_url: base_url, http_client: http_client, headers: headers))
end

Instance Method Details

#transmit_to_wallet(message) ⇒ String

POST binary frame to ‘base_url/wallet` and return the binary response body.

Parameters:

  • message (String)

    binary request frame (ASCII-8BIT)

Returns:

  • (String)

    binary result frame (ASCII-8BIT)

Raises:



56
57
58
59
60
61
62
63
64
65
# File 'lib/bsv/wallet/substrates/http_wallet_wire.rb', line 56

def transmit_to_wallet(message)
  http_post(message)
rescue BSV::Wallet::Error
  raise
rescue SocketError, Errno::ECONNREFUSED, Errno::ETIMEDOUT,
       Net::OpenTimeout, Net::ReadTimeout, Net::HTTPError => e
  raise BSV::Wallet::Error.new("wallet connection failed: #{e.message}", code: 1)
rescue StandardError => e
  raise BSV::Wallet::Error.new("wallet request failed: #{e.message}", code: 1)
end