@ledgerhq/coin-tron
Version:
Ledger Tron Coin integration
77 lines • 3.75 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildSignOperation = void 0;
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const rxjs_1 = require("rxjs");
const network_1 = require("../network");
const buildOptimisticOperation_1 = require("./buildOptimisticOperation");
const getEstimateFees_1 = __importDefault(require("./getEstimateFees"));
const buildSignOperation = (signerContext) => ({ account, transaction, deviceId }) => new rxjs_1.Observable(o => {
async function main() {
const subAccount = transaction.subAccountId && account.subAccounts
? account.subAccounts.find(sa => sa.id === transaction.subAccountId)
: undefined;
const fee = await (0, getEstimateFees_1.default)(account, transaction, subAccount);
const balance = subAccount
? subAccount.balance
: bignumber_js_1.default.max(0, account.spendableBalance.minus(fee));
if (transaction.useAllAmount) {
transaction = { ...transaction }; // transaction object must not be mutated
transaction.amount = balance; // force the amount to be the max
}
const { raw_data_hex: rawDataHex, raw_data: rawData, txID: hash, } = await prepareTransactionForSignature(account, subAccount, transaction);
o.next({
type: "device-signature-requested",
});
const txArg = {
rawDataHex,
// only for trc10, we need to put the token ledger signature
tokenSignature: subAccount &&
subAccount.type === "TokenAccount" &&
subAccount.token.id.includes("trc10")
? subAccount.token.ledgerSignature
: undefined,
};
// Sign by device
const signature = await signerContext(deviceId, signer => signer.sign(account.freshAddressPath, txArg.rawDataHex ?? "", //FIXME: why rawDataHex could be undefined? This shouldn't be the case.
txArg.tokenSignature ? [txArg.tokenSignature] : []));
o.next({
type: "device-signature-granted",
});
const operation = (0, buildOptimisticOperation_1.buildOptimisticOperation)(account, subAccount, transaction, fee, hash);
o.next({
type: "signed",
signedOperation: {
operation,
signature,
rawData,
},
});
}
main().then(() => o.complete(), e => o.error(e));
});
exports.buildSignOperation = buildSignOperation;
const prepareTransactionForSignature = (account, subAccount, transaction) => {
switch (transaction.mode) {
case "freeze":
return (0, network_1.freezeTronTransaction)(account, transaction);
case "unfreeze":
return (0, network_1.unfreezeTronTransaction)(account, transaction);
case "vote":
return (0, network_1.voteTronSuperRepresentatives)(account, transaction);
case "claimReward":
return (0, network_1.claimRewardTronTransaction)(account);
case "withdrawExpireUnfreeze":
return (0, network_1.withdrawExpireUnfreezeTronTransaction)(account, transaction);
case "unDelegateResource":
return (0, network_1.unDelegateResourceTransaction)(account, transaction);
case "legacyUnfreeze":
return (0, network_1.legacyUnfreezeTronTransaction)(account, transaction);
default:
return (0, network_1.createTronTransaction)(account, transaction, subAccount);
}
};
//# sourceMappingURL=signOperation.js.map