-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8672b54
commit c2561f9
Showing
22 changed files
with
490 additions
and
207 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
{ | ||
"typescript.tsdk": "node_modules/typescript/lib", | ||
"typescript.enablePromptUseWorkspaceTsdk": true, | ||
"editor.defaultFormatter": "biomejs.biome", | ||
"editor.formatOnSave": true, | ||
"editor.codeActionsOnSave": { | ||
"source.organizeImports.biome": true | ||
}, | ||
"files.associations": { | ||
"*.css": "tailwindcss" | ||
}, | ||
"[json]": { | ||
"editor.defaultFormatter": "biomejs.biome", | ||
"editor.formatOnSave": true, | ||
"editor.codeActionsOnSave": { | ||
"source.organizeImports.biome": true | ||
} | ||
}, | ||
"[typescript]": { | ||
"editor.defaultFormatter": "biomejs.biome", | ||
"editor.formatOnSave": true, | ||
"editor.codeActionsOnSave": { | ||
"source.organizeImports.biome": true | ||
} | ||
}, | ||
"[typescriptreact]": { | ||
"editor.defaultFormatter": "biomejs.biome", | ||
"editor.formatOnSave": true, | ||
"editor.codeActionsOnSave": { | ||
"source.organizeImports.biome": true | ||
} | ||
}, | ||
"[javascript]": { | ||
"editor.defaultFormatter": "biomejs.biome", | ||
"editor.formatOnSave": true, | ||
"editor.codeActionsOnSave": { | ||
"source.organizeImports.biome": true | ||
} | ||
}, | ||
"[javascriptreact]": { | ||
"editor.defaultFormatter": "biomejs.biome", | ||
"editor.formatOnSave": true, | ||
"editor.codeActionsOnSave": { | ||
"source.organizeImports.biome": true | ||
} | ||
}, | ||
"[ignore]": { | ||
"editor.defaultFormatter": "foxundermoon.shell-format" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,54 @@ | ||
#!/usr/bin/env node | ||
|
||
import pkg from "../package.json" assert { type: "json" }; | ||
import { Option } from "commander"; | ||
import { commander, logger } from "substreams-sink"; | ||
import { keyPair } from "../src/signMessage.js"; | ||
import { action } from "../index.js"; | ||
import pkg from "../package.json" assert { type: "json" }; | ||
import { ping } from "../src/ping.js"; | ||
import { Option } from "commander"; | ||
import { keyPair } from "../src/signMessage.js"; | ||
|
||
export interface WebhookRunOptions extends commander.RunOptions { | ||
webhookUrl: string; | ||
secretKey: string; | ||
disablePing: boolean; | ||
webhookUrl: string; | ||
secretKey: string; | ||
disablePing: boolean; | ||
} | ||
|
||
// Run Webhook Sink | ||
const program = commander.program(pkg); | ||
const command = commander.run(program, pkg); | ||
command.addOption(new Option("--webhook-url <string>", "Webhook URL to send POST").makeOptionMandatory().env("WEBHOOK_URL")) | ||
command.addOption(new Option("--secret-key <string>", "TweetNaCl Secret-key to sign POST data payload").makeOptionMandatory().env("SECRET_KEY")) | ||
command.addOption(new Option("--disable-ping", "Disable ping on init").env("DISABLE_PING").default(false)) | ||
command.addOption( | ||
new Option("--webhook-url <string>", "Webhook URL to send POST").makeOptionMandatory().env("WEBHOOK_URL"), | ||
); | ||
command.addOption( | ||
new Option("--secret-key <string>", "TweetNaCl Secret-key to sign POST data payload") | ||
.makeOptionMandatory() | ||
.env("SECRET_KEY"), | ||
); | ||
command.addOption(new Option("--disable-ping", "Disable ping on init").env("DISABLE_PING").default(false)); | ||
command.action(action); | ||
|
||
program.command("keypair") | ||
.description("Generate TweetNaCl keypair") | ||
.action(() => { | ||
const { publicKey, secretKey } = keyPair(); | ||
console.log(`PUBLIC_KEY=${publicKey}`); | ||
console.log(`SECRET_KEY=${secretKey}`); | ||
}) | ||
program | ||
.command("keypair") | ||
.description("Generate TweetNaCl keypair") | ||
.action(() => { | ||
const { publicKey, secretKey } = keyPair(); | ||
console.log(`PUBLIC_KEY=${publicKey}`); | ||
console.log(`SECRET_KEY=${secretKey}`); | ||
}); | ||
|
||
program.command("ping") | ||
.description("Ping Webhook URL") | ||
.addOption(new Option("--webhook-url <string>", "Webhook URL to send POST").makeOptionMandatory().env("WEBHOOK_URL")) | ||
.addOption(new Option("--secret-key <string>", "TweetNaCl Secret-key to sign POST data payload").makeOptionMandatory().env("SECRET_KEY")) | ||
.action(async (options: any) => { | ||
logger.settings.type = "hidden"; | ||
const response = await ping(options.webhookUrl, options.secretKey); | ||
if (response) console.log("✅ OK"); | ||
else console.log("⁉️ ERROR"); | ||
}) | ||
program | ||
.command("ping") | ||
.description("Ping Webhook URL") | ||
.addOption(new Option("--webhook-url <string>", "Webhook URL to send POST").makeOptionMandatory().env("WEBHOOK_URL")) | ||
.addOption( | ||
new Option("--secret-key <string>", "TweetNaCl Secret-key to sign POST data payload") | ||
.makeOptionMandatory() | ||
.env("SECRET_KEY"), | ||
) | ||
.action(async (options) => { | ||
logger.settings.type = "hidden"; | ||
const response = await ping(options.webhookUrl, options.secretKey); | ||
if (response) console.log("✅ OK"); | ||
else console.log("⁉️ ERROR"); | ||
}); | ||
program.parse(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"$schema": "https://biomejs.dev/schemas/1.3.3/schema.json", | ||
"organizeImports": { | ||
"enabled": true | ||
}, | ||
"formatter": { | ||
"indentStyle": "space", | ||
"lineWidth": 120 | ||
}, | ||
"linter": { | ||
"enabled": true, | ||
"rules": { | ||
"recommended": true | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
!http.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import "dotenv/config"; | ||
import express from "express"; | ||
import nacl from "tweetnacl"; | ||
|
||
const PORT = process.env.PORT ?? 3000; | ||
const PUBLIC_KEY = process.env.PUBLIC_KEY ?? "a3cb7366ee8ca77225b4d41772e270e4e831d171d1de71d91707c42e7ba82cc9"; | ||
const app = express(); | ||
|
||
app.use(express.text({ type: "application/json" })); | ||
|
||
app.use(async (req, res) => { | ||
// get headers and body from POST request | ||
const timestamp = req.headers["x-signature-timestamp"]; | ||
const signature = req.headers["x-signature-ed25519"]; | ||
const body = req.body; | ||
|
||
if (!timestamp) return res.send("missing required timestamp in headers").status(400); | ||
if (!signature) return res.send("missing required signature in headers").status(400); | ||
if (!body) return res.send("missing body").status(400); | ||
|
||
// validate signature using public key | ||
const isVerified = nacl.sign.detached.verify( | ||
Buffer.from(timestamp + body), | ||
Buffer.from(signature, "hex"), | ||
Buffer.from(PUBLIC_KEY, "hex"), | ||
); | ||
|
||
console.dir({ timestamp, signature, isVerified }); | ||
console.dir(body); | ||
if (!isVerified) { | ||
return res.send("invalid request signature").status(401); | ||
} | ||
return res.send("OK").status(200); | ||
}); | ||
|
||
app.listen(PORT, () => { | ||
console.log(`Listening on port http://localhost:${PORT}`); | ||
console.log(`Signature validation using ${PUBLIC_KEY}`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.