From 9e5f302288264bf84fed8f1e471a2cdf48b1eee1 Mon Sep 17 00:00:00 2001 From: Tadge Dryja Date: Sun, 17 Jan 2016 10:45:07 -0800 Subject: [PATCH] move lnadr from main to lndc package --- lndc/lnadr.go | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++ peer.go | 89 ++----------------------------------------------- rpcserver.go | 3 +- server.go | 12 +++---- 4 files changed, 102 insertions(+), 94 deletions(-) create mode 100644 lndc/lnadr.go diff --git a/lndc/lnadr.go b/lndc/lnadr.go new file mode 100644 index 00000000..066a41b0 --- /dev/null +++ b/lndc/lnadr.go @@ -0,0 +1,92 @@ +package lndc + +import ( + "encoding/hex" + "fmt" + "net" + "strings" + + "github.com/btcsuite/btcd/btcec" + "github.com/btcsuite/btcd/chaincfg" + "github.com/btcsuite/btcutil" +) + +// lnAddr... +type LNAdr struct { + lnId [16]byte // redundant because adr contains it + PubKey *btcec.PublicKey + + Base58Addr btcutil.Address // Base58 encoded address (1XXX...) + NetAddr *net.TCPAddr // IP address + + name string // human readable name? Not a thing yet. + endorsement []byte // a sig confirming the name? Not implemented +} + +// String... +func (l *LNAdr) String() string { + var encodedId []byte + if l.PubKey == nil { + encodedId = l.Base58Addr.ScriptAddress() + } else { + encodedId = l.PubKey.SerializeCompressed() + } + + return fmt.Sprintf("%v@%v", encodedId, l.NetAddr) +} + +// newLnAddr... +func LnAddrFromString(encodedAddr string) (*LNAdr, error) { + // The format of an lnaddr is "@host" + idHost := strings.Split(encodedAddr, "@") + if len(idHost) != 2 { + return nil, fmt.Errorf("invalid format for lnaddr string: %v", encodedAddr) + } + + // Attempt to resolve the IP address, this handles parsing IPv6 zones, + // and such. + fmt.Println("host: ", idHost[1]) + ipAddr, err := net.ResolveTCPAddr("tcp", idHost[1]) + if err != nil { + return nil, err + } + + addr := &LNAdr{NetAddr: ipAddr} + + idLen := len(idHost[0]) + switch { + // Is the ID a hex-encoded compressed public key? + case idLen > 65 && idLen < 69: + pubkeyBytes, err := hex.DecodeString(idHost[0]) + if err != nil { + return nil, err + } + + addr.PubKey, err = btcec.ParsePubKey(pubkeyBytes, btcec.S256()) + if err != nil { + return nil, err + } + + // got pubey, populate address from pubkey + pkh := btcutil.Hash160(addr.PubKey.SerializeCompressed()) + addr.Base58Addr, err = btcutil.NewAddressPubKeyHash(pkh, + &chaincfg.TestNet3Params) + if err != nil { + return nil, err + } + // Is the ID a string encoded bitcoin address? + case idLen > 33 && idLen < 37: + addr.Base58Addr, err = btcutil.DecodeAddress(idHost[0], + &chaincfg.TestNet3Params) + if err != nil { + return nil, err + } + default: + return nil, fmt.Errorf("invalid address %s", idHost[0]) + } + + // Finally, populate the lnid from the address. + copy(addr.lnId[:], addr.Base58Addr.ScriptAddress()) + + return addr, nil +} diff --git a/peer.go b/peer.go index db8b7942..d36aac44 100644 --- a/peer.go +++ b/peer.go @@ -2,18 +2,13 @@ package main import ( "container/list" - "encoding/hex" - "fmt" "net" - "strings" "sync" "sync/atomic" "time" - "github.com/btcsuite/btcd/btcec" - "github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcd/wire" - "github.com/btcsuite/btcutil" + "github.com/lightningnetwork/lnd/lndc" "li.lan/labs/plasma/lnwallet" "li.lan/labs/plasma/lnwire" ) @@ -41,86 +36,6 @@ const ( outgoingQueueLen = 50 ) -// lnAddr... -type lnAddr struct { - lnId [16]byte // redundant because adr contains it - pubKey *btcec.PublicKey - - bitcoinAddr btcutil.Address - netAddr *net.TCPAddr - - name string - endorsement []byte -} - -// String... -func (l *lnAddr) String() string { - var encodedId []byte - if l.pubKey == nil { - encodedId = l.bitcoinAddr.ScriptAddress() - } else { - encodedId = l.pubKey.SerializeCompressed() - } - - return fmt.Sprintf("%v@%v", encodedId, l.netAddr) -} - -// newLnAddr... -func newLnAddr(encodedAddr string) (*lnAddr, error) { - // The format of an lnaddr is "@host" - idHost := strings.Split(encodedAddr, "@") - if len(idHost) != 2 { - return nil, fmt.Errorf("invalid format for lnaddr string: %v", encodedAddr) - } - - // Attempt to resolve the IP address, this handles parsing IPv6 zones, - // and such. - fmt.Println("host: ", idHost[1]) - ipAddr, err := net.ResolveTCPAddr("tcp", idHost[1]) - if err != nil { - return nil, err - } - - addr := &lnAddr{netAddr: ipAddr} - - idLen := len(idHost[0]) - switch { - // Is the ID a hex-encoded compressed public key? - case idLen > 65 && idLen < 69: - pubkeyBytes, err := hex.DecodeString(idHost[0]) - if err != nil { - return nil, err - } - - addr.pubKey, err = btcec.ParsePubKey(pubkeyBytes, btcec.S256()) - if err != nil { - return nil, err - } - - // got pubey, populate address from pubkey - pkh := btcutil.Hash160(addr.pubKey.SerializeCompressed()) - addr.bitcoinAddr, err = btcutil.NewAddressPubKeyHash(pkh, - &chaincfg.TestNet3Params) - if err != nil { - return nil, err - } - // Is the ID a string encoded bitcoin address? - case idLen > 33 && idLen < 37: - addr.bitcoinAddr, err = btcutil.DecodeAddress(idHost[0], - &chaincfg.TestNet3Params) - if err != nil { - return nil, err - } - default: - return nil, fmt.Errorf("invalid address %s", idHost[0]) - } - - // Finally, populate the lnid from the address. - copy(addr.lnId[:], addr.bitcoinAddr.ScriptAddress()) - - return addr, nil -} - // outgoinMsg... type outgoinMsg struct { msg lnwire.Message @@ -137,7 +52,7 @@ type peer struct { conn net.Conn - lightningAddr lnAddr + lightningAddr lndc.LNAdr inbound bool protocolVersion uint32 peerId int32 diff --git a/rpcserver.go b/rpcserver.go index b32789dd..acaa9776 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -9,6 +9,7 @@ import ( "github.com/btcsuite/btcutil" "github.com/btcsuite/btcwallet/waddrmgr" + "github.com/lightningnetwork/lnd/lndc" "github.com/lightningnetwork/lnd/lnrpc" "golang.org/x/net/context" ) @@ -92,7 +93,7 @@ func (r *rpcServer) ConnectPeer(ctx context.Context, return nil, fmt.Errorf("need: lnc pubkeyhash@hostname") } - peerAddr, err := newLnAddr(in.IdAtHost) + peerAddr, err := lndc.LnAddrFromString(in.IdAtHost) if err != nil { return nil, err } diff --git a/server.go b/server.go index 206ae226..9aa0b7dc 100644 --- a/server.go +++ b/server.go @@ -110,7 +110,7 @@ out: // connectPeerMsg... type connectPeerMsg struct { - addr *lnAddr + addr *lndc.LNAdr reply chan error } @@ -143,10 +143,10 @@ out: // either need a compressed pubkey, or a // 20-byte pkh. var remoteId []byte - if addr.pubKey == nil { - remoteId = addr.bitcoinAddr.ScriptAddress() + if addr.PubKey == nil { + remoteId = addr.Base58Addr.ScriptAddress() } else { - remoteId = addr.pubKey.SerializeCompressed() + remoteId = addr.PubKey.SerializeCompressed() } // Attempt to connect to the remote @@ -154,7 +154,7 @@ out: // connection, or the crypto negotation // breaks down, then return an error to the // caller. - ipAddr := addr.netAddr.String() + ipAddr := addr.NetAddr.String() conn := lndc.NewConn(s.longTermPriv, nil) if err := conn.Dial(ipAddr, remoteId); err != nil { msg.reply <- err @@ -178,7 +178,7 @@ out: } // ConnectToPeer... -func (s *server) ConnectToPeer(addr *lnAddr) error { +func (s *server) ConnectToPeer(addr *lndc.LNAdr) error { reply := make(chan error, 1) s.queries <- &connectPeerMsg{addr, reply}