import { Connection, PublicKey } from "@solana/web3.js";
import {
TsnHttpClient,
buildPaymentAuthorizationIntentRequest,
createTcapAssetPaymentAuthorization,
createFundingNoteV1,
type CreateIntentRequest,
type PublicTsnMempoolIntent,
} from "@trustlink/tsn-sdk";
const fundingNote = createFundingNoteV1();
const authorization = await createTcapAssetPaymentAuthorization({
connection: new Connection(rpcUrl, "confirmed"),
depositor: new PublicKey(senderWallet),
mint: new PublicKey(tokenMintAddress),
senderWallet,
senderIdentity: senderWallet,
receiverIdentity: `tin:${recipientTin}`,
recipientDisplayName: recipient.displayName,
recipientDisplayNameSource: recipient.displayName.trim() ? "owner-provided" : "not provided",
amount,
senderFeeAmount,
totalTokenRequiredUi,
fundingMode: "tcap_funded_note_v1",
fundingNoteCommitment: fundingNote.commitment,
});
const signature = await wallet.signMessage(
new TextEncoder().encode(authorization.message),
);
const request = buildPaymentAuthorizationIntentRequest({
paymentId,
recipientHash: recipient.ownerPubkeyHash,
recipientTin,
tokenMintAddress: authorization.asset.mint,
tokenDecimals: authorization.asset.decimals,
senderWallet,
senderAuthorizationMessage: authorization.message,
senderAuthorizationSignature: Buffer.from(signature).toString("base64"),
senderAuthorizationNonce: authorization.nonce,
senderAuthorizationIssuedAt: authorization.issuedAt,
senderAuthorizationExpiresAt: authorization.expiresAt,
senderFeeAmount,
senderSignedFundingTransaction,
senderFundingMode: "tcap_funded_note_v1",
fundingNote,
amount,
});
const client = new TsnHttpClient({ baseUrl: (process.env.TSN_RECEIVER_URL! + "/node") });
const intent = await client.postIntent<CreateIntentRequest, PublicTsnMempoolIntent>(request);