From d897009671939600e1ccc2d934aa7cf4a3e1b226 Mon Sep 17 00:00:00 2001 From: Felicio Mununga Date: Wed, 25 May 2022 14:52:48 +0200 Subject: [PATCH] add scripts --- packages/status-js/.scripts/example copy 2.ts | 21 +++++++++++++++++ packages/status-js/src/client.ts | 23 +++++++++++++++++++ packages/status-js/src/community.spec.ts | 1 + packages/status-js/src/community.ts | 2 ++ packages/status-js/src/groupChats.ts | 1 + packages/status-js/src/index.ts | 1 + packages/status-js/src/messenger.ts | 20 +++++++++------- .../src/wire/community_description.ts | 5 ++++ 8 files changed, 66 insertions(+), 8 deletions(-) create mode 100644 packages/status-js/.scripts/example copy 2.ts create mode 100644 packages/status-js/src/client.ts diff --git a/packages/status-js/.scripts/example copy 2.ts b/packages/status-js/.scripts/example copy 2.ts new file mode 100644 index 00000000..53b33f86 --- /dev/null +++ b/packages/status-js/.scripts/example copy 2.ts @@ -0,0 +1,21 @@ +import { createClient } from "../src/client" + +import { Community } from '../src/community' +import { Messenger } from '../src/messenger' + +// const COMMUNITY_PUBLIC_KEY = '0x02cf13719c8b836bebd4e430c497ee38e798a43e4d8c4760c34bbd9bf4f2434d26' +const COMMUNITY_PUBLIC_KEY = '0x029dd5fecbd689dc11e2a5b399afed92cf1fab65d315b883efca753e8f3882f3bd' // compressed + +;(async () => { + const client = await createClient() + + // Retrieve Community's metadata (e.g. description) + const community = await Community.instantiateCommunity(COMMUNITY_PUBLIC_KEY, client) // decoded protobuf + + // // Retrieve and subscribe to messages + // const messenger = await Messenger.create(, client) + // // TODO: Register observers/callbacks + // messenger.addObserver(() => {}) + + await client.stop() +})() diff --git a/packages/status-js/src/client.ts b/packages/status-js/src/client.ts new file mode 100644 index 00000000..652ee902 --- /dev/null +++ b/packages/status-js/src/client.ts @@ -0,0 +1,23 @@ +import { Waku } from 'js-waku' +// import { Fleet } from 'js-waku/build/main/lib/discovery/predefined' + +// TOOD: params +// TODO?: reconnect/keep alive +// TODO?: error handling +// TOOD?: teardown +export async function createClient(): Promise { + const waku = await Waku.create({ + bootstrap: { + default: false, + // peers: ['/dns4/node-01.gc-us-central1-a.wakuv2.prod.statusim.net/tcp/443/wss/p2p/16Uiu2HAmVkKntsECaYfefR1V2yCR79CegLATuTPE6B9TxgxBiiiA'] + peers: [ + '/dns4/node-01.gc-us-central1-a.wakuv2.test.statusim.net/tcp/443/wss/p2p/16Uiu2HAmJb2e28qLXxT5kZxVUUoJt72EMzNGXB47Rxx5hw3q4YjS', + ], + }, + // TODO?: remove + libp2p: { config: { pubsub: { enabled: true, emitSelf: true } } }, + }) + await waku.waitForRemotePeer() + + return waku +} diff --git a/packages/status-js/src/community.spec.ts b/packages/status-js/src/community.spec.ts index 191d77df..7cf42fbe 100644 --- a/packages/status-js/src/community.spec.ts +++ b/packages/status-js/src/community.spec.ts @@ -15,6 +15,7 @@ describe('Community [live data]', () => { it('Retrieves community description For DappConnect Test from Waku prod fleet', async function () { this.timeout(20000) + // FIXME?: mock const waku = await Waku.create({ bootstrap: { default: true } }) await waku.waitForRemotePeer() diff --git a/packages/status-js/src/community.ts b/packages/status-js/src/community.ts index dc0c6134..8840fb7d 100644 --- a/packages/status-js/src/community.ts +++ b/packages/status-js/src/community.ts @@ -12,6 +12,7 @@ const dbg = debug('communities:community') export class Community { public publicKey: Uint8Array private waku: Waku + // TODO?: rename to channels public chats: Map // Chat id, Chat public description?: CommunityDescription @@ -21,6 +22,7 @@ export class Community { this.chats = new Map() } + // TODO: explain why init func instead of constructor /** * Instantiate a Community by retrieving its details from the Waku network. * diff --git a/packages/status-js/src/groupChats.ts b/packages/status-js/src/groupChats.ts index 014ae5b9..81dca8ae 100644 --- a/packages/status-js/src/groupChats.ts +++ b/packages/status-js/src/groupChats.ts @@ -1,5 +1,6 @@ import { waku_message, WakuMessage } from 'js-waku' +// FIXME?: import from 'js-waku' not /build import { ChatMessage } from '.' import { createSymKeyFromPassword } from './encryption' import { MembershipUpdateEvent_EventType } from './proto/communities/v1/membership_update_message' diff --git a/packages/status-js/src/index.ts b/packages/status-js/src/index.ts index b08889c7..fc4e8254 100644 --- a/packages/status-js/src/index.ts +++ b/packages/status-js/src/index.ts @@ -1,4 +1,5 @@ export { Chat } from './chat' +export { createClient } from './client' export { Community } from './community' export { Contacts } from './contacts' export type { GroupChat, GroupChatsType } from './groupChats' diff --git a/packages/status-js/src/messenger.ts b/packages/status-js/src/messenger.ts index 319b672d..9358bc79 100644 --- a/packages/status-js/src/messenger.ts +++ b/packages/status-js/src/messenger.ts @@ -1,5 +1,5 @@ import debug from 'debug' -import { Waku, waku_message, WakuMessage } from 'js-waku' +import { waku_message, WakuMessage } from 'js-waku' import { Chat } from './chat' import { ApplicationMetadataMessage_Type } from './proto/status/v1/application_metadata_message' @@ -9,10 +9,11 @@ import { ChatMessage } from './wire/chat_message' import type { Identity } from './identity' import type { Content } from './wire/chat_message' -import type { waku } from 'js-waku' +import type { Waku } from 'js-waku' const dbg = debug('communities:messenger') +// tood: pass waku client export class Messenger { waku: Waku chatsById: Map @@ -36,13 +37,16 @@ export class Messenger { public static async create( identity: Identity | undefined, - wakuOptions?: waku.CreateOptions + // wakuOptions?: waku.CreateOptions + // TODO: pass waku as client + // wakuOptions?: WakuCreateOptions + waku: Waku ): Promise { - const _wakuOptions = Object.assign( - { bootstrap: { default: true } }, - wakuOptions - ) - const waku = await Waku.create(_wakuOptions) + // const _wakuOptions = Object.assign( + // { bootstrap: { default: true } }, + // wakuOptions + // ) + // const waku = await Waku.create(_wakuOptions) return new Messenger(identity, waku) } diff --git a/packages/status-js/src/wire/community_description.ts b/packages/status-js/src/wire/community_description.ts index f61a6776..32b1d519 100644 --- a/packages/status-js/src/wire/community_description.ts +++ b/packages/status-js/src/wire/community_description.ts @@ -3,6 +3,7 @@ import { Reader } from 'protobufjs' import { idToContentTopic } from '../contentTopic' import { createSymKeyFromPassword } from '../encryption' +// TODO: replace for 'packages/status-js/protos/communities.ts' import * as proto from '../proto/communities/v1/communities' import { bufToHex } from '../utils' import { ApplicationMetadataMessage } from './application_metadata_message' @@ -34,12 +35,14 @@ export class CommunityDescription { wakuStore: WakuStore ): Promise { const hexCommunityPublicKey = bufToHex(communityPublicKey) + // TEST: diff topic const contentTopic = idToContentTopic(hexCommunityPublicKey) let communityDescription: CommunityDescription | undefined const callback = (messages: WakuMessage[]): void => { // Value found, stop processing + // FIXME?: return true/false if (communityDescription) return // Process most recent message first @@ -64,6 +67,8 @@ export class CommunityDescription { ) } }) + + // FIXME?: return true/false } const symKey = await createSymKeyFromPassword(hexCommunityPublicKey)