Skip to main content

Sending a Transaction

This guide will walk you through the complete process of sending a transaction on the OP_CAT layer, from generating a private key to broadcasting the transaction.

Prerequisites

Before you begin, make sure you have the necessary packages installed:

npm install @opcat-labs/lambit

Step 1: Generate a Private Key

First, let's create a private key and corresponding address for the testnet:

import {
createWifSigner,
generatePrivateKey,
} from '@opcat-labs/lambit';

const { privateKeyWif, address } = await generatePrivateKey('testnet');
const signer = createWifSigner(privateKeyWif, 'testnet');
const publicKey = await signer.getPublicKey();

console.log('Generated wallet:');
console.log({
privateKeyWif,
publicKey,
address,
});

This code generates a new private key and address on the testnet. Save both the private key WIF and address for the next steps.

Step 2: Get Testnet Coins

After generating your address, you'll need some testnet coins to send transactions. Visit the OP_CAT Layer testnet faucet to receive testnet coins.

Step 3: Fetch UTXOs from the Network

To get unspent transaction outputs (UTXOs) from the OP_CAT layer node, we'll use the MempoolProvider API:

import { createTestnetProvider } from "@opcat-labs/lambit";

const provider = createTestnetProvider({ wif: WIF, network: "testnet" });
const utxo = await provider.getAddressUtxos("yourAddress")
console.log("utxo : ", utxo);

Replace 'yourAddress' with the address you generated in Step 1.

Step 4: Construct the Transaction

Now let's build a simple transaction with 1 input and 2 outputs using PSBT (Partially Signed Transaction) for better transaction management:

import { sendTestnetTx, createTestnetProvider } from "@opcat-labs/lambit";

const main = async () => {
const senderAddress = getWifAddress(WIF, "testnet");
const utxos = await provider.getAddressUtxos(senderAddress);

if (utxos.length === 0) {
console.log("No UTXOs found. Please send some testnet coins to your address first from the faucet.");
return;
}

const txid = await sendTestnetTx({
wif: WIF,
recipientAddress: senderAddress,
amountSatoshis: 1_000n,
feeRateSatVb: 2,
});

console.log("Transaction broadcasted successfully! TXID:", txid);
};

main().catch((error) => {
console.error(error instanceof Error ? error.message : error);
});

This creates a transaction that spends 1000 satoshis back to your own address with a 2 sat/vB fee rate and broadcast it.

Complete Example

Here's the complete code combining all steps:

Key Generation Script

// generatePrivateKey.ts
import {
createWifSigner,
generatePrivateKey,
} from '@opcat-labs/lambit';

const { privateKeyWif, address } = await generatePrivateKey('testnet');
const signer = createWifSigner(privateKeyWif, 'testnet');
const publicKey = await signer.getPublicKey();

console.log('Generated wallet:');
console.log({
privateKeyWif,
publicKey,
address,
});

Transaction Sending Script

// sendTx.ts
import {
createTestnetProvider,
getWifAddress,
createWifSigner,
sendTestnetTx,
} from "@opcat-labs/lambit";

const main = async () => {
const WIF = "your wif privateKey";
const signer = createWifSigner(WIF, "testnet");
export const provider = createTestnetProvider({ wif: WIF, network: "testnet" });
const senderAddress = getWifAddress(WIF, "testnet");
const utxos = await provider.getAddressUtxos(senderAddress);

if (utxos.length === 0) {
console.log("No UTXOs found. Please send some testnet coins to your address first from the faucet.");
return;
}

const txid = await sendTestnetTx({
wif: WIF,
recipientAddress: senderAddress,
amountSatoshis: 1_000n,
feeRateSatVb: 2,
});

console.log("Transaction broadcasted successfully! TXID:", txid);
};

main().catch((error) => {
console.error(error instanceof Error ? error.message : error);
});

Important Notes

  • Always use testnet for development and testing
  • Keep your private keys secure and never share them
  • Make sure you have sufficient UTXOs before attempting to send transactions
  • The fee rate (2 sat/byte in this example) can be adjusted based on network conditions
  • Monitor your transaction on the OP_CAT testnet explorer after broadcasting