Skip to content

Commit

Permalink
refactor(delegation): align Seal/Unseal names to rest of encode/decod…
Browse files Browse the repository at this point in the history
…e names
  • Loading branch information
smoyer64 committed Sep 24, 2024
1 parent 6dd6f8a commit 0d9955b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
20 changes: 10 additions & 10 deletions delegation/ipld.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import (
"github.com/ucan-wg/go-ucan/internal/envelope"
)

// Seal wraps the delegation token in an envelope, generates the signature,
// encodes the result to DAG-CBOR and calculates the CID of the resulting
// binary data.
func (t *Token) Seal(privKey crypto.PrivKey) ([]byte, cid.Cid, error) {
// ToSealed wraps the delegation token in an envelope, generates the
// signature, encodes the result to DAG-CBOR and calculates the CID of
// the resulting binary data.
func (t *Token) ToSealed(privKey crypto.PrivKey) ([]byte, cid.Cid, error) {
data, err := t.ToDagCbor(privKey)
if err != nil {
return nil, cid.Undef, err
Expand All @@ -32,8 +32,8 @@ func (t *Token) Seal(privKey crypto.PrivKey) ([]byte, cid.Cid, error) {
return data, id, nil
}

// SealWriter is the same as Seal but accepts an io.Writer.
func (t *Token) SealWriter(w io.Writer, privKey crypto.PrivKey) (cid.Cid, error) {
// ToSealedWriter is the same as Seal but accepts an io.Writer.
func (t *Token) ToSealedWriter(w io.Writer, privKey crypto.PrivKey) (cid.Cid, error) {
cidWriter := envelope.NewCIDWriter(w)

if err := t.ToDagCborWriter(cidWriter, privKey); err != nil {
Expand All @@ -43,11 +43,11 @@ func (t *Token) SealWriter(w io.Writer, privKey crypto.PrivKey) (cid.Cid, error)
return cidWriter.CID()
}

// Unseal decodes the provided binary data from the DAG-CBOR format,
// FromSealed decodes the provided binary data from the DAG-CBOR format,
// verifies that the envelope's signature is correct based on the public
// key taken from the issuer (iss) field and calculates the CID of the
// incoming data.
func Unseal(data []byte) (*Token, error) {
func FromSealed(data []byte) (*Token, error) {
tkn, err := FromDagCbor(data)
if err != nil {
return nil, err
Expand All @@ -63,8 +63,8 @@ func Unseal(data []byte) (*Token, error) {
return tkn, nil
}

// UnsealReader is the same as Unseal but accepts an io.Reader.
func UnsealReader(r io.Reader) (*Token, error) {
// FromSealedReader is the same as Unseal but accepts an io.Reader.
func FromSealedReader(r io.Reader) (*Token, error) {
cidReader := envelope.NewCIDReader(r)

tkn, err := FromDagCborReader(cidReader)
Expand Down
24 changes: 12 additions & 12 deletions delegation/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ func TestSchemaRoundTrip(t *testing.T) {
p1, err := delegation.FromDagJson(delegationJson)
require.NoError(t, err)

cborBytes, id, err := p1.Seal(privKey)
cborBytes, id, err := p1.ToSealed(privKey)
require.NoError(t, err)
assert.Equal(t, newCID, envelope.CIDToBase58BTC(id))
fmt.Println("cborBytes length", len(cborBytes))
fmt.Println("cbor", string(cborBytes))

p2, err := delegation.Unseal(cborBytes)
p2, err := delegation.FromSealed(cborBytes)
require.NoError(t, err)
assert.Equal(t, id, p2.CID())
fmt.Println("read Cbor", p2)
Expand All @@ -64,13 +64,13 @@ func TestSchemaRoundTrip(t *testing.T) {
require.NoError(t, err)

cborBytes := &bytes.Buffer{}
id, err := p1.SealWriter(cborBytes, privKey)
id, err := p1.ToSealedWriter(cborBytes, privKey)
t.Log(len(id.Bytes()), id.Bytes())
require.NoError(t, err)
assert.Equal(t, newCID, envelope.CIDToBase58BTC(id))

// buf = bytes.NewBuffer(cborBytes.Bytes())
p2, err := delegation.UnsealReader(cborBytes)
p2, err := delegation.FromSealedReader(cborBytes)
require.NoError(t, err)
t.Log(len(p2.CID().Bytes()), p2.CID().Bytes())
assert.Equal(t, envelope.CIDToBase58BTC(id), envelope.CIDToBase58BTC(p2.CID()))
Expand All @@ -95,8 +95,8 @@ func BenchmarkRoundTrip(b *testing.B) {

b.Run("via buffers", func(b *testing.B) {
p1, _ := delegation.FromDagJson(delegationJson)
cborBytes, _, _ := p1.Seal(privKey)
p2, _ := delegation.Unseal(cborBytes)
cborBytes, _, _ := p1.ToSealed(privKey)
p2, _ := delegation.FromSealed(cborBytes)

b.ResetTimer()

Expand All @@ -110,14 +110,14 @@ func BenchmarkRoundTrip(b *testing.B) {
b.Run("Seal", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, _, _ = p1.Seal(privKey)
_, _, _ = p1.ToSealed(privKey)
}
})

b.Run("Unseal", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, _ = delegation.Unseal(cborBytes)
_, _ = delegation.FromSealed(cborBytes)
}
})

Expand All @@ -132,9 +132,9 @@ func BenchmarkRoundTrip(b *testing.B) {
b.Run("via streaming", func(b *testing.B) {
p1, _ := delegation.FromDagJsonReader(bytes.NewReader(delegationJson))
cborBuf := &bytes.Buffer{}
_, _ = p1.SealWriter(cborBuf, privKey)
_, _ = p1.ToSealedWriter(cborBuf, privKey)
cborBytes := cborBuf.Bytes()
p2, _ := delegation.UnsealReader(bytes.NewReader(cborBytes))
p2, _ := delegation.FromSealedReader(bytes.NewReader(cborBytes))

b.ResetTimer()

Expand All @@ -152,7 +152,7 @@ func BenchmarkRoundTrip(b *testing.B) {
buf := &bytes.Buffer{}
for i := 0; i < b.N; i++ {
buf.Reset()
_, _ = p1.SealWriter(buf, privKey)
_, _ = p1.ToSealedWriter(buf, privKey)
}
})

Expand All @@ -161,7 +161,7 @@ func BenchmarkRoundTrip(b *testing.B) {
reader := bytes.NewReader(cborBytes)
for i := 0; i < b.N; i++ {
_, _ = reader.Seek(0, 0)
_, _ = delegation.UnsealReader(reader)
_, _ = delegation.FromSealedReader(reader)
}
})

Expand Down

0 comments on commit 0d9955b

Please sign in to comment.