-
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
Showing
14 changed files
with
227 additions
and
208 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,4 +10,4 @@ | |
"path": "./packages/webhook/_cjs/index.js", | ||
"limit": "250 kB" | ||
} | ||
] | ||
] |
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,4 +1,4 @@ | ||
{ | ||
"editor.formatOnSave": true, | ||
"editor.defaultFormatter": "biomejs.biome" | ||
"editor.formatOnSave": true, | ||
"editor.defaultFormatter": "biomejs.biome" | ||
} |
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,41 +1,44 @@ | ||
{ | ||
"$schema": "https://biomejs.dev/schemas/1.9.4/schema.json", | ||
"vcs": { | ||
"enabled": false, | ||
"clientKind": "git", | ||
"useIgnoreFile": false | ||
}, | ||
"files": { | ||
"ignoreUnknown": false, | ||
"ignore": [ | ||
"node_modules", | ||
"**/node_modules", | ||
"cache", | ||
"coverage", | ||
"tsconfig.json", | ||
"tsconfig.*.json", | ||
"_cjs", | ||
"_esm", | ||
"_types", | ||
"bun.lockb" | ||
] | ||
}, | ||
"formatter": { | ||
"enabled": true, | ||
"indentStyle": "tab" | ||
}, | ||
"organizeImports": { | ||
"enabled": true | ||
}, | ||
"linter": { | ||
"enabled": true, | ||
"rules": { | ||
"recommended": true | ||
} | ||
}, | ||
"javascript": { | ||
"formatter": { | ||
"quoteStyle": "double" | ||
} | ||
} | ||
} | ||
"$schema": "https://biomejs.dev/schemas/1.0.0/schema.json", | ||
"files": { | ||
"ignore": [ | ||
"node_modules", | ||
"**/node_modules", | ||
"cache", | ||
"coverage", | ||
"tsconfig.json", | ||
"tsconfig.*.json", | ||
"_cjs", | ||
"_esm", | ||
"_types" | ||
] | ||
}, | ||
"organizeImports": { | ||
"enabled": true | ||
}, | ||
"linter": { | ||
"enabled": true, | ||
"rules": { | ||
"recommended": true, | ||
"suspicious": { | ||
"noExplicitAny": "warn" | ||
}, | ||
"style": { | ||
"noUnusedTemplateLiteral": "warn" | ||
} | ||
} | ||
}, | ||
"formatter": { | ||
"enabled": true, | ||
"formatWithErrors": true, | ||
"lineWidth": 80, | ||
"indentWidth": 4, | ||
"indentStyle": "space" | ||
}, | ||
"javascript": { | ||
"formatter": { | ||
"semicolons": "asNeeded", | ||
"trailingCommas": "none" | ||
} | ||
} | ||
} |
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,14 +1,17 @@ | ||
import type { VercelRequest, VercelResponse } from '@vercel/node' | ||
import { pimlicoWebhookVerifier } from '@pimlico/webhook' | ||
import { pimlicoWebhookVerifier } from "@pimlico/webhook" | ||
import type { VercelRequest, VercelResponse } from "@vercel/node" | ||
|
||
const apiKey = process.env.PIMLICO_API_KEY as string; | ||
const apiKey = process.env.PIMLICO_API_KEY as string | ||
|
||
const verifyWebhook = pimlicoWebhookVerifier(apiKey); | ||
const verifyWebhook = pimlicoWebhookVerifier(apiKey) | ||
|
||
export default async function handler(req: VercelRequest, res: VercelResponse) { | ||
const body = await verifyWebhook(req.headers as Record<string, string>, Buffer.from(JSON.stringify(req.body))); | ||
const body = await verifyWebhook( | ||
req.headers as Record<string, string>, | ||
Buffer.from(JSON.stringify(req.body)) | ||
) | ||
|
||
return res.status(200).json({ | ||
sponsor: true | ||
}) | ||
return res.status(200).json({ | ||
sponsor: 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import type { VercelRequest, VercelResponse } from '@vercel/node' | ||
import type { VercelRequest, VercelResponse } from "@vercel/node" | ||
|
||
export default function handler(req: VercelRequest, res: VercelResponse) { | ||
return res.status(200).send('Invalid response test') | ||
return res.status(200).send("Invalid response test") | ||
} |
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,7 +1,7 @@ | ||
import type { VercelRequest, VercelResponse } from '@vercel/node' | ||
import type { VercelRequest, VercelResponse } from "@vercel/node" | ||
|
||
export default function handler(req: VercelRequest, res: VercelResponse) { | ||
return res.status(403).json({ | ||
sponsor: true, | ||
}) | ||
return res.status(403).json({ | ||
sponsor: 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import type { VercelRequest, VercelResponse } from '@vercel/node' | ||
import type { VercelRequest, VercelResponse } from "@vercel/node" | ||
|
||
export default function handler(req: VercelRequest, res: VercelResponse) { | ||
return res.status(200).json({ | ||
sponsor: false, | ||
}) | ||
return res.status(200).json({ | ||
sponsor: false | ||
}) | ||
} |
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,9 +1,9 @@ | ||
import type { VercelRequest, VercelResponse } from '@vercel/node' | ||
import type { VercelRequest, VercelResponse } from "@vercel/node" | ||
|
||
export default function handler(req: VercelRequest, res: VercelResponse) { | ||
setTimeout(() => { | ||
return res.status(200).json({ | ||
sponsor: true | ||
}); | ||
}, 10_000); | ||
setTimeout(() => { | ||
return res.status(200).json({ | ||
sponsor: true | ||
}) | ||
}, 10_000) | ||
} |
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,18 +1,18 @@ | ||
{ | ||
"name": "@pimlico/example", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"private": true, | ||
"dependencies": { | ||
"@pimlico/webhook": "workspace:*", | ||
"@vercel/node": "^3.1.7", | ||
"vercel": "^34.2.7" | ||
} | ||
} | ||
"name": "@pimlico/example", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"private": true, | ||
"dependencies": { | ||
"@pimlico/webhook": "workspace:*", | ||
"@vercel/node": "^3.1.7", | ||
"vercel": "^34.2.7" | ||
} | ||
} |
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,25 +1,25 @@ | ||
{ | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"lint": "biome check --error-on-warnings .", | ||
"format": "biome format --write .", | ||
"webhook": "pnpm --filter @pimlico/webhook", | ||
"build": "pnpm webhook build", | ||
"clean": "rimraf ./packages/*/node_modules ./packages/*/_cjs ./packages/*/_esm ./packages/*/_types ./packages/*/dist ./packages/*/node_modules ./node_modules" | ||
}, | ||
"private": true, | ||
"keywords": [], | ||
"author": "Pimlico", | ||
"license": "MIT", | ||
"engines": { | ||
"node": ">=20.0.0" | ||
}, | ||
"type": "module", | ||
"devDependencies": { | ||
"@biomejs/biome": "1.9.4", | ||
"@types/node": "^20.11.18", | ||
"rimraf": "^6.0.1", | ||
"tsc-alias": "^1.8.10" | ||
} | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"lint": "biome check --fix --error-on-warnings .", | ||
"format": "biome format --write .", | ||
"webhook": "pnpm --filter @pimlico/webhook", | ||
"build": "pnpm webhook build", | ||
"clean": "rimraf ./packages/*/node_modules ./packages/*/_cjs ./packages/*/_esm ./packages/*/_types ./packages/*/dist ./packages/*/node_modules ./node_modules" | ||
}, | ||
"private": true, | ||
"keywords": [], | ||
"author": "Pimlico", | ||
"license": "MIT", | ||
"engines": { | ||
"node": ">=20.0.0" | ||
}, | ||
"type": "module", | ||
"devDependencies": { | ||
"@biomejs/biome": "1.9.4", | ||
"@types/node": "^20.11.18", | ||
"rimraf": "^6.0.1", | ||
"tsc-alias": "^1.8.10" | ||
} | ||
} |
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,32 +1,32 @@ | ||
{ | ||
"name": "@pimlico/webhook", | ||
"version": "0.0.1", | ||
"author": "Pimlico", | ||
"main": "./_cjs/index.js", | ||
"module": "./_esm/index.js", | ||
"types": "./_types/index.d.ts", | ||
"typings": "./_types/index.d.ts", | ||
"type": "module", | ||
"sideEffects": false, | ||
"description": "A utility library for working with Pimlico webhooks.", | ||
"license": "MIT", | ||
"exports": { | ||
".": { | ||
"types": "./_types/index.d.ts", | ||
"import": "./_esm/index.js", | ||
"default": "./_cjs/index.js" | ||
} | ||
}, | ||
"scripts": { | ||
"build": "pnpm build:cjs && pnpm build:esm", | ||
"build:cjs": "tsc --project ./../../tsconfig/tsconfig.webhook.cjs.json && tsc-alias -p ./../../tsconfig/tsconfig.webhook.cjs.json && printf '{\"type\":\"commonjs\"}' > ./_cjs/package.json", | ||
"build:esm": "tsc --project ./../../tsconfig/tsconfig.webhook.esm.json && tsc-alias -p ./../../tsconfig/tsconfig.webhook.esm.json && printf '{\"type\": \"module\",\"sideEffects\":false}' > ./_esm/package.json" | ||
}, | ||
"devDependencies": { | ||
"typescript": "^5.7.2", | ||
"viem": "^2.21.52" | ||
}, | ||
"peerDependencies": { | ||
"viem": "^2.21.52" | ||
} | ||
} | ||
"name": "@pimlico/webhook", | ||
"version": "0.0.1", | ||
"author": "Pimlico", | ||
"main": "./_cjs/index.js", | ||
"module": "./_esm/index.js", | ||
"types": "./_types/index.d.ts", | ||
"typings": "./_types/index.d.ts", | ||
"type": "module", | ||
"sideEffects": false, | ||
"description": "A utility library for working with Pimlico webhooks.", | ||
"license": "MIT", | ||
"exports": { | ||
".": { | ||
"types": "./_types/index.d.ts", | ||
"import": "./_esm/index.js", | ||
"default": "./_cjs/index.js" | ||
} | ||
}, | ||
"scripts": { | ||
"build": "pnpm build:cjs && pnpm build:esm", | ||
"build:cjs": "tsc --project ./../../tsconfig/tsconfig.webhook.cjs.json && tsc-alias -p ./../../tsconfig/tsconfig.webhook.cjs.json && printf '{\"type\":\"commonjs\"}' > ./_cjs/package.json", | ||
"build:esm": "tsc --project ./../../tsconfig/tsconfig.webhook.esm.json && tsc-alias -p ./../../tsconfig/tsconfig.webhook.esm.json && printf '{\"type\": \"module\",\"sideEffects\":false}' > ./_esm/package.json" | ||
}, | ||
"devDependencies": { | ||
"typescript": "^5.7.2", | ||
"viem": "^2.21.52" | ||
}, | ||
"peerDependencies": { | ||
"viem": "^2.21.52" | ||
} | ||
} |
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,35 +1,40 @@ | ||
import * as crypto from "crypto"; | ||
import type { PimlicoSponsorshipPolicyWebhookBody } from "./types"; | ||
import { keyFetcher } from "./key-fetcher"; | ||
import * as crypto from "node:crypto" | ||
import { keyFetcher } from "./key-fetcher" | ||
import type { PimlicoSponsorshipPolicyWebhookBody } from "./types" | ||
|
||
export const pimlicoWebhookVerifier = (apiKey: string) => async (headers: Record<string, string>, body: Buffer) => { | ||
const fetchKey = keyFetcher(apiKey); | ||
export const pimlicoWebhookVerifier = | ||
(apiKey: string) => | ||
async (headers: Record<string, string>, body: Buffer) => { | ||
const fetchKey = keyFetcher(apiKey) | ||
|
||
if (!Buffer.isBuffer(body)) { | ||
throw new Error("expected body to be a Buffer"); | ||
} | ||
if (!Buffer.isBuffer(body)) { | ||
throw new Error("expected body to be a Buffer") | ||
} | ||
|
||
if (process.env.UNSAFE_SKIP_WEBHOOK_VERIFY != null) { | ||
return JSON.parse((body).toString()) as PimlicoSponsorshipPolicyWebhookBody; | ||
} | ||
if (process.env.UNSAFE_SKIP_WEBHOOK_VERIFY != null) { | ||
return JSON.parse( | ||
body.toString() | ||
) as PimlicoSponsorshipPolicyWebhookBody | ||
} | ||
|
||
if (!body || body.length == 0) { | ||
throw new Error("invalid webhook: empty payload"); | ||
} | ||
if (!body || body.length === 0) { | ||
throw new Error("invalid webhook: empty payload") | ||
} | ||
|
||
const key = await fetchKey(); | ||
const key = await fetchKey() | ||
|
||
const signature = Buffer.from(headers["pimlico-signature"] ?? "", "base64"); | ||
const signature = Buffer.from( | ||
headers["pimlico-signature"] ?? "", | ||
"base64" | ||
) | ||
|
||
const message = Buffer.concat( | ||
[ | ||
body | ||
], | ||
); | ||
const message = Buffer.concat([body]) | ||
|
||
if (!crypto.verify("sha256", message, key, signature)) { | ||
throw new Error("invalid webhook: signature validation failed"); | ||
} | ||
if (!crypto.verify("sha256", message, key, signature)) { | ||
throw new Error("invalid webhook: signature validation failed") | ||
} | ||
|
||
return JSON.parse(body.toString()) as PimlicoSponsorshipPolicyWebhookBody; | ||
}; | ||
return JSON.parse( | ||
body.toString() | ||
) as PimlicoSponsorshipPolicyWebhookBody | ||
} |
Oops, something went wrong.