Submit Signature
Summary:
This script signs a scheduled transaction on the Hedera network using a specified private key. It demonstrates how to use the ScheduleSignTransaction to create a transaction for signing a scheduled transaction, sign it with a specified private key, execute it on the Hedera network, and retrieve the transaction status to confirm the signing operation.
Step 1: Imports and Environment Setup
const {
ScheduleSignTransaction,
Client,
PrivateKey,
ScheduleInfoQuery
} = require("@hashgraph/sdk");
require('dotenv').config({ path: 'Scheduled_TX/.env' });The script imports necessary modules from the Hedera JavaScript SDK (
@hashgraph/sdk):ScheduleSignTransaction,Client,PrivateKey, andScheduleInfoQuery.It also imports the
dotenvmodule to load environment variables from a.envfile located in theScheduled_TXdirectory.
Step 2: Environment Variables Retrieval and Validation
const myAccountId = process.env.MY_ACCOUNT_ID;
const myPrivateKey = PrivateKey.fromString(process.env.MY_PRIVATE_KEY);
const otherAccountId = process.env.OTHER_ACCOUNT_ID;
const otherPrivateKey = PrivateKey.fromString(process.env.OTHER_PRIVATE_KEY);
const scheduleId = process.env.SCHEDULE_ID;
if (myAccountId == null ||
myPrivateKey == null ) {
throw new Error("Environment variables myAccountId and myPrivateKey must be present");
}The code retrieves necessary environment variables such as account IDs and private keys from the
.envfile.It validates that the required environment variables (
MY_ACCOUNT_IDandMY_PRIVATE_KEY) are present. If not, it throws an error.
Step 3: Client Setup
const client = Client.forTestnet();
client.setOperator(myAccountId, myPrivateKey);It creates a client instance for the Hedera testnet and sets the operator account using the user's account ID and private key.
Step 4: Main Function
async function main() {
const transaction = await new ScheduleSignTransaction()
.setScheduleId(scheduleId)
.freezeWith(client)
.sign(otherPrivateKey);
const txResponse = await transaction.execute(client);
const receipt = await txResponse.getReceipt(client);
const transactionStatus = receipt.status;
console.log("The transaction consensus status is " +transactionStatus);
process.exit();
}This is the main function named
main().It creates a
ScheduleSignTransactioninstance to sign the scheduled transaction using the provided schedule ID and the specified private key (otherPrivateKey).The transaction is frozen with the client, signed with the specified private key (
otherPrivateKey), and executed using the client.The script then retrieves the receipt of the transaction to obtain its status, which indicates the consensus status of the signing operation.
The transaction consensus status is logged to the console.
Step 5: Execution
main();This line calls the
main()function to start the execution of the script.
Last updated
Was this helpful?