@enc-protocol/dataview
v0.9.0
Published
ENC DataView SDK — push receiver, base dataview, HTTP client.
Downloads
81
Readme
@enc-protocol/dataview
ENC Protocol DataView SDK — push receiver, base dataview class, and HTTP client for querying aggregated data.
Install
npm install @enc-protocol/dataviewAPI
PushReceiver
Decrypts and processes Push payloads from nodes using ECDH + HKDF + XChaCha20Poly1305.
new PushReceiver(privateKey)— Creates a receiver with a 32-byte private key (Uint8Array).decrypt(wireMsg): Object— Decrypts a Push wire message{ type, from, to, url, content }and returns the decrypted payload{ push_seq, push, notify }.receive(body): Object— Receives and processes a Push (supports both encrypted and plaintext modes); returns decrypted or plaintext payload.
BaseDataView
Core DataView class that receives Push events from nodes, stores them in SQL, and serves aggregated data. App-specific DataViews extend this and override processEvent().
new BaseDataView(sql, identity)— Creates a DataView with a SQL instance and identity object{ privateKey, publicKey, pubHex }. Initializes core tablesdv_enclavesanddv_push_state.registerEnclave(enclaveId, nodeUrl): void— Registers an enclave to watch.unregisterEnclave(enclaveId): void— Unregisters an enclave.getEnclaves(): string[]— Returns list of registered enclave IDs.async receivePush(body): Object— Receives a Push payload, decrypts if encrypted, and processes each event; returns{ ok: true }.async processEvent(event, enclaveId): void— Abstract method for app DataView to override. Called for each event; receives the event object and enclave ID.
DataViewClient
HTTP client for querying a DataView server from app frontends.
new DataViewClient(dvUrl)— Creates a client pointing to a DataView server URL.async getFeed(opts?: { limit?: number }): Object— Fetches aggregated feed data; defaults to limit 50.async getEnclaves(): Object— Fetches registered enclaves.async register(enclaveId): Object— Registers an enclave on the server.async unregister(enclaveId): Object— Unregisters an enclave on the server.async getIdentity(): Object— Fetches the DataView's identity.
Example
import { BaseDataView, DataViewClient } from '@enc-protocol/dataview'
// Server side: extend BaseDataView
class MyDataView extends BaseDataView {
async processEvent(event, enclaveId) {
// Store event in application-specific table
this.sql.exec(
'INSERT INTO my_posts (enclave_id, event_id, content) VALUES (?, ?, ?)',
enclaveId,
event.id,
event.content
)
}
getFeed(limit = 50) {
return this.sql.exec('SELECT * FROM my_posts LIMIT ?', limit)
}
}
// Receive encrypted push from node
const dv = new MyDataView(sqlInstance, identity)
await dv.receivePush(incomingPushPayload)
// Client side: query the server
const client = new DataViewClient('http://localhost:8788')
const feed = await client.getFeed({ limit: 20 })