Skip to content

Commit

Permalink
Correct the data type of certain fields to store the expected design …
Browse files Browse the repository at this point in the history
…values. (#498)
  • Loading branch information
overcat authored Aug 8, 2023
1 parent 25dc8a8 commit 17180d9
Show file tree
Hide file tree
Showing 38 changed files with 587 additions and 240 deletions.
10 changes: 6 additions & 4 deletions src/main/java/org/stellar/sdk/AllowTrustOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,13 @@ org.stellar.sdk.xdr.Operation.OperationBody toOperationBody(AccountConverter acc
Uint32 flag = new Uint32();
// authorize
if (authorize) {
flag.setUint32(TrustLineFlags.AUTHORIZED_FLAG.getValue());
flag.setUint32(new XdrUnsignedInteger(TrustLineFlags.AUTHORIZED_FLAG.getValue()));
} else if (authorizeToMaintainLiabilities) {
flag.setUint32(TrustLineFlags.AUTHORIZED_TO_MAINTAIN_LIABILITIES_FLAG.getValue());
flag.setUint32(
new XdrUnsignedInteger(
TrustLineFlags.AUTHORIZED_TO_MAINTAIN_LIABILITIES_FLAG.getValue()));
} else {
flag.setUint32(0);
flag.setUint32(new XdrUnsignedInteger(0));
}
op.setAuthorize(flag);

Expand Down Expand Up @@ -110,7 +112,7 @@ public static class Builder {
throw new RuntimeException("Unknown asset code");
}

int flag = op.getAuthorize().getUint32().intValue();
int flag = op.getAuthorize().getUint32().getNumber().intValue();
if (flag == TrustLineFlags.AUTHORIZED_FLAG.getValue()) {
authorize = true;
authorizeToMaintainLiabilities = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.stellar.sdk.xdr.ExtensionPoint;
import org.stellar.sdk.xdr.OperationType;
import org.stellar.sdk.xdr.Uint32;
import org.stellar.sdk.xdr.XdrUnsignedInteger;

/**
* Represents <a
Expand All @@ -28,7 +29,7 @@ public class BumpFootprintExpirationOperation extends Operation {
* the number of ledgers past the LCL (last closed ledger) by which to extend the validity of the
* ledger keys in this transaction
*/
@NonNull Integer ledgersToExpire;
@NonNull Long ledgersToExpire;

/**
* Constructs a new BumpFootprintExpirationOperation object from the XDR representation of the
Expand All @@ -38,15 +39,15 @@ public class BumpFootprintExpirationOperation extends Operation {
*/
public static BumpFootprintExpirationOperation fromXdr(BumpFootprintExpirationOp op) {
return BumpFootprintExpirationOperation.builder()
.ledgersToExpire(op.getLedgersToExpire().getUint32())
.ledgersToExpire(op.getLedgersToExpire().getUint32().getNumber())
.build();
}

@Override
org.stellar.sdk.xdr.Operation.OperationBody toOperationBody(AccountConverter accountConverter) {
BumpFootprintExpirationOp op = new BumpFootprintExpirationOp();
op.setExt(new ExtensionPoint.Builder().discriminant(0).build());
op.setLedgersToExpire(new Uint32(ledgersToExpire));
op.setLedgersToExpire(new Uint32(new XdrUnsignedInteger(ledgersToExpire)));

org.stellar.sdk.xdr.Operation.OperationBody body =
new org.stellar.sdk.xdr.Operation.OperationBody();
Expand All @@ -60,8 +61,8 @@ public abstract static class BumpFootprintExpirationOperationBuilder<
C extends BumpFootprintExpirationOperation,
B extends BumpFootprintExpirationOperationBuilder<C, B>>
extends OperationBuilder<C, B> {
public B ledgersToExpire(Integer ledgersToExpire) {
if (ledgersToExpire <= 0) {
public B ledgersToExpire(Long ledgersToExpire) {
if (ledgersToExpire <= 0 || ledgersToExpire > 0xFFFFFFFFL) {
throw new IllegalArgumentException("ledgersToExpire isn't a ledger quantity (uint32)");
}
this.ledgersToExpire = ledgersToExpire;
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/org/stellar/sdk/LedgerBounds.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.Value;
import org.stellar.sdk.xdr.Uint32;
import org.stellar.sdk.xdr.XdrUnsignedInteger;

@Value
@lombok.Builder
Expand All @@ -10,20 +11,20 @@
* href="https://github.com/stellar/stellar-protocol/blob/master/core/cap-0021.md#specification">CAP-21<a/>
*/
public class LedgerBounds {
int minLedger;
int maxLedger;
long minLedger;
long maxLedger;

public static LedgerBounds fromXdr(org.stellar.sdk.xdr.LedgerBounds xdrLedgerBounds) {
return new LedgerBoundsBuilder()
.minLedger(xdrLedgerBounds.getMinLedger().getUint32())
.maxLedger(xdrLedgerBounds.getMaxLedger().getUint32())
.minLedger(xdrLedgerBounds.getMinLedger().getUint32().getNumber())
.maxLedger(xdrLedgerBounds.getMaxLedger().getUint32().getNumber())
.build();
}

public org.stellar.sdk.xdr.LedgerBounds toXdr() {
return new org.stellar.sdk.xdr.LedgerBounds.Builder()
.maxLedger(new Uint32(maxLedger))
.minLedger(new Uint32(minLedger))
.maxLedger(new Uint32(new XdrUnsignedInteger(maxLedger)))
.minLedger(new Uint32(new XdrUnsignedInteger(minLedger)))
.build();
}
}
15 changes: 13 additions & 2 deletions src/main/java/org/stellar/sdk/Memo.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.stellar.sdk;

import java.math.BigInteger;

/**
* The memo contains optional extra information. It is the responsibility of the client to interpret
* this value. Memos can be one of the following types:
Expand Down Expand Up @@ -46,7 +48,16 @@ public static MemoText text(byte[] text) {
*
* @param id
*/
public static MemoId id(long id) {
public static MemoId id(BigInteger id) {
return new MemoId(id);
}

/**
* Creates new {@link MemoId} instance.
*
* @param id
*/
public static MemoId id(Long id) {
return new MemoId(id);
}

Expand Down Expand Up @@ -91,7 +102,7 @@ public static Memo fromXdr(org.stellar.sdk.xdr.Memo memo) {
case MEMO_NONE:
return none();
case MEMO_ID:
return id(memo.getId().getUint64().longValue());
return id(memo.getId().getUint64().getNumber());
case MEMO_TEXT:
return text(memo.getText().getBytes());
case MEMO_HASH:
Expand Down
24 changes: 16 additions & 8 deletions src/main/java/org/stellar/sdk/MemoId.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
package org.stellar.sdk;

import com.google.common.base.Objects;
import com.google.common.primitives.UnsignedLongs;
import java.math.BigInteger;
import org.stellar.sdk.xdr.MemoType;
import org.stellar.sdk.xdr.Uint64;
import org.stellar.sdk.xdr.XdrUnsignedHyperInteger;

/** Represents MEMO_ID. */
public class MemoId extends Memo {
private long id;
private final BigInteger id;

public MemoId(long id) {
public MemoId(BigInteger id) {
if (id.compareTo(XdrUnsignedHyperInteger.MIN_VALUE) < 0
|| id.compareTo(XdrUnsignedHyperInteger.MAX_VALUE) > 0) {
throw new IllegalArgumentException("MEMO_ID must be between 0 and 2^64-1");
}
this.id = id;
}

public long getId() {
public MemoId(Long id) {
this(BigInteger.valueOf(id));
}

public BigInteger getId() {
return id;
}

@Override
org.stellar.sdk.xdr.Memo toXdr() {
org.stellar.sdk.xdr.Memo memo = new org.stellar.sdk.xdr.Memo();
memo.setDiscriminant(MemoType.MEMO_ID);
Uint64 idXdr = new Uint64();
idXdr.setUint64(id);
Uint64 idXdr = new Uint64(new XdrUnsignedHyperInteger(id));
memo.setId(idXdr);
return memo;
}
Expand All @@ -37,11 +45,11 @@ public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MemoId memoId = (MemoId) o;
return id == memoId.id;
return Objects.equal(id, memoId.id);
}

@Override
public String toString() {
return UnsignedLongs.toString(this.id);
return id.toString();
}
}
41 changes: 33 additions & 8 deletions src/main/java/org/stellar/sdk/Predicate.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import com.google.common.base.Objects;
import com.google.common.collect.Lists;
import java.math.BigInteger;
import java.util.List;
import org.stellar.sdk.xdr.ClaimPredicate;
import org.stellar.sdk.xdr.ClaimPredicateType;
import org.stellar.sdk.xdr.Duration;
import org.stellar.sdk.xdr.Int64;
import org.stellar.sdk.xdr.TimePoint;
import org.stellar.sdk.xdr.Uint64;
import org.stellar.sdk.xdr.XdrUnsignedHyperInteger;
import org.threeten.bp.Instant;

public abstract class Predicate {
Expand Down Expand Up @@ -186,15 +188,38 @@ public AbsBefore(TimePoint timePoint) {
}

public AbsBefore(long epochSeconds) {
this(new TimePoint(new Uint64(epochSeconds)));
this(new TimePoint(new Uint64(new XdrUnsignedHyperInteger(epochSeconds))));
}

public long getTimestampSeconds() {
return timePoint.getTimePoint().getUint64();
public AbsBefore(BigInteger epochSeconds) {
this(new TimePoint(new Uint64(new XdrUnsignedHyperInteger(epochSeconds))));
}

/**
* Gets the Predicate epoch in seconds.
*
* @return BigInteger the predicate epoch in seconds
*/
public BigInteger getTimestampSeconds() {
return timePoint.getTimePoint().getUint64().getNumber();
}

/**
* Gets the java date representation of a Predicate. If the Predicate specifies an epoch larger
* than 31556889864403199, it will coerce to {@link Instant#MAX} instead as no greater value can
* be represented.
*
* <p>If you want to get the real epoch, use {@link #getTimestampSeconds()} instead.
*
* @return Instant the java date representation of the predicate
*/
public Instant getDate() {
return Instant.ofEpochSecond(timePoint.getTimePoint().getUint64());
Instant instantMax = Instant.MAX;
if (getTimestampSeconds().compareTo(BigInteger.valueOf(instantMax.getEpochSecond())) > 0) {
return instantMax;
}

return Instant.ofEpochSecond(timePoint.getTimePoint().getUint64().getNumber().longValue());
}

@Override
Expand All @@ -214,7 +239,7 @@ public int hashCode() {
public ClaimPredicate toXdr() {
org.stellar.sdk.xdr.ClaimPredicate xdr = new org.stellar.sdk.xdr.ClaimPredicate();
xdr.setDiscriminant(ClaimPredicateType.CLAIM_PREDICATE_BEFORE_ABSOLUTE_TIME);
xdr.setAbsBefore(new Int64(timePoint.getTimePoint().getUint64()));
xdr.setAbsBefore(new Int64(timePoint.getTimePoint().getUint64().getNumber().longValue()));
return xdr;
}
}
Expand All @@ -228,11 +253,11 @@ public RelBefore(Duration secondsSinceClose) {
}

public RelBefore(long secondsSinceClose) {
this(new Duration(new Uint64(secondsSinceClose)));
this(new Duration(new Uint64(new XdrUnsignedHyperInteger(secondsSinceClose))));
}

public long getSecondsSinceClose() {
return duration.getDuration().getUint64();
return duration.getDuration().getUint64().getNumber().longValue();
}

@Override
Expand All @@ -252,7 +277,7 @@ public int hashCode() {
public ClaimPredicate toXdr() {
org.stellar.sdk.xdr.ClaimPredicate xdr = new org.stellar.sdk.xdr.ClaimPredicate();
xdr.setDiscriminant(ClaimPredicateType.CLAIM_PREDICATE_BEFORE_RELATIVE_TIME);
xdr.setRelBefore(new Int64(duration.getDuration().getUint64()));
xdr.setRelBefore(new Int64(duration.getDuration().getUint64().getNumber().longValue()));
return xdr;
}
}
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/org/stellar/sdk/Sep10Challenge.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.google.common.collect.Multimap;
import com.google.common.io.BaseEncoding;
import java.io.IOException;
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.Collections;
Expand All @@ -18,7 +19,7 @@
import org.stellar.sdk.xdr.SignatureHint;

public class Sep10Challenge {
static final int GRACE_PERIOD_SECONDS = 5 * 60;
static final BigInteger GRACE_PERIOD_SECONDS = BigInteger.valueOf(5 * 60);
static final String CLIENT_DOMAIN_DATA_NAME = "client_domain";
private static final String HOME_DOMAIN_MANAGER_DATA_NAME_FLAG = "auth";
private static final String WEB_AUTH_DOMAIN_MANAGER_DATA_NAME = "web_auth_domain";
Expand Down Expand Up @@ -247,14 +248,15 @@ public static ChallengeTransaction readChallengeTransaction(
throw new InvalidSep10ChallengeException("only memo type `id` is supported");
}

long maxTime = transaction.getTimeBounds().getMaxTime();
long minTime = transaction.getTimeBounds().getMinTime();
if (maxTime == 0L) {
BigInteger maxTime = transaction.getTimeBounds().getMaxTime();
BigInteger minTime = transaction.getTimeBounds().getMinTime();
if (maxTime.equals(BigInteger.ZERO)) {
throw new InvalidSep10ChallengeException("Transaction requires non-infinite timebounds.");
}

long currentTime = System.currentTimeMillis() / 1000L;
if ((currentTime + GRACE_PERIOD_SECONDS) < minTime || currentTime > maxTime) {
BigInteger currentTime = BigInteger.valueOf(System.currentTimeMillis() / 1000L);
if (currentTime.add(GRACE_PERIOD_SECONDS).compareTo(minTime) < 0
|| currentTime.compareTo(maxTime) > 0) {
throw new InvalidSep10ChallengeException(
"Transaction is not within range of the specified timebounds.");
}
Expand Down
Loading

0 comments on commit 17180d9

Please sign in to comment.