Skip to content

Commit

Permalink
Transactions.TransactionType: make value unsigned short, add find()
Browse files Browse the repository at this point in the history
* Document `value` as unsigned short, make sure it is "wrapped" correctly
* valueOf() and find() take an (unsigned) int parameter
* Have value() return an (unsigned) int
* unsignedShortValue() returns unsigned short (for serialization etc)
* Add a find() method that returns an Optional
  • Loading branch information
msgilligan committed Sep 12, 2022
1 parent 9dedd1c commit 31f1e22
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ private PayloadBuilder(short version, Transactions.TransactionType type) {
// WARNING: Hardcoded for simple-send
buffer = ByteBuffer.allocate(2 + 2 + 4 + 8);
buffer.putShort(version);
buffer.putShort(type.value());
buffer.putShort(type.unsignedShortValue());
}

private PayloadBuilder(Transactions.TransactionType type) {
Expand Down
42 changes: 36 additions & 6 deletions omnij-core/src/main/java/foundation/omni/tx/Transactions.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.bitcoinj.core.Address;

import java.util.Arrays;
import java.util.Optional;

/**
*
Expand All @@ -27,8 +28,9 @@ public interface OmniRefTx extends OmniTx {
/**
* Omni Layer Transaction Type
* <p>
* Transaction type is an unsigned 16-bit value, but all currently defined transaction types
* are less than 100. This is a partial list, see {@code omnicore.h} for the complete list.
* Transaction type is an unsigned 16-bit value, and stored as a Java {@code short} that is treated as unsigned.
* The {@link #value()} accessor performs the proper conversion and returns an unsigned {@code int}.
* This is a partial list, see {@code omnicore.h} for the complete list.
* @see <a href="https://github.com/OmniLayer/omnicore/blob/master/src/omnicore/omnicore.h">enum TransactionType in omnicore.h</a>
*/
public enum TransactionType {
Expand All @@ -48,18 +50,34 @@ public enum TransactionType {
CHANGE_ISSUER_ADDRESS( (short) 0, (short) 70);

private final short version;
// This is stored as an unsigned short
private final short value;

TransactionType(short version, short type) {
this.version = version;
value = type;
}

public static TransactionType valueOf(short code) {
/**
* Get transaction type by numeric value
* @param code transaction type integer value
* @return The correct enum
* @throws IllegalArgumentException if not found
*/
public static TransactionType valueOf(int code) throws IllegalArgumentException {
return find(code)
.orElseThrow(() -> new IllegalArgumentException("Invalid transaction type code"));
}

/**
* Find transaction type by numeric value
* @param code transaction type integer value
* @return an Optional enum or {@link Optional#empty()} if not found.
*/
public static Optional<TransactionType> find(int code) {
return Arrays.stream(values())
.filter(e -> e.value() == code)
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("Invalid transaction type code"));
.findFirst();
}

/**
Expand All @@ -71,9 +89,21 @@ public short version() {
}

/**
* Return transaction type numeric value as an unsigned {@code int}
* @return numeric value of TransactionType
*/
public short value() {
public int value() {
return Short.toUnsignedInt(value);
}

/**
* Return the 16-bit unsigned value in a Java {@code short}.
* <p>
* Use this method with caution as Java treats {@code short}s as signed.
*
* @return numeric value of TransactionType as an unsigned {@code short}
*/
public short unsignedShortValue() {
return value;
}

Expand Down

0 comments on commit 31f1e22

Please sign in to comment.