UNPKG

@ledgerhq/coin-aptos

Version:
92 lines 3.59 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getAddress = void 0; const rxjs_1 = require("rxjs"); const operation_1 = require("@ledgerhq/coin-framework/operation"); const bignumber_js_1 = __importDefault(require("bignumber.js")); const network_1 = require("../network"); const network_2 = require("../network"); const helpers_1 = require("@ledgerhq/coin-framework/account/helpers"); const buildTransaction_1 = __importDefault(require("../logic/buildTransaction")); const getAddress = (a) => ({ address: a.freshAddress, derivationPath: a.freshAddressPath, }); exports.getAddress = getAddress; const getOperationType = (transaction) => { switch (transaction.mode) { case "stake": return "STAKE"; case "restake": return "STAKE"; case "unstake": return "UNSTAKE"; case "withdraw": return "WITHDRAW"; default: return "OUT"; } }; const buildSignOperation = (signerContext) => ({ account, transaction, deviceId }) => new rxjs_1.Observable(o => { async function main() { o.next({ type: "device-signature-requested" }); const aptosClient = new network_1.AptosAPI(account.currency.id); const rawTx = await (0, buildTransaction_1.default)(account, transaction, aptosClient); const txBytes = await (0, network_2.signTransaction)(signerContext, account, deviceId, rawTx); const signature = Buffer.from(txBytes).toString("hex"); o.next({ type: "device-signature-granted" }); const accountId = account.id; const hash = ""; const type = getOperationType(transaction); const fee = transaction.fees || new bignumber_js_1.default(0); const extra = {}; const senders = []; const recipients = []; senders.push(account.freshAddress); recipients.push(transaction.recipient); const subAccount = !!transaction.subAccountId && (0, helpers_1.findSubAccountById)(account, transaction.subAccountId); // build optimistic operation const operation = { id: (0, operation_1.encodeOperationId)(accountId, hash, type), hash, type, value: subAccount ? fee : transaction.amount.plus(fee), fee, extra, blockHash: null, blockHeight: null, senders, recipients, accountId, date: new Date(), transactionSequenceNumber: Number(rawTx.sequence_number), subOperations: subAccount ? [ { id: (0, operation_1.encodeOperationId)(subAccount.id, "", "OUT"), type: "OUT", accountId: transaction.subAccountId, senders: [account.freshAddress], recipients: [transaction.recipient], value: transaction.amount, fee, date: new Date(), }, ] : [], }; o.next({ type: "signed", signedOperation: { operation, signature, }, }); } main().then(() => o.complete(), e => o.error(e)); }); exports.default = buildSignOperation; //# sourceMappingURL=signOperation.js.map