Skip to content

Commit

Permalink
WIP: Make Profile() eval correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
bahner committed Apr 7, 2024
1 parent 1c44e91 commit 33a1909
Show file tree
Hide file tree
Showing 14 changed files with 148 additions and 183 deletions.
4 changes: 1 addition & 3 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,8 @@
},
"args": [
"--generate",
"--nick",
"-p",
"FUBAR",
"--publish",
"--force",
]
}
]
Expand Down
2 changes: 2 additions & 0 deletions cmd/actor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/bahner/go-ma-actor/p2p"
"github.com/bahner/go-ma-actor/ui"
"github.com/bahner/go-ma-actor/ui/web"
"github.com/spf13/pflag"

log "github.com/sirupsen/logrus"
)
Expand All @@ -17,6 +18,7 @@ func main() {

var err error

pflag.Parse()
initConfig(defaultProfileName)

fmt.Println("Initialising actor configuation...")
Expand Down
4 changes: 2 additions & 2 deletions cmd/node/actors.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ func getOrCreateActor(id string) (*actor.Actor, error) {
}

// Assuming entity.NewFromKeyset returns *actor.Actor
a, err := actor.NewFromKeyset(k)
a, err := actor.New(k)
if err != nil {
return nil, fmt.Errorf("failed to create entity: %w", err)
}

a.Entity.Doc, err = a.CreateEntityDocument(a.Entity.DID.Id)
a.Entity.Doc, err = doc.NewFromKeyset(a.Keyset)
if err != nil {
return nil, fmt.Errorf("failed to create DID Document: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/pong/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func main() {
go handleMessageEvents(ctx, a)
fmt.Println("Started event handlers.")

actor.HelloWorld(ctx, a)
a.HelloWorld(ctx)
fmt.Println("Sent hello world.")

// WEB
Expand Down
3 changes: 1 addition & 2 deletions cmd/robot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"log"

"github.com/bahner/go-ma-actor/entity/actor"
"github.com/bahner/go-ma-actor/ui/web"

"github.com/bahner/go-ma-actor/p2p"
Expand Down Expand Up @@ -34,7 +33,7 @@ func main() {
log.Fatal(err)
}

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

fmt.Println("Press Ctrl-C to stop.")
Expand Down
37 changes: 34 additions & 3 deletions config/actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/spf13/pflag"
"github.com/spf13/viper"

"github.com/bahner/go-ma/did/doc"
"github.com/bahner/go-ma/key/set"
log "github.com/sirupsen/logrus"
)
Expand All @@ -17,7 +18,7 @@ const (
)

var (
keyset set.Keyset
actorKeyset set.Keyset
ErrEmptyIdentity = fmt.Errorf("identity is empty")
ErrFakeIdentity = fmt.Errorf("your identity is fake. You need to define actorKeyset or generate a new one")
ErrEmptyNick = fmt.Errorf("nick is empty")
Expand Down Expand Up @@ -48,13 +49,20 @@ type ActorConfig struct {
// Eg. ActorFlags()
func Actor() ActorConfig {

// Fetch the identity from the config or generate one
identity, err := actorIdentity()
if err != nil {
panic(err)
}

// Unpack the keyset from the identity
initActorKeyset(identity)

// If we are generating a new identity we should publish it
if GenerateFlag() {
publishIdentityFromKeyset(actorKeyset)
}

return ActorConfig{
Identity: identity,
Nick: ActorNick(),
Expand All @@ -75,7 +83,7 @@ func ActorLocation() string {
}

func ActorKeyset() set.Keyset {
return keyset
return actorKeyset
}

func actorIdentity() (string, error) {
Expand Down Expand Up @@ -108,7 +116,7 @@ func initActorKeyset(keyset_string string) {
os.Exit(64) // EX_USAGE
}

keyset, err = set.Unpack(keyset_string)
actorKeyset, err = set.Unpack(keyset_string)
if err != nil {
log.Errorf("config.initActor: %v", err)
os.Exit(70) // EX_SOFTWARE
Expand All @@ -132,3 +140,26 @@ func generateKeysetString(nick string) (string, error) {

return pks, nil
}

func publishIdentityFromKeyset(k set.Keyset) error {

d, err := doc.NewFromKeyset(k)
if err != nil {
return fmt.Errorf("config.publishIdentityFromKeyset: failed to create DOC: %w", err)
}

assertionMethod, err := d.GetAssertionMethod()
if err != nil {
return fmt.Errorf("config.publishIdentityFromKeyset: %w", err)
}
d.Sign(k.SigningKey, assertionMethod)

_, err = d.Publish()
if err != nil {
return fmt.Errorf("config.publishIdentityFromKeyset: %w", err)

}
log.Debugf("Published identity: %s", d.ID)

return nil
}
3 changes: 2 additions & 1 deletion config/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var (
defaultHistoryPath = internal.NormalisePath(dataHome + "/" + Profile() + ".history")
)

func init() {
func DBFlags() {
pflag.String("peers", defaultPeersPath, "Filename for CSV peers file.")
pflag.String("entities", defaultEntitiesPath, "Filename for CSV entities file.")
pflag.String("history", defaultHistoryPath, "Filename for CSV history file.")
Expand All @@ -26,6 +26,7 @@ func init() {
viper.SetDefault("db.peers", defaultPeersPath)
viper.SetDefault("db.entities", defaultEntitiesPath)
viper.SetDefault("db.history", defaultHistoryPath)

}

type DBConfig struct {
Expand Down
50 changes: 12 additions & 38 deletions entity/actor/actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/bahner/go-ma-actor/entity"
"github.com/bahner/go-ma/did"
"github.com/bahner/go-ma/did/doc"
"github.com/bahner/go-ma/key/set"
"github.com/bahner/go-ma/msg"
)
Expand All @@ -25,14 +26,14 @@ type Actor struct {
// Create a new entity from a DID and a Keyset. We need both.
// The DID is to verify the entity, and the keyset is to create the
// DID Document.
func New(d did.DID, k set.Keyset) (*Actor, error) {
func New(k set.Keyset) (*Actor, error) {

err := k.Verify()
if err != nil {
return nil, fmt.Errorf("entity/new: failed to verify keyset: %w", err)
}

e, err := entity.New(d)
e, err := entity.New(k.DID)
if err != nil {
return nil, err
}
Expand All @@ -43,64 +44,37 @@ func New(d did.DID, k set.Keyset) (*Actor, error) {
Envelopes: make(chan *msg.Envelope, ENVELOPES_BUFFERSIZE),
}

a.Entity.Doc, err = a.CreateEntityDocument(d.Id)
a.Entity.Doc, err = doc.NewFromKeyset(a.Keyset)
if err != nil {
panic(err)
}

a.Entity.Doc.Publish()

store(a)

return a, nil
}

// Create a new entity from a DID and use fragment as nick.
func NewFromDID(id string, nick string) (*Actor, error) {

d, err := did.New(id)
if err != nil {
return nil, fmt.Errorf("entity/newfromdid: failed to create did from ipnsKey: %w", err)
}

k, err := set.GetOrCreate(d.Fragment)
if err != nil {
return nil, fmt.Errorf("entity/newfromdid: failed to get or create keyset: %w", err)
}

return New(d, k)
}

// // Get an entity from the global map.
// // The input is a full did string. If one is created it will have no Nick.
// // The function should do the required lookups to get the nick.
// // And verify the entity.
func GetOrCreate(id string) (*Actor, error) {

if id == "" {
return nil, fmt.Errorf("entity/getorcreate: empty id")
}

if !did.IsValid(id) {
return nil, fmt.Errorf("entity/getorcreate: invalid id")
// Creating a DID here implies validation before we try to load the actor.
d, err := did.New(id)
if err != nil {
return nil, fmt.Errorf("actor.GetOrCreate: %w", err)
}

var err error

e := load(id)
e := load(d.Id)
if e != nil {
return e, nil
}

e, err = NewFromDID(id, "")
if err != nil {
return nil, fmt.Errorf("entity/getorcreate: failed to create entity: %w", err)
}

err = e.Verify()
k, err := set.GetOrCreate(d.Fragment)
if err != nil {
return nil, fmt.Errorf("entity/getorcreate: failed to verify created entity: %w", err)
return nil, fmt.Errorf("entity/newfromdid: failed to get or create keyset: %w", err)
}

return e, nil
return New(k)
}
112 changes: 0 additions & 112 deletions entity/actor/document.go

This file was deleted.

2 changes: 1 addition & 1 deletion entity/actor/hello.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

const broadcastWait = 3 * time.Second

func HelloWorld(ctx context.Context, a *Actor) {
func (a *Actor) HelloWorld(ctx context.Context) {

topic, err := pubsub.GetOrCreateTopic(ma.BROADCAST_TOPIC)
if err != nil {
Expand Down
Loading

0 comments on commit 33a1909

Please sign in to comment.