package grants import ( "crypto/sha256" "encoding/hex" "encoding/json" "fmt" ) // **The part's user list and pass result are kept as objects** (design 47 G A-373): each outgrows one value of a key-value bucket // (live, 2026-11-13: the list 422584 bytes and the pass 479989, over the 262144 the bucket takes), so the state `user-list` keeps // them in the part's object bucket and the keys `list` and `pass` name the object, with its digest and size. A write that cannot be // made stays an error, as it was; a pointer that cannot be followed is an error naming the object, never an empty answer. // BigObjects is the object bucket the keys' values are kept in: the part's own (the process's connection). type BigObjects interface { PutObject(name string, data []byte) error GetObject(name string) ([]byte, error) } // bigKeys are the keys of StateUserList kept as objects. var bigKeys = map[string]bool{KeyList: true, KeyPass: true} // BigObjectName is the object a key of StateUserList is kept in. func BigObjectName(key string) string { return "state." + StateUserList + "." + key } // bigPointer is what the key holds in place of the value. type bigPointer struct { KeptIn string `json:"kept_in_object"` SHA256 string `json:"sha256"` Bytes int `json:"bytes"` // Small is the value's small fields, what a reader that cannot open the object is shown (the tools'): the pass's `sealed` and its // errors, the list's digest and revision. Small json.RawMessage `json:"small,omitempty"` } // smallFieldsOf is the fields of a JSON object value that are under 4 KiB each; nil when the value is no object. func smallFieldsOf(value []byte) json.RawMessage { var fields map[string]json.RawMessage if json.Unmarshal(value, &fields) != nil { return nil } for k, v := range fields { if len(v) > 4096 { delete(fields, k) } } out, _ := json.Marshal(fields) return out } // BigValues is store with the keys `list` and `pass` of the state user-list kept in objects. With no objects (the tools') a value is // kept in the key whole, and a key that names an object is an error saying where the value is. func BigValues(store Store, objects BigObjects) Store { return bigStore{Store: store, objects: objects} } type bigStore struct { Store objects BigObjects } func (s bigStore) Put(state, key string, value []byte) (uint64, error) { if state != StateUserList || !bigKeys[key] || s.objects == nil { return s.Store.Put(state, key, value) } name := BigObjectName(key) if err := s.objects.PutObject(name, value); err != nil { return 0, fmt.Errorf("the value of %s could not be put as the object %s: %w", key, name, err) } sum := sha256.Sum256(value) ptr, _ := json.Marshal(bigPointer{KeptIn: name, SHA256: hex.EncodeToString(sum[:]), Bytes: len(value), Small: smallFieldsOf(value)}) return s.Store.Put(state, key, ptr) } func (s bigStore) Get(state, key string) ([]byte, uint64, bool, error) { raw, rev, found, err := s.Store.Get(state, key) if err != nil || !found || state != StateUserList || !bigKeys[key] { return raw, rev, found, err } var p bigPointer if json.Unmarshal(raw, &p) != nil || p.KeptIn == "" { return raw, rev, found, nil // a value kept whole, as before } if s.objects == nil { // A reader that cannot open the object (the tools') is shown the value's small fields and where the rest is, so what a pass said of // the sealed list is readable without the object. var fields map[string]json.RawMessage if json.Unmarshal(p.Small, &fields) != nil || fields == nil { fields = map[string]json.RawMessage{} } fields["partial"], _ = json.Marshal(fmt.Sprintf("the rest of the value of %s is in the object %s (%d bytes), which this reader cannot open", key, p.KeptIn, p.Bytes)) out, _ := json.Marshal(fields) return out, rev, true, nil } data, err := s.objects.GetObject(p.KeptIn) if err != nil { return nil, rev, true, fmt.Errorf("the object %s that %s names cannot be read: %w", p.KeptIn, key, err) } if sum := sha256.Sum256(data); hex.EncodeToString(sum[:]) != p.SHA256 { return nil, rev, true, fmt.Errorf("the object %s is not the one %s names (digest differs): it was replaced while read", p.KeptIn, key) } return data, rev, true, nil }