2019-03-15 12:30:09 +03:00
|
|
|
package wtdb
|
|
|
|
|
|
|
|
import (
|
2019-05-24 06:48:08 +03:00
|
|
|
"io"
|
2019-03-15 12:30:09 +03:00
|
|
|
"net"
|
|
|
|
|
|
|
|
"github.com/btcsuite/btcd/btcec"
|
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
|
|
|
)
|
|
|
|
|
2019-05-24 06:47:22 +03:00
|
|
|
// TowerID is a unique 64-bit identifier allocated to each unique watchtower.
|
|
|
|
// This allows the client to conserve on-disk space by not needing to always
|
|
|
|
// reference towers by their pubkey.
|
|
|
|
type TowerID uint64
|
|
|
|
|
|
|
|
// TowerIDFromBytes constructs a TowerID from the provided byte slice. The
|
|
|
|
// argument must have at least 8 bytes, and should contain the TowerID in
|
|
|
|
// big-endian byte order.
|
|
|
|
func TowerIDFromBytes(towerIDBytes []byte) TowerID {
|
|
|
|
return TowerID(byteOrder.Uint64(towerIDBytes))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bytes encodes a TowerID into an 8-byte slice in big-endian byte order.
|
|
|
|
func (id TowerID) Bytes() []byte {
|
|
|
|
var buf [8]byte
|
|
|
|
byteOrder.PutUint64(buf[:], uint64(id))
|
|
|
|
return buf[:]
|
|
|
|
}
|
|
|
|
|
2019-03-15 12:30:09 +03:00
|
|
|
// Tower holds the necessary components required to connect to a remote tower.
|
|
|
|
// Communication is handled by brontide, and requires both a public key and an
|
|
|
|
// address.
|
|
|
|
type Tower struct {
|
|
|
|
// ID is a unique ID for this record assigned by the database.
|
2019-05-24 06:47:22 +03:00
|
|
|
ID TowerID
|
2019-03-15 12:30:09 +03:00
|
|
|
|
|
|
|
// IdentityKey is the public key of the remote node, used to
|
|
|
|
// authenticate the brontide transport.
|
|
|
|
IdentityKey *btcec.PublicKey
|
|
|
|
|
|
|
|
// Addresses is a list of possible addresses to reach the tower.
|
|
|
|
Addresses []net.Addr
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddAddress adds the given address to the tower's in-memory list of addresses.
|
|
|
|
// If the address's string is already present, the Tower will be left
|
|
|
|
// unmodified. Otherwise, the adddress is prepended to the beginning of the
|
|
|
|
// Tower's addresses, on the assumption that it is fresher than the others.
|
2019-05-24 06:48:08 +03:00
|
|
|
//
|
|
|
|
// NOTE: This method is NOT safe for concurrent use.
|
2019-03-15 12:30:09 +03:00
|
|
|
func (t *Tower) AddAddress(addr net.Addr) {
|
|
|
|
// Ensure we don't add a duplicate address.
|
|
|
|
addrStr := addr.String()
|
|
|
|
for _, existingAddr := range t.Addresses {
|
|
|
|
if existingAddr.String() == addrStr {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add this address to the front of the list, on the assumption that it
|
|
|
|
// is a fresher address and will be tried first.
|
|
|
|
t.Addresses = append([]net.Addr{addr}, t.Addresses...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// LNAddrs generates a list of lnwire.NetAddress from a Tower instance's
|
|
|
|
// addresses. This can be used to have a client try multiple addresses for the
|
|
|
|
// same Tower.
|
2019-05-24 06:48:08 +03:00
|
|
|
//
|
|
|
|
// NOTE: This method is NOT safe for concurrent use.
|
2019-03-15 12:30:09 +03:00
|
|
|
func (t *Tower) LNAddrs() []*lnwire.NetAddress {
|
|
|
|
addrs := make([]*lnwire.NetAddress, 0, len(t.Addresses))
|
|
|
|
for _, addr := range t.Addresses {
|
|
|
|
addrs = append(addrs, &lnwire.NetAddress{
|
|
|
|
IdentityKey: t.IdentityKey,
|
|
|
|
Address: addr,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return addrs
|
|
|
|
}
|
2019-05-24 06:48:08 +03:00
|
|
|
|
|
|
|
// Encode writes the Tower to the passed io.Writer. The TowerID is not
|
|
|
|
// serialized, since it acts as the key.
|
|
|
|
func (t *Tower) Encode(w io.Writer) error {
|
|
|
|
return WriteElements(w,
|
|
|
|
t.IdentityKey,
|
|
|
|
t.Addresses,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decode reads a Tower from the passed io.Reader. The TowerID is meant to be
|
|
|
|
// decoded from the key.
|
|
|
|
func (t *Tower) Decode(r io.Reader) error {
|
|
|
|
return ReadElements(r,
|
|
|
|
&t.IdentityKey,
|
|
|
|
&t.Addresses,
|
|
|
|
)
|
|
|
|
}
|