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

Implement partial protocol 13 support #276

Merged
merged 7 commits into from
Apr 28, 2020
Merged
Show file tree
Hide file tree
Changes from 6 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
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()) {
tamirms marked this conversation as resolved.
Show resolved Hide resolved
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
4 changes: 2 additions & 2 deletions src/main/java/org/stellar/sdk/Operation.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected static String fromXdrAmount(long value) {
public org.stellar.sdk.xdr.Operation toXdr() {
org.stellar.sdk.xdr.Operation xdr = new org.stellar.sdk.xdr.Operation();
if (getSourceAccount() != null) {
xdr.setSourceAccount(StrKey.encodeToXDRAccountId(mSourceAccount));
xdr.setSourceAccount(StrKey.encodeToXDRMuxedAccount(mSourceAccount));
}
xdr.setBody(toOperationBody());
return xdr;
Expand Down Expand Up @@ -115,7 +115,7 @@ public static Operation fromXdr(org.stellar.sdk.xdr.Operation xdr) {
}
if (xdr.getSourceAccount() != null) {
operation.setSourceAccount(
StrKey.encodeStellarAccountId(xdr.getSourceAccount().getAccountID().getEd25519().getUint256())
StrKey.encodeStellarMuxedAccount(xdr.getSourceAccount())
);
}
return operation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ org.stellar.sdk.xdr.Operation.OperationBody toOperationBody() {
sendMax.setInt64(Operation.toXdrAmount(this.sendMax));
op.setSendMax(sendMax);
// destination
op.setDestination(StrKey.encodeToXDRAccountId(this.destination));
op.setDestination(StrKey.encodeToXDRMuxedAccount(this.destination));
// destAsset
op.setDestAsset(destAsset.toXdr());
// destAmount
Expand Down Expand Up @@ -130,7 +130,7 @@ public static class Builder {
Builder(PathPaymentStrictReceiveOp op) {
sendAsset = Asset.fromXdr(op.getSendAsset());
sendMax = Operation.fromXdrAmount(op.getSendMax().getInt64().longValue());
destination = StrKey.encodeStellarAccountId(op.getDestination().getAccountID().getEd25519().getUint256());
destination = StrKey.encodeStellarMuxedAccount(op.getDestination());
destAsset = Asset.fromXdr(op.getDestAsset());
destAmount = Operation.fromXdrAmount(op.getDestAmount().getInt64().longValue());
path = new Asset[op.getPath().length];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ org.stellar.sdk.xdr.Operation.OperationBody toOperationBody() {
sendAmount.setInt64(Operation.toXdrAmount(this.sendAmount));
op.setSendAmount(sendAmount);
// destination
op.setDestination(StrKey.encodeToXDRAccountId(this.destination));
op.setDestination(StrKey.encodeToXDRMuxedAccount(this.destination));
// destAsset
op.setDestAsset(destAsset.toXdr());
// destMin
Expand Down Expand Up @@ -130,7 +130,7 @@ public static class Builder {
Builder(PathPaymentStrictSendOp op) {
sendAsset = Asset.fromXdr(op.getSendAsset());
sendAmount = Operation.fromXdrAmount(op.getSendAmount().getInt64().longValue());
destination = StrKey.encodeStellarAccountId(op.getDestination().getAccountID().getEd25519().getUint256());
destination = StrKey.encodeStellarMuxedAccount(op.getDestination());
destAsset = Asset.fromXdr(op.getDestAsset());
destMin = Operation.fromXdrAmount(op.getDestMin().getInt64().longValue());
path = new Asset[op.getPath().length];
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/stellar/sdk/PaymentOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ org.stellar.sdk.xdr.Operation.OperationBody toOperationBody() {
PaymentOp op = new PaymentOp();

// destination
op.setDestination(StrKey.encodeToXDRAccountId(this.destination));
op.setDestination(StrKey.encodeToXDRMuxedAccount(this.destination));
// asset
op.setAsset(asset.toXdr());
// amount
Expand Down Expand Up @@ -81,7 +81,7 @@ public static class Builder {
* @param op {@link PaymentOp}
*/
Builder(PaymentOp op) {
destination = StrKey.encodeStellarAccountId(op.getDestination().getAccountID().getEd25519().getUint256());
destination = StrKey.encodeStellarMuxedAccount(op.getDestination());
asset = Asset.fromXdr(op.getAsset());
amount = Operation.fromXdrAmount(op.getAmount().getInt64().longValue());
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/stellar/sdk/SetOptionsOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public static class Builder {

Builder(SetOptionsOp op) {
if (op.getInflationDest() != null) {
inflationDestination = StrKey.encodeStellarAccountId(op.getInflationDest().getAccountID().getEd25519().getUint256());
inflationDestination = StrKey.encodeStellarAccountId(op.getInflationDest());
}
if (op.getClearFlags() != null) {
clearFlags = op.getClearFlags().getUint32();
Expand Down
Loading