-
Notifications
You must be signed in to change notification settings - Fork 136
/
fee_bump_transaction.js
112 lines (99 loc) · 3.65 KB
/
fee_bump_transaction.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import xdr from './generated/stellar-xdr_generated';
import { hash } from './hashing';
import { Transaction } from './transaction';
import { TransactionBase } from './transaction_base';
import { encodeMuxedAccountToAddress } from './util/decode_encode_muxed_account';
/**
* 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 - transaction envelope
* object or base64 encoded string.
* @param {string} networkPassphrase - passphrase of the target Stellar network
* (e.g. "Public Global Stellar Network ; September 2015").
* @param {bool} [opts.withMuxing] - indicates that the fee source of this
* transaction is a proper muxed account (i.e. coming from an M... address).
* By default, this option is disabled until muxed accounts are mature.
*
* @extends TransactionBase
*/
export class FeeBumpTransaction extends TransactionBase {
constructor(envelope, networkPassphrase, withMuxing) {
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._feeSource = encodeMuxedAccountToAddress(
this.tx.feeSource(),
withMuxing
);
this._innerTransaction = new Transaction(
innerTxEnvelope,
networkPassphrase
);
}
/**
* @type {Transaction}
* @readonly
*/
get innerTransaction() {
return this._innerTransaction;
}
/**
* @type {string}
* @readonly
*/
get feeSource() {
return this._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);
}
}