Skip to content

Commit

Permalink
Support new preconditions when using building transactions (#513)
Browse files Browse the repository at this point in the history
Co-authored-by: George Kudrayvtsev <george@stellar.org>
  • Loading branch information
Paul Bellamy and Shaptic authored Apr 5, 2022
1 parent 317ceab commit e3e21b6
Show file tree
Hide file tree
Showing 7 changed files with 493 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
nodejs 12.6.0
nodejs 14.19.1
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

### Add

- Adds support for converting signed payloads ([CAP-40](https://stellar.org/protocol/cap-40)) to and from their StrKey (`P...`) representation ([#511](https://github.com/stellar/js-stellar-base/pull/511)).
- Support for converting signed payloads ([CAP-40](https://stellar.org/protocol/cap-40)) to and from their StrKey (`P...`) representation ([#511](https://github.com/stellar/js-stellar-base/pull/511)).

- Support for creating transactions with the new ([CAP-21](https://stellar.org/protocol/cap-21)) preconditions via `TransactionBuilder`. ([#513](https://github.com/stellar/js-stellar-base/pull/513)).

### Fix

Expand Down
84 changes: 79 additions & 5 deletions src/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export class Transaction extends TransactionBase {
break;
}

let cond = null;
let timeBounds = null;
switch (this._envelopeType) {
case xdr.EnvelopeType.envelopeTypeTxV0():
Expand All @@ -85,10 +86,8 @@ export class Transaction extends TransactionBase {
break;

case xdr.PreconditionType.precondV2():
timeBounds = tx
.cond()
.v2()
.timeBounds();
cond = tx.cond().v2();
timeBounds = cond.timeBounds();
break;

default:
Expand All @@ -107,6 +106,25 @@ export class Transaction extends TransactionBase {
};
}

if (cond) {
const ledgerBounds = cond.ledgerBounds();
if (ledgerBounds) {
this._ledgerBounds = {
minLedger: ledgerBounds.minLedger(),
maxLedger: ledgerBounds.maxLedger()
};
}

const minSeq = cond.minSeqNum();
if (minSeq) {
this._minAccountSequence = minSeq.toString();
}

this._minAccountSequenceAge = cond.minSeqAge();
this._minAccountSequenceLedgerGap = cond.minSeqLedgerGap();
this._extraSigners = cond.extraSigners();
}

const operations = tx.operations() || [];
this._operations = map(operations, (op) => Operation.fromXDRObject(op));
}
Expand All @@ -120,11 +138,67 @@ export class Transaction extends TransactionBase {
get timeBounds() {
return this._timeBounds;
}

set timeBounds(value) {
throw new Error('Transaction is immutable');
}

/**
* @type {object}
* @property {number} minLedger - smallest ledger bound (uint32)
* @property {number} maxLedger - largest ledger bound (or 0 for inf)
* @readonly
*/
get ledgerBounds() {
return this._ledgerBounds;
}
set ledgerBounds(value) {
throw new Error('Transaction is immutable');
}

/**
* @type {string} 64 bit account sequence
* @readonly
*/
get minAccountSequence() {
return this._minAccountSequence;
}
set minAccountSequence(value) {
throw new Error('Transaction is immutable');
}

/**
* @type {number} 64 bit number of seconds
* @readonly
*/
get minAccountSequenceAge() {
return this._minAccountSequenceAge;
}
set minAccountSequenceAge(value) {
throw new Error('Transaction is immutable');
}

/**
* @type {number} 32 bit number of ledgers
* @readonly
*/
get minAccountSequenceLedgerGap() {
return this._minAccountSequenceLedgerGap;
}
set minAccountSequenceLedgerGap(value) {
throw new Error('Transaction is immutable');
}

/**
* @type {xdr.SignerKey[]} array of extra signers
* @readonly
*/
get extraSigners() {
return this._extraSigners;
}
set extraSigners(value) {
throw new Error('Transaction is immutable');
}

/**
* @type {string}
* @readonly
Expand Down
Loading

0 comments on commit e3e21b6

Please sign in to comment.