Files
mesh/bundle_http.go
T
waozi ab3a8070c8 Sell server-priced unlocks, gate their packs with grants, and scale Dew
The owner set 100 Dew = 1 XMR (from 5) and made Inbe's Tai Chi a paid
practice whose price and discounts the node enforces.

- Unlocks: GET/POST /api/v1/unlocks list and buy products at the node's
  price; the client names only the product. Tai Chi costs 1 Dew, 90% off
  before DAOCHI_UNLOCK_TRIAL_UNTIL. The signed debit and the unlock row
  commit together, and a repeat unlock charges nothing.
- Grants: POST /api/v1/unlocks/grant signs a one-hour grant for an
  unlocked product with the Dew issuer key, prefixed so it can never
  match a receipt or checkpoint signature.
- Gated packs: an artifact with a node-side <sha>.zib.product file is
  served only at <sha>.zib/<grant> with a valid grant for that product,
  as private, no-store; unreadable markers stay gated, and the v1
  package route refuses gated artifacts.
- Storage plans cost 20 and 200 Dew, keeping their XMR price at the new
  rate.
- `daochi rescale-dew 20` adds signed credits to existing balances and
  scales uncredited invoices and deposits once, then signs a checkpoint.
2026-10-15 12:46:23 -03:00

233 lines
7.3 KiB
Go

