Skip to content

Commit

Permalink
Merge pull request #310 from overcat/dev-fix-balanceId
Browse files Browse the repository at this point in the history
fix the decoding of `balanceId` in `org.stellar.sdk.ClaimClaimableBalanceOperation`.
  • Loading branch information
tamirms authored Dec 5, 2020
2 parents 9fb2d90 + 32331b3 commit 7b3d60e
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 7 deletions.
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);
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.
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());
}
}
}

0 comments on commit 7b3d60e

Please sign in to comment.