Provides a sign transaction function for users to replace the sign function of the steller SDK.
npm install @57block/stellar-autoaction-sdk
The following example demonstrates signing a transaction using the stellar autoaction-sdk on the Stellar blockchain.
const {
Horizon,
Networks,
Asset,
Operation,
TransactionBuilder,
Keypair,
xdr,
} = require("@stellar/stellar-sdk");
const { sign } = require("@57block/stellar-autoaction-sdk");
const submitTx = async (
sourcePublicKey,
destinationPublicKey,
account,
org
) => {
// Configure the server to the Horizon instance hosted by Stellar.org
// To use the live network, set the hostname to 'horizon.stellar.org'
const server = new Horizon.Server('https://horizon-testnet.stellar.org');
console.log("Building transaction...");
// Transactions require a valid sequence number that is specific to this account.
// We can fetch the current sequence number for the source account from Horizon.
const sourceAccount = await server.loadAccount(sourcePublicKey);
// Right now, there's one function that fetches the base fee.
const fee = await server.fetchBaseFee();
const tx = new TransactionBuilder(sourceAccount, {
fee,
// Uncomment the following line to build transactions for the live network. Be
// sure to also change the horizon hostname.
// networkPassphrase: StellarSdk.Networks.PUBLIC,
networkPassphrase: Networks.TESTNET
})
// Add a payment operation to the transaction
.addOperation(Operation.payment({
destination: destinationPublicKey,
// The term native asset refers to lumens
asset: Asset.native(),
amount: '10',
}))
// Make this transaction valid for the next 30 seconds only
.setTimeout(30)
// Uncomment to add a memo (https://developers.stellar.org/docs/glossary/transactions/)
// .addMemo(Memo.text('Hello world!'))
.build();
console.log("Signing transaction...");
// get the signature from sdk sign server
const signature = await sign({
txHash: tx.hash().toString('base64'),
sourcePublicKey,
account,
org,
});
// use the signature to sign the transaction
tx.addDecoratedSignature(
new xdr.DecoratedSignature({
signature,
hint: Keypair.fromPublicKey(sourcePublicKey).signatureHint(),
})
);
console.log("Submiting transaction...");
// Submit the transaction to the Horizon server. The Horizon server will then
// submit the transaction into the network for us.
const transactionResult = await server.submitTransaction(tx);
console.log(`Transaction successful! Tx hash: ${transactionResult.hash}`);
};