UNPKG

@ledgerhq/coin-canton

Version:
71 lines 3.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.buildSignOperation = void 0; const rxjs_1 = require("rxjs"); const errors_1 = require("@ledgerhq/errors"); const operation_1 = require("@ledgerhq/coin-framework/operation"); const common_logic_1 = require("../common-logic"); const buildSignOperation = (signerContext) => ({ account, deviceId, transaction }) => new rxjs_1.Observable(o => { async function main() { const { fee } = transaction; if (!fee) throw new errors_1.FeeNotLoaded(); try { // o observables allows to define steps of the signing process with the device o.next({ type: "device-signature-requested", }); const nextSequenceNumber = await (0, common_logic_1.getNextValidSequence)(account.freshAddress); const signature = await signerContext(deviceId, async (signer) => { const { freshAddressPath: derivationPath } = account; const { publicKey } = await signer.getAddress(derivationPath); const { nativeTransaction, serializedTransaction } = await (0, common_logic_1.craftTransaction)({ address: account.freshAddress, publicKey, }, { recipient: transaction.recipient, amount: transaction.amount, fee: fee, }); const transactionSignature = await signer.signTransaction(derivationPath, serializedTransaction); return (0, common_logic_1.combine)(serializedTransaction, transactionSignature, publicKey); }); o.next({ type: "device-signature-granted", }); // We create an optimistic operation here, the framework will then replace this transaction with the one returned by the indexer const hash = ""; const operation = { id: (0, operation_1.encodeOperationId)(account.id, hash, "OUT"), hash, accountId: account.id, type: "OUT", value: transaction.amount, fee, blockHash: null, blockHeight: null, senders: [account.freshAddress], recipients: [transaction.recipient], date: new Date(), transactionSequenceNumber: nextSequenceNumber, extra: {}, }; o.next({ type: "signed", signedOperation: { operation, signature, }, }); } catch (e) { if (e instanceof Error) { throw new Error(e?.data?.resultMessage); } throw e; } } main().then(() => o.complete(), e => o.error(e)); }); exports.buildSignOperation = buildSignOperation; //# sourceMappingURL=signOperation.js.map