Skip to content

Commit

Permalink
Simpify publication substantially
Browse files Browse the repository at this point in the history
  • Loading branch information
bahner committed Apr 21, 2024
1 parent 36b6175 commit 079225c
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 242 deletions.
107 changes: 0 additions & 107 deletions api/dag.go

This file was deleted.

38 changes: 0 additions & 38 deletions api/rootcid.go

This file was deleted.

42 changes: 30 additions & 12 deletions did/doc/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/bahner/go-ma/did"
cbor "github.com/fxamacker/cbor/v2"
"github.com/ipfs/boxo/path"
log "github.com/sirupsen/logrus"
)

Expand All @@ -18,33 +19,42 @@ type Document struct {
AssertionMethod string `cbor:"assertionMethod" json:"assertionMethod"`
KeyAgreement string `cbor:"keyAgreement" json:"keyAgreement"`
Proof Proof `cbor:"proof" json:"proof"`
immutablePath path.ImmutablePath // This isn't published
did did.DID // This isn't published
}

// Takes an identity DID and a controller DID. They can be the same.
func New(identity string, controller string) (*Document, error) {
func New(identity did.DID, controller did.DID) (*Document, error) {

log.Debugf("doc/new: identifier: %s", identity)
log.Debugf("doc/new: controller: %s", controller)
log.Debugf("doc/new: identifier: %s", identity.Id)
log.Debugf("doc/new: controller: %s", controller.Id)

err := did.Validate(identity)
if err != nil {
return nil, fmt.Errorf("doc/new: invalid identifier: %w", err)
}

ctrller := []string{identity}
ctrller := []string{controller.Id}

doc := Document{
Context: DID_CONTEXT,
ID: identity,
ID: identity.Id,
Controller: ctrller,
did: identity,
}
doc.AddController(controller)
doc.AddController(controller.Id)
log.Infof("doc/new: created new document for %s", identity)
return &doc, nil
}

func (d *Document) MarshalToCBOR() ([]byte, error) {
bytes, err := cbor.Marshal(d)

marshalD := Document{
Context: d.Context,
ID: d.ID,
Controller: d.Controller,
VerificationMethod: d.VerificationMethod,
AssertionMethod: d.AssertionMethod,
KeyAgreement: d.KeyAgreement,
Proof: d.Proof,
}

bytes, err := cbor.Marshal(marshalD)
if err != nil {
return nil, fmt.Errorf("doc/string: failed to marshal document to CBOR: %w", err)
}
Expand Down Expand Up @@ -100,3 +110,11 @@ func compareSlices(a []string, b []string) bool {

return slices.Compare(a, b) == 0
}

// func (d *Document) Path() path.Path {
// return d.immutablePath
// }

// func (d *Document) DID() did.DID {
// return d.did
// }
2 changes: 1 addition & 1 deletion did/doc/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func FetchFromDID(d did.DID) (*Document, cid.Cid, error) {
var document = &Document{}

// Resolve the root CID, ie. the actual IPLD CID of the document
c, err := api.ResolveRootCID(d.Name.String())
c, err := resolveRootCID(d.Name.String())
if err != nil {
return nil, cid.Cid{}, fmt.Errorf("fetchFromDID: %w", err)
}
Expand Down
7 changes: 4 additions & 3 deletions did/doc/keyset.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package doc
import (
"fmt"

"github.com/bahner/go-ma/did"
"github.com/bahner/go-ma/key/set"
)

Expand All @@ -12,15 +13,15 @@ import (
// It's OK to set it as the DID of the keyset.IPNSKey.DID, but it's not required.
func NewFromKeyset(k set.Keyset) (*Document, error) {

return NewFromKeysetWithController(k, k.DID.Id)
return NewFromKeysetWithController(k, k.DID)

}

func NewFromKeysetWithController(k set.Keyset, controller string) (*Document, error) {
func NewFromKeysetWithController(k set.Keyset, controller did.DID) (*Document, error) {

id := k.DID.Name.String()

d, err := New(id, controller)
d, err := New(k.DID, controller)
if err != nil {
return nil, err
}
Expand Down
31 changes: 21 additions & 10 deletions did/doc/node.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,35 @@
package doc

import (
"fmt"

cbor "github.com/ipfs/go-ipld-cbor"
"github.com/ipfs/go-cid"
ipldcbor "github.com/ipfs/go-ipld-cbor"
format "github.com/ipfs/go-ipld-format"
mc "github.com/multiformats/go-multicodec"
mh "github.com/multiformats/go-multihash"
)

// IPFSDagAddCBOR takes a CBOR encoded byte array and adds it to IPFS.
func (d *Document) Node() (*cbor.Node, error) {
func (d *Document) Node() (format.Node, cid.Cid, error) {

dCBOR, err := d.MarshalToCBOR()
// Hash the CBOR data to create a CID
data, err := d.MarshalToCBOR()
if err != nil {
return nil, fmt.Errorf("doc/Node: failed to marshal document to CBOR: %w", err)
return nil, cid.Cid{}, err
}

n, err := cbor.WrapObject(dCBOR, mh.SHA2_256, -1)
hash, err := mh.Sum(data, mh.SHA2_256, -1)
if err != nil {
return nil, cid.Cid{}, err
}

// Create a CID with the DagCBOR codec
codecType := uint64(mc.DagCbor)
c := cid.NewCidV1(codecType, hash)

// Use go-ipld-cbor to decode the CBOR data into an IPLD node
node, err := ipldcbor.Decode(data, mh.SHA2_256, -1)
if err != nil {
return nil, fmt.Errorf("doc/Node: failed to wrap object: %w", err)
return nil, cid.Cid{}, err
}

return n, nil
return node, c, nil
}
Loading

0 comments on commit 079225c

Please sign in to comment.