Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
Initial Commit
  • Loading branch information
Alex Risch authored and Alex Risch committed May 1, 2024
0 parents commit 8fb33f8
Show file tree
Hide file tree
Showing 9 changed files with 354 additions and 0 deletions.
177 changes: 177 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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.*
18 changes: 18 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -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
}
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
Binary file added bun.lockb
Binary file not shown.
68 changes: 68 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -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}...`);
});
14 changes: 14 additions & 0 deletions lib/client.ts
Original file line number Diff line number Diff line change
@@ -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,
});
13 changes: 13 additions & 0 deletions lib/listSubscribers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { xmtpClient } from "./client";

interface Subscriber {
address: string;
canMessage: boolean;
}

export const listSubscribers = async (): Promise<Subscriber[]> => {
const client = await xmtpClient;

return [];
// subscribers: conversation.subscribers,
};
27 changes: 27 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
22 changes: 22 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit 8fb33f8

Please sign in to comment.