# zkitter-js

[zkitter-js](https://github.com/zkitter/zkitter-js) is an npm module and a CLI tool designed to make building on Zkitter easier.

#### To initialize Zkitter and sync with the network:

<pre class="language-typescript"><code class="lang-typescript"><strong>import {Zkitter} from "zkitter-js";
</strong>const zkitter = await Zkitter.initialize({
  arbitrumHttpProvider: 'https://...',
});

// Sync with arbitrum registrar
await zkitter.syncUsers();

// Sync with zk groups on zkitter
await zkitter.syncGroup();

// Get all historical messages (30 days) from Waku store
await zkitter.queryAll();

// Subscribe to all future messages from everyone
await zkitter.subscribe();
</code></pre>

#### To implement custom database instead of using default LevelDB:

```typescript
import { Zkitter, GenericDBAdapterInterface, Post, Proof } from 'zkitter-js';
import postgres from 'postgres';

const sql = postgres({ /* options */ });

class PostgresDB implements GenericDBAdapterInterface {
    async insertPost(post: Post, proof: Proof) {
        const existing = await sql`
            select * from posts
            where hash = ${post.hash()}
        `
        
        if (!existing) {
            await sql`
                insert into posts (...)
                values (...)
            `
        }
    }
}

const zkitter = await Zkitter.initialize({
  db: new PostgresDB(),
  arbitrumHttpProvider: 'https://...',
});
```

```typescript
interface GenericDBAdapterInterface {
  getUserCount: () => Promise<number>;
  getLastArbitrumBlockScanned: () => Promise<number>;
  updateLastArbitrumBlockScanned: (block: number) => Promise<number>;
  updateUser: (user: User) => Promise<User>;
  getUsers: (limit?: number, offset?: number|string) => Promise<User[]>;
  getUser: (address: string) => Promise<User|null>;
  getUserMeta: (address: string) => Promise<UserMeta>;
  getProof: (hash: string) => Promise<Proof | null>;
  insertGroupMember: (groupId: string, member: GroupMember) => Promise<GroupMember|null>;
  getGroupMembers: (groupId: string, limit?: number, offset?: number|string) => Promise<string[]>
  findGroupHash: (hash: string) => Promise<string | null>;
  insertPost: (post: Post, proof: Proof) => Promise<Post>;
  insertModeration: (moderation: Moderation, proof: Proof) => Promise<Moderation|null>;
  insertConnection: (connection: Connection, proof: Proof) => Promise<Connection|null>;
  insertProfile: (profile: Profile, proof: Proof) => Promise<Profile|null>;
  getMessagesByUser: (address: string, limit?: number, offset?: number|string) => Promise<Message[]>;
  getPostMeta: (postHash: string) => Promise<PostMeta>;
  getPost: (hash: string) => Promise<Post|null>;
  getPosts: (limit?: number, offset?: number|string) => Promise<Post[]>;
  getUserPosts: (address: string, limit?: number, offset?: number|string) => Promise<Post[]>;
  getReplies: (hash: string, limit?: number, offset?: number|string) => Promise<Post[]>;
  getReposts: (hash: string, limit?: number, offset?: number|string) => Promise<string[]>;
  getModerations: (hash: string, limit?: number, offset?: number|string) => Promise<Moderation[]>;
  getConnections: (address: string, limit?: number, offset?: number|string) => Promise<Connection[]>;
}
```

#### CLI Usage:

You must first initialize zkitter cli with an HTTP provider for Arbitrum mainnet

<pre class="language-shell"><code class="lang-shell"><strong>zkitter init --arbitrumHttpProvider="https://..."
</strong></code></pre>

**To fetch all users from Arbitrum:**

```
zkitter sync -a
```

**To sync with one group or all groups:**

```
zkitter sync -g semaphore_taz_members
zkitter sync --groups
```

**To list all groups or users:**

```
zkitter list -u
zkitter list -g
```

**To publish a post:**\
(instruction to generate private key is not available yet!)

```
zkitter write -c "Hello, World!" -u "0x12345..." -s "base64-encoded-private-key"
```

**To view a user profile:**

```
zkitter whois 0xd44a82dD160217d46D754a03C8f841edF06EBE3c
```

<figure><img src="/files/y3Fk0UtQikRFsUCb9KDr" alt=""><figcaption></figcaption></figure>

**To view timeline:**

```
zkitter timeline --limit=3
```

<figure><img src="/files/T2gA8QprtEgG1ShYAGxu" alt=""><figcaption></figcaption></figure>

**To run zkitter node:**

```
zkitter up
# type "help" view command options
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.zkitter.com/developers/libraries/zkitter-js.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
