# Module BSV::MCP::Tools::AddressInput <a id="module-BSV-MCP-Tools-AddressInput"></a>

|  |  |
| --- | --- |
| **Defined in** | gem/bsv-sdk/lib/bsv/mcp/tools/address_input.rb |

Validates address and WIF input for the tools that query an address or pay
one, before anything is sent over the network.

## Constants
### `ADDRESS_VERSIONS` <a id="constant-ADDRESS_VERSIONS"></a> <a id="ADDRESS_VERSIONS-constant"></a>
Not documented.

### `MAX_LENGTH` <a id="constant-MAX_LENGTH"></a> <a id="MAX_LENGTH-constant"></a>
Addresses are at most 35 characters and WIFs 52. Base58 decoding is quadratic,
so anything much longer could stall the single-threaded stdio server; the
input schemas carry the same limit.

### `P2PKH_HASH_BYTES` <a id="constant-P2PKH_HASH_BYTES"></a> <a id="P2PKH_HASH_BYTES-constant"></a>
Not documented.

### `WIF_VERSIONS` <a id="constant-WIF_VERSIONS"></a> <a id="WIF_VERSIONS-constant"></a>
Not documented.

## Public Class Methods
### `address_from_payload(payload, net_sym, allow_wif)` <a id="method-c-address_from_payload"></a> <a id="address_from_payload-class_method"></a>
- **@api** private

### `base58_address(payload)` <a id="method-c-base58_address"></a> <a id="base58_address-class_method"></a>
- **@api** private

### `decode_base58check(input)` <a id="method-c-decode_base58check"></a> <a id="decode_base58check-class_method"></a>
- **@api** private

### `ensure_length!(input, name)` <a id="method-c-ensure_length-21"></a> <a id="ensure_length!-class_method"></a>
- **@param** `input` [Object]
- **@param** `name` [String] the argument's name, for the error message
- **@raise** [ArgumentError] when a String input is over {MAX_LENGTH}; the
message never includes the input

### `p2pkh_hash(payload, net_sym)` <a id="method-c-p2pkh_hash"></a> <a id="p2pkh_hash-class_method"></a>
- **@api** private
- **@return** [String, nil] the frozen hash when the payload is a P2PKH one for the network

### `private_key(wif, net_sym, name: 'wif')` <a id="method-c-private_key"></a> <a id="private_key-class_method"></a>
The private key behind a WIF for `net_sym`, decoded the way addresses are, so
that a mistyped key is never echoed back: Base58's own error names the first
invalid character.

Only compressed WIFs are accepted, as in the TS SDK. An uncompressed one
stands for a different address from the compressed key's, which is the one the
tools would use.
- **@param** `wif` [Object]
- **@param** `net_sym` [Symbol] :mainnet or :testnet
- **@param** `name` [String] the argument's name, for the error message
- **@raise** [ArgumentError] with a fixed message, whether the WIF is invalid,
uncompressed (for either network) or belongs to the other network (so
a testnet key never signs a mainnet spend, or the reverse)
- **@return** [BSV::Primitives::PrivateKey]

### `pubkey_hash(input, net_sym, name: 'input')` <a id="method-c-pubkey_hash"></a> <a id="pubkey_hash-class_method"></a>
The 20-byte public key hash of a P2PKH address for `net_sym`, for building a
locking script. Callers must lock to this hash and never pass the address
string on: <code>Script.p2pkh_lock</code> takes any 20-byte String as a raw
hash, so a 20-character malformed address would be locked to its own ASCII
bytes.

The input is decoded as Base58Check and must carry the P2PKH version byte for
the network and a 20-byte payload. Anything else (bad characters or checksum,
P2SH, a WIF, the other network, wrong length, a non-String) raises an error
that never includes the input.
- **@param** `input` [Object]
- **@param** `net_sym` [Symbol] :mainnet or :testnet
- **@param** `name` [String] the argument's name, for the error message
- **@raise** [ArgumentError] with a fixed message
- **@return** [String] the frozen 20-byte binary hash

### `reject_uncompressed_wif!(payload, name)` <a id="method-c-reject_uncompressed_wif-21"></a> <a id="reject_uncompressed_wif!-class_method"></a>
- **@api** private
- **@raise** [ArgumentError] when the payload is an uncompressed WIF's (a
32-byte key, with no compression flag); the message is fixed

### `resolve(input, net_sym, allow_wif: false, name: 'input')` <a id="method-c-resolve"></a> <a id="resolve-class_method"></a>
Resolve user input to a P2PKH address for `net_sym`, before anything is sent
over the network.

The input is decoded as Base58Check: a P2PKH address for the network is
returned as is; with `allow_wif`, a WIF private key for the network yields its
address. Anything else (bad characters or checksum, P2SH, the other network,
wrong length) raises an error that never includes the input, which may be a
mistyped private key. A WIF must be compressed (see {private_key}).
- **@param** `input` [String]
- **@param** `net_sym` [Symbol] :mainnet or :testnet
- **@param** `allow_wif` [Boolean] also accept a WIF private key
- **@param** `name` [String] the argument's name, for the error message
- **@raise** [ArgumentError] with a fixed message
- **@return** [String] the address

### `wif_key_bytes(body)` <a id="method-c-wif_key_bytes"></a> <a id="wif_key_bytes-class_method"></a>
The 32 key bytes of a compressed WIF payload: the key and its 0x01 flag.
- **@api** private

### `wif_private_key(payload)` <a id="method-c-wif_private_key"></a> <a id="wif_private_key-class_method"></a>
The key of a WIF payload for either network.
- **@api** private
