From 9da3b45cb8668b0f486956265f6afd71ca2a8211 Mon Sep 17 00:00:00 2001 From: nickfrosty <75431177+nickfrosty@users.noreply.github.com> Date: Fri, 2 Aug 2024 12:39:23 -0400 Subject: [PATCH 1/2] feat: added helpers --- packages/solana-actions/src/constants.ts | 17 ++++++++-- packages/solana-actions/src/index.ts | 1 + packages/solana-actions/src/utils.ts | 41 ++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 packages/solana-actions/src/utils.ts diff --git a/packages/solana-actions/src/constants.ts b/packages/solana-actions/src/constants.ts index 61d13c0..af337f7 100644 --- a/packages/solana-actions/src/constants.ts +++ b/packages/solana-actions/src/constants.ts @@ -13,17 +13,28 @@ export const MEMO_PROGRAM_ID = "MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr"; */ export const BLINKS_QUERY_PARAM = "action"; +/** + * Blockchain IDs for Solana from CAIP + * + * @see https://namespaces.chainagnostic.org/solana/caip10 + */ +export const BLOCKCHAIN_IDS = { + mainnet: "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp", + devnet: "solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1", + testnet: "solana:4uhcVJyU9pJkvQyS88uRDiswHXSCkY3z", +}; + /** * Standard headers for use within frameworks that use the native `HeadersInit` (like NextJS) * * Note: `Access-Control-Allow-Origin=*` should ONLY be set on your Actions API routes and `actions.json`. * Setting "allow origin to any" on other routes on your server is bad practice and should be avoided. */ -export const ACTIONS_CORS_HEADERS: HeadersInit = { +export const ACTIONS_CORS_HEADERS: Record = { "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Methods": "GET,POST,PUT,OPTIONS", "Access-Control-Allow-Headers": - "Content-Type, Authorization, Content-Encoding, Accept-Encoding", + "Content-Type, Authorization, Content-Encoding, Accept-Encoding, X-Action-Version, X-Blockchain-Ids", "Content-Type": "application/json", }; @@ -42,5 +53,7 @@ export const ACTIONS_CORS_HEADERS_MIDDLEWARE = { "Authorization", "Content-Encoding", "Accept-Encoding", + "X-Action-Version", + "X-Blockchain-Ids", ], }; diff --git a/packages/solana-actions/src/index.ts b/packages/solana-actions/src/index.ts index fcbd345..ea3ed21 100644 --- a/packages/solana-actions/src/index.ts +++ b/packages/solana-actions/src/index.ts @@ -1,5 +1,6 @@ export * from "./types.js"; export * from "./constants.js"; +export * from "./utils.js"; export * from "./encodeURL.js"; export * from "./parseURL.js"; diff --git a/packages/solana-actions/src/utils.ts b/packages/solana-actions/src/utils.ts new file mode 100644 index 0000000..26042cf --- /dev/null +++ b/packages/solana-actions/src/utils.ts @@ -0,0 +1,41 @@ +import { ACTIONS_CORS_HEADERS, BLOCKCHAIN_IDS } from "./constants.js"; + +type HeaderHelperArgs = { + headers?: typeof ACTIONS_CORS_HEADERS; + chainId?: keyof typeof BLOCKCHAIN_IDS | string; + actionVersion?: string | number; +}; + +/** + * Construct valid headers for use with Action APIs + */ +export function actionHeaders({ + headers, + chainId, + actionVersion, +}: HeaderHelperArgs) { + if (chainId) { + headers = Object.assign({}, headers || {}, { + "X-Blockchain-Ids": Object.hasOwn(BLOCKCHAIN_IDS, chainId) + ? BLOCKCHAIN_IDS[chainId as keyof typeof BLOCKCHAIN_IDS] + : chainId, + }); + } + if (actionVersion) { + headers = Object.assign({}, headers || {}, { + "X-Action-Version": actionVersion.toString(), + }); + } + if (headers) return Object.assign({}, ACTIONS_CORS_HEADERS, headers); + else return ACTIONS_CORS_HEADERS; +} + +/** + * Middleware to enable proper CORS headers for Action APIs + */ +export function actionCorsMiddleware(args: HeaderHelperArgs) { + return (_req: any, res: any, next: Function) => { + res.set(actionHeaders(args)); + next(); + }; +} From c837013a8ee109a71cd15bacf7b2921859a63b86 Mon Sep 17 00:00:00 2001 From: nickfrosty <75431177+nickfrosty@users.noreply.github.com> Date: Fri, 2 Aug 2024 12:44:20 -0400 Subject: [PATCH 2/2] fix: tsconfig update --- packages/solana-actions/tsconfig.base.json | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/solana-actions/tsconfig.base.json b/packages/solana-actions/tsconfig.base.json index baeb8a3..f02e27f 100644 --- a/packages/solana-actions/tsconfig.base.json +++ b/packages/solana-actions/tsconfig.base.json @@ -1,6 +1,7 @@ { "include": [], "compilerOptions": { + "lib": ["esnext", "dom"], "target": "ESNext", "module": "ESNext", "moduleResolution": "Node",