From f4b3311687ba32e92a79268cb603dc3bcbfbad70 Mon Sep 17 00:00:00 2001
From: joshemb <josh@elixxir.io>
Date: Thu, 28 Jul 2022 10:05:07 -0700
Subject: [PATCH] Add GetNDF for gateways to the API

---
 cmd/getndf.go | 37 +++++++++++++++++--------------------
 xxdk/ndf.go   | 42 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 59 insertions(+), 20 deletions(-)

diff --git a/cmd/getndf.go b/cmd/getndf.go
index 12299183a..4a2145385 100644
--- a/cmd/getndf.go
+++ b/cmd/getndf.go
@@ -13,18 +13,18 @@ import (
 	"github.com/spf13/cobra"
 	jww "github.com/spf13/jwalterweatherman"
 	"github.com/spf13/viper"
+	"gitlab.com/elixxir/comms/client"
+
 	// "gitlab.com/elixxir/crypto/contact"
 	// "gitlab.com/elixxir/client/interfaces/message"
 	// "gitlab.com/elixxir/client/switchboard"
 	// "gitlab.com/elixxir/client/ud"
 	// "gitlab.com/elixxir/primitives/fact"
 	"gitlab.com/elixxir/client/xxdk"
-	"gitlab.com/elixxir/comms/client"
 	"gitlab.com/xx_network/comms/connect"
 	//"time"
 	pb "gitlab.com/elixxir/comms/mixmessages"
 	"gitlab.com/xx_network/primitives/id"
-	"gitlab.com/xx_network/primitives/id/ephemeral"
 	"gitlab.com/xx_network/primitives/utils"
 )
 
@@ -99,38 +99,35 @@ var getNDFCmd = &cobra.Command{
 					opensslCertDL)
 			}
 
-			params := connect.GetDefaultHostParams()
-			params.AuthEnabled = false
-			comms, _ := client.NewClientComms(nil, nil, nil, nil)
 			// Gateway lookup
 			if gwHost != "" {
-				host, _ := connect.NewHost(&id.TempGateway, gwHost,
-					cert, params)
-				dummyID := ephemeral.ReservedIDs[0]
-				pollMsg := &pb.GatewayPoll{
-					Partial: &pb.NDFHash{
-						Hash: nil,
-					},
-					LastUpdate:    uint64(0),
-					ReceptionID:   dummyID[:],
-					ClientVersion: []byte(xxdk.SEMVER),
-				}
-				resp, err := comms.SendPoll(host, pollMsg)
+				resp, err := xxdk.DownloadNdfFromGateway(gwHost, cert)
+				fmt.Printf("%s", resp.PartialNDF.Ndf)
 				if err != nil {
-					jww.FATAL.Panicf("Unable to poll %s for NDF:"+
-						" %+v",
-						gwHost, err)
+					jww.FATAL.Panicf("%v", err)
 				}
 				fmt.Printf("%s", resp.PartialNDF.Ndf)
 				return
 			}
 
 			if permHost != "" {
+				// Establish parameters for gRPC
+				params := connect.GetDefaultHostParams()
+				params.AuthEnabled = false
+
+				// Construct client's gRPC comms object
+				comms, _ := client.NewClientComms(nil, nil, nil, nil)
+
+				// Establish host for scheduling server
 				host, _ := connect.NewHost(&id.Permissioning, permHost,
 					cert, params)
+
+				// Construct a dummy message
 				pollMsg := &pb.NDFHash{
 					Hash: []byte("DummyUserRequest"),
 				}
+
+				// Send request to scheduling and get response
 				resp, err := comms.RequestNdf(host, pollMsg)
 				if err != nil {
 					jww.FATAL.Panicf("Unable to ask %s for NDF:"+
diff --git a/xxdk/ndf.go b/xxdk/ndf.go
index c37f72f49..01de46358 100644
--- a/xxdk/ndf.go
+++ b/xxdk/ndf.go
@@ -11,14 +11,56 @@ import (
 	"encoding/base64"
 	"github.com/pkg/errors"
 	jww "github.com/spf13/jwalterweatherman"
+	"gitlab.com/elixxir/comms/client"
 	pb "gitlab.com/elixxir/comms/mixmessages"
+	"gitlab.com/xx_network/comms/connect"
 	"gitlab.com/xx_network/comms/signature"
 	"gitlab.com/xx_network/crypto/tls"
+	"gitlab.com/xx_network/primitives/id"
+	"gitlab.com/xx_network/primitives/id/ephemeral"
 	"google.golang.org/protobuf/proto"
 	"io/ioutil"
 	"net/http"
 )
 
+// DownloadNdfFromGateway will download an NDF from a gateway on the cMix network.
+// It will take the given address and certificate and send a request to a gateway
+// for an NDF over HTTP/2 using the xx network's gRPC implementation.
+func DownloadNdfFromGateway(address string, cert []byte) (
+	*pb.GatewayPollResponse, error) {
+	// Establish parameters for gRPC
+	params := connect.GetDefaultHostParams()
+	params.AuthEnabled = false
+
+	// Construct client's gRPC comms object
+	comms, err := client.NewClientComms(nil, nil, nil, nil)
+	if err != nil {
+		return nil, err
+	}
+
+	// Construct a host off of the gateway to connect to
+	host, err := connect.NewHost(&id.TempGateway, address,
+		cert, params)
+	if err != nil {
+		return nil, err
+	}
+
+	// Construct a Poll message with dummy data.
+	// All that's needed is the NDF
+	dummyID := ephemeral.ReservedIDs[0]
+	pollMsg := &pb.GatewayPoll{
+		Partial: &pb.NDFHash{
+			Hash: nil,
+		},
+		LastUpdate:    uint64(0),
+		ReceptionID:   dummyID[:],
+		ClientVersion: []byte(SEMVER),
+	}
+
+	// Send poll request and receive response containing NDF
+	return comms.SendPoll(host, pollMsg)
+}
+
 // DownloadAndVerifySignedNdfWithUrl retrieves the NDF from a specified URL.
 // The NDF is processed into a protobuf containing a signature that is verified
 // using the cert string passed in. The NDF is returned as marshaled byte data
-- 
GitLab