Skip to main content

Passkey Integration with Swig Wallet

This documentation covers how to integrate WebAuthn passkeys with Swig Wallet for secure, passwordless authentication on Solana.

Reference Implementations

This guide references two key examples from the Swig TypeScript SDK:

Overview

Swig Wallet supports WebAuthn passkeys through the secp256r1 elliptic curve, enabling users to authenticate using biometrics, security keys, or platform authenticators without requiring passwords or seed phrases.

Key Components

PasskeyManager Class

The PasskeyManager class provides a complete interface for creating, storing, and using passkeys:
Below is a compact, PasskeyManager implementation (create, sign, and stored credential helpers):

Core Features

  • Passkey Creation: Generate new WebAuthn credentials
  • Secure Storage: Store credentials in browser localStorage
  • Message Signing: Sign transactions using passkeys
  • Browser Support Detection: Check WebAuthn compatibility

Usage Guide

1. Check Browser Support

Before implementing passkeys, verify browser support:

2. Create a Passkey

Create a new passkey credential for a user:
The createPasskey method:
  • Uses ES256 algorithm (secp256r1 with SHA-256)
  • Requires platform authenticator (built-in biometrics)
  • Sets 60-second timeout
  • Automatically stores the credential

3. Sign Messages with Passkeys

Use the stored passkey to sign transaction messages:

4. Retrieve Stored Credentials

Access previously created passkey credentials:

5. Clear Stored Credentials

Remove passkey data from storage:

Advanced Integration

Custom Signing Function

Create a reusable signing function for your application:

Direct WebAuthn Integration

For advanced use cases, use the low-level WebAuthn functions:

Raw secp256r1 Keys (Testing & Development)

For testing without WebAuthn or when you need direct cryptographic control, use raw secp256r1 private keys:
When to use raw secp256r1 keys:
  • Testing and development environments
  • Server-side applications
  • When WebAuthn is not available
  • Automated scripts and tooling
  • Non-browser environments
Security Note: Raw private keys should never be used in production client applications. Always prefer WebAuthn passkeys for user-facing authentication.

Technical Details

Public Key Format

Passkeys use compressed secp256r1 public keys (33 bytes):
  • 1 byte prefix (0x02 or 0x03)
  • 32 bytes X-coordinate
The PasskeyManager automatically converts SPKI format to compressed format.

Signature Format

WebAuthn signatures are:
  • Raw 64-byte format (not DER encoded)
  • Normalized S values (canonical signatures)
  • Generated from hash(authenticatorData + SHA256(clientDataJSON))

Authentication Data

The WebAuthn prefix includes:
  • Authentication type identifier
  • Authenticator data
  • Client data field order
  • Huffman-compressed origin URL
  • Additional WebAuthn metadata

Security Considerations

Best Practices

  1. Verify Origin: Passkeys are bound to your domain origin
  2. Timeout Handling: Set appropriate timeouts for user experience
  3. Error Handling: Gracefully handle user cancellation
  4. Fallback Options: Provide alternative auth methods
  5. Secure Storage: Credentials in localStorage are domain-isolated

Browser Requirements

  • Chrome 67+ / Safari 14+ / Firefox 60+
  • HTTPS required (except localhost)
  • Platform authenticator support varies by device

Example Implementation

WebAuthn Passkey Example

Here’s a complete example integrating passkeys with Swig Wallet:

Raw secp256r1 Key Example (LiteSVM Testing)

For testing with LiteSVM or other development scenarios:

Examples

Check out these working implementations:

secp256r1 Transfer Example

Complete example using secp256r1 keys for transfers with LiteSVM testing

Passkey UI Example

Interactive UI demonstrating passkey integration with helper functions

Troubleshooting

Common Issues

  1. “WebAuthn not supported”: Browser lacks WebAuthn API
  2. “No authenticator available”: Device lacks biometric/security key
  3. “User cancelled”: User declined authentication prompt
  4. “Invalid domain”: Origin mismatch or non-HTTPS context

Debug Tips

  • Test on multiple devices and browsers
  • Check browser console for detailed WebAuthn errors
  • Verify HTTPS in production environments
  • Test with different authenticator types

Resources