From 088fab977d0bdfac4a89f8b19419b0adf0c11e21 Mon Sep 17 00:00:00 2001 From: Adam Babik Date: Fri, 31 May 2019 09:50:25 +0200 Subject: [PATCH] add -add-contact flag (#71) --- main.go | 75 +++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 51 insertions(+), 24 deletions(-) diff --git a/main.go b/main.go index 2ff5d34..1b6c84e 100644 --- a/main.go +++ b/main.go @@ -39,6 +39,7 @@ var ( // flags acting like commands createKeyPair = fs.Bool("create-key-pair", false, "creates and prints a key pair instead of running") + addContact = fs.String("add-contact", "", "add contact using format: type,name[,public-key] where type can be 'private' or 'public' and 'public-key' is required for 'private' type") // flags for in-proc node dataDir = fs.String("data-dir", filepath.Join(os.TempDir(), "status-term-client"), "data directory for Ethereum node") @@ -124,34 +125,11 @@ func main() { } defer db.Close() - // initialize protocol - var ( - messenger *client.MessengerV2 - ) - - if *providerURI != "" { - messenger, err = createMessengerWithURI(*providerURI, privateKey, db) - if err != nil { - exitErr(err) - } - } else { - messenger, err = createMessengerInProc(privateKey, db) - if err != nil { - exitErr(err) - } - } - - adambContact, err := client.ContactWithPublicKey("adamb", "0x0493ac727e70ea62c4428caddf4da301ca67b699577988d6a782898acfd813addf79b2a2ca2c411499f2e0a12b7de4d00574cbddb442bec85789aea36b10f46895") - if err != nil { - exitErr(err) - } - + // Manage initial contacts. if contacts, err := db.Contacts(); len(contacts) == 0 || err != nil { debugContacts := []client.Contact{ {Name: "status", Type: client.ContactPublicRoom, Topic: "status"}, {Name: "status-core", Type: client.ContactPublicRoom, Topic: "status-core"}, - {Name: "testing-adamb", Type: client.ContactPublicRoom, Topic: "testing-adamb"}, - adambContact, } uniqueContacts := []client.Contact{} for _, c := range debugContacts { @@ -170,6 +148,55 @@ func main() { } } + // Handle add contact. + if *addContact != "" { + options := strings.Split(*addContact, ",") + + var c client.Contact + + if len(options) == 2 && options[0] == "public" { + c = client.Contact{ + Name: options[1], + Type: client.ContactPublicRoom, + Topic: options[1], + } + } else if len(options) == 3 && options[0] == "private" { + c, err = client.ContactWithPublicKey(options[1], options[2]) + if err != nil { + exitErr(err) + } + } else { + exitErr(errors.Errorf("invalid -add-contact value")) + } + + exists, err := db.ContactExist(c) + if err != nil { + exitErr(err) + } + if !exists { + if err := db.SaveContacts([]client.Contact{c}); err != nil { + exitErr(err) + } + } + } + + // initialize protocol + var ( + messenger *client.MessengerV2 + ) + + if *providerURI != "" { + messenger, err = createMessengerWithURI(*providerURI, privateKey, db) + if err != nil { + exitErr(err) + } + } else { + messenger, err = createMessengerInProc(privateKey, db) + if err != nil { + exitErr(err) + } + } + // run in a goroutine to show the UI faster go func() { if err := messenger.Start(); err != nil {