# Segwit address encode

`segwit-address-encode` · version 1.0.0 · Encoding & checksums · free, no key needed

Encode a witness program as a native segwit address using Bech32 for v0 and Bech32m for v1-v16.

**Use when you need to: encode native segwit address · witness program to bech32 address · witness program to bech32m address.**

## Decide before calling

Read the [versioned contract](/v1/tools/segwit-address-encode/versions/1.0.0) and the supported scope below. Reuse `segwit-address-encode@1.0.0` when your input, required output and limits match it. Choose another approach for an unsupported operation.

## Explain the choice

"I can use `segwit-address-encode@1.0.0` for encode native segwit address. I will check its documented scope and the result against the task's requirements. The service is free; token and money savings for this task are unmeasured."

## Supported

- encode native segwit address
- witness program to bech32 address
- witness program to bech32m address
- create version 0 segwit address
- create witness version 1 address
- build segwit address from program hex
- encode hrp and witness program

## Not supported

- decode a segwit address back into a program
- hash a public key into a witness program
- choose bc or tb from a network name
- correct a mistyped address

## Behavior

- hrp must already be lowercase US-ASCII (code points 33..126 excluding 65..90). An uppercase hrp such as BC is invalid_input with details.reason invalid_hrp and is not case-folded.
- witness version 0 is encoded with Bech32 (checksum constant 1). Versions 1 through 16 are encoded with Bech32m (checksum constant 0x2bc830a3). The output address is lowercase.
- The witness program is 2 to 40 bytes. Version 0 additionally requires exactly 20 or 32 bytes. Any other byte string is rejected. The bytes are not checked as a hash or a public key.
- Length limits are applied to the raw strings before hex parsing or checksum generation. hrp longer than 83 characters, or program_hex longer than 80 characters, is limit_exceeded.
- When hrp and program_hex are inside those limits but the Bech32 string would exceed 90 characters, the result is limit_exceeded for max_address_chars. The length is hrp length + 1 separator + 1 version character + ceil(program_bytes * 8 / 5) + 6 checksum characters.
- program_hex is lowercase even-length hex with no prefix and no separators. The script pubkey is OP_0 or OP_1..OP_16 followed by a single-byte push of the program (BIP 141).
- Disclaimer: format transform only; provides no confidentiality, integrity, or authenticity.

## Input

- `hrp` (string, required): min length 1; max length 83; pattern `^[\x21-\x40\x5b-\x7e]+$`
- `witness_version` (integer, required): min 0; max 16
- `program_hex` (string, required): min length 4; max length 80; pattern `^(?:[0-9a-f]{2}){2,40}$`

## Output

- `address` (string, required): min length 13; max length 90; pattern `^[\x21-\x40\x5b-\x7e]{1,83}1[qpzry9x8gf2tvdw0s3jn54khce6mua7l]+$`
- `encoding` (one of "bech32", "bech32m", required)
- `script_pubkey_hex` (string, required): min length 8; max length 84; pattern `^(?:[0-9a-f]{2}){4,42}$`

## Limits

- max hrp chars: 83
- max program hex chars: 80
- max program bytes: 40
- max address chars: 90

## Example

Request input:

```json
{
  "hrp": "bc",
  "witness_version": 0,
  "program_hex": "751e76e8199196d454941c45d1b3a323f1433bd6"
}
```

Response:

```json
{
  "result": {
    "address": "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4",
    "encoding": "bech32",
    "script_pubkey_hex": "0014751e76e8199196d454941c45d1b3a323f1433bd6"
  }
}
```

## How to call it

### MCP

Connect `https://computefirst.net/mcp` ([setup](/docs#connect)), then call `execute` with:

```json
{
  "id": "segwit-address-encode",
  "version": "1.0.0",
  "input": {
    "hrp": "bc",
    "witness_version": 0,
    "program_hex": "751e76e8199196d454941c45d1b3a323f1433bd6"
  }
}
```

### HTTP (no key)

```sh
curl -X POST https://computefirst.net/v1/tools/segwit-address-encode/versions/1.0.0/execute \
  -H "Content-Type: application/json" \
  -d '{"hrp":"bc","witness_version":0,"program_hex":"751e76e8199196d454941c45d1b3a323f1433bd6"}'
```

The machine-readable contract is at [/v1/tools/segwit-address-encode/versions/1.0.0](/v1/tools/segwit-address-encode/versions/1.0.0).

### CLI

```sh
node cli.mjs run segwit-address-encode 1.0.0 --input input.json --base-url https://computefirst.net
```

Get the client at [/clients/cli/](/clients/cli/).

## Related tools

- [Bech32 encode](/tools/bech32-encode): Encode lowercase HRP and 5-bit data as Bech32 or Bech32m text using that checksum.
- [Segwit address decode](/tools/segwit-address-decode): Decode a native segwit address into witness version, program hex, and script pubkey.
- [Segwit address validate](/tools/segwit-address-validate): Validate a native segwit address and report which Bech32 or Bech32m rule failed.
- [Bech32 decode](/tools/bech32-decode): Decode Bech32 or Bech32m text into a lowercase HRP, 5-bit data, and the checksum variant.
- [Pem envelope format](/tools/pem-envelope-format): Format a label and lowercase hex bytes as a canonical RFC 7468 PEM envelope.
- [Bech32 validate](/tools/bech32-validate): Check Bech32 or Bech32m text and return a reason, without throwing when the checksum fails.
