2024-10-02 10:53:30 +02:00
|
|
|
package invocation
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
_ "embed"
|
|
|
|
|
"fmt"
|
|
|
|
|
"sync"
|
|
|
|
|
|
2024-10-24 10:44:38 -04:00
|
|
|
"github.com/ipfs/go-cid"
|
2024-10-02 10:53:30 +02:00
|
|
|
"github.com/ipld/go-ipld-prime"
|
|
|
|
|
"github.com/ipld/go-ipld-prime/node/bindnode"
|
|
|
|
|
"github.com/ipld/go-ipld-prime/schema"
|
|
|
|
|
|
2026-01-08 15:45:57 -05:00
|
|
|
"code.sonr.org/go/ucan/pkg/args"
|
|
|
|
|
"code.sonr.org/go/ucan/pkg/meta"
|
|
|
|
|
"code.sonr.org/go/ucan/token/internal/envelope"
|
2024-10-02 10:53:30 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// [Tag] is the string used as a key within the SigPayload that identifies
|
|
|
|
|
// that the TokenPayload is an invocation.
|
|
|
|
|
//
|
|
|
|
|
// [Tag]: https://github.com/ucan-wg/invocation#type-tag
|
|
|
|
|
const Tag = "ucan/inv@1.0.0-rc.1"
|
|
|
|
|
|
|
|
|
|
//go:embed invocation.ipldsch
|
|
|
|
|
var schemaBytes []byte
|
|
|
|
|
|
|
|
|
|
var (
|
2024-11-13 16:58:48 +01:00
|
|
|
once sync.Once
|
|
|
|
|
ts *schema.TypeSystem
|
|
|
|
|
errSchema error
|
2024-10-02 10:53:30 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func mustLoadSchema() *schema.TypeSystem {
|
|
|
|
|
once.Do(func() {
|
2024-11-13 16:58:48 +01:00
|
|
|
ts, errSchema = ipld.LoadSchemaBytes(schemaBytes)
|
2024-10-02 10:53:30 +02:00
|
|
|
})
|
2024-11-13 16:58:48 +01:00
|
|
|
if errSchema != nil {
|
|
|
|
|
panic(fmt.Errorf("failed to load IPLD schema: %s", errSchema))
|
2024-10-02 10:53:30 +02:00
|
|
|
}
|
|
|
|
|
return ts
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func payloadType() schema.Type {
|
|
|
|
|
return mustLoadSchema().TypeByName("Payload")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var _ envelope.Tokener = (*tokenPayloadModel)(nil)
|
|
|
|
|
|
|
|
|
|
type tokenPayloadModel struct {
|
2024-10-24 10:44:38 -04:00
|
|
|
// The DID of the Invoker
|
2024-10-02 10:53:30 +02:00
|
|
|
Iss string
|
2024-10-24 10:44:38 -04:00
|
|
|
// The DID of Subject being invoked
|
|
|
|
|
Sub string
|
|
|
|
|
// The DID of the intended Executor if different from the Subject
|
|
|
|
|
Aud *string
|
2024-10-02 10:53:30 +02:00
|
|
|
|
2024-10-24 10:44:38 -04:00
|
|
|
// The Command
|
2024-10-02 10:53:30 +02:00
|
|
|
Cmd string
|
2024-10-24 10:44:38 -04:00
|
|
|
// The Command's Arguments
|
2024-11-07 13:50:20 -05:00
|
|
|
Args *args.Args
|
2024-10-24 10:44:38 -04:00
|
|
|
// Delegations that prove the chain of authority
|
|
|
|
|
Prf []cid.Cid
|
2024-10-02 10:53:30 +02:00
|
|
|
|
|
|
|
|
// Arbitrary Metadata
|
2024-10-24 10:44:38 -04:00
|
|
|
Meta *meta.Meta
|
2024-10-02 10:53:30 +02:00
|
|
|
|
2024-10-24 10:44:38 -04:00
|
|
|
// A unique, random nonce
|
|
|
|
|
Nonce []byte
|
2024-10-02 10:53:30 +02:00
|
|
|
// The timestamp at which the Invocation becomes invalid
|
|
|
|
|
// optional: can be nil
|
|
|
|
|
Exp *int64
|
2024-10-24 10:44:38 -04:00
|
|
|
// The timestamp at which the Invocation was created
|
|
|
|
|
Iat *int64
|
|
|
|
|
|
|
|
|
|
// An optional CID of the Receipt that enqueued the Task
|
|
|
|
|
Cause *cid.Cid
|
2024-10-02 10:53:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *tokenPayloadModel) Prototype() schema.TypedPrototype {
|
|
|
|
|
return bindnode.Prototype((*tokenPayloadModel)(nil), payloadType())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (*tokenPayloadModel) Tag() string {
|
|
|
|
|
return Tag
|
|
|
|
|
}
|