Skip to content

Commit

Permalink
add MoneroUtils.getPaymentUri(config)
Browse files Browse the repository at this point in the history
  • Loading branch information
woodser committed Apr 25, 2024
1 parent bfa0922 commit 007a199
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/main/java/monero/common/MoneroUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URI;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
Expand All @@ -21,6 +23,7 @@
import monero.wallet.model.MoneroAddressType;
import monero.wallet.model.MoneroDecodedAddress;
import monero.wallet.model.MoneroIntegratedAddress;
import monero.wallet.model.MoneroTxConfig;
import org.apache.commons.codec.binary.Hex;
import org.bouncycastle.jcajce.provider.digest.Keccak;

Expand Down Expand Up @@ -587,6 +590,33 @@ 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;
}

/**
* Creates a payment URI from a tx configuration.
*
* TODO: use native bindings to monero-project
*
* @param config specifies configuration for a payment URI
* @return the payment URI
*/
public static String getPaymentUri(MoneroTxConfig config) {
if (config.getAddress() == null) throw new IllegalArgumentException("Payment URI requires an address");
StringBuilder sb = new StringBuilder();
sb.append("monero:");
sb.append(config.getAddress());
StringBuilder paramSb = new StringBuilder();
if (config.getAmount() != null) paramSb.append("&tx_amount=").append(atomicUnitsToXmr(config.getAmount()));
if (config.getRecipientName() != null) paramSb.append("&recipient_name=").append(urlEncode(config.getRecipientName()));
if (config.getNote() != null) paramSb.append("&tx_description=").append(urlEncode(config.getNote()));
if (config.getPaymentId() != null && config.getPaymentId().length() > 0) throw new IllegalArgumentException("Standalone payment id deprecated, use integrated address instead");
char[] paramChars = paramSb.toString().toCharArray();
if (paramChars.length > 0) paramChars[0] = '?';
return sb.toString() + (paramChars.length > 0 ? new String(paramChars) : "");
}

private static String urlEncode(String string) {
return URLEncoder.encode(string, StandardCharsets.UTF_8).replaceAll("\\+", "%20");
}

// ---------------------------- NATIVE BINDINGS -----------------------------

Expand Down
12 changes: 12 additions & 0 deletions src/test/java/TestMoneroUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import monero.wallet.MoneroWallet;
import monero.wallet.MoneroWalletFull;
import monero.wallet.model.MoneroIntegratedAddress;
import monero.wallet.model.MoneroTxConfig;
import monero.wallet.model.MoneroWalletConfig;
import org.junit.jupiter.api.Test;
import utils.TestUtils;
Expand Down Expand Up @@ -232,6 +233,17 @@ public void testAtomicUnitConversion() {
assertEquals(new BigInteger("1250000000000").toString(), MoneroUtils.xmrToAtomicUnits(1.25).toString());
assertEquals(1.25, MoneroUtils.atomicUnitsToXmr(new BigInteger("1250000000000")));
};

@Test
public void testGetPaymentUri() {
MoneroTxConfig config = new MoneroTxConfig()
.setAddress("42U9v3qs5CjZEePHBZHwuSckQXebuZu299NSmVEmQ41YJZQhKcPyujyMSzpDH4VMMVSBo3U3b54JaNvQLwAjqDhKS3rvM3L")
.setAmount(MoneroUtils.xmrToAtomicUnits(0.25))
.setRecipientName("John Doe")
.setNote("My transfer to wallet");
String paymentUri = MoneroUtils.getPaymentUri(config);
assertEquals("monero:42U9v3qs5CjZEePHBZHwuSckQXebuZu299NSmVEmQ41YJZQhKcPyujyMSzpDH4VMMVSBo3U3b54JaNvQLwAjqDhKS3rvM3L?tx_amount=0.25&recipient_name=John%20Doe&tx_description=My%20transfer%20to%20wallet", paymentUri);
}

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

Expand Down

0 comments on commit 007a199

Please sign in to comment.