Skip to content

Commit

Permalink
channeldb/graph: don't nil curve of returned PubKey
Browse files Browse the repository at this point in the history
Nilling the key would cause signature verification to crash if the
returned key was used.

A test to exercise the behavior is added.
  • Loading branch information
halseth committed May 9, 2019
1 parent c4415f0 commit 21c989f
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 7 deletions.
1 change: 0 additions & 1 deletion channeldb/graph.go
Expand Up @@ -1982,7 +1982,6 @@ func (l *LightningNode) PubKey() (*btcec.PublicKey, error) {
return nil, err
}
l.pubKey = key
l.pubKey.Curve = nil

return key, nil
}
Expand Down
67 changes: 61 additions & 6 deletions channeldb/graph_test.go
Expand Up @@ -41,14 +41,9 @@ var (
testFeatures = lnwire.NewFeatureVector(nil, lnwire.GlobalFeatures)
)

func createTestVertex(db *DB) (*LightningNode, error) {
func createLightningNode(db *DB, priv *btcec.PrivateKey) (*LightningNode, error) {
updateTime := prand.Int63()

priv, err := btcec.NewPrivateKey(btcec.S256())
if err != nil {
return nil, err
}

pub := priv.PubKey().SerializeCompressed()
n := &LightningNode{
HaveNodeAnnouncement: true,
Expand All @@ -65,6 +60,15 @@ func createTestVertex(db *DB) (*LightningNode, error) {
return n, nil
}

func createTestVertex(db *DB) (*LightningNode, error) {
priv, err := btcec.NewPrivateKey(btcec.S256())
if err != nil {
return nil, err
}

return createLightningNode(db, priv)
}

func TestNodeInsertionAndDeletion(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -3005,3 +3009,54 @@ func compareEdgePolicies(a, b *ChannelEdgePolicy) error {
}
return nil
}

// TestLightningNodeSigVerifcation checks that we can use the LightningNode's
// pubkey to verify signatures.
func TestLightningNodeSigVerification(t *testing.T) {
t.Parallel()

// Create some dummy data to sign.
var data [32]byte
if _, err := prand.Read(data[:]); err != nil {
t.Fatalf("unable to read prand: %v", err)
}

// Create private key and sign the data with it.
priv, err := btcec.NewPrivateKey(btcec.S256())
if err != nil {
t.Fatalf("unable to crete priv key: %v", err)
}

sign, err := priv.Sign(data[:])
if err != nil {
t.Fatalf("unable to sign: %v", err)
}

// Sanity check that the signature checks out.
if !sign.Verify(data[:], priv.PubKey()) {
t.Fatalf("signature doesn't check out")
}

// Create a LightningNode from the same private key.
db, cleanUp, err := makeTestDB()
if err != nil {
t.Fatalf("unable to make test database: %v", err)
}
defer cleanUp()

node, err := createLightningNode(db, priv)
if err != nil {
t.Fatalf("unable to create node: %v", err)
}

// And finally check that we can verify the same signature from the
// pubkey returned from the lightning node.
nodePub, err := node.PubKey()
if err != nil {
t.Fatalf("unable to get pubkey: %v", err)
}

if !sign.Verify(data[:], nodePub) {
t.Fatalf("unable to verify sig")
}
}

0 comments on commit 21c989f

Please sign in to comment.