Skip to content

Commit

Permalink
Add FeeBumpTransaction class (#328)
Browse files Browse the repository at this point in the history
* Handle transaction envelope with MuxedAccount in sourceAccount.

* Add FeeBumpTransaction.

* Add more test for FeeBumpTransaction.

* Improve docs.

* Add TransactionBase and extend from it.

* Use named import and inherit documentation.

* Inherit TransactionBase in Transaction.

* Document readonly attrs.

* Document properties on fee bump tx.

* Show M accounts.

* Return a copy of tx and signatures when building toEnvelope.

* Add helper function fromXDR.

* Update types.

* Update XDR.

* Handle muxed accoutns in fee bump transactions.

* Add link to fromXDR in Transaction and FeeBumpTransaction docs.

* Extend fromXDR to take a xdr object or a string.
  • Loading branch information
abuiles committed Apr 17, 2020
1 parent c915c14 commit ce7907e
Show file tree
Hide file tree
Showing 13 changed files with 960 additions and 406 deletions.
102 changes: 102 additions & 0 deletions src/fee_bump_transaction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import xdr from './generated/stellar-xdr_generated';
import { hash } from './hashing';

import { StrKey } from './strkey';
import { Transaction } from './transaction';
import { TransactionBase } from './transaction_base';

/**
* Use {@link TransactionBuilder.buildFeeBumpTransaction} to build a
* FeeBumpTransaction object. If you have an object or base64-encoded string of
* the transaction envelope XDR use {@link TransactionBuilder.fromXDR}.
*
* Once a {@link FeeBumpTransaction} has been created, its attributes and operations
* should not be changed. You should only add signatures (using {@link FeeBumpTransaction#sign}) before
* submitting to the network or forwarding on to additional signers.
* @param {string|xdr.TransactionEnvelope} envelope - The transaction envelope object or base64 encoded string.
* @param {string} networkPassphrase passphrase of the target stellar network (e.g. "Public Global Stellar Network ; September 2015").
* @extends TransactionBase
*/
export class FeeBumpTransaction extends TransactionBase {
constructor(envelope, networkPassphrase) {
if (typeof envelope === 'string') {
const buffer = Buffer.from(envelope, 'base64');
envelope = xdr.TransactionEnvelope.fromXDR(buffer);
}

const envelopeType = envelope.switch();
if (envelopeType !== xdr.EnvelopeType.envelopeTypeTxFeeBump()) {
throw new Error(
`Invalid TransactionEnvelope: expected an envelopeTypeTxFeeBump but received an ${envelopeType.name}.`
);
}

const txEnvelope = envelope.value();
const tx = txEnvelope.tx();
const fee = tx.fee().toString();
// clone signatures
const signatures = (txEnvelope.signatures() || []).slice();

super(tx, signatures, fee, networkPassphrase);

const innerTxEnvelope = xdr.TransactionEnvelope.envelopeTypeTx(
tx.innerTx().v1()
);

this._innerTransaction = new Transaction(
innerTxEnvelope,
networkPassphrase
);
}

/**
* @type {Transaction}
* @readonly
*/
get innerTransaction() {
return this._innerTransaction;
}

/**
* @type {string}
* @readonly
*/
get feeSource() {
return this._muxedToString(this.tx.feeSource());
}

/**
* Returns the "signature base" of this transaction, which is the value
* that, when hashed, should be signed to create a signature that
* validators on the Stellar Network will accept.
*
* It is composed of a 4 prefix bytes followed by the xdr-encoded form
* of this transaction.
* @returns {Buffer}
*/
signatureBase() {
const taggedTransaction = new xdr.TransactionSignaturePayloadTaggedTransaction.envelopeTypeTxFeeBump(
this.tx
);

const txSignature = new xdr.TransactionSignaturePayload({
networkId: xdr.Hash.fromXDR(hash(this.networkPassphrase)),
taggedTransaction
});

return txSignature.toXDR();
}

/**
* To envelope returns a xdr.TransactionEnvelope which can be submitted to the network.
* @returns {xdr.TransactionEnvelope}
*/
toEnvelope() {
const envelope = new xdr.FeeBumpTransactionEnvelope({
tx: xdr.FeeBumpTransaction.fromXDR(this.tx.toXDR()), // make a copy of the tx
signatures: this.signatures.slice() // make a copy of the signatures
});

return new xdr.TransactionEnvelope.envelopeTypeTxFeeBump(envelope);
}
}
6 changes: 3 additions & 3 deletions src/generated/stellar-xdr_generated.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Automatically generated on 2020-04-14T16:01:24-05:00
// Automatically generated on 2020-04-16T16:15:58-05:00
// DO NOT EDIT or your changes may be overwritten

/* jshint maxstatements:2147483647 */
Expand Down Expand Up @@ -1481,7 +1481,7 @@ xdr.union("FeeBumpTransactionExt", {
//
// struct FeeBumpTransaction
// {
// AccountID feeSource;
// MuxedAccount feeSource;
// int64 fee;
// union switch (EnvelopeType type)
// {
Expand All @@ -1499,7 +1499,7 @@ xdr.union("FeeBumpTransactionExt", {
//
// ===========================================================================
xdr.struct("FeeBumpTransaction", [
["feeSource", xdr.lookup("AccountId")],
["feeSource", xdr.lookup("MuxedAccount")],
["fee", xdr.lookup("Int64")],
["innerTx", xdr.lookup("FeeBumpTransactionInnerTx")],
["ext", xdr.lookup("FeeBumpTransactionExt")],
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ export { hash } from './hashing';
export { sign, verify, FastSigning } from './signing';
export { Keypair } from './keypair';
export { UnsignedHyper, Hyper } from 'js-xdr';
export { TransactionBase } from './transaction_base';
export { Transaction } from './transaction';
export { FeeBumpTransaction } from './fee_bump_transaction';
export {
TransactionBuilder,
TimeoutInfinite,
Expand Down
Loading

0 comments on commit ce7907e

Please sign in to comment.