Skip to content
Snippets Groups Projects
Commit 54c89def authored by Jemimah Omodior's avatar Jemimah Omodior
Browse files

Update code samples

parent c589e817
No related branches found
No related tags found
No related merge requests found
...@@ -4,33 +4,36 @@ go 1.17 ...@@ -4,33 +4,36 @@ go 1.17
require ( require (
github.com/spf13/jwalterweatherman v1.1.0 github.com/spf13/jwalterweatherman v1.1.0
gitlab.com/elixxir/client v1.5.1-0.20211111194243-5cdddd1adb0d gitlab.com/elixxir/client v1.5.1-0.20220216175337-b35c5f632b80
gitlab.com/elixxir/crypto v0.0.7-0.20211111193842-1e8c19599a2a gitlab.com/elixxir/crypto v0.0.7-0.20220211185439-4a6d9f41f8ab
gitlab.com/xx_network/primitives v0.0.4-0.20220214225457-636c7b31cb78
) )
require ( require (
github.com/badoux/checkmail v1.2.1 // indirect github.com/badoux/checkmail v1.2.1 // indirect
github.com/cloudflare/circl v1.0.1-0.20211008185751-59b49bc148ce // indirect
github.com/elliotchance/orderedmap v1.4.0 // indirect
github.com/golang-collections/collections v0.0.0-20130729185459-604e922904d3 // indirect github.com/golang-collections/collections v0.0.0-20130729185459-604e922904d3 // indirect
github.com/golang/protobuf v1.5.2 // indirect github.com/golang/protobuf v1.5.2 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 // indirect
github.com/pkg/errors v0.9.1 // indirect github.com/pkg/errors v0.9.1 // indirect
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e // indirect github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e // indirect
github.com/ttacon/builder v0.0.0-20170518171403-c099f663e1c2 // indirect github.com/ttacon/builder v0.0.0-20170518171403-c099f663e1c2 // indirect
github.com/ttacon/libphonenumber v1.2.1 // indirect github.com/ttacon/libphonenumber v1.2.1 // indirect
github.com/tyler-smith/go-bip39 v1.1.0 // indirect github.com/tyler-smith/go-bip39 v1.1.0 // indirect
gitlab.com/elixxir/bloomfilter v0.0.0-20200930191214-10e9ac31b228 // indirect gitlab.com/elixxir/bloomfilter v0.0.0-20200930191214-10e9ac31b228 // indirect
gitlab.com/elixxir/comms v0.0.4-0.20211101174956-590ba1b47887 // indirect gitlab.com/elixxir/comms v0.0.4-0.20220214214811-4a1bd320aa45 // indirect
gitlab.com/elixxir/ekv v0.1.5 // indirect gitlab.com/elixxir/ekv v0.1.6 // indirect
gitlab.com/elixxir/primitives v0.0.3-0.20211102233208-a716d5c670b6 // indirect gitlab.com/elixxir/primitives v0.0.3-0.20220104173924-275cb9d7834f // indirect
gitlab.com/xx_network/comms v0.0.4-0.20211014163953-e774276b83ae // indirect gitlab.com/xx_network/comms v0.0.4-0.20220126231737-fe2338016cce // indirect
gitlab.com/xx_network/crypto v0.0.5-0.20211014163843-57b345890686 // indirect gitlab.com/xx_network/crypto v0.0.5-0.20211227194420-f311e8920467 // indirect
gitlab.com/xx_network/primitives v0.0.4-0.20211014163031-53405cf191fb // indirect
gitlab.com/xx_network/ring v0.0.3-0.20210527191221-ce3f170aabd5 // indirect gitlab.com/xx_network/ring v0.0.3-0.20210527191221-ce3f170aabd5 // indirect
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect golang.org/x/crypto v0.0.0-20220128200615-198e4374d7ed // indirect
golang.org/x/net v0.0.0-20210525063256-abc453219eb5 // indirect golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 // indirect
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 // indirect golang.org/x/sys v0.0.0-20210902050250-f475640dd07b // indirect
golang.org/x/text v0.3.6 // indirect golang.org/x/text v0.3.6 // indirect
google.golang.org/genproto v0.0.0-20210105202744-fe13368bc0e1 // indirect google.golang.org/genproto v0.0.0-20210105202744-fe13368bc0e1 // indirect
google.golang.org/grpc v1.38.0 // indirect google.golang.org/grpc v1.42.0 // indirect
google.golang.org/protobuf v1.27.1 // indirect google.golang.org/protobuf v1.27.1 // indirect
) )
This diff is collapsed.
...@@ -11,42 +11,55 @@ import ( ...@@ -11,42 +11,55 @@ import (
"gitlab.com/elixxir/client/interfaces/params" "gitlab.com/elixxir/client/interfaces/params"
"gitlab.com/elixxir/client/switchboard" "gitlab.com/elixxir/client/switchboard"
"gitlab.com/elixxir/crypto/contact" "gitlab.com/elixxir/crypto/contact"
"gitlab.com/xx_network/primitives/id"
// external
jww "github.com/spf13/jwalterweatherman" // logging
) )
func main() { func main() {
// Create a new client object------------------------------------------------------- // Create a new client object-------------------------------------------------------
// You'd ideally use a configuration tool to acquire these parameters // You would ideally use a configuration tool to acquire these parameters
sessionPath := "sessionPath" statePath := "statePath"
sessionPass := "sessionPass" statePass := "password"
// The following connects to mainnet. For historical reasons it is called a json file
// Create the client if there's no session // but it is actually a marshalled file with a cryptographic signature attached.
if _, err := os.Stat(sessionPath); os.IsNotExist(err) { // This may change in the future.
// Load NDF (assumes you've saved it to your current working directory) ndfURL := "https://elixxir-bins.s3.us-west-1.amazonaws.com/ndf/mainnet.json"
// You'd ideally use a configuration tool to acquire this path certificatePath := "mainnet.crt"
ndfPath := "ndf.json" ndfPath := "ndf.json"
ndfJSON, err := ioutil.ReadFile(ndfPath)
// Create the client if there is no session
if _, err := os.Stat(statePath); os.IsNotExist(err) {
var ndfJSON []byte
if ndfPath != "" {
ndfJSON, err = ioutil.ReadFile(ndfPath)
if err != nil {
fmt.Printf("Could not read NDF: %+v", err)
}
}
if ndfJSON == nil {
cert, err := ioutil.ReadFile(certificatePath)
if err != nil { if err != nil {
jww.FATAL.Panicf("Failed to read NDF: %+v", err) fmt.Printf("Failed to read certificate: %v", err)
} }
err = api.NewClient(string(ndfJSON), sessionPath, []byte(sessionPass), "")
ndfJSON, err = api.DownloadAndVerifySignedNdfWithUrl(ndfURL, string(cert))
if err != nil { if err != nil {
jww.FATAL.Panicf("Failed to create new client: %+v", err) fmt.Printf("Failed to download NDF: %+v", err)
}
}
err = api.NewClient(string(ndfJSON), statePath, []byte(statePass), "")
if err != nil {
fmt.Printf("Failed to create new client: %+v", err)
} }
} }
// Login to your client session----------------------------------------------------- // Login to your client session-----------------------------------------------------
// Login with the same sessionPath and sessionPass used to call NewClient() // Login with the same sessionPath and sessionPass used to call NewClient()
// Assumes you've imported "gitlab.com/elixxir/client/interfaces/params" // Assumes you have imported "gitlab.com/elixxir/client/interfaces/params"
client, err := api.Login(sessionPath, []byte(sessionPass), params.GetDefaultNetwork()) client, err := api.Login(statePath, []byte(statePass), params.GetDefaultNetwork())
if err != nil { if err != nil {
jww.FATAL.Panicf("Failed to initialize client: %+v", err) fmt.Printf("Failed to initialize client: %+v", err)
} }
// view current user identity-------------------------------------------------------- // view current user identity--------------------------------------------------------
...@@ -57,34 +70,39 @@ func main() { ...@@ -57,34 +70,39 @@ func main() {
// Set up a reception handler // Set up a reception handler
swboard := client.GetSwitchboard() swboard := client.GetSwitchboard()
receiverChannel := make(chan message.Receive, 10000) // Needs to be large // Note: the receiverChannel needs to be large enough that your reception thread will
// process the messages. If it is too small, messages can be dropped or important xxDK
// threads could be blocked.
receiverChannel := make(chan message.Receive, 10000)
// Note that the name `listenerID` is arbitrary // Note that the name `listenerID` is arbitrary
listenerID := swboard.RegisterChannel("DefaultCLIReceiver", listenerID := swboard.RegisterChannel("DefaultCLIReceiver",
switchboard.AnyUser(), message.Text, receiverChannel) switchboard.AnyUser(), message.XxMessage, receiverChannel)
jww.INFO.Printf("Message ListenerID: %v", listenerID) fmt.Printf("Message ListenerID: %v", listenerID)
// Start network threads------------------------------------------------------------ // Start network threads------------------------------------------------------------
networkFollowerTimeout := 1200 networkFollowerTimeout := 5 * time.Second
// Set networkFollowerTimeout to a value of your choice (seconds)
err = client.StartNetworkFollower(networkFollowerTimeout) err = client.StartNetworkFollower(networkFollowerTimeout)
if err != nil { if err != nil {
jww.FATAL.Panicf("Failed to start network follower: %+v", err) fmt.Printf("Failed to start network follower: %+v", err)
} }
waitUntilConnected := func(connected chan bool) { waitUntilConnected := func(connected chan bool) {
// Assumes you have imported the `time` package
waitTimeout := time.Duration(150) waitTimeout := time.Duration(150)
timeoutTimer := time.NewTimer(waitTimeout * time.Second) timeoutTimer := time.NewTimer(waitTimeout * time.Second)
isConnected := false isConnected := false
//Wait until we connect or panic if we can't by a timeout // Wait until we connect or panic if we cannot by a timeout
for !isConnected { for !isConnected {
select { select {
case isConnected = <-connected: case isConnected = <-connected:
jww.INFO.Printf("Network Status: %v\n", fmt.Printf("Network Status: %v\n",
isConnected) isConnected)
break break
case <-timeoutTimer.C: case <-timeoutTimer.C:
jww.FATAL.Panic("timeout on connection") fmt.Printf("timeout on connection")
} }
} }
} }
...@@ -100,11 +118,11 @@ func main() { ...@@ -100,11 +118,11 @@ func main() {
// Register a handler for authenticated channel requests----------------------------- // Register a handler for authenticated channel requests-----------------------------
// Handler for authenticated channel requests // Handler for authenticated channel requests
confirmChanRequest := func(requestor contact.Contact, message string) { confirmChanRequest := func(requestor contact.Contact) {
// Check if a channel exists for this recipientID // Check if a channel exists for this recipientID
recipientID := requestor.ID recipientID := requestor.ID
if client.HasAuthenticatedChannel(recipientID) { if client.HasAuthenticatedChannel(recipientID) {
jww.INFO.Printf("Authenticated channel already in place for %s", fmt.Printf("Authenticated channel already in place for %s",
recipientID) recipientID)
return return
} }
...@@ -112,17 +130,16 @@ func main() { ...@@ -112,17 +130,16 @@ func main() {
// one exists for the given userID. Returns an error if no contact is found. // one exists for the given userID. Returns an error if no contact is found.
recipientContact, err := client.GetAuthenticatedChannelRequest(recipientID) recipientContact, err := client.GetAuthenticatedChannelRequest(recipientID)
if err == nil { if err == nil {
jww.INFO.Printf("Accepting existing channel request for %s", fmt.Printf("Accepting existing channel request for %s",
recipientID) recipientID)
// ConfirmAuthenticatedChannel() creates an authenticated channel out of a valid // ConfirmAuthenticatedChannel() creates an authenticated channel out of a valid
// received request and informs the requestor that their request has // received request and informs the requestor that their request has
// been confirmed // been confirmed
roundID, err := client.ConfirmAuthenticatedChannel(recipientContact) roundID, err := client.ConfirmAuthenticatedChannel(recipientContact)
fmt.Println("Accepted existing channel request in round ", roundID) fmt.Printf("Accepted existing channel request in round %v",
jww.INFO.Printf("Accepted existing channel request in round %v",
roundID) roundID)
if err != nil { if err != nil {
jww.FATAL.Panicf("%+v", err) fmt.Printf("%+v", err)
} }
return return
} }
...@@ -136,49 +153,45 @@ func main() { ...@@ -136,49 +153,45 @@ func main() {
// Sender's contact for requesting auth channels // Sender's contact for requesting auth channels
me := client.GetUser().GetContact() me := client.GetUser().GetContact()
// Recipient's contact (read from a Client CLI-generated contact file) // Recipient's contact (read from a Client CLI-generated contact file)
contactData, _ := ioutil.ReadFile("../user2/user-contact.json") contactData, _ := ioutil.ReadFile("../user1b/user-contact1b.json")
// Assumes you've imported "gitlab.com/elixxir/crypto/contact" which provides // Assumes you have imported "gitlab.com/elixxir/crypto/contact"
// an `Unmarshal` function to convert the byte slice ([]byte) output // which provides an `Unmarshal` function to convert the byte slice ([]byte) output
// of `ioutil.ReadFile()` to the `Contact` type expected by // of `ioutil.ReadFile()` to the `Contact` type expected by `RequestAuthenticatedChannel()`
// `RequestAuthenticatedChannel()`
recipientContact, _ := contact.Unmarshal(contactData) recipientContact, _ := contact.Unmarshal(contactData)
recipientID := recipientContact.ID recipientID := recipientContact.ID
roundID, authReqErr := client.RequestAuthenticatedChannel(recipientContact, me, "Hi! Let's connect!") roundID, authReqErr := client.RequestAuthenticatedChannel(recipientContact, me, "Hi! Let's connect!")
if authReqErr == nil { if authReqErr == nil {
jww.INFO.Printf("Requested auth channel from: %s in round %d", fmt.Printf("Requested auth channel from: %s in round %d",
recipientID, roundID) recipientID, roundID)
} else { } else {
jww.FATAL.Panicf("%+v", err) fmt.Printf("%+v", err)
} }
// Send a message to another user---------------------------------------------------- // Send a message to another user----------------------------------------------------
// Send safe message with authenticated channel, requires an authenticated channel
// Test message
msgBody := "If this message is sent successfully, we'll have established first contact with aliens." msgBody := "If this message is sent successfully, we'll have established first contact with aliens."
unsafe := client.HasAuthenticatedChannel(recipientID)
msg := message.Send{ msg := message.Send{
Recipient: recipientID, Recipient: recipientID,
Payload: []byte(msgBody), Payload: []byte(msgBody),
MessageType: message.Text, MessageType: message.XxMessage,
} }
// Get default network parameters for E2E payloads
paramsE2E := params.GetDefaultE2E() paramsE2E := params.GetDefaultE2E()
paramsUnsafe := params.GetDefaultUnsafe()
fmt.Printf("Sending to %s: %s\n", recipientID, msgBody) fmt.Printf("Sending to %s: %s\n", recipientID, msgBody)
fmt.Println("Sending to: ", recipientID, " , ", msgBody) roundIDs, _, _, err := client.SendE2E(msg,
var roundIDs []id.Round
if unsafe {
roundIDs, err = client.SendUnsafe(msg,
paramsUnsafe)
} else {
roundIDs, _, _, err = client.SendE2E(msg,
paramsE2E) paramsE2E)
}
if err != nil { if err != nil {
jww.FATAL.Panicf("%+v", err) fmt.Printf("%+v", err)
} }
jww.INFO.Printf("RoundIDs: %+v\n", roundIDs) fmt.Printf("Message sent in RoundIDs: %+v\n", roundIDs)
// Keep app running to receive messages----------------------------------------------- // Keep app running to receive messages-----------------------------------------------
for { for {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment