token: don't store the CID in the token, symmetric API for sealed

This commit is contained in:
Michael Muré
2024-10-02 11:57:24 +02:00
parent 6b8fbcee0a
commit d9739a3bab
7 changed files with 68 additions and 57 deletions

View File

@@ -13,8 +13,6 @@ import (
"fmt"
"time"
"github.com/ipfs/go-cid"
"github.com/ucan-wg/go-ucan/did"
"github.com/ucan-wg/go-ucan/pkg/command"
"github.com/ucan-wg/go-ucan/pkg/meta"
@@ -41,8 +39,6 @@ type Token struct {
// The timestamp at which the Invocation was created
invokedAt *time.Time
// TODO: cause
// The CID of the Token when enclosed in an Envelope and encoded to DAG-CBOR
cid cid.Cid
}
// Issuer returns the did.DID representing the Token's issuer.
@@ -84,13 +80,6 @@ func (t *Token) Expiration() *time.Time {
return t.expiration
}
// CID returns the content identifier of the Token model when enclosed
// in an Envelope and encoded to DAG-CBOR.
// Returns cid.Undef if the token has not been serialized or deserialized yet.
func (t *Token) CID() cid.Cid {
return t.cid
}
func (t *Token) validate() error {
var errs error

View File

@@ -47,39 +47,35 @@ func (t *Token) ToSealedWriter(w io.Writer, privKey crypto.PrivKey) (cid.Cid, er
// 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 FromSealed(data []byte) (*Token, error) {
func FromSealed(data []byte) (*Token, cid.Cid, error) {
tkn, err := FromDagCbor(data)
if err != nil {
return nil, err
return nil, cid.Undef, err
}
id, err := envelope.CIDFromBytes(data)
if err != nil {
return nil, err
return nil, cid.Undef, err
}
tkn.cid = id
return tkn, nil
return tkn, id, nil
}
// FromSealedReader is the same as Unseal but accepts an io.Reader.
func FromSealedReader(r io.Reader) (*Token, error) {
func FromSealedReader(r io.Reader) (*Token, cid.Cid, error) {
cidReader := envelope.NewCIDReader(r)
tkn, err := FromDagCborReader(cidReader)
if err != nil {
return nil, err
return nil, cid.Undef, err
}
id, err := cidReader.CID()
if err != nil {
return nil, err
return nil, cid.Undef, err
}
tkn.cid = id
return tkn, nil
return tkn, id, nil
}
// Encode marshals a Token to the format specified by the provided