Skip to content

Commit

Permalink
Update doc and generate config.
Browse files Browse the repository at this point in the history
  • Loading branch information
bahner committed Feb 6, 2024
1 parent 604b645 commit 8a5b4db
Show file tree
Hide file tree
Showing 12 changed files with 206 additions and 123 deletions.
12 changes: 12 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@
"~/.ma/pong.yaml"
],
"console": "integratedTerminal"
},
{
"name": "Show config",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}",
"args": [
"--show_config",
],
"console": "integratedTerminal"
}

]
}
52 changes: 29 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,24 @@

This is go-ma-actor based on [an example from go-libp2p][src].

Now you can either run with `go run`, or build and run the binary:
## Requirements

This is a distributed app that relies heavily on the [libp2p](https://libp2p.io/) stack
and [IPFS][ipfs] in particular. It's unusable unless you have a running IPFS node.

I suggest using [Brave Browser][brave] or [IPFS Desktop][desktop] to run and IPFS node.

*By using Brave browser your can run an IPFS node without installing anything.
And you can investigate the IPFS network with the built-in IPFS node.
It provides The ability to browse IPFS properly, and to pin files and directories.*

## TL;DR

```bash
# Generate persistent environment variables of *SECRET* keysets
eval $(go run . -genenv -forcePublish | tee .env)

# Generate persistent config file with *SECRETS*
# It needs to be published to the IPFS network to be useful
./go-ma-actor --generate --nick "asj" --publish > actor.yaml
./go-ma-actor # Share and enjoy!
```

Expand All @@ -16,30 +29,17 @@ type `./go-ma-actor -help`. Most config settings can be set with environment var

```bash
export GO_MA_LOG_LEVEL="error"
export GO_MA_DISCOVERY_TIMEOUT="300"
export GO_MA_KEYSET="myBase58EncodedPrivkeyGeneratedByGenerate"
export GO_MA_LIBP2P_DISCOVERY_TIMEOUT="300"
export GO_MA_ACTOR_IDENTITY="myBase58EncodedPrivkeyGeneratedByGenerate"
```

## Identity

A `-generate` or `genenv` parameter to generate a text version of a secret key.
The key is text formatted privKey for your node.

This key can and should be kept safely on a PostIt note on your monitor :-)
Just don't store somewhere insecure. It's your future identity.

```bash
unset HISTFILE
export GO_MA_ACTOR_KEYSET=FooBarABCDEFbase58
```

or specified on the command line:

```bash
./go-ma-actor -keyset FooBarABCDEFbase58
```
A `-generate` flag is available to generate a new identity.
It uses defaults, BUT it generates a new random identity.

