John
John

Reputation: 5344

How can I create an associated token address with phantom JS?

@solana/spl-token has two methods:

Context, one only has the public address but access to phantom.

If the associated token already exists, getAssociatedTokenAddress works well but getOrCreateAssociatedTokenAccount requires the secret key.

Using Phantom, how can one generate that token address via a signature mechanism?

Concrete use case: one wants to send USDT to a public that does not have the USDT associated address. I would like phantom to somehow sign the action and create that address

Upvotes: 0

Views: 892

Answers (1)

nathanleclaire
nathanleclaire

Reputation: 1180

So, if this is all you want to do:

Concrete use case: one wants to send USDT to a public that does not have the USDT associated address. I would like phantom to somehow sign the action and create that address

You don't need to worry about creating the account directly, since you can just send the token to the wallet, and fund the account creation from the signer. So just a normal token::transfer should suffice IIRC.

But to answer your first question about how to do some operation that requires a private key using Phantom, the general approach is create a Transaction in JS, then use the wallet adapter signTransaction to sign it, and then send/confirm the signed transaction. (Depending on how you send and confirm it, you might have to add in a recent blockhash and payer to the Transaction as well)

This is similar to what createAssociatedTokenAccount does under the hood -- https://github.com/solana-labs/solana-program-library/blob/48fbb5b7/token/js/src/actions/createAssociatedTokenAccount.ts#L30 -- with the added twist of signing via wallet adapter.

Upvotes: 2

Related Questions