> ## Documentation Index
> Fetch the complete documentation index at: https://voltaire.tevm.sh/llms.txt
> Use this file to discover all available pages before exploring further.

# Swift

> Native Swift 5.9 bindings for Voltaire Ethereum primitives and cryptography on macOS 12+ and iOS 15+.

`Voltaire` (`packages/voltaire-swift`) provides native Swift 5.9 value types over Voltaire's Zig C ABI (`CVoltaire` linking `libprimitives_ts_native`), targeting macOS 12+ and iOS 15+.

```mermaid theme={null}
flowchart LR
  App["iOS / macOS App\n(import Voltaire)"] --> Swift["Swift Wrappers\n(Address, Hash, U256, Keccak256, Secp256k1)"]
  Swift --> CMod["CVoltaire SPM Target\n(primitives.h)"]
  CMod --> Dylib["libprimitives_ts_native.dylib\n(zig-out/native/)"]
  Dylib --> Zig["Voltaire Zig Core + Rust/C Crypto"]
```

## Requirements

* macOS 12+ or iOS 15+
* Swift 5.9+
* Zig 0.15+ (to compile `libprimitives_ts_native`)

## Build and Test

Build the Zig native library from the repository root first, then build and test the Swift package:

```bash theme={null}
# 1. Build the native C ABI dynamic library into zig-out/native/
zig build build-ts-native -Dwith-c-api=true

# 2. Build and test the Swift package
cd packages/voltaire-swift
swift build
swift test
```

## Quick Example

```swift theme={null}
import Voltaire

// 20-byte Ethereum address with EIP-55 checksum
let addr = try Address(hex: "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045")
print(addr.checksumHex)  // 0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045
print(addr.isZero)       // false

// Keccak-256 hashing
let digest = Keccak256.hash("hello world")
print(digest.hex)

// 256-bit unsigned integer and fixed 32-byte value
let amount = try U256(hex: "0x01")
let slot = try Bytes32(hex: "0x" + String(repeating: "00", count: 32))

// secp256k1 key generation and address derivation
let privateKey = try PrivateKey.generate()
let publicKey = try privateKey.publicKey()
let compressed = try publicKey.compressed()  // 33 bytes (SEC1)
let derivedAddress = try publicKey.address()
print(compressed.count, derivedAddress.checksumHex)

// Signature canonicalization and recovery
let msgHash = Keccak256.hash("hello")
let sig = try Signature(compact: rBytes + sBytes)
let canonical = sig.normalized()
let recovered = try canonical.recoverAddress(messageHash: msgHash)
```

## Type Surface

| Type | Representation | Key Operations |
| - | - | - |
| `Address` | 20 bytes | `init(hex:)`, `init(bytes:)`, `.zero`, `.hex`, `.checksumHex`, `.bytes`, `.isZero`, `isValidChecksum(_:)` |
| `Hash` | 32 bytes | `init(hex:)`, `init(bytes:)`, `.zero`, `.hex`, `.bytes` |
| `Bytes32` | 32 bytes | `init(hex:)`, `init(bytes:)`, `.zero`, `.hex`, `.bytes` |
| `U256` | 32 bytes (big-endian) | `init(hex:)`, `init(bytes:)`, `.zero`, `.hex`, `.bytes` |
| `Hex` | Namespace | `Hex.encode(_:)`, `Hex.decode(_:)` |
| `Keccak256` | Namespace | `Keccak256.hash(_:)` (`String`, `[UInt8]`, `Data`) |
| `PrivateKey` | 32 bytes | `generate()`, `init(bytes:)`, `.publicKey()`, `.bytes` |
| `PublicKey` | 64 bytes (`x \|\| y`) | `init(uncompressed:)`, `.compressed()`, `.address()` |
| `Signature` | `r` (32) + `s` (32) + `v` (1) | `init(compact:)`, `init(r:s:v:)`, `.serialize(includeV:)`, `.isCanonical`, `.normalized()`, `.recoverPublicKey(messageHash:)`, `.recoverAddress(messageHash:)` |

## Documentation

> [card] [Getting Started](https://voltaire.tevm.sh/swift/getting-started.md)
>
> Build the native Zig dylib and link `Voltaire` with SwiftPM.

> [card] [Xcode Integration](https://voltaire.tevm.sh/swift/getting-started-xcode.md)
>
> Add the package and dynamic library search paths in Xcode.

> [card] [Address](https://voltaire.tevm.sh/swift/primitives/address.md)
>
> 20-byte addresses and EIP-55 checksum validation.

> [card] [Keccak256](https://voltaire.tevm.sh/swift/crypto/keccak256.md)
>
> Keccak-256 hashing over strings, byte arrays, and `Data`.

> [card] [Hash & Bytes32](https://voltaire.tevm.sh/swift/primitives/hash.md)
>
> 32-byte hashes and fixed-width byte containers.

> [card] [U256 & Hex](https://voltaire.tevm.sh/swift/primitives/u256.md)
>
> 256-bit unsigned integers and hex encoding.

> [card] [Keys & Signatures](https://voltaire.tevm.sh/swift/primitives/signature.md)
>
> `PrivateKey`, `PublicKey`, low-s canonicalization, and `ecrecover`.

> [card] [Errors & Troubleshooting](https://voltaire.tevm.sh/swift/support/errors.md)
>
> `VoltaireError` cases and dylib rpath diagnostics.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.