Skip to content

Adapt to webhook-ui #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"dependencies": {
"@pusher/push-notifications-server": "^1.2.5",
"@snapshot-labs/snapshot.js": "^0.3.68",
"@snapshot-labs/snapshot.js": "^0.4.43",
"bluebird": "^3.7.2",
"body-parser": "^1.19.0",
"connection-string": "^1.0.1",
Expand Down
27 changes: 27 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import express from 'express';
import { sendEvent } from './events';
import pkg from '../package.json';
import { getSubscribers, addSubscriber, deactivateSubscriber } from './subscribers';
import { verifySignature } from './helpers/utils';

const router = express.Router();

Expand Down Expand Up @@ -28,4 +30,29 @@ router.get('/test', async (req, res) => {
}
});

router.get('/subscriptions/:owner', async (req, res) => {
// TODO: Currently this endpoint is open to anyone. Implement verification to secure the urls in the db, should be accessible only by their owner.
const { owner } = req.params;
const subscribers = await getSubscribers(owner);

return res.json({ subscribers });
});

router.post('/subscribers', async (req, res) => {
const body: any = req.body;
try {
await verifySignature(body);
const params = body.data.message;

params.active === 1
? await addSubscriber(params.from, params.url, params.space, params.active, params.timestamp)
: await deactivateSubscriber(params.id);

return res.json({ status: true });
} catch (error) {
console.log(error);
return res.json({ error: 'Failed to update subscription', message: error });
}
});

export default router;
3 changes: 2 additions & 1 deletion src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { sendPushNotification } from './helpers/beams';
import db from './helpers/mysql';
import { sha256 } from './helpers/utils';
import { getProposal, getProposalScores } from './helpers/proposal';
import { getSubscribers } from './subscribers';

const delay = 5;
const interval = 15;
Expand Down Expand Up @@ -127,7 +128,7 @@ async function processEvents(subscribers) {

async function run() {
try {
const subscribers = await db.queryAsync('SELECT * FROM subscribers');
const subscribers = await getSubscribers();
console.log('[events] Subscribers', subscribers.length);
await processEvents(subscribers);
} catch (e) {
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/beams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const beams = new PushNotifications({
secretKey: process.env.SERVICE_PUSHER_BEAMS_SECRET_KEY ?? ''
});

async function getSubscribers(space) {
async function getSubscribersFromSnapshot(space) {
let subscriptions: { [key: string]: any } = [];
const query = {
subscriptions: {
Expand All @@ -28,7 +28,7 @@ async function getSubscribers(space) {
}

export const sendPushNotification = async event => {
const subscribedWallets = await getSubscribers(event.space);
const subscribedWallets = await getSubscribersFromSnapshot(event.space);
const walletsChunks = chunk(subscribedWallets, 100);
const proposal = await getProposal(event.id.replace('proposal/', ''));
if (!proposal) {
Expand Down
11 changes: 11 additions & 0 deletions src/helpers/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createHash } from 'crypto';
import snapshot from '@snapshot-labs/snapshot.js';

export function shortenAddress(str = '') {
return `${str.slice(0, 6)}...${str.slice(str.length - 4)}`;
Expand All @@ -9,3 +10,13 @@ export function sha256(str) {
.update(str)
.digest('hex');
}

export async function verifySignature(body) {
try {
const isValidSig = await snapshot.utils.verify(body.address, body.sig, body.data);
if (!isValidSig) return Promise.reject('invalid signature');
} catch (e) {
console.warn(`signature validation failed for ${body.address}`);
return Promise.reject('signature validation failed');
}
}
33 changes: 33 additions & 0 deletions src/subscribers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import db from './helpers/mysql';

export async function getSubscribers(owner?: string) {
if (owner)
return db.queryAsync(
'SELECT * FROM subscribers WHERE active = 1 AND owner = ? ORDER BY created DESC',
owner
);

return db.queryAsync('SELECT * FROM subscribers');
}

export async function addSubscriber(
owner: string,
url: string,
space: string,
active: number,
created: string
) {
const params = {
owner,
url,
space,
active,
created
};

return await db.queryAsync('INSERT IGNORE INTO subscribers SET ?', params);
}

export async function deactivateSubscriber(id: string) {
return await db.queryAsync('UPDATE subscribers SET active = 0 WHERE id = ?', id);
}
Loading