Why use Secp256r1 with Swig?
- Native support for passkeys and hardware-backed authentication
- Secure, standards-based cryptography
- Compatible with modern browsers and devices
- Enables session-based authorities for limited-lifetime delegation
Technical Overview
Secp256r1 for SWIG Wallet: Secp256r1 Authority: Represents an account or role that can sign transactions using a Secp256r1 key. Session Authority: Supports session-based authorities for limited-lifetime delegation. Signature Odometer: Each authority tracks a counter to prevent replay attacks.How to Use Secp256r1 Authority
The walkthrough covers both Rust and TypeScript implementations:- Key Generation
- Authority Management
- Transaction Signing & Replay Protection
- Session Authorities
TypeScript Implementation
Key Generation & Basic Usage
Here’s a complete TypeScript example using secp256r1 keys with Swig Wallet:WebAuthn Passkey Integration
For browser-based applications, you can integrate WebAuthn passkeys:Rust Implementation
A. Key Generation:
To use Secp256r1, a valid key pair is needed. A create_secp256r1_keypair function handles this using the openssl crate.- Curve: It uses the Nid::X9_62_PRIME256V1 curve, which is the standard for P-256.
- Public Key Format: The public key is serialized into a 33-byte compressed format. This is crucial, as it’s the format the on-chain program expects.
B. Authority Management:
A SWIG account can be created with a secp256r1 key as its primary authority or have one added later. This gives the secp256r1 key holder control over the account. For creating an Account with a secp256r1 Authority, the create_swig_secp256r1 function shows how to initialize a new swig account where a secp256r1 public key is the owner.C. Transaction Signing & Replay Protection:
Signing transactions with secp256r1 is more complex than with Ed25519. It involves a critical feature for security: the signature_odometer. The signature_odometer: This is an on-chain counter (u32) that is part of the Secp256r1Authority struct. A get_secp256r1_counter helper function reads this value from the swig account’s data.D. Session Authorities:
SWIG also supports session-based secp256r1 authorities. These are temporary keys with a defined lifespan, measured in Solana slots.Live Examples
Explore these working implementations to see secp256r1 authorities in action:TypeScript secp256r1 Transfer
Complete TypeScript example demonstrating secp256r1 key generation, Swig creation, and transfer operations using LiteSVM
Passkey Helper Functions
TypeScript helper functions for WebAuthn passkey integration with secp256r1 authorities
Interactive Passkey UI
Complete UI example showing how to build user-facing passkey authentication with secp256r1
Key Differences: TypeScript vs Rust
| Aspect | TypeScript | Rust |
|---|---|---|
| Key Generation | Uses @noble/curves/nist p256 | Uses OpenSSL EcKey with X9_62_PRIME256V1 |
| Public Key Format | Auto-compressed via p256.getPublicKey(key, true) | Manual compression via PointConversionForm::COMPRESSED |
| Signing Function | getSigningFnForSecp256r1PrivateKey() | Custom closure with sign_message() |
| WebAuthn Support | Built-in passkey integration | Requires external WebAuthn library |
| Testing Environment | LiteSVM for lightweight testing | SwigTestContext with full SVM |
| Use Cases | Client-side apps, browsers, passkeys | Server-side, CLI tools, testing |

