tokens: expose Inspect and FindTag

This commit is contained in:
Michael Muré
2024-10-01 14:25:07 +02:00
parent 1b7059c029
commit 4201ab2dca
4 changed files with 58 additions and 126 deletions

View File

@@ -1,90 +1,19 @@
package tokens
//
// // EnvelopeInfo describes the fields of an Envelope enclosing a UCAN token.
// type EnvelopeInfo struct {
// Signature []byte
// Tag string
// VarsigHeader []byte
// }
//
// // InspectEnvelope accepts arbitrary data and attempts to decode it as a
// // UCAN token's Envelope.
// func Inspect(data []byte) (EnvelopeInfo, error) {
// undef := EnvelopeInfo{}
//
// node, err := decodeAny(data)
// if err != nil {
// return undef, err
// }
//
// info, err := envelope.inspect(node)
// if err != nil {
// return undef, err
// }
//
// iterator := info.SigPayloadNode.MapIterator()
// foundVarsigHeader := false
// foundTokenPayload := false
// tag := ""
// i := 0
//
// for !iterator.Done() {
// k, _, err := iterator.Next()
// if err != nil {
// return undef, err
// }
//
// key, err := k.AsString()
// if err != nil {
// return undef, err
// }
//
// if key == envelope.VarsigHeaderKey {
// foundVarsigHeader = true
// i++
//
// continue
// }
//
// if strings.HasPrefix(key, envelope.UCANTagPrefix) {
// tag = key
// foundTokenPayload = true
// i++
// }
// }
//
// if i != 2 {
// return undef, fmt.Errorf("expected two and only two fields in SigPayload: %d", i)
// }
//
// if !foundVarsigHeader {
// return undef, errors.New("failed to find VarsigHeader field")
// }
//
// if !foundTokenPayload {
// return undef, errors.New("failed to find TokenPayload field")
// }
//
// return EnvelopeInfo{
// Signature: info.Signature,
// Tag: tag,
// VarsigHeader: info.VarsigHeader,
// }, nil
// }
//
// func decodeAny(data []byte) (datamodel.Node, error) {
// for _, decoder := range []codec.Decoder{
// dagcbor.Decode,
// dagjson.Decode,
// cbor.Decode,
// json.Decode,
// raw.Decode,
// } {
// if node, err := ipld.Decode(data, decoder); err == nil {
// return node, nil
// }
// }
//
// return nil, errors.New("failed to decode (any) the provided data")
// }
import (
"github.com/ipld/go-ipld-prime/datamodel"
"github.com/ucan-wg/go-ucan/tokens/internal/envelope"
)
type Info = envelope.Info
// Inspect inspects the given token IPLD representation and extract some envelope facts.
func Inspect(node datamodel.Node) (Info, error) {
return envelope.Inspect(node)
}
// FindTag inspect the given token IPLD representation and extract the token tag.
func FindTag(node datamodel.Node) (string, error) {
return envelope.FindTag(node)
}