Skip to content

Commit

Permalink
refactor: improve webhook types
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Smart committed Dec 5, 2022
1 parent 084c301 commit ac24e73
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/Interactions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class InteractionBuilder<R, E> {
return Gateway.run<R, R2, E, E2>(this.definitions, catchAll, opts)
}

handleWebhook(headers: Record<string, string>, rawBody: string) {
handleWebhook(headers: Webhook.Headers, rawBody: string) {
return Webhook.run(this.definitions, headers, rawBody)
}
}
Expand Down
14 changes: 6 additions & 8 deletions src/Interactions/webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,17 @@ export class BadWebhookSignature {
readonly _tag = "BadWebhookSignature"
}

const checkSignature = (
publicKey: string,
headers: Record<string, string>,
body: string,
) =>
export type Headers = Record<string, string | string[]>

const checkSignature = (publicKey: string, headers: Headers, body: string) =>
Maybe.struct({
signature: Maybe.fromNullable(headers["x-signature-ed25519"]),
timestamp: Maybe.fromNullable(headers["x-signature-timestamp"]),
})
.filter((a) =>
Nacl.sign.detached.verify(
Buffer.from(a.timestamp + body),
Buffer.from(a.signature, "hex"),
Buffer.from(`${a.signature}`, "hex"),
Buffer.from(publicKey, "hex"),
),
)
Expand All @@ -36,7 +34,7 @@ export class WebhookParseError {
constructor(readonly reason: unknown) {}
}

const fromHeadersAndBody = (headers: Record<string, string>, body: string) =>
const fromHeadersAndBody = (headers: Headers, body: string) =>
Do(($) => {
const { publicKey } = $(Effect.service(WebhookConfig))
$(Effect.fromEither(checkSignature(publicKey, headers, body)))
Expand All @@ -50,7 +48,7 @@ const fromHeadersAndBody = (headers: Record<string, string>, body: string) =>

export const run = <R, E>(
definitions: D.InteractionDefinition<R, E>[],
headers: Record<string, string>,
headers: Headers,
body: string,
) =>
Do(($) => {
Expand Down

0 comments on commit ac24e73

Please sign in to comment.