-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for InvokeHostFunctionOperation & add
Address
class. (#481
- Loading branch information
Showing
391 changed files
with
20,848 additions
and
1,881 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
package org.stellar.sdk; | ||
|
||
import com.google.common.base.Objects; | ||
import org.stellar.sdk.xdr.Hash; | ||
import org.stellar.sdk.xdr.SCAddress; | ||
import org.stellar.sdk.xdr.SCVal; | ||
import org.stellar.sdk.xdr.SCValType; | ||
|
||
/** | ||
* Represents a single address in the Stellar network. An address can represent an account or a | ||
* contract. | ||
*/ | ||
public class Address { | ||
|
||
private final byte[] key; | ||
|
||
private final AddressType type; | ||
|
||
/** | ||
* Creates a new {@link Address} from a Stellar public key or contract ID. | ||
* | ||
* @param address the StrKey encoded format of Stellar public key or contract ID. | ||
*/ | ||
public Address(String address) { | ||
if (StrKey.isValidStellarAccountId(address)) { | ||
this.type = AddressType.ACCOUNT; | ||
this.key = StrKey.decodeStellarAccountId(address); | ||
} else if (StrKey.isValidContractId(address)) { | ||
this.type = AddressType.CONTRACT; | ||
this.key = StrKey.decodeContractId(address); | ||
} else { | ||
throw new IllegalArgumentException("Unsupported address type"); | ||
} | ||
} | ||
|
||
/** | ||
* Creates a new {@link Address} from a Stellar public key. | ||
* | ||
* @param accountId the byte array of the Stellar public key. | ||
* @return a new {@link Address} object from the given Stellar public key. | ||
*/ | ||
public static Address fromAccount(byte[] accountId) { | ||
return new Address(StrKey.encodeStellarAccountId(accountId)); | ||
} | ||
|
||
/** | ||
* Creates a new {@link Address} from a Stellar Contract ID. | ||
* | ||
* @param contractId the byte array of the Stellar Contract ID. | ||
* @return a new {@link Address} object from the given Stellar Contract ID. | ||
*/ | ||
public static Address fromContract(byte[] contractId) { | ||
return new Address(StrKey.encodeContractId(contractId)); | ||
} | ||
|
||
/** | ||
* Creates a new {@link Address} from a {@link SCAddress} XDR object. | ||
* | ||
* @param scAddress the {@link SCAddress} object to convert | ||
* @return a new {@link Address} object from the given XDR object | ||
*/ | ||
public static Address fromSCAddress(SCAddress scAddress) { | ||
switch (scAddress.getDiscriminant()) { | ||
case SC_ADDRESS_TYPE_ACCOUNT: | ||
return new Address(StrKey.encodeStellarAccountId(scAddress.getAccountId())); | ||
case SC_ADDRESS_TYPE_CONTRACT: | ||
return new Address(StrKey.encodeContractId(scAddress.getContractId().getHash())); | ||
default: | ||
throw new IllegalArgumentException("Unsupported address type"); | ||
} | ||
} | ||
|
||
/** | ||
* Creates a new {@link Address} from a {@link SCVal} XDR object. | ||
* | ||
* @param scVal the {@link SCVal} object to convert | ||
* @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"); | ||
} | ||
return Address.fromSCAddress(scVal.getAddress()); | ||
} | ||
|
||
/** | ||
* Converts this object to its {@link SCAddress} XDR object representation. | ||
* | ||
* @return a new {@link SCAddress} object from this object | ||
*/ | ||
public SCAddress toSCAddress() { | ||
SCAddress scAddress = new SCAddress(); | ||
switch (this.type) { | ||
case ACCOUNT: | ||
scAddress.setDiscriminant(org.stellar.sdk.xdr.SCAddressType.SC_ADDRESS_TYPE_ACCOUNT); | ||
scAddress.setAccountId(KeyPair.fromPublicKey(this.key).getXdrAccountId()); | ||
break; | ||
case CONTRACT: | ||
scAddress.setDiscriminant(org.stellar.sdk.xdr.SCAddressType.SC_ADDRESS_TYPE_CONTRACT); | ||
scAddress.setContractId(new Hash(this.key)); | ||
break; | ||
default: | ||
throw new IllegalArgumentException("Unsupported address type"); | ||
} | ||
return scAddress; | ||
} | ||
|
||
/** | ||
* Converts this object to its {@link SCVal} XDR object representation. | ||
* | ||
* @return a new {@link SCVal} object from this object | ||
*/ | ||
public SCVal toSCVal() { | ||
SCVal scVal = new SCVal(); | ||
scVal.setDiscriminant(SCValType.SCV_ADDRESS); | ||
scVal.setAddress(this.toSCAddress()); | ||
return scVal; | ||
} | ||
|
||
/** | ||
* Returns the byte array of the Stellar public key or contract ID. | ||
* | ||
* @return the byte array of the Stellar public key or contract ID. | ||
*/ | ||
public byte[] getBytes() { | ||
return key; | ||
} | ||
|
||
/** | ||
* Returns the type of this address. | ||
* | ||
* @return the type of this address. | ||
*/ | ||
public AddressType getType() { | ||
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) { | ||
case ACCOUNT: | ||
return StrKey.encodeStellarAccountId(this.key); | ||
case CONTRACT: | ||
return StrKey.encodeContractId(this.key); | ||
default: | ||
throw new IllegalArgumentException("Unsupported address type"); | ||
} | ||
} | ||
|
||
/** Represents the type of the address. */ | ||
public enum AddressType { | ||
ACCOUNT, | ||
CONTRACT | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
src/main/java/org/stellar/sdk/InvokeHostFunctionOperation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package org.stellar.sdk; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.NonNull; | ||
import lombok.Singular; | ||
import lombok.Value; | ||
import lombok.experimental.SuperBuilder; | ||
import org.stellar.sdk.xdr.HostFunction; | ||
import org.stellar.sdk.xdr.InvokeHostFunctionOp; | ||
import org.stellar.sdk.xdr.OperationType; | ||
import org.stellar.sdk.xdr.SorobanAuthorizationEntry; | ||
|
||
/** | ||
* Represents <a | ||
* href="https://developers.stellar.org/docs/fundamentals-and-concepts/list-of-operations#invoke-host-function" | ||
* target="_blank">InvokeHostFunction</a> operation. | ||
* | ||
* @see <a href="https://developers.stellar.org/docs/fundamentals-and-concepts/list-of-operations" | ||
* target="_blank">List of Operations</a> | ||
*/ | ||
@EqualsAndHashCode(callSuper = true) | ||
@SuperBuilder(toBuilder = true) | ||
@Value | ||
public class InvokeHostFunctionOperation extends Operation { | ||
|
||
/** The host function to invoke. */ | ||
@NonNull HostFunction hostFunction; | ||
|
||
/** The authorizations required to execute the host function */ | ||
@Singular("auth") | ||
@NonNull | ||
Collection<SorobanAuthorizationEntry> auth; | ||
|
||
/** | ||
* Constructs a new InvokeHostFunctionOperation object from the XDR representation of the {@link | ||
* InvokeHostFunctionOperation}. | ||
* | ||
* @param op the XDR representation of the {@link InvokeHostFunctionOperation}. | ||
*/ | ||
public static InvokeHostFunctionOperation fromXdr(InvokeHostFunctionOp op) { | ||
return InvokeHostFunctionOperation.builder() | ||
.hostFunction(op.getHostFunction()) | ||
.auth(Arrays.asList(op.getAuth())) | ||
.build(); | ||
} | ||
|
||
@Override | ||
org.stellar.sdk.xdr.Operation.OperationBody toOperationBody(AccountConverter accountConverter) { | ||
InvokeHostFunctionOp op = new InvokeHostFunctionOp(); | ||
op.setHostFunction(this.hostFunction); | ||
op.setAuth(this.auth.toArray(new SorobanAuthorizationEntry[0])); | ||
|
||
org.stellar.sdk.xdr.Operation.OperationBody body = | ||
new org.stellar.sdk.xdr.Operation.OperationBody(); | ||
body.setDiscriminant(OperationType.INVOKE_HOST_FUNCTION); | ||
body.setInvokeHostFunctionOp(op); | ||
|
||
return body; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.