Skip to content

Commit

Permalink
monero-java v0.5.2
Browse files Browse the repository at this point in the history
update to monero-project master with pr #7321
test compatibility between full and rpc wallets
support MoneroUtils xmrToAtomicUnits(), atomicUnitsToXmr() conversion
support MoneroTxConfig addDestination(address, amount), setAmount(str)
rename functions to exportOutputs(), exportKeyImages(), describeTxSet()
exportOutputs(), exportKeyImages() can export since last export
remove isLocked alias from MoneroOutputQuery
update spec document
  • Loading branch information
woodser committed Mar 10, 2021
1 parent 00b2021 commit e325620
Show file tree
Hide file tree
Showing 23 changed files with 418 additions and 222 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,13 @@ walletFull.close(true);
<dependency>
<groupId>io.github.monero-ecosystem</groupId>
<artifactId>monero-java</artifactId>
<version>0.5.1</version>
<version>0.5.2</version>
</dependency>
```

#### For Gradle, add to build.gradle:

`compile 'io.github.monero-ecosystem:monero-java:0.5.1'`
`compile 'io.github.monero-ecosystem:monero-java:0.5.2'`

You are now ready to use this library with [monero-daemon-rpc](https://getmonero.org/resources/developer-guides/daemon-rpc.html) and [monero-wallet-rpc](https://getmonero.org/resources/developer-guides/wallet-rpc.html) endpoints.

Expand All @@ -140,7 +140,7 @@ If you want to process binary data or use a client-side wallet instead of RPC, s
Mac: `brew install maven openjdk`
2. `export JAVA_HOME=/path/to/jdk`, for example:<br>
Ubuntu: `export JAVA_HOME=/usr/lib/jvm/default-java`<br>
Mac example: `export JAVA_HOME=/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home`
Mac: `export JAVA_HOME=/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home`
2. Clone the project repository: `git clone https://github.com/monero-ecosystem/monero-java.git`
3. `cd ./monero-java`
3. Install Maven dependencies: `mvn install`
Expand Down
4 changes: 2 additions & 2 deletions bin/update_submodules.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ git pull --ff-only origin master

# update monero-project
cd ./external/monero-project
git checkout tags/v0.17.1.9
git pull --ff-only origin tags/v0.17.1.9
git checkout master
git pull --ff-only origin master
cd ../../../../
2 changes: 1 addition & 1 deletion docs/monero-spec.drawio

Large diffs are not rendered by default.

Binary file modified docs/monero-spec.pdf
Binary file not shown.
2 changes: 1 addition & 1 deletion external/monero-cpp
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.monero-ecosystem</groupId>
<artifactId>monero-java</artifactId>
<version>0.5.1</version>
<version>0.5.2</version>
<name>Monero Java Library</name>
<description>A Java library for using Monero</description>
<url>https://github.com/monero-ecosystem/monero-java</url>
Expand Down
26 changes: 13 additions & 13 deletions src/main/cpp/monero_wallet_jni_bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1078,37 +1078,37 @@ JNIEXPORT jstring JNICALL Java_monero_wallet_MoneroWalletFull_getOutputsJni(JNIE
}
}

