mirror of
https://github.com/lightningnetwork/lnd.git
synced 2024-11-19 18:10:34 +01:00
7dfe4018ce
This commit was previously split into the following parts to ease review: - 2d746f68: replace imports - 4008f0fd: use ecdsa.Signature - 849e33d1: remove btcec.S256() - b8f6ebbd: use v2 library correctly - fa80bca9: bump go modules
109 lines
2.6 KiB
Go
109 lines
2.6 KiB
Go
package migration_01_to_11
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"math/rand"
|
|
"time"
|
|
|
|
"github.com/btcsuite/btcd/btcec/v2"
|
|
lnwire "github.com/lightningnetwork/lnd/channeldb/migration/lnwire21"
|
|
)
|
|
|
|
var (
|
|
priv, _ = btcec.NewPrivateKey()
|
|
pub = priv.PubKey()
|
|
)
|
|
|
|
func makeFakePayment() *outgoingPayment {
|
|
fakeInvoice := &Invoice{
|
|
// Use single second precision to avoid false positive test
|
|
// failures due to the monotonic time component.
|
|
CreationDate: time.Unix(time.Now().Unix(), 0),
|
|
Memo: []byte("fake memo"),
|
|
Receipt: []byte("fake receipt"),
|
|
PaymentRequest: []byte(""),
|
|
}
|
|
|
|
copy(fakeInvoice.Terms.PaymentPreimage[:], rev[:])
|
|
fakeInvoice.Terms.Value = lnwire.NewMSatFromSatoshis(10000)
|
|
|
|
fakePath := make([][33]byte, 3)
|
|
for i := 0; i < 3; i++ {
|
|
copy(fakePath[i][:], bytes.Repeat([]byte{byte(i)}, 33))
|
|
}
|
|
|
|
fakePayment := &outgoingPayment{
|
|
Invoice: *fakeInvoice,
|
|
Fee: 101,
|
|
Path: fakePath,
|
|
TimeLockLength: 1000,
|
|
}
|
|
copy(fakePayment.PaymentPreimage[:], rev[:])
|
|
return fakePayment
|
|
}
|
|
|
|
// randomBytes creates random []byte with length in range [minLen, maxLen)
|
|
func randomBytes(minLen, maxLen int) ([]byte, error) {
|
|
randBuf := make([]byte, minLen+rand.Intn(maxLen-minLen))
|
|
|
|
if _, err := rand.Read(randBuf); err != nil {
|
|
return nil, fmt.Errorf("Internal error. "+
|
|
"Cannot generate random string: %v", err)
|
|
}
|
|
|
|
return randBuf, nil
|
|
}
|
|
|
|
func makeRandomFakePayment() (*outgoingPayment, error) {
|
|
var err error
|
|
fakeInvoice := &Invoice{
|
|
// Use single second precision to avoid false positive test
|
|
// failures due to the monotonic time component.
|
|
CreationDate: time.Unix(time.Now().Unix(), 0),
|
|
}
|
|
|
|
fakeInvoice.Memo, err = randomBytes(1, 50)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
fakeInvoice.Receipt, err = randomBytes(1, 50)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
fakeInvoice.PaymentRequest, err = randomBytes(1, 50)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
preImg, err := randomBytes(32, 33)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
copy(fakeInvoice.Terms.PaymentPreimage[:], preImg)
|
|
|
|
fakeInvoice.Terms.Value = lnwire.MilliSatoshi(rand.Intn(10000))
|
|
|
|
fakePathLen := 1 + rand.Intn(5)
|
|
fakePath := make([][33]byte, fakePathLen)
|
|
for i := 0; i < fakePathLen; i++ {
|
|
b, err := randomBytes(33, 34)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
copy(fakePath[i][:], b)
|
|
}
|
|
|
|
fakePayment := &outgoingPayment{
|
|
Invoice: *fakeInvoice,
|
|
Fee: lnwire.MilliSatoshi(rand.Intn(1001)),
|
|
Path: fakePath,
|
|
TimeLockLength: uint32(rand.Intn(10000)),
|
|
}
|
|
copy(fakePayment.PaymentPreimage[:], fakeInvoice.Terms.PaymentPreimage[:])
|
|
|
|
return fakePayment, nil
|
|
}
|