UNPKG

@ledgerhq/coin-stellar

Version:
125 lines 5.6 kB
"use strict"; // SPDX-FileCopyrightText: © 2026 LEDGER SAS // SPDX-License-Identifier: Apache-2.0 var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createApi = createApi; exports.envelopeFromAnyXDR = envelopeFromAnyXDR; const rejectBalanceOptions_1 = require("@ledgerhq/coin-module-framework/api/getBalance/rejectBalanceOptions"); const craftTransactionData_1 = require("@ledgerhq/coin-module-framework/logic/craftTransactionData"); const stellar_sdk_1 = require("@stellar/stellar-sdk"); const config_1 = __importDefault(require("../config")); const logic_1 = require("../logic"); const logic_2 = require("../logic"); const validateAddress_1 = require("../logic/validateAddress"); const network_1 = require("../network"); function createApi(config) { config_1.default.setCoinConfig(() => ({ ...config, status: { type: 'active' } })); (0, network_1.registerHorizonInterceptors)(); return { broadcast: logic_1.broadcast, combine: compose, craftTransaction: craft, craftRawTransaction: (_transaction, _sender, _publicKey, _sequence) => { throw new Error('craftRawTransaction is not supported'); }, estimateFees: estimate, getBalance: (address, options) => (0, rejectBalanceOptions_1.rejectBalanceOptions)(() => (0, logic_1.getBalance)(address), options), lastBlock: logic_1.lastBlock, listOperations: operations, getBlock: logic_1.getBlock, getBlockInfo: logic_1.getBlockInfo, getStakes(_address, _cursor) { throw new Error('getStakes is not supported'); }, getRewards(_address, _cursor) { throw new Error('getRewards is not supported'); }, validateIntent: logic_1.validateIntent, getNextSequence: async (address) => { const sequence = await (0, network_1.fetchSequence)(address); return BigInt(sequence.plus(1).toFixed()); }, getValidators(_cursor) { throw new Error('getValidators is not supported'); }, validateAddress: validateAddress_1.validateAddress, craftTransactionData: craftTransactionData_1.craftTransactionData, }; } async function craft(transactionIntent, customFees) { const fees = customFees?.value || (await (0, logic_1.estimateFees)()); // NOTE: check how many memos, throw if more than one? // if (transactionIntent.memos && transactionIntent.memos.length > 1) { // throw new Error("Stellar only supports one memo per transaction."); // } const memo = 'memo' in transactionIntent ? transactionIntent.memo : undefined; const hasMemoValue = memo && memo.type !== 'NO_MEMO'; const tx = await (0, logic_1.craftTransaction)({ address: transactionIntent.sender }, { type: transactionIntent.type, recipient: transactionIntent.recipient, amount: transactionIntent.amount, fee: fees, ...(transactionIntent.asset.type !== 'native' && 'assetReference' in transactionIntent.asset ? { assetCode: transactionIntent.asset.assetReference, assetIssuer: transactionIntent.asset.assetOwner, } : {}), memoType: memo?.type, ...(hasMemoValue ? { memoValue: memo.value } : {}), }); // Note: the API returns the signature base, not the full XDR, see BACK-8727 for more context return { transaction: tx.signatureBase }; } function compose(tx, signature, pubkey) { if (!pubkey) { throw new Error('Missing pubkey'); } // note: accept here `TransactionEnvelope` or `TransactionSignaturePayload`, see BACK-8727 for more context return (0, logic_1.combine)(envelopeFromAnyXDR(tx, 'base64'), signature, pubkey); } async function estimate(_transactionIntent) { const value = await (0, logic_1.estimateFees)(); return { value }; } async function operations(address, options) { const { items, next } = await (0, logic_2.listOperations)(address, options); return { items, next: next || undefined }; } /** * Deserialize a transaction envelope, also accepting transaction signature payload form. * * @param input serialized `TransactionEnvelope` or `TransactionSignaturePayload` * @param format serialization encoding */ function envelopeFromAnyXDR(input, format) { try { return stellar_sdk_1.xdr.TransactionEnvelope.fromXDR(input, format); } catch (envelopeError) { try { return signatureBaseToEnvelope(stellar_sdk_1.xdr.TransactionSignaturePayload.fromXDR(input, format)); } catch (signatureBaseError) { throw new Error(`Failed decoding transaction as an envelope (${envelopeError}) or as a signature base: (${signatureBaseError})`); } } } /** * Convert a `TransactionSignaturePayload` into a `TransactionEnvelope`. * * @param signatureBase deserialized `TransactionSignaturePayload` */ function signatureBaseToEnvelope(signatureBase) { const tx = signatureBase.taggedTransaction().value(); if (tx instanceof stellar_sdk_1.xdr.Transaction) { return stellar_sdk_1.xdr.TransactionEnvelope.envelopeTypeTx(new stellar_sdk_1.xdr.TransactionV1Envelope({ tx, signatures: [] })); } else { return stellar_sdk_1.xdr.TransactionEnvelope.envelopeTypeTxFeeBump(new stellar_sdk_1.xdr.FeeBumpTransactionEnvelope({ tx, signatures: [] })); } } //# sourceMappingURL=index.js.map