<p>Method</p>
<pre><code>ECKey.signedMessageToKey
</code></pre>
<p>calls:</p>
<pre><code>formatMessageForSigning(message)
</code></pre>
<p>which uses constant BITCOIN_SIGNED_MESSAGE_HEADER_BYTES
defined:</p>
<pre><code>private static final String BITCOIN_SIGNED_MESSAGE_HEADER = &quot;Bitcoin Signed Message:\n&quot;;
private static final byte[] BITCOIN_SIGNED_MESSAGE_HEADER_BYTES = BITCOIN_SIGNED_MESSAGE_HEADER.getBytes(StandardCharsets.UTF_8);
</code></pre>
<p>I changed to:</p>
<pre><code>BITCOIN_SIGNED_MESSAGE_HEADER = &quot;Litecoin Signed Message:\n&quot;;
</code></pre>
<p>And your code worked with signatures generated by Electrum-ltc.</p>

CC BY-SA 4.0
2022-07-20T16:24:47.930Z
2022-07-20T16:28:27.067Z
2022-07-20T16:28:27.067Z
bartoszpn
<p>I have a problem with java library <code>pokkst/litecoinj</code>. I am using the regular <code>bitcoinj</code> library to verify signed messages with success. As <code>pokkst/litecoinj</code> is a fork of <code>bitcoinj</code> I have expected it to work in same or close manner.</p>
<p>I have a method:</p>
<pre><code>private boolean isSignatureValid(NetworkParameters params, String address, String message, String signature) {
    try {
        Address signingAddress = Address.fromString(params, address);
        ECKey key = ECKey.signedMessageToKey(message, signature);
        Address addressFromKey = Address.fromKey(params, key, signingAddress.getOutputScriptType());
        return signingAddress.equals(addressFromKey);
    } catch (SignatureException | AddressFormatException exception) {
        LOGGER.error(exception.getMessage());
        return false;
    } catch (NullPointerException exception) {
        LOGGER.error(&quot;Address cannot be null!&quot;);
        return false;
    }
}
</code></pre>
<p>Given data (this is just a test wallet so I do not worry about it):</p>
<pre><code>params -&gt; MainNetParams.get();
address -&gt; ltc1qhlnfnzc6djz92ylf89cvsw7jzyl98hx79ye5a5
message -&gt; test-test-test-test-test
signature -&gt; IC6AcfBff2TA1brwVneXPFtUyHCu1hffTvcaTyNM+i5IbNc7UgNIA4bdVu3mpr0awTsaIK7lpY4O5pS4+/SJDUw=
</code></pre>
<p>Important: this address and signature are generated using <code>Electrum-LTC</code> wallet, cause I haven't found any other litecoin wallet that will allow me to sign messages.</p>
<p>When I run a test against this method, i see that <code>addressFromKey</code> is different than <code>signingAddress</code> and it's value is <code>ltc1qewvcwvf9ln6uypme8wskmg5khqwpss7v9zcrj7</code></p>
<p>My code for Bitcoin message verification is very similar, and it is working fine.</p>
<p>I don't have access to the wallet keys, a wallet address, message and signature is all I have.</p>
<p>What am I doing wrong? May this be a bug in the library? Or could it be something wrong with Electrum-LTC?</p>

<litecoin><bitcoinj><java>
pokkst/litecoinj message verification problem - Address.fromKey() returning wrong address
hc0re
# pokkst/litecoinj message verification problem - Address.fromKey() returning wrong address

(Asked by: username_0 on 2022-02-23)

I have a problem with java library `pokkst/litecoinj`. I am using the regular `bitcoinj` library to verify signed messages with success. As `pokkst/litecoinj` is a fork of `bitcoinj` I have expected it to work in same or close manner.

I have a method:

```
private boolean isSignatureValid(NetworkParameters params, String address, String message, String signature) {
    try {
        Address signingAddress = Address.fromString(params, address);
        ECKey key = ECKey.signedMessageToKey(message, signature);
        Address addressFromKey = Address.fromKey(params, key, signingAddress.getOutputScriptType());
        return signingAddress.equals(addressFromKey);
    } catch (SignatureException | AddressFormatException exception) {
        LOGGER.error(exception.getMessage());
        return false;
    } catch (NullPointerException exception) {
        LOGGER.error("Address cannot be null!");
        return false;
    }
}

```

Given data (this is just a test wallet so I do not worry about it):

```
params -> MainNetParams.get();
address -> ltc1qhlnfnzc6djz92ylf89cvsw7jzyl98hx79ye5a5
message -> test-test-test-test-test
signature -> IC6AcfBff2TA1brwVneXPFtUyHCu1hffTvcaTyNM+i5IbNc7UgNIA4bdVu3mpr0awTsaIK7lpY4O5pS4+/SJDUw=

```

Important: this address and signature are generated using `Electrum-LTC` wallet, cause I haven't found any other litecoin wallet that will allow me to sign messages.

When I run a test against this method, i see that `addressFromKey` is different than `signingAddress` and it's value is `ltc1qewvcwvf9ln6uypme8wskmg5khqwpss7v9zcrj7`

My code for Bitcoin message verification is very similar, and it is working fine.

I don't have access to the wallet keys, a wallet address, message and signature is all I have.

What am I doing wrong? May this be a bug in the library? Or could it be something wrong with Electrum-LTC?

**Tags:** litecoin, bitcoinj, java


---

## 1 answers

### Answer by username_1 on 2022-07-20. Score: 2

Method

```
ECKey.signedMessageToKey

```

calls:

```
formatMessageForSigning(message)

```

which uses constant BITCOIN\_SIGNED\_MESSAGE\_HEADER\_BYTES
defined:

```
private static final String BITCOIN_SIGNED_MESSAGE_HEADER = "Bitcoin Signed Message:\n";
private static final byte[] BITCOIN_SIGNED_MESSAGE_HEADER_BYTES = BITCOIN_SIGNED_MESSAGE_HEADER.getBytes(StandardCharsets.UTF_8);

```

I changed to:

```
BITCOIN_SIGNED_MESSAGE_HEADER = "Litecoin Signed Message:\n";

```

And your code worked with signatures generated by Electrum-ltc.


---

