Skip to content

Commit

Permalink
container: more experiments
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelMure committed Oct 1, 2024
1 parent 3392a1b commit a73fec4
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
15 changes: 15 additions & 0 deletions pkg/container/container.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package container

import (
"compress/gzip"
"encoding/base64"
"io"

Expand Down Expand Up @@ -67,6 +68,14 @@ func FromCborBase64(r io.Reader) (Container, error) {
return FromCbor(base64.NewDecoder(base64.StdEncoding, r))
}

func FromCborGzip(r io.Reader) (Container, error) {
r2, err := gzip.NewReader(r)
if err != nil {
return nil, err
}
return FromCbor(r2)
}

func (ctn Container) AddBytes(cid cid.Cid, data []byte) {
ctn[cid] = data
}
Expand Down Expand Up @@ -105,3 +114,9 @@ func (ctn Container) ToCborBase64(w io.Writer) error {
defer w2.Close()
return ctn.ToCbor(w2)
}

func (ctn Container) ToCBorGzip(w io.Writer) error {
w2 := gzip.NewWriter(w)
defer w2.Close()
return ctn.ToCbor(w2)
}
59 changes: 58 additions & 1 deletion pkg/container/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,22 @@ package container
import (
"bytes"
"crypto/rand"
"fmt"
"io"
"testing"
"time"

"github.com/ipfs/go-cid"
"github.com/libp2p/go-libp2p/core/crypto"
mh "github.com/multiformats/go-multihash"
"github.com/stretchr/testify/require"

"github.com/ucan-wg/go-ucan/did"
"github.com/ucan-wg/go-ucan/pkg/command"
"github.com/ucan-wg/go-ucan/pkg/policy"
"github.com/ucan-wg/go-ucan/pkg/policy/literal"
"github.com/ucan-wg/go-ucan/pkg/policy/selector"
"github.com/ucan-wg/go-ucan/tokens/delegation"
)

func TestContainerRoundTrip(t *testing.T) {
Expand All @@ -21,6 +31,7 @@ func TestContainerRoundTrip(t *testing.T) {
{"carBase64", Container.ToCarBase64, FromCarBase64},
{"cbor", Container.ToCbor, FromCbor},
{"cborBase64", Container.ToCborBase64, FromCborBase64},
{"cborGzip", Container.ToCBorGzip, FromCborGzip},
} {
t.Run(tc.name, func(t *testing.T) {
ctn := New()
Expand All @@ -30,7 +41,7 @@ func TestContainerRoundTrip(t *testing.T) {
var dataSize int

for i := 0; i < 10; i++ {
data := randBytes(32)
data := randTokenBytes()
c, err := builder.Sum(data)
require.NoError(t, err)
ctn.AddBytes(c, data)
Expand Down Expand Up @@ -58,3 +69,49 @@ func randBytes(n int) []byte {
_, _ = rand.Read(b)
return b
}

func randDID() (crypto.PrivKey, did.DID) {
privKey, _, err := crypto.GenerateEd25519Key(rand.Reader)
if err != nil {
panic(err)
}
d, err := did.FromPrivKey(privKey)
if err != nil {
panic(err)
}
return privKey, d
}

func randomString(length int) string {
b := make([]byte, length/2+1)
rand.Read(b)
return fmt.Sprintf("%x", b)[0:length]
}

func randTokenBytes() []byte {
priv, iss := randDID()
_, aud := randDID()
cmd := command.New("foo", "bar")
pol := policy.Policy{policy.All(
selector.MustParse(".[]"),
policy.GreaterThan(selector.MustParse(".value"), literal.Int(2)),
)}

opts := []delegation.Option{
delegation.WithExpiration(time.Now().Add(time.Hour)),
delegation.WithSubject(iss),
}
for i := 0; i < 3; i++ {
opts = append(opts, delegation.WithMeta(randomString(8), randomString(10)))
}

t, err := delegation.New(priv, aud, cmd, pol, opts...)
if err != nil {
panic(err)
}
b, _, err := t.ToSealed(priv)
if err != nil {
panic(err)
}
return b
}

0 comments on commit a73fec4

Please sign in to comment.