Skip to content

Commit

Permalink
review fix
Browse files Browse the repository at this point in the history
  • Loading branch information
overcat committed Aug 10, 2023
1 parent fe8adfb commit fb44420
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 27 deletions.
18 changes: 16 additions & 2 deletions src/main/java/org/stellar/sdk/ClaimClaimableBalanceOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.base.Objects;
import com.google.common.io.BaseEncoding;
import java.io.IOException;
import org.stellar.sdk.xdr.*;

public class ClaimClaimableBalanceOperation extends Operation {
Expand All @@ -18,8 +20,16 @@ public String getBalanceId() {

@Override
org.stellar.sdk.xdr.Operation.OperationBody toOperationBody(AccountConverter accountConverter) {
byte[] balanceIdBytes = BaseEncoding.base16().lowerCase().decode(this.balanceId.toLowerCase());
ClaimableBalanceID balanceId;
try {
balanceId = ClaimableBalanceID.fromXdrByteArray(balanceIdBytes);
} catch (IOException e) {
throw new IllegalArgumentException("invalid balanceId: " + this.balanceId, e);
}

ClaimClaimableBalanceOp op = new ClaimClaimableBalanceOp();
op.setBalanceID(Util.claimableBalanceIdToXDR(balanceId));
op.setBalanceID(balanceId);
org.stellar.sdk.xdr.Operation.OperationBody body =
new org.stellar.sdk.xdr.Operation.OperationBody();
body.setDiscriminant(OperationType.CLAIM_CLAIMABLE_BALANCE);
Expand All @@ -38,7 +48,11 @@ public static class Builder {
* @param op {@link ClaimClaimableBalanceOp}
*/
Builder(ClaimClaimableBalanceOp op) {
balanceId = Util.xdrToClaimableBalanceId(op.getBalanceID());
try {
balanceId = BaseEncoding.base16().lowerCase().encode(op.getBalanceID().toXdrByteArray());
} catch (IOException e) {
throw new IllegalArgumentException("Invalid balanceId in the operation", e);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.base.Objects;
import com.google.common.io.BaseEncoding;
import java.io.IOException;
import org.stellar.sdk.xdr.ClaimableBalanceID;
import org.stellar.sdk.xdr.ClawbackClaimableBalanceOp;
import org.stellar.sdk.xdr.OperationType;

Expand All @@ -26,9 +29,17 @@ public String getBalanceId() {

@Override
org.stellar.sdk.xdr.Operation.OperationBody toOperationBody(AccountConverter accountConverter) {
byte[] balanceIdBytes = BaseEncoding.base16().lowerCase().decode(this.balanceId.toLowerCase());
ClaimableBalanceID balanceId;
try {
balanceId = ClaimableBalanceID.fromXdrByteArray(balanceIdBytes);
} catch (IOException e) {
throw new IllegalArgumentException("invalid balanceId: " + this.balanceId, e);
}

ClawbackClaimableBalanceOp op = new ClawbackClaimableBalanceOp();

op.setBalanceID(Util.claimableBalanceIdToXDR(balanceId));
op.setBalanceID(balanceId);

org.stellar.sdk.xdr.Operation.OperationBody body =
new org.stellar.sdk.xdr.Operation.OperationBody();
Expand All @@ -48,7 +59,11 @@ public static class Builder {
private String mSourceAccount;

Builder(ClawbackClaimableBalanceOp op) {
balanceId = Util.xdrToClaimableBalanceId(op.getBalanceID());
try {
balanceId = BaseEncoding.base16().lowerCase().encode(op.getBalanceID().toXdrByteArray());
} catch (IOException e) {
throw new IllegalArgumentException("Invalid balanceId in the operation", e);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.base.Objects;
import com.google.common.io.BaseEncoding;
import java.io.IOException;
import org.stellar.sdk.xdr.*;

public class RevokeClaimableBalanceSponsorshipOperation extends Operation {
Expand All @@ -18,13 +20,21 @@ public String getBalanceId() {

@Override
org.stellar.sdk.xdr.Operation.OperationBody toOperationBody(AccountConverter accountConverter) {
byte[] balanceIdBytes = BaseEncoding.base16().lowerCase().decode(this.balanceId.toLowerCase());
ClaimableBalanceID balanceId;
try {
balanceId = ClaimableBalanceID.fromXdrByteArray(balanceIdBytes);
} catch (IOException e) {
throw new IllegalArgumentException("invalid balanceId: " + this.balanceId, e);
}

RevokeSponsorshipOp op = new RevokeSponsorshipOp();
LedgerKey key = new LedgerKey();
key.setDiscriminant(LedgerEntryType.CLAIMABLE_BALANCE);
LedgerKey.LedgerKeyClaimableBalance claimableBalance =
new LedgerKey.LedgerKeyClaimableBalance();

claimableBalance.setBalanceID(Util.claimableBalanceIdToXDR(balanceId));
claimableBalance.setBalanceID(balanceId);
key.setClaimableBalance(claimableBalance);
op.setLedgerKey(key);
op.setDiscriminant(RevokeSponsorshipType.REVOKE_SPONSORSHIP_LEDGER_ENTRY);
Expand All @@ -49,8 +59,14 @@ public static class Builder {
* @param op {@link RevokeSponsorshipOp}
*/
Builder(RevokeSponsorshipOp op) {
balanceId =
Util.xdrToClaimableBalanceId(op.getLedgerKey().getClaimableBalance().getBalanceID());
try {
balanceId =
BaseEncoding.base16()
.lowerCase()
.encode(op.getLedgerKey().getClaimableBalance().getBalanceID().toXdrByteArray());
} catch (IOException e) {
throw new IllegalArgumentException("Invalid claimableBalance in the operation", e);
}
}

/**
Expand Down
20 changes: 0 additions & 20 deletions src/main/java/org/stellar/sdk/Util.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package org.stellar.sdk;

import com.google.common.io.BaseEncoding;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import org.stellar.sdk.xdr.ClaimableBalanceID;

public class Util {

Expand Down Expand Up @@ -96,23 +93,6 @@ public static String getSdkVersion() {
return clientVersion;
}

public static ClaimableBalanceID claimableBalanceIdToXDR(String balanceId) {
byte[] balanceIdBytes = BaseEncoding.base16().lowerCase().decode(balanceId.toLowerCase());
try {
return ClaimableBalanceID.fromXdrByteArray(balanceIdBytes);
} catch (IOException e) {
throw new IllegalArgumentException("invalid balanceId: " + balanceId, e);
}
}

public static String xdrToClaimableBalanceId(ClaimableBalanceID balanceId) {
try {
return BaseEncoding.base16().lowerCase().encode(balanceId.toXdrByteArray());
} catch (IOException e) {
throw new IllegalArgumentException("invalid claimClaimableBalanceOp.", e);
}
}

public static AssetTypeCreditAlphaNum assertNonNativeAsset(Asset asset) {
if (asset instanceof AssetTypeCreditAlphaNum) {
return (AssetTypeCreditAlphaNum) asset;
Expand Down

0 comments on commit fb44420

Please sign in to comment.