Skip to content

Commit

Permalink
Update to go-ipld-prime 0.9.0
Browse files Browse the repository at this point in the history
  • Loading branch information
alexjg committed May 23, 2021
1 parent 5b0da2f commit be12f9c
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 116 deletions.
39 changes: 39 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
### v0.0.5

Update to `go-ipld-prime` 0.9.0. `go-ipld-prime` now uses a `LinkSystem`
abstraction which combines the `ipld.Storer` and `ipld.Loader` interfaces.
Consequently the convenience functions `dagjose.BuildJOSELink` and
`dagjose.LoadJOSE` must change to use this new interface. We also expose a
`dagjose.LinkPrototype` which knows how to build CID links to a dag-jose object
for users who are using the underlying `LinkSystem` directly.

To change existing code to be compatible with the new API note the following:

1. `dagjose.BuildJOSELink` has become `StoreJOSE`. This accepts a `LinkSystem` in place
of the previous `ipld.Storer`
2. `dagjose.LoadJOSE` accepts a `LinkSystem` in place of the previous `ipld.Loader`

To call these you will now need to create a `LinkSystem`, typically this will
look like this:

```go
import (
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
)

ls := cidlink.DefaultLinkSystem()
ls.StorageWriteOpener = <whatever you were previously using as ipld.Storer>
ls.StorageReadOpener = <whatever you were previously using as ipld.Loader>

link, err := dagjose.StoreJOSE(
ipld.LinkContext{},
j,
ls,
)

jose, err := dagjose.LoadJOSE(
link,
ipld.LinkContext{},
ls,
)
```
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ An implementation of the [`dag-jose`](https://github.com/ipld/specs/pull/269) mu

To read a JWS from IPFS:

```go
import (
"github.com/alexjg/dagjose"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
```go import ( "github.com/alexjg/dagjose" cidlink "github.com/ipld/go-ipld-prime/linking/cid"
)
// Here we're creating a `CID` which points to a JWS
jwsCid, err := cid.Decode(
Expand Down Expand Up @@ -54,3 +51,9 @@ if err != nil {
}
fmt.Printf("Link is: %v", link)
```

## Changelog

This project attempts to stay up to date with changes in `go-ipld-prime`, this
means somewhat frequent API breakage as `go-ipld-prime` is not yet stable.
See [the changelog](./CHANGELOG.md).
48 changes: 22 additions & 26 deletions dagjose/dagjose.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package dagjose

import (
"context"

"github.com/ipfs/go-cid"
ipld "github.com/ipld/go-ipld-prime"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
Expand Down Expand Up @@ -77,37 +75,35 @@ func (d *DagJWS) PayloadLink() ipld.Link {
return cidlink.Link{Cid: *d.dagjose.payload}
}

// This exposes a similar interface to the cidlink.LinkBuilder from go-ipld-prime. It's primarily a convenience
// function so you don't have to specify the codec version yourself
func BuildJOSELink(ctx context.Context, linkContext ipld.LinkContext, jose *DagJOSE, storer ipld.Storer) (ipld.Link, error) {
lb := cidlink.LinkBuilder{Prefix: cid.Prefix{
Version: 1, // Usually '1'.
Codec: 0x85, // 0x71 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.
}}
return lb.Build(
ctx,
linkContext,
jose.AsNode(),
storer,
)
// 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, // 0x71 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.
}}

// A convenience function which passes the correct dagjose.LinkProtoype append
// jose.AsNode() to ipld.LinkSystem.Store
func StoreJOSE(linkContext ipld.LinkContext, jose *DagJOSE, linkSystem ipld.LinkSystem) (ipld.Link, error) {
return linkSystem.Store(linkContext, LinkPrototype, jose.AsNode())
}

// LoadJOSE is a convenience function which wraps ipld.Link.Load. This will provide the dagjose.NodeBuilder
// to the link and attempt to cast the result to a DagJOSE object
func LoadJOSE(lnk ipld.Link, ctx context.Context, linkContext ipld.LinkContext, loader ipld.Loader) (*DagJOSE, error) {
builder := NewBuilder()
err := lnk.Load(
ctx,
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 object
func LoadJOSE(lnk ipld.Link, linkContext ipld.LinkContext, linkSystem ipld.LinkSystem) (*DagJOSE, error) {
n, err := linkSystem.Load(
linkContext,
builder,
loader,
lnk,
NodePrototype,
)
if err != nil {
return nil, err
}

n := builder.Build()
return n.(dagJOSENode).DagJOSE, nil
}
46 changes: 21 additions & 25 deletions dagjose/dagjose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package dagjose

import (
"bytes"
"context"
"fmt"
"io"
"sort"
Expand Down Expand Up @@ -421,24 +420,26 @@ func normalizeIpldNode(n ipld.Node) ipld.Node {
// Given a JOSE object we encode it using BuildJOSELink and decode it using LoadJOSE and return the result
func roundTripJose(j *DagJOSE) *DagJOSE {
buf := bytes.Buffer{}
link, err := BuildJOSELink(
context.Background(),
ls := cidlink.DefaultLinkSystem()
ls.StorageWriteOpener = func(lnkCtx ipld.LinkContext) (io.Writer, ipld.BlockWriteCommitter, error) {
return &buf, func(lnk ipld.Link) error { return nil }, nil
}
ls.StorageReadOpener = func(lnkCtx ipld.LinkContext, lnk ipld.Link) (io.Reader, error) {
return bytes.NewReader(buf.Bytes()), nil
}

link, err := StoreJOSE(
ipld.LinkContext{},
j,
func(ipld.LinkContext) (io.Writer, ipld.StoreCommitter, error) {
return &buf, func(l ipld.Link) error { return nil }, nil
},
ls,
)
if err != nil {
panic(fmt.Errorf("error storing DagJOSE: %v", err))
}
jose, err := LoadJOSE(
link,
context.Background(),
ipld.LinkContext{},
func(l ipld.Link, _ ipld.LinkContext) (io.Reader, error) {
return bytes.NewBuffer(buf.Bytes()), nil
},
ls,
)
if err != nil {
panic(fmt.Errorf("error reading data from datastore: %v", err))
Expand Down Expand Up @@ -589,31 +590,26 @@ func TestLoadingJWSWithNonCIDPayloadReturnsError(t *testing.T) {
},
)
buf := bytes.Buffer{}
lb := cidlink.LinkBuilder{Prefix: cid.Prefix{
Version: 1, // Usually '1'.
Codec: 0x85, // 0x71 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.
}}
link, err := lb.Build(
context.Background(),
ls := cidlink.DefaultLinkSystem()
ls.StorageWriteOpener = func(lnkCtx ipld.LinkContext) (io.Writer, ipld.BlockWriteCommitter, error) {
return &buf, func(lnk ipld.Link) error { return nil }, nil
}
ls.StorageReadOpener = func(lnkCtx ipld.LinkContext, lnk ipld.Link) (io.Reader, error) {
return bytes.NewReader(buf.Bytes()), nil
}
link, err := ls.Store(
ipld.LinkContext{},
LinkPrototype,
node,
func(ipld.LinkContext) (io.Writer, ipld.StoreCommitter, error) {
return &buf, func(l ipld.Link) error { return nil }, nil
},
)
if err != nil {
t.Errorf("Error creating link to invalid payload node: %v", err)
return
}
_, err = LoadJOSE(
link,
context.Background(),
ipld.LinkContext{},
func(l ipld.Link, _ ipld.LinkContext) (io.Reader, error) {
return bytes.NewBuffer(buf.Bytes()), nil
},
ls,
)
require.NotNil(t, err)
require.Contains(t, err.Error(), "payload is not a valid CID")
Expand Down
12 changes: 6 additions & 6 deletions dagjose/doc_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dagjose_test

import (
"context"
"fmt"

"github.com/alexjg/go-dag-jose/dagjose"
Expand All @@ -19,11 +18,12 @@ func Example_read() {
// cidlink.Link is an implementation of `ipld.Link` backed by a CID
jwsLnk := cidlink.Link{Cid: jwsCid}

ls := cidlink.DefaultLinkSystem()

jose, err := dagjose.LoadJOSE(
jwsLnk,
context.Background(),
ipld.LinkContext{},
nil, //<an implementation of ipld.Loader, which knows how to get the block data from IPFS>,
ls, //<an implementation of ipld.Loader, which knows how to get the block data from IPFS>,
)
if err != nil {
panic(err)
Expand All @@ -41,11 +41,11 @@ func Example_write() {
if err != nil {
panic(err)
}
link, err := dagjose.BuildJOSELink(
context.Background(),
ls := cidlink.DefaultLinkSystem()
link, err := dagjose.StoreJOSE(
ipld.LinkContext{},
dagJws.AsJOSE(),
nil, // <an implementation of `ipld.Storer` which knows how to store the raw block data>
ls,
)
if err != nil {
panic(err)
Expand Down
2 changes: 1 addition & 1 deletion dagjose/jose_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (d dagJOSENode) AsLink() (ipld.Link, error) {
return nil, nil
}
func (d dagJOSENode) Prototype() ipld.NodePrototype {
return nil
return &DagJOSENodePrototype{}
}

// end ipld.Node implementation
Expand Down
6 changes: 3 additions & 3 deletions dagjose/multicodec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package dagjose

import (
dagcbor "github.com/ipld/go-ipld-prime/codec/dagcbor"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
multicodec "github.com/ipld/go-ipld-prime/multicodec"
)

func init() {
cidlink.RegisterMulticodecDecoder(0x85, dagcbor.Decoder)
cidlink.RegisterMulticodecEncoder(0x85, dagcbor.Encoder)
multicodec.RegisterDecoder(0x85, dagcbor.Decode)
multicodec.RegisterEncoder(0x85, dagcbor.Encode)
}
10 changes: 4 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ module github.com/alexjg/go-dag-jose
go 1.14

require (
github.com/google/go-cmp v0.5.2 // indirect
github.com/ipfs/go-cid v0.0.7
github.com/ipld/go-ipld-prime v0.7.0
github.com/multiformats/go-multihash v0.0.13
github.com/smartystreets/goconvey v1.6.4 // indirect
github.com/ipld/go-ipld-prime v0.9.0
github.com/multiformats/go-multihash v0.0.15
github.com/stretchr/testify v1.6.1
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
golang.org/x/tools v0.0.0-20201005185003-576e169c3de7 // indirect
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
gopkg.in/square/go-jose.v2 v2.5.1
pgregory.net/rapid v0.4.1
)
Loading

0 comments on commit be12f9c

Please sign in to comment.