# 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="https://3161038756-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mhc3kiIa13-x4GiJY3Y%2Fuploads%2FQ90gaTtvIUuz4mfheUh0%2FScreen%20Shot%202023-01-30%20at%202.35.53%20PM.png?alt=media&#x26;token=adbbb8d5-a8f1-46df-be03-9f8cb1ce89d4" alt=""><figcaption></figcaption></figure>

**To view timeline:**

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

<figure><img src="https://3161038756-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mhc3kiIa13-x4GiJY3Y%2Fuploads%2F5lEUuyyuhRPo54m3bUfs%2FScreen%20Shot%202023-01-30%20at%202.37.49%20PM.png?alt=media&#x26;token=fe02ea12-40ad-4bb4-be24-4928fe854026" alt=""><figcaption></figcaption></figure>

**To run zkitter node:**

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