Skip to content

Commit

Permalink
cleanup connection workflow in lit-af
Browse files Browse the repository at this point in the history
lots of redundant statements checking for things after they'd been
checked, should be good now
  • Loading branch information
Varunram committed Dec 5, 2018
1 parent 6bf4bea commit ffbfb4f
Showing 1 changed file with 37 additions and 65 deletions.
102 changes: 37 additions & 65 deletions cmd/lit-af/lit-af.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,78 +99,50 @@ func (lc *litAfClient) litAfSetup(conf litAfConfig) error {
// pks as well as pk hashes
adr, host, port := lnutil.ParseAdrStringWithPort(conf.Con)
// now we've split the address, check if pkh, if not, route straight to noise_xk
if len(adr) == 44 {
// remote PKH, do standard stuff
fmt.Printf("Adr: %s, Host: %s, Port: %d\n", adr, host, port)
if litrpc.LndcRpcCanConnectLocallyWithHomeDir(defaultDir) && adr == "" && (host == "localhost" || host == "127.0.0.1") {

lc.RPCClient, err = litrpc.NewLocalLndcRpcClientWithHomeDirAndPort(defaultDir, port)
if err != nil {
logging.Fatal(err.Error())
}
} else {
if !lnutil.LitAdrOK(adr) {
logging.Fatal("lit address passed in -con parameter is not valid")
}

keyFilePath := filepath.Join(defaultDir, "lit-af-key.hex")
privKey, err := lnutil.ReadKeyFile(keyFilePath)
if err != nil {
logging.Fatal(err.Error())
}
key, _ := koblitz.PrivKeyFromBytes(koblitz.S256(), privKey[:])
pubkey := key.PubKey().SerializeCompressed() // this is in bytes
fmt.Printf("The pubkey of this lit-af instance is: %s\n", hex.EncodeToString(pubkey))
var temp [33]byte
copy(temp[:], pubkey[:33])
fmt.Printf("The pkh of this lit-af instance is: %s\n", lnutil.LitAdrFromPubkey(temp))
if adr != "" && strings.HasPrefix(adr, "ln1") && host == "" {
ipv4, _, err := lnutil.Lookup(adr, conf.Tracker, "")
if err != nil {
logging.Fatalf("Error looking up address on the tracker: %s", err)
} else {
adr = fmt.Sprintf("%s@%s", adr, ipv4)
}
} else {
adr = fmt.Sprintf("%s@%s:%d", adr, host, port)
}

lc.RPCClient, err = litrpc.NewLndcRpcClient(adr, key)
if err != nil {
logging.Fatal(err.Error())
}
}
} else if len(adr) == 66 {
// remote PK, don't convert to remotePKH
// we have a pubkey, we should't bother checking addresses
// this is definitively a non-lit instance, so don't check for lit stuff here
keyFilePath := filepath.Join(defaultDir, "lit-af-key.hex")
privKey, err := lnutil.ReadKeyFile(keyFilePath)
if err != nil {
logging.Fatal(err.Error())
}
key, _ := koblitz.PrivKeyFromBytes(koblitz.S256(), privKey[:])
pubkey := key.PubKey().SerializeCompressed() // this is in bytes
fmt.Printf("The pubkey of this lit-af instance is: %s\n", hex.EncodeToString(pubkey))
var temp [33]byte
copy(temp[:], pubkey[:33])
fmt.Printf("The pkh of this lit-af instance is: %s\n", lnutil.LitAdrFromPubkey(temp))
// so this is a non lit node and hence, we don't need to lookup on the tracker (since we already have the pubkey)

// initialize a new rpc instance with noise_xk based authentication here
adr = fmt.Sprintf("%s@%s:%d", adr, host, port)
lc.RPCClient, err = litrpc.NewLndcRpcClient(adr, key)
if err != nil {
logging.Fatal(err.Error())
}
} else if len(adr) == 0 {

if len(adr) == 0 {
// so the user didn't provide us with an address to connect to
// we need to connect to the locally running lit-af instance
lc.RPCClient, err = litrpc.NewLocalLndcRpcClientWithHomeDirAndPort(defaultDir, port)
if err != nil {
logging.Fatal(err.Error())
}
return nil
}

keyFilePath := filepath.Join(defaultDir, "lit-af-key.hex")
privKey, err := lnutil.ReadKeyFile(keyFilePath)
if err != nil {
logging.Fatal(err.Error())
}
key, _ := koblitz.PrivKeyFromBytes(koblitz.S256(), privKey[:])
pubkey := key.PubKey().SerializeCompressed() // this is in bytes
fmt.Printf("The pubkey of this lit-af instance is: %s\n", hex.EncodeToString(pubkey))
var temp [33]byte
copy(temp[:], pubkey[:33])
fmt.Printf("The pkh of this lit-af instance is: %s\n", lnutil.LitAdrFromPubkey(temp))

if len(adr) == 44 && !lnutil.LitAdrOK(adr) {
logging.Fatal("lit address passed in -con parameter is not valid")
}

if host == "" {
ipv4, _, err := lnutil.Lookup(adr, conf.Tracker, "")
if err != nil {
logging.Fatalf("Error looking up address on the tracker: %s", err)
}
adr = fmt.Sprintf("%s@%s", adr, ipv4)
} else {
// host is non empty or address is remotePK, doesn't matter since NewLndcRpcClient will take acre of it for us
adr = fmt.Sprintf("%s@%s:%d", adr, host, port)
}

fmt.Printf("Remote Host: %s, Port: %d\n", host, port)
lc.RPCClient, err = litrpc.NewLndcRpcClient(adr, key)
if err != nil {
logging.Fatal(err.Error())
}

return nil
}

Expand Down

0 comments on commit ffbfb4f

Please sign in to comment.