Files
MikrotikManager/backend/src/services/bandwidth-node-bootstrap.ts
T

27 lines
2.0 KiB
TypeScript

/** Fixed signed operator statement, never a model readiness flag. The separate
* current actor/enrollment grant and global native-writer barrier remain
* mandatory; this statement does not grant network write authority. */
import {createHash} from "node:crypto"
import {z} from "zod"
import {BANDWIDTH_NODE_REGISTRY_DDL} from "./bandwidth-node-registry-schema.js"
import {BANDWIDTH_EXTERNAL_NODE_REGISTRY_DDL} from "./bandwidth-external-node-registry-schema.js"
const hash=z.string().regex(/^[a-f0-9]{64}$/)
export const bandwidthNodeBootstrapSchema=z.object({
format:z.enum(["mmapp-bandwidth-node-bootstrap-v1","mmapp-bandwidth-node-bootstrap-v2"]),installationId:z.string().uuid(),
issuerKeySha256:hash,quiescenceProof:hash,registrySchemaSourceSha256:hash,registryCatalogSha256:hash,
attestationRef:z.string().trim().min(30).max(2000),
}).strict()
export type BandwidthNodeBootstrap=z.infer<typeof bandwidthNodeBootstrapSchema>
export const bandwidthNodeBootstrapEnvelopeSchema=z.object({
format:z.literal("mmapp-bandwidth-node-bootstrap-envelope-v1"),
payload:z.string().min(1).max(24000).regex(/^[A-Za-z0-9_-]+$/),signature:z.string().length(86).regex(/^[A-Za-z0-9_-]+$/),
}).strict()
export function canonicalBandwidthNodeValue(value:unknown):string{
if(Array.isArray(value))return "["+value.map(canonicalBandwidthNodeValue).join(",")+"]"
if(value&&typeof value==="object")return "{"+Object.entries(value).sort(([a],[b])=>a<b?-1:a>b?1:0).map(([key,item])=>JSON.stringify(key)+":"+canonicalBandwidthNodeValue(item)).join(",")+"}"
return JSON.stringify(value)
}
export function canonicalBandwidthNodeBootstrap(value:unknown):string{return canonicalBandwidthNodeValue(bandwidthNodeBootstrapSchema.parse(value))}
export function bandwidthNodeRegistrySchemaSourceSha256():string{return createHash("sha256").update(BANDWIDTH_NODE_REGISTRY_DDL).digest("hex")}
export function bandwidthPeerRegistrySchemaSourceSha256():string{return createHash("sha256").update(BANDWIDTH_NODE_REGISTRY_DDL+"\0"+BANDWIDTH_EXTERNAL_NODE_REGISTRY_DDL).digest("hex")}