Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 36 additions & 9 deletions lib/stellar.ts
Original file line number Diff line number Diff line change
@@ -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();
};

/// <reference types="node" />

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();
};