Changeset View
Changeset View
Standalone View
Standalone View
modules/chronik-client/src/validation.ts
| // Copyright (c) 2024 The Bitcoin developers | // Copyright (c) 2024 The Bitcoin developers | ||||
| // Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
| // file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
| import { WsSubScriptClient } from './ChronikClient'; | import { WsSubScriptClient, WsSubPluginClient } from './ChronikClient'; | ||||
| const VALID_HEX_REGEX = new RegExp(/^[a-f0-9]+$/); | const VALID_HEX_REGEX = new RegExp(/^[a-f0-9]+$/); | ||||
| const VALID_LOKADID_REGEX = new RegExp(/^[a-f0-9]{8}$/); | const VALID_LOKADID_REGEX = new RegExp(/^[a-f0-9]{8}$/); | ||||
| const VALID_TOKENID_REGEX = new RegExp(/^[a-f0-9]{64}$/); | const VALID_TOKENID_REGEX = new RegExp(/^[a-f0-9]{64}$/); | ||||
| export const isValidWsSubscription = ( | export const isValidWsSubscription = ( | ||||
| subscription: WsSubScriptClient, | subscription: WsSubScriptClient, | ||||
| ): string | boolean => { | ): string | boolean => { | ||||
| ▲ Show 20 Lines • Show All 46 Lines • ▼ Show 20 Lines | |||||
| export const verifyTokenId = (tokenId: string) => { | export const verifyTokenId = (tokenId: string) => { | ||||
| if (!VALID_TOKENID_REGEX.test(tokenId)) { | if (!VALID_TOKENID_REGEX.test(tokenId)) { | ||||
| throw new Error( | throw new Error( | ||||
| `Invalid tokenId: "${tokenId}". tokenId must be 64 characters of lowercase hex.`, | `Invalid tokenId: "${tokenId}". tokenId must be 64 characters of lowercase hex.`, | ||||
| ); | ); | ||||
| } | } | ||||
| }; | }; | ||||
| // Tested in test/integration/plugins.ts | |||||
| export const verifyPluginSubscription = ( | |||||
| pluginSubscription: WsSubPluginClient, | |||||
| ) => { | |||||
| const { pluginName, group } = pluginSubscription; | |||||
| if (typeof pluginName === 'undefined') { | |||||
| throw new Error(`pluginName must be a string`); | |||||
| } | |||||
| if (typeof group === 'undefined') { | |||||
| throw new Error(`group must be a string`); | |||||
| } | |||||
| // Test for odd length | |||||
| if (group.length % 2 !== 0) { | |||||
| throw new Error( | |||||
| `group must have even length (complete bytes): "${group}"`, | |||||
| ); | |||||
| } | |||||
| // Test for valid hex | |||||
| if (!VALID_HEX_REGEX.test(group)) { | |||||
| throw new Error( | |||||
| `group must be a valid lowercase hex string: "${group}"`, | |||||
| ); | |||||
| } | |||||
| }; | |||||