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.
  • Loading branch information
overcat committed Aug 3, 2023
1 parent 25dc8a8 commit 315413a
Show file tree
Hide file tree
Showing 35 changed files with 530 additions and 236 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();
}
}
15 changes: 8 additions & 7 deletions src/main/java/org/stellar/sdk/Predicate.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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 +187,15 @@ 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();
return timePoint.getTimePoint().getUint64().getNumber().longValue();
}

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

@Override
Expand All @@ -214,7 +215,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 +229,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 +253,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
28 changes: 14 additions & 14 deletions src/main/java/org/stellar/sdk/SetOptionsOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,32 +131,32 @@ org.stellar.sdk.xdr.Operation.OperationBody toOperationBody(AccountConverter acc
}
if (clearFlags != null) {
Uint32 clearFlags = new Uint32();
clearFlags.setUint32(this.clearFlags);
clearFlags.setUint32(new XdrUnsignedInteger(this.clearFlags));
op.setClearFlags(clearFlags);
}
if (setFlags != null) {
Uint32 setFlags = new Uint32();
setFlags.setUint32(this.setFlags);
setFlags.setUint32(new XdrUnsignedInteger(this.setFlags));
op.setSetFlags(setFlags);
}
if (masterKeyWeight != null) {
Uint32 uint32 = new Uint32();
uint32.setUint32(masterKeyWeight);
uint32.setUint32(new XdrUnsignedInteger(masterKeyWeight));
op.setMasterWeight(uint32);
}
if (lowThreshold != null) {
Uint32 uint32 = new Uint32();
uint32.setUint32(lowThreshold);
uint32.setUint32(new XdrUnsignedInteger(lowThreshold));
op.setLowThreshold(uint32);
}
if (mediumThreshold != null) {
Uint32 uint32 = new Uint32();
uint32.setUint32(mediumThreshold);
uint32.setUint32(new XdrUnsignedInteger(mediumThreshold));
op.setMedThreshold(uint32);
}
if (highThreshold != null) {
Uint32 uint32 = new Uint32();
uint32.setUint32(highThreshold);
uint32.setUint32(new XdrUnsignedInteger(highThreshold));
op.setHighThreshold(uint32);
}
if (homeDomain != null) {
Expand All @@ -167,7 +167,7 @@ org.stellar.sdk.xdr.Operation.OperationBody toOperationBody(AccountConverter acc
if (signer != null) {
org.stellar.sdk.xdr.Signer signer = new org.stellar.sdk.xdr.Signer();
Uint32 weight = new Uint32();
weight.setUint32(signerWeight & 0xFF);
weight.setUint32(new XdrUnsignedInteger(signerWeight & 0xFF));
signer.setKey(this.signer);
signer.setWeight(weight);
op.setSigner(signer);
Expand Down Expand Up @@ -203,29 +203,29 @@ public static class Builder {
inflationDestination = StrKey.encodeStellarAccountId(op.getInflationDest());
}
if (op.getClearFlags() != null) {
clearFlags = op.getClearFlags().getUint32();
clearFlags = op.getClearFlags().getUint32().getNumber().intValue();
}
if (op.getSetFlags() != null) {
setFlags = op.getSetFlags().getUint32();
setFlags = op.getSetFlags().getUint32().getNumber().intValue();
}
if (op.getMasterWeight() != null) {
masterKeyWeight = op.getMasterWeight().getUint32();
masterKeyWeight = op.getMasterWeight().getUint32().getNumber().intValue();
}
if (op.getLowThreshold() != null) {
lowThreshold = op.getLowThreshold().getUint32();
lowThreshold = op.getLowThreshold().getUint32().getNumber().intValue();
}
if (op.getMedThreshold() != null) {
mediumThreshold = op.getMedThreshold().getUint32();
mediumThreshold = op.getMedThreshold().getUint32().getNumber().intValue();
}
if (op.getHighThreshold() != null) {
highThreshold = op.getHighThreshold().getUint32();
highThreshold = op.getHighThreshold().getUint32().getNumber().intValue();
}
if (op.getHomeDomain() != null) {
homeDomain = op.getHomeDomain().getString32().toString();
}
if (op.getSigner() != null) {
signer = op.getSigner().getKey();
signerWeight = op.getSigner().getWeight().getUint32() & 0xFF;
signerWeight = op.getSigner().getWeight().getUint32().getNumber().intValue() & 0xFF;
}
}

Expand Down
Loading

0 comments on commit 315413a

Please sign in to comment.