Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dagjose): add encoder and decoder methods #13

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
revert formatting changes
  • Loading branch information
smrz2001 committed Oct 22, 2021
commit 5aaefc2af35105f956e15ddc99c9e8dcc50cb3cc
42 changes: 21 additions & 21 deletions dagjose/dagjose.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package dagjose

import (
"github.com/ipfs/go-cid"
"github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/linking/cid"
ipld "github.com/ipld/go-ipld-prime"
cidLink "github.com/ipld/go-ipld-prime/linking/cid"
)

// DagJOSE is a union of the DagJWE and DagJWS types. Typically, you will want
// to use AsJWE and AsJWS to get a concrete JOSE object.
// This is a union of the DagJWE and DagJWS types. Typically you will want to
// use AsJWE and AsJWS to get a concrete JOSE object.
type DagJOSE struct {
// JWS top level keys
payload *cid.Cid
Expand Down Expand Up @@ -37,17 +37,17 @@ func (d *DagJOSE) AsNode() ipld.Node {
return dagJOSENode{d}
}

// AsJWS will return a DagJWS if this jose object is a JWS, or nil if it is a
// JWE
// If this jose object is a JWS then this will return a DagJWS, if it is a
// JWE then AsJWS will return nil
func (d *DagJOSE) AsJWS() *DagJWS {
if d.payload != nil {
return &DagJWS{dagjose: d}
}
return nil
}

// AsJWE will return a DagJWE if this jose object is a JWE, or nil if it is a
// JWS
// If this jose object is a JWE then this will return a DagJWE, if it is a
// JWS then AsJWE will return nil
func (d *DagJOSE) AsJWE() *DagJWE {
if d.ciphertext != nil {
return &DagJWE{dagjose: d}
Expand All @@ -57,44 +57,44 @@ func (d *DagJOSE) AsJWE() *DagJWE {

type DagJWS struct{ dagjose *DagJOSE }

// AsJOSE returns a DagJOSE object that implements ipld.Node and can be passed
// to ipld related infrastructure
// Returns a DagJOSE object that implements ipld.Node and can be passed to
// ipld related infrastructure
func (d *DagJWS) AsJOSE() *DagJOSE {
return d.dagjose
}

type DagJWE struct{ dagjose *DagJOSE }

// AsJOSE returns a DagJOSE object that implements ipld.Node and can be passed
// to ipld related infrastructure
// Returns a DagJOSE object that implements ipld.Node and can be passed to
// ipld related infrastructure
func (d *DagJWE) AsJOSE() *DagJOSE {
return d.dagjose
}

func (d *DagJWS) PayloadLink() ipld.Link {
return cidlink.Link{Cid: *d.dagjose.payload}
return cidLink.Link{Cid: *d.dagjose.payload}
}

// LinkPrototype will build CIDs using the dag-jose multicodec and the sha-384
// multihash
var LinkPrototype = cidlink.LinkPrototype{Prefix: cid.Prefix{
// A link prototype which will build CIDs using the dag-jose multicodec and
// the sha-384 multihash
var LinkPrototype = cidLink.LinkPrototype{Prefix: cid.Prefix{
Version: 1, // Usually '1'.
Codec: 0x85, // 0x85 means "dag-jose" -- See the multicodecs table: https://github.com/multiformats/multicodec/
MhType: 0x15, // 0x15 means "sha3-384" -- See the multicodecs table: https://github.com/multiformats/multicodec/
MhLength: 48, // sha3-224 hash has a 48-byte sum.
}}

// StoreJOSE is a convenience function which passes `dagjose.LinkPrototype` and
// a DAG-JOSE Node (using `DagJOSE.AsNode`) to `ipld.LinkSystem.Store`.
// A convenience function which passes the correct dagjose.LinkProtoype and
// DAG-JOSE object to ipld.LinkSystem.Store
func StoreJOSE(linkContext ipld.LinkContext, jose *DagJOSE, linkSystem ipld.LinkSystem) (ipld.Link, error) {
return linkSystem.Store(linkContext, LinkPrototype, jose.AsNode())
}

var NodePrototype = &DagJOSENodePrototype{}

// LoadJOSE is a convenience function which wraps `ipld.LinkSystem.Load`. This
// will provide the `dagjose.NodePrototype` to the link system and attempt to
// cast the result to a `dagjose.DagJOSE` object.
// LoadJOSE is a convenience function which wraps ipld.LinkSystem.Load. This
// will provide the dagjose.NodePrototype to the link system and attempt to
// cast the result to a DagJOSE object
func LoadJOSE(lnk ipld.Link, linkContext ipld.LinkContext, linkSystem ipld.LinkSystem) (*DagJOSE, error) {
n, err := linkSystem.Load(
linkContext,
Expand Down
4 changes: 2 additions & 2 deletions dagjose/dagjose_assembler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"fmt"

"github.com/ipfs/go-cid"
"github.com/ipld/go-ipld-prime"
ipld "github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/datamodel"
"github.com/ipld/go-ipld-prime/node/basic"
basicnode "github.com/ipld/go-ipld-prime/node/basic"
"github.com/ipld/go-ipld-prime/node/mixins"
"github.com/ipld/go-ipld-prime/schema"
)
Expand Down
4 changes: 2 additions & 2 deletions dagjose/dagjose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"github.com/ipfs/go-cid"
"github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/fluent"
"github.com/ipld/go-ipld-prime/linking/cid"
"github.com/ipld/go-ipld-prime/node/basic"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
basicnode "github.com/ipld/go-ipld-prime/node/basic"
"github.com/multiformats/go-multihash"
"github.com/stretchr/testify/require"
"golang.org/x/crypto/ed25519"
Expand Down