2

I wonder if there is a Java implementation of Monero addresses validation code somewhere?

I've found this JS implementation which seems to work well:

https://github.com/ognus/wallet-address-validator/blob/master/src/monero_validator.js

and, an online version:

https://xmr.llcoins.net/addresstests.html

But I've been unable to make a Java port of this code. As soon as I try ot decode the address to a byte[], using tools such as this one, I get a different array than the one from the JS lib.

For example, this Monero test address: "46oTQPpoxoxQ6XGWwPqEK1YvADH8X91rFC6brLWRHokoAC5qwbYKA2e9jzQyapENw4V2w5Tz1d4LiMSuDhhFCCf77mQgLRA"

The JS lib decodes it to (as an Hex string):

1288a647369f2eff8a13c3b9bc9d1448bed1046d431ec4a842578fcee2d49ec436f3310b57e0b12334429fb90e70304ba799a9ee799536a37a3df25dfd3e96c63bf7c2c4ad

When I try to do the samething in Java using:

byte[] decoded = Base58.decode(address);
String decodedStr = org.apache.commons.codec.binary.Hex.encodeHexString(decoded);

I get:

01375ee6692c3cc848c015498b3ae7660520580d15f8fc6c18ab92b68776e06658820d977b26913464b48a6d2961e95bb366d14fa19775482b7accecdb2f6b0c3c2565c1a3ad

Any idea?

2

Please refer to the following as to why your Base58 decoding mismatches:

Base58 does not strictly specify the format. This results in some implementations being incompatible with others, for example with regard to alphabet order.

...

Monero has its own variant of Base58.

In Monero the Base58 encoding is performed in 8-byte blocks, except the last block which is the remaining (8 or less) bytes .

[src]

I'm not aware of an existing Java implementation that encodes/decodes in the specific way Monero does (as described above), so you will likely need to roll your own, which should be an easy task.

Please also note, the Base58 decoding does not validate an address, it simply decodes the human readable string representation to binary. Depending on your needs, you could perform a very minimal verification of just the length and valid Base58 characters, a slightly fuller verification where you also check the network byte (which doesn't require a full Base58 decode), or you can go all the way doing a full decode, checking the network byte, verifying the checksum and checking the public view and spend keys are valid curve points.

I'd also like to draw your attention to an ecosystem project: monero-java. It currently has very little in the way of address validation (lots of "TODO" comments in the relevant file MoneroUtils.java), so if you do end up writing the relevant code, please consider contributing back to that project.

  • 1
    I made it work! :-) I'm not so proud since I more or less simply copied existing JS/Python code without really understanding it, but at least it works! I also learned that Keccak-256 is another part that is a bit special and requires an extra library in Java. I'll try to make a PR when I have time. Thanks for your help! – electrotype Sep 23 at 2:38
  • 1
1

Monero address validation using native Java has been merged into MoneroUtils.java of the monero-java project based on @electrotype's pull request.

Thanks @electrotype for your pull request and @jtgrassie for pointing to the Java library.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.