DEV Community

Luke Miles
Luke Miles

Posted on

7 3

How to sign anchor transactions with phantom or other wallets in the browser

If you've tried using the (solana) anchor rpc convenience instructions, to avoid assembling your accounts and everything into a buffer explicitly, and used a browser wallet as a signer:

// Wrong!
await program.rpc.stake(
    myArgs,
    {
        accounts: {
            account1,
            account2,
        },
        signers: [wallet],
    }
)
Enter fullscreen mode Exit fullscreen mode

Then you've probably gotten this error, because the browser wallet doesn't hand out its secret key:

Uncaught (in promise) TypeError: unexpected type, use Uint8Array
Enter fullscreen mode Exit fullscreen mode

Instead, you can use program.transaction to make the transaction, then ask the wallet to sign it:

const tx = program.transaction.myInstruction(
    myArgs,
    {
        accounts: {
            account1,
            account2,
        },
        signers: [],
    }
)
tx.feePayer = wallet.publicKey
tx.recentBlockhash = (await connection.getLatestBlockhash()).blockhash
const signedTx = await wallet.signTransaction(tx)
const txId = await connection.sendRawTransaction(signedTx.serialize())
await connection.confirmTransaction(txId)
Enter fullscreen mode Exit fullscreen mode

Thanks to kfartusov on this issue

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (1)

Collapse
 
cisco0x60 profile image
Cisco

I'm sorry, program.transaction result to be deprecated. How am I suppose to solve this problem then?

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more