Skip to content

Commit

Permalink
Add keystore and handle ipns.Name refcator
Browse files Browse the repository at this point in the history
  • Loading branch information
bahner committed Apr 22, 2024
1 parent fad7602 commit 31134e8
Show file tree
Hide file tree
Showing 35 changed files with 407 additions and 1,560 deletions.
10 changes: 5 additions & 5 deletions cmd/actor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ func main() {
fmt.Println("Initialising actor configuation...")
// actor.InitConfig(config.Profile())

// ACTOR
fmt.Println("Initialising actor...")
a := actor.Init()

// P2P
fmt.Println("Setting default p2p options...")
p2pOpts := p2p.DefaultOptions()
fmt.Println("Initialising p2p...")
p2P, err := p2p.Init(p2pOpts)
p2P, err := p2p.Init(a.Keyset.Identity, p2pOpts)
if err != nil {
panic(fmt.Sprintf("failed to initialize p2p: %v", err))
}

// ACTOR
fmt.Println("Initialising actor...")
a := actor.Init()

// WEB
fmt.Println("Initialising web UI...")
wh := web.NewEntityHandler(p2P, a.Entity)
Expand Down
3 changes: 2 additions & 1 deletion cmd/document/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ func main() {
log.Debugf("main: log level set to %v", _level)

// Create a new keyset for the entity
d, c, err := doc.GetOrCreate(*id, *id)
// d, c, err := doc.GetOrCreate(*id, *id)
d, c, err := doc.Fetch(*id)
if err != nil {
log.Errorf("main: failed to create document: %v", err)
} else {
Expand Down
13 changes: 10 additions & 3 deletions cmd/keyset/create_keyset.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package main

import (
"crypto/rand"
"flag"
"fmt"
"os"

doc "github.com/bahner/go-ma/did/doc"
keyset "github.com/bahner/go-ma/key/set"
"github.com/bahner/go-ma/key/set"
"github.com/libp2p/go-libp2p/core/crypto"
log "github.com/sirupsen/logrus"
)

Expand All @@ -19,7 +21,7 @@ func main() {

log.SetLevel(log.ErrorLevel)

name := flag.String("name", "", "Name of the entity to create")
name := flag.String("name", "", "(Nick)name of the entity to create")
publish := flag.Bool("publish", false, "Publish the entity document to IPFS")
logLevel := flag.String("loglevel", "error", "Set the log level (debug, info, warn, error, fatal, panic)")

Expand All @@ -32,7 +34,12 @@ func main() {
log.Debugf("main: log level set to %v", _level)

// Create a new keyset for the entity
keyset, err := keyset.GetOrCreate(*name)
privKey, _, err := crypto.GenerateEd25519Key(rand.Reader)
if err != nil {
log.Fatal(err)
}

keyset, err := set.New(privKey, *name)
if err != nil {
log.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/node/actors.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func getOrCreateActor(id string) (*actor.Actor, error) {
// Force publication of document.
o := doc.DefaultPublishOptions()
o.Force = true
a.Entity.Doc.Publish(o)
a.Entity.Doc.Publish()

// Cache the newly created entity for future retrievals
actors.Set(id, a)
Expand Down
9 changes: 3 additions & 6 deletions cmd/node/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,14 @@ func main() {

Config(config.Profile())

// Init config and logger
// actor.InitConfig(config.Profile())
// Init of actor requires P2P to be initialized
a := actor.Init()

p, err := p2p.Init(p2p.DefaultOptions())
p, err := p2p.Init(a.Keyset.Identity, p2p.DefaultOptions())
if err != nil {
log.Fatalf("Error initialising P2P: %v", err)
}

// Init of actor requires P2P to be initialized
a := actor.Init()

go web.Start(web.NewEntityHandler(p, a.Entity))
go p.StartDiscoveryLoop(ctx)
go a.Subscribe(ctx, a.Entity)
Expand Down
5 changes: 4 additions & 1 deletion cmd/node/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"sync"

"github.com/bahner/go-ma-actor/db"
"github.com/bahner/go-ma/key/set"
"github.com/hashicorp/vault/api"
"github.com/spf13/pflag"
Expand Down Expand Up @@ -132,7 +133,9 @@ func getOrCreateKeysetFromVault(id string) (set.Keyset, error) {
return set.Keyset{}, fmt.Errorf("error retrieving keyset: %s", err)
}

keyset, err = set.GetOrCreate(id)
privKey, err := db.GetOrCreateIdentity(id)

keyset, err = set.New(privKey, id)
if err != nil {
return set.Keyset{}, fmt.Errorf("error getting or creating keyset: %s", err)
}
Expand Down
24 changes: 6 additions & 18 deletions cmd/pong/envelopes.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"

"github.com/bahner/go-ma-actor/entity"
"github.com/bahner/go-ma-actor/entity/actor"
"github.com/bahner/go-ma/msg"
"github.com/pkg/errors"
Expand Down Expand Up @@ -47,28 +48,15 @@ func handleEnvelopeEvents(ctx context.Context, a *actor.Actor) {

func envelopeReply(ctx context.Context, a *actor.Actor, m *msg.Message) error {

// We need to reverse the to and from here. The message is from the other actor, and we are sending to them.
// We need a topic to send the message over
replyTo := m.From
replyFrom := m.To
replyMsg := []byte(viper.GetString("pong.reply"))

// Broadcast are sent to the topic, and the topic is the DID of the recipient
reply, err := msg.New(replyFrom, replyTo, replyMsg, "text/plain", a.Keyset.SigningKey.PrivKey)
if err != nil {
return fmt.Errorf("failed creating new message: %w", errors.Cause(err))
}

envelope, err := reply.Enclose()
replyToEntity, err := entity.GetOrCreate(replyTo)
if err != nil {
return fmt.Errorf("failed creating envelope: %w", errors.Cause(err))
}

err = envelope.Send(ctx, a.Entity.Topic)
if err != nil {
return fmt.Errorf("failed sending message: %w", errors.Cause(err))
return fmt.Errorf("failed getting or creating entity: %w", errors.Cause(err))
}

fmt.Printf("Sending private envelope to %s over %s\n", replyTo, a.Entity.Topic.String())

return nil
replyMsg := []byte(viper.GetString("pong.reply"))
return m.Reply(ctx, replyMsg, a.Keyset.SigningKey.PrivKey, replyToEntity.Topic)
}
8 changes: 4 additions & 4 deletions cmd/pong/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ func main() {

initConfig(defaultProfileName)

// Init of actor requires P2P to be initialized
a := actor.Init()

// THese are the relay specific parts.
p, err := p2p.Init(p2p.DefaultOptions())
p, err := p2p.Init(a.Keyset.Identity, p2p.DefaultOptions())
if err != nil {
fmt.Printf("Failed to initialize p2p: %v\n", err)
return
}

// Init of actor requires P2P to be initialized
a := actor.Init()

fmt.Printf("Starting pong mode as %s\n", a.Entity.DID.Id)
go p.StartDiscoveryLoop(ctx)
fmt.Println("Discovery loop started.")
Expand Down
36 changes: 15 additions & 21 deletions cmd/pong/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"github.com/bahner/go-ma-actor/entity"
"github.com/bahner/go-ma-actor/entity/actor"
"github.com/bahner/go-ma/msg"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)

func handleMessageEvents(ctx context.Context, a *actor.Actor) {
Expand Down Expand Up @@ -38,42 +38,36 @@ func handleMessageEvents(ctx context.Context, a *actor.Actor) {
continue
}

content := string(m.Message.Content)
from := m.Message.From
to := m.Message.To
_type := m.Message.Type

log.Debugf("Handling message: %v from %s to %s", content, from, to)

if from == me {
if m.Message.From == me {
log.Debugf("Received message from self, ignoring...")
continue
}

if to == me && _type == msg.PLAIN {
messageReply(ctx, a, m)
if m.Message.To == me && m.Message.Type == msg.CHAT {
messageReply(ctx, a, m.Message)
}
}
}
}

func messageReply(ctx context.Context, a *actor.Actor, m *entity.Message) error {
func messageReply(ctx context.Context, a *actor.Actor, m *msg.Message) error {

// Switch sender and receiver. Reply back to from :-)
replyFrom := m.Message.To
replyTo := m.Message.From
// Broadcast are sent to the topic, and the topic is the DID of the recipient
r, err := msg.New(replyFrom, replyTo, reply(m), "text/plain", a.Keyset.SigningKey.PrivKey)
replyTo := m.From
replyToEntity, err := entity.GetOrCreate(replyTo)
if err != nil {
return fmt.Errorf("failed creating new message: %w", errors.Cause(err))
log.Errorf("messageReply: %v", err)
return err
}

err = r.Send(ctx, a.Entity.Topic)
reply := []byte(viper.GetString("pong.reply"))
fmt.Printf("Sending private message to %s over %s\n", replyTo, a.Entity.Topic.String())

err = m.Reply(ctx, reply, a.Keyset.SigningKey.PrivKey, replyToEntity.Topic)
if err != nil {
return fmt.Errorf("failed sending message: %w", errors.Cause(err))
log.Errorf("messageReply: %v", err)
}

fmt.Printf("Sending private message to %s over %s\n", replyTo, a.Entity.Topic.String())
return err

return nil
}
10 changes: 9 additions & 1 deletion cmd/relay/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"fmt"

"github.com/bahner/go-ma-actor/config"
"github.com/bahner/go-ma-actor/db"
"github.com/bahner/go-ma-actor/p2p"
"github.com/bahner/go-ma-actor/ui/web"
)
Expand All @@ -16,8 +18,14 @@ func main() {
ctx := context.Background()
initConfig(defaultProfileName)

identity, err := db.GetOrCreateIdentity(config.Profile())
if err != nil {
fmt.Printf("Failed to get or create identity: %v\n", err)
panic(err)
}

// FIXME. Not default here
p, err := p2p.Init(p2p.DefaultOptions())
p, err := p2p.Init(identity, p2p.DefaultOptions())
if err != nil {
fmt.Printf("Failed to initialize p2p: %v\n", err)
return
Expand Down
12 changes: 5 additions & 7 deletions cmd/robot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,19 @@ func main() {
ctx := context.Background()
initConfig(defaultProfileName)

// THese are the relay specific parts.
i, err := NewRobot()
if err != nil {
log.Fatal(err)
}

p, err := p2p.Init(p2p.DefaultOptions())
p, err := p2p.Init(i.Robot.Keyset.Identity, p2p.DefaultOptions())
if err != nil {
fmt.Printf("Failed to initialize p2p: %v\n", err)
return
}

go p.StartDiscoveryLoop(ctx)

i, err := NewRobot()
if err != nil {
log.Fatal(err)
}

i.Robot.HelloWorld(ctx)
// i.Robot.HelloWorld(ctx, a)

Expand Down
30 changes: 8 additions & 22 deletions cmd/robot/robot.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (i *RobotStruct) handleEntityMessageEvents() {
continue
}

if m.Message.Type == msg.PLAIN {
if m.Message.Type == msg.CHAT {
i.handleMessage(ctx, m)
}
}
Expand All @@ -107,37 +107,23 @@ func (i *RobotStruct) handleEntityMessageEvents() {
func (i *RobotStruct) handleMessage(ctx context.Context, m *entity.Message) error {

// Switch sender and receiver. Reply back to from :-)
// Broadcast are sent to the topic, and the topic is the DID of the recipient
replyFrom := i.Robot.Entity.DID.Id
replyTo, err := entity.GetOrCreate(m.Message.From)
if err != nil {
return fmt.Errorf("failed to create new entity: %w", errors.Cause(err))
}

r, err := msg.New(replyFrom, replyTo.DID.Id, reply(m), "text/plain", i.Robot.Keyset.SigningKey.PrivKey)
replyToEntity, err := entity.GetOrCreate(replyTo.DID.Id)
if err != nil {
return fmt.Errorf("failed creating new message: %w", errors.Cause(err))
log.Errorf("failed to create new entity: %v", err)
}

if m.Enveloped {
env, err := r.Enclose()
if err != nil {
return fmt.Errorf("failed enclose message: %w", errors.Cause(err))
}

env.Send(ctx, i.Robot.Entity.Topic)
fmt.Printf("Sending private message to %s over %s\n", replyTo.DID.Id, replyTo.Topic.String())
replyBytes := reply(m)

} else {

err = r.Send(ctx, i.Location.Topic)
if err != nil {
return fmt.Errorf("failed sending message: %w", errors.Cause(err))
}
fmt.Printf("Sending public message to %s over %s\n", replyTo.DID.Id, i.Location.Topic.String())
err = m.Message.Reply(ctx, replyBytes, i.Robot.Keyset.SigningKey.PrivKey, replyToEntity.Topic)
if err != nil {
log.Errorf("failed to reply to message: %v", err)
}

return nil
return err
}

func reply(m *entity.Message) []byte {
Expand Down
Loading

0 comments on commit 31134e8

Please sign in to comment.