This repository has been archived by the owner on Feb 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
258 additions
and
94 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,29 @@ | ||
package org.stellar.base; | ||
|
||
import org.apache.commons.codec.DecoderException; | ||
import org.stellar.base.xdr.MemoType; | ||
|
||
/** | ||
* Represents MEMO_HASH. | ||
*/ | ||
public class MemoHash extends MemoHashAbstract { | ||
public MemoHash(byte[] bytes) { | ||
super(bytes); | ||
} | ||
|
||
public MemoHash(String hexString) throws DecoderException { | ||
super(hexString); | ||
} | ||
|
||
@Override | ||
org.stellar.base.xdr.Memo toXdr() { | ||
org.stellar.base.xdr.Memo memo = new org.stellar.base.xdr.Memo(); | ||
memo.setDiscriminant(MemoType.MEMO_HASH); | ||
|
||
org.stellar.base.xdr.Hash hash = new org.stellar.base.xdr.Hash(); | ||
hash.setHash(bytes); | ||
|
||
memo.setHash(hash); | ||
return memo; | ||
} | ||
} |
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,60 @@ | ||
package org.stellar.base; | ||
|
||
import org.apache.commons.codec.DecoderException; | ||
import org.apache.commons.codec.binary.Hex; | ||
|
||
abstract class MemoHashAbstract extends Memo { | ||
protected byte[] bytes; | ||
|
||
public MemoHashAbstract(byte[] bytes) { | ||
if (bytes.length < 32) { | ||
bytes = Util.paddedByteArray(bytes, 32); | ||
} else if (bytes.length > 32) { | ||
throw new MemoTooLongException("MEMO_HASH can contain 32 bytes at max."); | ||
} | ||
|
||
this.bytes = bytes; | ||
} | ||
|
||
public MemoHashAbstract(String hexString) throws DecoderException { | ||
this(Hex.decodeHex(hexString.toCharArray())); | ||
} | ||
|
||
/** | ||
* Returns 32 bytes long array contained in this memo. | ||
*/ | ||
public byte[] getBytes() { | ||
return bytes; | ||
} | ||
|
||
/** | ||
* <p>Returns hex representation of bytes contained in this memo.</p> | ||
* | ||
* <p>Example:</p> | ||
* <code><pre> | ||
* MemoHash memo = new MemoHash("4142434445"); | ||
* memo.getHexValue(); // 4142434445000000000000000000000000000000000000000000000000000000 | ||
* memo.getTrimmedHexValue(); // 4142434445 | ||
* </pre></code> | ||
*/ | ||
public String getHexValue() { | ||
return new String(Hex.encodeHex(this.bytes)); | ||
} | ||
|
||
/** | ||
* <p>Returns hex representation of bytes contained in this memo until null byte (0x00) is found.</p> | ||
* | ||
* <p>Example:</p> | ||
* <code><pre> | ||
* MemoHash memo = new MemoHash("4142434445"); | ||
* memo.getHexValue(); // 4142434445000000000000000000000000000000000000000000000000000000 | ||
* memo.getTrimmedHexValue(); // 4142434445 | ||
* </pre></code> | ||
*/ | ||
public String getTrimmedHexValue() { | ||
return this.getHexValue().split("00")[0]; | ||
} | ||
|
||
@Override | ||
abstract org.stellar.base.xdr.Memo toXdr(); | ||
} |
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,32 @@ | ||
package org.stellar.base; | ||
|
||
import org.stellar.base.xdr.MemoType; | ||
import org.stellar.base.xdr.Uint64; | ||
|
||
/** | ||
* Represents MEMO_ID. | ||
*/ | ||
public class MemoId extends Memo { | ||
private long id; | ||
|
||
public MemoId(long id) { | ||
if (id < 0) { | ||
throw new IllegalArgumentException("id must be a positive number"); | ||
} | ||
this.id = id; | ||
} | ||
|
||
public long getId() { | ||
return id; | ||
} | ||
|
||
@Override | ||
org.stellar.base.xdr.Memo toXdr() { | ||
org.stellar.base.xdr.Memo memo = new org.stellar.base.xdr.Memo(); | ||
memo.setDiscriminant(MemoType.MEMO_ID); | ||
Uint64 idXdr = new Uint64(); | ||
idXdr.setUint64(id); | ||
memo.setId(idXdr); | ||
return memo; | ||
} | ||
} |
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,15 @@ | ||
package org.stellar.base; | ||
|
||
import org.stellar.base.xdr.MemoType; | ||
|
||
/** | ||
* Represents MEMO_NONE. | ||
*/ | ||
public class MemoNone extends Memo { | ||
@Override | ||
org.stellar.base.xdr.Memo toXdr() { | ||
org.stellar.base.xdr.Memo memo = new org.stellar.base.xdr.Memo(); | ||
memo.setDiscriminant(MemoType.MEMO_NONE); | ||
return memo; | ||
} | ||
} |
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,30 @@ | ||
package org.stellar.base; | ||
|
||
import org.apache.commons.codec.DecoderException; | ||
import org.stellar.base.xdr.Memo; | ||
import org.stellar.base.xdr.MemoType; | ||
|
||
/** | ||
* Represents MEMO_RETURN. | ||
*/ | ||
public class MemoReturnHash extends MemoHashAbstract { | ||
public MemoReturnHash(byte[] bytes) { | ||
super(bytes); | ||
} | ||
|
||
public MemoReturnHash(String hexString) throws DecoderException { | ||
super(hexString); | ||
} | ||
|
||
@Override | ||
Memo toXdr() { | ||
org.stellar.base.xdr.Memo memo = new org.stellar.base.xdr.Memo(); | ||
memo.setDiscriminant(MemoType.MEMO_RETURN); | ||
|
||
org.stellar.base.xdr.Hash hash = new org.stellar.base.xdr.Hash(); | ||
hash.setHash(bytes); | ||
|
||
memo.setHash(hash); | ||
return memo; | ||
} | ||
} |
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,35 @@ | ||
package org.stellar.base; | ||
|
||
import org.stellar.base.xdr.MemoType; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
|
||
import static com.google.common.base.Preconditions.checkNotNull; | ||
|
||
/** | ||
* Represents MEMO_TEXT. | ||
*/ | ||
public class MemoText extends Memo { | ||
private String text; | ||
|
||
public MemoText(String text) { | ||
this.text = checkNotNull(text, "text cannot be null"); | ||
|
||
int length = text.getBytes((StandardCharsets.UTF_8)).length; | ||
if (length > 28) { | ||
throw new MemoTooLongException("text must be <= 28 bytes. length=" + String.valueOf(length)); | ||
} | ||
} | ||
|
||
public String getText() { | ||
return text; | ||
} | ||
|
||
@Override | ||
org.stellar.base.xdr.Memo toXdr() { | ||
org.stellar.base.xdr.Memo memo = new org.stellar.base.xdr.Memo(); | ||
memo.setDiscriminant(MemoType.MEMO_TEXT); | ||
memo.setText(text); | ||
return memo; | ||
} | ||
} |
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.