The first is the best. (Noticed that in most shells the empty space before the command, means that the line isn't saved in history.)
You can use the output as your future identity, but keep it secret.
Those identities are used to sign messages, and to encrypt and decrypt private messages.

## Usage

Expand All @@ -49,8 +49,14 @@ To quit, hit `Ctrl-C`, or type `/quit` into the input field.

- /status [sub|topic|host]
- /discover
- /alias [node|entity] set [DID|NAME] NAME
- /aliases
- /whereis [DID|NAME]
- /msg Name Message
- /enter room
- /nick Name
- /refresh

[src]: https://github.com/libp2p/go-libp2p/tree/master/examples/pubsub/chat
[brave]: <https://brave.com/> (Recommended Browser for 間)
[desktop]: <https://docs.ipfs.tech/install/ipfs-desktop/> (IPFS Desktop)
[ipfs]: <https://ipfs.io/> (IPFS)
40 changes: 15 additions & 25 deletions alias/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,26 @@ import (
"github.com/libp2p/go-libp2p/core/peer"
_ "github.com/mattn/go-sqlite3"
log "github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)

const (
defaultAliasFile = "~/.ma/aliases.db"
defaultAliasLength = 8

SELECT_ENTITY_NICK = "SELECT nick FROM entities WHERE did = ?"
SELECT_ENTITY_DID = "SELECT did FROM entities WHERE nick = ?"
SELECT_NODE_NICK = "SELECT nick FROM nodes WHERE id = ?"
SELECT_NODE_ID = "SELECT id FROM nodes WHERE nick = ?"
UPSERT_ENTITY = "INSERT INTO entities (did, nick) VALUES (?, ?) ON CONFLICT(did) DO UPDATE SET nick = ?"
UPSERT_NODE = "INSERT INTO nodes (id, nick) VALUES (?, ?) ON CONFLICT(id) DO UPDATE SET nick = ?"
DELETE_ENTITY = "DELETE FROM entities WHERE did = ?"
DELETE_NODE = "DELETE FROM nodes WHERE id = ?"
_SELECT_ENTITY_NICK = "SELECT nick FROM entities WHERE did = ?"
_SELECT_ENTITY_DID = "SELECT did FROM entities WHERE nick = ?"
_SELECT_NODE_NICK = "SELECT nick FROM nodes WHERE id = ?"
_SELECT_NODE_ID = "SELECT id FROM nodes WHERE nick = ?"
_UPSERT_ENTITY = "INSERT INTO entities (did, nick) VALUES (?, ?) ON CONFLICT(did) DO UPDATE SET nick = ?"
_UPSERT_NODE = "INSERT INTO nodes (id, nick) VALUES (?, ?) ON CONFLICT(id) DO UPDATE SET nick = ?"
_DELETE_ENTITY = "DELETE FROM entities WHERE did = ?"
_DELETE_NODE = "DELETE FROM nodes WHERE id = ?"
)

var (
once sync.Once
db *sql.DB
)

func init() {

pflag.String("aliases", defaultAliasFile, "File to *write* node aliases to. If the file does not exist, it will be created.")
viper.BindPFlag("aliases", pflag.Lookup("aliases"))
viper.SetDefault("aliases", defaultAliasFile)
}

// Initiates the database connection and creates the tables if they do not exist
func GetDB() (*sql.DB, error) {

Expand Down Expand Up @@ -89,7 +79,7 @@ func GetEntityAlias(id string) (string, error) {

var a string

err = db.QueryRow(SELECT_ENTITY_NICK, id).Scan(&a)
err = db.QueryRow(_SELECT_ENTITY_NICK, id).Scan(&a)
if err != nil {
return "", err
}
Expand All @@ -112,7 +102,7 @@ func GetEntityDID(nick string) (string, error) {

var id string

err = db.QueryRow(SELECT_ENTITY_DID, nick).Scan(&id)
err = db.QueryRow(_SELECT_ENTITY_DID, nick).Scan(&id)
if err != nil {
return "", err
}
Expand All @@ -136,7 +126,7 @@ func GetNodeAlias(id string) (string, error) {

var a string

err = db.QueryRow(SELECT_NODE_NICK, id).Scan(&a)
err = db.QueryRow(_SELECT_NODE_NICK, id).Scan(&a)
if err != nil {
return "", err
}
Expand All @@ -159,7 +149,7 @@ func GetNodeID(nick string) (string, error) {

var id string

err = db.QueryRow(SELECT_NODE_ID, nick).Scan(&id)
err = db.QueryRow(_SELECT_NODE_ID, nick).Scan(&id)
if err != nil {
return "", err
}
Expand All @@ -180,7 +170,7 @@ func SetEntityAlias(id string, nick string) error {
return err
}

_, err = db.Exec(UPSERT_ENTITY, id, nick, nick)
_, err = db.Exec(_UPSERT_ENTITY, id, nick, nick)
if err != nil {
return err
}
Expand All @@ -202,7 +192,7 @@ func SetNodeAlias(id string, nick string) error {
return err
}

_, err = db.Exec(UPSERT_NODE, id, nick, nick)
_, err = db.Exec(_UPSERT_NODE, id, nick, nick)
if err != nil {
return err
}
Expand All @@ -222,7 +212,7 @@ func RemoveEntityAlias(id string) error {
return err
}

_, err = db.Exec(DELETE_ENTITY, id)
_, err = db.Exec(_DELETE_ENTITY, id)
if err != nil {
return err
}
Expand Down
38 changes: 15 additions & 23 deletions config/actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,25 @@ func InitActor() {

}

func handleGenerateOrExit() {
// Genreates a libp2p and actor identity and returns the keyset and the actor identity
// These are imperative, so failure to generate them is a fatal error.
func handleGenerateOrExit() (string, string) {

// Generate a new keysets if requested
nick := viper.GetString("actor.nick")

keyset_string, err := generateAndPrintActorIdentity()
keyset_string, err := generateKeysetString(nick)
if err != nil {
log.Errorf("config.initIdentity: Failed to generate keyset: %v", err)
os.Exit(70) // EX_SOFTWARE
}

ni, err := generateNodeIdentity()
if err != nil {
log.Errorf("config.initIdentity: Failed to generate node identity: %v", err)
os.Exit(70) // EX_SOFTWARE
}

if viper.GetBool("publish") {
err = publishActorIdentityFromString(keyset_string)
if err != nil {
Expand All @@ -83,26 +93,7 @@ func handleGenerateOrExit() {
}
}

err = generateAndPrintNodeIdentity()
if err != nil {
log.Errorf("config.initIdentity: Failed to generate node identity: %v", err)
os.Exit(70) // EX_SOFTWARE
}

}

func generateAndPrintActorIdentity() (string, error) {

nick := viper.GetString("actor.nick")

keyset_string, err := generateKeyset(nick)
if err != nil {
return "", fmt.Errorf("config.initIdentity: Failed to generate keyset: %v", err)
}

fmt.Println(ENV_PREFIX + "_ACTOR_IDENTITY=" + keyset_string)

return keyset_string, nil
return keyset_string, ni
}

func publishActorIdentityFromString(keyset_string string) error {
Expand All @@ -120,7 +111,8 @@ func publishActorIdentityFromString(keyset_string string) error {
return nil
}

func generateKeyset(nick string) (string, error) {
// Generates a new keyset and returns the keyset as a string
func generateKeysetString(nick string) (string, error) {

ks, err := set.GetOrCreate(nick)
if err != nil {
Expand Down
10 changes: 10 additions & 0 deletions config/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,19 @@ package config

import (
"github.com/mitchellh/go-homedir"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)

const defaultAliases = "~/.ma/aliases.db"

func init() {

pflag.String("aliases", defaultAliases, "File to *write* node aliases to. If the file does not exist, it will be created.")
viper.BindPFlag("aliases", pflag.Lookup("aliases"))
viper.SetDefault("aliases", defaultAliases)
}

// Returns expanded path to the aliases file
// If the expansion fails it returns an empty string
func GetAliases() string {
Expand Down
27 changes: 26 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
log "github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"gopkg.in/yaml.v2"
)

const (
Expand All @@ -32,6 +33,10 @@ func init() {

// Allow to set config file via command line flag.
pflag.StringVarP(&configFile, "config", "c", "", "Config file to use.")
pflag.Bool("show-config", false, "Whether to print the config.")
viper.BindPFlag("show-config", pflag.Lookup("show-config"))
pflag.Bool("show-defaults", false, "Whether to print the config.")
viper.BindPFlag("show-defaults", pflag.Lookup("show-defaults"))

pflag.BoolP("version", "v", false, "Print version and exit.")
viper.BindPFlag("version", pflag.Lookup("version"))
Expand Down Expand Up @@ -62,7 +67,27 @@ func Init(configName string) error {
// This will exit when done. It will also publish if applicable.
if viper.GetBool("generate") {
log.Info("Generating new keyset and node identity")
handleGenerateOrExit()
actor, node := handleGenerateOrExit()
generateConfigFile(actor, node)
os.Exit(0)
}

if viper.GetBool("show-config") {
configMap := viper.AllSettings()
configYAML, err := yaml.Marshal(configMap)
if err != nil {
log.Fatalf("error: %v", err)
}

// Print the YAML to stdout or write it to a template file
fmt.Println(string(configYAML))
os.Exit(0)
}

if viper.GetBool("show-defaults") {

// Print the YAML to stdout or write it to a template file
generateConfigFile("zNO_DEFAULT_ACTOR_IDENITY", "zNO_DEFAULT_NODE_IDENITY")
os.Exit(0)
}

Expand Down
Loading

0 comments on commit 8a5b4db

Please sign in to comment.