From c76976875477c21c0588419e7ed9163d0b0e7db9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Mon, 2 Nov 2026 17:27:12 +0100 Subject: [PATCH] feat: add the `/ucan/revoke/batch` command MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Hilt revokes dozens of delegations in one bucket policy write and today sends one `/ucan/revoke` invocation per delegation. Swarf verifies and stores each invocation separately, and that time counts against Hilt's write deadline. `/ucan/revoke/batch` carries a list of revocations, each with the same `revoke` and optional `path` as `/ucan/revoke`, so the service can verify the whole list and store it with one write. The service rejects the batch when any revocation in it is invalid. Assisted-by: Claude:claude-opus-5-5 Signed-off-by: Miroslav Bajtoš --- commands/ucan/cbor_gen.go | 131 +++++++++++++++++++++++++++++++++ commands/ucan/codec_test.go | 13 ++++ commands/ucan/gen/main.go | 1 + commands/ucan/json_gen.go | 143 ++++++++++++++++++++++++++++++++++++ commands/ucan/revoke.go | 4 + commands/ucan/types.go | 7 ++ 6 files changed, 299 insertions(+) diff --git a/commands/ucan/cbor_gen.go b/commands/ucan/cbor_gen.go index 8745a04..705f11e 100644 --- a/commands/ucan/cbor_gen.go +++ b/commands/ucan/cbor_gen.go @@ -320,3 +320,134 @@ func (t *RevokeArguments) UnmarshalCBOR(r io.Reader) (err error) { return nil } +func (t *RevokeBatchArguments) MarshalCBOR(w io.Writer) error { + if t == nil { + _, err := w.Write(cbg.CborNull) + return err + } + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write([]byte{161}); err != nil { + return err + } + + // t.Revocations ([]ucan.RevokeArguments) (slice) + if len("revocations") > 8192 { + return xerrors.Errorf("Value in field \"revocations\" was too long") + } + + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("revocations"))); err != nil { + return err + } + if _, err := cw.WriteString(string("revocations")); err != nil { + return err + } + + if len(t.Revocations) > 8192 { + return xerrors.Errorf("Slice value in field t.Revocations was too long") + } + + if err := cw.WriteMajorTypeHeader(cbg.MajArray, uint64(len(t.Revocations))); err != nil { + return err + } + for _, v := range t.Revocations { + if err := v.MarshalCBOR(cw); err != nil { + return err + } + + } + return nil +} + +func (t *RevokeBatchArguments) UnmarshalCBOR(r io.Reader) (err error) { + *t = RevokeBatchArguments{} + + cr := cbg.NewCborReader(r) + + maj, extra, err := cr.ReadHeader() + if err != nil { + return err + } + defer func() { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } + }() + + if maj != cbg.MajMap { + return fmt.Errorf("cbor input should be of type map") + } + + if extra > cbg.MaxLength { + return fmt.Errorf("RevokeBatchArguments: map struct too large (%d)", extra) + } + + n := extra + + nameBuf := make([]byte, 11) + for i := uint64(0); i < n; i++ { + nameLen, ok, err := cbg.ReadFullStringIntoBuf(cr, nameBuf, 8192) + if err != nil { + return err + } + + if !ok { + // Field doesn't exist on this type, so ignore it + if err := cbg.ScanForLinks(cr, func(cid.Cid) {}); err != nil { + return err + } + continue + } + + switch string(nameBuf[:nameLen]) { + // t.Revocations ([]ucan.RevokeArguments) (slice) + case "revocations": + + maj, extra, err = cr.ReadHeader() + if err != nil { + return err + } + + if extra > 8192 { + return fmt.Errorf("t.Revocations: array too large (%d)", extra) + } + + if maj != cbg.MajArray { + return fmt.Errorf("expected cbor array") + } + + if extra > 0 { + t.Revocations = make([]RevokeArguments, extra) + } + + for i := 0; i < int(extra); i++ { + { + var maj byte + var extra uint64 + var err error + _ = maj + _ = extra + _ = err + + { + + if err := t.Revocations[i].UnmarshalCBOR(cr); err != nil { + return xerrors.Errorf("unmarshaling t.Revocations[i]: %w", err) + } + + } + + } + } + + default: + // Field doesn't exist on this type, so ignore it + if err := cbg.ScanForLinks(r, func(cid.Cid) {}); err != nil { + return err + } + } + } + + return nil +} diff --git a/commands/ucan/codec_test.go b/commands/ucan/codec_test.go index 4f69cee..1cea072 100644 --- a/commands/ucan/codec_test.go +++ b/commands/ucan/codec_test.go @@ -77,3 +77,16 @@ func TestConcludeArgumentsEmpty(t *testing.T) { require.NoError(t, outJSON.UnmarshalDagJSON(bytes.NewReader(jb.Bytes()))) require.Empty(t, outJSON.Receipts) } + +// TestRevokeBatchArguments pins the wire shape of a batch holding a direct +// revocation and one proven by a witness path. +func TestRevokeBatchArguments(t *testing.T) { + in := &ucancmds.RevokeBatchArguments{Revocations: []ucancmds.RevokeArguments{ + {Revoke: rcptA}, + {Revoke: rcptB, Path: []cid.Cid{rcptA, rcptB}}, + }} + require.Equal(t, + `{"revocations":[{"path":[],"revoke":{"/":"`+rcptA.String()+`"}},`+ + `{"path":[{"/":"`+rcptA.String()+`"},{"/":"`+rcptB.String()+`"}],"revoke":{"/":"`+rcptB.String()+`"}}]}`, + roundTrip(t, in)) +} diff --git a/commands/ucan/gen/main.go b/commands/ucan/gen/main.go index c68e8d1..b19a18c 100644 --- a/commands/ucan/gen/main.go +++ b/commands/ucan/gen/main.go @@ -26,6 +26,7 @@ func main() { models := []any{ ucan.ConcludeArguments{}, ucan.RevokeArguments{}, + ucan.RevokeBatchArguments{}, } const ( cborFile = "../cbor_gen.go" diff --git a/commands/ucan/json_gen.go b/commands/ucan/json_gen.go index ad7e173..76b3623 100644 --- a/commands/ucan/json_gen.go +++ b/commands/ucan/json_gen.go @@ -353,3 +353,146 @@ func (t *RevokeArguments) UnmarshalDagJSON(r io.Reader) (err error) { return nil } +func (t *RevokeBatchArguments) MarshalDagJSON(w io.Writer) error { + jw := jsg.NewDagJsonWriter(w) + if t == nil { + err := jw.WriteNull() + return err + } + if err := jw.WriteObjectOpen(); err != nil { + return err + } + + // t.Revocations ([]ucan.RevokeArguments) (slice) + if len("revocations") > 8192 { + return fmt.Errorf("string in field \"revocations\" was too long") + } + if err := jw.WriteString(string("revocations")); err != nil { + return fmt.Errorf("writing string for field \"revocations\": %w", err) + } + if err := jw.WriteObjectColon(); err != nil { + return err + } + if len(t.Revocations) > 8192 { + return fmt.Errorf("slice value in field t.Revocations was too long") + } + + if err := jw.WriteArrayOpen(); err != nil { + return fmt.Errorf("writing array open for field t.Revocations: %w", err) + } + for i, v := range t.Revocations { + if i > 0 { + if err := jw.WriteComma(); err != nil { + return fmt.Errorf("writing comma for field t.Revocations: %w", err) + } + } + if err := v.MarshalDagJSON(jw); err != nil { + return fmt.Errorf("marshaling field v: %w", err) + } + } + if err := jw.WriteArrayClose(); err != nil { + return fmt.Errorf("writing array close for field t.Revocations: %w", err) + } + + if err := jw.WriteObjectClose(); err != nil { + return err + } + return nil +} +func (t *RevokeBatchArguments) UnmarshalDagJSON(r io.Reader) (err error) { + *t = RevokeBatchArguments{} + + jr := jsg.NewDagJsonReader(r) + defer func() { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } + }() + if err := jr.ReadObjectOpen(); err != nil { + return fmt.Errorf("reading object open for RevokeBatchArguments: %w", err) + } + close, err := jr.PeekObjectClose() + if err != nil { + return fmt.Errorf("peeking object close for RevokeBatchArguments: %w", err) + } + if close { + if err := jr.ReadObjectClose(); err != nil { + return fmt.Errorf("reading object close for RevokeBatchArguments: %w", err) + } + } else { + for i := uint64(0); i < 8192; i++ { + name, err := jr.ReadString(8192) + if err != nil { + if errors.Is(err, jsg.ErrLimitExceeded) { + return fmt.Errorf("reading string for field RevokeBatchArguments: string too large") + } + return fmt.Errorf("reading string for field RevokeBatchArguments: %w", err) + } + if err := jr.ReadObjectColon(); err != nil { + return fmt.Errorf("reading object colon for field RevokeBatchArguments: %w", err) + } + switch name { + + // t.Revocations ([]ucan.RevokeArguments) (slice) + case "revocations": + { + + if err := jr.ReadArrayOpen(); err != nil { + return fmt.Errorf("reading array open for field t.Revocations: %w", err) + } + + close, err := jr.PeekArrayClose() + if err != nil { + return fmt.Errorf("peeking array close for field t.Revocations: %w", err) + } + if close { + if err := jr.ReadArrayClose(); err != nil { + return fmt.Errorf("reading array close for field t.Revocations: %w", err) + } + + } else { + for i := 0; i < 8192; i++ { + item := make([]RevokeArguments, 1) + + if err := item[0].UnmarshalDagJSON(jr); err != nil { + return fmt.Errorf("unmarshaling item[0]: %w", err) + } + + t.Revocations = append(t.Revocations, item[0]) + + close, err := jr.ReadArrayCloseOrComma() + if err != nil { + return fmt.Errorf("reading array close or comma for field t.Revocations: %w", err) + } + if close { + break + } + if i == 8192-1 { + return fmt.Errorf("reading array for field t.Revocations: slice too large") + } + } + } + + } + default: + // Field doesn't exist on this type, so ignore it + if err := jr.DiscardType(); err != nil { + return fmt.Errorf("ignoring field %s for RevokeBatchArguments: %w", name, err) + } + } + + close, err := jr.ReadObjectCloseOrComma() + if err != nil { + return fmt.Errorf("reading object close or comma for field RevokeBatchArguments: %w", err) + } + if close { + break + } + if i == 8192-1 { + return fmt.Errorf("map too large for RevokeBatchArguments") + } + } + } + + return nil +} diff --git a/commands/ucan/revoke.go b/commands/ucan/revoke.go index c1d5123..c2f0521 100644 --- a/commands/ucan/revoke.go +++ b/commands/ucan/revoke.go @@ -11,3 +11,7 @@ import ( type RevokeOK = commands.Unit var Revoke = binding.Bind[*RevokeArguments, *RevokeOK](command.MustParse("/ucan/revoke")) + +type RevokeBatchOK = commands.Unit + +var RevokeBatch = binding.Bind[*RevokeBatchArguments, *RevokeBatchOK](command.MustParse("/ucan/revoke/batch")) diff --git a/commands/ucan/types.go b/commands/ucan/types.go index e4b5717..e36b079 100644 --- a/commands/ucan/types.go +++ b/commands/ucan/types.go @@ -18,3 +18,10 @@ type RevokeArguments struct { // UCAN being revoked. Path []cid.Cid `cborgen:"path" dagjsongen:"path"` } + +// RevokeBatchArguments revokes several delegations with one invocation, so +// the service can verify and store them as one unit. The service rejects the +// whole batch when any revocation in it is invalid. +type RevokeBatchArguments struct { + Revocations []RevokeArguments `cborgen:"revocations" dagjsongen:"revocations"` +}