const { Keypair } = require("@stellar/stellar-sdk");
// Replace with your wallet's secret key
const secretKey = "your-stellar-secret-key-here";
// Replace with your Adamik API key from https://dashboard.adamik.io
const ADAMIK_API_KEY = "your-adamik-api-key";
// Replace with the recipient's address
const recipientAddress = "GCKFBEIYV2U22IO2BJ4KVJOIP7XPWQGQFKKWXR6DOSJBV7STMAQSMTMA";
async function main() {
try {
// Create wallet from secret key
const sourceKeypair = Keypair.fromSecret(secretKey);
const sourcePublicKey = sourceKeypair.publicKey();
// Prepare the transaction request for Adamik API
const requestBody = {
transaction: {
data: {
chainId: "stellar", // Target Stellar mainnet (use "stellar-testnet" for testnet)
mode: "transfer",
senderAddress: sourcePublicKey,
recipientAddress: recipientAddress || sourcePublicKey, // Self-send if no recipient
amount: "10000000", // Transaction amount in stroops (1 XLM = 10^7 stroops)
useMaxAmount: false,
fees: "0",
gas: "0",
memo: "Adamik Transfer",
format: "json",
validatorAddress: "",
},
},
};
// Encode the transaction with Adamik API
const responseEncode = await fetch(
"https://api.adamik.io/api/stellar/transaction/encode",
{
method: "POST",
headers: {
Authorization: ADAMIK_API_KEY,
"Content-Type": "application/json",
},
body: JSON.stringify(requestBody),
}
);
const encodedData = await responseEncode.json();
if (!responseEncode.ok) {
console.error("Encode failed:", encodedData);
return;
}
// Sign the transaction hash
const hashToSign = encodedData.transaction.encoded[0].hash.value;
const hashBuffer = Buffer.from(hashToSign, 'hex');
const signature = sourceKeypair.sign(hashBuffer);
const signatureHex = signature.toString('hex');
// Prepare to broadcast the signed transaction
const sendTransactionBody = {
transaction: {
data: encodedData.transaction.data,
encoded: encodedData.transaction.encoded,
signature: signatureHex,
},
};
// Broadcast the transaction using Adamik API
const responseBroadcast = await fetch(
"https://api.adamik.io/api/stellar/transaction/broadcast",
{
method: "POST",
headers: {
Authorization: ADAMIK_API_KEY,
"Content-Type": "application/json",
},
body: JSON.stringify(sendTransactionBody),
}
);
const responseData = await responseBroadcast.json();
console.log("Transaction hash:", responseData.hash);
} catch (error) {
console.error("Error:", error);
}
}
main();