import { ec } from "starknet";
// Replace these with your actual values
const walletPrivateKey = "YOUR_PRIVATE_KEY";
const ADAMIK_API_KEY = "your-adamik-api-key"; // get it from https://dashboard.adamik.io
const recipientAddress = "RECIPIENT_ADDRESS";
async function main() {
// Generate public key from private key
const pubKey = ec.starkCurve.getStarkKey(walletPrivateKey);
// First, let's get our wallet address
const requestBodyAddressEncode = {
pubkey: pubKey,
};
// Fetch the wallet address from Adamik API
const responseAddressEncode = await fetch(
"https://api-staging.adamik.io/api/starknet/address/encode",
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: ADAMIK_API_KEY,
},
body: JSON.stringify(requestBodyAddressEncode),
}
);
const addressEncode = await responseAddressEncode.json();
// Get the ArgentX wallet address
const senderAddress = addressEncode.addresses.find(
(address) => address.type === "argentx"
).address;
// Prepare the transfer transaction
const requestBody = {
transaction: {
data: {
mode: "transfer", // Transaction type
senderAddress, // Our wallet address
recipientAddress, // Where we're sending to
amount: "200000000000000000", // Amount in wei (0.2 STRK in this example)
},
},
};
// Encode the transaction with Adamik API
const response = await fetch(
"https://api-staging.adamik.io/api/starknet/transaction/encode",
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: ADAMIK_API_KEY,
},
body: JSON.stringify(requestBody),
}
);
const encodedData = await response.json();
// Check for encoding errors
if (encodedData.status.errors.length > 0) {
throw new Error(encodedData.status.errors[0].message);
}
// Sign the encoded transaction using StarkNet curve
const signature = ec.starkCurve.sign(
encodedData.transaction.encoded,
walletPrivateKey
);
const signatureHex = signature.toDERHex();
// Prepare the broadcast request
const broadcastTransactionBody = {
transaction: {
...encodedData.transaction,
signature: signatureHex,
},
};
// Broadcast the signed transaction
const broadcastResponse = await fetch(
"https://api.adamik.io/api/starknet/transaction/broadcast",
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: ADAMIK_API_KEY,
},
body: JSON.stringify(broadcastTransactionBody),
}
);
const responseData = await broadcastResponse.json();
console.log("Transaction Result:", JSON.stringify(responseData, null, 2));
}
main();