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

@@ -4,6 +4,7 @@ import (
"fmt"
"io"
"github.com/ipfs/go-cid"
"github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/codec"
"github.com/ipld/go-ipld-prime/codec/dagcbor"
@@ -15,12 +16,51 @@ import (
"github.com/ucan-wg/go-ucan/token/invocation"
)
// FromSealed decodes an arbitrary token type from the binary data,
// 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.
// Supported and returned types are:
// - delegation.Token
// - invocation.Token
func FromSealed(data []byte) (Token, cid.Cid, error) {
tkn, err := FromDagCbor(data)
if err != nil {
return nil, cid.Undef, err
}
id, err := envelope.CIDFromBytes(data)
if err != nil {
return nil, cid.Undef, err
}
return tkn, id, nil
}
// FromSealedReader is the same as Unseal but accepts an io.Reader.
func FromSealedReader(r io.Reader) (Token, cid.Cid, error) {
cidReader := envelope.NewCIDReader(r)
tkn, err := FromDagCborReader(cidReader)
if err != nil {
return nil, cid.Undef, err
}
id, err := cidReader.CID()
if err != nil {
return nil, cid.Undef, err
}
return tkn, id, nil
}
// Decode unmarshals the input data using the format specified by the
// provided codec.Decoder into an arbitrary UCAN token.
// An error is returned if the conversion fails, or if the resulting
// Token is invalid.
// Supported and returned types are:
// - delegation.Token
// - invocation.Token
func Decode(b []byte, decFn codec.Decoder) (Token, error) {
node, err := ipld.Decode(b, decFn)
if err != nil {
@@ -43,6 +83,7 @@ func DecodeReader(r io.Reader, decFn codec.Decoder) (Token, error) {
// Token is invalid.
// Supported and returned types are:
// - delegation.Token
// - invocation.Token
func FromDagCbor(b []byte) (Token, error) {
return Decode(b, dagcbor.Decode)
}
@@ -57,6 +98,7 @@ func FromDagCborReader(r io.Reader) (Token, error) {
// Token is invalid.
// Supported and returned types are:
// - delegation.Token
// - invocation.Token
func FromDagJson(b []byte) (Token, error) {
return Decode(b, dagjson.Decode)
}