Something went wrong. Try again.
UCAN 1.0.0 delegations and invocations, offline
Something went wrong. Try again.
2.3 kB · 90 lines
OCaml
at main
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091type t = { iss : Did.t; sub : Did.t; aud : Did.t option; cmd : Command.t; args : Atp.Dagcbor.t; prf : Atp.Cid.t list; nonce : string; exp : int64 option; iat : int64 option; meta : (string * Atp.Dagcbor.t) list; cause : Atp.Cid.t option;}
let tag = "ucan/inv@1.0.0"let ( let* ) = Result.bindlet err_invocation fmt = Fmt.kstr (fun s -> Error s) fmt
let pp ppf i = Fmt.pf ppf "@[<h>%a invokes %a on %a with %d proofs@]" Did.pp i.iss Command.pp i.cmd Did.pp i.sub (List.length i.prf)
let fields = [ "iss"; "sub"; "aud"; "cmd"; "args"; "prf"; "meta"; "nonce"; "exp"; "iat"; "cause"; ]
let link = function `Link c -> Ok c | _ -> Error "a proof is not a CID link"
let rec links = function | [] -> Ok [] | v :: rest -> let* c = link v in let* cs = links rest in Ok (c :: cs)
let principals m = let* iss = Payload.did m "iss" in let* sub = Payload.did m "sub" in let* aud = Payload.optional_did m "aud" in match aud with | Some aud when Did.equal aud sub -> Error "aud is present and equal to sub" | _ -> Ok (iss, sub, aud)
let task m = let* cmd = Payload.command m "cmd" in let* args = Payload.field m "args" in match args with `Map _ -> Ok (cmd, args) | _ -> Error "args is not a map"
let proofs m = let* prf = Payload.field m "prf" in match prf with `List l -> links l | _ -> Error "prf is not a list"
let window m = let* exp = Payload.int_or_null m "exp" in let* iat = Payload.optional_int m "iat" in Ok (exp, iat)
let extras m = let* meta = Payload.optional_map m "meta" in let* cause = match List.assoc_opt "cause" m with | None -> Ok None | Some v -> Result.map Option.some (link v) in if meta = [] && List.mem_assoc "meta" m then Error "meta is empty" else Ok (meta, cause)
let of_envelope (e : Envelope.t) = if not (String.equal (Envelope.final_tag e) tag) then err_invocation "the tag %S is not an invocation's" e.tag else let* m = Payload.of_ipld ~fields e.payload in let* iss, sub, aud = principals m in let* cmd, args = task m in let* prf = proofs m in let* nonce = Payload.bytes m "nonce" in let* exp, iat = window m in let* meta, cause = extras m in Ok { iss; sub; aud; cmd; args; prf; nonce; exp; iat; meta; cause }