// Code generated by zi2go from bundle_http.zi. DO NOT EDIT.
package main
import (
hex "encoding/hex"
fmt "fmt"
http "net/http"
strings "strings"
)
// #import std_go_types
type Release struct {
AppID string "json:\"app_id\""
Sequence int64 "json:\"sequence\""
Version string "json:\"version\""
Runtime string "json:\"runtime\""
SHA256 string "json:\"sha256\""
Size int64 "json:\"size\""
KeyID string "json:\"key_id\""
Signature string "json:\"signature\""
}
type zir_81018ee7c163d186_Decoded struct {
Value []uint8
Error Error
}
type zir_3ae2391e02ff8e4c_WriteResult struct {
Count int
Error Error
}
func BundleHttp_Decode(Value string) zir_81018ee7c163d186_Decoded {
var result zir_81018ee7c163d186_Decoded
result.Value, result.Error = hex.DecodeString(Value)
return result
}
func BundleHttp_Write(Writer ResponseWriter, Data []uint8) zir_3ae2391e02ff8e4c_WriteResult {
var result zir_3ae2391e02ff8e4c_WriteResult
result.Count, result.Error = (http.ResponseWriter).Write(Writer, Data)
return result
}
func BundleHttp_SigningMessage(release Release) string {
return fmt.Sprintf("daochi-zib-release-v1\n%s\n%d\n%s\n%s\n%s\n%d\n%s\n", release.AppID, int64(release.Sequence), release.Version, release.Runtime, release.SHA256, int64(release.Size), release.KeyID)
}
func BundleHttp_Valid(release Release, appID string) bool {
hash := BundleHttp_Decode(release.SHA256)
var value_0 bool = release.AppID == appID
if value_0 {
var value_1 bool = Identity_ValidNamespace(appID)
value_0 = value_1
}
var value_2 bool = value_0 && release.Sequence > 0 && release.Size >= 8 && release.Version != ""
if value_2 {
var value_3 bool = strings.ContainsAny(release.Version, "\r\n")
value_2 = !value_3
}
var value_4 bool = value_2 && release.Runtime == "kryon-desktop-v1"
if value_4 {
var value_5 bool = Identity_ValidNamespace(release.KeyID)
value_4 = value_5
}
var value_6 bool = value_4 && hash.Error == nil && int64(len(hash.Value)) == 32
var value_7 bool = value_6
if value_7 {
var value_8 bool = hex.EncodeToString(hash.Value) == release.SHA256
value_7 = value_8
}
return value_7 && int64(len(release.SHA256)) == 64
}
func BundleHttp_Route(registry Registry, writer ResponseWriter, request *Request) {
path := strings.TrimPrefix(StdUrlGo_Path(StdHttpGo_RequestURL(request)), "/api/v1/packages/")
parts := strings.Split(path, "/")
var value_0 bool = int64(len(parts)) != 2
if !value_0 {
var value_1 bool = Identity_ValidNamespace(parts[0])
value_0 = !value_1
}
if value_0 {
Response_Error(writer, 404, "package not found")
return
}
var value_2 Context = StdHttpGo_Context(request)
app := AppStore_ByID(registry.Database, value_2, parts[0])
if app.Error != nil {
Response_Error(writer, 500, "package registry unavailable")
return
}
var value_3 bool = !app.Found || app.Value.Status != "active"
if !value_3 {
var value_4 bool = app.Value.ManifestExpiresAt > 0
if value_4 {
var value_5 bool = app.Value.ManifestExpiresAt <= StdTimeGo_Unix(StdTimeGo_Now())
value_4 = value_5
}
value_3 = value_4
}
if value_3 {
Response_Error(writer, 404, "package not found")
return
}
root := fmt.Sprint(fmt.Sprint(registry.Configuration.DBPath, ".packages/"), fmt.Sprint(parts[0], "/"))
metadata := StdFileGo_ReadFile(fmt.Sprint(root, "latest.json"))
var release Release = Release{}
if metadata.Error != nil {
Response_Error(writer, 404, "no portable release")
return
}
var value_6 Error = StdJsonGo_Unmarshal(metadata.Value, &(release))
var value_7 bool = value_6 != nil
var value_8 bool = value_7
if !value_8 {
var value_9 bool = BundleHttp_Valid(release, parts[0])
value_8 = !value_9
}
if value_8 {
Response_Error(writer, 503, "invalid portable release")
return
}
var value_10 bool = parts[1] != "latest"
var value_11 bool = value_10
if value_11 {
var value_12 string = parts[1]
var value_13 bool = value_12 != fmt.Sprint(release.SHA256, ".zib")
value_11 = value_13
}
if value_11 {
var value_14 bool = int64(len(parts[1])) != 68
var value_15 bool = value_14
if !value_15 {
var value_16 bool = strings.ContainsAny(parts[1], "/\\")
value_15 = value_16
}
if value_15 {
Response_Error(writer, 404, "package not found")
return
}
archived := StdFileGo_ReadFile(fmt.Sprint(root, fmt.Sprint(parts[1], ".json")))
var value_17 bool = archived.Error != nil
var value_18 bool = value_17
if !value_18 {
var value_19 Error = StdJsonGo_Unmarshal(archived.Value, &(release))
value_18 = (value_19 != nil)
}
var value_20 bool = value_18
var value_21 bool = value_20
if !value_21 {
var value_22 bool = BundleHttp_Valid(release, parts[0])
value_21 = !value_22
}
var value_23 bool = value_21
var value_24 bool = value_23
if !value_24 {
var value_25 string = parts[1]
var value_26 bool = value_25 != fmt.Sprint(release.SHA256, ".zib")
value_24 = value_26
}
if value_24 {
Response_Error(writer, 404, "package not found")
return
}
}
var value_27 Context = StdHttpGo_Context(request)
key := AppStore_ActiveKey(registry.Database, value_27, parts[0], release.KeyID)
public := BundleHttp_Decode(key.Value.PublicKey)
signature := BundleHttp_Decode(release.Signature)
var value_28 bool = key.Error != nil || !key.Found || key.Value.Algorithm != "ed25519"
var value_29 bool = value_28
if !value_29 {
var value_30 bool = key.Value.ExpiresAt > 0
if value_30 {
var value_31 bool = key.Value.ExpiresAt <= StdTimeGo_Unix(StdTimeGo_Now())
value_30 = value_31
}
value_29 = value_30
}
var value_32 bool = value_29 || public.Error != nil || int64(len(public.Value)) != 32 || signature.Error != nil || int64(len(signature.Value)) != 64
var value_33 bool = value_32
var value_34 bool = value_33
if !value_34 {
var value_35 bool = StdEd25519Go_Verify(PublicKey(public.Value), StdTextGo_ToBytes(BundleHttp_SigningMessage(release)), signature.Value)
value_34 = !value_35
}
if value_34 {
Response_Error(writer, 503, "untrusted portable release")
return
}
if parts[1] == "latest" {
StdHttpGo_SetHeader(StdHttpGo_ResponseHeaders(writer), "Cache-Control", "no-store")
Response_JSON(writer, 200, release)
return
}
gate := PackGrant_Gate(fmt.Sprint(root, fmt.Sprint(release.SHA256, ".zib")))
if gate.Gated {
Response_Error(writer, 403, "package grant required")
return
}
bundle := StdFileGo_ReadFile(fmt.Sprint(root, fmt.Sprint(release.SHA256, ".zib")))
var value_36 bool = bundle.Error != nil || int64(len(bundle.Value)) != release.Size
var value_37 bool = value_36
if !value_37 {
var value_38 string = Signing_SHA256Hex(bundle.Value)
value_37 = (value_38 != release.SHA256)
}
if value_37 {
Response_Error(writer, 503, "portable release bytes do not match")
return
}
StdHttpGo_SetHeader(StdHttpGo_ResponseHeaders(writer), "Content-Type", "application/octet-stream")
StdHttpGo_SetHeader(StdHttpGo_ResponseHeaders(writer), "Cache-Control", "public, max-age=31536000, immutable")
StdHttpGo_SetHeader(StdHttpGo_ResponseHeaders(writer), "ETag", fmt.Sprint("\"", fmt.Sprint(release.SHA256, "\"")))
BundleHttp_Write(writer, bundle.Value)
}
var BundleHttp_ziranInitState uint8
func BundleHttp_ziranInit() {
if BundleHttp_ziranInitState == 2 {
return
}
if BundleHttp_ziranInitState == 1 {
panic("cyclic module startup")
}
BundleHttp_ziranInitState = 1
AppStore_ziranInit()
AppHttp_ziranInit()
BundleHttp_ziranInitState = 2
}