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

fix the decoding of balanceId in org.stellar.sdk.ClaimClaimableBalanceOperation. #310

Merged
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

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.21.2
- Fix the decoding of `balanceId` in `org.stellar.sdk.ClaimClaimableBalanceOperation`. ([#310](https://github.com/stellar/java-stellar-sdk/pull/310))

## 0.21.1
- Fix NullPointerException in `org.stellar.sdk.responses.operations.RevokeSponsorshipOperationResponse` accessor methods.

Expand Down
27 changes: 20 additions & 7 deletions src/main/java/org/stellar/sdk/ClaimClaimableBalanceOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
import com.google.common.io.BaseEncoding;
import org.stellar.sdk.xdr.*;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

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

public class ClaimClaimableBalanceOperation extends Operation {
Expand All @@ -22,14 +26,16 @@ public String getBalanceId() {
org.stellar.sdk.xdr.Operation.OperationBody toOperationBody() {
ClaimClaimableBalanceOp op = new ClaimClaimableBalanceOp();

ClaimableBalanceID id = new ClaimableBalanceID();
id.setDiscriminant(ClaimableBalanceIDType.CLAIMABLE_BALANCE_ID_TYPE_V0);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The CLAIMABLE_BALANCE_ID_TYPE is already included in the banlanceId.

Hash hash = new Hash();
hash.setHash(BaseEncoding.base16().lowerCase().decode(balanceId.toLowerCase()));
id.setV0(hash);
byte[] balanceIdBytes = BaseEncoding.base16().lowerCase().decode(balanceId.toLowerCase());
XdrDataInputStream balanceIdXdrDataInputStream = new XdrDataInputStream(new ByteArrayInputStream(balanceIdBytes));
ClaimableBalanceID id;
try {
id = ClaimableBalanceID.decode(balanceIdXdrDataInputStream);
} catch (IOException e) {
throw new IllegalArgumentException("invalid balanceId: " + balanceId, e);
}

op.setBalanceID(id);

org.stellar.sdk.xdr.Operation.OperationBody body = new org.stellar.sdk.xdr.Operation.OperationBody();
body.setDiscriminant(OperationType.CLAIM_CLAIMABLE_BALANCE);
body.setClaimClaimableBalanceOp(op);
Expand All @@ -46,7 +52,14 @@ public static class Builder {
* @param op {@link ClaimClaimableBalanceOp}
*/
Builder(ClaimClaimableBalanceOp op) {
balanceId = BaseEncoding.base16().lowerCase().encode(op.getBalanceID().getV0().getHash());
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
XdrDataOutputStream xdrDataOutputStream = new XdrDataOutputStream(byteArrayOutputStream);
try {
op.getBalanceID().encode(xdrDataOutputStream);
} catch (IOException e) {
throw new IllegalArgumentException("invalid claimClaimableBalanceOp.", e);
}
balanceId = BaseEncoding.base16().lowerCase().encode(byteArrayOutputStream.toByteArray());
}

/**
Expand Down
54 changes: 54 additions & 0 deletions src/test/java/org/stellar/sdk/OperationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -752,4 +752,58 @@ public void testInflationOperation() {
"AAAAAQAAAAC7JAuE3XvquOnbsgv2SRztjuk4RoBVefQ0rlrFMMQvfAAAAAk=",
operation.toXdrBase64());
}

@Test
public void testClaimClaimableBalanceOperationValid() {
String balanceId = "000000006d6a0c142516a9cc7885a85c5aba3a1f4af5181cf9e7a809ac7ae5e4a58c825f";
String accountId = "GABTTS6N4CT7AUN4LD7IFIUMRD5PSMCW6QTLIQNEFZDEI6ZQVUCQMCLN";
ClaimClaimableBalanceOperation operation = new ClaimClaimableBalanceOperation.Builder(balanceId).setSourceAccount(accountId).build();

org.stellar.sdk.xdr.Operation xdr = operation.toXdr();
ClaimClaimableBalanceOperation parsedOperation = (ClaimClaimableBalanceOperation) Operation.fromXdr(xdr);
assertEquals(accountId, parsedOperation.getSourceAccount());
assertEquals(balanceId, parsedOperation.getBalanceId());
// Generated by js-stellar-base.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const stellarBase = require("stellar-base")
const op = stellarBase.Operation.claimClaimableBalance({
    balanceId: "000000006d6a0c142516a9cc7885a85c5aba3a1f4af5181cf9e7a809ac7ae5e4a58c825f", 
    source: "GABTTS6N4CT7AUN4LD7IFIUMRD5PSMCW6QTLIQNEFZDEI6ZQVUCQMCLN"
})

console.log(op.toXDR().toString("base64"))

> AAAAAQAAAAADOcvN4KfwUbxY/oKijIj6+TBW9Ca0QaQuRkR7MK0FBgAAAA8AAAAAbWoMFCUWqcx4hahcWro6H0r1GBz556gJrHrl5KWMgl8=

assertEquals(
"AAAAAQAAAAADOcvN4KfwUbxY/oKijIj6+TBW9Ca0QaQuRkR7MK0FBgAAAA8AAAAAbWoMFCUWqcx4hahcWro6H0r1GBz556gJrHrl5KWMgl8=",
operation.toXdrBase64());
}

@Test
public void testClaimClaimableBalanceOperationInvalidEmptyBalanceId() {
String balanceId = "";
String accountId = "GABTTS6N4CT7AUN4LD7IFIUMRD5PSMCW6QTLIQNEFZDEI6ZQVUCQMCLN";
ClaimClaimableBalanceOperation operation = new ClaimClaimableBalanceOperation.Builder(balanceId).setSourceAccount(accountId).build();
try {
operation.toXdr();
fail();
} catch (IllegalArgumentException e) {
assertEquals("invalid balanceId: ", e.getMessage());
}
}

@Test
public void testClaimClaimableBalanceOperationInvalidClaimableBalanceIDTypeMissing() {
String balanceId = "6d6a0c142516a9cc7885a85c5aba3a1f4af5181cf9e7a809ac7ae5e4a58c825f";
String accountId = "GABTTS6N4CT7AUN4LD7IFIUMRD5PSMCW6QTLIQNEFZDEI6ZQVUCQMCLN";
ClaimClaimableBalanceOperation operation = new ClaimClaimableBalanceOperation.Builder(balanceId).setSourceAccount(accountId).build();
try {
operation.toXdr();
fail();
} catch (RuntimeException ignored) {
}
}

@Test
public void testClaimClaimableBalanceOperationInvalidClaimableBalanceIDBodyMissing() {
String balanceId = "00000000";
String accountId = "GABTTS6N4CT7AUN4LD7IFIUMRD5PSMCW6QTLIQNEFZDEI6ZQVUCQMCLN";
ClaimClaimableBalanceOperation operation = new ClaimClaimableBalanceOperation.Builder(balanceId).setSourceAccount(accountId).build();
try {
operation.toXdr();
fail();
} catch (IllegalArgumentException e) {
assertEquals("invalid balanceId: " + balanceId, e.getMessage());
}
}
}