Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions go/mechanisms/svm/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@ const (
// MemoProgramAddress is the SPL Memo program address
MemoProgramAddress = "MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr"

// SwigProgramAddress is the Swig smart wallet program address.
// Swig wraps inner instructions (e.g. SPL transferChecked) inside a signV2
// instruction and executes them via CPI using the Swig PDA as authority.
// See: https://github.com/anagram-xyz/swig
SwigProgramAddress = "swigypWHEksbC64pWKwah1WTeh9JXwx8H1rJHLdbQMB"

// SwigSignV2Discriminator is the U16 LE discriminator for Swig signV2 instructions
SwigSignV2Discriminator uint16 = 11

// Secp256r1PrecompileAddress is the secp256r1 precompile program address.
// Swig passkey wallets include secp256r1 signature verification instructions
// before the SignV2 instruction. These are filtered out during transaction flattening.
Secp256r1PrecompileAddress = "Secp256r1SigVerify1111111111111111111111111"

// DefaultCommitment is the default commitment level for transactions
DefaultCommitment = rpc.CommitmentConfirmed

Expand Down
33 changes: 16 additions & 17 deletions go/mechanisms/svm/exact/facilitator/scheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,35 +121,34 @@ func (f *ExactSvmScheme) Verify(
return nil, x402.NewVerifyError(ErrTransactionCouldNotBeDecoded, "", err.Error())
}

// Allow 3-6 instructions:
// Normalize the transaction (handles Swig, regular, and future wallet types)
normalized, err := svm.NormalizeTransaction(tx)
if err != nil {
return nil, x402.NewVerifyError(ErrNoTransferInstruction, "", err.Error())
}
instructions := normalized.Instructions
payer := normalized.Payer

// Instruction count check AFTER flattening (3-6)
// - 3 instructions: ComputeLimit + ComputePrice + TransferChecked
// - 4 instructions: ComputeLimit + ComputePrice + TransferChecked + Lighthouse or Memo
// - 5 instructions: ComputeLimit + ComputePrice + TransferChecked + Lighthouse + Lighthouse or Memo
// - 6 instructions: ComputeLimit + ComputePrice + TransferChecked + Lighthouse + Lighthouse + Memo
// See: https://github.com/coinbase/x402/issues/828
numInstructions := len(tx.Message.Instructions)
numInstructions := len(instructions)
if numInstructions < 3 || numInstructions > 6 {
return nil, x402.NewVerifyError(ErrTransactionInstructionsLength, "", fmt.Sprintf("transaction instructions length mismatch: %d < 3 or %d > 6", numInstructions, numInstructions))
}

// Step 3: Verify Compute Budget Instructions
if err := f.verifyComputeLimitInstruction(tx, tx.Message.Instructions[0]); err != nil {
if err := f.verifyComputeLimitInstruction(tx, instructions[0]); err != nil {
return nil, x402.NewVerifyError(err.Error(), "", err.Error())
}

if err := f.verifyComputePriceInstruction(tx, tx.Message.Instructions[1]); err != nil {
if err := f.verifyComputePriceInstruction(tx, instructions[1]); err != nil {
return nil, x402.NewVerifyError(err.Error(), "", err.Error())
}

// Extract payer from transaction
payer, err := svm.GetTokenPayerFromTransaction(tx)
if err != nil {
return nil, x402.NewVerifyError(ErrNoTransferInstruction, payer, err.Error())
}

// V2: payload.Accepted.Network is already validated by scheme lookup
// Network matching is implicit - facilitator was selected based on requirements.Network

// Convert requirements to old struct format for helper methods
reqStruct := x402.PaymentRequirements{
Scheme: requirements.Scheme,
Expand All @@ -160,17 +159,17 @@ func (f *ExactSvmScheme) Verify(
Extra: requirements.Extra,
}

// Step 4: Verify Transfer Instruction
if err := f.verifyTransferInstruction(tx, tx.Message.Instructions[2], reqStruct, signerAddressStrs); err != nil {
return nil, x402.NewVerifyError(err.Error(), payer, err.Error())
// Step 4: Verify Transfer Instruction (unified — works for both regular and Swig)
if verifyErr := f.verifyTransferInstruction(tx, instructions[2], reqStruct, signerAddressStrs); verifyErr != nil {
return nil, x402.NewVerifyError(verifyErr.Error(), payer, verifyErr.Error())
}

// Step 5: Verify optional instructions (if present)
// Allowed optional programs: Lighthouse (wallet protection) and Memo (uniqueness)
if numInstructions >= 4 {
lighthousePubkey := solana.MustPublicKeyFromBase58(svm.LighthouseProgramAddress)
memoPubkey := solana.MustPublicKeyFromBase58(svm.MemoProgramAddress)
optionalInstructions := tx.Message.Instructions[3:]
optionalInstructions := instructions[3:]
invalidReasons := []string{
ErrUnknownFourthInstruction,
ErrUnknownFifthInstruction,
Expand Down
60 changes: 60 additions & 0 deletions go/mechanisms/svm/normalizer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package svm

import (
"errors"

solana "github.com/gagliardetto/solana-go"
)

// NormalizedTransaction is the output of a TransactionNormalizer: a flat
// instruction list and the address of the entity paying the token transfer.
type NormalizedTransaction struct {
Instructions []solana.CompiledInstruction
Payer string
}

// TransactionNormalizer knows how to detect and flatten a particular wallet
// type's transaction layout into a NormalizedTransaction.
type TransactionNormalizer interface {
CanHandle(tx *solana.Transaction) bool
Normalize(tx *solana.Transaction) (*NormalizedTransaction, error)
}

// RegularNormalizer is the fallback normalizer for standard (non-smart-wallet)
// transactions. It returns the transaction's instructions as-is and derives the
// payer from the first TransferChecked instruction.
type RegularNormalizer struct{}

func (r *RegularNormalizer) CanHandle(_ *solana.Transaction) bool {
return true
}

func (r *RegularNormalizer) Normalize(tx *solana.Transaction) (*NormalizedTransaction, error) {
payer, err := GetTokenPayerFromTransaction(tx)
if err != nil {
return nil, err
}
return &NormalizedTransaction{
Instructions: tx.Message.Instructions,
Payer: payer,
}, nil
}

// DefaultNormalizers is the ordered list of normalizers tried by
// NormalizeTransaction. Specific wallet types come first; RegularNormalizer is
// the catch-all fallback.
var DefaultNormalizers = []TransactionNormalizer{
&SwigNormalizer{},
&RegularNormalizer{},
}

// NormalizeTransaction runs the default normalizer chain against tx and returns
// the first successful result.
func NormalizeTransaction(tx *solana.Transaction) (*NormalizedTransaction, error) {
for _, n := range DefaultNormalizers {
if n.CanHandle(tx) {
return n.Normalize(tx)
}
}
return nil, errors.New("no normalizer found for transaction")
}
Loading