Files
ucan/pkg/container/container.go

49 lines
938 B
Go
Raw Normal View History

package container
import (
2024-10-01 10:33:38 +02:00
"fmt"
"github.com/ipfs/go-cid"
2024-10-01 10:33:38 +02:00
"github.com/ucan-wg/go-ucan/token"
"github.com/ucan-wg/go-ucan/token/delegation"
)
// TODO: should the invocation being set as root in the car file?
2024-10-01 10:33:38 +02:00
var ErrNotFound = fmt.Errorf("not found")
type Container map[cid.Cid][]byte
func New() Container {
return make(Container)
}
2024-09-19 21:26:42 +02:00
func (ctn Container) AddBytes(cid cid.Cid, data []byte) {
ctn[cid] = data
}
func (ctn Container) GetBytes(cid cid.Cid) ([]byte, bool) {
b, ok := ctn[cid]
return b, ok
}
2024-10-01 10:33:38 +02:00
func (ctn Container) GetToken(cid cid.Cid) (token.Token, error) {
b, ok := ctn[cid]
if !ok {
return nil, ErrNotFound
2024-09-19 21:26:42 +02:00
}
2024-10-01 10:33:38 +02:00
return token.FromDagCbor(b)
}
2024-10-01 10:33:38 +02:00
func (ctn Container) GetDelegation(cid cid.Cid) (*delegation.Token, error) {
tkn, err := ctn.GetToken(cid)
if err != nil {
return nil, err
}
if tkn, ok := tkn.(*delegation.Token); ok {
return tkn, nil
}
return nil, fmt.Errorf("not a delegation token")
}