swig-sdk 0.3.2

Standalone Rust SDK for the Swig wallet protocol on Solana
Documentation
# swig-sdk

[![Build status](https://github.com/bitrouter/swig-sdk/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/bitrouter/swig-sdk/actions)
[![Crates.io](https://img.shields.io/crates/v/swig-sdk)](https://crates.io/crates/swig-sdk)
[![Documentation](https://docs.rs/swig-sdk/badge.svg)](https://docs.rs/swig-sdk)

A standalone Rust SDK for the [Swig wallet protocol](https://onswig.com) on Solana.

## Why this SDK?

The [original Swig SDK](https://github.com/anagrambuild/swig-wallet) has not been updated with newer dependency versions, which may conflict with modern Solana projects.

**swig-sdk** is a from-scratch reimplementation that provides the same Swig wallet functionality with up-to-date dependencies (Solana SDK v3–v4), so you can integrate Swig wallets without version conflicts.

Pure Rust, no system dependencies, no `openssl-sys`.

## Installation

Add the crate to your project with Cargo:

```sh
cargo add swig-sdk
```

Or add it directly to your `Cargo.toml`:

```toml
[dependencies]
swig-sdk = "0.1"
```

### Feature flags

| Feature            | Default | Description                                                                             |
| ------------------ | ------- | --------------------------------------------------------------------------------------- |
| `rpc`              || Enables `solana-transaction` and `solana-keypair` for building and signing transactions |
| `token`            || Enables SPL Token and Associated Token Account support                                  |
| `alloy`            || Enables Ethereum-compatible signing via Alloy (secp256k1)                               |
| `openssl-vendored` || Vendor OpenSSL for secp256r1 support (useful in CI/Docker)                              |

## Usage

The SDK covers the full Swig wallet lifecycle:

1. **Wallet creation**`instruction::create::create_v1`
2. **Authority management** — add, update, remove authorities with granular permissions
3. **Session keys** — slot-based expiring session keys for delegated signing
4. **Delegated execution**`sign_v2` to execute arbitrary instructions through the wallet
5. **Sub-accounts** — create, sign, withdraw, and toggle sub-accounts
6. **Asset transfers** — transfer assets out of the wallet
7. **Close wallet** — reclaim rent

### Quick start

```rust
use swig_sdk::auth::ed25519::Ed25519ClientRole;
use swig_sdk::auth::ClientRole;
use swig_sdk::instruction;
use swig_sdk::types::{AuthorityConfig, AuthorityType, Permission};
use swig_sdk::{pda, PROGRAM_ID};

// Derive the Swig PDA
let swig_id: [u8; 32] = rand::random();
let (swig_pk, bump) = pda::swig_account(&swig_id);
let (wallet_addr, wallet_bump) = pda::swig_wallet_address(&swig_pk);

// Create a wallet with full permissions
let ix = instruction::create::create_v1(
    swig_pk,
    payer_pk,
    wallet_addr,
    swig_id,
    bump,
    wallet_bump,
    AuthorityType::Ed25519,
    &authority_pk.to_bytes(),
    &[Permission::All],
);

// Build a role handle for subsequent operations
let role = Ed25519ClientRole::new(authority_pk);

// Add a delegated authority with specific permissions
let new_auth = AuthorityConfig {
    authority_type: AuthorityType::Ed25519,
    authority_bytes: delegate_pk.to_bytes().to_vec(),
};
let ixs = role.add_authority(
    swig_pk,
    authority_pk,
    0,                          // signing role id
    new_auth,
    vec![
        Permission::Sol { amount: 5_000_000 },
        Permission::Token { mint: token_mint, amount: 10_000_000 },
    ],
    None,
).expect("add_authority");

// Execute an instruction through the wallet via SignV2
let inner_ix = sol_transfer_ix(wallet_addr, destination, lamports);
let ixs = role
    .sign_v2(swig_pk, wallet_addr, 0, vec![inner_ix], None, &[])
    .expect("sign_v2");
```

### Full example

The [`examples/round_trip.rs`](examples/round_trip.rs) example exercises every major SDK feature on-chain using a single Ed25519 keypair. It covers:

- Wallet creation
- Adding delegated authorities with rich permission sets
- Updating authority permissions (AddActions, RemoveActionsByType, RemoveActionsByIndex, ReplaceAll)
- Creating session keys with slot-based expiry
- Delegated SOL transfers via SignV2
- Sub-account lifecycle (create, sign, withdraw, toggle)
- Removing authorities
- Transferring assets
- Fee-payer separation (payer ≠ authority)

Run it against devnet:

```sh
SWIG_PRIVATE_KEY=<base58-64-byte-keypair> cargo run --example round_trip

# With airdrop enabled:
AIRDROP=true SWIG_PRIVATE_KEY=<base58-keypair> cargo run --example round_trip

# Custom RPC:
SOLANA_RPC_URL=https://api.devnet.solana.com SWIG_PRIVATE_KEY=<base58-keypair> cargo run --example round_trip
```

## License

Apache-2.0