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
2017-12-14 12:04:18 +11:00
committed by Olaoluwa Osuntokun
parent fc18db0130
commit 0f161c5033
6 changed files with 150 additions and 134 deletions

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
}