JNIEXPORT jstring JNICALL Java_monero_wallet_MoneroWalletFull_getOutputsHexJni(JNIEnv* env, jobject instance) {
MTRACE("Java_monero_wallet_MoneroWalletFull_getOutputsHexJni()");
JNIEXPORT jstring JNICALL Java_monero_wallet_MoneroWalletFull_exportOutputsJni(JNIEnv* env, jobject instance, jboolean all) {
MTRACE("Java_monero_wallet_MoneroWalletFull_exportOutputsJni()");
monero_wallet* wallet = get_handle<monero_wallet>(env, instance, JNI_WALLET_HANDLE);
try {
return env->NewStringUTF(wallet->get_outputs_hex().c_str());
return env->NewStringUTF(wallet->export_outputs(all).c_str());
} catch (...) {
rethrow_cpp_exception_as_java_exception(env);
return 0;
}
}

JNIEXPORT jint JNICALL Java_monero_wallet_MoneroWalletFull_importOutputsHexJni(JNIEnv* env, jobject instance, jstring joutputs_hex) {
MTRACE("Java_monero_wallet_MoneroWalletFull_getOutputsHexJni()");
JNIEXPORT jint JNICALL Java_monero_wallet_MoneroWalletFull_importOutputsJni(JNIEnv* env, jobject instance, jstring joutputs_hex) {
MTRACE("Java_monero_wallet_MoneroWalletFull_exportOutputsJni()");
monero_wallet* wallet = get_handle<monero_wallet>(env, instance, JNI_WALLET_HANDLE);
const char* _outputs_hex = joutputs_hex ? env->GetStringUTFChars(joutputs_hex, NULL) : nullptr;
string outputs_hex = string(_outputs_hex ? _outputs_hex : "");
env->ReleaseStringUTFChars(joutputs_hex, _outputs_hex);
try {
return wallet->import_outputs_hex(outputs_hex);
return wallet->import_outputs(outputs_hex);
} catch (...) {
rethrow_cpp_exception_as_java_exception(env);
return 0;
}
}

JNIEXPORT jstring JNICALL Java_monero_wallet_MoneroWalletFull_getKeyImagesJni(JNIEnv* env, jobject instance) {
MTRACE("Java_monero_wallet_MoneroWalletFull_getKeyImagesJni");
JNIEXPORT jstring JNICALL Java_monero_wallet_MoneroWalletFull_exportKeyImagesJni(JNIEnv* env, jobject instance, jboolean all) {
MTRACE("Java_monero_wallet_MoneroWalletFull_exportKeyImagesJni");
monero_wallet* wallet = get_handle<monero_wallet>(env, instance, JNI_WALLET_HANDLE);

// fetch key images
vector<shared_ptr<monero_key_image>> key_images = wallet->get_key_images();
vector<shared_ptr<monero_key_image>> key_images = wallet->export_key_images(all);
MTRACE("Fetched " << key_images.size() << " key images");

// wrap and serialize key images
Expand Down Expand Up @@ -1245,8 +1245,8 @@ JNIEXPORT jstring JNICALL Java_monero_wallet_MoneroWalletFull_sweepDustJni(JNIEn
return env->NewStringUTF(txs[0]->m_tx_set.get()->serialize().c_str());
}

JNIEXPORT jstring JNICALL Java_monero_wallet_MoneroWalletFull_parseTxSetJni(JNIEnv* env, jobject instance, jstring jtx_set_json) {
MTRACE("Java_monero_wallet_MoneroWalletFull_parseTxSetJson(tx_set_json)");
JNIEXPORT jstring JNICALL Java_monero_wallet_MoneroWalletFull_describeTxSetJni(JNIEnv* env, jobject instance, jstring jtx_set_json) {
MTRACE("Java_monero_wallet_MoneroWalletFull_describeTxSetJson(tx_set_json)");
monero_wallet* wallet = get_handle<monero_wallet>(env, instance, JNI_WALLET_HANDLE);

// get tx set json string
Expand All @@ -1260,10 +1260,10 @@ JNIEXPORT jstring JNICALL Java_monero_wallet_MoneroWalletFull_parseTxSetJni(JNIE
monero_tx_set tx_set = monero_tx_set::deserialize(tx_set_json);

// parse tx set
monero_tx_set parsed_tx_set = wallet->parse_tx_set(tx_set);
monero_tx_set described_tx_set = wallet->describe_tx_set(tx_set);

// serialize and return parsed tx set
return env->NewStringUTF(parsed_tx_set.serialize().c_str());
return env->NewStringUTF(described_tx_set.serialize().c_str());
} catch (...) {
rethrow_cpp_exception_as_java_exception(env);
return 0;
Expand Down
8 changes: 4 additions & 4 deletions src/main/cpp/monero_wallet_jni_bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ JNIEXPORT jstring JNICALL Java_monero_wallet_MoneroWalletFull_getTransfersJni(JN

JNIEXPORT jstring JNICALL Java_monero_wallet_MoneroWalletFull_getOutputsJni(JNIEnv *, jobject, jstring);

JNIEXPORT jstring JNICALL Java_monero_wallet_MoneroWalletFull_getKeyImagesJni(JNIEnv *, jobject);
JNIEXPORT jstring JNICALL Java_monero_wallet_MoneroWalletFull_exportKeyImagesJni(JNIEnv *, jobject, jboolean);

JNIEXPORT jstring JNICALL Java_monero_wallet_MoneroWalletFull_importKeyImagesJni(JNIEnv *, jobject, jstring);

Expand All @@ -158,7 +158,7 @@ JNIEXPORT jstring JNICALL Java_monero_wallet_MoneroWalletFull_sweepOutputJni(JNI

JNIEXPORT jstring JNICALL Java_monero_wallet_MoneroWalletFull_sweepDustJni(JNIEnv *, jobject, jboolean);

JNIEXPORT jstring JNICALL Java_monero_wallet_MoneroWalletFull_parseTxSetJni(JNIEnv *, jobject, jstring);
JNIEXPORT jstring JNICALL Java_monero_wallet_MoneroWalletFull_describeTxSetJni(JNIEnv *, jobject, jstring);

JNIEXPORT jstring JNICALL Java_monero_wallet_MoneroWalletFull_signTxsJni(JNIEnv *, jobject, jstring);

Expand Down Expand Up @@ -192,9 +192,9 @@ JNIEXPORT jstring JNICALL Java_monero_wallet_MoneroWalletFull_createPaymentUriJn

JNIEXPORT jstring JNICALL Java_monero_wallet_MoneroWalletFull_parsePaymentUriJni(JNIEnv *, jobject, jstring);

JNIEXPORT jstring JNICALL Java_monero_wallet_MoneroWalletFull_getOutputsHexJni(JNIEnv *, jobject);
JNIEXPORT jstring JNICALL Java_monero_wallet_MoneroWalletFull_exportOutputsJni(JNIEnv *, jobject, jboolean);

JNIEXPORT jint JNICALL Java_monero_wallet_MoneroWalletFull_importOutputsHexJni(JNIEnv *, jobject, jstring);
JNIEXPORT jint JNICALL Java_monero_wallet_MoneroWalletFull_importOutputsJni(JNIEnv *, jobject, jstring);

JNIEXPORT jobjectArray JNICALL Java_monero_wallet_MoneroWalletFull_getTxNotesJni(JNIEnv *, jobject, jobjectArray);

Expand Down
10 changes: 10 additions & 0 deletions src/main/java/common/utils/GenUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/**
* Collection of general purpose utilities.
Expand Down Expand Up @@ -328,4 +329,13 @@ public static String kvLine(Object key, Object value, int indent, boolean newlin
if (value == null && ignoreUndefined) return "";
return GenUtils.getIndent(indent) + key + ": " + value + (newline ? '\n' : "");
}

/**
* Generates a UUID.
*
* @return the UUID
*/
public static String getUUID() {
return UUID.randomUUID().toString();
}
}
35 changes: 33 additions & 2 deletions src/main/java/monero/common/MoneroUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import common.utils.JsonUtils;
import java.io.File;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -37,11 +38,12 @@ public class MoneroUtils {
* @return the version of this monero-java library
*/
static String getVersion() {
return "0.5.1";
return "0.5.2";
}

public static final int RING_SIZE = 12; // network-enforced ring size


private static long AU_PER_XMR = 1000000000000l;
private static final int NUM_MNEMONIC_WORDS = 25;
private static final int VIEW_KEY_LENGTH = 64;
private static final String ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
Expand Down Expand Up @@ -317,6 +319,35 @@ public static void setJniLogLevel(int level) {
setLogLevelJni(level);
}


/**
* Convert XMR to atomic units.
*
* @param amountXmr amount in XMR to convert to atomic units
* @return amount in atomic units
*/
public static BigInteger xmrToAtomicUnits(double amountXmr) {
double decimalDivisor = 1;
String amountXmrStr = "" + amountXmr;
int decimalIdx = amountXmrStr.indexOf('.');
if (decimalIdx > -1) {
decimalDivisor = Math.pow(10, amountXmrStr.length() - decimalIdx - 1);
amountXmrStr = amountXmrStr.substring(0, decimalIdx) + amountXmrStr.substring(decimalIdx + 1);
}
return new BigInteger(amountXmrStr).multiply(BigInteger.valueOf(MoneroUtils.AU_PER_XMR)).divide(BigInteger.valueOf((long) decimalDivisor));
}

/**
* Convert atomic units to XMR.
*
* @param amountAtomicUnits amount in atomic units to convert to XMR
* @return amount in XMR
*/
public static double atomicUnitsToXmr(BigInteger amountAtomicUnits) {
BigInteger[] quotientAndRemainder = amountAtomicUnits.divideAndRemainder(BigInteger.valueOf(AU_PER_XMR));
return quotientAndRemainder[0].doubleValue() + quotientAndRemainder[1].doubleValue() / MoneroUtils.AU_PER_XMR;
}

// ---------------------------- PRIVATE HELPERS -----------------------------

private native static byte[] jsonToBinaryJni(String json);
Expand Down
36 changes: 26 additions & 10 deletions src/main/java/monero/wallet/MoneroWallet.java
Original file line number Diff line number Diff line change
Expand Up @@ -755,26 +755,42 @@ public interface MoneroWallet {
public List<MoneroOutputWallet> getOutputs(MoneroOutputQuery query);

/**
* Export all outputs in hex format.
* Export outputs since the last export.
*
* @return all outputs in hex format, null if no outputs
* @return outputs since the last export in hex format
*/
public String getOutputsHex();
public String exportOutputs();

/**
* Export outputs in hex format.
*
* @param all exports all outputs if true, else exports the outputs since the last export
* @return outputs in hex format
*/
public String exportOutputs(boolean all);

/**
* Import outputs in hex format.
*
* @param outputsHex are outputs in hex format
* @return the number of outputs imported
*/
public int importOutputsHex(String outputsHex);
public int importOutputs(String outputsHex);

/**
* Export key images since the last export.
*
* @return signed key images since the last export
*/
List<MoneroKeyImage> exportKeyImages();

/**
* Get all signed key images.
* Export signed key images.
*
* @return the wallet's signed key images
* @param all exports all key images if true, else exports the key images since the last export
* @return signed key images
*/
public List<MoneroKeyImage> getKeyImages();
public List<MoneroKeyImage> exportKeyImages(boolean all);

/**
* Import signed key images and verify their spent status.
Expand Down Expand Up @@ -915,12 +931,12 @@ public interface MoneroWallet {
public List<String> relayTxs(List<MoneroTxWallet> txs);

/**
* Parses a tx set containing unsigned or multisig tx hex to a new tx set containing structured transactions.
* Describes a tx set containing unsigned or multisig tx hex to a new tx set containing structured transactions.
*
* @param txSet is a tx set containing unsigned or multisig tx hex
* @return the parsed tx set containing structured transactions
* @return the tx set containing structured transactions
*/
public MoneroTxSet parseTxSet(MoneroTxSet txSet);
public MoneroTxSet describeTxSet(MoneroTxSet txSet);

/**
* Sign unsigned transactions from a view-only wallet.
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/monero/wallet/MoneroWalletBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.Set;
import monero.common.MoneroError;
import monero.common.MoneroRpcConnection;
import monero.daemon.model.MoneroKeyImage;
import monero.wallet.model.MoneroAccount;
import monero.wallet.model.MoneroAddressBookEntry;
import monero.wallet.model.MoneroIncomingTransfer;
Expand Down Expand Up @@ -277,6 +278,16 @@ public List<MoneroOutputWallet> getOutputs() {
return getOutputs(null);
}

@Override
public String exportOutputs() {
return exportOutputs(false);
}

@Override
public List<MoneroKeyImage> exportKeyImages() {
return exportKeyImages(false);
}

@Override
public MoneroTxWallet createTx(MoneroTxConfig config) {
if (config == null) throw new MoneroError("Send request cannot be null");
Expand Down
28 changes: 14 additions & 14 deletions src/main/java/monero/wallet/MoneroWalletFull.java
Original file line number Diff line number Diff line change
Expand Up @@ -817,22 +817,22 @@ public List<MoneroOutputWallet> getOutputs(MoneroOutputQuery query) {
}

@Override
public String getOutputsHex() {
public String exportOutputs(boolean all) {
assertNotClosed();
String outputsHex = getOutputsHexJni();
String outputsHex = exportOutputsJni(all);
return outputsHex.isEmpty() ? null : outputsHex;
}

@Override
public int importOutputsHex(String outputsHex) {
public int importOutputs(String outputsHex) {
assertNotClosed();
return importOutputsHexJni(outputsHex);
return importOutputsJni(outputsHex);
}

@Override
public List<MoneroKeyImage> getKeyImages() {
public List<MoneroKeyImage> exportKeyImages(boolean all) {
assertNotClosed();
String keyImagesJson = getKeyImagesJni();
String keyImagesJson = exportKeyImagesJni(all);
List<MoneroKeyImage> keyImages = JsonUtils.deserialize(MoneroRpcConnection.MAPPER, keyImagesJson, KeyImagesContainer.class).keyImages;
return keyImages;
}
Expand Down Expand Up @@ -937,15 +937,15 @@ public List<MoneroTxWallet> sweepDust(boolean relay) {
}

@Override
public MoneroTxSet parseTxSet(MoneroTxSet txSet) {
public MoneroTxSet describeTxSet(MoneroTxSet txSet) {
assertNotClosed();
String parsedTxSetJson;
String describedTxSetJson;
try {
parsedTxSetJson = parseTxSetJni(JsonUtils.serialize(txSet));
describedTxSetJson = describeTxSetJni(JsonUtils.serialize(txSet));
} catch (Exception e) {
throw new MoneroError(e.getMessage());
}
return JsonUtils.deserialize(parsedTxSetJson, MoneroTxSet.class);
return JsonUtils.deserialize(describedTxSetJson, MoneroTxSet.class);
}

@Override
Expand Down Expand Up @@ -1406,11 +1406,11 @@ public boolean isClosed() {

private native String getOutputsJni(String outputQueryJson);

private native String getOutputsHexJni();
private native String exportOutputsJni(boolean all);

private native int importOutputsHexJni(String outputsHex);
private native int importOutputsJni(String outputsHex);

private native String getKeyImagesJni();
private native String exportKeyImagesJni(boolean all);

private native String importKeyImagesJni(String keyImagesJson);

Expand All @@ -1424,7 +1424,7 @@ public boolean isClosed() {

private native String sweepDustJni(boolean doNotRelay);

private native String parseTxSetJni(String txSetJson);
private native String describeTxSetJni(String txSetJson);

private native String signTxsJni(String unsignedTxHex);

Expand Down
Loading

0 comments on commit e325620

Please sign in to comment.