mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-08-27 22:21:18 +02:00
lnwire: add ChannelAnnoucement,NodeAnnoucement,ChannelUpdateAnnoucement messages
This commit is contained in:
committed by
Olaoluwa Osuntokun
parent
d5423f007d
commit
b440005219
94
lnwire/node_announcement_test.go
Normal file
94
lnwire/node_announcement_test.go
Normal file
@@ -0,0 +1,94 @@
|
||||
package lnwire
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/roasbeef/btcd/btcec"
|
||||
"github.com/roasbeef/btcd/wire"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNodeAnnouncementEncodeDecode(t *testing.T) {
|
||||
cua := &NodeAnnouncement{
|
||||
Signature: someSig,
|
||||
Timestamp: maxUint32,
|
||||
Address: someAddress,
|
||||
NodeID: pubKey,
|
||||
RGBColor: someRGB,
|
||||
pad: maxUint16,
|
||||
Alias: someAlias,
|
||||
}
|
||||
|
||||
// Next encode the NA message into an empty bytes buffer.
|
||||
var b bytes.Buffer
|
||||
if err := cua.Encode(&b, 0); err != nil {
|
||||
t.Fatalf("unable to encode NodeAnnouncement: %v", err)
|
||||
}
|
||||
|
||||
// Deserialize the encoded NA message into a new empty struct.
|
||||
cua2 := &NodeAnnouncement{}
|
||||
if err := cua2.Decode(&b, 0); err != nil {
|
||||
t.Fatalf("unable to decode NodeAnnouncement: %v", err)
|
||||
}
|
||||
|
||||
// Assert equality of the two instances.
|
||||
if !reflect.DeepEqual(cua, cua2) {
|
||||
t.Fatalf("encode/decode error messages don't match %#v vs %#v",
|
||||
cua, cua2)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNodeAnnoucementValidation(t *testing.T) {
|
||||
getKeys := func(s string) (*btcec.PrivateKey, *btcec.PublicKey) {
|
||||
return btcec.PrivKeyFromBytes(btcec.S256(), []byte(s))
|
||||
}
|
||||
|
||||
nodePrivKey, nodePubKey := getKeys("node-id-1")
|
||||
|
||||
var hash []byte
|
||||
|
||||
na := &NodeAnnouncement{
|
||||
Timestamp: maxUint32,
|
||||
Address: someAddress,
|
||||
NodeID: nodePubKey,
|
||||
RGBColor: someRGB,
|
||||
pad: maxUint16,
|
||||
Alias: someAlias,
|
||||
}
|
||||
|
||||
dataToSign, _ := na.DataToSign()
|
||||
hash = wire.DoubleSha256(dataToSign)
|
||||
|
||||
signature, _ := nodePrivKey.Sign(hash)
|
||||
na.Signature = signature
|
||||
|
||||
if err := na.Validate(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNodeAnnoucementBadValidation(t *testing.T) {
|
||||
getKeys := func(s string) (*btcec.PrivateKey, *btcec.PublicKey) {
|
||||
return btcec.PrivKeyFromBytes(btcec.S256(), []byte(s))
|
||||
}
|
||||
|
||||
na := &NodeAnnouncement{
|
||||
Timestamp: maxUint32,
|
||||
Address: someAddress,
|
||||
NodeID: pubKey, // wrong pubkey
|
||||
RGBColor: someRGB,
|
||||
pad: maxUint16,
|
||||
Alias: someAlias,
|
||||
}
|
||||
|
||||
nodePrivKey, _ := getKeys("node-id-1")
|
||||
dataToSign, _ := na.DataToSign()
|
||||
hash := wire.DoubleSha256(dataToSign)
|
||||
|
||||
signature, _ := nodePrivKey.Sign(hash)
|
||||
na.Signature = signature
|
||||
|
||||
if err := na.Validate(); err == nil {
|
||||
t.Fatal("error wasn't raised")
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user