# Module BSV::Script::BIP276 <a id="module-BSV-Script-BIP276"></a>

BIP-276 text encoding for scripts and templates.

Encodes and decodes typed bitcoin data using the scheme described in
https://github.com/moneybutton/bips/blob/master/bip-0276.mediawiki

Format: `<prefix>:<version-byte><network-byte><hex-payload><checksum>` where
version and network are each one byte (two hex digits), and checksum is the
first 4 bytes of double-SHA256 over the full preimage (the string up to and
including the payload), hex-encoded.

- **@note** Field order is version-then-network, matching the BIP-276 spec
and the Go SDK reference. The Python SDK has these reversed — a known
py-sdk bug that is invisible at default v=1, n=1.

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

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

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

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

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

### `VALID_BIP276` <a id="constant-VALID_BIP276"></a> <a id="VALID_BIP276-constant"></a>
Regex: prefix(:)(version 2 hex)(network 2 hex)(data hex*)(checksum 8 hex)

## Public Class Methods
### `decode(str)` <a id="method-c-decode"></a> <a id="decode-class_method"></a>
Decode a BIP-276 string.
- **@param** `str` [String] BIP-276 encoded string
- **@raise** [InvalidFormat] if the string is structurally malformed
- **@raise** [InvalidChecksum] if the checksum does not match the payload
- **@return** [Result] value object with +:prefix+, +:version+, +:network+, +:data+

### `decode_script(str)` <a id="method-c-decode_script"></a> <a id="decode_script-class_method"></a>
Decode a BIP-276 string that must have the {PREFIX_SCRIPT} prefix.
- **@param** `str` [String] BIP-276 encoded string
- **@raise** [InvalidFormat] if prefix is not +bitcoin-script+
- **@raise** [InvalidChecksum] if the checksum does not match
- **@return** [Result]

### `decode_template(str)` <a id="method-c-decode_template"></a> <a id="decode_template-class_method"></a>
Decode a BIP-276 string that must have the {PREFIX_TEMPLATE} prefix.
- **@param** `str` [String] BIP-276 encoded string
- **@raise** [InvalidFormat] if prefix is not +bitcoin-template+
- **@raise** [InvalidChecksum] if the checksum does not match
- **@return** [Result]

### `encode(data, prefix: = PREFIX_SCRIPT, network: = NETWORK_MAINNET, version: = CURRENT_VERSION)` <a id="method-c-encode"></a> <a id="encode-class_method"></a>
Encode a binary payload as a BIP-276 string.
- **@param** `data` [String] binary payload (e.g. a {Script::Script}'s binary form)
- **@param** `prefix` [String] {PREFIX_SCRIPT} or {PREFIX_TEMPLATE} (or any custom prefix)
- **@param** `version` [Integer] 1..255 — {CURRENT_VERSION} by default
- **@param** `network` [Integer] 1..255 — {NETWORK_MAINNET} by default
- **@raise** [ArgumentError] if version or network is out of the range 1..255
- **@return** [String] BIP-276 encoded string

### `encode_script(data, network: = NETWORK_MAINNET, version: = CURRENT_VERSION)` <a id="method-c-encode_script"></a> <a id="encode_script-class_method"></a>
Encode a script payload using the {PREFIX_SCRIPT} prefix.
- **@param** `data` [String] binary payload (e.g. a {Script::Script}'s binary form)
- **@param** `prefix` [String] {PREFIX_SCRIPT} or {PREFIX_TEMPLATE} (or any custom prefix)
- **@param** `version` [Integer] 1..255 — {CURRENT_VERSION} by default
- **@param** `network` [Integer] 1..255 — {NETWORK_MAINNET} by default
- **@return** [String] BIP-276 encoded string with +bitcoin-script+ prefix

### `encode_template(data, network: = NETWORK_MAINNET, version: = CURRENT_VERSION)` <a id="method-c-encode_template"></a> <a id="encode_template-class_method"></a>
Encode a template payload using the {PREFIX_TEMPLATE} prefix.
- **@param** `data` [String] binary payload (e.g. a {Script::Script}'s binary form)
- **@param** `prefix` [String] {PREFIX_SCRIPT} or {PREFIX_TEMPLATE} (or any custom prefix)
- **@param** `version` [Integer] 1..255 — {CURRENT_VERSION} by default
- **@param** `network` [Integer] 1..255 — {NETWORK_MAINNET} by default
- **@return** [String] BIP-276 encoded string with +bitcoin-template+ prefix
