-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from pinax-network/update-webhook-signature
Update to latest `substreams-sink-webhook` signature
- Loading branch information
Showing
8 changed files
with
130 additions
and
93 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
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
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,28 @@ | ||
export type Result<T = undefined, E = Error> = OkResult<T> | ErrResult<E>; | ||
|
||
type OkResult<P = undefined> = { success: true } & (undefined extends P ? {} : { payload: P }); | ||
type ErrResult<E = Error> = { success: false; error: E }; | ||
|
||
export function Ok< | ||
P extends string | number | object | undefined = undefined, | ||
R = P extends undefined ? OkResult : OkResult<P> | ||
>(payload?: P): R { | ||
if (payload !== undefined) { | ||
return { success: true, payload } as R; | ||
} | ||
return { success: true } as R; | ||
} | ||
|
||
export function Err<E = Error>(error: E): ErrResult<E> { | ||
return { success: false, error }; | ||
} | ||
|
||
export function UnknownErr(err: unknown): ErrResult { | ||
if (err instanceof Error) { | ||
return Err(err); | ||
} else if (typeof err === "string") { | ||
return Err(new Error(err)); | ||
} else { | ||
return Err(new Error(JSON.stringify(err))); | ||
} | ||
} |
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,25 @@ | ||
import { PUBLIC_KEYS } from "../config.js"; | ||
import { toText } from "../fetch/cors.js"; | ||
import { Err, Ok, Result } from "../result.js"; | ||
import { cachedVerify } from "substreams-sink-webhook/auth"; | ||
|
||
export async function signatureEd25519(req: Request, text: string): Promise<Result<undefined, Response>> { | ||
const signature = req.headers.get("x-signature-ed25519"); | ||
const expiry = req.headers.get("x-signature-ed25519-expiry"); | ||
const publicKey = req.headers.get("x-signature-ed25519-public-key"); | ||
|
||
if (!signature) return Err(toText("missing required signature in headers", 400)); | ||
if (!expiry) return Err(toText("missing required expiry in headers", 400)); | ||
if (!publicKey) return Err(toText("missing required public key in headers", 400)); | ||
if (!text) return Err(toText("missing body", 400)); | ||
|
||
if (!PUBLIC_KEYS.includes(publicKey)) { | ||
return Err(toText("invalid public key", 401)); | ||
} | ||
|
||
if (!cachedVerify(signature, Number(expiry), publicKey)) { | ||
return Err(toText("invalid request signature", 401)); | ||
} | ||
|
||
return Ok(); | ||
} |