<p>As a rule of thumb it is probably best to let lnd generate the preimage for you.</p>

<p>If someone can guess your preimage they have the chance of steeling the payment. It is somewhat similar to guessing your Bitcoin private keys but probably not as bad as they can only get the current payment and technically even that might be tricky. </p>

<p>If you want to do it alone you should keep in mind three things :</p>

<ol>
<li>The preimage should be truly random which for software is always hard to achieve</li>
<li>It should have a large enough size. Flipping a coin might be truely random but as there are only two possible outcomes one could guess / brute force those</li>
<li>It should be unique and never reused. </li>
</ol>

<p>The third point is achieved in precise by following the first two points.</p>

<p>Also looking at BOLT 02 <a href="https://github.com/lightningnetwork/lightning-rfc/blob/master/02-peer-protocol.md#removing-an-htlc-update_fulfill_htlc-update_fail_htlc-and-update_fail_malformed_htlc" rel="nofollow noreferrer">https://github.com/lightningnetwork/lightning-rfc/blob/master/02-peer-protocol.md#removing-an-htlc-update_fulfill_htlc-update_fail_htlc-and-update_fail_malformed_htlc</a> you will see  that the preimage is 32 bytes of data which gives you even less flexibility than my second point suggested. </p>

CC BY-SA 4.0
2019-10-04T13:37:14.773Z
2019-10-04T13:37:14.773Z
Rene Pickhardt
<p>I'm developing API library for <a href="https://api.lightning.community/rest/index.html" rel="nofollow noreferrer">LND REST API</a>.<br/>
Method <a href="https://api.lightning.community/rest/?shell#post-v1-invoices" rel="nofollow noreferrer"><code>POST /v1/invoices</code></a> requires <code>r_preimage</code> parameter.<br/>
Should I generate it or LND can do it itself if I'll not specify it?<br/>
If I should do that - how to do it properly? General description would be enough.</p>

<api><lightning-network><lightning-network-daemon>
How can I generate preimage for lightning network invoice? Should I?
Vyacheslav
# How can I generate preimage for lightning network invoice? Should I?

(Asked by: username_0 on 2019-10-04)

I'm developing API library for [LND REST API](https://api.lightning.community/rest/index.html).  
Method [`POST /v1/invoices`](https://api.lightning.community/rest/?shell#post-v1-invoices) requires `r_preimage` parameter.  
Should I generate it or LND can do it itself if I'll not specify it?  
If I should do that - how to do it properly? General description would be enough.

**Tags:** api, lightning-network, lightning-network-daemon


---

## 1 answers

### Answer by username_1 on 2019-10-04. Score: 1 | Accepted answer

As a rule of thumb it is probably best to let lnd generate the preimage for you.

If someone can guess your preimage they have the chance of steeling the payment. It is somewhat similar to guessing your Bitcoin private keys but probably not as bad as they can only get the current payment and technically even that might be tricky.

If you want to do it alone you should keep in mind three things :

1. The preimage should be truly random which for software is always hard to achieve
2. It should have a large enough size. Flipping a coin might be truely random but as there are only two possible outcomes one could guess / brute force those
3. It should be unique and never reused.

The third point is achieved in precise by following the first two points.

Also looking at BOLT 02 <https://github.com/lightningnetwork/lightning-rfc/blob/master/02-peer-protocol.md#removing-an-htlc-update_fulfill_htlc-update_fail_htlc-and-update_fail_malformed_htlc> you will see that the preimage is 32 bytes of data which gives you even less flexibility than my second point suggested.


---

