Skip to content

Commit

Permalink
Refactor the code. (#562)
Browse files Browse the repository at this point in the history
This code refactoring does not bring any new functionality additions, nor does it bring any breaking changes.
The main aspects of the refactoring are as follows:
1. Use Lombok to reduce duplicate code (this also allows us to add `toString`, `hashCode`, and `equals` methods to most classes)
2. Updated some documentation
3. Fixed some warnings.
4. Modified the style of some code.
  • Loading branch information
overcat authored Jan 11, 2024
1 parent 050e8a2 commit 97e0295
Show file tree
Hide file tree
Showing 223 changed files with 2,471 additions and 5,830 deletions.
3 changes: 3 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# .git-blame-ignore-revs
# Add code formatting tool. (#480)
0803b98a7c80e2f68f5bf5652e0eddd99faef455
71 changes: 35 additions & 36 deletions src/main/java/org/stellar/sdk/AbstractTransaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,34 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NonNull;
import org.stellar.sdk.xdr.DecoratedSignature;
import org.stellar.sdk.xdr.Hash;
import org.stellar.sdk.xdr.SignatureHint;
import org.stellar.sdk.xdr.TransactionEnvelope;
import org.stellar.sdk.xdr.TransactionSignaturePayload;

/** Abstract class for transaction classes. */
@EqualsAndHashCode(exclude = {"accountConverter"})
// TODO: maybe we should not exclude accountConverter from equals and hashCode
public abstract class AbstractTransaction {
/** The network that the transaction is to be submitted to. */
@NonNull @Getter protected final Network network;

/** The {@link AccountConverter} for this transaction. */
@Getter @NonNull protected final AccountConverter accountConverter;

/** List of signatures attached to this transaction. */
@NonNull protected List<DecoratedSignature> signatures;

protected final Network mNetwork;
protected final AccountConverter accountConverter;
protected List<DecoratedSignature> mSignatures;
public static final int MIN_BASE_FEE = 100;

AbstractTransaction(@NonNull AccountConverter accountConverter, @NonNull Network network) {
this.accountConverter = accountConverter;
this.mNetwork = network;
this.mSignatures = new ArrayList<DecoratedSignature>();
this.network = network;
this.signatures = new ArrayList<>();
}

/**
Expand All @@ -32,7 +42,7 @@ public abstract class AbstractTransaction {
*/
public void sign(@NonNull KeyPair signer) {
byte[] txHash = this.hash();
mSignatures.add(signer.signDecorated(txHash));
signatures.add(signer.signDecorated(txHash));
}

/**
Expand All @@ -53,7 +63,7 @@ public void sign(byte @NonNull [] preimage) {
decoratedSignature.setHint(signatureHint);
decoratedSignature.setSignature(signature);

mSignatures.add(decoratedSignature);
signatures.add(decoratedSignature);
}

/** Returns transaction hash. */
Expand All @@ -69,40 +79,22 @@ public String hashHex() {
/** Returns signature base. */
public abstract byte[] signatureBase();

/**
* Gets the Network string for this transaction.
*
* @return the Network string
*/
public Network getNetwork() {
return mNetwork;
}

/**
* Gets the {@link AccountConverter} for this transaction.
*
* @return the {@link AccountConverter} object
*/
public AccountConverter getAccountConverter() {
return accountConverter;
}

/**
* Gets read only list(immutable) of the signatures on transaction.
*
* @return immutable list of signatures
*/
public List<DecoratedSignature> getSignatures() {
return Collections.unmodifiableList(mSignatures);
return Collections.unmodifiableList(signatures);
}

/**
* Adds an additional signature to the transaction's existing list of signatures.
* Adds a signature to the transaction's existing list of signatures.
*
* @param signature the signature to add
*/
public void addSignature(DecoratedSignature signature) {
mSignatures.add(signature);
signatures.add(signature);
}

public abstract TransactionEnvelope toEnvelopeXdr();
Expand All @@ -123,8 +115,8 @@ public String toEnvelopeXdrBase64() {
* Creates a <code>AbstractTransaction</code> instance from previously build <code>
* TransactionEnvelope</code>
*
* @param envelope
* @return
* @param envelope the transaction envelope
* @return the {@link Transaction} or {@link FeeBumpTransaction} instance
*/
public static AbstractTransaction fromEnvelopeXdr(
AccountConverter accountConverter, TransactionEnvelope envelope, Network network) {
Expand All @@ -146,8 +138,8 @@ public static AbstractTransaction fromEnvelopeXdr(
* Creates a <code>AbstractTransaction</code> instance from previously build <code>
* TransactionEnvelope</code>
*
* @param envelope
* @return
* @param envelope the transaction envelope
* @return the {@link Transaction} or {@link FeeBumpTransaction} instance
*/
public static AbstractTransaction fromEnvelopeXdr(TransactionEnvelope envelope, Network network) {
return fromEnvelopeXdr(AccountConverter.enableMuxed(), envelope, network);
Expand All @@ -158,8 +150,8 @@ public static AbstractTransaction fromEnvelopeXdr(TransactionEnvelope envelope,
* </code>
*
* @param envelope Base-64 encoded <code>TransactionEnvelope</code>
* @return
* @throws IOException
* @return the {@link Transaction} or {@link FeeBumpTransaction} instance
* @throws IOException if the envelope is malformed
*/
public static AbstractTransaction fromEnvelopeXdr(
AccountConverter accountConverter, String envelope, Network network) throws IOException {
Expand All @@ -172,14 +164,21 @@ public static AbstractTransaction fromEnvelopeXdr(
* </code>
*
* @param envelope Base-64 encoded <code>TransactionEnvelope</code>
* @return
* @throws IOException
* @return the {@link Transaction} or {@link FeeBumpTransaction} instance
* @throws IOException if the envelope is malformed
*/
public static AbstractTransaction fromEnvelopeXdr(String envelope, Network network)
throws IOException {
return fromEnvelopeXdr(AccountConverter.enableMuxed(), envelope, network);
}

/**
* Get the signature base of this transaction envelope.
*
* @param taggedTransaction the tagged transaction for signing
* @param network the network to sign on
* @return the signature base of this transaction envelope
*/
public static byte[] getTransactionSignatureBase(
TransactionSignaturePayload.TransactionSignaturePayloadTaggedTransaction taggedTransaction,
Network network) {
Expand Down
57 changes: 13 additions & 44 deletions src/main/java/org/stellar/sdk/Account.java
Original file line number Diff line number Diff line change
@@ -1,72 +1,41 @@
package org.stellar.sdk;

import java.util.Objects;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NonNull;

/**
* Represents an account in Stellar network with it's sequence number. Account object is required to
* Represents an account in Stellar network with its sequence number. Account object is required to
* build a {@link Transaction}.
*
* @see TransactionBuilder
*/
@Getter
@EqualsAndHashCode
@AllArgsConstructor
public class Account implements TransactionBuilderAccount {
private final String mAccountId;
private Long mSequenceNumber;

/**
* Class constructor.
*
* @param accountId ID associated with this Account
* @param sequenceNumber Current sequence number of the account (can be obtained using
* java-stellar-sdk or horizon server)
*/
public Account(@NonNull String accountId, @NonNull Long sequenceNumber) {
mAccountId = accountId;
mSequenceNumber = sequenceNumber;
}

@Override
public String getAccountId() {
return mAccountId;
}
@NonNull private final String accountId;
@NonNull private Long sequenceNumber;

@Override
public KeyPair getKeyPair() {
return KeyPair.fromAccountId(mAccountId);
}

@Override
public Long getSequenceNumber() {
return mSequenceNumber;
return KeyPair.fromAccountId(accountId);
}

@Override
public void setSequenceNumber(long seqNum) {
mSequenceNumber = seqNum;
sequenceNumber = seqNum;
}

@Override
public Long getIncrementedSequenceNumber() {
return mSequenceNumber + 1;
return sequenceNumber + 1;
}

/** Increments sequence number in this object by one. */
public void incrementSequenceNumber() {
mSequenceNumber++;
}

public int hashCode() {
return Objects.hash(this.mAccountId, this.mSequenceNumber);
}

@Override
public boolean equals(Object object) {
if (!(object instanceof Account)) {
return false;
}

Account other = (Account) object;
return Objects.equals(this.mAccountId, other.mAccountId)
&& Objects.equals(this.mSequenceNumber, other.mSequenceNumber);
sequenceNumber++;
}
}
15 changes: 11 additions & 4 deletions src/main/java/org/stellar/sdk/AccountConverter.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package org.stellar.sdk;

import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import org.stellar.sdk.xdr.CryptoKeyType;
import org.stellar.sdk.xdr.MuxedAccount;

/**
* AccountConverter is a helper class that can be used to encode and decode muxed accounts.
*
* @see <a href="https://stellar.org/blog/developers/muxed-accounts-faq">Muxed Accounts FAQ</a>
*/
@AllArgsConstructor(access = lombok.AccessLevel.PRIVATE)
@EqualsAndHashCode
public class AccountConverter {
private final boolean enableMuxed;

private AccountConverter(boolean enabled) {
this.enableMuxed = enabled;
}

/** Returns an AccountConverter which supports muxed accounts. */
public static AccountConverter enableMuxed() {
return new AccountConverter(true);
Expand All @@ -28,6 +33,7 @@ public static AccountConverter disableMuxed() {
* Encodes an account string into its XDR MuxedAccount representation.
*
* @param account the string representation of an account
* @return the XDR MuxedAccount representation of an account
*/
public MuxedAccount encode(String account) {
MuxedAccount muxed = StrKey.encodeToXDRMuxedAccount(account);
Expand All @@ -46,6 +52,7 @@ public MuxedAccount encode(String account) {
* Decodes an XDR MuxedAccount into its string representation.
*
* @param account the XDR MuxedAccount representation of an account
* @return the string representation of an account
*/
public String decode(MuxedAccount account) {
if (this.enableMuxed || account.getDiscriminant().equals(CryptoKeyType.KEY_TYPE_ED25519)) {
Expand Down
10 changes: 4 additions & 6 deletions src/main/java/org/stellar/sdk/AccountFlag.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package org.stellar.sdk;

import lombok.Getter;
import org.stellar.sdk.xdr.AccountFlags;

/**
* AccountFlag is the <code>enum</code> that can be used in {@link SetOptionsOperation}.
*
* @see <a href="https://developers.stellar.org/docs/glossary/accounts/#flags"
* target="_blank">Account Flags</a>
* @see <a href="https://developers.stellar.org/docs/glossary#flags" target="_blank">Account
* Flags</a>
*/
@Getter
public enum AccountFlag {
/**
* Authorization required (0x1): Requires the issuing account to give other accounts permission
Expand All @@ -31,8 +33,4 @@ public enum AccountFlag {
AccountFlag(int value) {
this.value = value;
}

public int getValue() {
return value;
}
}
Loading

0 comments on commit 97e0295

Please sign in to comment.