const {Address, AddressPrefix, LedgerClientFactory} = require("@signumjs/core");
const {Amount, CurrencySymbol} = require("@signumjs/util");
const {generateMasterKeys} = require("@signumjs/crypto");
const LedgerHostUrls = {
TestNet: 'http://localhost:6876',
MainNet: 'https://europe.signum.network',
}
async function sendSigna(args) {
try {
const {ledgerType, recipient, amount, feeType, passphrase} = args
const ledger = LedgerClientFactory.create({
nodeHost: LedgerHostUrls[ledgerType])
}
const recipientId = Address.create(recipient).getNumericId()
console.info("Verifying recipient...")
await ledger.account.getAccount({accountId: recipientId})
const {publicKey, signPrivateKey} = generateMasterKeys(passphrase)
const suggestedFees = await ledger.network.getSuggestedFees()
const selectedFeePlanck = suggestedFees[feeType.toLowerCase()]
const {transaction} = await ledger.transaction.sendAmountToSingleRecipient({
recipientId,
amountPlanck: Amount.fromSigna(amount).getPlanck(),
feePlanck: selectedFeePlanck,
senderPrivateKey: signPrivateKey,
senderPublicKey: publicKey
});
const addressPrefix = ledgerType === 'MainNet' ? AddressPrefix.MainNet : AddressPrefix.TestNet
console.info(`Successfully sent ${amount} ${CurrencySymbol} to ${Address.create(recipientId, addressPrefix).getReedSolomonAddress()} - Transaction Id: ${transaction}`)
console.info(`You paid a total of ${Amount.fromSigna(amount).add(Amount.fromPlanck(selectedFeePlanck)).toString()}`)
} catch (e) {
handleError(e);
}
}
(async () => {
await sendSigna({
ledgerType: 'TestNet',
recipient: 'TS-QAJA-QW5Y-SWVP-4RVP4',
amount: 100.00,
feeType: 'cheap',
passphrase: 'YourVerySecretPassphraseWhichCannotBeRevealedHere'
});
})();