Skip to content

Commit 0270c45

Browse files
committed
Created zpay32.InvoiceSigner, NodeContextSigner refers to that now.
1 parent 845b765 commit 0270c45

File tree

5 files changed

+62
-12
lines changed

5 files changed

+62
-12
lines changed

channeldb/migration_01_to_11/migration_11_invoices_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@ package migration_01_to_11
22

33
import (
44
"bytes"
5-
"fmt"
65
"testing"
76
"time"
87

98
"github.com/btcsuite/btcd/btcec"
109
bitcoinCfg "github.com/btcsuite/btcd/chaincfg"
1110
"github.com/lightningnetwork/lnd/channeldb/kvdb"
12-
"github.com/lightningnetwork/lnd/lntest/mock"
1311
"github.com/lightningnetwork/lnd/zpay32"
1412
litecoinCfg "github.com/ltcsuite/ltcd/chaincfg"
1513
)
@@ -160,5 +158,6 @@ func getPayReq(net *bitcoinCfg.Params) (string, error) {
160158
}
161159

162160
privKey, _ := btcec.PrivKeyFromBytes(btcec.S256(), testPrivKeyBytes)
163-
return payReq.Encode(NewSingleNodeContextSigner(privKey))
161+
testInvoiceSigner := zpay32.NewMockInvoiceSigner(privKey)
162+
return payReq.Encode(testInvoiceSigner)
164163
}

lnwallet/contextsigner.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/lightningnetwork/lnd/keychain"
99
"github.com/lightningnetwork/lnd/lnwallet/chanfunding"
1010
"github.com/lightningnetwork/lnd/lnwire"
11+
"github.com/lightningnetwork/lnd/zpay32"
1112
)
1213

1314
type ContextSigner interface {
@@ -33,10 +34,7 @@ type NodeContextSigner interface {
3334
SignChannelUpdate(dataToSign []byte) (input.Signature, error)
3435

3536
// Generate a signature for an invoice.
36-
SignInvoice(
37-
humanReadablePart string,
38-
fieldsData []byte,
39-
) ([]byte, []byte, error)
37+
zpay32.InvoiceSigner
4038

4139
// Generate a signature for an ad-hoc message.
4240
SignMessage(dataToSign []byte) ([]byte, error)

zpay32/encode.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,20 @@ import (
77

88
"github.com/btcsuite/btcutil"
99
"github.com/btcsuite/btcutil/bech32"
10-
"github.com/lightningnetwork/lnd/lnwallet"
1110
"github.com/lightningnetwork/lnd/lnwire"
1211
)
1312

13+
type InvoiceSigner interface {
14+
// Generate a signature for an invoice.
15+
SignInvoice(
16+
humanReadablePart string,
17+
fieldsData []byte,
18+
) ([]byte, []byte, error)
19+
}
20+
1421
// Encode takes the given MessageSigner and returns a string encoding this
1522
// invoice signed by the node key of the signer.
16-
func (invoice *Invoice) Encode(signer lnwallet.NodeContextSigner) (string, error) {
23+
func (invoice *Invoice) Encode(signer InvoiceSigner) (string, error) {
1724
// First check that this invoice is valid before starting the encoding.
1825
if err := validateInvoice(invoice); err != nil {
1926
return "", err

zpay32/invoice_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"github.com/btcsuite/btcd/chaincfg/chainhash"
1818
"github.com/btcsuite/btcd/wire"
1919
"github.com/btcsuite/btcutil"
20-
"github.com/lightningnetwork/lnd/lntest/mock"
2120
"github.com/lightningnetwork/lnd/lnwire"
2221

2322
litecoinCfg "github.com/ltcsuite/ltcd/chaincfg"
@@ -101,7 +100,7 @@ var (
101100
},
102101
}
103102

104-
testMessageSigner = mock.NewSingleNodeContextSigner(testPrivKey)
103+
testMessageSigner = NewMockInvoiceSigner(testPrivKey)
105104

106105
emptyFeatures = lnwire.NewFeatureVector(nil, lnwire.Features)
107106

@@ -905,7 +904,7 @@ func TestInvoiceChecksumMalleability(t *testing.T) {
905904
ts := time.Unix(0, 0)
906905

907906
privKey, _ := btcec.PrivKeyFromBytes(btcec.S256(), privKeyBytes)
908-
msgSigner := mock.NewSingleNodeContextSigner(privKey)
907+
msgSigner := NewMockInvoiceSigner(privKey)
909908

910909
opts := []func(*Invoice){Description("test")}
911910
invoice, err := NewInvoice(chain, payHash, ts, opts...)

zpay32/mock.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package zpay32
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/btcsuite/btcd/btcec"
7+
"github.com/btcsuite/btcd/chaincfg/chainhash"
8+
"github.com/btcsuite/btcutil/bech32"
9+
)
10+
11+
type MockInvoiceSigner struct {
12+
privKey *btcec.PrivateKey
13+
}
14+
15+
func NewMockInvoiceSigner(
16+
privKey *btcec.PrivateKey) *MockInvoiceSigner {
17+
return &MockInvoiceSigner{
18+
privKey: privKey,
19+
}
20+
}
21+
22+
// Generate a signature for an invoice.
23+
func (is *MockInvoiceSigner) SignInvoice(
24+
humanReadablePart string,
25+
fieldsData []byte,
26+
) ([]byte, []byte, error) {
27+
// The signature is over the single SHA-256 hash of the hrp + the
28+
// tagged fields encoded in base256.
29+
taggedFieldsBytes, err := bech32.ConvertBits(fieldsData, 5, 8, true)
30+
if err != nil {
31+
return nil, nil, err
32+
}
33+
toSign := append([]byte(humanReadablePart), taggedFieldsBytes...)
34+
hash := chainhash.HashB(toSign)
35+
36+
// Should the signature reference a compressed public key or not.
37+
isCompressedKey := true
38+
39+
// btcec.SignCompact returns a pubkey-recoverable signature
40+
sign, err := btcec.SignCompact(
41+
btcec.S256(), is.privKey, hash, isCompressedKey,
42+
)
43+
if err != nil {
44+
return nil, nil, fmt.Errorf("can't sign the hash: %v", err)
45+
}
46+
return hash, sign, nil
47+
}

0 commit comments

Comments
 (0)