| Crates.io | stx2btc |
| lib.rs | stx2btc |
| version | 0.2.0 |
| created_at | 2025-07-17 05:54:48.136427+00 |
| updated_at | 2025-07-17 07:26:24.847545+00 |
| description | A tool for converting STX addresses to BTC addresses |
| homepage | |
| repository | |
| max_upload_size | |
| id | 1757103 |
| size | 34,192 |
A example Rust library for converting between Stacks (STX) and Bitcoin (BTC) native SegWit addresses.
This library is only a proof concept example. Do not use in production.
use stx2btc::{stx2btc, btc2stx};
let stx_address = "SP2V0G568F20Q1XCRT43XX8Q32V2DPMMYFHHBD8PP";
let btc_address = stx2btc(stx_address).unwrap();
println!("{}", btc_address); // bc1qkcypfjrcs9c0txx3ql029cckcnd498nuvl6wpy
let back_to_stx = btc2stx(&btc_address).unwrap();
println!("{}", back_to_stx); // SP2V0G568F20Q1XCRT43XX8Q32V2DPMMYFHHBD8PP
This crate includes Swift bindings generated using UniFFI. The bindings allow you to use the library from Swift/iOS applications.
Note: Bindings are not included in the repository and must be generated locally.
Using cargo aliases:
cargo build-libs && cargo swift-bindings
Or using just (install with cargo install just):
just swift
# Build the library (creates both libstx2btc.dylib and libstx2btc.a)
cargo build --release
# Generate Swift bindings (uses .dylib for introspection, but bindings work with both .dylib and .a)
cargo run --bin uniffi-bindgen generate --library target/release/libstx2btc.dylib --language swift --out-dir bindings --no-format
This will create the bindings/ directory with the Swift files.
Note: The binding generation uses the .dylib file to introspect the interface, but the generated Swift bindings work with both the dynamic library (.dylib) and static library (.a) - you choose which to link at build time in Xcode.
The generated Swift bindings are located in the bindings/ directory. You can integrate them into your iOS/macOS project:
Works on both macOS and iOS:
stx2btc.swift, stx2btcFFI.h, and stx2btcFFI.modulemap to your Xcode projectlibstx2btc.a static library to your projectFor macOS development/testing when you want smaller binaries:
stx2btc.swift, stx2btcFFI.h, and stx2btcFFI.modulemap to your Xcode projectlibstx2btc.dylib library to your projectNote: iOS requires the static library (.a) for App Store distribution, but macOS can use either.
import Foundation
do {
let btcAddress = try stx2btc(stxAddress: "SP2V0G568F20Q1XCRT43XX8Q32V2DPMMYFHHBD8PP")
print(btcAddress) // bc1qkcypfjrcs9c0txx3ql029cckcnd498nuvl6wpy
let backToStx = try btc2stx(btcAddress: btcAddress)
print(backToStx) // SP2V0G568F20Q1XCRT43XX8Q32V2DPMMYFHHBD8PP
} catch {
print("Conversion error: \(error)")
}
The Swift bindings include proper error handling with the ConversionError enum:
enum ConversionError: Error {
case SegwitDecode(String)
case UnsupportedVersion
case SegwitEncode(String)
}
UniFFI supports generating bindings for additional languages including kotlin, python and ruby. These are available but untested:
# Using cargo aliases
cargo kotlin-bindings # Kotlin/Android
cargo python-bindings # Python
cargo ruby-bindings # Ruby
# Using just
just kotlin # Kotlin/Android
just python # Python
just ruby # Ruby
just all # All languages (Swift, Kotlin, Python, Ruby)
See the UniFFI documentation for more details on supported languages and usage.