Skip to content

Module BSV::WireFormat

Provides deep camelCase <-> snake_case key conversion for JSON wire boundaries.

Used wherever the SDK serialises or deserialises JSON payloads that follow the BRC-100/BRC-103 naming conventions (camelCase on the wire, snake_case in Ruby).

@example Convert a hash to wire format

BSV::WireFormat.to_wire({ identity_key: '02abc', protocol_id: [2, 'test'] })
# => { 'identityKey' => '02abc', 'protocolID' => [2, 'test'] }

@example Convert a wire format hash to Ruby

BSV::WireFormat.from_wire({ 'identityKey' => '02abc' })
# => { identity_key: '02abc' }

@example Deep conversion with nested hashes and arrays

BSV::WireFormat.to_wire({ outputs: [{ locking_script: 'abc' }] })
# => { 'outputs' => [{ 'lockingScript' => 'abc' }] }

Constants

CAMEL_TO_SNAKE

Inverse lookup table: camelCase -> snake_case.

SNAKE_TO_CAMEL

Well-known snake_case -> camelCase pairs for BRC-100/BRC-103 protocol keys.

Acronyms like protocolID, keyID are canonical per the TS SDK (Wallet.interfaces.ts). Generic regex fallback handles all other keys.

Public Class Methods

camel_to_snake(str)

Converts a single camelCase string to snake_case.

Uses the lookup table for known protocol keys. Falls back to generic regex substitution for unknown keys. - @param str [String] a camelCase string - @return [String] the snake_case equivalent

from_wire(hash)

Deeply converts all Hash keys from camelCase strings to snake_case symbols.

Recurses into nested Hash values and Array elements. Non-Hash, non-Array values are passed through unchanged (only keys are converted, not values). - @param hash [Hash] the hash to convert - @raise [ArgumentError] if the argument is nil - @return [Hash] a new hash with snake_case symbol keys

shallow_from_wire(hash)

Converts top-level Hash keys only from camelCase to snake_case symbols.

Unlike {.from_wire}, this does NOT recurse into nested hashes. Use this for auth handshake messages where nested values may contain user-data keys (e.g. base64 certificate type identifiers) that must not be mangled. - @param hash [Hash] the hash to convert - @raise [ArgumentError] - @return [Hash] a new hash with snake_case symbol keys (values unchanged)

shallow_to_wire(hash)

Converts top-level Hash keys only from snake_case to camelCase strings.

Unlike {.to_wire}, this does NOT recurse into nested hashes. Use this for auth handshake messages where nested values may contain user-data keys (e.g. base64 certificate type identifiers) that must not be mangled. - @param hash [Hash] the hash to convert - @raise [ArgumentError] - @return [Hash] a new hash with camelCase string keys (values unchanged)

snake_to_camel(str)

Converts a single snake_case string to camelCase.

Uses the lookup table for known protocol keys (preserving acronyms like protocolID). Falls back to generic capitalisation for unknown keys. - @param str [String] a snake_case string - @return [String] the camelCase equivalent

to_wire(hash)

Deeply converts all Hash keys from snake_case symbols/strings to camelCase strings.

Recurses into nested Hash values and Array elements. Non-Hash, non-Array values are passed through unchanged (only keys are converted, not values). - @param hash [Hash] the hash to convert - @raise [ArgumentError] if the argument is nil - @return [Hash] a new hash with camelCase string keys