Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge release-0.16.0 to master #280

Merged
merged 5 commits into from
May 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@

As this project is pre 1.0, breaking changes may happen for minor version bumps. A breaking change will get clearly notified in this log.

## 0.16.0

* Update XDR definitions and auto-generated classes to support upcoming protocol 13 release ([#276](https://github.com/stellar/java-stellar-sdk/pull/276)).
* Extend StrKey implementation to handle [CAP 27 Muxed Accounts](https://github.com/stellar/stellar-protocol/blob/master/core/cap-0027.md) ([#276](https://github.com/stellar/java-stellar-sdk/pull/276)).
* Update `TransactionResponse` to include new fields which are relevant to [CAP 15 Fee-Bump Transactions](https://github.com/stellar/stellar-protocol/blob/master/core/cap-0015.md) ([#275](https://github.com/stellar/java-stellar-sdk/pull/275)).
* Update `AccountResponse.Balance`, `AllowTrustOperationResponse`, and create `TrustlineAuthorizedToMaintainLiabilitiesEffectResponse` to support [CAP 18 Fine-Grained Control of Authorization](https://github.com/stellar/stellar-protocol/blob/master/core/cap-0018.md) ([#274](https://github.com/stellar/java-stellar-sdk/pull/274)).
* Add `FeeBumpTransaction` and `FeeBumpTransaction.Builder` for parsing and creating [CAP 15 Fee-Bump Transactions](https://github.com/stellar/stellar-protocol/blob/master/core/cap-0015.md) ([#278](https://github.com/stellar/java-stellar-sdk/pull/278)).
* Add methods to `Server` for submitting [CAP 15 Fee-Bump Transactions](https://github.com/stellar/stellar-protocol/blob/master/core/cap-0015.md) ([#278](https://github.com/stellar/java-stellar-sdk/pull/278)).
* Update SEP 10 implementation to reject fee-bump transactions and transactions with multiplexed addresses ([#278] (https://github.com/stellar/java-stellar-sdk/pull/278)).
* Update SEP 29 implementation to handle bump transactions ([#278](https://github.com/stellar/java-stellar-sdk/pull/278)).


## 0.15.0

- Add SEP0029 (memo required) support. (https://github.com/stellar/java-stellar-sdk/issues/272)
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ apply plugin: 'com.github.ben-manes.versions' // gradle dependencyUpdates -Drevi
apply plugin: 'project-report' // gradle htmlDependencyReport

sourceCompatibility = 1.6
version = '0.15.0'
version = '0.16.0'
group = 'stellar'

jar {
Expand Down
134 changes: 134 additions & 0 deletions src/main/java/org/stellar/sdk/AbstractTransaction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package org.stellar.sdk;

import com.google.common.io.BaseEncoding;
import org.stellar.sdk.xdr.*;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static com.google.common.base.Preconditions.checkNotNull;

public abstract class AbstractTransaction {
protected final Network mNetwork;
protected List<DecoratedSignature> mSignatures;
public static final int MIN_BASE_FEE = 100;


AbstractTransaction(Network network) {
this.mNetwork = checkNotNull(network, "network cannot be null");
this.mSignatures = new ArrayList<DecoratedSignature>();
}

/**
* Adds a new signature ed25519PublicKey to this transaction.
* @param signer {@link KeyPair} object representing a signer
*/
public void sign(KeyPair signer) {
checkNotNull(signer, "signer cannot be null");
byte[] txHash = this.hash();
mSignatures.add(signer.signDecorated(txHash));
}

/**
* Adds a new sha256Hash signature to this transaction by revealing preimage.
* @param preimage the sha256 hash of preimage should be equal to signer hash
*/
public void sign(byte[] preimage) {
checkNotNull(preimage, "preimage cannot be null");
org.stellar.sdk.xdr.Signature signature = new org.stellar.sdk.xdr.Signature();
signature.setSignature(preimage);

byte[] hash = Util.hash(preimage);
byte[] signatureHintBytes = Arrays.copyOfRange(hash, hash.length - 4, hash.length);
SignatureHint signatureHint = new SignatureHint();
signatureHint.setSignatureHint(signatureHintBytes);

DecoratedSignature decoratedSignature = new DecoratedSignature();
decoratedSignature.setHint(signatureHint);
decoratedSignature.setSignature(signature);

mSignatures.add(decoratedSignature);
}

/**
* Returns transaction hash.
*/
public byte[] hash() {
return Util.hash(this.signatureBase());
}

/**
* Returns transaction hash encoded as a hexadecimal string.
*/
public String hashHex() {
return BaseEncoding.base16().lowerCase().encode(this.hash());
}

/**
* Returns signature base.
*/
public abstract byte[] signatureBase();

public Network getNetwork() {
return mNetwork;
}

public List<DecoratedSignature> getSignatures() {
return mSignatures;
}

public abstract TransactionEnvelope toEnvelopeXdr();

/**
* Returns base64-encoded TransactionEnvelope XDR object. Transaction need to have at least one signature.
*/
public String toEnvelopeXdrBase64() {
try {
TransactionEnvelope envelope = this.toEnvelopeXdr();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
XdrDataOutputStream xdrOutputStream = new XdrDataOutputStream(outputStream);
TransactionEnvelope.encode(xdrOutputStream, envelope);

BaseEncoding base64Encoding = BaseEncoding.base64();
return base64Encoding.encode(outputStream.toByteArray());
} catch (IOException e) {
throw new AssertionError(e);
}
}

/**
* Creates a <code>AbstractTransaction</code> instance from previously build <code>TransactionEnvelope</code>
* @param envelope
* @return
*/
public static AbstractTransaction fromEnvelopeXdr(TransactionEnvelope envelope, Network network) {
switch (envelope.getDiscriminant()) {
case ENVELOPE_TYPE_TX:
return Transaction.fromV1EnvelopeXdr(envelope.getV1(), network);
case ENVELOPE_TYPE_TX_V0:
return Transaction.fromV0EnvelopeXdr(envelope.getV0(), network);
case ENVELOPE_TYPE_TX_FEE_BUMP:
return FeeBumpTransaction.fromFeeBumpTransactionEnvelope(envelope.getFeeBump(), network);
default:
throw new IllegalArgumentException("transaction type is not supported: "+envelope.getDiscriminant());
}
}

/**
* Creates a <code>Transaction</code> instance from previously build <code>TransactionEnvelope</code>
* @param envelope Base-64 encoded <code>TransactionEnvelope</code>
* @return
* @throws IOException
*/
public static AbstractTransaction fromEnvelopeXdr(String envelope, Network network) throws IOException {
BaseEncoding base64Encoding = BaseEncoding.base64();
byte[] bytes = base64Encoding.decode(envelope);

TransactionEnvelope transactionEnvelope = TransactionEnvelope.decode(new XdrDataInputStream(new ByteArrayInputStream(bytes)));
return fromEnvelopeXdr(transactionEnvelope, network);
}
}
6 changes: 3 additions & 3 deletions src/main/java/org/stellar/sdk/AccountMergeOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public String getDestination() {
@Override
OperationBody toOperationBody() {
OperationBody body = new org.stellar.sdk.xdr.Operation.OperationBody();
body.setDestination(StrKey.encodeToXDRAccountId(this.destination));
body.setDestination(StrKey.encodeToXDRMuxedAccount(this.destination));
body.setDiscriminant(OperationType.ACCOUNT_MERGE);
return body;
}
Expand All @@ -43,8 +43,8 @@ public static class Builder {
private String mSourceAccount;

Builder(OperationBody op) {
destination = StrKey.encodeStellarAccountId(
op.getDestination().getAccountID().getEd25519().getUint256()
destination = StrKey.encodeStellarMuxedAccount(
op.getDestination()
);
}

Expand Down
45 changes: 39 additions & 6 deletions src/main/java/org/stellar/sdk/AllowTrustOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ public class AllowTrustOperation extends Operation {
private final String trustor;
private final String assetCode;
private final boolean authorize;
private final boolean authorizeToMaintainLiabilities;

private AllowTrustOperation(String trustor, String assetCode, boolean authorize) {
private AllowTrustOperation(String trustor, String assetCode, boolean authorize, boolean authorizeToMaintainLiabilities) {
this.trustor = checkNotNull(trustor, "trustor cannot be null");
this.assetCode = checkNotNull(assetCode, "assetCode cannot be null");
this.authorize = authorize;
this.authorizeToMaintainLiabilities = authorizeToMaintainLiabilities;
}

/**
Expand Down Expand Up @@ -62,8 +64,16 @@ org.stellar.sdk.xdr.Operation.OperationBody toOperationBody() {
asset.setAssetCode12(assetCode12);
}
op.setAsset(asset);
Uint32 flag = new Uint32();
// authorize
op.setAuthorize(authorize);
if (authorize) {
flag.setUint32(TrustLineFlags.AUTHORIZED_FLAG.getValue());
} else if (authorizeToMaintainLiabilities) {
flag.setUint32(TrustLineFlags.AUTHORIZED_TO_MAINTAIN_LIABILITIES_FLAG.getValue());
} else {
flag.setUint32(0);
}
op.setAuthorize(flag);

org.stellar.sdk.xdr.Operation.OperationBody body = new org.stellar.sdk.xdr.Operation.OperationBody();
body.setDiscriminant(OperationType.ALLOW_TRUST);
Expand All @@ -79,11 +89,12 @@ public static class Builder {
private final String trustor;
private final String assetCode;
private final boolean authorize;
private boolean authorizeToMaintainLiabilities;

private String mSourceAccount;

Builder(AllowTrustOp op) {
trustor = StrKey.encodeStellarAccountId(op.getTrustor().getAccountID().getEd25519().getUint256());
trustor = StrKey.encodeStellarAccountId(op.getTrustor());
switch (op.getAsset().getDiscriminant()) {
case ASSET_TYPE_CREDIT_ALPHANUM4:
assetCode = new String(op.getAsset().getAssetCode4().getAssetCode4()).trim();
Expand All @@ -94,7 +105,20 @@ public static class Builder {
default:
throw new RuntimeException("Unknown asset code");
}
authorize = op.getAuthorize();

int flag = op.getAuthorize().getUint32().intValue();
if (flag == TrustLineFlags.AUTHORIZED_FLAG.getValue()) {
authorize = true;
authorizeToMaintainLiabilities = false;
} else if (flag == TrustLineFlags.AUTHORIZED_TO_MAINTAIN_LIABILITIES_FLAG.getValue()) {
authorize = false;
authorizeToMaintainLiabilities = true;
} else if (flag != 0) {
throw new IllegalArgumentException("invalid authorize flag "+flag);
} else {
authorize = false;
authorizeToMaintainLiabilities = false;
}
}

/**
Expand Down Expand Up @@ -123,7 +147,9 @@ public Builder setSourceAccount(String sourceAccount) {
* Builds an operation
*/
public AllowTrustOperation build() {
AllowTrustOperation operation = new AllowTrustOperation(trustor, assetCode, authorize);
AllowTrustOperation operation = new AllowTrustOperation(
trustor, assetCode, authorize, authorizeToMaintainLiabilities
);
if (mSourceAccount != null) {
operation.setSourceAccount(mSourceAccount);
}
Expand All @@ -133,7 +159,13 @@ public AllowTrustOperation build() {

@Override
public int hashCode() {
return Objects.hashCode(this.getSourceAccount(), this.assetCode, this.authorize, this.trustor);
return Objects.hashCode(
this.getSourceAccount(),
this.assetCode,
this.authorize,
this.authorizeToMaintainLiabilities,
this.trustor
);
}

@Override
Expand All @@ -145,6 +177,7 @@ public boolean equals(Object object) {
AllowTrustOperation other = (AllowTrustOperation) object;
return Objects.equal(this.assetCode, other.assetCode) &&
Objects.equal(this.authorize, other.authorize) &&
Objects.equal(this.authorizeToMaintainLiabilities, other.authorizeToMaintainLiabilities) &&
Objects.equal(this.trustor, other.trustor) &&
Objects.equal(this.getSourceAccount(), other.getSourceAccount());
}
Expand Down
8 changes: 2 additions & 6 deletions src/main/java/org/stellar/sdk/Asset.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,11 @@ public static Asset fromXdr(org.stellar.sdk.xdr.Asset xdr) {
return new AssetTypeNative();
case ASSET_TYPE_CREDIT_ALPHANUM4:
String assetCode4 = Util.paddedByteArrayToString(xdr.getAlphaNum4().getAssetCode().getAssetCode4());
accountId = StrKey.encodeStellarAccountId(
xdr.getAlphaNum4().getIssuer().getAccountID().getEd25519().getUint256()
);
accountId = StrKey.encodeStellarAccountId(xdr.getAlphaNum4().getIssuer());
return new AssetTypeCreditAlphaNum4(assetCode4, accountId);
case ASSET_TYPE_CREDIT_ALPHANUM12:
String assetCode12 = Util.paddedByteArrayToString(xdr.getAlphaNum12().getAssetCode().getAssetCode12());
accountId = StrKey.encodeStellarAccountId(
xdr.getAlphaNum12().getIssuer().getAccountID().getEd25519().getUint256()
);
accountId = StrKey.encodeStellarAccountId(xdr.getAlphaNum12().getIssuer());
return new AssetTypeCreditAlphaNum12(assetCode12, accountId);
default:
throw new IllegalArgumentException("Unknown asset type " + xdr.getDiscriminant());
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/stellar/sdk/CreateAccountOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static class Builder {
* @param op {@link CreateAccountOp}
*/
Builder(CreateAccountOp op) {
destination = StrKey.encodeStellarAccountId(op.getDestination().getAccountID().getEd25519().getUint256());
destination = StrKey.encodeStellarAccountId(op.getDestination());
startingBalance = Operation.fromXdrAmount(op.getStartingBalance().getInt64().longValue());
}

Expand Down
Loading