move lnadr from main to lndc package

This commit is contained in:
Tadge Dryja 2016-01-17 10:45:07 -08:00
parent ba0f86d4dc
commit 9e5f302288
4 changed files with 102 additions and 94 deletions

92
lndc/lnadr.go Normal file

@ -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 "<pubkey or pkh>@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
}

89
peer.go

@ -2,18 +2,13 @@ package main
import ( import (
"container/list" "container/list"
"encoding/hex"
"fmt"
"net" "net"
"strings"
"sync" "sync"
"sync/atomic" "sync/atomic"
"time" "time"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil" "github.com/lightningnetwork/lnd/lndc"
"li.lan/labs/plasma/lnwallet" "li.lan/labs/plasma/lnwallet"
"li.lan/labs/plasma/lnwire" "li.lan/labs/plasma/lnwire"
) )
@ -41,86 +36,6 @@ const (
outgoingQueueLen = 50 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 "<pubkey or pkh>@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... // outgoinMsg...
type outgoinMsg struct { type outgoinMsg struct {
msg lnwire.Message msg lnwire.Message
@ -137,7 +52,7 @@ type peer struct {
conn net.Conn conn net.Conn
lightningAddr lnAddr lightningAddr lndc.LNAdr
inbound bool inbound bool
protocolVersion uint32 protocolVersion uint32
peerId int32 peerId int32

@ -9,6 +9,7 @@ import (
"github.com/btcsuite/btcutil" "github.com/btcsuite/btcutil"
"github.com/btcsuite/btcwallet/waddrmgr" "github.com/btcsuite/btcwallet/waddrmgr"
"github.com/lightningnetwork/lnd/lndc"
"github.com/lightningnetwork/lnd/lnrpc" "github.com/lightningnetwork/lnd/lnrpc"
"golang.org/x/net/context" "golang.org/x/net/context"
) )
@ -92,7 +93,7 @@ func (r *rpcServer) ConnectPeer(ctx context.Context,
return nil, fmt.Errorf("need: lnc pubkeyhash@hostname") return nil, fmt.Errorf("need: lnc pubkeyhash@hostname")
} }
peerAddr, err := newLnAddr(in.IdAtHost) peerAddr, err := lndc.LnAddrFromString(in.IdAtHost)
if err != nil { if err != nil {
return nil, err return nil, err
} }

@ -110,7 +110,7 @@ out:
// connectPeerMsg... // connectPeerMsg...
type connectPeerMsg struct { type connectPeerMsg struct {
addr *lnAddr addr *lndc.LNAdr
reply chan error reply chan error
} }
@ -143,10 +143,10 @@ out:
// either need a compressed pubkey, or a // either need a compressed pubkey, or a
// 20-byte pkh. // 20-byte pkh.
var remoteId []byte var remoteId []byte
if addr.pubKey == nil { if addr.PubKey == nil {
remoteId = addr.bitcoinAddr.ScriptAddress() remoteId = addr.Base58Addr.ScriptAddress()
} else { } else {
remoteId = addr.pubKey.SerializeCompressed() remoteId = addr.PubKey.SerializeCompressed()
} }
// Attempt to connect to the remote // Attempt to connect to the remote
@ -154,7 +154,7 @@ out:
// connection, or the crypto negotation // connection, or the crypto negotation
// breaks down, then return an error to the // breaks down, then return an error to the
// caller. // caller.
ipAddr := addr.netAddr.String() ipAddr := addr.NetAddr.String()
conn := lndc.NewConn(s.longTermPriv, nil) conn := lndc.NewConn(s.longTermPriv, nil)
if err := conn.Dial(ipAddr, remoteId); err != nil { if err := conn.Dial(ipAddr, remoteId); err != nil {
msg.reply <- err msg.reply <- err
@ -178,7 +178,7 @@ out:
} }
// ConnectToPeer... // ConnectToPeer...
func (s *server) ConnectToPeer(addr *lnAddr) error { func (s *server) ConnectToPeer(addr *lndc.LNAdr) error {
reply := make(chan error, 1) reply := make(chan error, 1)
s.queries <- &connectPeerMsg{addr, reply} s.queries <- &connectPeerMsg{addr, reply}