channeldb+lnrpc: store the payment pre-image not rhash

Fixes #481.

Prior to this commit, payments stored in the channel DB only kept a
record of the payment hash. This is a problem as the preimage is what
serves as proof of payment and a user should be able to look up this
value in the future (not just immediately after payment).

Instead of storing both the payment hash and the preimage, we store the
preimage only since the hash can be derrived from this using a SHA256.

In the RPC listpayments command, we now give the preimage in addition to
the payment hash.
This commit is contained in:
Andrew Naoum authored and Olaoluwa Osuntokun committed 2018-01-27 18:36:33 -08:00
1 parent fc18db0130
commit 0f161c5033
6 files changed
+150 -134

No files matched your search

+5 -7
View File
@@ -37,11 +37,9 @@ type OutgoingPayment struct {
// compressed public key of each of the nodes involved in the payment.
Path [][33]byte
// PaymentHash is the payment hash (r-hash) used to send the payment.
//
// TODO(roasbeef): weave through preimage on payment success to can
// store only supplemental info the embedded Invoice
PaymentHash [32]byte
// PaymentPreimage is the preImage of a successful payment. This is used
// to calculate the PaymentHash as well as serve as a proof of payment.
PaymentPreimage [32]byte
}
// AddPayment saves a successful payment to the database. It is assumed that
@@ -163,7 +161,7 @@ func serializeOutgoingPayment(w io.Writer, p *OutgoingPayment) error {
return err
}
if _, err := w.Write(p.PaymentHash[:]); err != nil {
if _, err := w.Write(p.PaymentPreimage[:]); err != nil {
return err
}
@@ -204,7 +202,7 @@ func deserializeOutgoingPayment(r io.Reader) (*OutgoingPayment, error) {
}
p.TimeLockLength = byteOrder.Uint32(scratch[:4])
if _, err := r.Read(p.PaymentHash[:]); err != nil {
if _, err := r.Read(p.PaymentPreimage[:]); err != nil {
return nil, err
}
+4 -5
View File
@@ -2,7 +2,6 @@ package channeldb
import (
"bytes"
"crypto/sha256"
"fmt"
"math/rand"
"reflect"
@@ -31,13 +30,14 @@ func makeFakePayment() *OutgoingPayment {
copy(fakePath[i][:], bytes.Repeat([]byte{byte(i)}, 33))
}
return &OutgoingPayment{
fakePayment := &OutgoingPayment{
Invoice: *fakeInvoice,
Fee: 101,
Path: fakePath,
TimeLockLength: 1000,
PaymentHash: sha256.Sum256(rev[:]),
}
copy(fakePayment.PaymentPreimage[:], rev[:])
return fakePayment
}
// randomBytes creates random []byte with length in range [minLen, maxLen)
@@ -90,14 +90,13 @@ func makeRandomFakePayment() (*OutgoingPayment, error) {
copy(fakePath[i][:], b)
}
rHash := sha256.Sum256(fakeInvoice.Terms.PaymentPreimage[:])
fakePayment := &OutgoingPayment{
Invoice: *fakeInvoice,
Fee: lnwire.MilliSatoshi(rand.Intn(1001)),
Path: fakePath,
TimeLockLength: uint32(rand.Intn(10000)),
PaymentHash: rHash,
}
copy(fakePayment.PaymentPreimage[:], fakeInvoice.Terms.PaymentPreimage[:])
return fakePayment, nil
}