Finally breakthrough in spl-token transfer of a Solana minted token from wallet A to wallet B. The technical details are not difficult but (1) the dated documentation of older working libs and (2) rapidly changing Solana libraries lack clarity.
In this case, attempting to move a PRGC token. We know its address public key. Available source code always assumes the destination token address is newly generated (keypair.generate()), while in this trail the assumption is the destination wallet already exists:
let prgcMintPublicKey = "66edZnAPEJSxnAK4SckuupssXpbu5doV57FUcghaqPsY";
const mintPublicKey = new PublicKey(prgcMintPublicKey);
var mintToken = new Token(
connection,
mintPublicKey,
TOKEN_PROGRAM_ID,
sourceWallet
);
const fromTokenAccount = await mintToken.getOrCreateAssociatedAccountInfo(
sourceWallet.publicKey
);
var destWallet = new PublicKey("<some dest wallet>");
const account = await connection.getTokenAccountsByOwner(destWallet, {
mint: mintPublicKey
});
var destTokenAccount = account.value[0].pubkey; //needs index check, else create
let tx = new Transaction().add(
Token.createTransferInstruction(
TOKEN_PROGRAM_ID,
fromTokenAccount.address,
destTokenAccount,
sourceWallet.publicKey,
[],
amount
)
);
tx.feePayer = sourceWallet.publicKey;
const txhash = await sendAndConfirmTransaction(connection, tx, [sourceWallet]);
It takes bit of time to appreicate the “Token Account” sub-structure of Solana. In this case, we identify the destTokenAccountv directly by filtering on the mint public key.
Obviously, sourceWallet must have private key for signing. I’ll refactor to offload this key management into the Hashicorp Vault Solana Plugin extension with an API call for signing transaction.