fee_bump_transaction.js

  1. import xdr from './xdr';
  2. import { hash } from './hashing';
  3. import { Transaction } from './transaction';
  4. import { TransactionBase } from './transaction_base';
  5. import { encodeMuxedAccountToAddress } from './util/decode_encode_muxed_account';
  6. /**
  7. * Use {@link TransactionBuilder.buildFeeBumpTransaction} to build a
  8. * FeeBumpTransaction object. If you have an object or base64-encoded string of
  9. * the transaction envelope XDR use {@link TransactionBuilder.fromXDR}.
  10. *
  11. * Once a {@link FeeBumpTransaction} has been created, its attributes and operations
  12. * should not be changed. You should only add signatures (using {@link FeeBumpTransaction#sign}) before
  13. * submitting to the network or forwarding on to additional signers.
  14. *
  15. * @param {string|xdr.TransactionEnvelope} envelope - transaction envelope
  16. * object or base64 encoded string.
  17. * @param {string} networkPassphrase - passphrase of the target Stellar network
  18. * (e.g. "Public Global Stellar Network ; September 2015").
  19. *
  20. * @extends TransactionBase
  21. */
  22. export class FeeBumpTransaction extends TransactionBase {
  23. constructor(envelope, networkPassphrase) {
  24. if (typeof envelope === 'string') {
  25. const buffer = Buffer.from(envelope, 'base64');
  26. envelope = xdr.TransactionEnvelope.fromXDR(buffer);
  27. }
  28. const envelopeType = envelope.switch();
  29. if (envelopeType !== xdr.EnvelopeType.envelopeTypeTxFeeBump()) {
  30. throw new Error(
  31. `Invalid TransactionEnvelope: expected an envelopeTypeTxFeeBump but received an ${envelopeType.name}.`
  32. );
  33. }
  34. const txEnvelope = envelope.value();
  35. const tx = txEnvelope.tx();
  36. const fee = tx.fee().toString();
  37. // clone signatures
  38. const signatures = (txEnvelope.signatures() || []).slice();
  39. super(tx, signatures, fee, networkPassphrase);
  40. const innerTxEnvelope = xdr.TransactionEnvelope.envelopeTypeTx(
  41. tx.innerTx().v1()
  42. );
  43. this._feeSource = encodeMuxedAccountToAddress(this.tx.feeSource());
  44. this._innerTransaction = new Transaction(
  45. innerTxEnvelope,
  46. networkPassphrase
  47. );
  48. }
  49. /**
  50. * @type {Transaction}
  51. * @readonly
  52. */
  53. get innerTransaction() {
  54. return this._innerTransaction;
  55. }
  56. /**
  57. * @type {Operation[]}
  58. * @readonly
  59. */
  60. get operations() {
  61. return this._innerTransaction.operations;
  62. }
  63. /**
  64. * @type {string}
  65. * @readonly
  66. */
  67. get feeSource() {
  68. return this._feeSource;
  69. }
  70. /**
  71. * Returns the "signature base" of this transaction, which is the value
  72. * that, when hashed, should be signed to create a signature that
  73. * validators on the Stellar Network will accept.
  74. *
  75. * It is composed of a 4 prefix bytes followed by the xdr-encoded form
  76. * of this transaction.
  77. * @returns {Buffer}
  78. */
  79. signatureBase() {
  80. const taggedTransaction =
  81. new xdr.TransactionSignaturePayloadTaggedTransaction.envelopeTypeTxFeeBump(
  82. this.tx
  83. );
  84. const txSignature = new xdr.TransactionSignaturePayload({
  85. networkId: xdr.Hash.fromXDR(hash(this.networkPassphrase)),
  86. taggedTransaction
  87. });
  88. return txSignature.toXDR();
  89. }
  90. /**
  91. * To envelope returns a xdr.TransactionEnvelope which can be submitted to the network.
  92. * @returns {xdr.TransactionEnvelope}
  93. */
  94. toEnvelope() {
  95. const envelope = new xdr.FeeBumpTransactionEnvelope({
  96. tx: xdr.FeeBumpTransaction.fromXDR(this.tx.toXDR()), // make a copy of the tx
  97. signatures: this.signatures.slice() // make a copy of the signatures
  98. });
  99. return new xdr.TransactionEnvelope.envelopeTypeTxFeeBump(envelope);
  100. }
  101. }