-
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.
* Update webhook * Update * Disable sideEffects * Update pnpm workspace * Add changeset * Remove packageManager * chore: format
- Loading branch information
Showing
11 changed files
with
170 additions
and
150 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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@pimlico/example": patch | ||
"@pimlico/webhook": patch | ||
--- | ||
|
||
Added webhook secret support |
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
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 @@ | ||
export * from "./src" |
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,40 +1,28 @@ | ||
import * as crypto from "node:crypto" | ||
import { keyFetcher } from "./key-fetcher" | ||
import basex from "base-x" | ||
import { Webhook } from "svix" | ||
import type { PimlicoSponsorshipPolicyWebhookBody } from "./types" | ||
|
||
export const pimlicoWebhookVerifier = | ||
(apiKey: string) => | ||
async (headers: Record<string, string>, body: Buffer) => { | ||
const fetchKey = keyFetcher(apiKey) | ||
const ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvxyz" // no 'w', it's used for padding only | ||
|
||
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 | ||
} | ||
const bs58 = basex(ALPHABET) | ||
|
||
if (!body || body.length === 0) { | ||
throw new Error("invalid webhook: empty payload") | ||
} | ||
export const pimlicoWebhookVerifier = (webhookSecret: string) => { | ||
const webhookSecretHex = Buffer.from( | ||
bs58.decode(webhookSecret.replace("pim_whsec_", "")) | ||
).toString("hex") | ||
|
||
const key = await fetchKey() | ||
const verify = (headers: Record<string, string>, payload: string) => { | ||
const wh = new Webhook(webhookSecretHex) | ||
|
||
const signature = Buffer.from( | ||
headers["pimlico-signature"] ?? "", | ||
"base64" | ||
) | ||
|
||
const message = Buffer.concat([body]) | ||
|
||
if (!crypto.verify("sha256", message, key, signature)) { | ||
throw new Error("invalid webhook: signature validation failed") | ||
if (!payload || payload.length === 0) { | ||
throw new Error("invalid webhook: empty payload") | ||
} | ||
|
||
return JSON.parse( | ||
body.toString() | ||
return wh.verify( | ||
payload, | ||
headers | ||
) as PimlicoSponsorshipPolicyWebhookBody | ||
} | ||
|
||
return verify | ||
} |
This file was deleted.
Oops, something went wrong.
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.