> For the complete documentation index, see [llms.txt](https://docs.stacks.co/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.stacks.co/cookbook/clarity/address-utilities/convert-btc-to-stx-address.md).

# Convert BTC to STX address

{% code fullWidth="false" expandable="true" %}

```clarity
(define-read-only (btc-to-stx (input (string-ascii 60)))
  (let (
    ;; Decode base58 string to numbers
    (b58-numbers (map unwrap-uint (filter is-some-uint (map b58-to-uint input))))
    ;; Validate all characters are valid base58
    (t1 (asserts! (>= (len b58-numbers) (len input)) ERR_INVALID_CHAR))
    ;; Count leading '1's (zeros in base58)
    (leading-ones-count (default-to (len input) (index-of? (map is-zero b58-numbers) false)))
    ;; Convert to bytes
    (decoded (concat (fold decode-outer to-decode LST) leading-zeros))
    (decoded-hex (fold to-hex-rev decoded 0x))
    ;; Verify checksum
    (actual-checksum (unwrap-panic (slice? (sha256 (sha256 (unwrap-panic (slice? decoded-hex u0 (- decoded-hex-len u4))))) u0 u4)))
    (expected-checksum (unwrap-panic (slice? decoded-hex (- decoded-hex-len u4) decoded-hex-len)))
    (t3 (asserts! (is-eq actual-checksum expected-checksum) ERR_BAD_CHECKSUM))
    ;; Extract version and construct principal
    (version (unwrap-panic (element-at? STX_VER (unwrap! (index-of? BTC_VER (unwrap-panic (element-at? decoded-hex u0))) ERR_INVALID_VERSION))))
    )
    (principal-construct? version (unwrap-panic (as-max-len? (unwrap-panic (slice? decoded-hex u1 (- decoded-hex-len u4))) u20)))
  )
)

;; Example usage
(btc-to-stx "1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa") ;; Returns Stacks address
```

{% endcode %}

### Description

Convert Bitcoin addresses to their corresponding Stacks addresses using base58 decoding in Clarity

### Use Cases

* Cross-chain address mapping for Bitcoin-Stacks bridges
* Verifying ownership across both chains
* Converting legacy Bitcoin addresses to Stacks format
* Building cross-chain authentication systems

### Key Concepts

* **Base58 decoding** - Bitcoin addresses use base58 encoding
* **Checksum verification** - Last 4 bytes are a double SHA-256 checksum
* **Version mapping** - Bitcoin version bytes map to Stacks version bytes
* **Principal construction** - Build Stacks principal from decoded data


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.stacks.co/cookbook/clarity/address-utilities/convert-btc-to-stx-address.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
