# @origintrail-official/dkg-publisher

Publishing protocol for DKG V10. Handles the complete lifecycle of getting Knowledge Assets from a node into the network — from RDF processing through Merkle tree construction to on-chain finalization.

## Features

- **DKGPublisher** — high-level publishing API: submit RDF, get back a finalized Knowledge Asset UAL
- **PublishHandler** — P2P protocol handler that processes incoming publish requests from other nodes, validates data, stores triples, and returns signed ACKs
- **SharedMemoryHandler** — feeless Shared Working Memory handling for local-only or staging workflows, including signed gossip envelope verification for agent-gated context graphs; signatures authenticate writers, but do not encrypt GossipSub payload bytes
- **Context Graphs** — `createContextGraph` and `publishToContextGraph` for M/N signature-gated subgraphs within contextGraphs
- **Context Oracle** — `ContextOracle` class providing verifiable read operations on Context Graphs: `queryWithProofs` (SPARQL with Merkle inclusion proofs), `entityWithProofs` (entity lookup with proofs), and `proveTriple` (single triple existence proof). Provenance triples are scoped to subjects discovered in the query results for efficiency.
- **Merkle trees** — per-KA triple hashing, public/private sub-roots, and collection-level Merkle root computation
- **Skolemization** — blank node to skolemized URI conversion for deterministic RDF processing
- **Auto-partitioning** — splits a batch of RDF triples into individual Knowledge Assets by root entity
- **KC metadata** — generates on-chain metadata (merkle root, byte sizes, entity count) for Knowledge Collection creation
- **ChainEventPoller** — monitors on-chain events for KA finalization confirmations
- **Access control** — `AccessHandler` for serving private triple access requests; `AccessClient` for requesting them

## Usage

```typescript
import { DKGPublisher } from '@origintrail-official/dkg-publisher';

const publisher = new DKGPublisher(config);

// Publish a Knowledge Asset
const result = await publisher.publish({
  contextGraphId: 'urn:contextGraph:example',
  quads: myTriples,
  privateQuads: sensitiveTriples,
  accessPolicy: 'ownerOnly',
});
console.log('Published KA:', result.ual);

// Write to workspace (feeless staging)
await publisher.writeToWorkspace('urn:contextGraph:example', quads, {
  publisherPeerId: agent.peerId,
});

// Conditional write with Compare-and-Swap
await publisher.writeConditionalToWorkspace('urn:contextGraph:example', quads, {
  publisherPeerId: agent.peerId,
  conditions: [
    { subject: 'urn:entity:1', predicate: 'urn:status', expectedValue: '"pending"' },
  ],
});
```

## Internal Dependencies

- `@origintrail-official/dkg-core` — P2P networking, protocol streams, crypto
- `@origintrail-official/dkg-chain` — on-chain KC creation and finalization
- `@origintrail-official/dkg-query` — entity resolution during publishing
- `@origintrail-official/dkg-storage` — triple persistence
