diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..32270cb --- /dev/null +++ b/.gitignore @@ -0,0 +1,177 @@ +# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore + +# Logs + +logs +_.log +npm-debug.log_ +yarn-debug.log* +yarn-error.log* +lerna-debug.log* +.pnpm-debug.log* + +# Caches + +.cache + +# Diagnostic reports (https://nodejs.org/api/report.html) + +report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json + +# Runtime data + +pids +_.pid +_.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover + +lib-cov + +# Coverage directory used by tools like istanbul + +coverage +*.lcov + +# nyc test coverage + +.nyc_output + +# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) + +.grunt + +# Bower dependency directory (https://bower.io/) + +bower_components + +# node-waf configuration + +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) + +build/Release + +# Dependency directories + +node_modules/ +jspm_packages/ + +# Snowpack dependency directory (https://snowpack.dev/) + +web_modules/ + +# TypeScript cache + +*.tsbuildinfo + +# Optional npm cache directory + +.npm + +# Optional eslint cache + +.eslintcache + +# Optional stylelint cache + +.stylelintcache + +# Microbundle cache + +.rpt2_cache/ +.rts2_cache_cjs/ +.rts2_cache_es/ +.rts2_cache_umd/ + +# Optional REPL history + +.node_repl_history + +# Output of 'npm pack' + +*.tgz + +# Yarn Integrity file + +.yarn-integrity + +# dotenv environment variable files + +.env +.env.development.local +.env.test.local +.env.production.local +.env.local + +# parcel-bundler cache (https://parceljs.org/) + +.parcel-cache + +# Next.js build output + +.next +out + +# Nuxt.js build / generate output + +.nuxt +dist + +# Gatsby files + +# Comment in the public line in if your project uses Gatsby and not Next.js + +# https://nextjs.org/blog/next-9-1#public-directory-support + +# public + +# vuepress build output + +.vuepress/dist + +# vuepress v2.x temp and cache directory + +.temp + +# Docusaurus cache and generated files + +.docusaurus + +# Serverless directories + +.serverless/ + +# FuseBox cache + +.fusebox/ + +# DynamoDB Local files + +.dynamodb/ + +# TernJS port file + +.tern-port + +# Stores VSCode versions used for testing VSCode extensions + +.vscode-test + +# yarn v2 + +.yarn/cache +.yarn/unplugged +.yarn/build-state.yml +.yarn/install-state.gz +.pnp.* + +# IntelliJ based IDEs +.idea + +# Finder (MacOS) folder config +.DS_Store + +.env.* diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..52a06bf --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,18 @@ +{ + "editor.formatOnSave": true, + "editor.codeActionsOnSave": { + "source.fixAll.eslint": "explicit" + }, + "[typescript]": { + "editor.defaultFormatter": "esbenp.prettier-vscode" + }, + "[javascript]": { + "editor.defaultFormatter": "esbenp.prettier-vscode" + }, + "[json]": { + "editor.defaultFormatter": "esbenp.prettier-vscode" + }, + "editor.tabSize": 2, + "editor.detectIndentation": false, + "files.insertFinalNewline": true +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..9c98c29 --- /dev/null +++ b/README.md @@ -0,0 +1,15 @@ +# broadcast-api + +To install dependencies: + +```bash +bun install +``` + +To run: + +```bash +bun run index.ts +``` + +This project was created using `bun init` in bun v1.0.25. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime. diff --git a/bun.lockb b/bun.lockb new file mode 100755 index 0000000..eb3ffac Binary files /dev/null and b/bun.lockb differ diff --git a/index.ts b/index.ts new file mode 100644 index 0000000..2f98ffc --- /dev/null +++ b/index.ts @@ -0,0 +1,68 @@ +import express, { type Request, type Response } from "express"; +import cors from "cors"; +import dotenv from "dotenv"; +import { xmtpClient } from "./lib/client"; + +const envPath = `.env.${process.env.NODE_ENV}`; +dotenv.config({ path: envPath }); +const PORT = process.env.PORT; +const app = express(); +app.use(express.json()); +app.use(cors()); + +app.post("/lookup", async (req: Request, res: Response) => { + const { address } = req.body; + console.log(req.body); + if (typeof address !== "string") { + console.log(req.body); + res.status(400).send("Address must be a string"); + return; + } + const client = await xmtpClient; + const canMessage = await client.canMessage(address); + res.json({ onNetwork: canMessage }).status(200); +}); + +app.post("/subscribe", async (req: Request, res: Response) => { + const { address, signature } = req.body; + if (typeof address !== "string") { + res.status(400).send("Address must be a string"); + return; + } + + if (typeof signature !== "string") { + res.status(400).send("Signature must be a string"); + return; + } + try { + const client = await xmtpClient; + // TODO: Set Signature on new conversation + const conversation = await client.conversations.newConversation(address); + await conversation.send("Welcome to Good Morning!"); + res.status(200).send({ topic: conversation.topic }); + } catch (err) { + console.error(err); + res.status(500).send("Internal Server Error"); + } +}); + +app.get("/subscriptions", async (req: Request, res: Response) => {}); + +app.post("/broadcast", async (req: Request, res: Response) => { + const { message } = req.body; + // Supporting sending only Text Content, but can be updated to send different types of content + if (typeof message !== "string") { + res.status(400).send("Message must be a string"); + return; + } + const client = await xmtpClient; + const conversations = await client.conversations.list(); + for (const conversation of conversations) { + await conversation.send(message); + } + res.status(200).send("Broadcasted message to all conversations"); +}); + +app.listen(PORT, () => { + console.log(`Listening on port ${PORT}...`); +}); diff --git a/lib/client.ts b/lib/client.ts new file mode 100644 index 0000000..67e0e04 --- /dev/null +++ b/lib/client.ts @@ -0,0 +1,14 @@ +import { Wallet } from "ethers"; +import { Client, type XmtpEnv } from "@xmtp/xmtp-js"; +import { GrpcApiClient } from "@xmtp/grpc-api-client"; +import { FsPersistence } from "@xmtp/fs-persistence"; + +const signer = process.env.KEY + ? new Wallet(process.env.KEY) + : Wallet.createRandom(); + +export const xmtpClient = Client.create(signer, { + // apiClientFactory: GrpcApiClient.fromOptions, + basePersistence: new FsPersistence("/tmp/xmtp"), + env: process.env.XMTP_ENV as XmtpEnv, +}); diff --git a/lib/listSubscribers.ts b/lib/listSubscribers.ts new file mode 100644 index 0000000..9c368fa --- /dev/null +++ b/lib/listSubscribers.ts @@ -0,0 +1,13 @@ +import { xmtpClient } from "./client"; + +interface Subscriber { + address: string; + canMessage: boolean; +} + +export const listSubscribers = async (): Promise => { + const client = await xmtpClient; + + return []; + // subscribers: conversation.subscribers, +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..5706461 --- /dev/null +++ b/package.json @@ -0,0 +1,27 @@ +{ + "name": "broadcast-api", + "module": "index.ts", + "type": "module", + "scripts": { + "start": "bun run index.ts", + "dev": "bun --env.NODE_ENV=dev --watch run index.ts" + }, + "devDependencies": { + "@types/bun": "latest", + "@types/cors": "^2.8.17", + "@types/express": "^4.17.21" + }, + "peerDependencies": { + "typescript": "^5.0.0" + }, + "dependencies": { + "@xmtp/fs-persistence": "^0.0.4", + "@xmtp/grpc-api-client": "^0.2.4", + "@xmtp/xmtp-js": "^11.5.1", + "cors": "^2.8.5", + "dotenv": "^16.4.5", + "ethers": "^6.12.0", + "express": "^4.19.2", + "viem": "^2.9.29" + } +} diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..dcd8fc5 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "lib": ["ESNext"], + "target": "ESNext", + "module": "ESNext", + "moduleDetection": "force", + "jsx": "react-jsx", + "allowJs": true, + + /* Bundler mode */ + "moduleResolution": "bundler", + "allowImportingTsExtensions": true, + "verbatimModuleSyntax": true, + "noEmit": true, + + /* Linting */ + "skipLibCheck": true, + "strict": true, + "noFallthroughCasesInSwitch": true, + "forceConsistentCasingInFileNames": true + } +}