diff options
| author | Fuwn <[email protected]> | 2026-02-26 15:41:45 -0800 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2026-02-26 15:41:45 -0800 |
| commit | fec9114caaa7d274e524793d5eb0cf2ef2c5af11 (patch) | |
| tree | 0897394ccdfaf6633e1a4ca8eb02bff49bb93c00 /internal/types/operation.go | |
| parent | feat: add read-only PLC API compatibility endpoints (diff) | |
| download | plutia-test-fec9114caaa7d274e524793d5eb0cf2ef2c5af11.tar.xz plutia-test-fec9114caaa7d274e524793d5eb0cf2ef2c5af11.zip | |
feat: Apply Iku formatting
Diffstat (limited to 'internal/types/operation.go')
| -rw-r--r-- | internal/types/operation.go | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/internal/types/operation.go b/internal/types/operation.go index 2facb82..d659a92 100644 --- a/internal/types/operation.go +++ b/internal/types/operation.go @@ -30,16 +30,21 @@ type ParsedOperation struct { func CanonicalizeJSON(raw []byte) ([]byte, error) { trimmed := bytes.TrimSpace(raw) + if len(trimmed) == 0 { return nil, fmt.Errorf("empty json payload") } + if !json.Valid(trimmed) { return nil, fmt.Errorf("invalid json payload") } + var out bytes.Buffer + if err := json.Compact(&out, trimmed); err != nil { return nil, fmt.Errorf("compact json: %w", err) } + return out.Bytes(), nil } @@ -47,19 +52,26 @@ func ParseOperation(rec ExportRecord) (ParsedOperation, error) { if rec.DID == "" { return ParsedOperation{}, fmt.Errorf("missing did") } + canonical, err := CanonicalizeJSON(rec.Operation) + if err != nil { return ParsedOperation{}, err } + payload := map[string]any{} + if err := json.Unmarshal(canonical, &payload); err != nil { return ParsedOperation{}, fmt.Errorf("decode operation: %w", err) } + prev, _ := payload["prev"].(string) cid := rec.CID + if cid == "" { cid = ComputeDigestCID(canonical) } + return ParsedOperation{ DID: rec.DID, CanonicalBytes: canonical, @@ -73,5 +85,6 @@ func ParseOperation(rec ExportRecord) (ParsedOperation, error) { func ComputeDigestCID(payload []byte) string { sum := sha256.Sum256(payload) + return "sha256:" + hex.EncodeToString(sum[:]) } |