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

Add abstractions for converting between SCVal and native types. #500

Merged
merged 20 commits into from
Aug 14, 2023
Merged
30 changes: 9 additions & 21 deletions src/main/java/org/stellar/sdk/Address.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.stellar.sdk;

import com.google.common.base.Objects;
import lombok.EqualsAndHashCode;
import org.stellar.sdk.xdr.Hash;
import org.stellar.sdk.xdr.SCAddress;
import org.stellar.sdk.xdr.SCVal;
Expand All @@ -10,8 +10,9 @@
* Represents a single address in the Stellar network. An address can represent an account or a
* contract.
*/
@EqualsAndHashCode
public class Address {

private static final SCValType TYPE = SCValType.SCV_ADDRESS;
private final byte[] key;

private final AddressType type;
Expand Down Expand Up @@ -77,8 +78,10 @@ public static Address fromSCAddress(SCAddress scAddress) {
* @return a new {@link Address} object from the given XDR object
*/
public static Address fromSCVal(SCVal scVal) {
if (!SCValType.SCV_ADDRESS.equals(scVal.getDiscriminant())) {
throw new IllegalArgumentException("SCVal is not of type SCV_ADDRESS");
if (!TYPE.equals(scVal.getDiscriminant())) {
throw new IllegalArgumentException(
String.format(
"invalid scVal type, expected %s, but got %s", TYPE, scVal.getDiscriminant()));
}
return Address.fromSCAddress(scVal.getAddress());
}
Expand Down Expand Up @@ -112,7 +115,7 @@ public SCAddress toSCAddress() {
*/
public SCVal toSCVal() {
SCVal scVal = new SCVal();
scVal.setDiscriminant(SCValType.SCV_ADDRESS);
scVal.setDiscriminant(TYPE);
scVal.setAddress(this.toSCAddress());
return scVal;
}
Expand All @@ -131,25 +134,10 @@ public byte[] getBytes() {
*
* @return the type of this address.
*/
public AddressType getType() {
public AddressType getAddressType() {
return type;
}

@Override
public int hashCode() {
return Objects.hashCode(this.key, this.type);
}

@Override
public boolean equals(Object object) {
if (!(object instanceof Address)) {
return false;
}

Address other = (Address) object;
return Objects.equal(this.key, other.key) && Objects.equal(this.type, other.type);
}

@Override
public String toString() {
switch (this.type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
*
* @see <a href="https://developers.stellar.org/docs/fundamentals-and-concepts/list-of-operations"
* target="_blank">List of Operations</a>
* @see org.stellar.sdk.scval.Scv
*/
@EqualsAndHashCode(callSuper = true)
@SuperBuilder(toBuilder = true)
Expand Down Expand Up @@ -225,6 +226,7 @@ public static InvokeHostFunctionOperation fromXdr(InvokeHostFunctionOp op) {
* parameter preset, so that you can conveniently build an {@link InvokeHostFunctionOperation} to
* invoke a contract function.
*
* @see org.stellar.sdk.scval.Scv
* @see <a
* href="https://soroban.stellar.org/docs/fundamentals-and-concepts/interacting-with-contracts"
* target="_blank">Interacting with Contracts</a>
Expand All @@ -236,7 +238,7 @@ public static InvokeHostFunctionOperation fromXdr(InvokeHostFunctionOp op) {
public static InvokeHostFunctionOperationBuilder<?, ?> invokeContractFunctionOperationBuilder(
String contractId, String functionName, @Nullable Collection<SCVal> parameters) {
Address address = new Address(contractId);
if (address.getType() != Address.AddressType.CONTRACT) {
if (address.getAddressType() != Address.AddressType.CONTRACT) {
throw new IllegalArgumentException("\"contractId\" must be a contract address");
}
SCVal contractIdScVal = address.toSCVal();
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/org/stellar/sdk/Util.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.stellar.sdk;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
Expand Down Expand Up @@ -99,4 +100,22 @@ public static AssetTypeCreditAlphaNum assertNonNativeAsset(Asset asset) {
}
throw new IllegalArgumentException("native assets are not supported");
}

/**
* Returns an 8 byte array representation of a BigInteger value.
*
* @param value BigInteger value to convert to byte array
* @return byte array
*/
public static byte[] getBytes(BigInteger value) {
byte[] bytes = value.toByteArray();
if (bytes.length < 8) {
byte[] temp = new byte[8];
System.arraycopy(bytes, 0, temp, 8 - bytes.length, bytes.length);
bytes = temp;
} else if (bytes.length > 8) {
bytes = Arrays.copyOfRange(bytes, bytes.length - 8, bytes.length);
}
return bytes;
}
}
Loading
Loading