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?

jtgrassie
  • 19,601
  • 4
  • 17
  • 54
electrotype
  • 123
  • 4

2 Answers2

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.

jtgrassie
  • 19,601
  • 4
  • 17
  • 54
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.

woodser
  • 41
  • 1