Skip to content

Latest commit

 

History

History

Folders and files

README.md

External Delegate Token Master (Anchor)

Note

This is the Anchor v2 copy of this example. Every anchor command on this page needs the v2 CLI: cargo install anchor-cli --version 2.0.0-rc.1 --locked (avm has no prebuilt binary for this pre-release). The Anchor v1 version of this example is in ../anchor-v1.

Authorize token transfers using an external secp256k1 delegate signature.

See the example overview for the signed-message format and nonce semantics shared with the Quasar variant, and the repository catalog.

Major concepts

  • UserAccount state: the Solana authority, the delegate's 20-byte ethereum_address, and a nonce consumed by each signature-authorized transfer.
  • transfer_tokens rebuilds the authorized message onchain as keccak256(program id || user account || amount LE || recipient token account || nonce LE), recovers the signer with the secp256k1 syscall, compares the recovered Ethereum address to the stored one, and increments the nonce before the transfer CPI. The authority must also sign the transaction; the Ethereum signature supplements that check.
  • authority_transfer moves tokens with only the Solana authority's signature.
  • Both transfer handlers use transfer_checked through anchor_spl::token_interface, so the program works against the Classic Token Program and the Token Extensions Program.
  • Tokens are held by a token account owned by a PDA derived from the user account's address; the program signs the CPI with that PDA.

Setup

From this directory (tokens/external-delegate-token-master/anchor/):

cargo build-sbf

Prerequisites: Agave CLI (version in Anchor.toml [toolchain]) and Anchor.

Testing

Tests run in-process with LiteSVM. No local validator. Build first so target/deploy/external_delegate_token_master.so exists, then:

cargo test

The tests sign real transfer authorizations with a fixed secp256k1 key, send transactions, and assert token balances and nonce state, including the replay, wrong-amount, wrong-recipient, and wrong-authority failure paths.

Usage

Read the program source under programs/ and Anchor.toml for the program ID. For deployment, use anchor build && anchor deploy against your target cluster.