PHP WebShell

Текущая директория: /usr/lib/node_modules/bitgo-express/node_modules/ripple-lib/dist/npm/transaction

Просмотр файла: payment.js

'use strict'; // eslint-disable-line strict

var _ = require('lodash');
var utils = require('./utils');
var validate = utils.common.validate;
var toRippledAmount = utils.common.toRippledAmount;
var paymentFlags = utils.common.txFlags.Payment;
var ValidationError = utils.common.errors.ValidationError;


function isXRPToXRPPayment(payment) {
  var sourceCurrency = _.get(payment, 'source.maxAmount.currency', _.get(payment, 'source.amount.currency'));
  var destinationCurrency = _.get(payment, 'destination.amount.currency', _.get(payment, 'destination.minAmount.currency'));
  return sourceCurrency === 'XRP' && destinationCurrency === 'XRP';
}

function isIOUWithoutCounterparty(amount) {
  return amount && amount.currency !== 'XRP' && amount.counterparty === undefined;
}

function applyAnyCounterpartyEncoding(payment) {
  // Convert blank counterparty to sender or receiver's address
  //   (Ripple convention for 'any counterparty')
  // https://ripple.com/build/transactions/
  //    #special-issuer-values-for-sendmax-and-amount
  // https://ripple.com/build/ripple-rest/#counterparties-in-payments
  _.forEach([payment.source, payment.destination], function (adjustment) {
    _.forEach(['amount', 'minAmount', 'maxAmount'], function (key) {
      if (isIOUWithoutCounterparty(adjustment[key])) {
        adjustment[key].counterparty = adjustment.address;
      }
    });
  });
}

function createMaximalAmount(amount) {
  var maxXRPValue = '100000000000';
  var maxIOUValue = '9999999999999999e80';
  var maxValue = amount.currency === 'XRP' ? maxXRPValue : maxIOUValue;
  return _.assign({}, amount, { value: maxValue });
}

function createPaymentTransaction(address, paymentArgument) {
  var payment = _.cloneDeep(paymentArgument);
  applyAnyCounterpartyEncoding(payment);

  if (address !== payment.source.address) {
    throw new ValidationError('address must match payment.source.address');
  }

  if (payment.source.maxAmount && payment.destination.minAmount || payment.source.amount && payment.destination.amount) {
    throw new ValidationError('payment must specify either (source.maxAmount ' + 'and destination.amount) or (source.amount and destination.minAmount)');
  }

  // when using destination.minAmount, rippled still requires that we set
  // a destination amount in addition to DeliverMin. the destination amount
  // is interpreted as the maximum amount to send. we want to be sure to
  // send the whole source amount, so we set the destination amount to the
  // maximum possible amount. otherwise it's possible that the destination
  // cap could be hit before the source cap.
  var amount = payment.destination.minAmount && !isXRPToXRPPayment(payment) ? createMaximalAmount(payment.destination.minAmount) : payment.destination.amount || payment.destination.minAmount;

  var txJSON = {
    TransactionType: 'Payment',
    Account: payment.source.address,
    Destination: payment.destination.address,
    Amount: toRippledAmount(amount),
    Flags: 0
  };

  if (payment.invoiceID !== undefined) {
    txJSON.InvoiceID = payment.invoiceID;
  }
  if (payment.source.tag !== undefined) {
    txJSON.SourceTag = payment.source.tag;
  }
  if (payment.destination.tag !== undefined) {
    txJSON.DestinationTag = payment.destination.tag;
  }
  if (payment.memos !== undefined) {
    txJSON.Memos = _.map(payment.memos, utils.convertMemo);
  }
  if (payment.noDirectRipple === true) {
    txJSON.Flags |= paymentFlags.NoRippleDirect;
  }
  if (payment.limitQuality === true) {
    txJSON.Flags |= paymentFlags.LimitQuality;
  }
  if (!isXRPToXRPPayment(payment)) {
    // Don't set SendMax for XRP->XRP payment
    // temREDUNDANT_SEND_MAX removed in:
    // https://github.com/ripple/rippled/commit/
    //  c522ffa6db2648f1d8a987843e7feabf1a0b7de8/
    if (payment.allowPartialPayment === true || payment.destination.minAmount !== undefined) {
      txJSON.Flags |= paymentFlags.PartialPayment;
    }

    txJSON.SendMax = toRippledAmount(payment.source.maxAmount || payment.source.amount);

    if (payment.destination.minAmount !== undefined) {
      txJSON.DeliverMin = toRippledAmount(payment.destination.minAmount);
    }

    if (payment.paths !== undefined) {
      txJSON.Paths = JSON.parse(payment.paths);
    }
  } else if (payment.allowPartialPayment === true) {
    throw new ValidationError('XRP to XRP payments cannot be partial payments');
  }

  return txJSON;
}

function preparePayment(address, payment) {
  var instructions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};

  validate.preparePayment({ address: address, payment: payment, instructions: instructions });
  var txJSON = createPaymentTransaction(address, payment);
  return utils.prepareTransaction(txJSON, this, instructions);
}

module.exports = preparePayment;

Выполнить команду


Для локальной разработки. Не используйте в интернете!