From b42ce08b4c8ad79ed4e689e3f601cdf8cd9a0e97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Feyzanur=20ATE=C5=9E?= <123100654+Feyzanur25@users.noreply.github.com> Date: Sun, 22 Mar 2026 21:10:56 +0300 Subject: [PATCH] Refactor signTransaction with error handling and comments Added error handling for missing parameters and improved documentation. --- lib/stellar.ts | 45 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/lib/stellar.ts b/lib/stellar.ts index 1d66b91d..784e36f6 100644 --- a/lib/stellar.ts +++ b/lib/stellar.ts @@ -1,9 +1,36 @@ -import { Keypair, TransactionBuilder } from "stellar-sdk"; - -export const signTransaction = (txXDR: string, networkPassphrase: string) => { - const keypair = Keypair.fromSecret(`${process.env.STELLAR_PRIVATE_KEY}`); - const transaction = TransactionBuilder.fromXDR(txXDR, networkPassphrase); - transaction.sign(keypair); - return transaction.toXDR(); - }; - \ No newline at end of file +/// + +import { Keypair, TransactionBuilder } from "@stellar/stellar-sdk"; + +/** + * Signs a Stellar transaction XDR string using the private key + * stored in environment variables. + */ +export const signTransaction = ( + txXDR: string, + networkPassphrase: string +): string => { + const secretKey = process.env.STELLAR_PRIVATE_KEY; + + if (!secretKey) { + throw new Error("Missing STELLAR_PRIVATE_KEY in environment variables"); + } + + if (!txXDR) { + throw new Error("txXDR parameter is required"); + } + + if (!networkPassphrase) { + throw new Error("networkPassphrase parameter is required"); + } + + const keypair = Keypair.fromSecret(secretKey); + const transaction = TransactionBuilder.fromXDR( + txXDR, + networkPassphrase + ); + + transaction.sign(keypair); + + return transaction.toXDR(); +};