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 (first parameter) must be a string`); | |||||
tobias_ruck: is there a reason for the "first parameter"? since it's an object and not a list | |||||
bytesofmanAuthorUnsubmitted Not Done Inline ActionsIn this case, the user only sees this error msg by calling subscribeToPlugin(pluginName, group) case to be made that all subscription functions should be more like subscribeTo<Thing>(objectOfExpectedShape), e.g. in this case subscribeToPlugin({pluginName: <name>, group: <group>}) -- that is not currently the API tho (subscribeToScript is like this as well). That said ... yeah I don't think "first parameter" is really helping. If we do truly need it, good sign the API needs to be changed. bytesofman: In this case, the user only sees this error msg by calling `subscribeToPlugin(pluginName… | |||||
} | |||||
if (typeof group === 'undefined') { | |||||
throw new Error(`group (second parameter) must be a string`); | |||||
} | |||||
// Test for odd length | |||||
if (group.length % 2 !== 0) { | |||||
throw new Error( | |||||
`group (second parameter) must have even length (complete bytes): "${group}"`, | |||||
tobias_ruckUnsubmitted Done Inline Actionssame here tobias_ruck: same here | |||||
); | |||||
} | |||||
// Test for valid hex | |||||
if (!VALID_HEX_REGEX.test(group)) { | |||||
throw new Error( | |||||
`group (second parameter) must be a valid lowercase hex string: "${group}"`, | |||||
tobias_ruckUnsubmitted Done Inline Actionsand here tobias_ruck: and here | |||||
); | |||||
} | |||||
}; |
is there a reason for the "first parameter"? since it's an object and not a list