2016-12-07 18:46:22 +03:00
|
|
|
package lnwire
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
2016-12-22 23:24:48 +03:00
|
|
|
|
|
|
|
"github.com/roasbeef/btcd/btcec"
|
2017-01-06 00:56:27 +03:00
|
|
|
"github.com/roasbeef/btcd/chaincfg/chainhash"
|
2016-12-07 18:46:22 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestChannelAnnoucementEncodeDecode(t *testing.T) {
|
|
|
|
ca := &ChannelAnnouncement{
|
2017-03-27 18:22:37 +03:00
|
|
|
NodeSig1: someSig,
|
|
|
|
NodeSig2: someSig,
|
2017-04-17 01:22:20 +03:00
|
|
|
ShortChannelID: someShortChannelID,
|
2017-03-27 18:22:37 +03:00
|
|
|
BitcoinSig1: someSig,
|
|
|
|
BitcoinSig2: someSig,
|
|
|
|
NodeID1: pubKey,
|
|
|
|
NodeID2: pubKey,
|
|
|
|
BitcoinKey1: pubKey,
|
|
|
|
BitcoinKey2: pubKey,
|
2016-12-07 18:46:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Next encode the CA message into an empty bytes buffer.
|
|
|
|
var b bytes.Buffer
|
|
|
|
if err := ca.Encode(&b, 0); err != nil {
|
|
|
|
t.Fatalf("unable to encode ChannelAnnouncement: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-12-24 23:49:52 +03:00
|
|
|
// Ensure the max payload estimate is correct.
|
|
|
|
serializedLength := uint32(b.Len())
|
|
|
|
if serializedLength != ca.MaxPayloadLength(0) {
|
|
|
|
t.Fatalf("payload length estimate is incorrect: expected %v "+
|
|
|
|
"got %v", serializedLength, ca.MaxPayloadLength(0))
|
|
|
|
}
|
|
|
|
|
2016-12-07 18:46:22 +03:00
|
|
|
// Deserialize the encoded CA message into a new empty struct.
|
|
|
|
ca2 := &ChannelAnnouncement{}
|
|
|
|
if err := ca2.Decode(&b, 0); err != nil {
|
|
|
|
t.Fatalf("unable to decode ChannelAnnouncement: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assert equality of the two instances.
|
|
|
|
if !reflect.DeepEqual(ca, ca2) {
|
|
|
|
t.Fatalf("encode/decode error messages don't match %#v vs %#v",
|
|
|
|
ca, ca2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestChannelAnnoucementValidation(t *testing.T) {
|
|
|
|
getKeys := func(s string) (*btcec.PrivateKey, *btcec.PublicKey) {
|
|
|
|
return btcec.PrivKeyFromBytes(btcec.S256(), []byte(s))
|
|
|
|
}
|
|
|
|
|
|
|
|
firstNodePrivKey, firstNodePubKey := getKeys("node-id-1")
|
|
|
|
secondNodePrivKey, secondNodePubKey := getKeys("node-id-2")
|
|
|
|
firstBitcoinPrivKey, firstBitcoinPubKey := getKeys("bitcoin-key-1")
|
|
|
|
secondBitcoinPrivKey, secondBitcoinPubKey := getKeys("bitcoin-key-2")
|
|
|
|
|
2017-03-16 14:04:19 +03:00
|
|
|
hash := chainhash.DoubleHashB(firstNodePubKey.SerializeCompressed())
|
2016-12-07 18:46:22 +03:00
|
|
|
firstBitcoinSig, _ := firstBitcoinPrivKey.Sign(hash)
|
|
|
|
|
2017-01-06 00:56:27 +03:00
|
|
|
hash = chainhash.DoubleHashB(secondNodePubKey.SerializeCompressed())
|
2016-12-07 18:46:22 +03:00
|
|
|
secondBitcoinSig, _ := secondBitcoinPrivKey.Sign(hash)
|
|
|
|
|
|
|
|
ca := &ChannelAnnouncement{
|
2017-04-17 01:22:20 +03:00
|
|
|
ShortChannelID: someShortChannelID,
|
2017-03-27 18:22:37 +03:00
|
|
|
BitcoinSig1: firstBitcoinSig,
|
|
|
|
BitcoinSig2: secondBitcoinSig,
|
|
|
|
NodeID1: firstNodePubKey,
|
|
|
|
NodeID2: secondNodePubKey,
|
|
|
|
BitcoinKey1: firstBitcoinPubKey,
|
|
|
|
BitcoinKey2: secondBitcoinPubKey,
|
2016-12-07 18:46:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
dataToSign, _ := ca.DataToSign()
|
2017-01-06 00:56:27 +03:00
|
|
|
hash = chainhash.DoubleHashB(dataToSign)
|
2016-12-07 18:46:22 +03:00
|
|
|
|
|
|
|
firstNodeSign, _ := firstNodePrivKey.Sign(hash)
|
2017-03-27 18:22:37 +03:00
|
|
|
ca.NodeSig1 = firstNodeSign
|
2016-12-07 18:46:22 +03:00
|
|
|
|
|
|
|
secondNodeSign, _ := secondNodePrivKey.Sign(hash)
|
2017-03-27 18:22:37 +03:00
|
|
|
ca.NodeSig2 = secondNodeSign
|
2016-12-07 18:46:22 +03:00
|
|
|
|
|
|
|
if err := ca.Validate(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|